Revision 201
- Setup bootloader to initialize pins to a known safe state
- Pulled pindefs for the toolbox out of the toolbox into common/toolbox_pindefs.h
- Shared toolbox pindefs with the bootloader
- Replaced all raw pin names with the #defined names
- Optimizied out some bytes out of the bootloader by sharing variables
bootloader.c | ||
---|---|---|
13 | 13 |
// Error thresholds |
14 | 14 |
#define MAX_RETRIES 5 // Number of times to retry before giving up |
15 | 15 |
|
16 |
//Status LED |
|
17 |
#define LED_DDR DDRB |
|
18 |
#define LED_PORT PORTB |
|
19 |
#define LED PORTB1 |
|
20 |
|
|
21 | 16 |
/** |
22 | 17 |
* Where we store the jump to user code. The jump address is in words |
23 | 18 |
* due to how the rjmp instruction works. It is 1 word below the bootloader |
... | ... | |
51 | 46 |
int main(void) { |
52 | 47 |
uint8_t mbuf[PROGD_PACKET_SIZE]; |
53 | 48 |
rjump_t jbuf; |
54 |
uint16_t caddr = MAIN_ADDR;
|
|
55 |
uint8_t iteration;
|
|
49 |
uint16_t caddr; |
|
50 |
uint16_t prog_len;
|
|
56 | 51 |
uint8_t resp; |
57 |
uint16_t prog_len; |
|
58 | 52 |
uint8_t i; |
59 | 53 |
uint8_t retries; |
60 | 54 |
uint8_t addr; |
61 | 55 |
uint8_t boot; |
62 | 56 |
|
63 |
retry_jpnt: |
|
64 |
iteration = 0; |
|
57 |
// This is where we jump to if we fail out because of retries |
|
58 |
retry_jpnt: |
|
59 |
|
|
60 |
// We set the variables here |
|
61 |
caddr = MAIN_ADDR; |
|
65 | 62 |
retries = 0; |
66 |
boot = 0; |
|
67 |
addr = eeprom_read_byte(EEPROM_ADDR); |
|
68 |
//set LED pins as output |
|
69 |
LED_DDR = 0x07; |
|
70 |
PORTB = 0x00; |
|
63 |
boot = FALSE; |
|
71 | 64 |
|
72 |
|
|
65 |
// Grab the address from EEPROM |
|
66 |
addr = eeprom_read_byte((void*)EEPROM_ADDR); |
|
67 |
|
|
68 |
// Initialize the Pins |
|
69 |
DDRB = LED_GREEN | LED_YELLOW | LED_RED; // Set the LED pins as outputs |
|
70 |
PORTB = 0x00; // LEDs on == in bootloader |
|
71 |
DDRD = RELAY; // Set Relay as an output |
|
72 |
PORTD = 0x00; // Turn relay off |
|
73 |
|
|
73 | 74 |
// Clear the watchdog timer |
74 | 75 |
if (MCUSR & _BV(WDRF)) { |
75 | 76 |
MCUSR &= ~_BV(WDRF); |
76 | 77 |
wdt_disable(); |
77 | 78 |
WDTCSR = 0; |
78 |
boot = 1;
|
|
79 |
boot = TRUE;
|
|
79 | 80 |
} |
80 | 81 |
|
81 |
if (!(PINB & (_BV(PINB3) | _BV(PINB4)))) { |
|
82 |
PORTB |= 0x01; |
|
83 |
boot = 1; |
|
82 |
if (!(BUT_PORT & (BUT_RED | BUT_BLACK))) { |
|
83 |
boot = TRUE; |
|
84 | 84 |
} |
85 | 85 |
|
86 |
// Initialize the RS485 |
|
86 | 87 |
rs485_init(BAUD9600); |
87 | 88 |
|
88 |
//Start bootloading process |
|
89 |
if (boot == 0) { |
|
90 |
PORTB |= 0x07; |
|
91 |
main_start(); |
|
89 |
//Execute user code if we aren't supposed to enter bootloading mode |
|
90 |
if (boot == FALSE) { |
|
91 |
goto run_user_code; |
|
92 | 92 |
} |
93 | 93 |
|
94 |
// Send the boot packet |
|
94 | 95 |
send_packet(TT_BOOT, addr); |
95 | 96 |
resp = parse_packet(mbuf, addr); |
96 | 97 |
|
... | ... | |
108 | 109 |
|
109 | 110 |
// Run user code |
110 | 111 |
} else { |
111 |
PORTB = 0x07; |
|
112 |
run_user_code: |
|
113 |
LED_PORT = LED_GREEN | LED_YELLOW | LED_RED; |
|
112 | 114 |
main_start(); |
113 | 115 |
} |
114 | 116 |
|
... | ... | |
119 | 121 |
|
120 | 122 |
if (resp == TT_PROGD) { |
121 | 123 |
// We need to muck with the reset vector jump in the first page |
122 |
if (iteration == 0) {
|
|
124 |
if (caddr == MAIN_ADDR) {
|
|
123 | 125 |
// Store the jump to user code |
124 | 126 |
jbuf.bytes[0] = mbuf[0]; |
125 | 127 |
jbuf.bytes[1] = mbuf[1]; |
... | ... | |
134 | 136 |
// Rewrite the reset vector to jump to the bootloader |
135 | 137 |
mbuf[0] = (BOOT_START/2 - 1) & 0xFF; |
136 | 138 |
mbuf[1] = 0xC0 | (((BOOT_START/2 - 1) >> 8) & 0x0F); |
137 |
|
|
138 |
iteration = 1; |
|
139 | 139 |
} |
140 | 140 |
|
141 | 141 |
// Write the page to the flash |
... | ... | |
167 | 167 |
|
168 | 168 |
onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf); |
169 | 169 |
|
170 |
PORTB = 0x07;
|
|
171 |
main_start();
|
|
170 |
// Jump to user code that we just wrote
|
|
171 |
goto run_user_code;
|
|
172 | 172 |
} else { |
173 | 173 |
prog_len -= PROGD_PACKET_SIZE; |
174 | 174 |
} |
Also available in: Unified diff