Subversion Repositories HomeAutomation

Rev

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

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