root / trunk / bootloader / bootloader.c @ 287
History | View | Annotate | Download (5.1 KB)
| 1 | #include "bootloader.h" |
|---|---|
| 2 | |
| 3 | // Setup the default fuses
|
| 4 | // This seems to be broken for now...
|
| 5 | /*
|
| 6 | FUSES = {
|
| 7 | .low = (FUSE_SUT0 & FUSE_CKSEL3 & FUSE_CKSEL2 & FUSE_CKSEL0), |
| 8 | .high = (FUSE_EESAVE & FUSE_SPIEN), |
| 9 | .extended = (FUSE_SELFPRGEN) |
| 10 | }; |
| 11 | */ |
| 12 | |
| 13 | // Error thresholds
|
| 14 | #define MAX_RETRIES 5 // Number of times to retry before giving up |
| 15 | |
| 16 | /**
|
| 17 | * Where we store the jump to user code. The jump address is in words |
| 18 | * due to how the rjmp instruction works. It is 1 word below the bootloader |
| 19 | * We declare it as noreturn to save some space since we will never return |
| 20 | * to the bootloader unless we do a system reset |
| 21 | */ |
| 22 | void (*main_start)(void) __attribute__((noreturn)) = (void*)(BOOT_START/2 - 1); |
| 23 | |
| 24 | typedef union { |
| 25 | uint8_t bytes[2];
|
| 26 | int16_t sword; |
| 27 | } rjump_t; |
| 28 | |
| 29 | |
| 30 | // SPM_PAGESIZE is set to 32 bytes
|
| 31 | void onboard_program_write(uint16_t page, uint8_t *buf) {
|
| 32 | uint16_t i; |
| 33 | |
| 34 | boot_page_erase (page); |
| 35 | boot_spm_busy_wait (); // Wait until the memory is erased.
|
| 36 | |
| 37 | for (i=0; i < SPM_PAGESIZE; i+=2){ |
| 38 | // Set up little-endian word.
|
| 39 | boot_page_fill (page + i, buf[i] | (buf[i+1] <<8)); |
| 40 | } |
| 41 | |
| 42 | boot_page_write (page); // Store buffer in flash page.
|
| 43 | boot_spm_busy_wait(); // Wait until the memory is written.
|
| 44 | } |
| 45 | |
| 46 | int main(void) { |
| 47 | uint8_t mbuf[MAX_PAYLOAD_SIZE]; |
| 48 | rjump_t jbuf; |
| 49 | uint16_t caddr; |
| 50 | uint16_t prog_len; |
| 51 | uint8_t resp; |
| 52 | uint8_t i; |
| 53 | uint8_t retries; |
| 54 | uint8_t addr; |
| 55 | uint8_t boot; |
| 56 | |
| 57 | // This is where we jump to if we fail out because of retries
|
| 58 | retry_jpnt:
|
| 59 | |
| 60 | // We set the variables here
|
| 61 | caddr = MAIN_ADDR; |
| 62 | retries = 0;
|
| 63 | boot = FALSE; |
| 64 | |
| 65 | // Grab the address from EEPROM
|
| 66 | addr = read_addr(); |
| 67 | |
| 68 | // Initialize the Pins
|
| 69 | DDRB = LED_GREEN | LED_YELLOW | LED_RED; // Set the LED pins as outputs
|
| 70 | PORTB = 0x00; // LEDs on == in bootloader |
| 71 | DDRD = RELAY; // Set Relay as an output
|
| 72 | PORTD = 0x00; // Turn relay off |
| 73 | |
| 74 | // Clear the watchdog timer
|
| 75 | if (MCUSR & _BV(WDRF)) {
|
| 76 | MCUSR &= ~_BV(WDRF); |
| 77 | wdt_disable(); |
| 78 | WDTCSR = 0;
|
| 79 | boot = TRUE; |
| 80 | } |
| 81 | |
| 82 | // External override at bootup to enter bootloading mode
|
| 83 | if (!(BUT_PORT & (BUT_RED | BUT_BLACK))) {
|
| 84 | boot = TRUE; |
| 85 | } |
| 86 | |
| 87 | // Initialize the RS485
|
| 88 | rs485_init(BAUD9600); |
| 89 | |
| 90 | //Execute user code if we aren't supposed to enter bootloading mode
|
| 91 | if (boot == FALSE) {
|
| 92 | goto run_user_code;
|
| 93 | } |
| 94 | |
| 95 | // Send the boot packet
|
| 96 | // We're not using the loop variable i right now and we are ignoring the
|
| 97 | // packet length so we're just going to "capture" the length and ignore it
|
| 98 | send_packet(TT_BOOT, addr, NULL, 0); |
| 99 | resp = parse_packet(mbuf, &i, addr); |
| 100 | |
| 101 | // Enter programming mode
|
| 102 | if (resp == TT_PROGM) {
|
| 103 | prog_len = mbuf[0];
|
| 104 | prog_len |= mbuf[1] << 8; |
| 105 | |
| 106 | // This will insert a NOP into the user code jump in case
|
| 107 | // the programming fails
|
| 108 | for (i = 0; i < PROGD_PACKET_SIZE; i++) { |
| 109 | mbuf[i]= 0;
|
| 110 | } |
| 111 | onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf); |
| 112 | |
| 113 | // Run user code
|
| 114 | } else {
|
| 115 | run_user_code:
|
| 116 | LED_PORT = LED_GREEN | LED_YELLOW | LED_RED; |
| 117 | main_start(); |
| 118 | } |
| 119 | |
| 120 | send_packet(TT_ACK, addr, NULL, 0); |
| 121 | |
| 122 | while(1) { |
| 123 | // We are ignoring the length of the packet so we are reusing the loop
|
| 124 | // variable i which is not being used right now to throw away the value
|
| 125 | resp = parse_packet(mbuf, &i, addr); |
| 126 | |
| 127 | if (resp == TT_PROGD) {
|
| 128 | // We need to muck with the reset vector jump in the first page
|
| 129 | if (caddr == MAIN_ADDR) {
|
| 130 | // Store the jump to user code
|
| 131 | jbuf.bytes[0] = mbuf[0]; |
| 132 | jbuf.bytes[1] = mbuf[1]; |
| 133 | |
| 134 | // Rewrite the user code jump to be correct since we are
|
| 135 | // using relative jumps (rjmp)
|
| 136 | jbuf.sword &= 0x0FFF;
|
| 137 | jbuf.sword -= (BOOT_START >> 1) - 1; |
| 138 | jbuf.sword &= 0x0FFF;
|
| 139 | jbuf.sword |= 0xC000;
|
| 140 | |
| 141 | // Rewrite the reset vector to jump to the bootloader
|
| 142 | mbuf[0] = (BOOT_START/2 - 1) & 0xFF; |
| 143 | mbuf[1] = 0xC0 | (((BOOT_START/2 - 1) >> 8) & 0x0F); |
| 144 | } |
| 145 | |
| 146 | // Write the page to the flash
|
| 147 | onboard_program_write(caddr, mbuf); |
| 148 | caddr += PROGD_PACKET_SIZE; |
| 149 | retries = 0;
|
| 150 | } else {
|
| 151 | send_packet(TT_NACK, addr, NULL, 0); |
| 152 | retries++; |
| 153 | |
| 154 | // If we failed too many times, reset. This goes to the start
|
| 155 | // of the bootloader function
|
| 156 | if (retries > MAX_RETRIES) {
|
| 157 | goto retry_jpnt;
|
| 158 | } else {
|
| 159 | continue;
|
| 160 | } |
| 161 | } |
| 162 | |
| 163 | send_packet(TT_ACK, addr, NULL, 0); |
| 164 | |
| 165 | // Once we write the last packet we must override the jump to
|
| 166 | // user code to point to the correct address
|
| 167 | if (prog_len <= PROGD_PACKET_SIZE) {
|
| 168 | //for (i = 0; i < MAX_PAYLOAD_SIZE; i++) {
|
| 169 | // mbuf[i]= 0;
|
| 170 | //}
|
| 171 | |
| 172 | mbuf[PROGD_PACKET_SIZE-2] = jbuf.bytes[0]; |
| 173 | mbuf[PROGD_PACKET_SIZE-1] = jbuf.bytes[1]; |
| 174 | |
| 175 | onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf); |
| 176 | |
| 177 | // Jump to user code that we just wrote
|
| 178 | goto run_user_code;
|
| 179 | } else {
|
| 180 | prog_len -= PROGD_PACKET_SIZE; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Should never get here
|
| 185 | return -1; |
| 186 | } |