Subversion Repositories HomeAutomation

Rev

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

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