Rev 389 | Rev 671 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 389 | Rev 665 | ||
|---|---|---|---|
| Line 14... | Line 14... | ||
| 14 | #include <avr/io.h> |
14 | #include <avr/io.h> |
| 15 | #include <avr/interrupt.h> |
15 | #include <avr/interrupt.h> |
| 16 | #include <avr/wdt.h> |
16 | #include <avr/wdt.h> |
| 17 | #include <avr/eeprom.h> |
17 | #include <avr/eeprom.h> |
| 18 | #include <stdio.h> |
18 | #include <stdio.h> |
| - | 19 | #include <string.h> |
|
| 19 | /* lib files */ |
20 | /* lib files */ |
| 20 | #include < |
21 | #include <mcp2515.h> |
| 21 | #include <serial.h> |
22 | #include <serial.h> |
| 22 | #include <uart.h> |
23 | #include <uart.h> |
| 23 | #include <timebase.h> |
24 | #include <timebase.h> |
| 24 | 25 | ||
| 25 | 26 | ||
| 26 | /*----------------------------------------------------------------------------- |
27 | /*----------------------------------------------------------------------------- |
| 27 | * Defines |
28 | * Defines |
| 28 | *---------------------------------------------------------------------------*/ |
29 | *---------------------------------------------------------------------------*/ |
| 29 | #define UART_START_BYTE 253 |
30 | #define UART_START_BYTE 253 |
| 30 | #define UART_END_BYTE 250 |
31 | #define UART_END_BYTE 250 |
| - | 32 | ||
| - | 33 | volatile Can_Message_t rxMsg; // Message storage |
|
| - | 34 | volatile uint8_t rxMsgFull; // Synchronization flag |
|
| 31 | 35 | ||
| 32 | const uint8_t days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; |
36 | const uint8_t days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; |
| 33 | 37 | ||
| 34 | union { |
38 | union { |
| 35 | uint32_t packed; |
39 | uint32_t packed; |
| Line 41... | Line 45... | ||
| 41 | unsigned MM:4; |
45 | unsigned MM:4; |
| 42 | unsigned YY:6; |
46 | unsigned YY:6; |
| 43 | } unpacked; |
47 | } unpacked; |
| 44 | } date; |
48 | } date; |
| 45 | 49 | ||
| - | 50 | #if SENDTIMESTAMP == 1 |
|
| 46 | void IncTime(void) { |
51 | void IncTime(void) { |
| 47 | static uint8_t YY=07, MM=03, DD=02, hh=23, mm=58, ss=00; |
52 | static uint8_t YY=07, MM=03, DD=02, hh=23, mm=58, ss=00; |
| 48 | 53 | ||
| 49 | if (ss++ > 59) { |
54 | if (ss++ > 59) { |
| 50 | ss=0; |
55 | ss=0; |
| Line 66... | Line 71... | ||
| 66 | date.unpacked.DD = DD; |
71 | date.unpacked.DD = DD; |
| 67 | date.unpacked.hh = hh; |
72 | date.unpacked.hh = hh; |
| 68 | date.unpacked.mm = mm; |
73 | date.unpacked.mm = mm; |
| 69 | date.unpacked.ss = ss; |
74 | date.unpacked.ss = ss; |
| 70 | } |
75 | } |
| - | 76 | #endif |
|
| 71 | /*----------------------------------------------------------------------------- |
77 | /*----------------------------------------------------------------------------- |
| 72 | * Functions |
78 | * Functions |
| 73 | *---------------------------------------------------------------------------*/ |
79 | *---------------------------------------------------------------------------*/ |
| 74 | /** |
80 | /** |
| 75 | * Parses an incoming UART byte by maintaining a state machine. The UART |
81 | * Parses an incoming UART byte by maintaining a state machine. The UART |
| Line 86... | Line 92... | ||
| 86 | static uint32_t startTime = 0; |
92 | static uint32_t startTime = 0; |
| 87 | static int8_t count = 0; |
93 | static int8_t count = 0; |
| 88 | 94 | ||
| 89 | /* 50ms timeout */ |
95 | /* 50ms timeout */ |
| 90 | if (waitingMessage && Timebase_PassedTimeMillis(startTime) > 50) { |
96 | if (waitingMessage && Timebase_PassedTimeMillis(startTime) > 50) { |
| 91 | waitingMessage = 0; |
97 | waitingMessage = 0; |
| 92 | } |
98 | } |
| 93 | 99 | ||
| 94 | if (waitingMessage) { |
100 | if (waitingMessage) { |
| 95 | /* save start time */ |
101 | /* save start time */ |
| 96 | startTime = Timebase_CurrentTime(); |
102 | startTime = Timebase_CurrentTime(); |
| 97 | /* UART END */ |
103 | /* UART END */ |
| Line 139... | Line 145... | ||
| 139 | waitingMessage = 1; |
145 | waitingMessage = 1; |
| 140 | startTime = Timebase_CurrentTime(); |
146 | startTime = Timebase_CurrentTime(); |
| 141 | count = 0; |
147 | count = 0; |
| 142 | cm.Id = 0; |
148 | cm.Id = 0; |
| 143 | return; |
149 | return; |
| - | 150 | } |
|
| - | 151 | } |
|
| - | 152 | ||
| - | 153 | void Can_Process(Can_Message_t* msg) { |
|
| - | 154 | if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames. |
|
| - | 155 | ||
| - | 156 | if (!rxMsgFull) { |
|
| - | 157 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
|
| - | 158 | rxMsgFull = 1; |
|
| - | 159 | } |
|
| - | 160 | } |
|
| - | 161 | ||
| - | 162 | ISR(MCP_INT_VECTOR) { |
|
| - | 163 | // Get first available message from controller and pass it to |
|
| - | 164 | // application handler. If both RX buffers contain messages |
|
| - | 165 | // we will get another interrupt as soon as this one returns. |
|
| - | 166 | if (Can_Receive(&rxMsg) == CAN_OK) { |
|
| - | 167 | // Callbacks are run with global interrupts disabled but |
|
| - | 168 | // with controller flag cleared so another msg can be |
|
| - | 169 | // received while this one is processed. |
|
| - | 170 | Can_Process(&rxMsg); |
|
| 144 | } |
171 | } |
| 145 | } |
172 | } |
| 146 | 173 | ||
| 147 | 174 | ||
| 148 | /*----------------------------------------------------------------------------- |
175 | /*----------------------------------------------------------------------------- |
| Line 158... | Line 185... | ||
| 158 | DDRC = 1<<PC1 | 1<<PC0; |
185 | DDRC = 1<<PC1 | 1<<PC0; |
| 159 | PORTC = (1<<PC1) | (1<<PC0); |
186 | PORTC = (1<<PC1) | (1<<PC0); |
| 160 | 187 | ||
| 161 | sei(); |
188 | sei(); |
| 162 | 189 | ||
| 163 | Can_Message_t rxMsg; |
- | |
| 164 | Can_Message_t timeMsg; |
190 | Can_Message_t timeMsg; |
| - | 191 | #if SENDTIMESTAMP == 1 |
|
| 165 | uint32_t time = Timebase_CurrentTime(); |
192 | uint32_t time = Timebase_CurrentTime(); |
| 166 | uint32_t time1 = time; |
193 | uint32_t time1 = time; |
| 167 | uint32_t unixtime = 0; |
194 | uint32_t unixtime = 0; |
| - | 195 | #endif |
|
| 168 | uint16_t rxByte; |
196 | uint16_t rxByte; |
| 169 | uint8_t i = 0; |
197 | uint8_t i = 0; |
| 170 | 198 | ||
| - | 199 | #if SENDTIMESTAMP == 1 |
|
| 171 | timeMsg.ExtendedFlag = 1; |
200 | timeMsg.ExtendedFlag = 1; |
| 172 | timeMsg.RemoteFlag = 0; |
201 | timeMsg.RemoteFlag = 0; |
| 173 |
|
202 | timeMsg.Id = (CAN_NMT << CAN_SHIFT_CLASS) | (CAN_NMT_TIME << CAN_SHIFT_NMT_TYPE); |
| 174 | timeMsg. |
203 | //timeMsg.Id = 0; //Same thing, and lib's can.h is not updated. |
| 175 | timeMsg.DataLength = 8; |
204 | timeMsg.DataLength = 8; |
| - | 205 | #endif |
|
| 176 | 206 | ||
| 177 | /* main loop */ |
207 | /* main loop */ |
| 178 | while (1) { |
208 | while (1) { |
| 179 | /* service the CAN routines */ |
- | |
| 180 | Can_Service(); |
- | |
| 181 | - | ||
| 182 | /* any new CAN messages received? */ |
209 | /* any new CAN messages received? */ |
| 183 | if ( |
210 | if (rxMsgFull) { |
| 184 | // Toggle activity LED |
211 | // Toggle activity LED |
| 185 | PORTC ^= (1<<PC1); |
212 | PORTC ^= (1<<PC1); |
| 186 | /* send message to CanWatcher */ |
213 | /* send message to CanWatcher */ |
| 187 | uart_putc(UART_START_BYTE); |
214 | uart_putc(UART_START_BYTE); |
| 188 | uart_putc((uint8_t)rxMsg.Id); |
215 | uart_putc((uint8_t)rxMsg.Id); |
| Line 194... | Line 221... | ||
| 194 | uart_putc(rxMsg.DataLength); |
221 | uart_putc(rxMsg.DataLength); |
| 195 | for (i=0; i<8; i++) { |
222 | for (i=0; i<8; i++) { |
| 196 | uart_putc(rxMsg.Data.bytes[i]); |
223 | uart_putc(rxMsg.Data.bytes[i]); |
| 197 | } |
224 | } |
| 198 | uart_putc(UART_END_BYTE); |
225 | uart_putc(UART_END_BYTE); |
| - | 226 | ||
| - | 227 | rxMsgFull = 0; |
|
| 199 | } |
228 | } |
| 200 | 229 | ||
| 201 | /* any UART bytes received? */ |
230 | /* any UART bytes received? */ |
| 202 | rxByte = uart_getc(); |
231 | rxByte = uart_getc(); |
| 203 | while (rxByte != UART_NO_DATA) { |
232 | while (rxByte != UART_NO_DATA) { |
| Line 205... | Line 234... | ||
| 205 | UartParseByte((uint8_t)(rxByte & 0x00FF)); |
234 | UartParseByte((uint8_t)(rxByte & 0x00FF)); |
| 206 | /* receive next */ |
235 | /* receive next */ |
| 207 | rxByte = uart_getc(); |
236 | rxByte = uart_getc(); |
| 208 | } |
237 | } |
| 209 | 238 | ||
| - | 239 | #if SENDTIMESTAMP == 1 |
|
| 210 | time1 = Timebase_CurrentTime(); |
240 | time1 = Timebase_CurrentTime(); |
| 211 | if ((time1 - time) > 1000) { |
241 | if ((time1 - time) > 1000) { |
| 212 | time = time1; |
242 | time = time1; |
| 213 | timeMsg.Data.dwords[0] = ++unixtime; |
243 | timeMsg.Data.dwords[0] = ++unixtime; |
| 214 | IncTime(); |
244 | IncTime(); |
| 215 | timeMsg.Data.dwords[1] = date.packed; |
245 | timeMsg.Data.dwords[1] = date.packed; |
| 216 | Can_Send(&timeMsg); |
246 | Can_Send(&timeMsg); |
| 217 | } |
247 | } |
| - | 248 | #endif |
|
| 218 | } |
249 | } |
| 219 | 250 | ||
| 220 | return 0; |
251 | return 0; |
| 221 | } |
252 | } |