Rev 1475 | Rev 1555 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1475 | Rev 1536 | ||
|---|---|---|---|
| Line 15... | Line 15... | ||
| 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 | static uint16_t format = CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK; |
19 | static uint16_t format = CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK; |
| - | 20 | ||
| - | 21 | #define sns_Serial_DEBUG 0 |
|
| 20 | 22 | ||
| 21 | void sns_Serial_Init(void) |
23 | void sns_Serial_Init(void) |
| 22 | { |
24 | { |
| 23 | #ifdef sns_Serial_USEEEPROM |
25 | #ifdef sns_Serial_USEEEPROM |
| 24 | if (EEDATA_OK) |
26 | if (EEDATA_OK) |
| Line 52... | Line 54... | ||
| 52 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
54 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
| 53 | 55 | ||
| 54 | // to use PCINt lib, call this function: (the callback function look as a timer callback function) |
56 | // to use PCINt lib, call this function: (the callback function look as a timer callback function) |
| 55 | // Pcint_SetCallbackPin(sns_Serial_PCINT, EXP_C , &sns_Serial_pcint_callback); |
57 | // Pcint_SetCallbackPin(sns_Serial_PCINT, EXP_C , &sns_Serial_pcint_callback); |
| 56 | 58 | ||
| - | 59 | #if sns_Serial_DEBUG==1 |
|
| 57 | printf("Serial started!\n"); |
60 | printf("Serial started!\n"); |
| - | 61 | #endif |
|
| 58 | } |
62 | } |
| 59 | 63 | ||
| 60 | void sns_Serial_Process(void) |
64 | void sns_Serial_Process(void) |
| 61 | { |
65 | { |
| 62 | // prepare a new message, in case new data is available |
66 | // prepare a new message, in case new data is available |
| 63 | StdCan_Msg_t msg; |
67 | StdCan_Msg_t msg; |
| 64 | msg.Length = 0; // no data so far |
68 | msg.Length = 0; // no data so far |
| Line 80... | Line 84... | ||
| 80 | status = (unsigned char)((data & 0xFF00) >> 8); |
84 | status = (unsigned char)((data & 0xFF00) >> 8); |
| 81 | 85 | ||
| 82 | // status == 0 means we just received a new char |
86 | // status == 0 means we just received a new char |
| 83 | if (status == 0) |
87 | if (status == 0) |
| 84 | { |
88 | { |
| - | 89 | #if sns_Serial_DEBUG==1 |
|
| 85 | printf("RX\n"); |
90 | printf("RX\n"); |
| - | 91 | #endif |
|
| 86 | // insert it into the message |
92 | // insert it into the message |
| 87 | msg.Data[(uint8_t)msg.Length] = c; |
93 | msg.Data[(uint8_t)msg.Length] = c; |
| 88 | msg.Length++; |
94 | msg.Length++; |
| 89 | } |
95 | } |
| 90 | // keep going until max data length reached, or until uart RXBUF is empty |
96 | // keep going until max data length reached, or until uart RXBUF is empty |
| Line 106... | Line 112... | ||
| 106 | rxMsg->Header.ModuleId == sns_Serial_ID) |
112 | rxMsg->Header.ModuleId == sns_Serial_ID) |
| 107 | { |
113 | { |
| 108 | switch (rxMsg->Header.Command) |
114 | switch (rxMsg->Header.Command) |
| 109 | { |
115 | { |
| 110 | case CAN_MODULE_CMD_SERIAL_SERIALDATA: |
116 | case CAN_MODULE_CMD_SERIAL_SERIALDATA: |
| - | 117 | #if sns_Serial_DEBUG==1 |
|
| - | 118 | printf("TX\n"); |
|
| - | 119 | #endif |
|
| 111 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
120 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
| 112 | for (uint8_t i=0; i<rxMsg->Length; i++) |
121 | for (uint8_t i=0; i<rxMsg->Length; i++) |
| 113 | { |
122 | { |
| 114 | uart_putc(rxMsg->Data[i]); |
123 | uart_putc(rxMsg->Data[i]); |
| 115 | } |
124 | } |
| - | 125 | /* TODO Cannot disable TX yet, the data has only been placed in buffer but not sent */ |
|
| 116 |
|
126 | // gpio_clr_pin(sns_Serial_TXEN); // disable TX |
| 117 | break; |
127 | break; |
| 118 | case CAN_MODULE_CMD_SERIAL_SERIALCONFIG: |
128 | case CAN_MODULE_CMD_SERIAL_SERIALCONFIG: |
| 119 | baudRate = 10 * (((uint32_t)rxMsg->Data[1] << 0) | ((uint32_t)rxMsg->Data[0] << 8)); |
129 | baudRate = 10 * (((uint32_t)rxMsg->Data[1] << 0) | ((uint32_t)rxMsg->Data[0] << 8)); |
| 120 | format = (uint16_t)rxMsg->Data[2]; |
130 | format = (uint16_t)rxMsg->Data[2]; |
| 121 | uart_init(UART_BAUD_SELECT(baudRate, F_CPU)); |
131 | uart_init(UART_BAUD_SELECT(baudRate, F_CPU)); |
| 122 | if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS232) |
132 | if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS232) |
| 123 | { |
133 | { |
| - | 134 | #if sns_Serial_DEBUG==1 |
|
| 124 | printf("RS232\n"); |
135 | printf("RS232\n"); |
| - | 136 | #endif |
|
| 125 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
137 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
| 126 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
138 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
| 127 | gpio_clr_pin(sns_Serial_485_232); // RS232 mode |
139 | gpio_clr_pin(sns_Serial_485_232); // RS232 mode |
| 128 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
140 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
| 129 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
141 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
| 130 | } |
142 | } |
| 131 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485FULLDUPLEX) |
143 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485FULLDUPLEX) |
| 132 | { |
144 | { |
| - | 145 | #if sns_Serial_DEBUG==1 |
|
| 133 | printf("RS485FD\n"); |
146 | printf("RS485FD\n"); |
| - | 147 | #endif |
|
| 134 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
148 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
| 135 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
149 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
| 136 | gpio_set_pin(sns_Serial_485_232); // RS485 mode |
150 | gpio_set_pin(sns_Serial_485_232); // RS485 mode |
| 137 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
151 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
| 138 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
152 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
| 139 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
153 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
| 140 | } |
154 | } |
| 141 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485HALFDUPLEX) |
155 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485HALFDUPLEX) |
| 142 | { |
156 | { |
| - | 157 | #if sns_Serial_DEBUG==1 |
|
| 143 | printf("RS485HD\n"); |
158 | printf("RS485HD\n"); |
| - | 159 | #endif |
|
| 144 | gpio_set_pin(sns_Serial_485_CONNECT); // enable RS485_CONNECT |
160 | gpio_set_pin(sns_Serial_485_CONNECT); // enable RS485_CONNECT |
| 145 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
161 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
| 146 | gpio_set_pin(sns_Serial_485_232); // RS485 mode |
162 | gpio_set_pin(sns_Serial_485_232); // RS485 mode |
| 147 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
163 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
| - | 164 | /* TODO shoud the TX really be enabled in half duplex? */ |
|
| 148 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
165 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
| 149 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
166 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
| 150 | } |
167 | } |
| 151 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485HALFDUPLEXWITHTERMINATION) |
168 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485HALFDUPLEXWITHTERMINATION) |
| 152 | { |
169 | { |
| - | 170 | #if sns_Serial_DEBUG==1 |
|
| 153 | printf("485HDT\n"); |
171 | printf("485HDT\n"); |
| - | 172 | #endif |
|
| 154 | gpio_set_pin(sns_Serial_485_CONNECT); // enable RS485_CONNECT |
173 | gpio_set_pin(sns_Serial_485_CONNECT); // enable RS485_CONNECT |
| 155 | gpio_set_pin(sns_Serial_TERM_EN); // enable termination |
174 | gpio_set_pin(sns_Serial_TERM_EN); // enable termination |
| 156 | gpio_set_pin(sns_Serial_485_232); // RS485 mode |
175 | gpio_set_pin(sns_Serial_485_232); // RS485 mode |
| - | 176 | /* TODO shoud the TX really be enabled in half duplex? */ |
|
| 157 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
177 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
| 158 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
178 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
| 159 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
179 | gpio_set_pin(sns_Serial_ON); // enable charge pump |
| 160 | } |
180 | } |
| 161 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK) |
181 | else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK) |
| 162 | { |
182 | { |
| - | 183 | #if sns_Serial_DEBUG==1 |
|
| 163 | printf("LPMODE\n"); |
184 | printf("LPMODE\n"); |
| - | 185 | #endif |
|
| 164 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
186 | gpio_clr_pin(sns_Serial_485_CONNECT); // disable RS485_CONNECT |
| 165 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
187 | gpio_clr_pin(sns_Serial_TERM_EN); // disable termination |
| 166 | gpio_set_pin(sns_Serial_485_232); // RS232 mode |
188 | gpio_set_pin(sns_Serial_485_232); // RS232 mode |
| 167 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
189 | gpio_set_pin(sns_Serial_RXEN); // enable RX |
| 168 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
190 | gpio_set_pin(sns_Serial_TXEN); // enable TX |
| 169 | gpio_clr_pin(sns_Serial_ON); // disable charge pump (i.e. enable loopback) |
191 | gpio_clr_pin(sns_Serial_ON); // disable charge pump (i.e. enable loopback) |
| 170 | } |
192 | } |
| 171 | else |
193 | else |
| 172 | { |
194 | { |
| 173 | //invalid format |
195 | //invalid format |
| - | 196 | #if sns_Serial_DEBUG==1 |
|
| 174 | printf("ERROR!\n"); |
197 | printf("ERROR!\n"); |
| - | 198 | #endif |
|
| 175 | } |
199 | } |
| 176 | break; |
200 | break; |
| 177 | } |
201 | } |
| 178 | } |
202 | } |
| 179 | } |
203 | } |