Rev 33 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 33 | Rev 48 | ||
|---|---|---|---|
| 1 | /********************************************************************* |
1 | /********************************************************************* |
| 2 | * |
2 | * |
| 3 | * Main application |
3 | * Main application |
| 4 | * |
4 | * |
| 5 | ********************************************************************* |
5 | ********************************************************************* |
| 6 | * FileName: Main.c |
6 | * FileName: Main.c |
| 7 | * |
7 | * |
| 8 | * Author Date Comment |
8 | * Author Date Comment |
| 9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | * Johan Böhlin 21-10-06 Original (Rev 1.0) |
10 | * Johan Böhlin 21-10-06 Original (Rev 1.0) |
| 11 | ********************************************************************/ |
11 | ********************************************************************/ |
| 12 | 12 | ||
| 13 | #include "../Include/compiler.h" |
13 | #include "../Include/compiler.h" |
| 14 | #include "../Include/stackTasks.h" |
14 | #include "../Include/stackTasks.h" |
| 15 | #include "../Include/Tick.h" |
15 | #include "../Include/Tick.h" |
| 16 | 16 | ||
| 17 | #ifdef USE_CAN |
17 | #ifdef USE_CAN |
| 18 | #include "../Include/CAN.h" |
18 | #include "../Include/CAN.h" |
| 19 | #endif |
19 | #endif |
| 20 | 20 | ||
| 21 | #ifdef USE_UART |
21 | #ifdef USE_UART |
| 22 | #include "../Include/uart.h" |
22 | #include "../Include/uart.h" |
| 23 | #endif |
23 | #endif |
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | static void mainInit(void); |
27 | static void mainInit(void); |
| 28 | 28 | ||
| 29 | static BOOL heartBeatEnabled = FALSE; |
29 | static BOOL heartBeatEnabled = FALSE; |
| - | 30 | BYTE ldc = 50; |
|
| 30 | 31 | ||
| 31 | void main() |
32 | void main() |
| 32 | { |
33 | { |
| 33 | static TICK t = 0; |
34 | static TICK t = 0; |
| 34 | 35 | ||
| 35 | // Inits |
36 | // Inits |
| 36 | 37 | ||
| 37 | mainInit(); |
38 | mainInit(); |
| 38 | 39 | ||
| 39 | #ifdef USE_CAN |
40 | #ifdef USE_CAN |
| 40 | canInit(); |
41 | canInit(); |
| 41 | #endif |
42 | #endif |
| 42 | 43 | ||
| 43 | #ifdef USE_UART |
44 | #ifdef USE_UART |
| 44 | uartInit(); |
45 | uartInit(); |
| 45 | #endif |
46 | #endif |
| 46 | 47 | ||
| 47 | tickInit(); |
48 | tickInit(); |
| 48 | 49 | ||
| 49 | while(1) |
50 | while(1) |
| 50 | { |
51 | { |
| - | 52 | #ifdef USE_CAN |
|
| 51 | if ((tickGet()-t)>=5*TICK_SECOND && heartBeatEnabled==TRUE) |
53 | if ((tickGet()-t)>=5*TICK_SECOND && heartBeatEnabled==TRUE) |
| 52 | { |
54 | { |
| 53 | CAN_MESSAGE cm; |
55 | CAN_MESSAGE cm; |
| 54 | 56 | ||
| 55 | t = tickGet(); |
57 | t = tickGet(); |
| - | 58 | ||
| - | 59 | // Send heartbeat |
|
| - | 60 | cm.ident=0x00000666; |
|
| - | 61 | cm.extended=FALSE; |
|
| - | 62 | cm.data_length=1; |
|
| - | 63 | cm.data[0]='H'; |
|
| - | 64 | ||
| - | 65 | while(!canSendMessage(cm)); |
|
| - | 66 | ||
| - | 67 | ||
| - | 68 | } |
|
| - | 69 | #endif |
|
| - | 70 | } |
|
| - | 71 | } |
|
| - | 72 | ||
| - | 73 | ||
| - | 74 | #pragma interruptlow HighISR |
|
| - | 75 | void HighISR(void) |
|
| - | 76 | { |
|
| - | 77 | //if(ldc--<=1) {LED0_IO=~LED0_IO; ldc=50; } |
|
| - | 78 | ||
| - | 79 | #ifdef USE_CAN |
|
| - | 80 | canISR(); |
|
| - | 81 | #endif |
|
| - | 82 | ||
| - | 83 | #ifdef USE_UART |
|
| - | 84 | uartISR(); |
|
| - | 85 | #endif |
|
| - | 86 | ||
| - | 87 | if (INTCONbits.INT0IE && INTCONbits.INT0IF) |
|
| - | 88 | { |
|
| - | 89 | static TICK t = 0; |
|
| - | 90 | CAN_MESSAGE cm; |
|
| - | 91 | ||
| - | 92 | if ((tickGet()-t)>TICK_SECOND/2) |
|
| - | 93 | { |
|
| - | 94 | ||
| - | 95 | LED0_IO=~LED0_IO; |
|
| 56 | 96 | ||
| 57 | // Send heartbeat |
97 | // Send heartbeat |
| 58 | cm.ident=0x00000666; |
98 | cm.ident=0x00000666; |
| 59 | cm.extended=FALSE; |
99 | cm.extended=FALSE; |
| 60 | cm.data_length=1; |
100 | cm.data_length=1; |
| 61 | cm.data[0]='H'; |
101 | cm.data[0]='H'; |
| 62 | 102 | ||
| 63 | while(!canSendMessage(cm)); |
103 | while(!canSendMessage(cm)); |
| 64 | - | ||
| 65 | 104 | ||
| 66 | |
105 | t=tickGet(); |
| 67 | } |
106 | } |
| 68 | } |
- | |
| 69 | - | ||
| 70 | 107 | ||
| 71 | #pragma interruptlow HighISR |
- | |
| 72 |
|
108 | INTCONbits.INT0IF=0; |
| 73 | |
109 | } |
| 74 | #ifdef USE_CAN |
- | |
| 75 | canISR(); |
- | |
| 76 | #endif |
- | |
| 77 | 110 | ||
| 78 |
|
111 | //if (RCSTAbits.OERR) { RCSTAbits.CREN=0; RCSTAbits.CREN=1; } |
| 79 | uartISR(); |
- | |
| 80 | #endif |
- | |
| 81 | 112 | ||
| 82 | tickUpdate(); |
113 | tickUpdate(); |
| 83 | } |
114 | } |
| 84 | #pragma code highVector=0x08 |
115 | #pragma code highVector=0x08 |
| 85 | void HighVector (void) |
116 | void HighVector (void) |
| 86 | { |
117 | { |
| 87 | _asm goto HighISR _endasm |
118 | _asm goto HighISR _endasm |
| 88 | } |
119 | } |
| 89 | #pragma code // Return to default code section |
120 | #pragma code // Return to default code section |
| 90 | 121 | ||
| 91 | 122 | ||
| 92 | 123 | ||
| 93 | #pragma interruptlow LowISR |
124 | #pragma interruptlow LowISR |
| 94 | void LowISR(void) |
125 | void LowISR(void) |
| 95 | { |
126 | { |
| 96 | 127 | ||
| 97 | } |
128 | } |
| 98 | #pragma code lowVector=0x18 |
129 | #pragma code lowVector=0x18 |
| 99 | void LowVector (void) |
130 | void LowVector (void) |
| 100 | { |
131 | { |
| 101 | _asm goto LowISR _endasm |
132 | _asm goto LowISR _endasm |
| 102 | } |
133 | } |
| 103 | #pragma code // Return to default code section |
134 | #pragma code // Return to default code section |
| 104 | 135 | ||
| 105 | 136 | ||
| 106 | /* |
137 | /* |
| 107 | * Function: mainInit |
138 | * Function: mainInit |
| 108 | * |
139 | * |
| 109 | * Input: none |
140 | * Input: none |
| 110 | * Output: none |
141 | * Output: none |
| 111 | * Pre-conditions: none |
142 | * Pre-conditions: none |
| 112 | * Affects: Se code |
143 | * Affects: Se code |
| 113 | * Depends: none. |
144 | * Depends: none. |
| 114 | */ |
145 | */ |
| 115 | void mainInit() |
146 | void mainInit() |
| 116 | { |
147 | { |
| 117 | LED0_TRIS=0; |
148 | LED0_TRIS=0; |
| 118 | 149 | ||
| 119 | // Enable internal PORTB pull-ups |
150 | // Enable internal PORTB pull-ups |
| 120 | INTCON2bits.RBPU = 0; |
151 | INTCON2bits.RBPU = 0; |
| 121 | 152 | ||
| 122 | // Enable Interrupts |
153 | // Enable Interrupts |
| 123 | INTCONbits.GIEH = 1; |
154 | INTCONbits.GIEH = 1; |
| 124 | INTCONbits.GIEL = 1; |
155 | INTCONbits.GIEL = 1; |
| - | 156 | ||
| - | 157 | ||
| - | 158 | // RB0 interrupt |
|
| - | 159 | INTCON2bits.INTEDG0 = 1; //rising edge |
|
| - | 160 | INTCONbits.INT0IE = 1; |
|
| - | 161 | ||
| - | 162 | // Enable interrupt prirority |
|
| - | 163 | RCONbits.IPEN=1; |
|
| - | 164 | ||
| 125 | 165 | ||
| 126 | } |
166 | } |
| 127 | 167 | ||
| 128 | /* |
168 | /* |
| 129 | * Function: canParse |
169 | * Function: canParse |
| 130 | * |
170 | * |
| 131 | * Input: Received can message |
171 | * Input: Received can message |
| 132 | * Output: none |
172 | * Output: none |
| 133 | * Pre-conditions: canInit and received packet. |
173 | * Pre-conditions: canInit and received packet. |
| 134 | * Affects: Sensors/actuators/etc. See code. |
174 | * Affects: Sensors/actuators/etc. See code. |
| 135 | * Depends: none. |
175 | * Depends: none. |
| 136 | */ |
176 | */ |
| 137 | #ifdef USE_CAN |
177 | #ifdef USE_CAN |
| 138 | void canParse(CAN_MESSAGE cm) |
178 | void canParse(CAN_MESSAGE cm) |
| 139 | { |
179 | { |
| 140 | int i=0; |
180 | int i=0; |
| 141 | 181 | ||
| 142 | uartPutc(UART_START_BYTE); |
182 | uartPutc(UART_START_BYTE); |
| 143 | uartPutc((BYTE)(cm.ident)); |
183 | uartPutc((BYTE)(cm.ident)); |
| 144 | uartPutc((BYTE)(cm.ident>>8)); |
184 | uartPutc((BYTE)(cm.ident>>8)); |
| 145 | uartPutc((BYTE)(cm.ident>>16)); |
185 | uartPutc((BYTE)(cm.ident>>16)); |
| 146 | uartPutc((BYTE)(cm.ident>>24)); |
186 | uartPutc((BYTE)(cm.ident>>24)); |
| 147 | uartPutc(cm.extended); |
187 | uartPutc(cm.extended); |
| 148 | uartPutc(cm.data_length); |
188 | uartPutc(cm.data_length); |
| 149 | for(i=0;i<8;i++) uartPutc(cm.data[i]); |
189 | for(i=0;i<8;i++) uartPutc(cm.data[i]); |
| 150 | uartPutc(UART_END_BYTE); |
190 | uartPutc(UART_END_BYTE); |
| 151 | 191 | ||
| 152 | } |
192 | } |
| 153 | #endif |
193 | #endif |
| 154 | 194 | ||
| 155 | 195 | ||
| 156 | /* |
196 | /* |
| 157 | * Function: uartParse |
197 | * Function: uartParse |
| 158 | * |
198 | * |
| 159 | * Input: Received uart message |
199 | * Input: Received uart message |
| 160 | * Output: none |
200 | * Output: none |
| 161 | * Pre-conditions: uartInit and received byte. |
201 | * Pre-conditions: uartInit and received byte. |
| 162 | * Affects: Sensors/actuators/etc. See code. |
202 | * Affects: Sensors/actuators/etc. See code. |
| 163 | * Depends: none. |
203 | * Depends: none. |
| 164 | */ |
204 | */ |
| 165 | #ifdef USE_UART |
205 | #ifdef USE_UART |
| 166 | void uartParse(BYTE c) |
206 | void uartParse(BYTE c) |
| 167 | { |
207 | { |
| 168 | BYTE i; |
208 | BYTE i; |
| 169 | unsigned int wait = 0; |
209 | unsigned int wait = 0; |
| 170 | CAN_MESSAGE cm; |
210 | CAN_MESSAGE cm; |
| 171 | static BOOL waitingMessage = FALSE; |
211 | static BOOL waitingMessage = FALSE; |
| 172 | static TICK timeout=0; |
212 | static TICK timeout=0; |
| 173 | static BYTE count=0; |
213 | static BYTE count=0; |
| - | 214 | ||
| 174 | 215 | ||
| 175 | if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20) |
216 | if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20) |
| 176 | { |
217 | { |
| 177 | waitingMessage=FALSE; |
218 | waitingMessage=FALSE; |
| 178 | } |
219 | } |
| - | 220 | ||
| 179 | 221 | ||
| 180 | if (waitingMessage==TRUE) |
222 | if (waitingMessage==TRUE) |
| 181 | { |
223 | { |
| - | 224 | ||
| 182 | timeout=tickGet(); |
225 | timeout=tickGet(); |
| 183 | 226 | ||
| 184 | // UART END |
227 | // UART END |
| 185 | if(count>=14) |
228 | if(count>=14) |
| 186 | { |
229 | { |
| 187 | if (c==UART_END_BYTE) |
230 | if (c==UART_END_BYTE) |
| 188 | { |
231 | { |
| 189 | while(!canSendMessage(cm)); |
232 | while(!canSendMessage(cm)); |
| 190 | } |
233 | } |
| 191 | waitingMessage=FALSE; |
234 | waitingMessage=FALSE; |
| 192 | return; |
235 | return; |
| 193 | } |
236 | } |
| 194 | 237 | ||
| 195 | // data |
238 | // data |
| 196 | if(count>=6) |
239 | if(count>=6) |
| 197 | { |
240 | { |
| 198 | cm.data[count-6]=c; |
241 | cm.data[count-6]=c; |
| 199 | count++; |
242 | count++; |
| 200 | return; |
243 | return; |
| 201 | } |
244 | } |
| 202 | 245 | ||
| 203 | // data length |
246 | // data length |
| 204 | if(count>=5) |
247 | if(count>=5) |
| 205 | { |
248 | { |
| 206 | cm.data_length=c; |
249 | cm.data_length=c; |
| 207 | count++; |
250 | count++; |
| 208 | return; |
251 | return; |
| 209 | } |
252 | } |
| 210 | 253 | ||
| 211 | // extended |
254 | // extended |
| 212 | if(count>=4) |
255 | if(count>=4) |
| 213 | { |
256 | { |
| 214 | cm.extended=c; |
257 | cm.extended=c; |
| 215 | count++; |
258 | count++; |
| 216 | return; |
259 | return; |
| 217 | } |
260 | } |
| 218 | 261 | ||
| 219 | // ident |
262 | // ident |
| 220 | if(count>=0) |
263 | if(count>=0) |
| 221 | { |
264 | { |
| 222 | cm.ident+=((DWORD)c<<(count*8)); |
265 | cm.ident+=((DWORD)c<<(count*8)); |
| 223 | count++; |
266 | count++; |
| 224 | return; |
267 | return; |
| 225 | } |
268 | } |
| 226 | 269 | ||
| 227 | } |
270 | } |
| 228 | 271 | ||
| 229 | 272 | ||
| 230 | if (c==UART_START_BYTE && waitingMessage==FALSE) |
273 | if (c==UART_START_BYTE && waitingMessage==FALSE) |
| 231 | { |
274 | { |
| 232 | waitingMessage=TRUE; |
275 | waitingMessage=TRUE; |
| 233 | timeout=tickGet(); |
276 | timeout=tickGet(); |
| 234 | count=0; |
277 | count=0; |
| 235 | cm.ident=0; |
278 | cm.ident=0; |
| 236 | return; |
279 | return; |
| 237 | } |
280 | } |
| 238 | - | ||
| 239 | 281 | ||
| 240 | if(c=='1' && waitingMessage==FALSE) |
282 | if(c=='1' && waitingMessage==FALSE) |
| 241 | { |
283 | { |
| 242 | cm.ident=0x00000123; |
284 | cm.ident=0x00000123; |
| 243 | cm.extended=FALSE; |
285 | cm.extended=FALSE; |
| 244 | cm.data_length=4; |
286 | cm.data_length=4; |
| 245 | cm.data[3]='H'; |
287 | cm.data[3]='H'; |
| 246 | cm.data[2]='e'; |
288 | cm.data[2]='e'; |
| 247 | cm.data[1]='j'; |
289 | cm.data[1]='j'; |
| 248 | cm.data[0]='!'; |
290 | cm.data[0]='!'; |
| 249 | 291 | ||
| 250 | while(!canSendMessage(cm)); |
292 | while(!canSendMessage(cm)); |
| 251 | } |
293 | } |
| 252 | 294 | ||
| 253 | if(c=='2' && waitingMessage==FALSE) |
295 | if(c=='2' && waitingMessage==FALSE) |
| 254 | { |
296 | { |
| 255 | cm.ident=0x0ABCDEF0; |
297 | cm.ident=0x0ABCDEF0; |
| 256 | cm.extended=TRUE; |
298 | cm.extended=TRUE; |
| 257 | cm.data_length=8; |
299 | cm.data_length=8; |
| 258 | cm.data[7]='G'; |
300 | cm.data[7]='G'; |
| 259 | cm.data[6]='o'; |
301 | cm.data[6]='o'; |
| 260 | cm.data[5]='d'; |
302 | cm.data[5]='d'; |
| 261 | cm.data[4]=' '; |
303 | cm.data[4]=' '; |
| 262 | cm.data[3]='d'; |
304 | cm.data[3]='d'; |
| 263 | cm.data[2]='a'; |
305 | cm.data[2]='a'; |
| 264 | cm.data[1]='g'; |
306 | cm.data[1]='g'; |
| 265 | cm.data[0]='!'; |
307 | cm.data[0]='!'; |
| 266 | 308 | ||
| 267 | while(!canSendMessage(cm)); |
309 | while(!canSendMessage(cm)); |
| 268 | } |
310 | } |
| 269 | 311 | ||
| 270 | if(c=='3' && waitingMessage==FALSE) |
312 | if(c=='3' && waitingMessage==FALSE) |
| 271 | { |
313 | { |
| 272 | cm.ident=0x00000010; |
314 | cm.ident=0x00000010; |
| 273 | cm.extended=FALSE; |
315 | cm.extended=FALSE; |
| 274 | cm.data_length=6; |
316 | cm.data_length=6; |
| 275 | cm.data[5]='5'; |
317 | cm.data[5]='5'; |
| 276 | cm.data[4]='4'; |
318 | cm.data[4]='4'; |
| 277 | cm.data[3]='3'; |
319 | cm.data[3]='3'; |
| 278 | cm.data[2]='2'; |
320 | cm.data[2]='2'; |
| 279 | cm.data[1]='1'; |
321 | cm.data[1]='1'; |
| 280 | cm.data[0]='0'; |
322 | cm.data[0]='0'; |
| 281 | 323 | ||
| 282 | while(!canSendMessage(cm)); |
324 | while(!canSendMessage(cm)); |
| 283 | } |
325 | } |
| 284 | 326 | ||
| 285 | 327 | ||
| 286 | 328 | ||
| 287 | } |
329 | } |
| 288 | #endif |
330 | #endif |
| 289 | 331 | ||