Subversion Repositories HomeAutomation

Rev

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

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