Rev 1560 | Rev 1575 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1449 | myhrman | 1 | |
| 2 | #include "sns_Serial.h" |
||
| 3 | |||
| 4 | #ifdef sns_Serial_USEEEPROM |
||
| 5 | #include "sns_Serial_eeprom.h" |
||
| 6 | struct eeprom_sns_Serial EEMEM eeprom_sns_Serial = |
||
| 7 | { |
||
| 8 | { |
||
| 9 | ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file. |
||
| 10 | 0xAB, // x |
||
| 11 | 0x1234 // y |
||
| 12 | }, |
||
| 13 | |||
| 14 | }; |
||
| 15 | #endif |
||
| 16 | |||
| 1459 | arune | 17 | // internal variables |
| 1475 | myhrman | 18 | static uint32_t baudRate = 9600; |
| 19 | static uint16_t format = CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK; |
||
| 1459 | arune | 20 | |
| 1559 | arune | 21 | // prepare a new message, in case new data is available |
| 22 | static StdCan_Msg_t msg; |
||
| 23 | |||
| 1536 | arune | 24 | #define sns_Serial_DEBUG 0 |
| 25 | |||
| 1561 | arune | 26 | |
| 27 | |||
| 28 | uint8_t sns_Serial_setSettings(void) |
||
| 29 | { |
||
| 30 | uint8_t returnval = 1; |
||
| 31 | |||
| 32 | if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS232) |
||
| 33 | { |
||
| 34 | #if sns_Serial_DEBUG==1 |
||
| 35 | printf("RS232\n"); |
||
| 36 | #endif |
||
| 37 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
||
| 38 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
||
| 39 | gpio_clr_pin(sns_Serial_485_232); // RS232 mode |
||
| 40 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
||
| 41 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
||
| 42 | } |
||
| 43 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485FULLDUPLEX) |
||
| 44 | { |
||
| 45 | #if sns_Serial_DEBUG==1 |
||
| 46 | printf("RS485FD\n"); |
||
| 47 | #endif |
||
| 48 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
||
| 49 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
||
| 50 | gpio_set_pin(sns_Serial_485_232); // RS485 mode |
||
| 51 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
||
| 52 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
||
| 53 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
||
| 54 | } |
||
| 55 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485HALFDUPLEX) |
||
| 56 | { |
||
| 57 | #if sns_Serial_DEBUG==1 |
||
| 58 | printf("RS485HD\n"); |
||
| 59 | #endif |
||
| 60 | gpio_set_pin(sns_Serial_485_CONNECT); // enable RS485_CONNECT |
||
| 61 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
||
| 62 | gpio_set_pin(sns_Serial_485_232); // RS485 mode |
||
| 63 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
||
| 64 | /* TODO shoud the TX really be enabled in half duplex? */ |
||
| 65 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
||
| 66 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
||
| 67 | } |
||
| 68 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485HALFDUPLEXWITHTERMINATION) |
||
| 69 | { |
||
| 70 | #if sns_Serial_DEBUG==1 |
||
| 71 | printf("485HDT\n"); |
||
| 72 | #endif |
||
| 73 | gpio_set_pin(sns_Serial_485_CONNECT); // enable RS485_CONNECT |
||
| 74 | gpio_set_pin(sns_Serial_TERM_EN); // enable termination |
||
| 75 | gpio_set_pin(sns_Serial_485_232); // RS485 mode |
||
| 76 | /* TODO shoud the TX really be enabled in half duplex? */ |
||
| 77 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
||
| 78 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
||
| 79 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
||
| 80 | } |
||
| 81 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK) |
||
| 82 | { |
||
| 83 | #if sns_Serial_DEBUG==1 |
||
| 84 | printf("LPMODE\n"); |
||
| 85 | #endif |
||
| 86 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
||
| 87 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
||
| 88 | gpio_set_pin(sns_Serial_485_232); // RS232 mode |
||
| 89 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
||
| 90 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
||
| 91 | gpio_clr_pin(sns_Serial_ON); // disable charge pump (i.e. enable loopback) |
||
| 92 | } |
||
| 93 | else |
||
| 94 | { |
||
| 95 | //invalid format |
||
| 96 | #if sns_Serial_DEBUG==1 |
||
| 97 | printf("ERROR!\n"); |
||
| 98 | #endif |
||
| 99 | //return 0, not successful |
||
| 100 | returnval = 0; |
||
| 101 | } |
||
| 102 | |||
| 103 | return returnval; |
||
| 104 | } |
||
| 105 | |||
| 1449 | myhrman | 106 | void sns_Serial_Init(void) |
| 107 | { |
||
| 108 | #ifdef sns_Serial_USEEEPROM |
||
| 1555 | myhrman | 109 | if (EEDATA_OK) { |
| 1556 | arune | 110 | #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) |
| 1555 | myhrman | 111 | baudRate = eeprom_read_dword(EEDATA32.baudRate); |
| 1556 | arune | 112 | #else |
| 113 | #warning This version of AVR-libc does not have support for eeprom read dword |
||
| 114 | #endif |
||
| 1555 | myhrman | 115 | format = eeprom_read_word(EEDATA16.format); |
| 1561 | arune | 116 | // printf("y\n"); |
| 1449 | myhrman | 117 | } |
| 1555 | myhrman | 118 | else { |
| 119 | //The CRC of the EEPROM is not correct, store default values and update CRC |
||
| 1556 | arune | 120 | #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) |
| 1555 | myhrman | 121 | eeprom_write_dword_crc(EEDATA32.baudRate, 9600, WITHOUT_CRC); |
| 1556 | arune | 122 | #else |
| 123 | #warning This version of AVR-libc does not have support for eeprom write dword |
||
| 124 | #endif |
||
| 1555 | myhrman | 125 | eeprom_write_word_crc(EEDATA16.format, CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK, WITHOUT_CRC); |
| 126 | EEDATA_UPDATE_CRC; |
||
| 1561 | arune | 127 | // printf("z\n"); |
| 1555 | myhrman | 128 | } |
| 1459 | arune | 129 | #endif |
| 130 | // default baudrate |
||
| 131 | uart_init(UART_BAUD_SELECT(baudRate, F_CPU)); |
||
| 1561 | arune | 132 | |
| 133 | // configure control pins to outputs |
||
| 1459 | arune | 134 | gpio_set_out(sns_Serial_RXEN); |
| 135 | gpio_set_out(sns_Serial_TXEN); |
||
| 136 | gpio_set_out(sns_Serial_ON); |
||
| 137 | gpio_set_out(sns_Serial_485_232); |
||
| 138 | gpio_set_out(sns_Serial_TERM_EN); |
||
| 139 | gpio_set_out(sns_Serial_485_CONNECT); |
||
| 1449 | myhrman | 140 | |
| 1561 | arune | 141 | sns_Serial_setSettings(); |
| 1449 | myhrman | 142 | |
| 1536 | arune | 143 | #if sns_Serial_DEBUG==1 |
| 1475 | myhrman | 144 | printf("Serial started!\n"); |
| 1536 | arune | 145 | #endif |
| 1559 | arune | 146 | |
| 147 | msg.Length = 0; // no data so far |
||
| 1449 | myhrman | 148 | } |
| 149 | |||
| 150 | void sns_Serial_Process(void) |
||
| 1459 | arune | 151 | { |
| 152 | StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS); |
||
| 153 | StdCan_Set_direction(msg.Header, DIRECTIONFLAG_TO_OWNER); |
||
| 154 | msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL; |
||
| 155 | msg.Header.ModuleId = sns_Serial_ID; |
||
| 156 | msg.Header.Command = CAN_MODULE_CMD_SERIAL_SERIALDATA; |
||
| 157 | |||
| 158 | unsigned char status; |
||
| 159 | |||
| 160 | do |
||
| 161 | { |
||
| 162 | // ask uart driver for more data |
||
| 163 | unsigned int data = uart_getc(); |
||
| 164 | // character is contained in LSB |
||
| 165 | unsigned char c = (unsigned char)(data & 0x00FF); |
||
| 166 | // status is contained in MSB |
||
| 167 | status = (unsigned char)((data & 0xFF00) >> 8); |
||
| 168 | |||
| 169 | // status == 0 means we just received a new char |
||
| 170 | if (status == 0) |
||
| 171 | { |
||
| 1536 | arune | 172 | #if sns_Serial_DEBUG==1 |
| 1475 | myhrman | 173 | printf("RX\n"); |
| 1536 | arune | 174 | #endif |
| 1459 | arune | 175 | // insert it into the message |
| 176 | msg.Data[(uint8_t)msg.Length] = c; |
||
| 177 | msg.Length++; |
||
| 1559 | arune | 178 | |
| 1560 | arune | 179 | #if sns_Serial_FORCE_SEND_TIME_MS > 0 |
| 1559 | arune | 180 | Timer_SetTimeout(sns_Serial_FORCE_SEND_TIMER, sns_Serial_FORCE_SEND_TIME_MS , TimerTypeOneShot, 0); |
| 1560 | arune | 181 | #endif |
| 1459 | arune | 182 | } |
| 183 | // keep going until max data length reached, or until uart RXBUF is empty |
||
| 184 | } while (status == 0 && msg.Length < 8); |
||
| 185 | |||
| 1559 | arune | 186 | // check if force send or send-frame full |
| 1561 | arune | 187 | if ((Timer_Expired(sns_Serial_FORCE_SEND_TIMER) && msg.Length>0) || msg.Length == 8 || (sns_Serial_FORCE_SEND_TIME_MS == 0 && msg.Length>0)) |
| 1459 | arune | 188 | { |
| 189 | // transmit this chunk of data (max 8 chars) |
||
| 190 | while(StdCan_Put(&msg) != StdCan_Ret_OK); |
||
| 1559 | arune | 191 | msg.Length = 0; // no data so far |
| 1449 | myhrman | 192 | } |
| 193 | } |
||
| 194 | |||
| 195 | void sns_Serial_HandleMessage(StdCan_Msg_t *rxMsg) |
||
| 1459 | arune | 196 | { |
| 1449 | myhrman | 197 | if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS && |
| 198 | StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER && |
||
| 199 | rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_SERIAL && |
||
| 200 | rxMsg->Header.ModuleId == sns_Serial_ID) |
||
| 201 | { |
||
| 202 | switch (rxMsg->Header.Command) |
||
| 1459 | arune | 203 | { |
| 204 | case CAN_MODULE_CMD_SERIAL_SERIALDATA: |
||
| 1536 | arune | 205 | #if sns_Serial_DEBUG==1 |
| 206 | printf("TX\n"); |
||
| 207 | #endif |
||
| 1475 | myhrman | 208 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
| 1459 | arune | 209 | for (uint8_t i=0; i<rxMsg->Length; i++) |
| 210 | { |
||
| 211 | uart_putc(rxMsg->Data[i]); |
||
| 1452 | myhrman | 212 | } |
| 1536 | arune | 213 | /* TODO Cannot disable TX yet, the data has only been placed in buffer but not sent */ |
| 214 | // gpio_clr_pin(sns_Serial_TXEN); // disable TX |
||
| 1452 | myhrman | 215 | break; |
| 1459 | arune | 216 | case CAN_MODULE_CMD_SERIAL_SERIALCONFIG: |
| 1475 | myhrman | 217 | baudRate = 10 * (((uint32_t)rxMsg->Data[1] << 0) | ((uint32_t)rxMsg->Data[0] << 8)); |
| 218 | format = (uint16_t)rxMsg->Data[2]; |
||
| 1459 | arune | 219 | uart_init(UART_BAUD_SELECT(baudRate, F_CPU)); |
| 1561 | arune | 220 | |
| 221 | uint8_t returnval = sns_Serial_setSettings(); |
||
| 222 | |||
| 223 | if (returnval) |
||
| 1459 | arune | 224 | { |
| 1555 | myhrman | 225 | // update EEPROM data |
| 1558 | arune | 226 | #ifdef sns_Serial_USEEEPROM |
| 1556 | arune | 227 | #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) |
| 1561 | arune | 228 | eeprom_write_dword_crc(EEDATA32.baudRate, baudRate, WITHOUT_CRC); |
| 1556 | arune | 229 | #else |
| 230 | #warning This version of AVR-libc does not have support for eeprom read dword |
||
| 231 | #endif |
||
| 1561 | arune | 232 | eeprom_write_word_crc(EEDATA16.format, format, WITHOUT_CRC); |
| 1555 | myhrman | 233 | EEDATA_UPDATE_CRC; |
| 1561 | arune | 234 | |
| 235 | printf("x\n"); |
||
| 1558 | arune | 236 | #endif |
| 1561 | arune | 237 | } |
| 1459 | arune | 238 | break; |
| 1449 | myhrman | 239 | } |
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | void sns_Serial_List(uint8_t ModuleSequenceNumber) |
||
| 244 | { |
||
| 245 | StdCan_Msg_t txMsg; |
||
| 246 | |||
| 247 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); |
||
| 248 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
||
| 249 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL; |
||
| 250 | txMsg.Header.ModuleId = sns_Serial_ID; |
||
| 251 | txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST; |
||
| 252 | txMsg.Length = 6; |
||
| 253 | |||
| 254 | txMsg.Data[0] = NODE_HW_ID_BYTE0; |
||
| 255 | txMsg.Data[1] = NODE_HW_ID_BYTE1; |
||
| 256 | txMsg.Data[2] = NODE_HW_ID_BYTE2; |
||
| 257 | txMsg.Data[3] = NODE_HW_ID_BYTE3; |
||
| 258 | |||
| 259 | txMsg.Data[4] = NUMBER_OF_MODULES; |
||
| 260 | txMsg.Data[5] = ModuleSequenceNumber; |
||
| 261 | |||
| 262 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
||
| 263 | } |
||
| 1475 | myhrman | 264 |