Subversion Repositories HomeAutomation

Rev

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

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