Revision 165
bootloader stuff that should work
trunk/toolbox/bootloader/bootloader.c | ||
---|---|---|
1 | 1 |
#include <avr/io.h> |
2 |
#include <util/delay.h> |
|
3 | 2 |
#include <avr/boot.h> |
3 |
#include <avr/pgmspace.h> |
|
4 | 4 |
|
5 | 5 |
#include "tooltron.h" |
6 | 6 |
#include "uart.h" |
... | ... | |
13 | 13 |
|
14 | 14 |
#define MAX_WAIT_IN_CYCLES 800000 |
15 | 15 |
#define MAX_TIMEOUT 60000 // Seconds to wait before exiting bootloader mode |
16 |
#define MAIN_ADDR 0x02 |
|
16 |
#define MAIN_ADDR 0x000 |
|
17 |
#define BOOT_ADDR 0x5A0 |
|
18 |
#define JUMP_ADDR 0x590 |
|
17 | 19 |
|
18 | 20 |
//Status LED |
19 | 21 |
#define LED_DDR DDRB |
... | ... | |
21 | 23 |
#define LED PORTB1 |
22 | 24 |
|
23 | 25 |
//Function prototypes |
24 |
void flash_led(uint8_t); |
|
25 |
void onboard_program_write(uint16_t page, uint8_t *buf); |
|
26 |
void (*main_start)(void) = MAIN_ADDR; |
|
26 |
void __init(void) __attribute__((naked)); |
|
27 | 27 |
|
28 |
//void onboard_program_write(uint16_t page, uint8_t *buf); |
|
29 |
void (*main_start)(void) = BOOT_ADDR - 2; |
|
30 |
|
|
31 |
|
|
28 | 32 |
typedef enum { |
29 | 33 |
sd, |
30 | 34 |
src, |
... | ... | |
35 | 39 |
ack |
36 | 40 |
} state_t; |
37 | 41 |
|
42 |
// Redirect all unused interrupts to reti |
|
43 |
EMPTY_INTERRUPT(__vector_default); |
|
44 |
extern void __ctors_end(void) __attribute__((__noreturn__)); |
|
45 |
|
|
46 |
// macros for entering subprograms |
|
47 |
#define __JMP_INIT__ __asm__ __volatile__ ("rjmp __ctors_end" ) |
|
48 |
|
|
38 | 49 |
BOOTLOADER_SECTION void init_uart(uint16_t baud) { |
39 | 50 |
// Set baud rate |
40 | 51 |
UBRRH = (uint8_t)(baud>>8); |
... | ... | |
192 | 203 |
boot_spm_busy_wait(); // Wait until the memory is written. |
193 | 204 |
} |
194 | 205 |
|
195 |
BOOTLOADER_SECTION void flash_led(uint8_t count) { |
|
196 |
uint8_t i; |
|
197 |
|
|
198 |
for (i = 0; i < count; ++i) { |
|
199 |
LED_PORT |= _BV(LED); |
|
200 |
_delay_ms(100); |
|
201 |
LED_PORT &= ~_BV(LED); |
|
202 |
_delay_ms(100); |
|
203 |
} |
|
204 |
} |
|
205 |
|
|
206 |
BOOTLOADER_SECTION int main(void) { |
|
206 |
BOOTLOADER_SECTION void __init(void) { |
|
207 | 207 |
uint8_t mbuf[PROGD_PACKET_SIZE]; |
208 |
uint8_t jbuf[2]; |
|
208 | 209 |
uint16_t caddr = MAIN_ADDR; |
209 | 210 |
uint8_t iteration = 0; |
210 | 211 |
uint8_t resp; |
211 | 212 |
uint16_t prog_len = 0; |
213 |
uint8_t i; |
|
212 | 214 |
|
213 | 215 |
init_uart(51); //MAGIC NUMBER?? |
214 | 216 |
|
... | ... | |
234 | 236 |
|
235 | 237 |
if (resp == TT_PROGD) { |
236 | 238 |
if (iteration == 0) { |
237 |
onboard_program_write(caddr++, &(mbuf[2])); |
|
239 |
// Store the jump to user code |
|
240 |
jbuf[0] = mbuf[0]; |
|
241 |
jbuf[1] = mbuf[1]; |
|
242 |
|
|
243 |
*((uint16_t*)mbuf) = pgm_read_word(MAIN_ADDR); |
|
244 |
|
|
245 |
onboard_program_write(caddr, mbuf); |
|
238 | 246 |
iteration = 1; |
239 | 247 |
caddr += PROGD_PACKET_SIZE - 2; |
240 | 248 |
} else { |
241 |
onboard_program_write(caddr, &(mbuf[2]));
|
|
249 |
onboard_program_write(caddr, mbuf);
|
|
242 | 250 |
caddr += PROGD_PACKET_SIZE; |
243 | 251 |
} |
244 | 252 |
} else { |
245 |
while(1) { |
|
246 |
flash_led(1); |
|
247 |
} |
|
253 |
main_start(); |
|
248 | 254 |
} |
249 | 255 |
|
250 | 256 |
send_packet(TT_ACK); |
251 | 257 |
|
252 | 258 |
if (prog_len <= PROGD_PACKET_SIZE) { |
253 | 259 |
send_packet(TT_ACK); |
260 |
|
|
261 |
for (i = 0; i < PROGD_PACKET_SIZE; i++) { |
|
262 |
mbuf[i]= 0; |
|
263 |
} |
|
264 |
|
|
265 |
mbuf[PROGD_PACKET_SIZE-2] = jbuf[0]; |
|
266 |
mbuf[PROGD_PACKET_SIZE-1] = jbuf[1]; |
|
267 |
|
|
268 |
onboard_program_write(JUMP_ADDR, mbuf); |
|
269 |
|
|
254 | 270 |
PORTB = 0; |
255 |
_delay_ms(1000); |
|
256 | 271 |
main_start(); |
257 | 272 |
} else { |
258 | 273 |
prog_len -= PROGD_PACKET_SIZE; |
... | ... | |
260 | 275 |
} |
261 | 276 |
} |
262 | 277 |
|
278 |
|
|
279 |
int main (void) { |
|
280 |
while(1); |
|
281 |
} |
trunk/toolbox/bootloader/Makefile | ||
---|---|---|
180 | 180 |
# -Wl,...: tell GCC to pass this to linker. |
181 | 181 |
# -Map: create map file |
182 | 182 |
# --cref: add cross reference to map file |
183 |
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref,--section-start=.bootloader=0x400
|
|
183 |
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref,--section-start=.bootloader=0x5C0
|
|
184 | 184 |
LDFLAGS += $(EXTMEMOPTS) |
185 | 185 |
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB) |
186 | 186 |
|
Also available in: Unified diff