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 */
10
/* date : 15.08.2005 */
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 m128,m8,m163 - feel free to let me know */
57
/* how/if it works for you. */
59
/**********************************************************/
65
#include <avr/pgmspace.h>
66
#include <avr/interrupt.h>
70
#define set_output(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
71
#define set_input(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
74
#define high(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
75
#define low(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
80
/* the current avr-libc eeprom functions do not support the ATmega168 */
81
/* own eeprom write/read functions are used instead */
82
#ifndef __AVR_ATmega168__
83
#include <avr/eeprom.h>
86
/* define F_CPU according to AVR_FREQ set in Makefile */
87
/* Is there a better way to pass such a parameter from Makefile to source code ? */
89
#define F_CPU 16000000L
91
#include <util/delay.h>
94
/* 20060803: hacked by DojoCorp */
95
/* set the waiting time for the bootloader */
96
#define MAX_TIME_COUNT (F_CPU>>1)
98
/* set the UART baud rate */
99
/* 20060803: hacked by DojoCorp */
100
#define BAUD_RATE 115200
103
/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
104
/* never allow AVR Studio to do an update !!!! */
106
#define SW_MAJOR 0x01
107
#define SW_MINOR 0x0f
110
/* Adjust to suit whatever pin your hardware uses to enter the bootloader */
111
/* ATmega128 has two UARTS so two pins are used to enter bootloader and select UART */
112
/* BL0... means UART0, BL1... means UART1 */
113
#ifdef __AVR_ATmega128__
115
#define BL_PORT PORTF
120
/* other ATmegas have only one UART, so only one pin is defined to enter bootloader */
122
#define BL_PORT PORTD
128
/* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */
129
/* if monitor functions are included, LED goes on after monitor was entered */
130
#ifdef __AVR_ATmega128__
131
/* Onboard LED is connected to pin PB7 (e.g. Crumb128, PROBOmega128, Savvy128) */
133
#define LED_PORT PORTB
137
/* Onboard LED is connected to pin PB2 (e.g. Crumb8, Crumb168) */
139
#define LED_PORT PORTB
141
/* 20060803: hacked by DojoCorp, LED pin is B5 in Arduino */
142
/* #define LED PINB2 */
147
/* monitor functions will only be compiled when using ATmega128, due to bootblock size constraints */
148
#ifdef __AVR_ATmega128__
153
/* define various device id's */
154
/* manufacturer byte is always the same */
155
#define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
157
#if defined __AVR_ATmega128__
160
#define PAGE_SIZE 0x80U //128 words
162
#elif defined __AVR_ATmega64__
165
#define PAGE_SIZE 0x80U //128 words
167
#elif defined __AVR_ATmega32__
170
#define PAGE_SIZE 0x40U //64 words
172
#elif defined __AVR_ATmega16__
175
#define PAGE_SIZE 0x40U //64 words
177
#elif defined __AVR_ATmega8__
180
#define PAGE_SIZE 0x20U //32 words
182
#elif defined __AVR_ATmega88__
185
#define PAGE_SIZE 0x20U //32 words
187
#elif defined __AVR_ATmega168__
190
#define PAGE_SIZE 0x40U //64 words
192
#elif defined __AVR_ATmega162__
195
#define PAGE_SIZE 0x40U //64 words
197
#elif defined __AVR_ATmega163__
200
#define PAGE_SIZE 0x40U //64 words
202
#elif defined __AVR_ATmega169__
205
#define PAGE_SIZE 0x40U //64 words
207
#elif defined __AVR_ATmega8515__
210
#define PAGE_SIZE 0x20U //32 words
212
#elif defined __AVR_ATmega8535__
215
#define PAGE_SIZE 0x20U //32 words
219
/* function prototypes */
222
void getNch(uint8_t);
223
void byte_response(uint8_t);
224
void nothing_response(void);
227
void flash_led(uint8_t);
230
union address_union {
240
struct flags_struct {
246
uint8_t address_high;
251
uint8_t bootuart = 0;
253
void (*app_start)(void) = 0x0000;
256
/* main program starts here */
262
asm volatile("nop\n\t");
264
/* set pin direction for bootloader pin and enable pullup */
265
/* for ATmega128, two pins need to be initialized */
266
#ifdef __AVR_ATmega128__
277
#ifdef __AVR_ATmega128__
278
/* check which UART should be used for booting */
279
if(bit_is_clear(BL_PIN, BL0)) {
282
else if(bit_is_clear(BL_PIN, BL1)) {
287
/* check if flash is programmed already, if not start bootloader anyway */
288
if(pgm_read_byte_near(0x0000) != 0xFF) {
290
#ifdef __AVR_ATmega128__
291
/* no UART was selected, start application */
296
/* check if bootloader pin is set low */
297
/* we don't start this part neither for the m8, nor m168 */
298
//if(bit_is_set(BL_PIN, BL)) {
304
#ifdef __AVR_ATmega128__
305
/* no bootuart was selected, default to uart 0 */
312
/* initialize UART(s) depending on CPU defined */
313
#ifdef __AVR_ATmega128__
315
UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
316
UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
319
UCSR0B = _BV(TXEN0)|_BV(RXEN0);
322
UBRR1L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
323
UBRR1H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
326
UCSR1B = _BV(TXEN1)|_BV(RXEN1);
328
#elif defined __AVR_ATmega163__
329
UBRR = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
330
UBRRHI = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
332
UCSRB = _BV(TXEN)|_BV(RXEN);
333
#elif defined __AVR_ATmega168__
334
UBRR0H = ((F_CPU / 16 + BAUD_RATE / 2) / BAUD_RATE - 1) >> 8;
335
UBRR0L = ((F_CPU / 16 + BAUD_RATE / 2) / BAUD_RATE - 1);
338
//UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
339
//UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
340
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
341
UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
342
#elif defined __AVR_ATmega8__
344
UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
345
UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
346
UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & Tx
347
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
349
/* m16,m32,m169,m8515,m8535 */
350
UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
351
UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
354
UCSRB = _BV(TXEN)|_BV(RXEN);
357
/* set LED pin as output */
362
set_output(DDRD,PIND7);
364
for (i = 0; i < 16; i++) {
373
/* flash onboard LED to signal entering of bootloader */
374
#ifdef __AVR_ATmega128__
375
// 4x for UART0, 5x for UART1
376
flash_led(3 + bootuart);
381
/* 20050803: by DojoCorp, this is one of the parts provoking the
382
system to stop listening, cancelled from the original */
386
//message("SET BT PAGEMODE 3 2000 1");
414
//put_s("SET BT ROLE 0 f 7d00");
445
/* get character from UART */
448
/* A bunch of if...else if... gives smaller code than switch...case ! */
450
/* Hello is anyone home ? */
456
/* Request programmer ID */
457
/* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
458
/* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
460
if (getch() == ' ') {
474
/* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
477
if (ch2>0x85) getch();
482
/* AVR ISP/STK500 board requests */
485
if(ch2==0x80) byte_response(HW_VER); // Hardware version
486
else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
487
else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
488
else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
489
else byte_response(0x00); // Covers various unnecessary responses we don't care about
493
/* Device Parameters DON'T CARE, DEVICE IS FIXED */
500
/* Parallel programming stuff DON'T CARE */
507
/* Enter programming mode */
513
/* Leave programming mode */
519
/* Erase device, don't care as we will erase one page at a time anyway. */
525
/* Set address, little endian. EEPROM in bytes, FLASH in words */
526
/* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
527
/* This might explain why little endian was used here, big endian used everywhere else. */
529
address.byte[0] = getch();
530
address.byte[1] = getch();
535
/* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
542
/* Write memory, length is big endian and is in bytes */
544
length.byte[1] = getch();
545
length.byte[0] = getch();
547
if (getch() == 'E') flags.eeprom = 1;
548
for (w=0;w<length.word;w++) {
549
buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
551
if (getch() == ' ') {
552
if (flags.eeprom) { //Write to EEPROM one byte at a time
553
for(w=0;w<length.word;w++) {
554
#ifdef __AVR_ATmega168__
555
while(EECR & (1<<EEPE));
556
EEAR = (uint16_t)(void *)address.word;
561
eeprom_write_byte((void *)address.word,buff[w]);
566
else { //Write to FLASH one page at a time
567
if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
568
else address_high = 0x00;
569
#ifdef __AVR_ATmega128__
570
RAMPZ = address_high;
572
address.word = address.word << 1; //address * 2 -> byte location
573
/* if ((length.byte[0] & 0x01) == 0x01) length.word++; //Even up an odd number of bytes */
574
if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
575
cli(); //Disable interrupts, just to be sure
576
// HACKME: EEPE used to be EEWE
577
while(bit_is_set(EECR,EEPE)); //Wait for previous EEPROM writes to complete
579
"clr r17 \n\t" //page_word_count
580
"lds r30,address \n\t" //Address of FLASH location (in bytes)
581
"lds r31,address+1 \n\t"
582
"ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
583
"ldi r29,hi8(buff) \n\t"
584
"lds r24,length \n\t" //Length of data to be written (in bytes)
585
"lds r25,length+1 \n\t"
586
"length_loop: \n\t" //Main loop, repeat for number of words in block
587
"cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
588
"brne no_page_erase \n\t"
590
"lds r16,%0 \n\t" //Wait for previous spm to complete
593
"breq wait_spm1 \n\t"
594
"ldi r16,0x03 \n\t" //Erase page pointed to by Z
597
#ifdef __AVR_ATmega163__
602
"lds r16,%0 \n\t" //Wait for previous spm to complete
605
"breq wait_spm2 \n\t"
607
"ldi r16,0x11 \n\t" //Re-enable RWW section
610
#ifdef __AVR_ATmega163__
614
"no_page_erase: \n\t"
615
"ld r0,Y+ \n\t" //Write 2 bytes into page buffer
619
"lds r16,%0 \n\t" //Wait for previous spm to complete
622
"breq wait_spm3 \n\t"
623
"ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
627
"inc r17 \n\t" //page_word_count++
629
"brlo same_page \n\t" //Still same page in FLASH
631
"clr r17 \n\t" //New page, write current one first
633
"lds r16,%0 \n\t" //Wait for previous spm to complete
636
"breq wait_spm4 \n\t"
637
#ifdef __AVR_ATmega163__
638
"andi r30,0x80 \n\t" // m163 requires Z6:Z1 to be zero during page write
640
"ldi r16,0x05 \n\t" //Write page pointed to by Z
643
#ifdef __AVR_ATmega163__
646
"ori r30,0x7E \n\t" // recover Z6:Z1 state after page write (had to be zero during write)
649
"lds r16,%0 \n\t" //Wait for previous spm to complete
652
"breq wait_spm5 \n\t"
653
"ldi r16,0x11 \n\t" //Re-enable RWW section
656
#ifdef __AVR_ATmega163__
661
"adiw r30,2 \n\t" //Next word in FLASH
662
"sbiw r24,2 \n\t" //length-2
663
"breq final_write \n\t" //Finished
664
"rjmp length_loop \n\t"
667
"breq block_done \n\t"
668
"adiw r24,2 \n\t" //length+2, fool above check on length after short page write
669
"rjmp write_page \n\t"
671
"clr __zero_reg__ \n\t" //restore zero register
672
#if defined __AVR_ATmega168__
673
: "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
675
: "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
678
/* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
679
/* exit the bootloader without a power cycle anyhow */
687
/* Read memory block mode, length is big endian. */
689
length.byte[1] = getch();
690
length.byte[0] = getch();
691
#if defined __AVR_ATmega128__
692
if (address.word>0x7FFF) flags.rampz = 1; // No go with m256, FIXME
693
else flags.rampz = 0;
695
if (getch() == 'E') flags.eeprom = 1;
698
address.word = address.word << 1; // address * 2 -> byte location
700
if (getch() == ' ') { // Command terminator
702
for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
703
if (flags.eeprom) { // Byte access EEPROM read
704
#ifdef __AVR_ATmega168__
705
while(EECR & (1<<EEPE));
706
EEAR = (uint16_t)(void *)address.word;
710
putch(eeprom_read_byte((void *)address.word));
716
if (!flags.rampz) putch(pgm_read_byte_near(address.word));
717
#if defined __AVR_ATmega128__
718
else putch(pgm_read_byte_far(address.word + 0x10000));
719
// Hmmmm, yuck FIXME when m256 arrvies
729
/* Get device signature bytes */
731
if (getch() == ' ') {
741
/* Read oscillator calibration byte */
749
/* here come the extended monitor commands by Erik Lins */
751
/* check for three times exclamation mark pressed */
758
#ifdef __AVR_ATmega128__
761
uint8_t addrl, addrh;
764
PGM_P welcome = {"ATmegaBOOT / Crumb128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
765
#elif defined PROBOMEGA128
766
PGM_P welcome = {"ATmegaBOOT / PROBOmega128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
767
#elif defined SAVVY128
768
PGM_P welcome = {"ATmegaBOOT / Savvy128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
773
LED_PORT &= ~_BV(LED);
775
/* print a welcome message and command overview */
776
for(i=0; welcome[i] != '\0'; ++i) {
780
/* test for valid commands */
792
if(bit_is_set(LED_PIN,LED)) {
793
LED_PORT &= ~_BV(LED);
796
LED_PORT |= _BV(LED);
802
/* read byte from address */
804
ch = getch(); putch(ch);
808
ch = *(uint8_t *)((addrh << 8) + addrl);
812
/* write a byte to address */
814
ch = getch(); putch(ch);
817
ch = getch(); putch(ch);
819
*(uint8_t *)((addrh << 8) + addrl) = ch;
823
/* read from uart and echo back */
829
#ifdef __AVR_ATmega128__
830
/* external bus loop */
840
ch = *(volatile uint8_t *)extaddr;
853
/* end of monitor functions */
863
/* end of forever loop */
871
ah = getch(); putch(ah);
872
al = getch(); putch(al);
874
ah = ah - 'a' + 0x0a;
875
} else if(ah >= '0') {
879
al = al - 'a' + 0x0a;
880
} else if(al >= '0') {
883
return (ah << 4) + al;
887
void puthex(char ch) {
890
ah = (ch & 0xf0) >> 4;
892
ah = ah - 0x0a + 'a';
898
al = al - 0x0a + 'a';
909
#ifdef __AVR_ATmega128__
911
while (!(UCSR0A & _BV(UDRE0)));
914
else if (bootuart == 2) {
915
while (!(UCSR1A & _BV(UDRE1)));
918
#elif defined __AVR_ATmega168__
919
while (!(UCSR0A & _BV(UDRE0)));
922
/* m8,16,32,169,8515,8535,163 */
923
while (!(UCSRA & _BV(UDRE)));
931
#ifdef __AVR_ATmega128__
933
while(!(UCSR0A & _BV(RXC0)));
936
else if(bootuart == 2) {
937
while(!(UCSR1A & _BV(RXC1)));
941
#elif defined __AVR_ATmega168__
943
while(!(UCSR0A & _BV(RXC0))){
944
/* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
945
/* HACKME:: here is a good place to count times*/
947
if (count > MAX_TIME_COUNT)
952
/* m8,16,32,169,8515,8535,163 */
954
while(!(UCSRA & _BV(RXC))){
955
/* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
956
/* HACKME:: here is a good place to count times*/
958
if (count > MAX_TIME_COUNT)
966
void getNch(uint8_t count)
969
for(i=0;i<count;i++) {
970
#ifdef __AVR_ATmega128__
972
while(!(UCSR0A & _BV(RXC0)));
975
else if(bootuart == 2) {
976
while(!(UCSR1A & _BV(RXC1)));
979
#elif defined __AVR_ATmega168__
980
while(!(UCSR0A & _BV(RXC0)));
983
/* m8,16,32,169,8515,8535,163 */
984
/* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
985
//while(!(UCSRA & _BV(RXC)));
988
for(i=0;i<count;i++) {
989
getch(); // need to handle time out
996
void byte_response(uint8_t val)
998
if (getch() == ' ') {
1006
void nothing_response(void)
1008
if (getch() == ' ') {
1014
void flash_led(uint8_t count)
1016
/* flash onboard LED three times to signal entering of bootloader */
1023
for (i = 0; i < count; ++i) {
1024
LED_PORT |= _BV(LED);
1025
for(l = 0; l < (2 * F_CPU); ++l);
1026
LED_PORT &= ~_BV(LED);
1027
for(l = 0; l < (F_CPU / 5); ++l);
1032
/* end of file ATmegaBOOT.c */