Project

General

Profile

Statistics
| Revision:

root / trunk / bootloader / bootloader.c @ 199

History | View | Annotate | Download (4.04 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
//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
  uint8_t addr;
61

    
62
retry_jpnt:
63
  iteration = 0;
64
  retries = 0;
65
  addr = eeprom_read_byte(EEPROM_ADDR);
66
  
67
  // Clear the watchdog timer
68
  MCUSR &= ~_BV(WDRF);
69
  wdt_disable();
70
  WDTCSR = 0;
71

    
72
  rs485_init(BAUD9600);
73

    
74
  //set LED pin as output
75
  LED_DDR |= 0x07;
76
  PORTB = 0x07;
77

    
78
  //Start bootloading process
79
  send_packet(TT_BOOT, addr);
80

    
81
  resp = parse_packet(mbuf, addr);
82

    
83
  // Enter programming mode 
84
  if (resp == TT_PROGM) {
85
    prog_len = mbuf[0];
86
    prog_len |= mbuf[1] << 8;
87

    
88
    // This will insert a NOP into the user code jump in case
89
    // the programming fails
90
    for (i = 0; i < PROGD_PACKET_SIZE; i++) {
91
      mbuf[i]= 0;
92
    }
93
    onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf);
94

    
95
  // Run user code
96
  } else {
97
      main_start();
98
  }
99

    
100
  send_packet(TT_ACK, addr);
101

    
102
  while(1) {
103
      resp = parse_packet(mbuf, addr);
104

    
105
      if (resp == TT_PROGD) {
106
          // We need to muck with the reset vector jump in the first page
107
          if (iteration == 0) {
108
              // Store the jump to user code
109
              jbuf.bytes[0] = mbuf[0];
110
              jbuf.bytes[1] = mbuf[1];
111
             
112
              // Rewrite the user code jump to be correct since we are
113
              // using relative jumps (rjmp)
114
              jbuf.sword &= 0x0FFF;
115
              jbuf.sword -= (BOOT_START >> 1) - 1;
116
              jbuf.sword &= 0x0FFF;
117
              jbuf.sword |= 0xC000;
118

    
119
              // Rewrite the reset vector to jump to the bootloader
120
              mbuf[0] = (BOOT_START/2 - 1) & 0xFF;
121
              mbuf[1] = 0xC0 | (((BOOT_START/2 - 1) >> 8) & 0x0F);
122

    
123
              iteration = 1;
124
          }
125

    
126
          // Write the page to the flash
127
          onboard_program_write(caddr, mbuf);
128
          caddr += PROGD_PACKET_SIZE;
129
          retries = 0;
130
      } else {
131
          send_packet(TT_NACK, addr);
132
          retries++;
133

    
134
          // If we failed too many times, reset. This goes to the start
135
          // of the bootloader function
136
          if (retries > MAX_RETRIES) {
137
              goto retry_jpnt;
138
          }
139
      }
140

    
141
      send_packet(TT_ACK, addr);
142

    
143
      // Once we write the last packet we must override the jump to
144
      // user code to point to the correct address
145
      if (prog_len <= PROGD_PACKET_SIZE) {
146
          for (i = 0; i < PROGD_PACKET_SIZE; i++) {
147
              mbuf[i]= 0;
148
          }
149

    
150
          mbuf[PROGD_PACKET_SIZE-2] = jbuf.bytes[0];
151
          mbuf[PROGD_PACKET_SIZE-1] = jbuf.bytes[1];
152

    
153
          onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf);
154

    
155
          main_start();
156
      } else { 
157
          prog_len -= PROGD_PACKET_SIZE;
158
      }
159
  }
160

    
161
  // Should never get here
162
  return -1;
163
}