Subversion Repositories HomeAutomation

Rev

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

  1. /*
  2.  * $Id: bios.c 585 2007-08-06 17:44:46Z 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.  
  51. //---------------------------------------------------------------------------
  52. // Private functions
  53.  
  54. void Can_Process(Can_Message_t* msg) {
  55.     if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames.
  56.    
  57.     if ((msg->Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS == CAN_NMT) {
  58.        
  59.         if ((msg->Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE == CAN_NMT_TIME) {
  60.             // Reset watchdog as long as time messages from the master node
  61.             // are received periodically. If anything hangs the CAN communication
  62.             // or the master node is not up, the node will be reset.
  63.             wdt_reset();
  64.             //TODO: Don't care about time right now... Figure out what to do with it.
  65. #if defined(AUTOSTART)
  66.             autostart_cnt++;
  67. #endif
  68.         }
  69.        
  70.         if ((msg->Id & CAN_MASK_NMT_RID)>>CAN_SHIFT_NMT_RID == NODE_ID) {
  71.  
  72.             if ((msg->Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE == CAN_NMT_RESET) {
  73.                 BIOS_Reset();
  74.             }
  75.             // Copy message to bios buffer if bios is done with the previous message.
  76.             // This is also a check to see if bios is still running. When app is
  77.             // started, bios_msg_full is never reset to 0 so the check fails and all
  78.             // NMT communication except reset is ignored.
  79.             if (!(bios_msg_full)) {
  80.                 memcpy(bios_msg_ptr, msg, sizeof(Can_Message_t));
  81.                 bios_msg_full = 1;
  82.             }
  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 = 3;
  125.     tx_msg.Data.words[0] = BIOS_VERSION;
  126.  
  127.     if (pgm_read_word(0) == 0xffff) {
  128.         // No application in flash
  129.         // Send CAN_NMT_BIOS_START(BIOS_VERSION, 0)
  130.         tx_msg.Data.bytes[2] = 0;
  131.         bios_state = BIOS_NOAPP;
  132.     } else {
  133.         // Application exists
  134.         // Send CAN_NMT_BIOS_START(BIOS_VERSION, 1)
  135.         tx_msg.Data.bytes[2] = 1;
  136.         bios_state = BIOS_APP;
  137.     }
  138.    
  139.     while (Can_Send(&tx_msg) != CAN_OK);
  140.  
  141.     tx_msg.DataLength = 2; // All msg sent after BIOS_START are length 2 so set it once.
  142.    
  143.     // Main loop
  144.     while (1) {
  145.         // Wait for message
  146.         while (!bios_msg_full) {
  147. #if defined(AUTOSTART)
  148.             if (bios_state == BIOS_APP && autostart_cnt == AUTOSTART) {
  149.                 app_reset();
  150.             }
  151. #endif
  152.         }
  153.        
  154.         nmt_type = (bios_msg.Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE;
  155.        
  156.         switch (bios_state) {
  157.         case BIOS_APP:
  158.             if (nmt_type == CAN_NMT_START_APP) {
  159.                 app_reset(); // Will never return
  160.             }
  161.             // Fall through
  162.         case BIOS_NOAPP:
  163.             if (nmt_type == CAN_NMT_PGM_START) {
  164.                 // Set base address
  165.                 base_addr = bios_msg.Data.words[0];
  166.                 flash_init(pagebuf);
  167.                 //send CAN_NMT_PGM_ACK(offset)
  168.                 tx_msg.Id = CAN_ID_NMT_PGM_ACK;
  169.                 tx_msg.Data.words[0] = base_addr;
  170.                 send_msg = 1;
  171.                 bios_state = BIOS_PGM;
  172.             }
  173.             if (nmt_type == CAN_NMT_PGM_COPY) {
  174.                 // For BIOS updating over CAN. Upload bios to application area,
  175.                 // send this message with correct parameters to copy data from
  176.                 // application to bios area and pray it will come alive again.
  177.                 flash_copy_data(bios_msg.Data.words[0],
  178.                                 bios_msg.Data.words[1],
  179.                                 bios_msg.Data.words[2]);
  180.                 // flash_copy_data will never return.
  181.             }
  182.             break;
  183.         case BIOS_PGM:
  184.             // Default to send CAN_NMT_PGM_ACK(offset)
  185.             tx_msg.Id = CAN_ID_NMT_PGM_ACK;
  186.            
  187.             if (nmt_type == CAN_NMT_PGM_DATA) {
  188.                 // Set address = base address + offset.
  189.                 offset = bios_msg.Data.words[0];
  190.                 addr = base_addr + offset;
  191.                
  192.                 // One of ACK and NACK will be sent, both have offset as data.
  193.                 tx_msg.Data.words[0] = offset;
  194.                 send_msg = 1;
  195.  
  196.                 // Flash all data sent, beginning at addr.
  197.                 len = (bios_msg.DataLength+1)/2; // Number of words in message, rounded up.
  198.                 uint8_t i;
  199.                 for (i=1; i<len; i++) { // Skip first word (offset).
  200.                    
  201.                     // Abort if trying to write in BIOS area.
  202.                     if (addr >= (uint16_t)&__bios_start) {
  203.                         // Send CAN_NMT_PGM_NACK(offset).
  204.                         tx_msg.Id = CAN_ID_NMT_PGM_NACK;
  205.                         break; //for loop
  206.                     }
  207.  
  208.                     data = bios_msg.Data.words[i];
  209.                     flash_write_word(addr, data);
  210.                     addr += 2;
  211.                 }
  212.                 // Adjust addr if an odd number of bytes were sent, for use in crc calc.
  213.                 addr -= bios_msg.DataLength & 1;
  214.             }
  215.             if (nmt_type == CAN_NMT_PGM_END) {
  216.                 // Make sure a partly filled page will be flashed.
  217.                 flash_flush_buffer();
  218.                
  219.                 // Calculate crc on written data.
  220.                 //TODO: This will not work if data is not sent sequentially without
  221.                 // holes, starting with offset=0. We might want to send a NACK if the
  222.                 // offset in a CAN_NMT_PGM_DATA msg differs from the expected offset.
  223.                 uint16_t loc, crc = 0;
  224.                 for (loc = base_addr; loc < addr; loc++) { // addr will be last location written + 2.
  225.                     crc = _crc16_update(crc, pgm_read_byte(loc));
  226.                 }
  227.                
  228.                 tx_msg.Data.words[0] = crc;
  229.                 send_msg = 1;
  230.                 bios_state = BIOS_NOAPP;
  231.             }
  232.         }
  233.        
  234.         if (send_msg) {
  235.             while (Can_Send(&tx_msg) != CAN_OK);
  236.             send_msg = 0;
  237.         }
  238.  
  239.         bios_msg_full = 0; // We're done with this message, ready for the next.
  240.     }
  241.  
  242.     return 0;
  243. }
  244.