Subversion Repositories HomeAutomation

Rev

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

  1. /*
  2.  * $Id: bios.c 766 2008-04-10 18:33:30Z arune $
  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[4] = hwid&0xff;
  134.     tx_msg.Data.bytes[5] = (hwid>>8)&0xff;
  135.     tx_msg.Data.bytes[6] = (hwid>>16)&0xff;
  136.     tx_msg.Data.bytes[7] = (hwid>>24)&0xff;
  137.  
  138.     if (pgm_read_word(0) == 0xffff) {
  139.         // No application in flash
  140.         // Send CAN_NMT_BIOS_START(BIOS_VERSION, 0)
  141.         tx_msg.Data.bytes[2] = 0;
  142.         bios_state = BIOS_NOAPP;
  143.     } else {
  144.         // Application exists
  145.         // Send CAN_NMT_BIOS_START(BIOS_VERSION, 1)
  146.         tx_msg.Data.bytes[2] = 1;
  147.         bios_state = BIOS_APP;
  148.     }
  149.    
  150.     while (Can_Send(&tx_msg) != CAN_OK);
  151.  
  152.     tx_msg.DataLength = 2; // All msg sent after BIOS_START are length 2 so set it once.
  153.    
  154.     // Main loop
  155.     while (1) {
  156.         // Wait for message
  157.         while (!bios_msg_full) {
  158. #if defined(AUTOSTART)
  159.             if (bios_state == BIOS_APP && autostart_cnt == AUTOSTART) {
  160.                 app_reset();
  161.             }
  162. #endif
  163.         }
  164.        
  165.         nmt_type = (bios_msg.Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE;
  166.        
  167.         switch (bios_state) {
  168.         case BIOS_APP:
  169.             //if (nmt_type == CAN_NMT_START_APP) {
  170.             if (nmt_type == CAN_NMT_START_APP && bios_msg.DataLength == 4 &&
  171.                     bios_msg.Data.bytes[0] == (hwid&0xff) && bios_msg.Data.bytes[1] == ((hwid>>8)&0xff) &&
  172.                     bios_msg.Data.bytes[2] == ((hwid>>16)&0xff) && bios_msg.Data.bytes[3] == ((hwid>>24)&0xff) ) {
  173.                 app_reset(); // Will never return
  174.             }
  175.             // Fall through
  176.         case BIOS_NOAPP:
  177.             //if (nmt_type == CAN_NMT_PGM_START) {
  178.             if (nmt_type == CAN_NMT_PGM_START && bios_msg.DataLength == 8 &&
  179.                     bios_msg.Data.bytes[4] == (hwid&0xff) && bios_msg.Data.bytes[5] == ((hwid>>8)&0xff) &&
  180.                     bios_msg.Data.bytes[6] == ((hwid>>16)&0xff) && bios_msg.Data.bytes[7] == ((hwid>>24)&0xff) ) {
  181.                 // Set base address
  182.                 base_addr = bios_msg.Data.words[0];
  183.                 flash_init(pagebuf);
  184.                 //send CAN_NMT_PGM_ACK(offset)
  185.                 tx_msg.Id = CAN_ID_NMT_PGM_ACK;
  186.                 tx_msg.Data.words[0] = base_addr;
  187.                 send_msg = 1;
  188.                 bios_state = BIOS_PGM;
  189.             }
  190.             break;
  191.         case BIOS_PGM:
  192.             // Default to send CAN_NMT_PGM_ACK(offset)
  193.             tx_msg.Id = CAN_ID_NMT_PGM_ACK;
  194.            
  195.             if (nmt_type == CAN_NMT_PGM_DATA) {
  196.                 // Set address = base address + offset.
  197.                 offset = bios_msg.Data.words[0];
  198.                 addr = base_addr + offset;
  199.                
  200.                 // One of ACK and NACK will be sent, both have offset as data.
  201.                 tx_msg.Data.words[0] = offset;
  202.                 send_msg = 1;
  203.  
  204.                 // Flash all data sent, beginning at addr.
  205.                 len = (bios_msg.DataLength+1)/2; // Number of words in message, rounded up.
  206.                 uint8_t i;
  207.                 for (i=1; i<len; i++) { // Skip first word (offset).
  208.                    
  209.                     // Abort if trying to write in BIOS area.
  210.                     if (addr >= (uint16_t)&__bios_start) {
  211.                         // Send CAN_NMT_PGM_NACK(offset).
  212.                         tx_msg.Id = CAN_ID_NMT_PGM_NACK;
  213.                         break; //for loop
  214.                     }
  215.  
  216.                     data = bios_msg.Data.words[i];
  217.                     flash_write_word(addr, data);
  218.                     addr += 2;
  219.                 }
  220.                 // Adjust addr if an odd number of bytes were sent, for use in crc calc.
  221.                 addr -= bios_msg.DataLength & 1;
  222.             }
  223.             if (nmt_type == CAN_NMT_PGM_END) {
  224.                 // Make sure a partly filled page will be flashed.
  225.                 flash_flush_buffer();
  226.                
  227.                 // Calculate crc on written data.
  228.                 //TODO: This will not work if data is not sent sequentially without
  229.                 // holes, starting with offset=0. We might want to send a NACK if the
  230.                 // offset in a CAN_NMT_PGM_DATA msg differs from the expected offset.
  231.                 uint16_t loc, crc = 0;
  232.                 for (loc = base_addr; loc < addr; loc++) { // addr will be last location written + 2.
  233.                     crc = _crc16_update(crc, pgm_read_byte(loc));
  234.                 }
  235.                
  236.                 tx_msg.Data.words[0] = crc;
  237.                 send_msg = 1;
  238.                 bios_state = BIOS_END_PGM;
  239.             }
  240.             break;
  241.         case BIOS_END_PGM:
  242.             if (nmt_type == CAN_NMT_PGM_COPY) {
  243.                 // For BIOS updating over CAN. Upload bios to application area,
  244.                 // send this message with correct parameters to copy data from
  245.                 // application to bios area and pray it will come alive again.
  246.                 flash_copy_data(bios_msg.Data.words[0],
  247.                                 bios_msg.Data.words[1],
  248.                                 bios_msg.Data.words[2]);
  249.                 // flash_copy_data will never return.
  250.             }
  251.         }
  252.        
  253.         if (send_msg) {
  254.             while (Can_Send(&tx_msg) != CAN_OK);
  255.             send_msg = 0;
  256.         }
  257.  
  258.         bios_msg_full = 0; // We're done with this message, ready for the next.
  259.     }
  260.  
  261.     return 0;
  262. }
  263.