Rev 149 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 149 | Rev 356 | ||
|---|---|---|---|
| 1 | /** |
1 | /** |
| 2 | * CAN test program. |
2 | * CAN test program. |
| 3 | * |
3 | * |
| 4 | * @target AVR |
4 | * @target AVR |
| 5 | * @date 2006-10-29 |
5 | * @date 2006-10-29 |
| 6 | * @author Jimmy Myhrman |
6 | * @author Jimmy Myhrman |
| 7 | * |
7 | * |
| 8 | */ |
8 | */ |
| 9 | 9 | ||
| 10 | /*----------------------------------------------------------------------------- |
10 | /*----------------------------------------------------------------------------- |
| 11 | * Includes |
11 | * Includes |
| 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> |
| 22 | #include <timebase.h> |
23 | #include <timebase.h> |
| 23 | 24 | ||
| 24 | 25 | ||
| 25 | /*----------------------------------------------------------------------------- |
26 | /*----------------------------------------------------------------------------- |
| 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 |
| 37 | * bytes will build up a CAN message according to the protocol described here: |
76 | * bytes will build up a CAN message according to the protocol described here: |
| 38 | * |
77 | * |
| 39 | * http://www.arune.se/projekt/doku.php?id=homeautomation:pc-mjukvara |
78 | * http://www.arune.se/projekt/doku.php?id=homeautomation:pc-mjukvara |
| 40 | * |
79 | * |
| 41 | * @param c |
80 | * @param c |
| 42 | * The received UART byte. |
81 | * The received UART byte. |
| 43 | */ |
82 | */ |
| 44 | void UartParseByte(uint8_t c) { |
83 | void UartParseByte(uint8_t c) { |
| 45 | static Can_Message_t cm; |
84 | static Can_Message_t cm; |
| 46 | static uint8_t waitingMessage = 0; |
85 | static uint8_t waitingMessage = 0; |
| 47 | static uint32_t startTime = 0; |
86 | static uint32_t startTime = 0; |
| 48 | static int8_t count = 0; |
87 | static int8_t count = 0; |
| 49 | 88 | ||
| 50 | /* 50ms timeout */ |
89 | /* 50ms timeout */ |
| 51 | if (waitingMessage && Timebase_PassedTimeMillis(startTime) > 50) { |
90 | if (waitingMessage && Timebase_PassedTimeMillis(startTime) > 50) { |
| 52 | waitingMessage = 0; |
91 | waitingMessage = 0; |
| 53 | } |
92 | } |
| 54 | 93 | ||
| 55 | if (waitingMessage) { |
94 | if (waitingMessage) { |
| 56 | /* save start time */ |
95 | /* save start time */ |
| 57 | startTime = Timebase_CurrentTime(); |
96 | startTime = Timebase_CurrentTime(); |
| 58 | /* UART END */ |
97 | /* UART END */ |
| 59 | if (count >= 15) { |
98 | if (count >= 15) { |
| 60 | if (c == UART_END_BYTE) { |
99 | if (c == UART_END_BYTE) { |
| 61 | PORTC ^= (1<<PC0); |
100 | PORTC ^= (1<<PC0); |
| 62 | Can_Send(&cm); |
101 | Can_Send(&cm); |
| 63 | } |
102 | } |
| 64 | waitingMessage = 0; |
103 | waitingMessage = 0; |
| 65 | return; |
104 | return; |
| 66 | } |
105 | } |
| 67 | /* data */ |
106 | /* data */ |
| 68 | else if (count >= 7) { |
107 | else if (count >= 7) { |
| 69 | cm.Data.bytes[count-7] = c; |
108 | cm.Data.bytes[count-7] = c; |
| 70 | count++; |
109 | count++; |
| 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; |
| 84 | } |
123 | } |
| 85 | /* extended */ |
124 | /* extended */ |
| 86 | else if (count >= 4) { |
125 | else if (count >= 4) { |
| 87 | cm.ExtendedFlag = c; |
126 | cm.ExtendedFlag = c; |
| 88 | count++; |
127 | count++; |
| 89 | return; |
128 | return; |
| 90 | } |
129 | } |
| 91 | /* ident */ |
130 | /* ident */ |
| 92 | else if (count >= 0) { |
131 | else if (count >= 0) { |
| 93 | cm.Id += ((uint32_t)c << (count*8)); |
132 | cm.Id += ((uint32_t)c << (count*8)); |
| 94 | count++; |
133 | count++; |
| 95 | return; |
134 | return; |
| 96 | } |
135 | } |
| 97 | } |
136 | } |
| 98 | 137 | ||
| 99 | if (c == UART_START_BYTE && !waitingMessage) { |
138 | if (c == UART_START_BYTE && !waitingMessage) { |
| 100 | waitingMessage = 1; |
139 | waitingMessage = 1; |
| 101 | startTime = Timebase_CurrentTime(); |
140 | startTime = Timebase_CurrentTime(); |
| 102 | count = 0; |
141 | count = 0; |
| 103 | cm.Id = 0; |
142 | cm.Id = 0; |
| 104 | return; |
143 | return; |
| 105 | } |
144 | } |
| 106 | } |
145 | } |
| 107 | 146 | ||
| 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(); |
| 130 | 180 | ||
| 131 | /* any new CAN messages received? */ |
181 | /* any new CAN messages received? */ |
| 132 | if (Can_Receive(&rxMsg) == CAN_OK) { |
182 | if (Can_Receive(&rxMsg) == CAN_OK) { |
| 133 | PORTC ^= (1<<PC1); |
183 | PORTC ^= (1<<PC1); |
| 134 | /* send message to CanWatcher */ |
184 | /* send message to CanWatcher */ |
| 135 | uart_putc(UART_START_BYTE); |
185 | uart_putc(UART_START_BYTE); |
| 136 | uart_putc((uint8_t)rxMsg.Id); |
186 | uart_putc((uint8_t)rxMsg.Id); |
| 137 | uart_putc((uint8_t)(rxMsg.Id>>8)); |
187 | uart_putc((uint8_t)(rxMsg.Id>>8)); |
| 138 | uart_putc((uint8_t)(rxMsg.Id>>16)); |
188 | uart_putc((uint8_t)(rxMsg.Id>>16)); |
| 139 | uart_putc((uint8_t)(rxMsg.Id>>24)); |
189 | uart_putc((uint8_t)(rxMsg.Id>>24)); |
| 140 | uart_putc(rxMsg.ExtendedFlag); |
190 | uart_putc(rxMsg.ExtendedFlag); |
| 141 | uart_putc(rxMsg.RemoteFlag); |
191 | uart_putc(rxMsg.RemoteFlag); |
| 142 | uart_putc(rxMsg.DataLength); |
192 | uart_putc(rxMsg.DataLength); |
| 143 | for (i=0; i<8; i++) { |
193 | for (i=0; i<8; i++) { |
| 144 | uart_putc(rxMsg.Data.bytes[i]); |
194 | uart_putc(rxMsg.Data.bytes[i]); |
| 145 | } |
195 | } |
| 146 | uart_putc(UART_END_BYTE); |
196 | uart_putc(UART_END_BYTE); |
| 147 | } |
197 | } |
| 148 | 198 | ||
| 149 | /* any UART bytes received? */ |
199 | /* any UART bytes received? */ |
| 150 | rxByte = uart_getc(); |
200 | rxByte = uart_getc(); |
| 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 | } |
| 161 | 220 | ||