Rev 149 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 149 | Rev 356 | ||
|---|---|---|---|
| Line 12... | Line 12... | ||
| 12 | *---------------------------------------------------------------------------*/ |
12 | *---------------------------------------------------------------------------*/ |
| 13 | /* system files */ |
13 | /* system files */ |
| 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 <stdio.h> |
18 | #include <stdio.h> |
| 18 | /* lib files */ |
19 | /* lib files */ |
| 19 | #include <can.h> |
20 | #include <can.h> |
| 20 | #include <serial.h> |
21 | #include <serial.h> |
| 21 | #include <uart.h> |
22 | #include <uart.h> |
| Line 26... | Line 27... | ||
| 26 | * Defines |
27 | * Defines |
| 27 | *---------------------------------------------------------------------------*/ |
28 | *---------------------------------------------------------------------------*/ |
| 28 | #define UART_START_BYTE 253 |
29 | #define UART_START_BYTE 253 |
| 29 | #define UART_END_BYTE 250 |
30 | #define UART_END_BYTE 250 |
| 30 | 31 | ||
| - | 32 | const uint8_t days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; |
|
| 31 | 33 | ||
| - | 34 | union { |
|
| - | 35 | uint32_t packed; |
|
| - | 36 | struct date_t { |
|
| - | 37 | unsigned ss:6; |
|
| - | 38 | unsigned mm:6; |
|
| - | 39 | unsigned hh:5; |
|
| - | 40 | unsigned DD:5; |
|
| - | 41 | unsigned MM:4; |
|
| - | 42 | unsigned YY:6; |
|
| - | 43 | } unpacked; |
|
| - | 44 | } date; |
|
| - | 45 | ||
| - | 46 | void IncTime(void) { |
|
| - | 47 | static uint8_t YY=07, MM=03, DD=02, hh=23, mm=58, ss=00; |
|
| - | 48 | ||
| - | 49 | if (ss++ > 59) { |
|
| - | 50 | ss=0; |
|
| - | 51 | if (mm++ > 59) { |
|
| - | 52 | mm=0; |
|
| - | 53 | if (hh++ > 23) { |
|
| - | 54 | hh=0; |
|
| - | 55 | if (DD++ > days[MM-1] + ((YY%4) & (MM==2)) ? 1 : 0))) { |
|
| - | 56 | DD=1; |
|
| - | 57 | if (MM++ > 12) { |
|
| - | 58 | MM=1; YY++; |
|
| - | 59 | } |
|
| - | 60 | } |
|
| - | 61 | } |
|
| - | 62 | } |
|
| - | 63 | } |
|
| - | 64 | date.unpacked.YY = YY; |
|
| - | 65 | date.unpacked.MM = MM; |
|
| - | 66 | date.unpacked.DD = DD; |
|
| - | 67 | date.unpacked.hh = hh; |
|
| - | 68 | date.unpacked.mm = mm; |
|
| - | 69 | date.unpacked.ss = ss; |
|
| - | 70 | } |
|
| 32 | /*----------------------------------------------------------------------------- |
71 | /*----------------------------------------------------------------------------- |
| 33 | * Functions |
72 | * Functions |
| 34 | *---------------------------------------------------------------------------*/ |
73 | *---------------------------------------------------------------------------*/ |
| 35 | /** |
74 | /** |
| 36 | * Parses an incoming UART byte by maintaining a state machine. The UART |
75 | * Parses an incoming UART byte by maintaining a state machine. The UART |
| Line 71... | Line 110... | ||
| 71 | return; |
110 | return; |
| 72 | } |
111 | } |
| 73 | /* data length */ |
112 | /* data length */ |
| 74 | else if (count >= 6) { |
113 | else if (count >= 6) { |
| 75 | cm.DataLength = c; |
114 | cm.DataLength = c; |
| 76 | count++; |
115 | count++; |
| 77 | return; |
116 | return; |
| 78 | } |
117 | } |
| 79 | /* remote request flag */ |
118 | /* remote request flag */ |
| 80 | else if (count >= 5) { |
119 | else if (count >= 5) { |
| 81 | cm.RemoteFlag = c; |
120 | cm.RemoteFlag = c; |
| 82 | count++; |
121 | count++; |
| 83 | return; |
122 | return; |
| Line 108... | Line 147... | ||
| 108 | 147 | ||
| 109 | /*----------------------------------------------------------------------------- |
148 | /*----------------------------------------------------------------------------- |
| 110 | * Main Program |
149 | * Main Program |
| 111 | *---------------------------------------------------------------------------*/ |
150 | *---------------------------------------------------------------------------*/ |
| 112 | int main(void) { |
151 | int main(void) { |
| - | 152 | OSCCAL = eeprom_read_byte(0); |
|
| 113 | Timebase_Init(); |
153 | Timebase_Init(); |
| 114 | Serial_Init(); |
154 | Serial_Init(); |
| 115 | Can_Init(); |
155 | Can_Init(); |
| 116 | 156 | ||
| 117 | DDRC = 1<<PC1 | 1<<PC0; |
157 | DDRC = 1<<PC1 | 1<<PC0; |
| 118 | PORTC = (1<<PC1) | (1<<PC0); |
158 | PORTC = (1<<PC1) | (1<<PC0); |
| 119 | 159 | ||
| 120 | sei(); |
160 | sei(); |
| 121 | 161 | ||
| 122 | Can_Message_t rxMsg; |
162 | Can_Message_t rxMsg; |
| - | 163 | Can_Message_t timeMsg; |
|
| - | 164 | uint32_t time = Timebase_CurrentTime(); |
|
| - | 165 | uint32_t time1 = time; |
|
| - | 166 | uint32_t unixtime = 0; |
|
| 123 | uint16_t rxByte; |
167 | uint16_t rxByte; |
| 124 | uint8_t i = 0; |
168 | uint8_t i = 0; |
| - | 169 | ||
| - | 170 | timeMsg.ExtendedFlag = 1; |
|
| - | 171 | timeMsg.RemoteFlag = 0; |
|
| - | 172 | //timeMsg.Id = (CAN_NMT << CAN_SHIFT_CLASS) | (CAN_NMT_TIME << CAN_SHIFT_NMT_TYPE); |
|
| - | 173 | timeMsg.Id = 0; //Same thing, and lib's can.h is not updated. |
|
| - | 174 | timeMsg.DataLength = 8; |
|
| 125 | 175 | ||
| 126 | /* main loop */ |
176 | /* main loop */ |
| 127 | while (1) { |
177 | while (1) { |
| 128 | /* service the CAN routines */ |
178 | /* service the CAN routines */ |
| 129 | Can_Service(); |
179 | Can_Service(); |
| Line 151... | Line 201... | ||
| 151 | while (rxByte != UART_NO_DATA) { |
201 | while (rxByte != UART_NO_DATA) { |
| 152 | /* parse byte! */ |
202 | /* parse byte! */ |
| 153 | UartParseByte((uint8_t)(rxByte & 0x00FF)); |
203 | UartParseByte((uint8_t)(rxByte & 0x00FF)); |
| 154 | /* receive next */ |
204 | /* receive next */ |
| 155 | rxByte = uart_getc(); |
205 | rxByte = uart_getc(); |
| - | 206 | } |
|
| - | 207 | ||
| - | 208 | time1 = Timebase_CurrentTime(); |
|
| - | 209 | if ((time1 - time) > 1000) { |
|
| - | 210 | time = time1; |
|
| - | 211 | timeMsg.Data.dwords[0] = ++unixtime; |
|
| - | 212 | IncTime(); |
|
| - | 213 | timeMsg.Data.dwords[1] = date.packed; |
|
| - | 214 | Can_Send(&timeMsg); |
|
| 156 | } |
215 | } |
| 157 | } |
216 | } |
| 158 | 217 | ||
| 159 | return 0; |
218 | return 0; |
| 160 | } |
219 | } |