Project

General

Profile

Revision 161

Added by Kevin Woo about 14 years ago

Bootloader fixes

View differences:

bootloader.c
15 15

  
16 16
#define MAX_WAIT_IN_CYCLES 800000
17 17
#define MAX_TIMEOUT 60000        // Seconds to wait before exiting bootloader mode
18
#define MAIN_ADDR 1024
18 19

  
19 20
//Status LED
20 21
#define LED_DDR  DDRB
......
25 26

  
26 27
//Function prototypes
27 28
void flash_led(uint8_t);
28
void onboard_program_write(uint32_t page, uint8_t *buf, uint8_t offset);
29
void (*main_start)(void) = 0x0000;
29
void onboard_program_write(uint16_t page, uint8_t *buf);
30
void (*main_start)(void) = MAIN_ADDR;
30 31

  
31 32
//Variables
32 33
uint8_t incoming_page_data[PAGE_SIZE];
......
41 42
  uint8_t  byte[2];
42 43
} page_address;
43 44

  
44
char getch(void);
45

  
46 45
typedef enum {
47 46
    sd,
48 47
    src,
......
96 95
  while (1) {
97 96
    // Wait for the next byte
98 97
    while ((uart_get_byte(&r)) < 0) {
99
        //if (count >= MAX_TIMEOUT) {
100
        //    return TT_BAD;
101
       //}
98
        if (count >= MAX_TIMEOUT) {
99
            return TT_BAD;
100
       }
102 101
    }
103 102

  
104 103
    switch (state) {
......
184 183
    uint8_t mbuf[PROGD_PACKET_SIZE];
185 184
  uint8_t check_sum = 0;
186 185
  uint16_t i;
186
  uint16_t caddr = MAIN_ADDR;
187 187

  
188 188
  uint8_t resp;
189 189
  uint16_t prog_len;
......
193 193
  sei();
194 194

  
195 195
  //set LED pin as output
196
  LED_DDR |= _BV(LED);
197
  PORTB |= _BV(LED);
196
  LED_DDR |= 0x07;
197
  PORTB = 0x07;
198 198

  
199
  //flash onboard LED to signal entering of bootloader
200
  //flash_led(1);
201

  
202 199
  //Start bootloading process
203 200
  send_packet(TT_BOOT);
204 201

  
......
207 204
    prog_len = mbuf[0];
208 205
    prog_len |= mbuf[1] << 8;
209 206
  } else {
210
      while(1) {
211
          flash_led(1);
212
      }
213
      //main_start();
207
      //while(1) {
208
      //    flash_led(1);
209
      //}
210
      main_start();
214 211
  }
215 212
  send_packet(TT_ACK);
216 213

  
217 214
  while(1) {
218 215
      resp = parse_packet(mbuf);
219 216

  
220
      //if (resp == TT_PROGD) {
221
      //    onboard_program_write((uint32_t)page_address.word, incoming_page_data);
222
      //}
217
      if (resp == TT_PROGD) {
218
          onboard_program_write(caddr, incoming_page_data);
219
          caddr += PROGD_PACKET_SIZE;
220
      } else {
221
          while(1) {
222
              flash_led(1);
223
          }
224
      }
225

  
223 226
      send_packet(TT_ACK);
227

  
228
      if (prog_len <= PROGD_PACKET_SIZE) {
229
          send_packet(TT_ACK);
230
          //while(1) {
231
             PORTB = 0;
232
          //}
233
          main_start();
234
      } else { 
235
          prog_len -= PROGD_PACKET_SIZE;
236
      }
224 237
  }
225

  
226 238
}
227 239

  
228
//#define SPM_PAGESIZE 128
229
void onboard_program_write(uint32_t page, uint8_t *buf, uint8_t offset)
240
#define SPM_PAGESIZE 32
241
void onboard_program_write(uint16_t page, uint8_t *buf)
230 242
{
231 243
  uint16_t i;
232 244

  
245
  cli();
246

  
247
  eeprom_busy_wait();
248

  
233 249
  boot_page_erase (page);
234 250
  boot_spm_busy_wait ();      // Wait until the memory is erased.
235 251

  
236
  for (i=offset; i < SPM_PAGESIZE; i+=2){
252
  for (i=0; i < SPM_PAGESIZE; i+=2){
237 253
    // Set up little-endian word.
238 254

  
239 255
    uint16_t w = *buf++;
......
244 260

  
245 261
  boot_page_write (page);     // Store buffer in flash page.
246 262
  boot_spm_busy_wait();       // Wait until the memory is written.
247
}
248 263

  
249
char getch(void) {
250
  retransmit_flag = FALSE;
251
	
252
  uint32_t count = 0;
253
  uint8_t resp;
254
  while(uart_get_byte(&resp)==-1) {
255
    count++;
256
    if (count > MAX_WAIT_IN_CYCLES) {
257
      retransmit_flag = TRUE;
258
      break;
259
    }
260
  }
261

  
262
  return resp;
264
  sei();
263 265
}
264 266

  
265 267
void flash_led(uint8_t count)

Also available in: Unified diff