-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm.c
163 lines (123 loc) · 4.69 KB
/
comm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* File: comm.c
* Author: D.W.
*
* Created: 9.5.2015
* Project: BLDC driver v3.0
*/
/******************************************************************************/
/*Files to Include */
/******************************************************************************/
#include "user.h"
#include "system.h"
#if defined(__XC)
#include <xc.h> /* XC8 General Include File */
#elif defined(HI_TECH_C)
#include <htc.h> /* HiTech General Include File */
#elif defined(__18CXX)
#include <p18cxxx.h> /* C18 General Include File */
#endif
#if defined(__XC) || defined(HI_TECH_C)
#include <stdio.h> /* sprintf() */
#endif
/******************************************************************************/
/*Variables */
/******************************************************************************/
// Requests
extern unsigned int req_current;
extern unsigned char req_motor_mode;
extern int req_velocity;
// Status
extern unsigned int dutycycle;
// Flags
extern unsigned char flags_status;
bit flag_SPI_data_rdy = 0; // new SPI data recieved
/******************************************************************************/
/* Communication routines */
/******************************************************************************/
/********** SPI *******************************************************/
/* Decode received SPI data and set request variables */
void SPI_request_update (void){
unsigned char buff = 0;
/* Load Current request value */
// Calculate internal current reference from received value
// input value is 0-255 ~ 0-25.5 A, internal is 0-511 ~ 0-25 A
req_current = (RX_tab[RX_CURRENT_REQ])<<1;
TX_tab[TX_CURRENT_REQ] = RX_tab[RX_CURRENT_REQ];
/* Load motor mode request */
// TODO: add regen braking
buff = RX_tab[RX_MOTOR_MODE];
switch(buff){ // set appropriate motor mode bits
case SPI_free_run : { // free run requested
req_motor_mode = mode_free_run; break;
}
case SPI_CW: { // motor CW requested
req_motor_mode = mode_motor_CW; break;
}
case SPI_CCW : { // motor CCW requested
req_motor_mode = mode_motor_CCW; break;
}
case SPI_regen : { // regenerative braking
req_motor_mode = mode_regen; break;
}
default : { // unknown command
req_motor_mode = mode_free_run; motor_halt(); break;
}
}
flag_SPI_data_rdy = 0;
}
/* Receive new SPI data and store to RX_tab */
unsigned char Receive_SPI_data(unsigned char length){
getsSPI(RX_tab,length);
SSPBUF = 0;
flag_SPI_data_rdy = 1;
return(0);
}
/* Transmit data from TX_tab over SPI */
unsigned char Transmit_SPI_data(unsigned char length){
unsigned char count, tmp;
TRISDbits.RD1 = 0; // SDO as output
for(count=0;count<8;count++){
SSPBUF=TX_tab[count];
while(!SSPSTATbits.BF && !SLAVE_SELECT );
}
TRISDbits.RD1 = 1; // SDO as input
tmp = SSPBUF; // clear buffer
return(0);
}
void calc_tx_data(void) {
float f_tmp = 0;
int i_tmp = 0;
// Calculate dutycycle percentage
i_tmp = (PTPERH<<8) + PTPERL; // calculate dutycycle from PWM period
i_tmp = (i_tmp << 2); // now, get the real value from it
f_tmp = dutycycle/(i_tmp / 100);
TX_tab[TX_DTC] = (unsigned char) f_tmp;
// Add status flags to TX_tab
TX_tab[TX_STATUS_BYTE] = flags_status;
}
/********** UART *******************************************************/
#ifdef DEBUG_STATUS
void debug_status(void){
char USART_dbg_msg[15];
sprintf(USART_dbg_msg,"\nREQ:%d\n\r",req_current);
putsUSART((char *)USART_dbg_msg);
sprintf(USART_dbg_msg,"STA:%d\n\r",status.current);
putsUSART((char *)USART_dbg_msg);
sprintf(USART_dbg_msg,"DTC:%d\n\r",dutycycle);
putsUSART((char *)USART_dbg_msg);
sprintf(USART_dbg_msg,"M_TEMP:%d C\n\r",status.motor_temp);
putsUSART((char *)USART_dbg_msg);
sprintf(USART_dbg_msg,"T_TEMP:%d C\n\r",status.transistor_temp);
putsUSART((char *)USART_dbg_msg);
sprintf(USART_dbg_msg,"BATT:%d dV\n\r",status.batt_voltage);
putsUSART((char *)USART_dbg_msg);
sprintf(USART_dbg_msg,"Veloc:%d rpm\n\r",60*status.velocity);
putsUSART((char *)USART_dbg_msg);
if(flags_error){
char USART_dbg_msg[]="OpCond exceeded!\n\r";
putsUSART((char *)USART_dbg_msg);
}
return;
}
#endif