Subversion Repositories HomeAutomation

Rev

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

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