Rev 1751 | Rev 1769 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1751 | Rev 1764 | ||
|---|---|---|---|
| Line 14... | Line 14... | ||
| 14 | }; |
14 | }; |
| 15 | #endif |
15 | #endif |
| 16 | 16 | ||
| 17 | // internal variables |
17 | // internal variables |
| 18 | static uint32_t baudRate = 9600; |
18 | static uint32_t baudRate = 9600; |
| 19 |
|
19 | //static uint16_t format = CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK; |
| - | 20 | static uint16_t format = CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS232; |
|
| 20 | static uint8_t databits = 8; |
21 | static uint8_t databits = 8; |
| 21 | static uint8_t stopbits = 1; |
22 | static uint8_t stopbits = 1; |
| 22 | 23 | ||
| 23 | static enum { |
24 | static enum { |
| 24 | PARITY_NONE = 0, |
25 | PARITY_NONE = 0, |
| 25 | PARITY_EVEN = 1, |
26 | PARITY_EVEN = 1, |
| 26 | PARITY_ODD = 2, |
27 | PARITY_ODD = 2, |
| 27 | } parityMode; |
28 | } parityMode = 1; |
| - | 29 | ||
| - | 30 | // filter variables |
|
| - | 31 | static uint8_t dataTimeout = 0; |
|
| - | 32 | static uint8_t packetLength = 0; |
|
| - | 33 | ||
| - | 34 | static struct __attribute__ ((packed)) { |
|
| - | 35 | uint8_t prefixLength:2; |
|
| - | 36 | uint8_t suffixLength:2; |
|
| - | 37 | uint8_t prefixPattern[3]; |
|
| - | 38 | uint8_t suffixPattern[3]; |
|
| - | 39 | } filter; |
|
| 28 | 40 | ||
| 29 | // prepare a new message, in case new data is available |
41 | // prepare a new message, in case new data is available |
| 30 | static StdCan_Msg_t msg; |
42 | static StdCan_Msg_t msg; |
| 31 | 43 | ||
| 32 | #define sns_Serial_DEBUG 0 |
44 | #define sns_Serial_DEBUG 0 |
| 33 | 45 | ||
| 34 | 46 | ||
| 35 | #ifndef min |
47 | #ifndef min |
| 36 | #define min(_a,_b) ((_a)<(_b) ? (_a) : (_b)) |
48 | #define min(_a,_b) ((_a)<(_b) ? (_a) : (_b)) |
| 37 | #endif |
49 | #endif |
| 38 | 50 | ||
| 39 | #ifndef max |
51 | #ifndef max |
| 40 | #define max(_a,_b) ((_a)>(_b) ? (_a) : (_b)) |
52 | #define max(_a,_b) ((_a)>(_b) ? (_a) : (_b)) |
| 41 | #endif |
53 | #endif |
| 42 | 54 | ||
| 43 | #ifndef lim |
55 | #ifndef lim |
| 44 | #define lim(_a,_minval,_maxval) (max(min((_a),(_maxval)),(_minval))) |
56 | #define lim(_a,_minval,_maxval) (max(min((_a),(_maxval)),(_minval))) |
| 45 | #endif |
57 | #endif |
| 46 | 58 | ||
| 47 | 59 | ||
| 48 | 60 | ||
| 49 | uint8_t sns_Serial_setSettings(void) |
61 | uint8_t sns_Serial_setSettings(void) |
| 50 | { |
62 | { |
| Line 52... | Line 64... | ||
| 52 | 64 | ||
| 53 | if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS232) |
65 | if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS232) |
| 54 | { |
66 | { |
| 55 | #if sns_Serial_DEBUG==1 |
67 | #if sns_Serial_DEBUG==1 |
| 56 | printf("RS232\n"); |
68 | printf("RS232\n"); |
| 57 | #endif |
69 | #endif |
| 58 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
70 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
| 59 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
71 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
| 60 | gpio_clr_pin(sns_Serial_485_232); // RS232 mode |
72 | gpio_clr_pin(sns_Serial_485_232); // RS232 mode |
| 61 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
73 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
| 62 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
74 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
| 63 | } |
75 | } |
| 64 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485FULLDUPLEX) |
76 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485FULLDUPLEX) |
| Line 114... | Line 126... | ||
| 114 | else |
126 | else |
| 115 | { |
127 | { |
| 116 | //invalid format |
128 | //invalid format |
| 117 | #if sns_Serial_DEBUG==1 |
129 | #if sns_Serial_DEBUG==1 |
| 118 | printf("ERROR!\n"); |
130 | printf("ERROR!\n"); |
| 119 | #endif |
131 | #endif |
| 120 | //return 0, not successful |
132 | //return 0, not successful |
| 121 | returnval = 0; |
133 | returnval = 0; |
| 122 | } |
134 | } |
| 123 | 135 | ||
| 124 | return returnval; |
136 | return returnval; |
| 125 | } |
137 | } |
| 126 | 138 | ||
| 127 | void sns_Serial_Init(void) |
139 | void sns_Serial_Init(void) |
| 128 | { |
140 | { |
| 129 | #ifdef sns_Serial_USEEEPROM |
141 | #ifdef sns_Serial_USEEEPROM |
| 130 |
|
142 | //if (EEDATA_OK) { |
| - | 143 | if (0) { |
|
| 131 | #if ((__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >=2)||(__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1) |
144 | #if ((__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >=2)||(__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1) |
| 132 | baudRate = eeprom_read_dword(EEDATA32.baudRate); |
145 | baudRate = eeprom_read_dword(EEDATA32.baudRate); |
| 133 | #else |
146 | #else |
| 134 | #warning This version of AVR-libc does not have support for eeprom read dword |
147 | #warning This version of AVR-libc does not have support for eeprom read dword |
| 135 | #endif |
148 | #endif |
| 136 | format = eeprom_read_word(EEDATA16.format); |
149 | format = eeprom_read_word(EEDATA16.format); |
| 137 | databits = eeprom_read_byte(EEDATA.databits); |
150 | databits = eeprom_read_byte(EEDATA.databits); |
| 138 | stopbits = eeprom_read_byte(EEDATA.stopbits); |
151 | stopbits = eeprom_read_byte(EEDATA.stopbits); |
| 139 | parityMode = eeprom_read_byte(EEDATA.parityMode); |
152 | parityMode = eeprom_read_byte(EEDATA.parityMode); |
| - | 153 | dataTimeout = eeprom_read_byte(EEDATA.dataTimeout); |
|
| - | 154 | packetLength = eeprom_read_byte(EEDATA.packetLength); |
|
| - | 155 | filter.prefixLength = lim(eeprom_read_byte(EEDATA.prefixLength), 0, 3); |
|
| - | 156 | filter.suffixLength = lim(eeprom_read_byte(EEDATA.suffixLength), 0, 3); |
|
| - | 157 | filter.prefixPattern[0] = eeprom_read_byte(EEDATA.prefixPattern[0]); |
|
| - | 158 | filter.prefixPattern[1] = eeprom_read_byte(EEDATA.prefixPattern[1]); |
|
| - | 159 | filter.prefixPattern[2] = eeprom_read_byte(EEDATA.prefixPattern[2]); |
|
| - | 160 | filter.suffixPattern[0] = eeprom_read_byte(EEDATA.suffixPattern[0]); |
|
| - | 161 | filter.suffixPattern[1] = eeprom_read_byte(EEDATA.suffixPattern[1]); |
|
| - | 162 | filter.suffixPattern[2] = eeprom_read_byte(EEDATA.suffixPattern[2]); |
|
| 140 | } |
163 | } |
| 141 | else { |
164 | else { |
| 142 | //The CRC of the EEPROM is not correct, store default values and update CRC |
165 | //The CRC of the EEPROM is not correct, store default values and update CRC |
| 143 | #if ((__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >=2)||(__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1) |
166 | #if ((__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >=2)||(__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1) |
| 144 | eeprom_write_dword_crc(EEDATA32.baudRate, 9600, WITHOUT_CRC); |
167 | eeprom_write_dword_crc(EEDATA32.baudRate, 9600, WITHOUT_CRC); |
| Line 147... | Line 170... | ||
| 147 | #endif |
170 | #endif |
| 148 | eeprom_write_word_crc(EEDATA16.format, CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK, WITHOUT_CRC); |
171 | eeprom_write_word_crc(EEDATA16.format, CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK, WITHOUT_CRC); |
| 149 | eeprom_write_byte_crc(EEDATA.databits, 8, WITHOUT_CRC); |
172 | eeprom_write_byte_crc(EEDATA.databits, 8, WITHOUT_CRC); |
| 150 | eeprom_write_byte_crc(EEDATA.stopbits, 1, WITHOUT_CRC); |
173 | eeprom_write_byte_crc(EEDATA.stopbits, 1, WITHOUT_CRC); |
| 151 | eeprom_write_byte_crc(EEDATA.parityMode, PARITY_NONE, WITHOUT_CRC); |
174 | eeprom_write_byte_crc(EEDATA.parityMode, PARITY_NONE, WITHOUT_CRC); |
| - | 175 | eeprom_write_byte_crc(EEDATA.dataTimeout, 0, WITHOUT_CRC); |
|
| - | 176 | eeprom_write_byte_crc(EEDATA.packetLength, 8, WITHOUT_CRC); |
|
| - | 177 | eeprom_write_byte_crc(EEDATA.prefixLength, 0, WITHOUT_CRC); |
|
| - | 178 | eeprom_write_byte_crc(EEDATA.suffixLength, 0, WITHOUT_CRC); |
|
| - | 179 | eeprom_write_byte_crc(EEDATA.prefixPattern[0], 0, WITHOUT_CRC); |
|
| - | 180 | eeprom_write_byte_crc(EEDATA.prefixPattern[1], 0, WITHOUT_CRC); |
|
| - | 181 | eeprom_write_byte_crc(EEDATA.prefixPattern[2], 0, WITHOUT_CRC); |
|
| - | 182 | eeprom_write_byte_crc(EEDATA.suffixPattern[0], 0, WITHOUT_CRC); |
|
| - | 183 | eeprom_write_byte_crc(EEDATA.suffixPattern[1], 0, WITHOUT_CRC); |
|
| - | 184 | eeprom_write_byte_crc(EEDATA.suffixPattern[2], 0, WITHOUT_CRC); |
|
| 152 | EEDATA_UPDATE_CRC; |
185 | EEDATA_UPDATE_CRC; |
| 153 | } |
186 | } |
| 154 | #endif |
187 | #endif |
| 155 | uart_setDatabits(databits); |
188 | uart_setDatabits(databits); |
| 156 | uart_setStopbits(stopbits); |
189 | uart_setStopbits(stopbits); |
| 157 | uart_setParity(parityMode!=PARITY_NONE, parityMode==PARITY_ODD); |
190 | uart_setParity(parityMode!=PARITY_NONE, parityMode==PARITY_ODD); |
| 158 | // default baudrate |
- | |
| 159 | uart_init(UART_BAUD_SELECT(baudRate, F_CPU)); |
191 | uart_init(UART_BAUD_SELECT(baudRate, F_CPU)); |
| 160 | 192 | ||
| 161 | // configure control pins to outputs |
193 | // configure control pins to outputs |
| 162 | gpio_set_out(sns_Serial_RXEN); |
194 | gpio_set_out(sns_Serial_RXEN); |
| 163 | gpio_set_out(sns_Serial_TXEN); |
195 | gpio_set_out(sns_Serial_TXEN); |
| Line 171... | Line 203... | ||
| 171 | #if sns_Serial_DEBUG==1 |
203 | #if sns_Serial_DEBUG==1 |
| 172 | printf("Serial started!\n"); |
204 | printf("Serial started!\n"); |
| 173 | #endif |
205 | #endif |
| 174 | 206 | ||
| 175 | msg.Length = 0; // no data so far |
207 | msg.Length = 0; // no data so far |
| - | 208 | ||
| - | 209 | //gpio_set_pin(sns_Serial_TXEN); // enable TX |
|
| - | 210 | //uart_putc('7'); |
|
| 176 | } |
211 | } |
| 177 | 212 | ||
| 178 | void sns_Serial_Process(void) |
213 | void sns_Serial_Process(void) |
| 179 | { |
214 | { |
| 180 | StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS); |
215 | StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS); |
| Line 207... | Line 242... | ||
| 207 | #if sns_Serial_FORCE_SEND_TIME_MS > 0 |
242 | #if sns_Serial_FORCE_SEND_TIME_MS > 0 |
| 208 | Timer_SetTimeout(sns_Serial_FORCE_SEND_TIMER, sns_Serial_FORCE_SEND_TIME_MS , TimerTypeOneShot, 0); |
243 | Timer_SetTimeout(sns_Serial_FORCE_SEND_TIMER, sns_Serial_FORCE_SEND_TIME_MS , TimerTypeOneShot, 0); |
| 209 | #endif |
244 | #endif |
| 210 | } |
245 | } |
| 211 | // keep going until max data length reached, or until uart RXBUF is empty |
246 | // keep going until max data length reached, or until uart RXBUF is empty |
| 212 | } while (status == 0 && msg.Length < |
247 | } while (status == 0 && msg.Length < packetLength); |
| 213 | 248 | ||
| 214 | // check if force send or send-frame full |
249 | // check if force send or send-frame full |
| 215 | if ((Timer_Expired(sns_Serial_FORCE_SEND_TIMER) && msg.Length>0) || msg.Length == |
250 | if ((Timer_Expired(sns_Serial_FORCE_SEND_TIMER) && msg.Length>0) || msg.Length == packetLength || (sns_Serial_FORCE_SEND_TIME_MS == 0 && msg.Length>0)) |
| 216 | { |
251 | { |
| 217 | // transmit this chunk of data (max 8 chars) |
252 | // transmit this chunk of data (max 8 chars) |
| 218 | while(StdCan_Put(&msg) != StdCan_Ret_OK); |
253 | while(StdCan_Put(&msg) != StdCan_Ret_OK); |
| 219 | msg.Length = 0; // no data so far |
254 | msg.Length = 0; // no data so far |
| 220 | } |
255 | } |
| Line 238... | Line 273... | ||
| 238 | { |
273 | { |
| 239 | uart_putc(rxMsg->Data[i]); |
274 | uart_putc(rxMsg->Data[i]); |
| 240 | } |
275 | } |
| 241 | /* TODO Cannot disable TX yet, the data has only been placed in buffer but not sent */ |
276 | /* TODO Cannot disable TX yet, the data has only been placed in buffer but not sent */ |
| 242 | // gpio_clr_pin(sns_Serial_TXEN); // disable TX |
277 | // gpio_clr_pin(sns_Serial_TXEN); // disable TX |
| 243 | break; |
278 | break; |
| 244 | 279 | ||
| 245 | case CAN_MODULE_CMD_SERIAL_SERIALCONFIG: |
280 | case CAN_MODULE_CMD_SERIAL_SERIALCONFIG: |
| 246 | baudRate = 10 * (((uint32_t)rxMsg->Data[1] << 0) | ((uint32_t)rxMsg->Data[0] << 8)); |
281 | baudRate = 10 * (((uint32_t)rxMsg->Data[1] << 0) | ((uint32_t)rxMsg->Data[0] << 8)); |
| 247 | format = (uint16_t)rxMsg->Data[2]; |
282 | format = (uint16_t)rxMsg->Data[2]; |
| 248 | databits = lim(rxMsg->Data[3], 5, 9); |
283 | databits = lim(rxMsg->Data[3], 5, 9); |
| Line 250... | Line 285... | ||
| 250 | parityMode = lim(rxMsg->Data[5], 0, 2); |
285 | parityMode = lim(rxMsg->Data[5], 0, 2); |
| 251 | uart_setDatabits(databits); |
286 | uart_setDatabits(databits); |
| 252 | uart_setStopbits(stopbits); |
287 | uart_setStopbits(stopbits); |
| 253 | uart_setParity(parityMode!=PARITY_NONE, parityMode==PARITY_ODD); |
288 | uart_setParity(parityMode!=PARITY_NONE, parityMode==PARITY_ODD); |
| 254 | uart_init(UART_BAUD_SELECT(baudRate, F_CPU)); |
289 | uart_init(UART_BAUD_SELECT(baudRate, F_CPU)); |
| 255 | 290 | ||
| 256 | uint8_t returnval = sns_Serial_setSettings(); |
291 | uint8_t returnval = sns_Serial_setSettings(); |
| 257 | 292 | ||
| 258 | if (returnval) |
293 | if (returnval) |
| 259 | { |
294 | { |
| 260 | // update EEPROM data |
295 | // update EEPROM data |
| 261 | #ifdef sns_Serial_USEEEPROM |
296 | #ifdef sns_Serial_USEEEPROM |
| 262 | #if ((__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >=2)||(__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1) |
297 | #if ((__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >=2)||(__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1) |
| 263 | eeprom_write_dword_crc(EEDATA32.baudRate, baudRate, WITHOUT_CRC); |
298 | eeprom_write_dword_crc(EEDATA32.baudRate, baudRate, WITHOUT_CRC); |
| 264 | #else |
299 | #else |
| 265 | #warning This version of AVR-libc does not have support for eeprom read dword |
300 | #warning This version of AVR-libc does not have support for eeprom read dword |
| 266 | #endif |
301 | #endif |
| 267 | eeprom_write_word_crc(EEDATA16.format, format, WITHOUT_CRC); |
302 | eeprom_write_word_crc(EEDATA16.format, format, WITHOUT_CRC); |
| 268 | eeprom_write_byte_crc(EEDATA.databits, databits, WITHOUT_CRC); |
303 | eeprom_write_byte_crc(EEDATA.databits, databits, WITHOUT_CRC); |
| 269 | eeprom_write_byte_crc(EEDATA.stopbits, stopbits, WITHOUT_CRC); |
304 | eeprom_write_byte_crc(EEDATA.stopbits, stopbits, WITHOUT_CRC); |
| 270 | eeprom_write_byte_crc(EEDATA.parityMode, parityMode, WITHOUT_CRC); |
305 | eeprom_write_byte_crc(EEDATA.parityMode, parityMode, WITHOUT_CRC); |
| 271 | EEDATA_UPDATE_CRC; |
306 | EEDATA_UPDATE_CRC; |
| 272 | #endif |
307 | #endif |
| 273 | } |
308 | } |
| - | 309 | break; |
|
| - | 310 | ||
| - | 311 | case CAN_MODULE_CMD_SERIAL_SERIALFILTER: |
|
| - | 312 | dataTimeout = rxMsg->Data[0]; |
|
| - | 313 | filter.prefixLength = ((rxMsg->Data[1] & 0x0F) >> 0); |
|
| - | 314 | filter.suffixLength = ((rxMsg->Data[1] & 0x30) >> 4); |
|
| - | 315 | packetLength = ((rxMsg->Data[1] & 0xC0) >> 6); |
|
| - | 316 | filter.prefixPattern[2] = rxMsg->Data[2]; |
|
| - | 317 | filter.prefixPattern[1] = rxMsg->Data[3]; |
|
| - | 318 | filter.prefixPattern[0] = rxMsg->Data[4]; |
|
| - | 319 | filter.suffixPattern[2] = rxMsg->Data[5]; |
|
| - | 320 | filter.suffixPattern[1] = rxMsg->Data[6]; |
|
| - | 321 | filter.suffixPattern[0] = rxMsg->Data[7]; |
|
| - | 322 | ||
| - | 323 | // update EEPROM data |
|
| - | 324 | eeprom_write_byte_crc(EEDATA.dataTimeout, dataTimeout, WITHOUT_CRC); |
|
| - | 325 | eeprom_write_byte_crc(EEDATA.packetLength, packetLength, WITHOUT_CRC); |
|
| - | 326 | eeprom_write_byte_crc(EEDATA.prefixLength, filter.prefixLength, WITHOUT_CRC); |
|
| - | 327 | eeprom_write_byte_crc(EEDATA.suffixLength, filter.suffixLength, WITHOUT_CRC); |
|
| - | 328 | eeprom_write_byte_crc(EEDATA.prefixPattern[0], filter.prefixPattern[0], WITHOUT_CRC); |
|
| - | 329 | eeprom_write_byte_crc(EEDATA.prefixPattern[1], filter.prefixPattern[1], WITHOUT_CRC); |
|
| - | 330 | eeprom_write_byte_crc(EEDATA.prefixPattern[2], filter.prefixPattern[2], WITHOUT_CRC); |
|
| - | 331 | eeprom_write_byte_crc(EEDATA.suffixPattern[0], filter.suffixPattern[0], WITHOUT_CRC); |
|
| - | 332 | eeprom_write_byte_crc(EEDATA.suffixPattern[1], filter.suffixPattern[1], WITHOUT_CRC); |
|
| - | 333 | eeprom_write_byte_crc(EEDATA.suffixPattern[2], filter.suffixPattern[2], WITHOUT_CRC); |
|
| - | 334 | EEDATA_UPDATE_CRC; |
|
| - | 335 | ||
| 274 | break; |
336 | break; |
| 275 | } |
337 | } |
| 276 | } |
338 | } |
| 277 | } |
339 | } |
| 278 | 340 | ||