Subversion Repositories HomeAutomation

Rev

Rev 771 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * $Id: bios.c 771 2008-05-01 18:01:33Z linlun $
  3.  */
  4.  
  5. #include <inttypes.h>
  6. #include <avr/io.h>
  7. #include <avr/interrupt.h>
  8. #include <avr/wdt.h>
  9. #include <avr/pgmspace.h>
  10. #include <string.h>
  11. #include <util/crc16.h>
  12. #include <config.h>
  13. #include <vectors.h> // Must be included after sfr_defs.h but before any ISR()
  14. #include <drivers/can/can.h>
  15. #include <flash.h>
  16. #include <bios_export.h>
  17. #include CAN_DRIVER_H
  18.  
  19. #if !defined( NODE_HW_ID )
  20. #error No NODE_HW_ID in bios.inc, set NODE_HW_ID=<GENERATE_HW> and Ill generate it for you
  21. #endif
  22.  
  23. #if defined(__AVR_ATmega8__)
  24. #define IVSEL_REG GICR
  25. #elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
  26. #define IVSEL_REG MCUCR
  27. #else
  28. #error AVR device not supported!
  29. #endif
  30.  
  31. #if defined(AUTOSTART)
  32. #if AUTOSTART == 0
  33. #error Please choose a higher AUTOSTART value in bios.inc, the nod will cease to work, and you can only reflash node with ISP
  34. #endif
  35. #if AUTOSTART == 1
  36. #error Please choose a higher AUTOSTART value in bios.inc, there might be problems with updating software on node
  37. #endif
  38. #endif
  39.  
  40. //---------------------------------------------------------------------------
  41. // Private declarations
  42.  
  43. volatile uint8_t bios_msg_full;
  44. Can_Message_t* bios_msg_ptr; // only a pointer to main-local structure to save .bss space
  45. extern void __bios_start; // Start of BIOS area in flash, from ld-script.
  46.  
  47. prog_uint32_t hwid = NODE_HW_ID;
  48.  
  49. #if defined(AUTOSTART)
  50. uint8_t autostart_cnt;
  51. #endif
  52.  
  53. #define BIOS_APP        1
  54. #define BIOS_NOAPP      2
  55. #define BIOS_PGM        3
  56. #define BIOS_END_PGM    4
  57.  
  58. //---------------------------------------------------------------------------
  59. // Private functions
  60.  
  61. void Can_Process(Can_Message_t* msg) {
  62.     if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames.
  63.    
  64.     if ((msg->Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS == CAN_NMT) {
  65.        
  66.         if ((msg->Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE == CAN_NMT_TIME) {
  67.             // Reset watchdog as long as time messages from the master node
  68.             // are received periodically. If anything hangs the CAN communication
  69.             // or the master node is not up, the node will be reset.
  70.             wdt_reset();
  71.             //TODO: Don't care about time right now... Figure out what to do with it.
  72. #if defined(AUTOSTART)
  73.             autostart_cnt++;
  74. #endif
  75.         }
  76.        
  77.         if (((msg->Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE) == CAN_NMT_RESET && msg->DataLength == 4 &&
  78.                 msg->Data.bytes[0] == (hwid&0xff) && msg->Data.bytes[1] == ((hwid>>8)&0xff) &&
  79.                 msg->Data.bytes[2] == ((hwid>>16)&0xff) && msg->Data.bytes[3] == ((hwid>>24)&0xff) ) {
  80.             BIOS_Reset();
  81.         }
  82.         // Copy message to bios buffer if bios is done with the previous message.
  83.         // This is also a check to see if bios is still running. When app is
  84.         // started, bios_msg_full is never reset to 0 so the check fails and all
  85.         // NMT communication except reset is ignored.
  86.         if (!(bios_msg_full)) {
  87.             memcpy(bios_msg_ptr, msg, sizeof(Can_Message_t));
  88.             bios_msg_full = 1;
  89.         }
  90.     } else if (BIOS_CanCallback) {
  91.         //printf("Calling app callback.\n");
  92.         BIOS_CanCallback(msg);
  93.     }
  94. }
  95.  
  96. int main(void) {
  97.     void (*app_reset)(void) = 0; // Function pointer to jump to application reset vector.
  98.     uint8_t bios_state;
  99.     uint8_t nmt_type;
  100.     uint8_t pagebuf[SPM_PAGESIZE];
  101.     // These unneccessary initializations uses 12 bytes flash, just to make the compiler happy!
  102.     uint16_t base_addr = 0;
  103.     uint16_t offset = 0;
  104.     uint16_t addr = 0;
  105.     uint8_t len;
  106.     uint16_t data;
  107.     uint8_t send_msg = 0;
  108.     Can_Message_t tx_msg;
  109.     Can_Message_t bios_msg;
  110.     bios_msg_ptr = &bios_msg;
  111.  
  112. #if defined(AUTOSTART)
  113.     autostart_cnt = 0;
  114. #endif
  115.  
  116.     // Enable watchdog timer to protect against an application locking the
  117.     // node by leaving interrupts disabled.
  118.     wdt_enable(WDTO_2S);
  119.    
  120.     // Move interrupt vectors to start of bootloader section and enable interrupts
  121.     IVSEL_REG = _BV(IVCE);
  122.     IVSEL_REG = _BV(IVSEL);
  123.     sei();
  124.    
  125.     if (Can_Init() != CAN_OK) BIOS_Reset();
  126.    
  127.     tx_msg.RemoteFlag = 0;
  128.     tx_msg.ExtendedFlag = 1;
  129.     tx_msg.Id = CAN_ID_NMT_BIOS_START;
  130.     tx_msg.DataLength = 8;
  131.     tx_msg.Data.bytes[0] = BIOS_VERSION&0xff;
  132.     tx_msg.Data.bytes[1] = (BIOS_VERSION>>8)&0xff;
  133.     tx_msg.Data.bytes[3] = 0x00;
  134.     tx_msg.Data.bytes[4] = hwid&0xff;
  135.     tx_msg.Data.bytes[5] = (hwid>>8)&0xff;
  136.     tx_msg.Data.bytes[6] = (hwid>>16)&0xff;
  137.     tx_msg.Data.bytes[7] = (hwid>>24)&0xff;
  138.  
  139.     if (pgm_read_word(0) == 0xffff) {
  140.         // No application in flash
  141.         // Send CAN_NMT_BIOS_START(BIOS_VERSION, 0)
  142.         tx_msg.Data.bytes[2] = 0;
  143.         bios_state = BIOS_NOAPP;
  144.     } else {
  145.         // Application exists
  146.         // Send CAN_NMT_BIOS_START(BIOS_VERSION, 1)
  147.         tx_msg.Data.bytes[2] = 1;
  148.         bios_state = BIOS_APP;
  149.     }
  150.    
  151.     while (Can_Send(&tx_msg) != CAN_OK);
  152.  
  153.     tx_msg.DataLength = 2; // All msg sent after BIOS_START are length 2 so set it once.
  154.    
  155.     // Main loop
  156.     while (1) {
  157.         // Wait for message
  158.         while (!bios_msg_full) {
  159. #if defined(AUTOSTART)
  160.             if (bios_state == BIOS_APP && autostart_cnt == AUTOSTART) {
  161.                 app_reset();
  162.             }
  163. #endif
  164.         }
  165.        
  166.         nmt_type = (bios_msg.Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE;
  167.        
  168.         switch (bios_state) {
  169.         case BIOS_APP:
  170.             //if (nmt_type == CAN_NMT_START_APP) {
  171.             if (nmt_type == CAN_NMT_START_APP && bios_msg.DataLength == 4 &&
  172.                     bios_msg.Data.bytes[0] == (hwid&0xff) && bios_msg.Data.bytes[1] == ((hwid>>8)&0xff) &&
  173.                     bios_msg.Data.bytes[2] == ((hwid>>16)&0xff) && bios_msg.Data.bytes[3] == ((hwid>>24)&0xff) ) {
  174.                 app_reset(); // Will never return
  175.             }
  176.             // Fall through
  177.         case BIOS_NOAPP:
  178.             //if (nmt_type == CAN_NMT_PGM_START) {
  179.             if (nmt_type == CAN_NMT_PGM_START && bios_msg.DataLength == 8 &&
  180.                     bios_msg.Data.bytes[4] == (hwid&0xff) && bios_msg.Data.bytes[5] == ((hwid>>8)&0xff) &&
  181.                     bios_msg.Data.bytes[6] == ((hwid>>16)&0xff) && bios_msg.Data.bytes[7] == ((hwid>>24)&0xff) ) {
  182.                 // Set base address
  183.                 base_addr = bios_msg.Data.words[0];
  184.                 flash_init(pagebuf);
  185.                 //send CAN_NMT_PGM_ACK(offset)
  186.                 tx_msg.Id = CAN_ID_NMT_PGM_ACK;
  187.                 tx_msg.Data.words[0] = base_addr;
  188.                 send_msg = 1;
  189.                 bios_state = BIOS_PGM;
  190.             }
  191.             break;
  192.         case BIOS_PGM:
  193.             // Default to send CAN_NMT_PGM_ACK(offset)
  194.             tx_msg.Id = CAN_ID_NMT_PGM_ACK;
  195.            
  196.             if (nmt_type == CAN_NMT_PGM_DATA) {
  197.                 // Set address = base address + offset.
  198.                 offset = bios_msg.Data.words[0];
  199.                 addr = base_addr + offset;
  200.                
  201.                 // One of ACK and NACK will be sent, both have offset as data.
  202.                 tx_msg.Data.words[0] = offset;
  203.                 send_msg = 1;
  204.  
  205.                 // Flash all data sent, beginning at addr.
  206.                 len = (bios_msg.DataLength+1)/2; // Number of words in message, rounded up.
  207.                 uint8_t i;
  208.                 for (i=1; i<len; i++) { // Skip first word (offset).
  209.                    
  210.                     // Abort if trying to write in BIOS area.
  211.                     if (addr >= (uint16_t)&__bios_start) {
  212.                         // Send CAN_NMT_PGM_NACK(offset).
  213.                         tx_msg.Id = CAN_ID_NMT_PGM_NACK;
  214.                         break; //for loop
  215.                     }
  216.  
  217.                     data = bios_msg.Data.words[i];
  218.                     flash_write_word(addr, data);
  219.                     addr += 2;
  220.                 }
  221.                 // Adjust addr if an odd number of bytes were sent, for use in crc calc.
  222.                 addr -= bios_msg.DataLength & 1;
  223.             }
  224.             if (nmt_type == CAN_NMT_PGM_END) {
  225.                 // Make sure a partly filled page will be flashed.
  226.                 flash_flush_buffer();
  227.                
  228.                 // Calculate crc on written data.
  229.                 //TODO: This will not work if data is not sent sequentially without
  230.                 // holes, starting with offset=0. We might want to send a NACK if the
  231.                 // offset in a CAN_NMT_PGM_DATA msg differs from the expected offset.
  232.                 uint16_t loc, crc = 0;
  233.                 for (loc = base_addr; loc < addr; loc++) { // addr will be last location written + 2.
  234.                     crc = _crc16_update(crc, pgm_read_byte(loc));
  235.                 }
  236.                
  237.                 tx_msg.Data.words[0] = crc;
  238.                 send_msg = 1;
  239.                 bios_state = BIOS_END_PGM;
  240.             }
  241.             break;
  242.         case BIOS_END_PGM:
  243.             if (nmt_type == CAN_NMT_PGM_COPY) {
  244.                 // For BIOS updating over CAN. Upload bios to application area,
  245.                 // send this message with correct parameters to copy data from
  246.                 // application to bios area and pray it will come alive again.
  247.                 flash_copy_data(bios_msg.Data.words[0],
  248.                                 bios_msg.Data.words[1],
  249.                                 bios_msg.Data.words[2]);
  250.                 // flash_copy_data will never return.
  251.             }
  252.         }
  253.        
  254.         if (send_msg) {
  255.             while (Can_Send(&tx_msg) != CAN_OK);
  256.             send_msg = 0;
  257.         }
  258.  
  259.         bios_msg_full = 0; // We're done with this message, ready for the next.
  260.     }
  261.  
  262.     return 0;
  263. }
  264.