Revision 179
Moved bootloader to the trunk
trunk/toolbox/bootloader/bootloader.c | ||
---|---|---|
1 |
#include <avr/io.h> |
|
2 |
#include <avr/boot.h> |
|
3 |
#include <avr/pgmspace.h> |
|
4 |
#include <avr/wdt.h> |
|
5 |
|
|
6 |
#include "tooltron.h" |
|
7 |
#include "uart.h" |
|
8 |
|
|
9 |
#define TRUE 0 |
|
10 |
#define FALSE 1 |
|
11 |
|
|
12 |
#define ADDR 18 |
|
13 |
|
|
14 |
|
|
15 |
// Error thresholds |
|
16 |
#define MAX_TIMEOUT 60000 // Seconds to wait before exiting bootloader mode |
|
17 |
#define MAX_RETRIES 5 // Number of times to retry before giving up |
|
18 |
|
|
19 |
// Memory locations |
|
20 |
#define MAIN_ADDR 0x000 // Where the user code starts |
|
21 |
#define BOOT_START 0x400 // Where the bootloader starts |
|
22 |
|
|
23 |
//Status LED |
|
24 |
#define LED_DDR DDRB |
|
25 |
#define LED_PORT PORTB |
|
26 |
#define LED PORTB1 |
|
27 |
|
|
28 |
//Function prototypes |
|
29 |
void (*main_start)(void) = BOOT_START/2 - 1; |
|
30 |
|
|
31 |
// Packet handler states |
|
32 |
typedef enum { |
|
33 |
sd, |
|
34 |
src, |
|
35 |
dest, |
|
36 |
comd, |
|
37 |
read, |
|
38 |
cs, |
|
39 |
ack |
|
40 |
} state_t; |
|
41 |
|
|
42 |
typedef union { |
|
43 |
uint8_t bytes[2]; |
|
44 |
int16_t sword; |
|
45 |
} rjump_t; |
|
46 |
|
|
47 |
void init_uart(uint16_t baud) { |
|
48 |
// Set baud rate |
|
49 |
UBRRH = (uint8_t)(baud>>8); |
|
50 |
UBRRL = (uint8_t)baud; |
|
51 |
|
|
52 |
// Enable RX/TX |
|
53 |
UCSRB = _BV(RXEN) | _BV(TXEN); |
|
54 |
|
|
55 |
// Enable the TXEN pin as output |
|
56 |
DDRD |= TX_EN; |
|
57 |
uart_toggle_transmit(UART_TX_OFF); |
|
58 |
} |
|
59 |
|
|
60 |
int8_t uart_get_byte(uint8_t *output_byte) { |
|
61 |
if (UCSRA & _BV(RXC)) { |
|
62 |
*output_byte = UDR; |
|
63 |
return 0; |
|
64 |
} else { |
|
65 |
return -1; |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
void uart_send_byte(uint8_t data) { |
|
70 |
//Waits until current transmit is done |
|
71 |
while (!(UCSRA & _BV(UDRE))); |
|
72 |
|
|
73 |
// Enable writes and send |
|
74 |
uart_toggle_transmit(UART_TX_ON); |
|
75 |
UDR = data; |
|
76 |
|
|
77 |
// Waits until the transmit is done |
|
78 |
while(!(UCSRA & _BV(TXC))); |
|
79 |
uart_toggle_transmit(UART_TX_OFF); |
|
80 |
UCSRA |= _BV(TXC); |
|
81 |
|
|
82 |
return; |
|
83 |
} |
|
84 |
|
|
85 |
void uart_toggle_transmit(uint8_t state) { |
|
86 |
if (state == UART_TX_ON) { |
|
87 |
PORTD |= TX_EN; |
|
88 |
} else { |
|
89 |
PORTD &= ~TX_EN; |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
char parse_packet(uint8_t *mbuf) { |
|
94 |
uint8_t r; // Byte from the network |
|
95 |
uint8_t crc; // Running checksum of the packet |
|
96 |
uint8_t cmd; // The command received |
|
97 |
uint8_t pos; // Position in the message buffer |
|
98 |
uint8_t lim; // Max number of bytes to read into the message buf |
|
99 |
state_t state; // State machine |
|
100 |
uint16_t count; |
|
101 |
|
|
102 |
r = 0; |
|
103 |
crc = 0; |
|
104 |
cmd = 0; |
|
105 |
pos = 0; |
|
106 |
lim = 0; |
|
107 |
state = sd; |
|
108 |
count = 0; |
|
109 |
|
|
110 |
while (1) { |
|
111 |
// Wait for the next byte |
|
112 |
while ((uart_get_byte(&r)) < 0) { |
|
113 |
if (count >= MAX_TIMEOUT) { |
|
114 |
return TT_BAD; |
|
115 |
} else { |
|
116 |
count++; |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
switch (state) { |
|
121 |
case sd: |
|
122 |
if (r == DELIM) { |
|
123 |
state = src; |
|
124 |
} |
|
125 |
break; |
|
126 |
|
|
127 |
case src: |
|
128 |
if (r == DELIM) { |
|
129 |
state = src; |
|
130 |
} else { |
|
131 |
crc = r; |
|
132 |
state = dest; |
|
133 |
} |
|
134 |
break; |
|
135 |
|
|
136 |
case dest: |
|
137 |
if (r == DELIM) { |
|
138 |
state = src; |
|
139 |
} else if (r == ADDR) { |
|
140 |
crc ^= r; |
|
141 |
state = comd; |
|
142 |
} else { |
|
143 |
state = sd; |
|
144 |
} |
|
145 |
break; |
|
146 |
|
|
147 |
case comd: |
|
148 |
cmd = r; |
|
149 |
crc ^= r; |
|
150 |
|
|
151 |
if (r == DELIM) { |
|
152 |
state = src; |
|
153 |
} else if (r == TT_PROGM) { |
|
154 |
lim = PROGM_PACKET_SIZE; |
|
155 |
state = read; |
|
156 |
} else if (r == TT_PROGD) { |
|
157 |
lim = PROGD_PACKET_SIZE; |
|
158 |
state = read; |
|
159 |
} else { |
|
160 |
state = cs; |
|
161 |
} |
|
162 |
break; |
|
163 |
|
|
164 |
case read: |
|
165 |
mbuf[pos] = r; |
|
166 |
crc ^= r; |
|
167 |
pos++; |
|
168 |
|
|
169 |
if (pos == lim) { |
|
170 |
state = cs; |
|
171 |
} |
|
172 |
|
|
173 |
break; |
|
174 |
|
|
175 |
case cs: |
|
176 |
if (r == crc) { |
|
177 |
return cmd; |
|
178 |
} else { |
|
179 |
return TT_BAD; |
|
180 |
} |
|
181 |
|
|
182 |
break; |
|
183 |
|
|
184 |
default: |
|
185 |
return TT_BAD; |
|
186 |
} |
|
187 |
} |
|
188 |
} |
|
189 |
|
|
190 |
void send_packet(uint8_t cmd) { |
|
191 |
uart_send_byte(DELIM); |
|
192 |
uart_send_byte(ADDR); |
|
193 |
uart_send_byte(SERVER); |
|
194 |
uart_send_byte(cmd); |
|
195 |
uart_send_byte(ACK_CRC ^ cmd); |
|
196 |
} |
|
197 |
|
|
198 |
// SPM_PAGESIZE is set to 32 bytes |
|
199 |
void onboard_program_write(uint16_t page, uint8_t *buf) { |
|
200 |
uint16_t i; |
|
201 |
|
|
202 |
boot_page_erase (page); |
|
203 |
boot_spm_busy_wait (); // Wait until the memory is erased. |
|
204 |
|
|
205 |
for (i=0; i < SPM_PAGESIZE; i+=2){ |
|
206 |
// Set up little-endian word. |
|
207 |
boot_page_fill (page + i, buf[i] | (buf[i+1] <<8)); |
|
208 |
} |
|
209 |
|
|
210 |
boot_page_write (page); // Store buffer in flash page. |
|
211 |
boot_spm_busy_wait(); // Wait until the memory is written. |
|
212 |
} |
|
213 |
|
|
214 |
int main(void) { |
|
215 |
uint8_t mbuf[PROGD_PACKET_SIZE]; |
|
216 |
rjump_t jbuf; |
|
217 |
uint16_t caddr = MAIN_ADDR; |
|
218 |
uint8_t iteration; |
|
219 |
uint8_t resp; |
|
220 |
uint16_t prog_len; |
|
221 |
uint8_t i; |
|
222 |
uint8_t retries; |
|
223 |
|
|
224 |
retry_jpnt: |
|
225 |
iteration = 0; |
|
226 |
retries = 0; |
|
227 |
|
|
228 |
// Clear the watchdog timer |
|
229 |
MCUSR &= ~_BV(WDRF); |
|
230 |
wdt_disable(); |
|
231 |
WDTCSR = 0; |
|
232 |
|
|
233 |
|
|
234 |
init_uart(51); //MAGIC NUMBER?? |
|
235 |
|
|
236 |
//set LED pin as output |
|
237 |
LED_DDR |= 0x07; |
|
238 |
PORTB = 0x07; |
|
239 |
|
|
240 |
//Start bootloading process |
|
241 |
send_packet(TT_BOOT); |
|
242 |
|
|
243 |
resp = parse_packet(mbuf); |
|
244 |
|
|
245 |
// Enter programming mode |
|
246 |
if (resp == TT_PROGM) { |
|
247 |
prog_len = mbuf[0]; |
|
248 |
prog_len |= mbuf[1] << 8; |
|
249 |
|
|
250 |
// This will insert a NOP into the user code jump in case |
|
251 |
// the programming fails |
|
252 |
for (i = 0; i < PROGD_PACKET_SIZE; i++) { |
|
253 |
mbuf[i]= 0; |
|
254 |
} |
|
255 |
onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf); |
|
256 |
|
|
257 |
// Run user code |
|
258 |
} else { |
|
259 |
main_start(); |
|
260 |
} |
|
261 |
|
|
262 |
send_packet(TT_ACK); |
|
263 |
|
|
264 |
while(1) { |
|
265 |
resp = parse_packet(mbuf); |
|
266 |
|
|
267 |
if (resp == TT_PROGD) { |
|
268 |
// We need to muck with the reset vector jump in the first page |
|
269 |
if (iteration == 0) { |
|
270 |
// Store the jump to user code |
|
271 |
jbuf.bytes[0] = mbuf[0]; |
|
272 |
jbuf.bytes[1] = mbuf[1]; |
|
273 |
|
|
274 |
// Rewrite the user code jump to be correct since we are |
|
275 |
// using relative jumps (rjmp) |
|
276 |
jbuf.sword &= 0x0FFF; |
|
277 |
jbuf.sword -= (BOOT_START >> 1) - 1; |
|
278 |
jbuf.sword &= 0x0FFF; |
|
279 |
jbuf.sword |= 0xC000; |
|
280 |
|
|
281 |
// Rewrite the reset vector to jump to the bootloader |
|
282 |
mbuf[0] = (BOOT_START/2 - 1) & 0xFF; |
|
283 |
mbuf[1] = 0xC0 | (((BOOT_START/2 - 1) >> 8) & 0x0F); |
|
284 |
|
|
285 |
iteration = 1; |
|
286 |
} |
|
287 |
|
|
288 |
// Write the page to the flash |
|
289 |
onboard_program_write(caddr, mbuf); |
|
290 |
caddr += PROGD_PACKET_SIZE; |
|
291 |
retries = 0; |
|
292 |
} else { |
|
293 |
send_packet(TT_NACK); |
|
294 |
retries++; |
|
295 |
|
|
296 |
// If we failed too many times, reset. This goes to the start |
|
297 |
// of the bootloader function |
|
298 |
if (retries > MAX_RETRIES) { |
|
299 |
goto retry_jpnt; |
|
300 |
} |
|
301 |
} |
|
302 |
|
|
303 |
send_packet(TT_ACK); |
|
304 |
|
|
305 |
// Once we write the last packet we must override the jump to |
|
306 |
// user code to point to the correct address |
|
307 |
if (prog_len <= PROGD_PACKET_SIZE) { |
|
308 |
for (i = 0; i < PROGD_PACKET_SIZE; i++) { |
|
309 |
mbuf[i]= 0; |
|
310 |
} |
|
311 |
|
|
312 |
mbuf[PROGD_PACKET_SIZE-2] = jbuf.bytes[0]; |
|
313 |
mbuf[PROGD_PACKET_SIZE-1] = jbuf.bytes[1]; |
|
314 |
|
|
315 |
onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf); |
|
316 |
|
|
317 |
main_start(); |
|
318 |
} else { |
|
319 |
prog_len -= PROGD_PACKET_SIZE; |
|
320 |
} |
|
321 |
} |
|
322 |
|
|
323 |
// Should never get here |
|
324 |
return -1; |
|
325 |
} |
trunk/toolbox/bootloader/uart.h | ||
---|---|---|
1 |
/******** |
|
2 |
* This file is part of Tooltron. |
|
3 |
* |
|
4 |
* Tooltron is free software: you can redistribute it and/or modify |
|
5 |
* it under the terms of the Lesser GNU General Public License as published by |
|
6 |
* the Free Software Foundation, either version 3 of the License, or |
|
7 |
* (at your option) any later version. |
|
8 |
* |
|
9 |
* Tooltron is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* Lesser GNU General Public License for more details. |
|
13 |
* You should have received a copy of the Lesser GNU General Public License |
|
14 |
* along with Tooltron. If not, see <http://www.gnu.org/licenses/>. |
|
15 |
* |
|
16 |
* Copyright 2009 Kevin Woo <kwoo@2ndt.com> |
|
17 |
* |
|
18 |
********/ |
|
19 |
/** @file uart.h |
|
20 |
* |
|
21 |
* @brief Initializes UART functions using the UART hardware module |
|
22 |
* |
|
23 |
* @author Kevin Woo (kwoo) |
|
24 |
*/ |
|
25 |
|
|
26 |
#ifndef UART_H |
|
27 |
#define UART_H |
|
28 |
|
|
29 |
#include <avr/io.h> |
|
30 |
#include <avr/interrupt.h> |
|
31 |
#include <stdint.h> |
|
32 |
/** **/ |
|
33 |
#define UART_TX_OFF 0 |
|
34 |
#define UART_TX_ON 1 |
|
35 |
|
|
36 |
/** @brief RX Pin for the UART **/ |
|
37 |
#define RX _BV(PORTD0) |
|
38 |
#define TX _BV(PORTD1) |
|
39 |
#define TX_EN _BV(PORTD5) |
|
40 |
|
|
41 |
/** @brief The most recently received byte **/ |
|
42 |
extern uint8_t received_byte; |
|
43 |
/** @brief If the value in received_byte has been read or not **/ |
|
44 |
extern uint8_t byte_ready; |
|
45 |
|
|
46 |
/** @brief Initializes the UART registers and sets it to the buad rate |
|
47 |
* which msut be the value that is defined in the datasheet |
|
48 |
* for any particular speed (ie: 51 -> 9600bps) |
|
49 |
**/ |
|
50 |
void init_uart(uint16_t baud); |
|
51 |
/** @brief Gets latest byte and returns it in output_byte. If the byte |
|
52 |
* was already read, returns -1 otherwise it returns 0 |
|
53 |
**/ |
|
54 |
int8_t uart_get_byte(uint8_t *output_byte); |
|
55 |
/** @brief Sends a character array of size size. If we are currently |
|
56 |
* transmitting it will block until the the current transmit |
|
57 |
* is done. |
|
58 |
**/ |
|
59 |
void uart_send_byte(uint8_t data); |
|
60 |
void uart_toggle_transmit(uint8_t state); |
|
61 |
#endif |
trunk/toolbox/bootloader/Makefile | ||
---|---|---|
1 |
# Hey Emacs, this is a -*- makefile -*- |
|
2 |
#---------------------------------------------------------------------------- |
|
3 |
# WinAVR Makefile Template written by Eric B. Weddington, J?rg Wunsch, et al. |
|
4 |
# |
|
5 |
# Released to the Public Domain |
|
6 |
# |
|
7 |
# Additional material for this makefile was written by: |
|
8 |
# Peter Fleury |
|
9 |
# Tim Henigan |
|
10 |
# Colin O'Flynn |
|
11 |
# Reiner Patommel |
|
12 |
# Markus Pfaff |
|
13 |
# Sander Pool |
|
14 |
# Frederik Rouleau |
|
15 |
# |
|
16 |
#---------------------------------------------------------------------------- |
|
17 |
# On command line: |
|
18 |
# |
|
19 |
# make all = Make software. |
|
20 |
# |
|
21 |
# make clean = Clean out built project files. |
|
22 |
# |
|
23 |
# make coff = Convert ELF to AVR COFF. |
|
24 |
# |
|
25 |
# make extcoff = Convert ELF to AVR Extended COFF. |
|
26 |
# |
|
27 |
# make program = Download the hex file to the device, using avrdude. |
|
28 |
# Please customize the avrdude settings below first! |
|
29 |
# |
|
30 |
# make debug = Start either simulavr or avarice as specified for debugging, |
|
31 |
# with avr-gdb or avr-insight as the front end for debugging. |
|
32 |
# |
|
33 |
# make filename.s = Just compile filename.c into the assembler code only. |
|
34 |
# |
|
35 |
# make filename.i = Create a preprocessed source file for use in submitting |
|
36 |
# bug reports to the GCC project. |
|
37 |
# |
|
38 |
# To rebuild project do "make clean" then "make all". |
|
39 |
#---------------------------------------------------------------------------- |
|
40 |
|
|
41 |
|
|
42 |
# MCU name |
|
43 |
MCU = attiny2313 |
|
44 |
|
|
45 |
# Processor frequency. |
|
46 |
# This will define a symbol, F_CPU, in all source code files equal to the |
|
47 |
# processor frequency. You can then use this symbol in your source code to |
|
48 |
# calculate timings. Do NOT tack on a 'UL' at the end, this will be done |
|
49 |
# automatically to create a 32-bit value in your source code. |
|
50 |
F_CPU = 8000000 |
|
51 |
|
|
52 |
|
|
53 |
# Output format. (can be srec, ihex, binary) |
|
54 |
FORMAT = ihex |
|
55 |
|
|
56 |
# Target file name (without extension). |
|
57 |
TARGET = bootloader |
|
58 |
|
|
59 |
# List C source files here. (C dependencies are automatically generated.) |
|
60 |
SRC = $(wildcard *.c) |
|
61 |
|
|
62 |
# List Assembler source files here. |
|
63 |
# Make them always end in a capital .S. Files ending in a lowercase .s |
|
64 |
# will not be considered source files but generated files (assembler |
|
65 |
# output from the compiler), and will be deleted upon "make clean"! |
|
66 |
# Even though the DOS/Win* filesystem matches both .s and .S the same, |
|
67 |
# it will preserve the spelling of the filenames, and gcc itself does |
|
68 |
# care about how the name is spelled on its command-line. |
|
69 |
ASRC = |
|
70 |
|
|
71 |
|
|
72 |
# Optimization level, can be [0, 1, 2, 3, s]. |
|
73 |
# 0 = turn off optimization. s = optimize for size. |
|
74 |
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.) |
|
75 |
OPT = s |
|
76 |
|
|
77 |
|
|
78 |
# Debugging format. |
|
79 |
# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs. |
|
80 |
# AVR Studio 4.10 requires dwarf-2. |
|
81 |
# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run. |
|
82 |
DEBUG = |
|
83 |
|
|
84 |
|
|
85 |
# List any extra directories to look for include files here. |
|
86 |
# Each directory must be seperated by a space. |
|
87 |
# Use forward slashes for directory separators. |
|
88 |
# For a directory that has spaces, enclose it in quotes. |
|
89 |
#EXTRAINCDIRS = C:\WinAVR\include\fwr |
|
90 |
|
|
91 |
|
|
92 |
# Compiler flag to set the C Standard level. |
|
93 |
# c89 = "ANSI" C |
|
94 |
# gnu89 = c89 plus GCC extensions |
|
95 |
# c99 = ISO C99 standard (not yet fully implemented) |
|
96 |
# gnu99 = c99 plus GCC extensions |
|
97 |
CSTANDARD = -std=gnu99 |
|
98 |
|
|
99 |
|
|
100 |
# Place -D or -U options here |
|
101 |
CDEFS = -DF_CPU=$(F_CPU)UL |
|
102 |
|
|
103 |
|
|
104 |
# Place -I options here |
|
105 |
CINCS = |
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
#---------------- Compiler Options ---------------- |
|
110 |
# -g*: generate debugging information |
|
111 |
# -O*: optimization level |
|
112 |
# -f...: tuning, see GCC manual and avr-libc documentation |
|
113 |
# -Wall...: warning level |
|
114 |
# -Wa,...: tell GCC to pass this to the assembler. |
|
115 |
# -adhlns...: create assembler listing |
|
116 |
#CFLAGS = -g$(DEBUG) |
|
117 |
CFLAGS += $(CDEFS) $(CINCS) |
|
118 |
CFLAGS += -O$(OPT) |
|
119 |
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums |
|
120 |
CFLAGS += -Wall -Wstrict-prototypes |
|
121 |
CFLAGS += -Wa,-adhlns=$(<:.c=.lst) |
|
122 |
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) |
|
123 |
CFLAGS += $(CSTANDARD) |
|
124 |
|
|
125 |
|
|
126 |
#---------------- Assembler Options ---------------- |
|
127 |
# -Wa,...: tell GCC to pass this to the assembler. |
|
128 |
# -ahlms: create listing |
|
129 |
# -gstabs: have the assembler create line number information; note that |
|
130 |
# for use in COFF files, additional information about filenames |
|
131 |
# and function names needs to be present in the assembler source |
|
132 |
# files -- see avr-libc docs [FIXME: not yet described there] |
|
133 |
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs |
|
134 |
|
|
135 |
|
|
136 |
#---------------- Library Options ---------------- |
|
137 |
# Minimalistic printf version |
|
138 |
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min |
|
139 |
|
|
140 |
# Floating point printf version (requires MATH_LIB = -lm below) |
|
141 |
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt |
|
142 |
|
|
143 |
# If this is left blank, then it will use the Standard printf version. |
|
144 |
PRINTF_LIB = |
|
145 |
#PRINTF_LIB = $(PRINTF_LIB_MIN) |
|
146 |
#PRINTF_LIB = $(PRINTF_LIB_FLOAT) |
|
147 |
|
|
148 |
|
|
149 |
# Minimalistic scanf version |
|
150 |
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min |
|
151 |
|
|
152 |
# Floating point + %[ scanf version (requires MATH_LIB = -lm below) |
|
153 |
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt |
|
154 |
|
|
155 |
# If this is left blank, then it will use the Standard scanf version. |
|
156 |
SCANF_LIB = |
|
157 |
#SCANF_LIB = $(SCANF_LIB_MIN) |
|
158 |
#SCANF_LIB = $(SCANF_LIB_FLOAT) |
|
159 |
|
|
160 |
|
|
161 |
MATH_LIB = -lm |
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
#---------------- External Memory Options ---------------- |
|
166 |
|
|
167 |
# 64 KB of external RAM, starting after internal RAM (ATmega128!), |
|
168 |
# used for variables (.data/.bss) and heap (malloc()). |
|
169 |
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff |
|
170 |
|
|
171 |
# 64 KB of external RAM, starting after internal RAM (ATmega128!), |
|
172 |
# only used for heap (malloc()). |
|
173 |
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff |
|
174 |
|
|
175 |
EXTMEMOPTS = |
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
#---------------- Linker Options ---------------- |
|
180 |
# -Wl,...: tell GCC to pass this to linker. |
|
181 |
# -Map: create map file |
|
182 |
# --cref: add cross reference to map file |
|
183 |
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref,--section-start=.text=0x400 |
|
184 |
LDFLAGS += $(EXTMEMOPTS) |
|
185 |
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB) |
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
#---------------- Programming Options (avrdude) ---------------- |
|
190 |
|
|
191 |
# Programming hardware: alf avr910 avrisp bascom bsd |
|
192 |
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500 |
|
193 |
# |
|
194 |
# Type: avrdude -c ? |
|
195 |
# to get a full listing. |
|
196 |
# |
|
197 |
AVRDUDE_PROGRAMMER = avrispmkII |
|
198 |
|
|
199 |
# com1 = serial port. Use lpt1 to connect to parallel port. |
|
200 |
AVRDUDE_PORT = usb |
|
201 |
# programmer connected to serial device |
|
202 |
|
|
203 |
AVRDUDE_WRITE_FLASH = -b 4800 -U flash:w:$(TARGET).hex |
|
204 |
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep |
|
205 |
|
|
206 |
|
|
207 |
# Uncomment the following if you want avrdude's erase cycle counter. |
|
208 |
# Note that this counter needs to be initialized first using -Yn, |
|
209 |
# see avrdude manual. |
|
210 |
#AVRDUDE_ERASE_COUNTER = -y |
|
211 |
|
|
212 |
# Uncomment the following if you do /not/ wish a verification to be |
|
213 |
# performed after programming the device. |
|
214 |
#AVRDUDE_NO_VERIFY = -V |
|
215 |
|
|
216 |
# Increase verbosity level. Please use this when submitting bug |
|
217 |
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> |
|
218 |
# to submit bug reports. |
|
219 |
#AVRDUDE_VERBOSE = -v -v |
|
220 |
|
|
221 |
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) |
|
222 |
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY) |
|
223 |
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE) |
|
224 |
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER) |
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
#---------------- Debugging Options ---------------- |
|
229 |
|
|
230 |
# For simulavr only - target MCU frequency. |
|
231 |
DEBUG_MFREQ = $(F_CPU) |
|
232 |
|
|
233 |
# Set the DEBUG_UI to either gdb or insight. |
|
234 |
# DEBUG_UI = gdb |
|
235 |
DEBUG_UI = insight |
|
236 |
|
|
237 |
# Set the debugging back-end to either avarice, simulavr. |
|
238 |
DEBUG_BACKEND = avarice |
|
239 |
#DEBUG_BACKEND = simulavr |
|
240 |
|
|
241 |
# GDB Init Filename. |
|
242 |
GDBINIT_FILE = __avr_gdbinit |
|
243 |
|
|
244 |
# When using avarice settings for the JTAG |
|
245 |
JTAG_DEV = /dev/com1 |
|
246 |
|
|
247 |
# Debugging port used to communicate between GDB / avarice / simulavr. |
|
248 |
DEBUG_PORT = 4242 |
|
249 |
|
|
250 |
# Debugging host used to communicate between GDB / avarice / simulavr, normally |
|
251 |
# just set to localhost unless doing some sort of crazy debugging when |
|
252 |
# avarice is running on a different computer. |
|
253 |
DEBUG_HOST = localhost |
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
#============================================================================ |
|
258 |
|
|
259 |
|
|
260 |
# Define programs and commands. |
|
261 |
SHELL = sh |
|
262 |
CC = avr-gcc |
|
263 |
OBJCOPY = avr-objcopy |
|
264 |
OBJDUMP = avr-objdump |
|
265 |
SIZE = avr-size |
|
266 |
NM = avr-nm |
|
267 |
AVRDUDE = avrdude |
|
268 |
REMOVE = rm -f |
|
269 |
COPY = cp |
|
270 |
WINSHELL = cmd |
|
271 |
|
|
272 |
|
|
273 |
# Define Messages |
|
274 |
# English |
|
275 |
MSG_ERRORS_NONE = Errors: none |
|
276 |
MSG_BEGIN = -------- begin -------- |
|
277 |
MSG_END = -------- end -------- |
|
278 |
MSG_SIZE_BEFORE = Size before: |
|
279 |
MSG_SIZE_AFTER = Size after: |
|
280 |
MSG_COFF = Converting to AVR COFF: |
|
281 |
MSG_EXTENDED_COFF = Converting to AVR Extended COFF: |
|
282 |
MSG_FLASH = Creating load file for Flash: |
|
283 |
MSG_EEPROM = Creating load file for EEPROM: |
|
284 |
MSG_EXTENDED_LISTING = Creating Extended Listing: |
|
285 |
MSG_SYMBOL_TABLE = Creating Symbol Table: |
|
286 |
MSG_LINKING = Linking: |
|
287 |
MSG_COMPILING = Compiling: |
|
288 |
MSG_ASSEMBLING = Assembling: |
|
289 |
MSG_CLEANING = Cleaning project: |
|
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
# Define all object files. |
|
295 |
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) |
|
296 |
|
|
297 |
# Define all listing files. |
|
298 |
LST = $(SRC:.c=.lst) $(ASRC:.S=.lst) |
|
299 |
|
|
300 |
|
|
301 |
# Compiler flags to generate dependency files. |
|
302 |
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d |
|
303 |
|
|
304 |
|
|
305 |
# Combine all necessary flags and optional flags. |
|
306 |
# Add target processor to flags. |
|
307 |
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS) |
|
308 |
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) |
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
# Default target. |
|
315 |
all: begin gccversion sizebefore build sizeafter end |
|
316 |
|
|
317 |
build: elf hex eep lss sym |
|
318 |
|
|
319 |
elf: $(TARGET).elf |
|
320 |
hex: $(TARGET).hex |
|
321 |
eep: $(TARGET).eep |
|
322 |
lss: $(TARGET).lss |
|
323 |
sym: $(TARGET).sym |
|
324 |
|
|
325 |
|
|
326 |
|
|
327 |
# Eye candy. |
|
328 |
# AVR Studio 3.x does not check make's exit code but relies on |
|
329 |
# the following magic strings to be generated by the compile job. |
|
330 |
begin: |
|
331 |
@echo |
|
332 |
@echo $(MSG_BEGIN) |
|
333 |
|
|
334 |
end: |
|
335 |
@echo $(MSG_END) |
|
336 |
@echo |
|
337 |
|
|
338 |
|
|
339 |
# Display size of file. |
|
340 |
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex |
|
341 |
ELFSIZE = $(SIZE) -A $(TARGET).elf |
|
342 |
AVRMEM = avr-mem.sh $(TARGET).elf $(MCU) |
|
343 |
|
|
344 |
sizebefore: |
|
345 |
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \ |
|
346 |
$(AVRMEM) 2>/dev/null; echo; fi |
|
347 |
|
|
348 |
sizeafter: |
|
349 |
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \ |
|
350 |
$(AVRMEM) 2>/dev/null; echo; fi |
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
# Display compiler version information. |
|
355 |
gccversion : |
|
356 |
@$(CC) --version |
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
# Program the device. |
|
361 |
program: $(TARGET).hex $(TARGET).eep |
|
362 |
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) |
|
363 |
|
|
364 |
|
|
365 |
# Generate avr-gdb config/init file which does the following: |
|
366 |
# define the reset signal, load the target file, connect to target, and set |
|
367 |
# a breakpoint at main(). |
|
368 |
gdb-config: |
|
369 |
@$(REMOVE) $(GDBINIT_FILE) |
|
370 |
@echo define reset >> $(GDBINIT_FILE) |
|
371 |
@echo SIGNAL SIGHUP >> $(GDBINIT_FILE) |
|
372 |
@echo end >> $(GDBINIT_FILE) |
|
373 |
@echo file $(TARGET).elf >> $(GDBINIT_FILE) |
|
374 |
@echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE) |
|
375 |
ifeq ($(DEBUG_BACKEND),simulavr) |
|
376 |
@echo load >> $(GDBINIT_FILE) |
|
377 |
endif |
|
378 |
@echo break main >> $(GDBINIT_FILE) |
|
379 |
|
|
380 |
debug: gdb-config $(TARGET).elf |
|
381 |
ifeq ($(DEBUG_BACKEND), avarice) |
|
382 |
@echo Starting AVaRICE - Press enter when "waiting to connect" message displays. |
|
383 |
@$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \ |
|
384 |
$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT) |
|
385 |
@$(WINSHELL) /c pause |
|
386 |
|
|
387 |
else |
|
388 |
@$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \ |
|
389 |
$(DEBUG_MFREQ) --port $(DEBUG_PORT) |
|
390 |
endif |
|
391 |
@$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE) |
|
392 |
|
|
393 |
|
|
394 |
|
|
395 |
|
|
396 |
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. |
|
397 |
COFFCONVERT=$(OBJCOPY) --debugging \ |
|
398 |
--change-section-address .data-0x800000 \ |
|
399 |
--change-section-address .bss-0x800000 \ |
|
400 |
--change-section-address .noinit-0x800000 \ |
|
401 |
--change-section-address .eeprom-0x810000 |
|
402 |
|
|
403 |
|
|
404 |
coff: $(TARGET).elf |
|
405 |
@echo |
|
406 |
@echo $(MSG_COFF) $(TARGET).cof |
|
407 |
$(COFFCONVERT) -O coff-avr $< $(TARGET).cof |
|
408 |
|
|
409 |
|
|
410 |
extcoff: $(TARGET).elf |
|
411 |
@echo |
|
412 |
@echo $(MSG_EXTENDED_COFF) $(TARGET).cof |
|
413 |
$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof |
|
414 |
|
|
415 |
|
|
416 |
|
|
417 |
# Create final output files (.hex, .eep) from ELF output file. |
|
418 |
%.hex: %.elf |
|
419 |
@echo |
|
420 |
@echo $(MSG_FLASH) $@ |
|
421 |
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ |
|
422 |
|
|
423 |
%.eep: %.elf |
|
424 |
@echo |
|
425 |
@echo $(MSG_EEPROM) $@ |
|
426 |
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ |
|
427 |
--change-section-lma .eeprom=0 -O $(FORMAT) $< $@ |
|
428 |
|
|
429 |
# Create extended listing file from ELF output file. |
|
430 |
%.lss: %.elf |
|
431 |
@echo |
|
432 |
@echo $(MSG_EXTENDED_LISTING) $@ |
|
433 |
$(OBJDUMP) -h -S $< > $@ |
|
434 |
|
|
435 |
# Create a symbol table from ELF output file. |
|
436 |
%.sym: %.elf |
|
437 |
@echo |
|
438 |
@echo $(MSG_SYMBOL_TABLE) $@ |
|
439 |
$(NM) -n $< > $@ |
|
440 |
|
|
441 |
|
|
442 |
|
|
443 |
# Link: create ELF output file from object files. |
|
444 |
.SECONDARY : $(TARGET).elf |
|
445 |
.PRECIOUS : $(OBJ) |
|
446 |
%.elf: $(OBJ) |
|
447 |
@echo |
|
448 |
@echo $(MSG_LINKING) $@ |
|
449 |
$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS) |
|
450 |
|
|
451 |
|
|
452 |
# Compile: create object files from C source files. |
|
453 |
%.o : %.c |
|
454 |
@echo |
|
455 |
@echo $(MSG_COMPILING) $< |
|
456 |
$(CC) -c $(ALL_CFLAGS) $< -o $@ |
|
457 |
|
|
458 |
|
|
459 |
# Compile: create assembler files from C source files. |
|
460 |
%.s : %.c |
|
461 |
$(CC) -S $(ALL_CFLAGS) $< -o $@ |
|
462 |
|
|
463 |
|
|
464 |
# Assemble: create object files from assembler source files. |
|
465 |
%.o : %.S |
|
466 |
@echo |
|
467 |
@echo $(MSG_ASSEMBLING) $< |
|
468 |
$(CC) -c $(ALL_ASFLAGS) $< -o $@ |
|
469 |
|
|
470 |
# Create preprocessed source for use in sending a bug report. |
|
471 |
%.i : %.c |
|
472 |
$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@ |
|
473 |
|
|
474 |
|
|
475 |
# Target: clean project. |
|
476 |
clean: begin clean_list end |
|
477 |
|
|
478 |
clean_list : |
|
479 |
@echo |
|
480 |
@echo $(MSG_CLEANING) |
|
481 |
$(REMOVE) $(TARGET).hex |
|
482 |
$(REMOVE) $(TARGET).eep |
|
483 |
$(REMOVE) $(TARGET).cof |
|
484 |
$(REMOVE) $(TARGET).elf |
|
485 |
$(REMOVE) $(TARGET).map |
|
486 |
$(REMOVE) $(TARGET).sym |
|
487 |
$(REMOVE) $(TARGET).lss |
|
488 |
$(REMOVE) $(OBJ) |
|
489 |
$(REMOVE) $(LST) |
|
490 |
$(REMOVE) $(SRC:.c=.s) |
|
491 |
$(REMOVE) $(SRC:.c=.d) |
|
492 |
$(REMOVE) .dep/* |
|
493 |
|
|
494 |
|
|
495 |
|
|
496 |
# Include the dependency files. |
|
497 |
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) |
|
498 |
|
|
499 |
|
|
500 |
# Listing of phony targets. |
|
501 |
.PHONY : all begin finish end sizebefore sizeafter gccversion \ |
|
502 |
build elf hex eep lss sym coff extcoff \ |
|
503 |
clean clean_list program debug gdb-config |
|
504 |
|
trunk/bootloader/uart.h | ||
---|---|---|
1 |
/******** |
|
2 |
* This file is part of Tooltron. |
|
3 |
* |
|
4 |
* Tooltron is free software: you can redistribute it and/or modify |
|
5 |
* it under the terms of the Lesser GNU General Public License as published by |
|
6 |
* the Free Software Foundation, either version 3 of the License, or |
|
7 |
* (at your option) any later version. |
|
8 |
* |
|
9 |
* Tooltron is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* Lesser GNU General Public License for more details. |
|
13 |
* You should have received a copy of the Lesser GNU General Public License |
|
14 |
* along with Tooltron. If not, see <http://www.gnu.org/licenses/>. |
|
15 |
* |
|
16 |
* Copyright 2009 Kevin Woo <kwoo@2ndt.com> |
|
17 |
* |
|
18 |
********/ |
|
19 |
/** @file uart.h |
|
20 |
* |
|
21 |
* @brief Initializes UART functions using the UART hardware module |
|
22 |
* |
|
23 |
* @author Kevin Woo (kwoo) |
|
24 |
*/ |
|
25 |
|
|
26 |
#ifndef UART_H |
|
27 |
#define UART_H |
|
28 |
|
|
29 |
#include <avr/io.h> |
|
30 |
#include <avr/interrupt.h> |
|
31 |
#include <stdint.h> |
|
32 |
/** **/ |
|
33 |
#define UART_TX_OFF 0 |
|
34 |
#define UART_TX_ON 1 |
|
35 |
|
|
36 |
/** @brief RX Pin for the UART **/ |
|
37 |
#define RX _BV(PORTD0) |
|
38 |
#define TX _BV(PORTD1) |
|
39 |
#define TX_EN _BV(PORTD5) |
|
40 |
|
|
41 |
/** @brief The most recently received byte **/ |
|
42 |
extern uint8_t received_byte; |
|
43 |
/** @brief If the value in received_byte has been read or not **/ |
|
44 |
extern uint8_t byte_ready; |
|
45 |
|
|
46 |
/** @brief Initializes the UART registers and sets it to the buad rate |
|
47 |
* which msut be the value that is defined in the datasheet |
|
48 |
* for any particular speed (ie: 51 -> 9600bps) |
|
49 |
**/ |
|
50 |
void init_uart(uint16_t baud); |
|
51 |
/** @brief Gets latest byte and returns it in output_byte. If the byte |
|
52 |
* was already read, returns -1 otherwise it returns 0 |
|
53 |
**/ |
|
54 |
int8_t uart_get_byte(uint8_t *output_byte); |
|
55 |
/** @brief Sends a character array of size size. If we are currently |
|
56 |
* transmitting it will block until the the current transmit |
|
57 |
* is done. |
|
58 |
**/ |
|
59 |
void uart_send_byte(uint8_t data); |
|
60 |
void uart_toggle_transmit(uint8_t state); |
|
61 |
#endif |
trunk/bootloader/bootloader.c | ||
---|---|---|
1 |
#include <avr/io.h> |
|
2 |
#include <avr/boot.h> |
|
3 |
#include <avr/pgmspace.h> |
|
4 |
#include <avr/wdt.h> |
|
5 |
|
|
6 |
#include "tooltron.h" |
|
7 |
#include "uart.h" |
|
8 |
|
|
9 |
#define TRUE 0 |
|
10 |
#define FALSE 1 |
|
11 |
|
|
12 |
#define ADDR 18 |
|
13 |
|
|
14 |
|
|
15 |
// Error thresholds |
|
16 |
#define MAX_TIMEOUT 60000 // Seconds to wait before exiting bootloader mode |
|
17 |
#define MAX_RETRIES 5 // Number of times to retry before giving up |
|
18 |
|
|
19 |
// Memory locations |
|
20 |
#define MAIN_ADDR 0x000 // Where the user code starts |
|
21 |
#define BOOT_START 0x400 // Where the bootloader starts |
|
22 |
|
|
23 |
//Status LED |
|
24 |
#define LED_DDR DDRB |
|
25 |
#define LED_PORT PORTB |
|
26 |
#define LED PORTB1 |
|
27 |
|
|
28 |
//Function prototypes |
|
29 |
void (*main_start)(void) = BOOT_START/2 - 1; |
|
30 |
|
|
31 |
// Packet handler states |
|
32 |
typedef enum { |
|
33 |
sd, |
|
34 |
src, |
|
35 |
dest, |
|
36 |
comd, |
|
37 |
read, |
|
38 |
cs, |
|
39 |
ack |
|
40 |
} state_t; |
|
41 |
|
|
42 |
typedef union { |
|
43 |
uint8_t bytes[2]; |
|
44 |
int16_t sword; |
|
45 |
} rjump_t; |
|
46 |
|
|
47 |
void init_uart(uint16_t baud) { |
|
48 |
// Set baud rate |
|
49 |
UBRRH = (uint8_t)(baud>>8); |
|
50 |
UBRRL = (uint8_t)baud; |
|
51 |
|
|
52 |
// Enable RX/TX |
|
53 |
UCSRB = _BV(RXEN) | _BV(TXEN); |
|
54 |
|
|
55 |
// Enable the TXEN pin as output |
|
56 |
DDRD |= TX_EN; |
|
57 |
uart_toggle_transmit(UART_TX_OFF); |
|
58 |
} |
|
59 |
|
|
60 |
int8_t uart_get_byte(uint8_t *output_byte) { |
|
61 |
if (UCSRA & _BV(RXC)) { |
|
62 |
*output_byte = UDR; |
|
63 |
return 0; |
|
64 |
} else { |
|
65 |
return -1; |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
void uart_send_byte(uint8_t data) { |
|
70 |
//Waits until current transmit is done |
|
71 |
while (!(UCSRA & _BV(UDRE))); |
|
72 |
|
|
73 |
// Enable writes and send |
|
74 |
uart_toggle_transmit(UART_TX_ON); |
|
75 |
UDR = data; |
|
76 |
|
|
77 |
// Waits until the transmit is done |
|
78 |
while(!(UCSRA & _BV(TXC))); |
|
79 |
uart_toggle_transmit(UART_TX_OFF); |
|
80 |
UCSRA |= _BV(TXC); |
|
81 |
|
|
82 |
return; |
|
83 |
} |
|
84 |
|
|
85 |
void uart_toggle_transmit(uint8_t state) { |
|
86 |
if (state == UART_TX_ON) { |
|
87 |
PORTD |= TX_EN; |
|
88 |
} else { |
|
89 |
PORTD &= ~TX_EN; |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
char parse_packet(uint8_t *mbuf) { |
|
94 |
uint8_t r; // Byte from the network |
|
95 |
uint8_t crc; // Running checksum of the packet |
|
96 |
uint8_t cmd; // The command received |
|
97 |
uint8_t pos; // Position in the message buffer |
|
98 |
uint8_t lim; // Max number of bytes to read into the message buf |
|
99 |
state_t state; // State machine |
|
100 |
uint16_t count; |
|
101 |
|
|
102 |
r = 0; |
|
103 |
crc = 0; |
|
104 |
cmd = 0; |
|
105 |
pos = 0; |
|
106 |
lim = 0; |
|
107 |
state = sd; |
|
108 |
count = 0; |
|
109 |
|
|
110 |
while (1) { |
|
111 |
// Wait for the next byte |
|
112 |
while ((uart_get_byte(&r)) < 0) { |
|
113 |
if (count >= MAX_TIMEOUT) { |
|
114 |
return TT_BAD; |
|
115 |
} else { |
|
116 |
count++; |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
switch (state) { |
|
121 |
case sd: |
|
122 |
if (r == DELIM) { |
|
123 |
state = src; |
|
124 |
} |
|
125 |
break; |
|
126 |
|
|
127 |
case src: |
|
128 |
if (r == DELIM) { |
|
129 |
state = src; |
|
130 |
} else { |
|
131 |
crc = r; |
|
132 |
state = dest; |
|
133 |
} |
|
134 |
break; |
|
135 |
|
|
136 |
case dest: |
|
137 |
if (r == DELIM) { |
|
138 |
state = src; |
|
139 |
} else if (r == ADDR) { |
|
140 |
crc ^= r; |
|
141 |
state = comd; |
|
142 |
} else { |
|
143 |
state = sd; |
|
144 |
} |
|
145 |
break; |
|
146 |
|
|
147 |
case comd: |
|
148 |
cmd = r; |
|
149 |
crc ^= r; |
|
150 |
|
|
151 |
if (r == DELIM) { |
|
152 |
state = src; |
|
153 |
} else if (r == TT_PROGM) { |
|
154 |
lim = PROGM_PACKET_SIZE; |
|
155 |
state = read; |
|
156 |
} else if (r == TT_PROGD) { |
|
157 |
lim = PROGD_PACKET_SIZE; |
|
158 |
state = read; |
|
159 |
} else { |
|
160 |
state = cs; |
|
161 |
} |
|
162 |
break; |
|
163 |
|
|
164 |
case read: |
|
165 |
mbuf[pos] = r; |
|
166 |
crc ^= r; |
|
167 |
pos++; |
|
168 |
|
|
169 |
if (pos == lim) { |
|
170 |
state = cs; |
|
171 |
} |
|
172 |
|
|
173 |
break; |
|
174 |
|
|
175 |
case cs: |
|
176 |
if (r == crc) { |
|
177 |
return cmd; |
|
178 |
} else { |
|
179 |
return TT_BAD; |
|
180 |
} |
|
181 |
|
|
182 |
break; |
|
183 |
|
|
184 |
default: |
|
185 |
return TT_BAD; |
|
186 |
} |
|
187 |
} |
|
188 |
} |
|
189 |
|
|
190 |
void send_packet(uint8_t cmd) { |
|
191 |
uart_send_byte(DELIM); |
|
192 |
uart_send_byte(ADDR); |
|
193 |
uart_send_byte(SERVER); |
|
194 |
uart_send_byte(cmd); |
|
195 |
uart_send_byte(ACK_CRC ^ cmd); |
|
196 |
} |
|
197 |
|
|
198 |
// SPM_PAGESIZE is set to 32 bytes |
|
199 |
void onboard_program_write(uint16_t page, uint8_t *buf) { |
|
200 |
uint16_t i; |
|
201 |
|
|
202 |
boot_page_erase (page); |
|
203 |
boot_spm_busy_wait (); // Wait until the memory is erased. |
|
204 |
|
|
205 |
for (i=0; i < SPM_PAGESIZE; i+=2){ |
|
206 |
// Set up little-endian word. |
|
207 |
boot_page_fill (page + i, buf[i] | (buf[i+1] <<8)); |
|
208 |
} |
|
209 |
|
|
210 |
boot_page_write (page); // Store buffer in flash page. |
|
211 |
boot_spm_busy_wait(); // Wait until the memory is written. |
|
212 |
} |
|
213 |
|
|
214 |
int main(void) { |
|
215 |
uint8_t mbuf[PROGD_PACKET_SIZE]; |
|
216 |
rjump_t jbuf; |
|
217 |
uint16_t caddr = MAIN_ADDR; |
|
218 |
uint8_t iteration; |
|
219 |
uint8_t resp; |
|
220 |
uint16_t prog_len; |
|
221 |
uint8_t i; |
|
222 |
uint8_t retries; |
|
223 |
|
|
224 |
retry_jpnt: |
|
225 |
iteration = 0; |
|
226 |
retries = 0; |
|
227 |
|
|
228 |
// Clear the watchdog timer |
|
229 |
MCUSR &= ~_BV(WDRF); |
|
230 |
wdt_disable(); |
|
231 |
WDTCSR = 0; |
|
232 |
|
|
233 |
|
|
234 |
init_uart(51); //MAGIC NUMBER?? |
|
235 |
|
|
236 |
//set LED pin as output |
|
237 |
LED_DDR |= 0x07; |
|
238 |
PORTB = 0x07; |
|
239 |
|
|
240 |
//Start bootloading process |
|
241 |
send_packet(TT_BOOT); |
|
242 |
|
|
243 |
resp = parse_packet(mbuf); |
|
244 |
|
|
245 |
// Enter programming mode |
|
246 |
if (resp == TT_PROGM) { |
|
247 |
prog_len = mbuf[0]; |
|
248 |
prog_len |= mbuf[1] << 8; |
|
249 |
|
|
250 |
// This will insert a NOP into the user code jump in case |
|
251 |
// the programming fails |
|
252 |
for (i = 0; i < PROGD_PACKET_SIZE; i++) { |
|
253 |
mbuf[i]= 0; |
|
254 |
} |
|
255 |
onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf); |
|
256 |
|
|
257 |
// Run user code |
|
258 |
} else { |
|
259 |
main_start(); |
|
260 |
} |
|
261 |
|
|
262 |
send_packet(TT_ACK); |
|
263 |
|
|
264 |
while(1) { |
|
265 |
resp = parse_packet(mbuf); |
|
266 |
|
|
267 |
if (resp == TT_PROGD) { |
|
268 |
// We need to muck with the reset vector jump in the first page |
|
269 |
if (iteration == 0) { |
|
270 |
// Store the jump to user code |
|
271 |
jbuf.bytes[0] = mbuf[0]; |
|
272 |
jbuf.bytes[1] = mbuf[1]; |
|
273 |
|
|
274 |
// Rewrite the user code jump to be correct since we are |
|
275 |
// using relative jumps (rjmp) |
|
276 |
jbuf.sword &= 0x0FFF; |
|
277 |
jbuf.sword -= (BOOT_START >> 1) - 1; |
|
278 |
jbuf.sword &= 0x0FFF; |
|
279 |
jbuf.sword |= 0xC000; |
|
280 |
|
|
281 |
// Rewrite the reset vector to jump to the bootloader |
|
282 |
mbuf[0] = (BOOT_START/2 - 1) & 0xFF; |
|
283 |
mbuf[1] = 0xC0 | (((BOOT_START/2 - 1) >> 8) & 0x0F); |
|
284 |
|
|
285 |
iteration = 1; |
|
286 |
} |
|
287 |
|
|
288 |
// Write the page to the flash |
|
289 |
onboard_program_write(caddr, mbuf); |
|
290 |
caddr += PROGD_PACKET_SIZE; |
|
291 |
retries = 0; |
|
292 |
} else { |
|
293 |
send_packet(TT_NACK); |
|
294 |
retries++; |
|
295 |
|
|
296 |
// If we failed too many times, reset. This goes to the start |
|
297 |
// of the bootloader function |
|
298 |
if (retries > MAX_RETRIES) { |
|
299 |
goto retry_jpnt; |
|
300 |
} |
|
301 |
} |
|
302 |
|
|
303 |
send_packet(TT_ACK); |
|
304 |
|
|
305 |
// Once we write the last packet we must override the jump to |
|
306 |
// user code to point to the correct address |
|
307 |
if (prog_len <= PROGD_PACKET_SIZE) { |
|
308 |
for (i = 0; i < PROGD_PACKET_SIZE; i++) { |
|
309 |
mbuf[i]= 0; |
|
310 |
} |
|
311 |
|
|
312 |
mbuf[PROGD_PACKET_SIZE-2] = jbuf.bytes[0]; |
|
313 |
mbuf[PROGD_PACKET_SIZE-1] = jbuf.bytes[1]; |
|
314 |
|
|
315 |
onboard_program_write(BOOT_START - SPM_PAGESIZE, mbuf); |
|
316 |
|
|
317 |
main_start(); |
|
318 |
} else { |
|
319 |
prog_len -= PROGD_PACKET_SIZE; |
|
320 |
} |
|
321 |
} |
|
322 |
|
|
323 |
// Should never get here |
|
324 |
return -1; |
|
325 |
} |
trunk/bootloader/Makefile | ||
---|---|---|
1 |
# Hey Emacs, this is a -*- makefile -*- |
|
2 |
#---------------------------------------------------------------------------- |
|
3 |
# WinAVR Makefile Template written by Eric B. Weddington, J?rg Wunsch, et al. |
|
4 |
# |
|
5 |
# Released to the Public Domain |
|
6 |
# |
|
7 |
# Additional material for this makefile was written by: |
|
8 |
# Peter Fleury |
|
9 |
# Tim Henigan |
|
10 |
# Colin O'Flynn |
|
11 |
# Reiner Patommel |
|
12 |
# Markus Pfaff |
|
13 |
# Sander Pool |
|
14 |
# Frederik Rouleau |
|
15 |
# |
|
16 |
#---------------------------------------------------------------------------- |
|
17 |
# On command line: |
|
18 |
# |
|
19 |
# make all = Make software. |
|
20 |
# |
|
21 |
# make clean = Clean out built project files. |
|
22 |
# |
|
23 |
# make coff = Convert ELF to AVR COFF. |
|
24 |
# |
|
25 |
# make extcoff = Convert ELF to AVR Extended COFF. |
|
26 |
# |
|
27 |
# make program = Download the hex file to the device, using avrdude. |
|
28 |
# Please customize the avrdude settings below first! |
|
29 |
# |
|
30 |
# make debug = Start either simulavr or avarice as specified for debugging, |
|
31 |
# with avr-gdb or avr-insight as the front end for debugging. |
|
32 |
# |
|
33 |
# make filename.s = Just compile filename.c into the assembler code only. |
|
34 |
# |
|
35 |
# make filename.i = Create a preprocessed source file for use in submitting |
|
36 |
# bug reports to the GCC project. |
|
37 |
# |
|
38 |
# To rebuild project do "make clean" then "make all". |
|
39 |
#---------------------------------------------------------------------------- |
|
40 |
|
|
41 |
|
|
42 |
# MCU name |
|
43 |
MCU = attiny2313 |
|
44 |
|
|
45 |
# Processor frequency. |
|
46 |
# This will define a symbol, F_CPU, in all source code files equal to the |
|
47 |
# processor frequency. You can then use this symbol in your source code to |
|
48 |
# calculate timings. Do NOT tack on a 'UL' at the end, this will be done |
|
49 |
# automatically to create a 32-bit value in your source code. |
|
50 |
F_CPU = 8000000 |
|
51 |
|
|
52 |
|
|
53 |
# Output format. (can be srec, ihex, binary) |
|
54 |
FORMAT = ihex |
|
55 |
|
|
56 |
# Target file name (without extension). |
|
57 |
TARGET = bootloader |
|
58 |
|
|
59 |
# List C source files here. (C dependencies are automatically generated.) |
|
60 |
SRC = $(wildcard *.c) |
|
61 |
|
|
62 |
# List Assembler source files here. |
|
63 |
# Make them always end in a capital .S. Files ending in a lowercase .s |
|
64 |
# will not be considered source files but generated files (assembler |
|
65 |
# output from the compiler), and will be deleted upon "make clean"! |
|
66 |
# Even though the DOS/Win* filesystem matches both .s and .S the same, |
|
67 |
# it will preserve the spelling of the filenames, and gcc itself does |
|
68 |
# care about how the name is spelled on its command-line. |
|
69 |
ASRC = |
|
70 |
|
|
71 |
|
|
72 |
# Optimization level, can be [0, 1, 2, 3, s]. |
|
73 |
# 0 = turn off optimization. s = optimize for size. |
|
74 |
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.) |
|
75 |
OPT = s |
|
76 |
|
|
77 |
|
|
78 |
# Debugging format. |
|
79 |
# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs. |
|
80 |
# AVR Studio 4.10 requires dwarf-2. |
|
81 |
# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run. |
|
82 |
DEBUG = |
|
83 |
|
|
84 |
|
|
85 |
# List any extra directories to look for include files here. |
|
86 |
# Each directory must be seperated by a space. |
|
87 |
# Use forward slashes for directory separators. |
|
88 |
# For a directory that has spaces, enclose it in quotes. |
|
89 |
#EXTRAINCDIRS = C:\WinAVR\include\fwr |
|
90 |
|
|
91 |
|
|
92 |
# Compiler flag to set the C Standard level. |
|
93 |
# c89 = "ANSI" C |
|
94 |
# gnu89 = c89 plus GCC extensions |
|
95 |
# c99 = ISO C99 standard (not yet fully implemented) |
|
96 |
# gnu99 = c99 plus GCC extensions |
|
97 |
CSTANDARD = -std=gnu99 |
|
98 |
|
|
99 |
|
|
100 |
# Place -D or -U options here |
|
101 |
CDEFS = -DF_CPU=$(F_CPU)UL |
|
102 |
|
|
103 |
|
|
104 |
# Place -I options here |
|
105 |
CINCS = |
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
#---------------- Compiler Options ---------------- |
|
110 |
# -g*: generate debugging information |
|
111 |
# -O*: optimization level |
|
112 |
# -f...: tuning, see GCC manual and avr-libc documentation |
|
113 |
# -Wall...: warning level |
|
114 |
# -Wa,...: tell GCC to pass this to the assembler. |
|
115 |
# -adhlns...: create assembler listing |
|
116 |
#CFLAGS = -g$(DEBUG) |
|
117 |
CFLAGS += $(CDEFS) $(CINCS) |
|
118 |
CFLAGS += -O$(OPT) |
|
119 |
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums |
|
120 |
CFLAGS += -Wall -Wstrict-prototypes |
|
121 |
CFLAGS += -Wa,-adhlns=$(<:.c=.lst) |
|
122 |
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) |
|
123 |
CFLAGS += $(CSTANDARD) |
|
124 |
|
|
125 |
|
|
126 |
#---------------- Assembler Options ---------------- |
|
127 |
# -Wa,...: tell GCC to pass this to the assembler. |
|
128 |
# -ahlms: create listing |
|
129 |
# -gstabs: have the assembler create line number information; note that |
|
130 |
# for use in COFF files, additional information about filenames |
|
131 |
# and function names needs to be present in the assembler source |
|
132 |
# files -- see avr-libc docs [FIXME: not yet described there] |
|
133 |
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs |
|
134 |
|
|
135 |
|
|
136 |
#---------------- Library Options ---------------- |
|
137 |
# Minimalistic printf version |
|
138 |
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min |
|
139 |
|
|
140 |
# Floating point printf version (requires MATH_LIB = -lm below) |
|
141 |
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt |
|
142 |
|
|
143 |
# If this is left blank, then it will use the Standard printf version. |
|
144 |
PRINTF_LIB = |
|
145 |
#PRINTF_LIB = $(PRINTF_LIB_MIN) |
|
146 |
#PRINTF_LIB = $(PRINTF_LIB_FLOAT) |
|
147 |
|
|
148 |
|
|
149 |
# Minimalistic scanf version |
|
150 |
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min |
|
151 |
|
|
152 |
# Floating point + %[ scanf version (requires MATH_LIB = -lm below) |
|
153 |
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt |
|
154 |
|
|
155 |
# If this is left blank, then it will use the Standard scanf version. |
|
156 |
SCANF_LIB = |
|
157 |
#SCANF_LIB = $(SCANF_LIB_MIN) |
|
158 |
#SCANF_LIB = $(SCANF_LIB_FLOAT) |
|
159 |
|
|
160 |
|
|
161 |
MATH_LIB = -lm |
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
#---------------- External Memory Options ---------------- |
|
166 |
|
|
167 |
# 64 KB of external RAM, starting after internal RAM (ATmega128!), |
|
168 |
# used for variables (.data/.bss) and heap (malloc()). |
|
169 |
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff |
|
170 |
|
|
171 |
# 64 KB of external RAM, starting after internal RAM (ATmega128!), |
|
172 |
# only used for heap (malloc()). |
|
173 |
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff |
|
174 |
|
|
175 |
EXTMEMOPTS = |
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
#---------------- Linker Options ---------------- |
|
180 |
# -Wl,...: tell GCC to pass this to linker. |
|
181 |
# -Map: create map file |
|
182 |
# --cref: add cross reference to map file |
|
183 |
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref,--section-start=.text=0x400 |
|
184 |
LDFLAGS += $(EXTMEMOPTS) |
|
185 |
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB) |
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
#---------------- Programming Options (avrdude) ---------------- |
|
190 |
|
|
191 |
# Programming hardware: alf avr910 avrisp bascom bsd |
|
192 |
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500 |
|
193 |
# |
|
194 |
# Type: avrdude -c ? |
|
195 |
# to get a full listing. |
Also available in: Unified diff