1
/**********************************************************/
2
/* Serial Bootloader for Atmel megaAVR Controllers */
4
/* tested with ATmega8, ATmega128 and ATmega168 */
5
/* should work with other mega's, see code for details */
9
/* 20070626: hacked for Arduino Diecimila (which auto- */
10
/* resets when a USB connection is made to it) */
12
/* 20060802: hacked for Arduino by D. Cuartielles */
13
/* based on a previous hack by D. Mellis */
14
/* and D. Cuartielles */
16
/* Monitor and debug functions were added to the original */
17
/* code by Dr. Erik Lins, chip45.com. (See below) */
19
/* Thanks to Karl Pitrich for fixing a bootloader pin */
20
/* problem and more informative LED blinking! */
22
/* For the latest version see: */
23
/* http://www.chip45.com/ */
25
/* ------------------------------------------------------ */
27
/* based on stk500boot.c */
28
/* Copyright (c) 2003, Jason P. Kyle */
29
/* All rights reserved. */
30
/* see avr1.org for original file and information */
32
/* This program is free software; you can redistribute it */
33
/* and/or modify it under the terms of the GNU General */
34
/* Public License as published by the Free Software */
35
/* Foundation; either version 2 of the License, or */
36
/* (at your option) any later version. */
38
/* This program is distributed in the hope that it will */
39
/* be useful, but WITHOUT ANY WARRANTY; without even the */
40
/* implied warranty of MERCHANTABILITY or FITNESS FOR A */
41
/* PARTICULAR PURPOSE. See the GNU General Public */
42
/* License for more details. */
44
/* You should have received a copy of the GNU General */
45
/* Public License along with this program; if not, write */
46
/* to the Free Software Foundation, Inc., */
47
/* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
49
/* Licence can be viewed at */
50
/* http://www.fsf.org/licenses/gpl.txt */
52
/* Target = Atmel AVR m128,m64,m32,m16,m8,m162,m163,m169, */
53
/* m8515,m8535. ATmega161 has a very small boot block so */
54
/* isn't supported. */
56
/* Tested with m168 */
57
/**********************************************************/
65
#include <avr/pgmspace.h>
66
#include <avr/interrupt.h>
70
/* the current avr-libc eeprom functions do not support the ATmega168 */
71
/* own eeprom write/read functions are used instead */
72
#ifndef __AVR_ATmega168__
73
#include <avr/eeprom.h>
76
/* Use the F_CPU defined in Makefile */
78
/* 20060803: hacked by DojoCorp */
79
/* 20070626: hacked by David A. Mellis to decrease waiting time for auto-reset */
80
/* set the waiting time for the bootloader */
81
/* get this from the Makefile instead */
82
/* #define MAX_TIME_COUNT (F_CPU>>4) */
84
/* 20070707: hacked by David A. Mellis - after this many errors give up and launch application */
85
#define MAX_ERROR_COUNT 5
87
/* set the UART baud rate */
88
/* 20060803: hacked by DojoCorp */
89
//#define BAUD_RATE 115200
90
#define BAUD_RATE 19200
93
/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
94
/* never allow AVR Studio to do an update !!!! */
100
/* Adjust to suit whatever pin your hardware uses to enter the bootloader */
101
/* ATmega128 has two UARTS so two pins are used to enter bootloader and select UART */
102
/* BL0... means UART0, BL1... means UART1 */
103
#ifdef __AVR_ATmega128__
105
#define BL_PORT PORTF
110
/* other ATmegas have only one UART, so only one pin is defined to enter bootloader */
112
#define BL_PORT PORTD
118
/* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */
119
/* if monitor functions are included, LED goes on after monitor was entered */
120
#ifdef __AVR_ATmega128__
121
/* Onboard LED is connected to pin PB7 (e.g. Crumb128, PROBOmega128, Savvy128) */
123
#define LED_PORT PORTB
127
/* Onboard LED is connected to pin PB2 (e.g. Crumb8, Crumb168) */
129
#define LED_PORT PORTB
131
/* 20060803: hacked by DojoCorp, LED pin is B5 in Arduino */
132
/* #define LED PINB2 */
137
/* monitor functions will only be compiled when using ATmega128, due to bootblock size constraints */
138
#ifdef __AVR_ATmega128__
143
/* define various device id's */
144
/* manufacturer byte is always the same */
145
#define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
147
#if defined __AVR_ATmega128__
150
#define PAGE_SIZE 0x80U //128 words
152
#elif defined __AVR_ATmega64__
155
#define PAGE_SIZE 0x80U //128 words
157
#elif defined __AVR_ATmega32__
160
#define PAGE_SIZE 0x40U //64 words
162
#elif defined __AVR_ATmega16__
165
#define PAGE_SIZE 0x40U //64 words
167
#elif defined __AVR_ATmega8__
170
#define PAGE_SIZE 0x20U //32 words
172
#elif defined __AVR_ATmega88__
175
#define PAGE_SIZE 0x20U //32 words
177
#elif defined __AVR_ATmega168__
180
#define PAGE_SIZE 0x40U //64 words
182
#elif defined __AVR_ATmega162__
185
#define PAGE_SIZE 0x40U //64 words
187
#elif defined __AVR_ATmega163__
190
#define PAGE_SIZE 0x40U //64 words
192
#elif defined __AVR_ATmega169__
195
#define PAGE_SIZE 0x40U //64 words
197
#elif defined __AVR_ATmega8515__
200
#define PAGE_SIZE 0x20U //32 words
202
#elif defined __AVR_ATmega8535__
205
#define PAGE_SIZE 0x20U //32 words
209
/* function prototypes */
212
void getNch(uint8_t);
213
void byte_response(uint8_t);
214
void nothing_response(void);
217
void flash_led(uint8_t);
220
union address_union {
230
struct flags_struct {
236
uint8_t address_high;
241
uint8_t bootuart = 0;
243
uint8_t error_count = 0;
245
void (*app_start)(void) = 0x0000;
248
/* main program starts here */
254
asm volatile("nop\n\t");
256
/* set pin direction for bootloader pin and enable pullup */
257
/* for ATmega128, two pins need to be initialized */
258
#ifdef __AVR_ATmega128__
264
/* We run the bootloader regardless of the state of this pin. Thus, don't
265
put it in a different state than the other pins. --DAM, 070709
272
#ifdef __AVR_ATmega128__
273
/* check which UART should be used for booting */
274
if(bit_is_clear(BL_PIN, BL0)) {
277
else if(bit_is_clear(BL_PIN, BL1)) {
282
/* check if flash is programmed already, if not start bootloader anyway */
283
if(pgm_read_byte_near(0x0000) != 0xFF) {
285
#ifdef __AVR_ATmega128__
286
/* no UART was selected, start application */
291
/* check if bootloader pin is set low */
292
/* we don't start this part neither for the m8, nor m168 */
293
//if(bit_is_set(BL_PIN, BL)) {
299
#ifdef __AVR_ATmega128__
300
/* no bootuart was selected, default to uart 0 */
307
/* initialize UART(s) depending on CPU defined */
308
#ifdef __AVR_ATmega128__
310
UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
311
UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
314
UCSR0B = _BV(TXEN0)|_BV(RXEN0);
317
UBRR1L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
318
UBRR1H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
321
UCSR1B = _BV(TXEN1)|_BV(RXEN1);
323
#elif defined __AVR_ATmega163__
324
UBRR = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
325
UBRRHI = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
327
UCSRB = _BV(TXEN)|_BV(RXEN);
328
#elif defined __AVR_ATmega168__
329
UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
330
UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
331
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
332
UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
334
/* Enable internal pull-up resistor on pin D0 (RX), in order
335
to supress line noise that prevents the bootloader from
336
timing out (DAM: 20070509) */
339
#elif defined __AVR_ATmega8__
341
UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
342
UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
343
UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & Tx
344
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
346
/* m16,m32,m169,m8515,m8535 */
347
UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
348
UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
351
UCSRB = _BV(TXEN)|_BV(RXEN);
354
/* set LED pin as output */
358
/* flash onboard LED to signal entering of bootloader */
359
#ifdef __AVR_ATmega128__
360
// 4x for UART0, 5x for UART1
361
flash_led(NUM_LED_FLASHES + bootuart);
363
flash_led(NUM_LED_FLASHES);
366
/* 20050803: by DojoCorp, this is one of the parts provoking the
367
system to stop listening, cancelled from the original */
374
/* get character from UART */
377
/* A bunch of if...else if... gives smaller code than switch...case ! */
379
/* Hello is anyone home ? */
385
/* Request programmer ID */
386
/* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
387
/* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
389
if (getch() == ' ') {
400
if (++error_count == MAX_ERROR_COUNT)
406
/* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
409
if (ch2>0x85) getch();
414
/* AVR ISP/STK500 board requests */
417
if(ch2==0x80) byte_response(HW_VER); // Hardware version
418
else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
419
else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
420
else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
421
else byte_response(0x00); // Covers various unnecessary responses we don't care about
425
/* Device Parameters DON'T CARE, DEVICE IS FIXED */
432
/* Parallel programming stuff DON'T CARE */
439
/* Enter programming mode */
445
/* Leave programming mode */
451
/* Erase device, don't care as we will erase one page at a time anyway. */
457
/* Set address, little endian. EEPROM in bytes, FLASH in words */
458
/* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
459
/* This might explain why little endian was used here, big endian used everywhere else. */
461
address.byte[0] = getch();
462
address.byte[1] = getch();
467
/* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
474
/* Write memory, length is big endian and is in bytes */
476
length.byte[1] = getch();
477
length.byte[0] = getch();
479
if (getch() == 'E') flags.eeprom = 1;
480
for (w=0;w<length.word;w++) {
481
buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
483
if (getch() == ' ') {
484
if (flags.eeprom) { //Write to EEPROM one byte at a time
485
for(w=0;w<length.word;w++) {
486
#ifdef __AVR_ATmega168__
487
while(EECR & (1<<EEPE));
488
EEAR = (uint16_t)(void *)address.word;
493
eeprom_write_byte((void *)address.word,buff[w]);
498
else { //Write to FLASH one page at a time
499
if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
500
else address_high = 0x00;
501
#ifdef __AVR_ATmega128__
502
RAMPZ = address_high;
504
address.word = address.word << 1; //address * 2 -> byte location
505
/* if ((length.byte[0] & 0x01) == 0x01) length.word++; //Even up an odd number of bytes */
506
if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
507
cli(); //Disable interrupts, just to be sure
508
// HACKME: EEPE used to be EEWE
509
while(bit_is_set(EECR,EEPE)); //Wait for previous EEPROM writes to complete
511
"clr r17 \n\t" //page_word_count
512
"lds r30,address \n\t" //Address of FLASH location (in bytes)
513
"lds r31,address+1 \n\t"
514
"ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
515
"ldi r29,hi8(buff) \n\t"
516
"lds r24,length \n\t" //Length of data to be written (in bytes)
517
"lds r25,length+1 \n\t"
518
"length_loop: \n\t" //Main loop, repeat for number of words in block
519
"cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
520
"brne no_page_erase \n\t"
522
"lds r16,%0 \n\t" //Wait for previous spm to complete
525
"breq wait_spm1 \n\t"
526
"ldi r16,0x03 \n\t" //Erase page pointed to by Z
529
#ifdef __AVR_ATmega163__
534
"lds r16,%0 \n\t" //Wait for previous spm to complete
537
"breq wait_spm2 \n\t"
539
"ldi r16,0x11 \n\t" //Re-enable RWW section
542
#ifdef __AVR_ATmega163__
546
"no_page_erase: \n\t"
547
"ld r0,Y+ \n\t" //Write 2 bytes into page buffer
551
"lds r16,%0 \n\t" //Wait for previous spm to complete
554
"breq wait_spm3 \n\t"
555
"ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
559
"inc r17 \n\t" //page_word_count++
561
"brlo same_page \n\t" //Still same page in FLASH
563
"clr r17 \n\t" //New page, write current one first
565
"lds r16,%0 \n\t" //Wait for previous spm to complete
568
"breq wait_spm4 \n\t"
569
#ifdef __AVR_ATmega163__
570
"andi r30,0x80 \n\t" // m163 requires Z6:Z1 to be zero during page write
572
"ldi r16,0x05 \n\t" //Write page pointed to by Z
575
#ifdef __AVR_ATmega163__
578
"ori r30,0x7E \n\t" // recover Z6:Z1 state after page write (had to be zero during write)
581
"lds r16,%0 \n\t" //Wait for previous spm to complete
584
"breq wait_spm5 \n\t"
585
"ldi r16,0x11 \n\t" //Re-enable RWW section
588
#ifdef __AVR_ATmega163__
593
"adiw r30,2 \n\t" //Next word in FLASH
594
"sbiw r24,2 \n\t" //length-2
595
"breq final_write \n\t" //Finished
596
"rjmp length_loop \n\t"
599
"breq block_done \n\t"
600
"adiw r24,2 \n\t" //length+2, fool above check on length after short page write
601
"rjmp write_page \n\t"
603
"clr __zero_reg__ \n\t" //restore zero register
604
#if defined __AVR_ATmega168__
605
: "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
607
: "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
610
/* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
611
/* exit the bootloader without a power cycle anyhow */
616
if (++error_count == MAX_ERROR_COUNT)
622
/* Read memory block mode, length is big endian. */
624
length.byte[1] = getch();
625
length.byte[0] = getch();
626
#if defined __AVR_ATmega128__
627
if (address.word>0x7FFF) flags.rampz = 1; // No go with m256, FIXME
628
else flags.rampz = 0;
630
if (getch() == 'E') flags.eeprom = 1;
633
address.word = address.word << 1; // address * 2 -> byte location
635
if (getch() == ' ') { // Command terminator
637
for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
638
if (flags.eeprom) { // Byte access EEPROM read
639
#ifdef __AVR_ATmega168__
640
while(EECR & (1<<EEPE));
641
EEAR = (uint16_t)(void *)address.word;
645
putch(eeprom_read_byte((void *)address.word));
651
if (!flags.rampz) putch(pgm_read_byte_near(address.word));
652
#if defined __AVR_ATmega128__
653
else putch(pgm_read_byte_far(address.word + 0x10000));
654
// Hmmmm, yuck FIXME when m256 arrvies
664
/* Get device signature bytes */
666
if (getch() == ' ') {
673
if (++error_count == MAX_ERROR_COUNT)
679
/* Read oscillator calibration byte */
687
/* here come the extended monitor commands by Erik Lins */
689
/* check for three times exclamation mark pressed */
696
#ifdef __AVR_ATmega128__
699
uint8_t addrl, addrh;
702
PGM_P welcome = {"ATmegaBOOT / Crumb128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
703
#elif defined PROBOMEGA128
704
PGM_P welcome = {"ATmegaBOOT / PROBOmega128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
705
#elif defined SAVVY128
706
PGM_P welcome = {"ATmegaBOOT / Savvy128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
711
LED_PORT &= ~_BV(LED);
713
/* print a welcome message and command overview */
714
for(i=0; welcome[i] != '\0'; ++i) {
718
/* test for valid commands */
730
if(bit_is_set(LED_PIN,LED)) {
731
LED_PORT &= ~_BV(LED);
734
LED_PORT |= _BV(LED);
740
/* read byte from address */
742
ch = getch(); putch(ch);
746
ch = *(uint8_t *)((addrh << 8) + addrl);
750
/* write a byte to address */
752
ch = getch(); putch(ch);
755
ch = getch(); putch(ch);
757
*(uint8_t *)((addrh << 8) + addrl) = ch;
761
/* read from uart and echo back */
767
#ifdef __AVR_ATmega128__
768
/* external bus loop */
778
ch = *(volatile uint8_t *)extaddr;
791
/* end of monitor functions */
798
else if (++error_count == MAX_ERROR_COUNT) {
802
/* end of forever loop */
810
ah = getch(); putch(ah);
811
al = getch(); putch(al);
813
ah = ah - 'a' + 0x0a;
814
} else if(ah >= '0') {
818
al = al - 'a' + 0x0a;
819
} else if(al >= '0') {
822
return (ah << 4) + al;
826
void puthex(char ch) {
829
ah = (ch & 0xf0) >> 4;
831
ah = ah - 0x0a + 'a';
837
al = al - 0x0a + 'a';
848
#ifdef __AVR_ATmega128__
850
while (!(UCSR0A & _BV(UDRE0)));
853
else if (bootuart == 2) {
854
while (!(UCSR1A & _BV(UDRE1)));
857
#elif defined __AVR_ATmega168__
858
while (!(UCSR0A & _BV(UDRE0)));
861
/* m8,16,32,169,8515,8535,163 */
862
while (!(UCSRA & _BV(UDRE)));
870
#ifdef __AVR_ATmega128__
872
while(!(UCSR0A & _BV(RXC0)));
875
else if(bootuart == 2) {
876
while(!(UCSR1A & _BV(RXC1)));
880
#elif defined __AVR_ATmega168__
882
while(!(UCSR0A & _BV(RXC0))){
883
/* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
884
/* HACKME:: here is a good place to count times*/
886
if (count > MAX_TIME_COUNT)
891
/* m8,16,32,169,8515,8535,163 */
893
while(!(UCSRA & _BV(RXC))){
894
/* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
895
/* HACKME:: here is a good place to count times*/
897
if (count > MAX_TIME_COUNT)
905
void getNch(uint8_t count)
908
for(i=0;i<count;i++) {
909
#ifdef __AVR_ATmega128__
911
while(!(UCSR0A & _BV(RXC0)));
914
else if(bootuart == 2) {
915
while(!(UCSR1A & _BV(RXC1)));
918
#elif defined __AVR_ATmega168__
919
while(!(UCSR0A & _BV(RXC0)));
922
/* m8,16,32,169,8515,8535,163 */
923
/* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
924
//while(!(UCSRA & _BV(RXC)));
927
for(i=0;i<count;i++) {
928
getch(); // need to handle time out
935
void byte_response(uint8_t val)
937
if (getch() == ' ') {
942
if (++error_count == MAX_ERROR_COUNT)
948
void nothing_response(void)
950
if (getch() == ' ') {
954
if (++error_count == MAX_ERROR_COUNT)
959
void flash_led(uint8_t count)
961
/* flash onboard LED three times to signal entering of bootloader */
962
/* l needs to be volatile or the delay loops below might get
963
optimized away if compiling with optimizations (DAM). */
970
for (i = 0; i < count; ++i) {
971
LED_PORT |= _BV(LED);
972
for(l = 0; l < (F_CPU / 1000); ++l);
973
LED_PORT &= ~_BV(LED);
974
for(l = 0; l < (F_CPU / 1000); ++l);
979
/* end of file ATmegaBOOT.c */