Revision 169
it works
bootloader.c | ||
---|---|---|
14 | 14 |
#define MAX_WAIT_IN_CYCLES 800000 |
15 | 15 |
#define MAX_TIMEOUT 60000 // Seconds to wait before exiting bootloader mode |
16 | 16 |
#define MAIN_ADDR 0x000 |
17 |
#define BOOT_START 0x500
|
|
17 |
#define BOOT_START 0x400
|
|
18 | 18 |
|
19 | 19 |
//Status LED |
20 | 20 |
#define LED_DDR DDRB |
... | ... | |
23 | 23 |
|
24 | 24 |
//Function prototypes |
25 | 25 |
void __init(void) __attribute__((naked)); |
26 |
void (*main_start)(void) = BOOT_START/2 - 1; |
|
26 | 27 |
|
27 | 28 |
|
28 | 29 |
typedef enum { |
... | ... | |
41 | 42 |
} rjump_t; |
42 | 43 |
|
43 | 44 |
// Redirect all unused interrupts to reti |
44 |
EMPTY_INTERRUPT(__vector_default); |
|
45 |
extern void __ctors_end(void) __attribute__((__noreturn__)); |
|
45 |
//EMPTY_INTERRUPT(__vector_default);
|
|
46 |
//extern void __ctors_end(void) __attribute__((__noreturn__));
|
|
46 | 47 |
|
47 | 48 |
// macros for entering subprograms |
48 |
#define __JMP_INIT__ __asm__ __volatile__ ("rjmp __ctors_end" ) |
|
49 |
//#define __JMP_INIT__ __asm__ __volatile__ ("rjmp __ctors_end" )
|
|
49 | 50 |
|
50 |
BOOTLOADER_SECTION void init_uart(uint16_t baud) {
|
|
51 |
/*BOOTLOADER_SECTION*/ void init_uart(uint16_t baud) {
|
|
51 | 52 |
// Set baud rate |
52 | 53 |
UBRRH = (uint8_t)(baud>>8); |
53 | 54 |
UBRRL = (uint8_t)baud; |
... | ... | |
60 | 61 |
uart_toggle_transmit(UART_TX_OFF); |
61 | 62 |
} |
62 | 63 |
|
63 |
BOOTLOADER_SECTION int8_t uart_get_byte(uint8_t *output_byte) {
|
|
64 |
/*BOOTLOADER_SECTION*/ int8_t uart_get_byte(uint8_t *output_byte) {
|
|
64 | 65 |
if (UCSRA & _BV(RXC)) { |
65 | 66 |
*output_byte = UDR; |
66 | 67 |
return 0; |
... | ... | |
69 | 70 |
} |
70 | 71 |
} |
71 | 72 |
|
72 |
BOOTLOADER_SECTION void uart_send_byte(uint8_t data) {
|
|
73 |
/*BOOTLOADER_SECTION*/ void uart_send_byte(uint8_t data) {
|
|
73 | 74 |
//Waits until current transmit is done |
74 | 75 |
while (!(UCSRA & _BV(UDRE))); |
75 | 76 |
//PORTB |= _BV(PORTB2); |
... | ... | |
89 | 90 |
return; |
90 | 91 |
} |
91 | 92 |
|
92 |
BOOTLOADER_SECTION void uart_toggle_transmit(uint8_t state) {
|
|
93 |
/*BOOTLOADER_SECTION*/ void uart_toggle_transmit(uint8_t state) {
|
|
93 | 94 |
if (state == UART_TX_ON) { |
94 | 95 |
PORTD |= TX_EN; |
95 | 96 |
} else { |
... | ... | |
97 | 98 |
} |
98 | 99 |
} |
99 | 100 |
|
100 |
BOOTLOADER_SECTION char parse_packet(uint8_t *mbuf) {
|
|
101 |
/*BOOTLOADER_SECTION*/ char parse_packet(uint8_t *mbuf) {
|
|
101 | 102 |
uint8_t r; // Byte from the network |
102 | 103 |
uint8_t crc; // Running checksum of the packet |
103 | 104 |
uint8_t cmd; // The command received |
... | ... | |
192 | 193 |
} |
193 | 194 |
} |
194 | 195 |
|
195 |
BOOTLOADER_SECTION void send_packet(uint8_t cmd) {
|
|
196 |
/*BOOTLOADER_SECTION*/ void send_packet(uint8_t cmd) {
|
|
196 | 197 |
uart_send_byte(DELIM); |
197 | 198 |
uart_send_byte(ADDR); |
198 | 199 |
uart_send_byte(SERVER); |
... | ... | |
201 | 202 |
} |
202 | 203 |
|
203 | 204 |
//#define SPM_PAGESIZE 32 |
204 |
BOOTLOADER_SECTION void onboard_program_write(uint16_t page, uint8_t *buf) {
|
|
205 |
/*BOOTLOADER_SECTION*/ void onboard_program_write(uint16_t page, uint8_t *buf) {
|
|
205 | 206 |
uint16_t i; |
206 | 207 |
|
207 | 208 |
boot_page_erase (page); |
... | ... | |
216 | 217 |
boot_spm_busy_wait(); // Wait until the memory is written. |
217 | 218 |
} |
218 | 219 |
|
219 |
BOOTLOADER_SECTION void main(void) {
|
|
220 |
/*BOOTLOADER_SECTION*/ int main(void) {
|
|
220 | 221 |
uint8_t mbuf[PROGD_PACKET_SIZE]; |
221 | 222 |
rjump_t jbuf; |
222 | 223 |
uint16_t caddr = MAIN_ADDR; |
... | ... | |
224 | 225 |
uint8_t resp; |
225 | 226 |
uint16_t prog_len; |
226 | 227 |
uint8_t i; |
227 |
void (*main_start)(void); |
|
228 | 228 |
|
229 |
main_start = BOOT_START - 2;
|
|
229 |
//main_start = (void*)(BOOT_START - 2);
|
|
230 | 230 |
iteration = 0; |
231 | 231 |
|
232 | 232 |
init_uart(51); //MAGIC NUMBER?? |
... | ... | |
258 | 258 |
jbuf.bytes[1] = mbuf[1]; |
259 | 259 |
|
260 | 260 |
jbuf.sword &= 0x0FFF; |
261 |
jbuf.sword -= BOOT_START >> 1;
|
|
261 |
jbuf.sword -= (BOOT_START >> 1) - 1;
|
|
262 | 262 |
jbuf.sword &= 0x0FFF; |
263 | 263 |
jbuf.sword |= 0xC000; |
264 | 264 |
|
265 |
mbuf[0] = pgm_read_byte(MAIN_ADDR); |
|
266 |
mbuf[1] = pgm_read_byte(MAIN_ADDR + 1); |
|
265 |
mbuf[0] = (BOOT_START/2 - 1) & 0xFF; |
|
266 |
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); |
|
267 | 270 |
//PORTB &= ~_BV(PORTB0); |
268 | 271 |
//while(1); |
269 | 272 |
|
... | ... | |
300 | 303 |
prog_len -= PROGD_PACKET_SIZE; |
301 | 304 |
} |
302 | 305 |
} |
306 |
|
|
307 |
return -1; |
|
303 | 308 |
} |
304 | 309 |
|
305 | 310 |
/* |
Also available in: Unified diff