Rev 693 | Rev 698 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 693 | Rev 697 | ||
|---|---|---|---|
| Line 19... | Line 19... | ||
| 19 | #include <stdio.h> |
19 | #include <stdio.h> |
| 20 | #include <string.h> |
20 | #include <string.h> |
| 21 | 21 | ||
| 22 | #include <config.h> |
22 | #include <config.h> |
| 23 | /* lib files */ |
23 | /* lib files */ |
| 24 | |
24 | #if USE_STDCAN == 1 |
| 25 | #include <stdcan.h> |
25 | #include <stdcan.h> |
| 26 | #else |
26 | #else |
| 27 | #include <mcp2515.h> |
27 | #include <mcp2515.h> |
| 28 | #endif |
28 | #endif |
| 29 | #include <serial.h> |
29 | #include <serial.h> |
| 30 | #include <uart.h> |
30 | #include <uart.h> |
| 31 | #include <timebase.h> |
31 | #include <timebase.h> |
| 32 | 32 | ||
| - | 33 | ||
| 33 | |
34 | #if USE_STDCAN == 1 |
| 34 | #if SENDTIMESTAMP == 1 |
35 | #if SENDTIMESTAMP == 1 |
| 35 | |
36 | #warning SENDTIMESTAMP not fully implemented with StdCan yet. |
| 36 | #endif |
37 | #endif |
| 37 | #endif |
38 | #endif |
| 38 | 39 | ||
| 39 | 40 | ||
| 40 | /*----------------------------------------------------------------------------- |
41 | /*----------------------------------------------------------------------------- |
| 41 | * Defines |
42 | * Defines |
| 42 | *---------------------------------------------------------------------------*/ |
43 | *---------------------------------------------------------------------------*/ |
| 43 | #define UART_START_BYTE 253 |
44 | #define UART_START_BYTE 253 |
| 44 | #define UART_END_BYTE 250 |
45 | #define UART_END_BYTE 250 |
| 45 | 46 | ||
| 46 | |
47 | #if USE_STDCAN == 0 |
| 47 | volatile Can_Message_t rxMsg; // Message storage |
48 | volatile Can_Message_t rxMsg; // Message storage |
| 48 | volatile uint8_t rxMsgFull; // Synchronization flag |
49 | volatile uint8_t rxMsgFull; // Synchronization flag |
| 49 | #endif |
50 | #endif |
| 50 | 51 | ||
| 51 | const uint8_t days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; |
52 | const uint8_t days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; |
| Line 100... | Line 101... | ||
| 100 | * |
101 | * |
| 101 | * @param c |
102 | * @param c |
| 102 | * The received UART byte. |
103 | * The received UART byte. |
| 103 | */ |
104 | */ |
| 104 | void UartParseByte(uint8_t c) { |
105 | void UartParseByte(uint8_t c) { |
| 105 | |
106 | #if USE_STDCAN == 0 |
| 106 | static Can_Message_t cm; |
107 | static Can_Message_t cm; |
| 107 | #else |
108 | #else |
| 108 | static StdCan_Msg_t cm; |
109 | static StdCan_Msg_t cm; |
| 109 | #endif |
110 | #endif |
| 110 | static uint8_t waitingMessage = 0; |
111 | static uint8_t waitingMessage = 0; |
| Line 121... | Line 122... | ||
| 121 | startTime = Timebase_CurrentTime(); |
122 | startTime = Timebase_CurrentTime(); |
| 122 | /* UART END */ |
123 | /* UART END */ |
| 123 | if (count >= 15) { |
124 | if (count >= 15) { |
| 124 | if (c == UART_END_BYTE) { |
125 | if (c == UART_END_BYTE) { |
| 125 | PORTC ^= (1<<PC0); |
126 | PORTC ^= (1<<PC0); |
| 126 | |
127 | #if USE_STDCAN == 0 |
| 127 | Can_Send(&cm); |
128 | Can_Send(&cm); |
| 128 | #else |
129 | #else |
| 129 | StdCan_Put(&cm); |
130 | StdCan_Put(&cm); |
| 130 | #endif |
131 | #endif |
| 131 | } |
132 | } |
| 132 | waitingMessage = 0; |
133 | waitingMessage = 0; |
| 133 | return; |
134 | return; |
| 134 | } |
135 | } |
| 135 | /* data */ |
136 | /* data */ |
| 136 | else if (count >= 7) { |
137 | else if (count >= 7) { |
| 137 | |
138 | #if USE_STDCAN == 0 |
| 138 | cm.Data.bytes[count-7] = c; |
139 | cm.Data.bytes[count-7] = c; |
| 139 | #else |
140 | #else |
| 140 | cm.Data[count-7] = c; |
141 | cm.Data[count-7] = c; |
| 141 | #endif |
142 | #endif |
| 142 | count++; |
143 | count++; |
| 143 | return; |
144 | return; |
| 144 | } |
145 | } |
| 145 | /* data length */ |
146 | /* data length */ |
| 146 | else if (count >= 6) { |
147 | else if (count >= 6) { |
| 147 | |
148 | #if USE_STDCAN == 0 |
| 148 | cm.DataLength = c; |
149 | cm.DataLength = c; |
| 149 | #else |
150 | #else |
| 150 | cm.Length = c; |
151 | cm.Length = c; |
| 151 | #endif |
152 | #endif |
| 152 | count++; |
153 | count++; |
| 153 | return; |
154 | return; |
| 154 | } |
155 | } |
| 155 | /* remote request flag */ |
156 | /* remote request flag */ |
| 156 | else if (count >= 5) { |
157 | else if (count >= 5) { |
| 157 | |
158 | #if USE_STDCAN == 0 |
| 158 | cm.RemoteFlag = c; |
159 | cm.RemoteFlag = c; |
| 159 | #endif |
160 | #endif |
| 160 | count++; |
161 | count++; |
| 161 | return; |
162 | return; |
| 162 | } |
163 | } |
| 163 | /* extended */ |
164 | /* extended */ |
| 164 | else if (count >= 4) { |
165 | else if (count >= 4) { |
| 165 | |
166 | #if USE_STDCAN == 0 |
| 166 | cm.ExtendedFlag = c; |
167 | cm.ExtendedFlag = c; |
| 167 | #endif |
168 | #endif |
| 168 | count++; |
169 | count++; |
| 169 | return; |
170 | return; |
| 170 | } |
171 | } |
| Line 183... | Line 184... | ||
| 183 | cm.Id = 0; |
184 | cm.Id = 0; |
| 184 | return; |
185 | return; |
| 185 | } |
186 | } |
| 186 | } |
187 | } |
| 187 | 188 | ||
| 188 | |
189 | #if USE_STDCAN == 0 |
| 189 | void Can_Process(Can_Message_t* msg) { |
190 | void Can_Process(Can_Message_t* msg) { |
| 190 | if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames. |
191 | if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames. |
| 191 | 192 | ||
| 192 | if (!rxMsgFull) { |
193 | if (!rxMsgFull) { |
| 193 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
194 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
| 194 | rxMsgFull = 1; |
195 | rxMsgFull = 1; |
| 195 | } |
196 | } |
| 196 | } |
197 | } |
| 197 | #endif |
198 | #endif |
| 198 | 199 | ||
| 199 | 200 | ||
| 200 | /*----------------------------------------------------------------------------- |
201 | /*----------------------------------------------------------------------------- |
| 201 | * Main Program |
202 | * Main Program |
| 202 | *---------------------------------------------------------------------------*/ |
203 | *---------------------------------------------------------------------------*/ |
| 203 | int main(void) { |
204 | int main(void) { |
| 204 | // For calibrating internal oscillator |
205 | // For calibrating internal oscillator |
| 205 | // OSCCAL = eeprom_read_byte(0); |
206 | // OSCCAL = eeprom_read_byte(0); |
| 206 | Timebase_Init(); |
207 | Timebase_Init(); |
| 207 | Serial_Init(); |
208 | Serial_Init(); |
| 208 | |
209 | #if USE_STDCAN == 0 |
| 209 | Can_Init(); |
210 | Can_Init(); |
| 210 | #else |
211 | #else |
| 211 | StdCan_Init(0); |
212 | StdCan_Init(0); |
| 212 | #endif |
213 | #endif |
| 213 | 214 | ||
| 214 | DDRC = 1<<PC1 | 1<<PC0; |
215 | DDRC = 1<<PC1 | 1<<PC0; |
| 215 | PORTC = (1<<PC1) | (1<<PC0); |
216 | PORTC = (1<<PC1) | (1<<PC0); |
| 216 | 217 | ||
| 217 | sei(); |
218 | sei(); |
| 218 | 219 | ||
| 219 | #if SENDTIMESTAMP == 1 |
220 | #if SENDTIMESTAMP == 1 |
| - | 221 | #if USE_STDCAN == 0 |
|
| 220 | Can_Message_t timeMsg; |
222 | Can_Message_t timeMsg; |
| - | 223 | #else |
|
| - | 224 | StdCan_Msg_t timeMsg; |
|
| - | 225 | #endif |
|
| 221 | uint32_t time = Timebase_CurrentTime(); |
226 | uint32_t time = Timebase_CurrentTime(); |
| 222 | uint32_t time1 = time; |
227 | uint32_t time1 = time; |
| 223 | uint32_t unixtime = 0; |
228 | uint32_t unixtime = 0; |
| 224 | #endif |
229 | #endif |
| 225 | uint16_t rxByte; |
230 | uint16_t rxByte; |
| 226 | uint8_t i = 0; |
231 | uint8_t i = 0; |
| 227 | 232 | ||
| 228 | #if SENDTIMESTAMP == 1 |
233 | #if SENDTIMESTAMP == 1 |
| - | 234 | #if USE_STDCAN == 0 |
|
| 229 | timeMsg.ExtendedFlag = 1; |
235 | timeMsg.ExtendedFlag = 1; |
| 230 | timeMsg.RemoteFlag = 0; |
236 | timeMsg.RemoteFlag = 0; |
| 231 | timeMsg.Id = (CAN_NMT << CAN_SHIFT_CLASS) | (CAN_NMT_TIME << CAN_SHIFT_NMT_TYPE); |
- | |
| 232 | //timeMsg.Id = 0; //Same thing, and lib's can.h is not updated. |
- | |
| 233 | timeMsg.DataLength = 8; |
237 | timeMsg.DataLength = 8; |
| - | 238 | #else |
|
| - | 239 | /* Jag orkar inte fixa in datan i paketen, sätter längd till 0 sålänge */ |
|
| - | 240 | timeMsg.Length = 0; |
|
| - | 241 | #endif |
|
| - | 242 | timeMsg.Id = (CAN_NMT << CAN_SHIFT_CLASS) | (CAN_NMT_TIME << CAN_SHIFT_NMT_TYPE); |
|
| - | 243 | //timeMsg.Id = 0; //Same thing, and lib's can.h is not updated. |
|
| 234 | #endif |
244 | #endif |
| 235 | 245 | ||
| 236 | |
246 | #if USE_STDCAN == 1 |
| 237 | StdCan_Msg_t rxMsg; |
247 | StdCan_Msg_t rxMsg; |
| 238 | #endif |
248 | #endif |
| 239 | 249 | ||
| 240 | /* main loop */ |
250 | /* main loop */ |
| 241 | while (1) { |
251 | while (1) { |
| 242 | /* any new CAN messages received? */ |
252 | /* any new CAN messages received? */ |
| 243 | |
253 | #if USE_STDCAN == 0 |
| 244 | if (rxMsgFull) { |
254 | if (rxMsgFull) { |
| 245 | #else |
255 | #else |
| 246 | if (StdCan_Get(&rxMsg) == StdCan_Ret_OK) { |
256 | if (StdCan_Get(&rxMsg) == StdCan_Ret_OK) { |
| 247 | #endif |
257 | #endif |
| 248 | // Toggle activity LED |
258 | // Toggle activity LED |
| Line 251... | Line 261... | ||
| 251 | uart_putc(UART_START_BYTE); |
261 | uart_putc(UART_START_BYTE); |
| 252 | uart_putc((uint8_t)rxMsg.Id); |
262 | uart_putc((uint8_t)rxMsg.Id); |
| 253 | uart_putc((uint8_t)(rxMsg.Id>>8)); |
263 | uart_putc((uint8_t)(rxMsg.Id>>8)); |
| 254 | uart_putc((uint8_t)(rxMsg.Id>>16)); |
264 | uart_putc((uint8_t)(rxMsg.Id>>16)); |
| 255 | uart_putc((uint8_t)(rxMsg.Id>>24)); |
265 | uart_putc((uint8_t)(rxMsg.Id>>24)); |
| 256 | |
266 | #if USE_STDCAN == 0 |
| 257 | uart_putc(rxMsg.ExtendedFlag); |
267 | uart_putc(rxMsg.ExtendedFlag); |
| 258 | uart_putc(rxMsg.RemoteFlag); |
268 | uart_putc(rxMsg.RemoteFlag); |
| 259 | uart_putc(rxMsg.DataLength); |
269 | uart_putc(rxMsg.DataLength); |
| 260 | for (i=0; i<8; i++) { |
270 | for (i=0; i<8; i++) { |
| 261 | uart_putc(rxMsg.Data.bytes[i]); |
271 | uart_putc(rxMsg.Data.bytes[i]); |
| 262 | } |
272 | } |
| 263 | #else |
273 | #else |
| 264 | uart_putc(1); |
274 | uart_putc(1); |
| 265 | uart_putc(0); |
275 | uart_putc(0); |
| 266 | uart_putc(rxMsg.Length); |
276 | uart_putc(rxMsg.Length); |
| 267 | for (i=0; i<8; i++) { |
277 | for (i=0; i<8; i++) { |
| 268 | uart_putc(rxMsg.Data[i]); |
278 | uart_putc(rxMsg.Data[i]); |
| 269 | } |
279 | } |
| 270 | #endif |
280 | #endif |
| 271 | uart_putc(UART_END_BYTE); |
281 | uart_putc(UART_END_BYTE); |
| 272 | 282 | ||
| 273 | |
283 | #if USE_STDCAN == 0 |
| 274 | rxMsgFull = 0; |
284 | rxMsgFull = 0; |
| 275 | #endif |
285 | #endif |
| 276 | } |
286 | } |
| 277 | 287 | ||
| 278 | /* any UART bytes received? */ |
288 | /* any UART bytes received? */ |
| 279 | rxByte = uart_getc(); |
289 | rxByte = uart_getc(); |
| 280 | while (rxByte != UART_NO_DATA) { |
290 | while (rxByte != UART_NO_DATA) { |
| 281 | /* parse byte! */ |
291 | /* parse byte! */ |
| 282 | UartParseByte((uint8_t)(rxByte & 0x00FF)); |
292 | UartParseByte((uint8_t)(rxByte & 0x00FF)); |
| 283 | /* receive next */ |
293 | /* receive next */ |
| 284 | rxByte = uart_getc(); |
294 | rxByte = uart_getc(); |
| Line 286... | Line 296... | ||
| 286 | 296 | ||
| 287 | #if SENDTIMESTAMP == 1 |
297 | #if SENDTIMESTAMP == 1 |
| 288 | time1 = Timebase_CurrentTime(); |
298 | time1 = Timebase_CurrentTime(); |
| 289 | if ((time1 - time) > 1000) { |
299 | if ((time1 - time) > 1000) { |
| 290 | time = time1; |
300 | time = time1; |
| - | 301 | #if USE_STDCAN == 0 |
|
| 291 | timeMsg.Data.dwords[0] = ++unixtime; |
302 | timeMsg.Data.dwords[0] = ++unixtime; |
| 292 | IncTime(); |
303 | IncTime(); |
| 293 | timeMsg.Data.dwords[1] = date.packed; |
304 | timeMsg.Data.dwords[1] = date.packed; |
| 294 | Can_Send(&timeMsg); |
305 | Can_Send(&timeMsg); |
| - | 306 | #else |
|
| - | 307 | /* Jag orkar inte fixa in datan i paketen, sätter längd till 0 sålänge */ |
|
| - | 308 | StdCan_Put(&timeMsg); |
|
| - | 309 | #endif |
|
| 295 | } |
310 | } |
| 296 | #endif |
311 | #endif |
| 297 | } |
312 | } |
| 298 | 313 | ||
| 299 | return 0; |
314 | return 0; |