Project

General

Profile

Statistics
| Revision:

root / trunk / bootloader / bootloader.c @ 209

History | View | Annotate | Download (4.75 KB)

1 189 kwoo
#include "bootloader.h"
2 154 bneuman
3 194 kwoo
// Setup the default fuses
4 197 kwoo
// This seems to be broken for now...
5 196 kwoo
/*
6 194 kwoo
FUSES = {
7
    .low = (FUSE_SUT0 & FUSE_CKSEL3 & FUSE_CKSEL2 & FUSE_CKSEL0),
8
    .high = (FUSE_EESAVE & FUSE_SPIEN),
9 197 kwoo
    .extended = (FUSE_SELFPRGEN)
10
};
11
*/
12 156 kwoo
13 173 kwoo
// Error thresholds
14
#define MAX_RETRIES 5       // Number of times to retry before giving up
15 154 bneuman
16 195 kwoo
/**
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 196 kwoo
 * 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 195 kwoo
 */
22 196 kwoo
void (*main_start)(void) __attribute__((noreturn)) = BOOT_START/2 - 1;
23 195 kwoo
24 168 kwoo
typedef union {
25
    uint8_t bytes[2];
26
    int16_t sword;
27
} rjump_t;
28
29 156 kwoo
30 171 kwoo
// SPM_PAGESIZE is set to 32 bytes
31
void onboard_program_write(uint16_t page, uint8_t *buf) {
32 164 kwoo
  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 168 kwoo
    boot_page_fill (page + i, buf[i] | (buf[i+1] <<8));
40 164 kwoo
  }
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 171 kwoo
int main(void) {
47 164 kwoo
  uint8_t mbuf[PROGD_PACKET_SIZE];
48 168 kwoo
  rjump_t jbuf;
49 201 kwoo
  uint16_t caddr;
50
  uint16_t prog_len;
51 158 kwoo
  uint8_t resp;
52 165 kwoo
  uint8_t i;
53 173 kwoo
  uint8_t retries;
54 199 kwoo
  uint8_t addr;
55 200 kwoo
  uint8_t boot;
56 176 kwoo
57 201 kwoo
  // 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 173 kwoo
  retries = 0;
63 201 kwoo
  boot = FALSE;
64 200 kwoo
65 201 kwoo
  // Grab the address from EEPROM
66
  addr = eeprom_read_byte((void*)EEPROM_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 176 kwoo
  // Clear the watchdog timer
75 200 kwoo
  if (MCUSR & _BV(WDRF)) {
76
      MCUSR &= ~_BV(WDRF);
77
      wdt_disable();
78
      WDTCSR = 0;
79 201 kwoo
      boot = TRUE;
80 200 kwoo
  }
81 158 kwoo
82 201 kwoo
  if (!(BUT_PORT & (BUT_RED | BUT_BLACK))) {
83
      boot = TRUE;
84 200 kwoo
  }
85
86 201 kwoo
  // Initialize the RS485
87 198 kwoo
  rs485_init(BAUD9600);
88 154 bneuman
89 201 kwoo
  //Execute user code if we aren't supposed to enter bootloading mode
90
  if (boot == FALSE) {
91
      goto run_user_code;
92 200 kwoo
  }
93 154 bneuman
94 201 kwoo
  // Send the boot packet
95 199 kwoo
  send_packet(TT_BOOT, addr);
96
  resp = parse_packet(mbuf, addr);
97 173 kwoo
98
  // Enter programming mode
99 158 kwoo
  if (resp == TT_PROGM) {
100
    prog_len = mbuf[0];
101
    prog_len |= mbuf[1] << 8;
102 173 kwoo
103
    // This will insert a NOP into the user code jump in case
104
    // the programming fails
105
    for (i = 0; i < PROGD_PACKET_SIZE; i++) {
106
      mbuf[i]= 0;
107
    }
108
    onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf);
109
110
  // Run user code
111 158 kwoo
  } else {
112 201 kwoo
      run_user_code:
113
      LED_PORT = LED_GREEN | LED_YELLOW | LED_RED;
114 161 kwoo
      main_start();
115 154 bneuman
  }
116 173 kwoo
117 199 kwoo
  send_packet(TT_ACK, addr);
118 154 bneuman
119
  while(1) {
120 199 kwoo
      resp = parse_packet(mbuf, addr);
121 154 bneuman
122 161 kwoo
      if (resp == TT_PROGD) {
123 173 kwoo
          // We need to muck with the reset vector jump in the first page
124 201 kwoo
          if (caddr == MAIN_ADDR) {
125 165 kwoo
              // Store the jump to user code
126 168 kwoo
              jbuf.bytes[0] = mbuf[0];
127
              jbuf.bytes[1] = mbuf[1];
128
129 173 kwoo
              // Rewrite the user code jump to be correct since we are
130
              // using relative jumps (rjmp)
131 168 kwoo
              jbuf.sword &= 0x0FFF;
132 169 kwoo
              jbuf.sword -= (BOOT_START >> 1) - 1;
133 168 kwoo
              jbuf.sword &= 0x0FFF;
134
              jbuf.sword |= 0xC000;
135 165 kwoo
136 173 kwoo
              // Rewrite the reset vector to jump to the bootloader
137 169 kwoo
              mbuf[0] = (BOOT_START/2 - 1) & 0xFF;
138
              mbuf[1] = 0xC0 | (((BOOT_START/2 - 1) >> 8) & 0x0F);
139 171 kwoo
          }
140 167 kwoo
141 173 kwoo
          // Write the page to the flash
142 167 kwoo
          onboard_program_write(caddr, mbuf);
143
          caddr += PROGD_PACKET_SIZE;
144 173 kwoo
          retries = 0;
145 161 kwoo
      } else {
146 199 kwoo
          send_packet(TT_NACK, addr);
147 173 kwoo
          retries++;
148
149 174 kwoo
          // If we failed too many times, reset. This goes to the start
150
          // of the bootloader function
151 173 kwoo
          if (retries > MAX_RETRIES) {
152 174 kwoo
              goto retry_jpnt;
153 209 kwoo
          } else {
154
              continue;
155 173 kwoo
          }
156 161 kwoo
      }
157
158 199 kwoo
      send_packet(TT_ACK, addr);
159 161 kwoo
160 173 kwoo
      // Once we write the last packet we must override the jump to
161
      // user code to point to the correct address
162 161 kwoo
      if (prog_len <= PROGD_PACKET_SIZE) {
163 165 kwoo
          for (i = 0; i < PROGD_PACKET_SIZE; i++) {
164
              mbuf[i]= 0;
165
          }
166
167 168 kwoo
          mbuf[PROGD_PACKET_SIZE-2] = jbuf.bytes[0];
168
          mbuf[PROGD_PACKET_SIZE-1] = jbuf.bytes[1];
169 165 kwoo
170 168 kwoo
          onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf);
171 200 kwoo
172 201 kwoo
          // Jump to user code that we just wrote
173
          goto run_user_code;
174 161 kwoo
      } else {
175
          prog_len -= PROGD_PACKET_SIZE;
176
      }
177 154 bneuman
  }
178 171 kwoo
179
  // Should never get here
180 169 kwoo
  return -1;
181 154 bneuman
}