root / trunk / toolbox / bootloader / bootloader.c @ 156
History | View | Annotate | Download (6.3 KB)
1 |
|
---|---|
2 |
|
3 |
#include <avr/io.h> |
4 |
#include <util/delay.h> |
5 |
#include <avr/boot.h> |
6 |
|
7 |
#include "tooltron.h" |
8 |
#include "uart.h" |
9 |
|
10 |
#define TRUE 0 |
11 |
#define FALSE 1 |
12 |
|
13 |
#define ADDR 18 |
14 |
|
15 |
|
16 |
#define MAX_WAIT_IN_CYCLES 800000 |
17 |
|
18 |
//Status LED
|
19 |
#define LED_DDR DDRB
|
20 |
#define LED_PORT PORTB
|
21 |
#define LED PORTB1
|
22 |
|
23 |
#define PAGE_SIZE 32 |
24 |
|
25 |
//Function prototypes
|
26 |
void flash_led(uint8_t);
|
27 |
void onboard_program_write(uint32_t page, uint8_t *buf);
|
28 |
void (*main_start)(void) = 0x0000; |
29 |
|
30 |
//Variables
|
31 |
uint8_t incoming_page_data[PAGE_SIZE]; |
32 |
uint8_t page_length; |
33 |
uint8_t retransmit_flag = FALSE; |
34 |
|
35 |
union page_address_union {
|
36 |
uint16_t word; |
37 |
uint8_t byte[2];
|
38 |
} page_address; |
39 |
|
40 |
char getch(void); |
41 |
|
42 |
typedef enum { |
43 |
sd, |
44 |
src, |
45 |
dest, |
46 |
comd, |
47 |
read, |
48 |
cs, |
49 |
ack |
50 |
} state_t; |
51 |
|
52 |
|
53 |
char parse_packet(char *mbuf) { |
54 |
uint8_t r; // Byte from the network
|
55 |
uint8_t crc; // Running checksum of the packet
|
56 |
uint8_t cmd; // The command received
|
57 |
uint8_t pos; // Position in the message buffer
|
58 |
uint8_t lim; // Max number of bytes to read into the message buf
|
59 |
state_t state; // State machine
|
60 |
|
61 |
while (1) { |
62 |
// Wait for the next byte
|
63 |
while ((uart_get_byte(&r)) < 0); |
64 |
|
65 |
switch (state) {
|
66 |
case sd:
|
67 |
if (r == DELIM) {
|
68 |
state = src; |
69 |
} |
70 |
break;
|
71 |
|
72 |
case src:
|
73 |
if (r == DELIM) {
|
74 |
state = src; |
75 |
} else {
|
76 |
crc = r; |
77 |
state = dest; |
78 |
} |
79 |
break;
|
80 |
|
81 |
case dest:
|
82 |
if (r == DELIM) {
|
83 |
state = src; |
84 |
} else if (r == ADDR) { |
85 |
crc ^= r; |
86 |
state = comd; |
87 |
} else {
|
88 |
state = sd; |
89 |
} |
90 |
break;
|
91 |
|
92 |
case comd:
|
93 |
cmd = r; |
94 |
crc ^= r; |
95 |
|
96 |
if (r == DELIM) {
|
97 |
state = src; |
98 |
} else if (r == TT_PROGM) { |
99 |
lim = PROGM_PACKET_SIZE; |
100 |
state = read; |
101 |
} else if (r == TT_PROGD) { |
102 |
lim = PROGD_PACKET_SIZE; |
103 |
state = read; |
104 |
} else {
|
105 |
state = cs; |
106 |
} |
107 |
break;
|
108 |
|
109 |
case read:
|
110 |
mbuf[pos] = r; |
111 |
crc ^= r; |
112 |
pos++; |
113 |
|
114 |
if (pos == lim) {
|
115 |
state = cs; |
116 |
} |
117 |
|
118 |
break;
|
119 |
|
120 |
case cs:
|
121 |
if (r == crc) {
|
122 |
return cmd;
|
123 |
} else {
|
124 |
return TT_BAD;
|
125 |
} |
126 |
|
127 |
break;
|
128 |
|
129 |
default:
|
130 |
return TT_BAD;
|
131 |
} |
132 |
} |
133 |
} |
134 |
|
135 |
void send_ack(void) { |
136 |
uart_send_byte(DELIM); |
137 |
uart_send_byte(ADDR); |
138 |
uart_send_byte(SERVER); |
139 |
uart_send_byte(TT_ACK); |
140 |
uart_send_byte(ACK_CRC); |
141 |
} |
142 |
|
143 |
int main(void) |
144 |
{ |
145 |
uint8_t check_sum = 0;
|
146 |
uint16_t i; |
147 |
|
148 |
init_uart(51); //MAGIC NUMBER?? |
149 |
|
150 |
//set LED pin as output
|
151 |
LED_DDR |= _BV(LED); |
152 |
|
153 |
//flash onboard LED to signal entering of bootloader
|
154 |
flash_led(1);
|
155 |
|
156 |
//Start bootloading process
|
157 |
uart_send_byte(5); //Tell the world we can be bootloaded |
158 |
|
159 |
//Check to see if the computer responded
|
160 |
uint32_t count = 0;
|
161 |
uint8_t resp; |
162 |
|
163 |
while(uart_get_byte(&resp) == -1) { |
164 |
count++; |
165 |
if (count > MAX_WAIT_IN_CYCLES)
|
166 |
//TODO: flash some leds or something
|
167 |
main_start(); |
168 |
} |
169 |
|
170 |
/* If the computer did not respond correctly with a ACK, we jump to
|
171 |
* user's program
|
172 |
*/
|
173 |
if(resp != 6) |
174 |
main_start(); |
175 |
|
176 |
while(1) { |
177 |
//Determine if the last received data was good or bad
|
178 |
if (check_sum != 0) //If the check sum does not compute, tell computer to resend same line |
179 |
RESTART: |
180 |
uart_send_byte(7); //Ascii character BELL |
181 |
else
|
182 |
uart_send_byte('T'); //Tell the computer that we are ready for the next line |
183 |
|
184 |
while(1) {//Wait for the computer to initiate transfer |
185 |
if (getch() == ':') break; //This is the "gimme the next chunk" command |
186 |
if (retransmit_flag == TRUE) goto RESTART; |
187 |
} |
188 |
|
189 |
page_length = getch(); //Get the length of this block
|
190 |
if (retransmit_flag == TRUE) goto RESTART; |
191 |
if(page_length > PAGE_SIZE) {
|
192 |
while(1) { |
193 |
flash_led(1);
|
194 |
} |
195 |
} |
196 |
|
197 |
if (page_length == 'S') {//Check to see if we are done - this is the "all done" command |
198 |
//boot_rww_enable (); //Wait for any flash writes to complete?
|
199 |
main_start(); |
200 |
} |
201 |
|
202 |
//Get the memory address at which to store this block of data
|
203 |
page_address.byte[0] = getch(); if (retransmit_flag == TRUE) goto RESTART; |
204 |
page_address.byte[1] = getch(); if (retransmit_flag == TRUE) goto RESTART; |
205 |
|
206 |
check_sum = getch(); //Pick up the check sum for error dectection
|
207 |
if (retransmit_flag == TRUE) goto RESTART; |
208 |
|
209 |
for(i = 0 ; i < page_length ; i++) {//Read the program data |
210 |
incoming_page_data[i] = getch(); |
211 |
if (retransmit_flag == TRUE) goto RESTART; |
212 |
} |
213 |
|
214 |
//Calculate the checksum
|
215 |
for(i = 0 ; i < page_length ; i++) |
216 |
check_sum = check_sum + incoming_page_data[i]; |
217 |
|
218 |
check_sum = check_sum + page_length; |
219 |
check_sum = check_sum + page_address.byte[0];
|
220 |
check_sum = check_sum + page_address.byte[1];
|
221 |
|
222 |
if(check_sum == 0) //If we have a good transmission, put it in ink |
223 |
onboard_program_write((uint32_t)page_address.word, incoming_page_data); |
224 |
} |
225 |
|
226 |
} |
227 |
|
228 |
//#define SPM_PAGESIZE 128
|
229 |
void onboard_program_write(uint32_t page, uint8_t *buf)
|
230 |
{ |
231 |
uint16_t i; |
232 |
//uint8_t sreg;
|
233 |
|
234 |
// Disable interrupts.
|
235 |
|
236 |
//sreg = SREG;
|
237 |
//cli();
|
238 |
|
239 |
//eeprom_busy_wait ();
|
240 |
|
241 |
boot_page_erase (page); |
242 |
boot_spm_busy_wait (); // Wait until the memory is erased.
|
243 |
|
244 |
for (i=0; i<SPM_PAGESIZE; i+=2){ |
245 |
// Set up little-endian word.
|
246 |
|
247 |
uint16_t w = *buf++; |
248 |
w += (*buf++) << 8;
|
249 |
|
250 |
boot_page_fill (page + i, w); |
251 |
} |
252 |
|
253 |
boot_page_write (page); // Store buffer in flash page.
|
254 |
boot_spm_busy_wait(); // Wait until the memory is written.
|
255 |
|
256 |
// Reenable RWW-section again. We need this if we want to jump back
|
257 |
// to the application after bootloading.
|
258 |
|
259 |
//boot_rww_enable ();
|
260 |
|
261 |
// Re-enable interrupts (if they were ever enabled).
|
262 |
|
263 |
//SREG = sreg;
|
264 |
} |
265 |
|
266 |
char getch(void) { |
267 |
retransmit_flag = FALSE; |
268 |
|
269 |
uint32_t count = 0;
|
270 |
uint8_t resp; |
271 |
while(uart_get_byte(&resp)==-1) { |
272 |
count++; |
273 |
if (count > MAX_WAIT_IN_CYCLES) {
|
274 |
retransmit_flag = TRUE; |
275 |
break;
|
276 |
} |
277 |
} |
278 |
|
279 |
return resp;
|
280 |
} |
281 |
|
282 |
void flash_led(uint8_t count)
|
283 |
{ |
284 |
uint8_t i; |
285 |
|
286 |
for (i = 0; i < count; ++i) { |
287 |
LED_PORT |= _BV(LED); |
288 |
_delay_ms(100);
|
289 |
LED_PORT &= ~_BV(LED); |
290 |
_delay_ms(100);
|
291 |
} |
292 |
} |