Rev 110 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 110 | Rev 260 | ||
|---|---|---|---|
| Line 2... | Line 2... | ||
| 2 | * |
2 | * |
| 3 | * Main application |
3 | * Main application |
| 4 | * |
4 | * |
| 5 | ********************************************************************* |
5 | ********************************************************************* |
| 6 | * FileName: $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBoot/Source/Main.c $ |
6 | * FileName: $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBoot/Source/Main.c $ |
| 7 | * Last changed: $LastChangedDate: |
7 | * Last changed: $LastChangedDate: 2007-01-28 14:58:11 +0100 (Sun, 28 Jan 2007) $ |
| 8 | * By: $LastChangedBy: johboh $ |
8 | * By: $LastChangedBy: johboh $ |
| 9 | * Revision: $Revision: |
9 | * Revision: $Revision: 260 $ |
| 10 | ********************************************************************/ |
10 | ********************************************************************/ |
| 11 | 11 | ||
| 12 | #include <compiler.h> |
12 | #include <compiler.h> |
| 13 | #include <stackTasks.h> |
13 | #include <stackTasks.h> |
| 14 | #include <CANdefs.h> |
14 | #include <CANdefs.h> |
| Line 89... | Line 89... | ||
| 89 | if (EERead(NODE_ID_EE)==NODE_HAS_ID) |
89 | if (EERead(NODE_ID_EE)==NODE_HAS_ID) |
| 90 | { |
90 | { |
| 91 | MY_ID=(((WORD)EERead(NODE_ID_EE + 1))<<8)+EERead(NODE_ID_EE + 2); |
91 | MY_ID=(((WORD)EERead(NODE_ID_EE + 1))<<8)+EERead(NODE_ID_EE + 2); |
| 92 | MY_NID=EERead(NODE_ID_EE + 3); |
92 | MY_NID=EERead(NODE_ID_EE + 3); |
| 93 | } |
93 | } |
| - | 94 | ||
| 94 | 95 | ||
| 95 | // Send bootloader startup heatbeat |
96 | // Send bootloader startup heatbeat |
| 96 | outCmHeart.funct = FUNCT_BOOTLOADER; |
97 | outCmHeart.funct = FUNCT_BOOTLOADER; |
| 97 | outCmHeart.funcc = FUNCC_BOOT_HEARTBEAT; |
98 | outCmHeart.funcc = FUNCC_BOOT_HEARTBEAT; |
| 98 | outCmHeart.nid = MY_NID; |
99 | outCmHeart.nid = MY_NID; |
| 99 | outCmHeart.sid = MY_ID; |
100 | outCmHeart.sid = MY_ID; |
| 100 | outCmHeart.data_length = 1; |
101 | outCmHeart.data_length = 1; |
| 101 | outCmHeart.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_BOOT_STARTUP; |
102 | outCmHeart.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_BOOT_STARTUP; |
| 102 | while(!canSendMessage(outCmHeart,PRIO_HIGH)); |
103 | while(!canSendMessage(outCmHeart,PRIO_HIGH)); |
| 103 | 104 | ||
| - | 105 | LED0_IO= 1; |
|
| 104 | 106 | ||
| 105 | for(;;) |
107 | for(;;) |
| 106 | { |
108 | { |
| 107 | BYTE i; |
109 | BYTE i; |
| 108 | CAN_PROTO_MESSAGE outCm; |
110 | CAN_PROTO_MESSAGE outCm; |
| Line 128... | Line 130... | ||
| 128 | t = tickGet(); |
130 | t = tickGet(); |
| 129 | errMsg = ACK_ERR_NO_ERROR; |
131 | errMsg = ACK_ERR_NO_ERROR; |
| 130 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
132 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
| 131 | pgs = pgsSEND_ACK; |
133 | pgs = pgsSEND_ACK; |
| 132 | // save pgm adress. |
134 | // save pgm adress. |
| 133 | 135 | ||
| 134 | } |
136 | } |
| 135 | 137 | ||
| 136 | // Bootloader timeout |
138 | // Bootloader timeout |
| 137 | if ((tickGet()-tBoot)>BOOTLOADER_INIT_TIMOUT) |
139 | if ((tickGet()-tBoot)>BOOTLOADER_INIT_TIMOUT) |
| 138 | { |
140 | { |
| - | 141 | LED0_IO= 0; |
|
| 139 | pgs = pgsDONE; |
142 | pgs = pgsDONE; |
| 140 | } |
143 | } |
| 141 | break; |
144 | break; |
| 142 | 145 | ||
| 143 | case pgsSEND_ACK: |
146 | case pgsSEND_ACK: |
| 144 | // send ack |
147 | // send ack |
| 145 | outCm.funct = FUNCT_BOOTLOADER; |
148 | outCm.funct = FUNCT_BOOTLOADER; |
| 146 | outCm.funcc = FUNCC_BOOT_ACK; |
149 | outCm.funcc = FUNCC_BOOT_ACK; |
| Line 231... | Line 234... | ||
| 231 | else |
234 | else |
| 232 | { |
235 | { |
| 233 | pgs = pgsWRITE_PROGRAM; |
236 | pgs = pgsWRITE_PROGRAM; |
| 234 | afterAckPgs = pgsDONE; |
237 | afterAckPgs = pgsDONE; |
| 235 | } |
238 | } |
| 236 | } |
239 | } |
| 237 | 240 | ||
| 238 | // If bytes received 64, write.. |
241 | // If bytes received 64, write.. |
| 239 | if (bytesReceived==64) |
242 | if (bytesReceived==64) |
| 240 | { |
243 | { |
| 241 | LED0_IO=~LED0_IO; |
244 | LED0_IO=~LED0_IO; |
| 242 | pgs = pgsWRITE_PROGRAM; |
245 | pgs = pgsWRITE_PROGRAM; |
| 243 | afterAckPgs = pgsWAIT_PGM_PACKET; |
246 | afterAckPgs = pgsWAIT_PGM_PACKET; |
| 244 | // TODO! Send ack more than once.. |
247 | // TODO! Send ack more than once.. |
| 245 | } |
248 | } |
| 246 | 249 | ||
| 247 | 250 | ||
| 248 | break; |
251 | break; |
| 249 | 252 | ||
| 250 | 253 | ||
| 251 | case pgsWRITE_PROGRAM: |
254 | case pgsWRITE_PROGRAM: |
| 252 | 255 | ||
| 253 | ClrWdt(); |
256 | ClrWdt(); |
| 254 | 257 | ||
| 255 | // Write program and send ack. |
258 | // Write program and send ack. |
| 256 | 259 | ||
| 257 | // Check addr limit |
260 | // Check addr limit |
| Line 274... | Line 277... | ||
| 274 | EECON1bits.CFGS = 0; |
277 | EECON1bits.CFGS = 0; |
| 275 | // enable write to memory |
278 | // enable write to memory |
| 276 | EECON1bits.WREN = 1; |
279 | EECON1bits.WREN = 1; |
| 277 | // enable Row Erase operation |
280 | // enable Row Erase operation |
| 278 | EECON1bits.FREE = 1; |
281 | EECON1bits.FREE = 1; |
| 279 | // disable interrupts |
282 | // disable interrupts |
| 280 | INTCONbits.GIE = 0; |
283 | INTCONbits.GIE = 0; |
| 281 | 284 | ||
| 282 | // Do it! |
285 | // Do it! |
| 283 | EECON2 = 0x55; |
286 | EECON2 = 0x55; |
| 284 | EECON2 = 0xAA; |
287 | EECON2 = 0xAA; |
| 285 | EECON1bits.WR = 1; |
288 | EECON1bits.WR = 1; |
| 286 | 289 | ||
| 287 | // enable interrupts |
290 | // enable interrupts |
| 288 | INTCONbits.GIE = 1; |
291 | INTCONbits.GIE = 1; |
| 289 | 292 | ||
| 290 | //Load Table pointer registers with base address to program. |
293 | //Load Table pointer registers with base address to program. |
| 291 | TBLPTRU = (BYTE)((pgmAddress & 0xFF0000)>>16); |
294 | TBLPTRU = (BYTE)((pgmAddress & 0xFF0000)>>16); |
| 292 | TBLPTRH = (BYTE)((pgmAddress & 0xFF00)>>8); |
295 | TBLPTRH = (BYTE)((pgmAddress & 0xFF00)>>8); |
| 293 | TBLPTRL = (BYTE)(pgmAddress & 0xFF); |
296 | TBLPTRL = (BYTE)(pgmAddress & 0xFF); |
| 294 | 297 | ||
| 295 | // Load bytes |
298 | // Load bytes |
| 296 | for(i=0;i<63;i++) |
299 | for(i=0;i<63;i++) |
| 297 | { |
300 | { |
| 298 | TABLAT=pgmData[i]; |
301 | TABLAT=pgmData[i]; |
| 299 | TBLWTPOSTINC_Func(); |
302 | TBLWTPOSTINC_Func(); |
| Line 313... | Line 316... | ||
| 313 | 316 | ||
| 314 | // Do it! |
317 | // Do it! |
| 315 | EECON2 = 0x55; |
318 | EECON2 = 0x55; |
| 316 | EECON2 = 0xAA; |
319 | EECON2 = 0xAA; |
| 317 | EECON1bits.WR = 1; |
320 | EECON1bits.WR = 1; |
| 318 | 321 | ||
| 319 | // enable interrupts |
322 | // enable interrupts |
| 320 | INTCONbits.GIE = 1; |
323 | INTCONbits.GIE = 1; |
| 321 | 324 | ||
| 322 | // disable write to memory |
325 | // disable write to memory |
| 323 | EECON1bits.WREN = 0; |
326 | EECON1bits.WREN = 0; |
| 324 | 327 | ||
| 325 | pgmAddress += 64; |
328 | pgmAddress += 64; |
| 326 | bytesReceived = 0; |
329 | bytesReceived = 0; |
| 327 | errMsg = ACK_ERR_NO_ERROR; |
330 | errMsg = ACK_ERR_NO_ERROR; |
| 328 | pgs = pgsSEND_ACK; |
331 | pgs = pgsSEND_ACK; |
| 329 | 332 | ||
| 330 | break; |
333 | break; |
| 331 | 334 | ||
| 332 | 335 | ||
| 333 | case pgsDONE: |
336 | case pgsDONE: |
| 334 | _asm goto RM_RESET_VECTOR _endasm |
337 | _asm goto RM_RESET_VECTOR _endasm |
| 335 | break; |
338 | break; |
| 336 | 339 | ||
| 337 | 340 | ||
| 338 | 341 | ||
| 339 | 342 | ||
| 340 | default: |
343 | default: |
| 341 | pgs = pgsWATING_START; |
344 | pgs = pgsWATING_START; |
| 342 | break; |
345 | break; |
| 343 | } |
346 | } |
| 344 | 347 | ||
| 345 | 348 | ||
| 346 | } |
349 | } |
| 347 | } |
350 | } |
| 348 | 351 | ||
| 349 | 352 | ||
| 350 | /* |
353 | /* |
| 351 | * Function: mainInit |
354 | * Function: mainInit |
| 352 | * |
355 | * |
| 353 | * Input: none |
356 | * Input: none |
| 354 | * Output: none |
357 | * Output: none |
| Line 360... | Line 363... | ||
| 360 | { |
363 | { |
| 361 | LED0_TRIS=0; |
364 | LED0_TRIS=0; |
| 362 | 365 | ||
| 363 | // Enable internal PORTB pull-ups |
366 | // Enable internal PORTB pull-ups |
| 364 | INTCON2bits.RBPU = 0; |
367 | INTCON2bits.RBPU = 0; |
| 365 | 368 | ||
| 366 | // Enable Interrupts |
369 | // Enable Interrupts |
| 367 | INTCONbits.GIEH = 1; |
370 | INTCONbits.GIEH = 1; |
| 368 | INTCONbits.GIEL = 1; |
371 | INTCONbits.GIEL = 1; |
| 369 | 372 | ||
| 370 | // Enable interrupt prirority |
373 | // Enable interrupt prirority |
| 371 | RCONbits.IPEN=1; |
374 | RCONbits.IPEN=1; |
| 372 | 375 | ||
| 373 | 376 | ||
| 374 | } |
377 | } |
| 375 | 378 | ||
| 376 | 379 | ||
| 377 | 380 | ||
| 378 | /// CAN------------------------------------------------------- |
381 | /// CAN------------------------------------------------------- |
| 379 | 382 | ||
| 380 | 383 | ||
| 381 | #ifdef USE_CAN |
384 | #ifdef USE_CAN |
| 382 | 385 | ||
| 383 | 386 | ||
| 384 | /* |
387 | /* |
| 385 | * Function: canInit |
388 | * Function: canInit |
| 386 | * |
389 | * |
| 387 | * Input: none |
390 | * Input: none |
| Line 395... | Line 398... | ||
| 395 | CANCON = 0x80; //request config mode |
398 | CANCON = 0x80; //request config mode |
| 396 | while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request |
399 | while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request |
| 397 | 400 | ||
| 398 | // BAUD AND TQS |
401 | // BAUD AND TQS |
| 399 | 402 | ||
| - | 403 | // 500Khz@40Mhz (new by the book) |
|
| - | 404 | BRGCON1 = 0x04; |
|
| - | 405 | BRGCON2 = 0xD1; |
|
| - | 406 | BRGCON3 = 0x81; |
|
| - | 407 | ||
| 400 |
|
408 | //BRGCON1=0; |
| 401 | 409 | ||
| 402 | // Sync_Seq = 2 x TQ = 1 |
410 | // Sync_Seq = 2 x TQ = 1 |
| 403 | BRGCON1bits. |
411 | //BRGCON1bits.SJW1=0; |
| 404 | BRGCON1bits. |
412 | //BRGCON1bits.SJW0=1; |
| 405 | 413 | ||
| 406 | // Setting BRP. |
414 | // Setting BRP. |
| 407 |
|
415 | //BRGCON1=BRGCON1|CAN_BRP; |
| 408 | 416 | ||
| 409 | // Maximum PHEG1 |
417 | // Maximum PHEG1 |
| 410 | BRGCON2bits. |
418 | //BRGCON2bits.SEG2PHTS = 1; |
| 411 | 419 | ||
| 412 | // Phase_Seg1 = 2 x TQ = 1 |
420 | // Phase_Seg1 = 2 x TQ = 1 |
| 413 | BRGCON2bits. |
421 | //BRGCON2bits.SEG1PH2 = 0; |
| 414 | BRGCON2bits. |
422 | //BRGCON2bits.SEG1PH1 = 0; |
| 415 | BRGCON2bits. |
423 | //BRGCON2bits.SEG1PH0 = 1; |
| 416 | 424 | ||
| 417 | // Prop_Seq = 2 x TQ = 1 |
425 | // Prop_Seq = 2 x TQ = 1 |
| 418 | BRGCON2bits. |
426 | //BRGCON2bits.PRSEG2 = 0; |
| 419 | BRGCON2bits. |
427 | //BRGCON2bits.PRSEG1 = 0; |
| 420 | BRGCON2bits. |
428 | //BRGCON2bits.PRSEG0 = 1; |
| 421 | 429 | ||
| 422 | // Enableing bus wake-up |
430 | // Enableing bus wake-up |
| 423 | BRGCON3bits. |
431 | //BRGCON3bits.WAKDIS = 0; |
| 424 | 432 | ||
| 425 | // Dont use filter when wakeup |
433 | // Dont use filter when wakeup |
| 426 | BRGCON3bits. |
434 | //BRGCON3bits.WAKFIL = 0; |
| 427 | 435 | ||
| 428 | // Phase_Seg2 = 2 x TQ = 1 |
436 | // Phase_Seg2 = 2 x TQ = 1 |
| 429 | BRGCON3bits. |
437 | //BRGCON3bits.SEG2PH2 = 0; |
| 430 | BRGCON3bits. |
438 | //BRGCON3bits.SEG2PH1 = 0; |
| 431 | BRGCON3bits. |
439 | //BRGCON3bits.SEG2PH0 = 1; |
| 432 | 440 | ||
| 433 | ECANCON=0; |
441 | ECANCON=0; |
| 434 | ECANCONbits.MDSEL1 = 1; |
442 | ECANCONbits.MDSEL1 = 1; |
| 435 | ECANCONbits.MDSEL0 = 0; |
443 | ECANCONbits.MDSEL0 = 0; |
| 436 | 444 | ||
| Line 481... | Line 489... | ||
| 481 | { |
489 | { |
| 482 | 490 | ||
| 483 | // Get which buffer that is ful. |
491 | // Get which buffer that is ful. |
| 484 | ECANCON=(ECANCON&0b00000)|(0b10000|(CANCON&0x0F)); |
492 | ECANCON=(ECANCON&0b00000)|(0b10000|(CANCON&0x0F)); |
| 485 | if (!RXB0CONbits.RXFUL) return FALSE; |
493 | if (!RXB0CONbits.RXFUL) return FALSE; |
| - | 494 | ||
| 486 | 495 | ||
| 487 | //RXB0SIDH 28 27 26 25 24 23 22 21 |
496 | //RXB0SIDH 28 27 26 25 24 23 22 21 |
| 488 | //RXB0SIDL 20 19 18 xx xx xx 17 16 |
497 | //RXB0SIDL 20 19 18 xx xx xx 17 16 |
| 489 | //RXB0EIDH 15 14 13 12 11 10 09 08 |
498 | //RXB0EIDH 15 14 13 12 11 10 09 08 |
| 490 | //RXB0EIDL 07 06 05 04 03 02 01 00 |
499 | //RXB0EIDL 07 06 05 04 03 02 01 00 |