Subversion Repositories HomeAutomation

Rev

Rev 1630 | Rev 1747 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

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