root / trunk / bootloader / bootloader.c @ 196
History | View | Annotate | Download (3.9 KB)
| 1 | #include "bootloader.h" |
|---|---|
| 2 | |
| 3 | // Setup the default fuses
|
| 4 | /*
|
| 5 | FUSES = {
|
| 6 | .low = (FUSE_SUT0 & FUSE_CKSEL3 & FUSE_CKSEL2 & FUSE_CKSEL0), |
| 7 | .high = (FUSE_EESAVE & FUSE_SPIEN), |
| 8 | .extended = (FUSE_SELFPRGEN), |
| 9 | };*/ |
| 10 | |
| 11 | |
| 12 | |
| 13 | // Error thresholds
|
| 14 | #define MAX_RETRIES 5 // Number of times to retry before giving up |
| 15 | |
| 16 | //Status LED
|
| 17 | #define LED_DDR DDRB
|
| 18 | #define LED_PORT PORTB
|
| 19 | #define LED PORTB1
|
| 20 | |
| 21 | /**
|
| 22 | * Where we store the jump to user code. The jump address is in words |
| 23 | * due to how the rjmp instruction works. It is 1 word below the bootloader |
| 24 | * We declare it as noreturn to save some space since we will never return |
| 25 | * to the bootloader unless we do a system reset |
| 26 | */ |
| 27 | void (*main_start)(void) __attribute__((noreturn)) = BOOT_START/2 - 1; |
| 28 | |
| 29 | typedef union { |
| 30 | uint8_t bytes[2];
|
| 31 | int16_t sword; |
| 32 | } rjump_t; |
| 33 | |
| 34 | |
| 35 | // SPM_PAGESIZE is set to 32 bytes
|
| 36 | void onboard_program_write(uint16_t page, uint8_t *buf) {
|
| 37 | uint16_t i; |
| 38 | |
| 39 | boot_page_erase (page); |
| 40 | boot_spm_busy_wait (); // Wait until the memory is erased.
|
| 41 | |
| 42 | for (i=0; i < SPM_PAGESIZE; i+=2){ |
| 43 | // Set up little-endian word.
|
| 44 | boot_page_fill (page + i, buf[i] | (buf[i+1] <<8)); |
| 45 | } |
| 46 | |
| 47 | boot_page_write (page); // Store buffer in flash page.
|
| 48 | boot_spm_busy_wait(); // Wait until the memory is written.
|
| 49 | } |
| 50 | |
| 51 | int main(void) { |
| 52 | uint8_t mbuf[PROGD_PACKET_SIZE]; |
| 53 | rjump_t jbuf; |
| 54 | uint16_t caddr = MAIN_ADDR; |
| 55 | uint8_t iteration; |
| 56 | uint8_t resp; |
| 57 | uint16_t prog_len; |
| 58 | uint8_t i; |
| 59 | uint8_t retries; |
| 60 | |
| 61 | retry_jpnt:
|
| 62 | iteration = 0;
|
| 63 | retries = 0;
|
| 64 | |
| 65 | // Clear the watchdog timer
|
| 66 | MCUSR &= ~_BV(WDRF); |
| 67 | wdt_disable(); |
| 68 | WDTCSR = 0;
|
| 69 | |
| 70 | |
| 71 | rs485_init(51); //MAGIC NUMBER?? |
| 72 | |
| 73 | //set LED pin as output
|
| 74 | LED_DDR |= 0x07;
|
| 75 | PORTB = 0x07;
|
| 76 | |
| 77 | //Start bootloading process
|
| 78 | send_packet(TT_BOOT); |
| 79 | |
| 80 | resp = parse_packet(mbuf); |
| 81 | |
| 82 | // Enter programming mode
|
| 83 | if (resp == TT_PROGM) {
|
| 84 | prog_len = mbuf[0];
|
| 85 | prog_len |= mbuf[1] << 8; |
| 86 | |
| 87 | // This will insert a NOP into the user code jump in case
|
| 88 | // the programming fails
|
| 89 | for (i = 0; i < PROGD_PACKET_SIZE; i++) { |
| 90 | mbuf[i]= 0;
|
| 91 | } |
| 92 | onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf); |
| 93 | |
| 94 | // Run user code
|
| 95 | } else {
|
| 96 | main_start(); |
| 97 | } |
| 98 | |
| 99 | send_packet(TT_ACK); |
| 100 | |
| 101 | while(1) { |
| 102 | resp = parse_packet(mbuf); |
| 103 | |
| 104 | if (resp == TT_PROGD) {
|
| 105 | // We need to muck with the reset vector jump in the first page
|
| 106 | if (iteration == 0) { |
| 107 | // Store the jump to user code
|
| 108 | jbuf.bytes[0] = mbuf[0]; |
| 109 | jbuf.bytes[1] = mbuf[1]; |
| 110 | |
| 111 | // Rewrite the user code jump to be correct since we are
|
| 112 | // using relative jumps (rjmp)
|
| 113 | jbuf.sword &= 0x0FFF;
|
| 114 | jbuf.sword -= (BOOT_START >> 1) - 1; |
| 115 | jbuf.sword &= 0x0FFF;
|
| 116 | jbuf.sword |= 0xC000;
|
| 117 | |
| 118 | // Rewrite the reset vector to jump to the bootloader
|
| 119 | mbuf[0] = (BOOT_START/2 - 1) & 0xFF; |
| 120 | mbuf[1] = 0xC0 | (((BOOT_START/2 - 1) >> 8) & 0x0F); |
| 121 | |
| 122 | iteration = 1;
|
| 123 | } |
| 124 | |
| 125 | // Write the page to the flash
|
| 126 | onboard_program_write(caddr, mbuf); |
| 127 | caddr += PROGD_PACKET_SIZE; |
| 128 | retries = 0;
|
| 129 | } else {
|
| 130 | send_packet(TT_NACK); |
| 131 | retries++; |
| 132 | |
| 133 | // If we failed too many times, reset. This goes to the start
|
| 134 | // of the bootloader function
|
| 135 | if (retries > MAX_RETRIES) {
|
| 136 | goto retry_jpnt;
|
| 137 | } |
| 138 | } |
| 139 | |
| 140 | send_packet(TT_ACK); |
| 141 | |
| 142 | // Once we write the last packet we must override the jump to
|
| 143 | // user code to point to the correct address
|
| 144 | if (prog_len <= PROGD_PACKET_SIZE) {
|
| 145 | for (i = 0; i < PROGD_PACKET_SIZE; i++) { |
| 146 | mbuf[i]= 0;
|
| 147 | } |
| 148 | |
| 149 | mbuf[PROGD_PACKET_SIZE-2] = jbuf.bytes[0]; |
| 150 | mbuf[PROGD_PACKET_SIZE-1] = jbuf.bytes[1]; |
| 151 | |
| 152 | onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf); |
| 153 | |
| 154 | main_start(); |
| 155 | } else {
|
| 156 | prog_len -= PROGD_PACKET_SIZE; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Should never get here
|
| 161 | return -1; |
| 162 | } |