Revision 171
Code cleanup
bootloader.c | ||
---|---|---|
11 | 11 |
#define ADDR 18 |
12 | 12 |
|
13 | 13 |
|
14 |
#define MAX_WAIT_IN_CYCLES 800000 |
|
15 |
#define MAX_TIMEOUT 60000 // Seconds to wait before exiting bootloader mode |
|
14 |
#define MAX_TIMEOUT 60000 // Seconds to wait before exiting bootloader mode |
|
16 | 15 |
#define MAIN_ADDR 0x000 |
17 | 16 |
#define BOOT_START 0x400 |
18 | 17 |
|
... | ... | |
22 | 21 |
#define LED PORTB1 |
23 | 22 |
|
24 | 23 |
//Function prototypes |
25 |
void __init(void) __attribute__((naked)); |
|
26 |
void (*main_start)(void) = BOOT_START/2 - 1; |
|
24 |
void (*main_start)(void) = BOOT_START/2 - 1; |
|
27 | 25 |
|
28 |
|
|
26 |
// Packet handler states |
|
29 | 27 |
typedef enum { |
30 | 28 |
sd, |
31 | 29 |
src, |
... | ... | |
41 | 39 |
int16_t sword; |
42 | 40 |
} rjump_t; |
43 | 41 |
|
44 |
// Redirect all unused interrupts to reti |
|
45 |
//EMPTY_INTERRUPT(__vector_default); |
|
46 |
//extern void __ctors_end(void) __attribute__((__noreturn__)); |
|
47 |
|
|
48 |
// macros for entering subprograms |
|
49 |
//#define __JMP_INIT__ __asm__ __volatile__ ("rjmp __ctors_end" ) |
|
50 |
|
|
51 |
/*BOOTLOADER_SECTION*/ void init_uart(uint16_t baud) { |
|
42 |
void init_uart(uint16_t baud) { |
|
52 | 43 |
// Set baud rate |
53 | 44 |
UBRRH = (uint8_t)(baud>>8); |
54 | 45 |
UBRRL = (uint8_t)baud; |
... | ... | |
61 | 52 |
uart_toggle_transmit(UART_TX_OFF); |
62 | 53 |
} |
63 | 54 |
|
64 |
/*BOOTLOADER_SECTION*/ int8_t uart_get_byte(uint8_t *output_byte) {
|
|
55 |
int8_t uart_get_byte(uint8_t *output_byte) { |
|
65 | 56 |
if (UCSRA & _BV(RXC)) { |
66 | 57 |
*output_byte = UDR; |
67 | 58 |
return 0; |
... | ... | |
70 | 61 |
} |
71 | 62 |
} |
72 | 63 |
|
73 |
/*BOOTLOADER_SECTION*/ void uart_send_byte(uint8_t data) {
|
|
64 |
void uart_send_byte(uint8_t data) { |
|
74 | 65 |
//Waits until current transmit is done |
75 | 66 |
while (!(UCSRA & _BV(UDRE))); |
76 |
//PORTB |= _BV(PORTB2); |
|
77 | 67 |
|
78 | 68 |
// Enable writes and send |
79 | 69 |
uart_toggle_transmit(UART_TX_ON); |
... | ... | |
84 | 74 |
uart_toggle_transmit(UART_TX_OFF); |
85 | 75 |
UCSRA |= _BV(TXC); |
86 | 76 |
|
87 |
//PORTB |= _BV(PORTB1); |
|
88 |
|
|
89 |
|
|
90 | 77 |
return; |
91 | 78 |
} |
92 | 79 |
|
93 |
/*BOOTLOADER_SECTION*/ void uart_toggle_transmit(uint8_t state) {
|
|
80 |
void uart_toggle_transmit(uint8_t state) { |
|
94 | 81 |
if (state == UART_TX_ON) { |
95 | 82 |
PORTD |= TX_EN; |
96 | 83 |
} else { |
... | ... | |
98 | 85 |
} |
99 | 86 |
} |
100 | 87 |
|
101 |
/*BOOTLOADER_SECTION*/ char parse_packet(uint8_t *mbuf) {
|
|
88 |
char parse_packet(uint8_t *mbuf) { |
|
102 | 89 |
uint8_t r; // Byte from the network |
103 | 90 |
uint8_t crc; // Running checksum of the packet |
104 | 91 |
uint8_t cmd; // The command received |
... | ... | |
120 | 107 |
while ((uart_get_byte(&r)) < 0) { |
121 | 108 |
if (count >= MAX_TIMEOUT) { |
122 | 109 |
return TT_BAD; |
123 |
} |
|
110 |
} else { |
|
111 |
count++; |
|
112 |
} |
|
124 | 113 |
} |
125 | 114 |
|
126 | 115 |
switch (state) { |
... | ... | |
193 | 182 |
} |
194 | 183 |
} |
195 | 184 |
|
196 |
/*BOOTLOADER_SECTION*/ void send_packet(uint8_t cmd) {
|
|
185 |
void send_packet(uint8_t cmd) { |
|
197 | 186 |
uart_send_byte(DELIM); |
198 | 187 |
uart_send_byte(ADDR); |
199 | 188 |
uart_send_byte(SERVER); |
... | ... | |
201 | 190 |
uart_send_byte(ACK_CRC ^ cmd); |
202 | 191 |
} |
203 | 192 |
|
204 |
//#define SPM_PAGESIZE 32
|
|
205 |
/*BOOTLOADER_SECTION*/ void onboard_program_write(uint16_t page, uint8_t *buf) {
|
|
193 |
// SPM_PAGESIZE is set to 32 bytes
|
|
194 |
void onboard_program_write(uint16_t page, uint8_t *buf) { |
|
206 | 195 |
uint16_t i; |
207 | 196 |
|
208 | 197 |
boot_page_erase (page); |
... | ... | |
217 | 206 |
boot_spm_busy_wait(); // Wait until the memory is written. |
218 | 207 |
} |
219 | 208 |
|
220 |
/*BOOTLOADER_SECTION*/ int main(void) {
|
|
209 |
int main(void) { |
|
221 | 210 |
uint8_t mbuf[PROGD_PACKET_SIZE]; |
222 | 211 |
rjump_t jbuf; |
223 | 212 |
uint16_t caddr = MAIN_ADDR; |
... | ... | |
264 | 253 |
|
265 | 254 |
mbuf[0] = (BOOT_START/2 - 1) & 0xFF; |
266 | 255 |
mbuf[1] = 0xC0 | (((BOOT_START/2 - 1) >> 8) & 0x0F); |
267 |
//memcpy_P(mbuf, (PGM_VOID_P)MAIN_ADDR, 2); |
|
268 |
//mbuf[0] = pgm_read_byte(MAIN_ADDR); |
|
269 |
//mbuf[1] = pgm_read_byte(MAIN_ADDR + 1); |
|
270 |
//PORTB &= ~_BV(PORTB0); |
|
271 |
//while(1); |
|
272 | 256 |
|
273 | 257 |
iteration = 1; |
274 |
} /*else { |
|
275 |
PORTB &= ~_BV(PORTB1); |
|
276 |
while(1); |
|
277 |
}*/ |
|
258 |
} |
|
278 | 259 |
|
279 |
|
|
280 | 260 |
onboard_program_write(caddr, mbuf); |
281 | 261 |
caddr += PROGD_PACKET_SIZE; |
282 | 262 |
} else { |
... | ... | |
303 | 283 |
prog_len -= PROGD_PACKET_SIZE; |
304 | 284 |
} |
305 | 285 |
} |
306 |
|
|
286 |
|
|
287 |
// Should never get here |
|
307 | 288 |
return -1; |
308 | 289 |
} |
309 |
|
|
310 |
/* |
|
311 |
int main (void) { |
|
312 |
while(1); |
|
313 |
}*/ |
Also available in: Unified diff