aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbootloaders/atmega168/ATmegaBOOT_168.c979
-rw-r--r--bootloaders/atmega168/ATmegaBOOT_168_diecimila.hex117
-rw-r--r--bootloaders/atmega168/ATmegaBOOT_168_ng.hex117
-rwxr-xr-xbootloaders/atmega168/Makefile92
-rwxr-xr-xbootloaders/atmega8/ATmegaBOOT.c507
-rw-r--r--bootloaders/atmega8/ATmegaBOOT.hex66
-rw-r--r--bootloaders/atmega8/Makefile88
-rw-r--r--bootloaders/bt/ATmegaBOOT_168.c1032
-rw-r--r--bootloaders/bt/ATmegaBOOT_168.hex121
9 files changed, 3119 insertions, 0 deletions
diff --git a/bootloaders/atmega168/ATmegaBOOT_168.c b/bootloaders/atmega168/ATmegaBOOT_168.c
new file mode 100755
index 0000000..c542af1
--- /dev/null
+++ b/bootloaders/atmega168/ATmegaBOOT_168.c
@@ -0,0 +1,979 @@
+/**********************************************************/
+/* Serial Bootloader for Atmel megaAVR Controllers */
+/* */
+/* tested with ATmega8, ATmega128 and ATmega168 */
+/* should work with other mega's, see code for details */
+/* */
+/* ATmegaBOOT.c */
+/* */
+/* 20070626: hacked for Arduino Diecimila (which auto- */
+/* resets when a USB connection is made to it) */
+/* by D. Mellis */
+/* 20060802: hacked for Arduino by D. Cuartielles */
+/* based on a previous hack by D. Mellis */
+/* and D. Cuartielles */
+/* */
+/* Monitor and debug functions were added to the original */
+/* code by Dr. Erik Lins, chip45.com. (See below) */
+/* */
+/* Thanks to Karl Pitrich for fixing a bootloader pin */
+/* problem and more informative LED blinking! */
+/* */
+/* For the latest version see: */
+/* http://www.chip45.com/ */
+/* */
+/* ------------------------------------------------------ */
+/* */
+/* based on stk500boot.c */
+/* Copyright (c) 2003, Jason P. Kyle */
+/* All rights reserved. */
+/* see avr1.org for original file and information */
+/* */
+/* This program is free software; you can redistribute it */
+/* and/or modify it under the terms of the GNU General */
+/* Public License as published by the Free Software */
+/* Foundation; either version 2 of the License, or */
+/* (at your option) any later version. */
+/* */
+/* This program is distributed in the hope that it will */
+/* be useful, but WITHOUT ANY WARRANTY; without even the */
+/* implied warranty of MERCHANTABILITY or FITNESS FOR A */
+/* PARTICULAR PURPOSE. See the GNU General Public */
+/* License for more details. */
+/* */
+/* You should have received a copy of the GNU General */
+/* Public License along with this program; if not, write */
+/* to the Free Software Foundation, Inc., */
+/* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+/* */
+/* Licence can be viewed at */
+/* http://www.fsf.org/licenses/gpl.txt */
+/* */
+/* Target = Atmel AVR m128,m64,m32,m16,m8,m162,m163,m169, */
+/* m8515,m8535. ATmega161 has a very small boot block so */
+/* isn't supported. */
+/* */
+/* Tested with m168 */
+/**********************************************************/
+
+/* $Id$ */
+
+
+/* some includes */
+#include <inttypes.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/interrupt.h>
+#include <avr/wdt.h>
+
+
+/* the current avr-libc eeprom functions do not support the ATmega168 */
+/* own eeprom write/read functions are used instead */
+#ifndef __AVR_ATmega168__
+#include <avr/eeprom.h>
+#endif
+
+/* Use the F_CPU defined in Makefile */
+
+/* 20060803: hacked by DojoCorp */
+/* 20070626: hacked by David A. Mellis to decrease waiting time for auto-reset */
+/* set the waiting time for the bootloader */
+/* get this from the Makefile instead */
+/* #define MAX_TIME_COUNT (F_CPU>>4) */
+
+/* 20070707: hacked by David A. Mellis - after this many errors give up and launch application */
+#define MAX_ERROR_COUNT 5
+
+/* set the UART baud rate */
+/* 20060803: hacked by DojoCorp */
+//#define BAUD_RATE 115200
+#define BAUD_RATE 19200
+
+
+/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
+/* never allow AVR Studio to do an update !!!! */
+#define HW_VER 0x02
+#define SW_MAJOR 0x01
+#define SW_MINOR 0x10
+
+
+/* Adjust to suit whatever pin your hardware uses to enter the bootloader */
+/* ATmega128 has two UARTS so two pins are used to enter bootloader and select UART */
+/* BL0... means UART0, BL1... means UART1 */
+#ifdef __AVR_ATmega128__
+#define BL_DDR DDRF
+#define BL_PORT PORTF
+#define BL_PIN PINF
+#define BL0 PINF7
+#define BL1 PINF6
+#else
+/* other ATmegas have only one UART, so only one pin is defined to enter bootloader */
+#define BL_DDR DDRD
+#define BL_PORT PORTD
+#define BL_PIN PIND
+#define BL PIND6
+#endif
+
+
+/* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */
+/* if monitor functions are included, LED goes on after monitor was entered */
+#ifdef __AVR_ATmega128__
+/* Onboard LED is connected to pin PB7 (e.g. Crumb128, PROBOmega128, Savvy128) */
+#define LED_DDR DDRB
+#define LED_PORT PORTB
+#define LED_PIN PINB
+#define LED PINB7
+#else
+/* Onboard LED is connected to pin PB2 (e.g. Crumb8, Crumb168) */
+#define LED_DDR DDRB
+#define LED_PORT PORTB
+#define LED_PIN PINB
+/* 20060803: hacked by DojoCorp, LED pin is B5 in Arduino */
+/* #define LED PINB2 */
+#define LED PINB5
+#endif
+
+
+/* monitor functions will only be compiled when using ATmega128, due to bootblock size constraints */
+#ifdef __AVR_ATmega128__
+#define MONITOR
+#endif
+
+
+/* define various device id's */
+/* manufacturer byte is always the same */
+#define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
+
+#if defined __AVR_ATmega128__
+#define SIG2 0x97
+#define SIG3 0x02
+#define PAGE_SIZE 0x80U //128 words
+
+#elif defined __AVR_ATmega64__
+#define SIG2 0x96
+#define SIG3 0x02
+#define PAGE_SIZE 0x80U //128 words
+
+#elif defined __AVR_ATmega32__
+#define SIG2 0x95
+#define SIG3 0x02
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega16__
+#define SIG2 0x94
+#define SIG3 0x03
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega8__
+#define SIG2 0x93
+#define SIG3 0x07
+#define PAGE_SIZE 0x20U //32 words
+
+#elif defined __AVR_ATmega88__
+#define SIG2 0x93
+#define SIG3 0x0a
+#define PAGE_SIZE 0x20U //32 words
+
+#elif defined __AVR_ATmega168__
+#define SIG2 0x94
+#define SIG3 0x06
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega162__
+#define SIG2 0x94
+#define SIG3 0x04
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega163__
+#define SIG2 0x94
+#define SIG3 0x02
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega169__
+#define SIG2 0x94
+#define SIG3 0x05
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega8515__
+#define SIG2 0x93
+#define SIG3 0x06
+#define PAGE_SIZE 0x20U //32 words
+
+#elif defined __AVR_ATmega8535__
+#define SIG2 0x93
+#define SIG3 0x08
+#define PAGE_SIZE 0x20U //32 words
+#endif
+
+
+/* function prototypes */
+void putch(char);
+char getch(void);
+void getNch(uint8_t);
+void byte_response(uint8_t);
+void nothing_response(void);
+char gethex(void);
+void puthex(char);
+void flash_led(uint8_t);
+
+/* some variables */
+union address_union {
+ uint16_t word;
+ uint8_t byte[2];
+} address;
+
+union length_union {
+ uint16_t word;
+ uint8_t byte[2];
+} length;
+
+struct flags_struct {
+ unsigned eeprom : 1;
+ unsigned rampz : 1;
+} flags;
+
+uint8_t buff[256];
+uint8_t address_high;
+
+uint8_t pagesz=0x80;
+
+uint8_t i;
+uint8_t bootuart = 0;
+
+uint8_t error_count = 0;
+
+void (*app_start)(void) = 0x0000;
+
+
+/* main program starts here */
+int main(void)
+{
+ uint8_t ch,ch2;
+ uint16_t w;
+
+ asm volatile("nop\n\t");
+
+ /* set pin direction for bootloader pin and enable pullup */
+ /* for ATmega128, two pins need to be initialized */
+#ifdef __AVR_ATmega128__
+ BL_DDR &= ~_BV(BL0);
+ BL_DDR &= ~_BV(BL1);
+ BL_PORT |= _BV(BL0);
+ BL_PORT |= _BV(BL1);
+#else
+ /* We run the bootloader regardless of the state of this pin. Thus, don't
+ put it in a different state than the other pins. --DAM, 070709
+ BL_DDR &= ~_BV(BL);
+ BL_PORT |= _BV(BL);
+ */
+#endif
+
+
+#ifdef __AVR_ATmega128__
+ /* check which UART should be used for booting */
+ if(bit_is_clear(BL_PIN, BL0)) {
+ bootuart = 1;
+ }
+ else if(bit_is_clear(BL_PIN, BL1)) {
+ bootuart = 2;
+ }
+#endif
+
+ /* check if flash is programmed already, if not start bootloader anyway */
+ if(pgm_read_byte_near(0x0000) != 0xFF) {
+
+#ifdef __AVR_ATmega128__
+ /* no UART was selected, start application */
+ if(!bootuart) {
+ app_start();
+ }
+#else
+ /* check if bootloader pin is set low */
+ /* we don't start this part neither for the m8, nor m168 */
+ //if(bit_is_set(BL_PIN, BL)) {
+ // app_start();
+ // }
+#endif
+ }
+
+#ifdef __AVR_ATmega128__
+ /* no bootuart was selected, default to uart 0 */
+ if(!bootuart) {
+ bootuart = 1;
+ }
+#endif
+
+
+ /* initialize UART(s) depending on CPU defined */
+#ifdef __AVR_ATmega128__
+ if(bootuart == 1) {
+ UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ UCSR0A = 0x00;
+ UCSR0C = 0x06;
+ UCSR0B = _BV(TXEN0)|_BV(RXEN0);
+ }
+ if(bootuart == 2) {
+ UBRR1L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ UBRR1H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ UCSR1A = 0x00;
+ UCSR1C = 0x06;
+ UCSR1B = _BV(TXEN1)|_BV(RXEN1);
+ }
+#elif defined __AVR_ATmega163__
+ UBRR = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ UBRRHI = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ UCSRA = 0x00;
+ UCSRB = _BV(TXEN)|_BV(RXEN);
+#elif defined __AVR_ATmega168__
+ UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ UCSR0B = (1<<RXEN0) | (1<<TXEN0);
+ UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
+
+ /* Enable internal pull-up resistor on pin D0 (RX), in order
+ to supress line noise that prevents the bootloader from
+ timing out (DAM: 20070509) */
+ DDRD &= ~_BV(PIND0);
+ PORTD |= _BV(PIND0);
+#elif defined __AVR_ATmega8__
+ /* m8 */
+ UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
+ UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
+ UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & Tx
+ UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
+#else
+ /* m16,m32,m169,m8515,m8535 */
+ UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ UCSRA = 0x00;
+ UCSRC = 0x06;
+ UCSRB = _BV(TXEN)|_BV(RXEN);
+#endif
+
+ /* set LED pin as output */
+ LED_DDR |= _BV(LED);
+
+
+ /* flash onboard LED to signal entering of bootloader */
+#ifdef __AVR_ATmega128__
+ // 4x for UART0, 5x for UART1
+ flash_led(NUM_LED_FLASHES + bootuart);
+#else
+ flash_led(NUM_LED_FLASHES);
+#endif
+
+ /* 20050803: by DojoCorp, this is one of the parts provoking the
+ system to stop listening, cancelled from the original */
+ //putch('\0');
+
+
+ /* forever loop */
+ for (;;) {
+
+ /* get character from UART */
+ ch = getch();
+
+ /* A bunch of if...else if... gives smaller code than switch...case ! */
+
+ /* Hello is anyone home ? */
+ if(ch=='0') {
+ nothing_response();
+ }
+
+
+ /* Request programmer ID */
+ /* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
+ /* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
+ else if(ch=='1') {
+ if (getch() == ' ') {
+ putch(0x14);
+ putch('A');
+ putch('V');
+ putch('R');
+ putch(' ');
+ putch('I');
+ putch('S');
+ putch('P');
+ putch(0x10);
+ } else {
+ if (++error_count == MAX_ERROR_COUNT)
+ app_start();
+ }
+ }
+
+
+ /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
+ else if(ch=='@') {
+ ch2 = getch();
+ if (ch2>0x85) getch();
+ nothing_response();
+ }
+
+
+ /* AVR ISP/STK500 board requests */
+ else if(ch=='A') {
+ ch2 = getch();
+ if(ch2==0x80) byte_response(HW_VER); // Hardware version
+ else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
+ else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
+ else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
+ else byte_response(0x00); // Covers various unnecessary responses we don't care about
+ }
+
+
+ /* Device Parameters DON'T CARE, DEVICE IS FIXED */
+ else if(ch=='B') {
+ getNch(20);
+ nothing_response();
+ }
+
+
+ /* Parallel programming stuff DON'T CARE */
+ else if(ch=='E') {
+ getNch(5);
+ nothing_response();
+ }
+
+
+ /* Enter programming mode */
+ else if(ch=='P') {
+ nothing_response();
+ }
+
+
+ /* Leave programming mode */
+ else if(ch=='Q') {
+ nothing_response();
+ }
+
+
+ /* Erase device, don't care as we will erase one page at a time anyway. */
+ else if(ch=='R') {
+ nothing_response();
+ }
+
+
+ /* Set address, little endian. EEPROM in bytes, FLASH in words */
+ /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
+ /* This might explain why little endian was used here, big endian used everywhere else. */
+ else if(ch=='U') {
+ address.byte[0] = getch();
+ address.byte[1] = getch();
+ nothing_response();
+ }
+
+
+ /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
+ else if(ch=='V') {
+ getNch(4);
+ byte_response(0x00);
+ }
+
+
+ /* Write memory, length is big endian and is in bytes */
+ else if(ch=='d') {
+ length.byte[1] = getch();
+ length.byte[0] = getch();
+ flags.eeprom = 0;
+ if (getch() == 'E') flags.eeprom = 1;
+ for (w=0;w<length.word;w++) {
+ buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
+ }
+ if (getch() == ' ') {
+ if (flags.eeprom) { //Write to EEPROM one byte at a time
+ for(w=0;w<length.word;w++) {
+#ifdef __AVR_ATmega168__
+ while(EECR & (1<<EEPE));
+ EEAR = (uint16_t)(void *)address.word;
+ EEDR = buff[w];
+ EECR |= (1<<EEMPE);
+ EECR |= (1<<EEPE);
+#else
+ eeprom_write_byte((void *)address.word,buff[w]);
+#endif
+ address.word++;
+ }
+ }
+ else { //Write to FLASH one page at a time
+ if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
+ else address_high = 0x00;
+#ifdef __AVR_ATmega128__
+ RAMPZ = address_high;
+#endif
+ address.word = address.word << 1; //address * 2 -> byte location
+ /* if ((length.byte[0] & 0x01) == 0x01) length.word++; //Even up an odd number of bytes */
+ if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
+ cli(); //Disable interrupts, just to be sure
+ // HACKME: EEPE used to be EEWE
+ while(bit_is_set(EECR,EEPE)); //Wait for previous EEPROM writes to complete
+ asm volatile(
+ "clr r17 \n\t" //page_word_count
+ "lds r30,address \n\t" //Address of FLASH location (in bytes)
+ "lds r31,address+1 \n\t"
+ "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
+ "ldi r29,hi8(buff) \n\t"
+ "lds r24,length \n\t" //Length of data to be written (in bytes)
+ "lds r25,length+1 \n\t"
+ "length_loop: \n\t" //Main loop, repeat for number of words in block
+ "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
+ "brne no_page_erase \n\t"
+ "wait_spm1: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm1 \n\t"
+ "ldi r16,0x03 \n\t" //Erase page pointed to by Z
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+#ifdef __AVR_ATmega163__
+ ".word 0xFFFF \n\t"
+ "nop \n\t"
+#endif
+ "wait_spm2: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm2 \n\t"
+
+ "ldi r16,0x11 \n\t" //Re-enable RWW section
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+#ifdef __AVR_ATmega163__
+ ".word 0xFFFF \n\t"
+ "nop \n\t"
+#endif
+ "no_page_erase: \n\t"
+ "ld r0,Y+ \n\t" //Write 2 bytes into page buffer
+ "ld r1,Y+ \n\t"
+
+ "wait_spm3: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm3 \n\t"
+ "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+
+ "inc r17 \n\t" //page_word_count++
+ "cpi r17,%1 \n\t"
+ "brlo same_page \n\t" //Still same page in FLASH
+ "write_page: \n\t"
+ "clr r17 \n\t" //New page, write current one first
+ "wait_spm4: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm4 \n\t"
+#ifdef __AVR_ATmega163__
+ "andi r30,0x80 \n\t" // m163 requires Z6:Z1 to be zero during page write
+#endif
+ "ldi r16,0x05 \n\t" //Write page pointed to by Z
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+#ifdef __AVR_ATmega163__
+ ".word 0xFFFF \n\t"
+ "nop \n\t"
+ "ori r30,0x7E \n\t" // recover Z6:Z1 state after page write (had to be zero during write)
+#endif
+ "wait_spm5: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm5 \n\t"
+ "ldi r16,0x11 \n\t" //Re-enable RWW section
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+#ifdef __AVR_ATmega163__
+ ".word 0xFFFF \n\t"
+ "nop \n\t"
+#endif
+ "same_page: \n\t"
+ "adiw r30,2 \n\t" //Next word in FLASH
+ "sbiw r24,2 \n\t" //length-2
+ "breq final_write \n\t" //Finished
+ "rjmp length_loop \n\t"
+ "final_write: \n\t"
+ "cpi r17,0 \n\t"
+ "breq block_done \n\t"
+ "adiw r24,2 \n\t" //length+2, fool above check on length after short page write
+ "rjmp write_page \n\t"
+ "block_done: \n\t"
+ "clr __zero_reg__ \n\t" //restore zero register
+#if defined __AVR_ATmega168__
+ : "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
+#else
+ : "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
+#endif
+ );
+ /* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
+ /* exit the bootloader without a power cycle anyhow */
+ }
+ putch(0x14);
+ putch(0x10);
+ } else {
+ if (++error_count == MAX_ERROR_COUNT)
+ app_start();
+ }
+ }
+
+
+ /* Read memory block mode, length is big endian. */
+ else if(ch=='t') {
+ length.byte[1] = getch();
+ length.byte[0] = getch();
+#if defined __AVR_ATmega128__
+ if (address.word>0x7FFF) flags.rampz = 1; // No go with m256, FIXME
+ else flags.rampz = 0;
+#endif
+ if (getch() == 'E') flags.eeprom = 1;
+ else {
+ flags.eeprom = 0;
+ address.word = address.word << 1; // address * 2 -> byte location
+ }
+ if (getch() == ' ') { // Command terminator
+ putch(0x14);
+ for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
+ if (flags.eeprom) { // Byte access EEPROM read
+#ifdef __AVR_ATmega168__
+ while(EECR & (1<<EEPE));
+ EEAR = (uint16_t)(void *)address.word;
+ EECR |= (1<<EERE);
+ putch(EEDR);
+#else
+ putch(eeprom_read_byte((void *)address.word));
+#endif
+ address.word++;
+ }
+ else {
+
+ if (!flags.rampz) putch(pgm_read_byte_near(address.word));
+#if defined __AVR_ATmega128__
+ else putch(pgm_read_byte_far(address.word + 0x10000));
+ // Hmmmm, yuck FIXME when m256 arrvies
+#endif
+ address.word++;
+ }
+ }
+ putch(0x10);
+ }
+ }
+
+
+ /* Get device signature bytes */
+ else if(ch=='u') {
+ if (getch() == ' ') {
+ putch(0x14);
+ putch(SIG1);
+ putch(SIG2);
+ putch(SIG3);
+ putch(0x10);
+ } else {
+ if (++error_count == MAX_ERROR_COUNT)
+ app_start();
+ }
+ }
+
+
+ /* Read oscillator calibration byte */
+ else if(ch=='v') {
+ byte_response(0x00);
+ }
+
+
+#ifdef MONITOR
+
+ /* here come the extended monitor commands by Erik Lins */
+
+ /* check for three times exclamation mark pressed */
+ else if(ch=='!') {
+ ch = getch();
+ if(ch=='!') {
+ ch = getch();
+ if(ch=='!') {
+
+#ifdef __AVR_ATmega128__
+ uint16_t extaddr;
+#endif
+ uint8_t addrl, addrh;
+
+#ifdef CRUMB128
+ PGM_P welcome = {"ATmegaBOOT / Crumb128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
+#elif defined PROBOMEGA128
+ PGM_P welcome = {"ATmegaBOOT / PROBOmega128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
+#elif defined SAVVY128
+ PGM_P welcome = {"ATmegaBOOT / Savvy128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
+#endif
+
+ /* turn on LED */
+ LED_DDR |= _BV(LED);
+ LED_PORT &= ~_BV(LED);
+
+ /* print a welcome message and command overview */
+ for(i=0; welcome[i] != '\0'; ++i) {
+ putch(welcome[i]);
+ }
+
+ /* test for valid commands */
+ for(;;) {
+ putch('\n');
+ putch('\r');
+ putch(':');
+ putch(' ');
+
+ ch = getch();
+ putch(ch);
+
+ /* toggle LED */
+ if(ch == 't') {
+ if(bit_is_set(LED_PIN,LED)) {
+ LED_PORT &= ~_BV(LED);
+ putch('1');
+ } else {
+ LED_PORT |= _BV(LED);
+ putch('0');
+ }
+
+ }
+
+ /* read byte from address */
+ else if(ch == 'r') {
+ ch = getch(); putch(ch);
+ addrh = gethex();
+ addrl = gethex();
+ putch('=');
+ ch = *(uint8_t *)((addrh << 8) + addrl);
+ puthex(ch);
+ }
+
+ /* write a byte to address */
+ else if(ch == 'w') {
+ ch = getch(); putch(ch);
+ addrh = gethex();
+ addrl = gethex();
+ ch = getch(); putch(ch);
+ ch = gethex();
+ *(uint8_t *)((addrh << 8) + addrl) = ch;
+
+ }
+
+ /* read from uart and echo back */
+ else if(ch == 'u') {
+ for(;;) {
+ putch(getch());
+ }
+ }
+#ifdef __AVR_ATmega128__
+ /* external bus loop */
+ else if(ch == 'b') {
+ putch('b');
+ putch('u');
+ putch('s');
+ MCUCR = 0x80;
+ XMCRA = 0;
+ XMCRB = 0;
+ extaddr = 0x1100;
+ for(;;) {
+ ch = *(volatile uint8_t *)extaddr;
+ if(++extaddr == 0) {
+ extaddr = 0x1100;
+ }
+ }
+ }
+#endif
+
+ else if(ch == 'j') {
+ app_start();
+ }
+
+ }
+ /* end of monitor functions */
+
+ }
+ }
+ }
+ /* end of monitor */
+#endif
+ else if (++error_count == MAX_ERROR_COUNT) {
+ app_start();
+ }
+ }
+ /* end of forever loop */
+
+}
+
+
+char gethex(void) {
+ char ah,al;
+
+ ah = getch(); putch(ah);
+ al = getch(); putch(al);
+ if(ah >= 'a') {
+ ah = ah - 'a' + 0x0a;
+ } else if(ah >= '0') {
+ ah -= '0';
+ }
+ if(al >= 'a') {
+ al = al - 'a' + 0x0a;
+ } else if(al >= '0') {
+ al -= '0';
+ }
+ return (ah << 4) + al;
+}
+
+
+void puthex(char ch) {
+ char ah,al;
+
+ ah = (ch & 0xf0) >> 4;
+ if(ah >= 0x0a) {
+ ah = ah - 0x0a + 'a';
+ } else {
+ ah += '0';
+ }
+ al = (ch & 0x0f);
+ if(al >= 0x0a) {
+ al = al - 0x0a + 'a';
+ } else {
+ al += '0';
+ }
+ putch(ah);
+ putch(al);
+}
+
+
+void putch(char ch)
+{
+#ifdef __AVR_ATmega128__
+ if(bootuart == 1) {
+ while (!(UCSR0A & _BV(UDRE0)));
+ UDR0 = ch;
+ }
+ else if (bootuart == 2) {
+ while (!(UCSR1A & _BV(UDRE1)));
+ UDR1 = ch;
+ }
+#elif defined __AVR_ATmega168__
+ while (!(UCSR0A & _BV(UDRE0)));
+ UDR0 = ch;
+#else
+ /* m8,16,32,169,8515,8535,163 */
+ while (!(UCSRA & _BV(UDRE)));
+ UDR = ch;
+#endif
+}
+
+
+char getch(void)
+{
+#ifdef __AVR_ATmega128__
+ if(bootuart == 1) {
+ while(!(UCSR0A & _BV(RXC0)));
+ return UDR0;
+ }
+ else if(bootuart == 2) {
+ while(!(UCSR1A & _BV(RXC1)));
+ return UDR1;
+ }
+ return 0;
+#elif defined __AVR_ATmega168__
+ uint32_t count = 0;
+ while(!(UCSR0A & _BV(RXC0))){
+ /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
+ /* HACKME:: here is a good place to count times*/
+ count++;
+ if (count > MAX_TIME_COUNT)
+ app_start();
+ }
+ return UDR0;
+#else
+ /* m8,16,32,169,8515,8535,163 */
+ uint32_t count = 0;
+ while(!(UCSRA & _BV(RXC))){
+ /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
+ /* HACKME:: here is a good place to count times*/
+ count++;
+ if (count > MAX_TIME_COUNT)
+ app_start();
+ }
+ return UDR;
+#endif
+}
+
+
+void getNch(uint8_t count)
+{
+ uint8_t i;
+ for(i=0;i<count;i++) {
+#ifdef __AVR_ATmega128__
+ if(bootuart == 1) {
+ while(!(UCSR0A & _BV(RXC0)));
+ UDR0;
+ }
+ else if(bootuart == 2) {
+ while(!(UCSR1A & _BV(RXC1)));
+ UDR1;
+ }
+#elif defined __AVR_ATmega168__
+ while(!(UCSR0A & _BV(RXC0)));
+ UDR0;
+#else
+ /* m8,16,32,169,8515,8535,163 */
+ /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
+ //while(!(UCSRA & _BV(RXC)));
+ //UDR;
+ uint8_t i;
+ for(i=0;i<count;i++) {
+ getch(); // need to handle time out
+ }
+#endif
+ }
+}
+
+
+void byte_response(uint8_t val)
+{
+ if (getch() == ' ') {
+ putch(0x14);
+ putch(val);
+ putch(0x10);
+ } else {
+ if (++error_count == MAX_ERROR_COUNT)
+ app_start();
+ }
+}
+
+
+void nothing_response(void)
+{
+ if (getch() == ' ') {
+ putch(0x14);
+ putch(0x10);
+ } else {
+ if (++error_count == MAX_ERROR_COUNT)
+ app_start();
+ }
+}
+
+void flash_led(uint8_t count)
+{
+ /* flash onboard LED three times to signal entering of bootloader */
+ /* l needs to be volatile or the delay loops below might get
+ optimized away if compiling with optimizations (DAM). */
+ volatile uint32_t l;
+
+ if (count == 0) {
+ count = 3;
+ }
+
+ for (i = 0; i < count; ++i) {
+ LED_PORT |= _BV(LED);
+ for(l = 0; l < (F_CPU / 1000); ++l);
+ LED_PORT &= ~_BV(LED);
+ for(l = 0; l < (F_CPU / 1000); ++l);
+ }
+}
+
+
+/* end of file ATmegaBOOT.c */
diff --git a/bootloaders/atmega168/ATmegaBOOT_168_diecimila.hex b/bootloaders/atmega168/ATmegaBOOT_168_diecimila.hex
new file mode 100644
index 0000000..26bf3e5
--- /dev/null
+++ b/bootloaders/atmega168/ATmegaBOOT_168_diecimila.hex
@@ -0,0 +1,117 @@
+:103800000C94341C0C944F1C0C944F1C0C944F1CA7
+:103810000C944F1C0C944F1C0C944F1C0C944F1C7C
+:103820000C944F1C0C944F1C0C944F1C0C944F1C6C
+:103830000C944F1C0C944F1C0C944F1C0C944F1C5C
+:103840000C944F1C0C944F1C0C944F1C0C944F1C4C
+:103850000C944F1C0C944F1C0C944F1C0C944F1C3C
+:103860000C944F1C0C944F1C11241FBECFEFD4E0BE
+:10387000DEBFCDBF11E0A0E0B1E0E8E1FFE302C0B0
+:1038800005900D92A230B107D9F712E0A2E0B1E0A5
+:1038900001C01D92AD30B107E1F70C94311D0C94BD
+:1038A000001CCF93DF93CDB7DEB724970FB6F89403
+:1038B000DEBF0FBECDBF382F882309F433E010924E
+:1038C0000A02332309F44BC020E02D9A19821A8290
+:1038D0001B821C8289819A81AB81BC8180589E4366
+:1038E000A040B040A0F489819A81AB81BC8101964F
+:1038F000A11DB11D89839A83AB83BC8389819A8181
+:10390000AB81BC8180589E43A040B04060F32D98AD
+:1039100019821A821B821C8289819A81AB81BC81A7
+:1039200080589E43A040B040A0F489819A81AB8129
+:10393000BC810196A11DB11D89839A83AB83BC8391
+:1039400089819A81AB81BC8180589E43A040B04060
+:1039500060F32F5F231708F4B8CF20930A02249650
+:103960000FB6F894DEBF0FBECDBFDF91CF910895A3
+:10397000EF92FF920F931F93EE24FF248701809113
+:10398000C00087FD17C00894E11CF11C011D111D2A
+:1039900081E4E81682E4F8068FE0080780E0180763
+:1039A00070F3E0910201F091030109958091C0004C
+:1039B00087FFE9CF8091C600992787FD90951F91D9
+:1039C0000F91FF90EF900895982F8091C00085FF90
+:1039D000FCCF9093C60008950E94B81C803271F00D
+:1039E000809104018F5F80930401853009F0089570
+:1039F000E0910201F09103010995089584E10E948C
+:103A0000E41C80E10E94E41C08951F93182F0E947B
+:103A1000B81C803269F0809104018F5F80930401AB
+:103A2000853079F4E0910201F0910301099509C014
+:103A300084E10E94E41C812F0E94E41C80E10E942A
+:103A4000E41C1F910895282F882351F090E0809165
+:103A5000C00087FFFCCF8091C6009F5F2917B9F790
+:103A60000895CFEFD4E0DEBFCDBF000083E38093A5
+:103A7000C4001092C50088E18093C10086E0809365
+:103A8000C2005098589A259A81E00E94511C0E94C9
+:103A9000B81C8033B1F18133B9F1803409F454C0DA
+:103AA000813409F45AC0823409F469C0853409F4B8
+:103AB0006CC0803531F1813521F1823511F18535C8
+:103AC00009F4B2C0863509F4BAC0843609F463C07B
+:103AD000843709F4BBC0853709F40EC1863709F471
+:103AE0004AC0809104018F5F80930401853079F68C
+:103AF000E0910201F091030109950E94B81C803306
+:103B000051F60E94EC1CC3CF0E94B81C803249F7CA
+:103B100084E10E94E41C81E40E94E41C86E50E948A
+:103B2000E41C82E50E94E41C80E20E94E41C89E41B
+:103B30000E94E41C83E50E94E41C80E50E94E41CD2
+:103B400080E10E94E41CA3CF0E94B81C8638C8F212
+:103B50000E94B81C0E94EC1C9ACF0E94B81C8038AE
+:103B600009F4F7C0813809F4F8C0823809F4F9C0C3
+:103B7000883909F4BDC080E00E94051D88CF84E12A
+:103B80000E94231D0E94EC1C82CF85E00E94231D11
+:103B90000E94EC1C7CCF0E94B81C809309020E94FA
+:103BA000B81C8093080280910C028E7F80930C02D7
+:103BB0000E94B81C853409F4C6C080910802909117
+:103BC0000902892B09F0ADC00E94B81C803209F0AF
+:103BD00088CF80910C0280FFC8C08091080290912C
+:103BE00009020097D1F02091060130910701E8E029
+:103BF000F1E0AC014E0F5F1FF999FECF32BD21BD40
+:103C0000819180BDFA9AF99A2F5F3F4F4E175F0757
+:103C100099F7309307012093060184E10E94E41C88
+:103C200080E10E94E41C33CF0E94B81C80930601FF
+:103C30000E94B81C809307010E94EC1C28CF84E0EE
+:103C40000E94231D80E00E94051D21CF0E94B81C08
+:103C5000809309020E94B81C809308020E94B81C3D
+:103C6000853409F4F4C080910C028E7F80930C029D
+:103C70008091060190910701880F991F9093070189
+:103C8000809306010E94B81C803209F000CF84E1C5
+:103C90000E94E41C2091080230910902211531058F
+:103CA00019F1C0E0D0E0E0910601F09107018091A8
+:103CB0000C0280FFC4C0F999FECFF2BDE1BDF89AB5
+:103CC00080B50E94E41CE0910601F0910701319655
+:103CD000F0930701E0930601209108023091090258
+:103CE0002196C217D30718F380E10E94E41CCFCEBF
+:103CF00083E00E94051DCBCE0E94B81C803209F0E3
+:103D0000F0CE84E10E94E41C8EE10E94E41C84E970
+:103D10000E94E41C86E00E94E41C80E10E94E41CF6
+:103D2000B6CEC0E0D0E008E011E00E94B81CF80177
+:103D300081938F0121968091080290910902C81702
+:103D4000D90798F341CF80910C02816080930C02D7
+:103D500034CF82E00E94051D9ACE81E00E94051DAD
+:103D600096CE80E10E94051D92CE8091070187FDCD
+:103D700080C010920B028091060190910701880F7C
+:103D8000991F90930701809306018091080280FF9C
+:103D900009C080910802909109020196909309024E
+:103DA00080930802F894F999FECF1127E09106015B
+:103DB000F0910701C8E0D1E08091080290910902DA
+:103DC000103091F40091570001700130D9F303E0F5
+:103DD00000935700E8950091570001700130D9F326
+:103DE00001E100935700E895099019900091570060
+:103DF00001700130D9F301E000935700E895139565
+:103E0000103498F011270091570001700130D9F358
+:103E100005E000935700E8950091570001700130CC
+:103E2000D9F301E100935700E8953296029709F023
+:103E3000C7CF103011F00296E5CF1124EECE81FFEE
+:103E40000CC03196F0930701E093060149CF8091B1
+:103E50000C02816080930C0215CF84910E94E41CB7
+:103E60002091080230910902E0910601F0910701CA
+:103E7000E8CF81E080930B027ECF0F931F930E94C7
+:103E8000B81C182F0E94E41C0E94B81C082F0E9426
+:103E9000E41C11362CF0175501363CF0075508C0CC
+:103EA0001033D4F310530136CCF700330CF0005329
+:103EB0001295107F100F812F992787FD90951F91E4
+:103EC0000F9108951F93282F992787FD9095807F44
+:103ED00090709595879595958795959587959595E6
+:103EE00087958A304CF0982F995A822F8F708A309C
+:103EF0004CF0182F195A08C0982F905D822F8F70A0
+:103F00008A30BCF7182F105D892F0E94E41C812F86
+:083F10000E94E41C1F910895BA
+:023F1800800027
+:0400000300003800C1
+:00000001FF
diff --git a/bootloaders/atmega168/ATmegaBOOT_168_ng.hex b/bootloaders/atmega168/ATmegaBOOT_168_ng.hex
new file mode 100644
index 0000000..0cb6879
--- /dev/null
+++ b/bootloaders/atmega168/ATmegaBOOT_168_ng.hex
@@ -0,0 +1,117 @@
+:103800000C94341C0C944F1C0C944F1C0C944F1CA7
+:103810000C944F1C0C944F1C0C944F1C0C944F1C7C
+:103820000C944F1C0C944F1C0C944F1C0C944F1C6C
+:103830000C944F1C0C944F1C0C944F1C0C944F1C5C
+:103840000C944F1C0C944F1C0C944F1C0C944F1C4C
+:103850000C944F1C0C944F1C0C944F1C0C944F1C3C
+:103860000C944F1C0C944F1C11241FBECFEFD4E0BE
+:10387000DEBFCDBF11E0A0E0B1E0E8E1FFE302C0B0
+:1038800005900D92A230B107D9F712E0A2E0B1E0A5
+:1038900001C01D92AD30B107E1F70C94311D0C94BD
+:1038A000001CCF93DF93CDB7DEB724970FB6F89403
+:1038B000DEBF0FBECDBF382F882309F433E010924E
+:1038C0000A02332309F44BC020E02D9A19821A8290
+:1038D0001B821C8289819A81AB81BC8180589E4366
+:1038E000A040B040A0F489819A81AB81BC8101964F
+:1038F000A11DB11D89839A83AB83BC8389819A8181
+:10390000AB81BC8180589E43A040B04060F32D98AD
+:1039100019821A821B821C8289819A81AB81BC81A7
+:1039200080589E43A040B040A0F489819A81AB8129
+:10393000BC810196A11DB11D89839A83AB83BC8391
+:1039400089819A81AB81BC8180589E43A040B04060
+:1039500060F32F5F231708F4B8CF20930A02249650
+:103960000FB6F894DEBF0FBECDBFDF91CF910895A3
+:10397000EF92FF920F931F93EE24FF248701809113
+:10398000C00087FD17C00894E11CF11C011D111D2A
+:1039900081E0E81682E1F8068AE7080780E0180768
+:1039A00070F3E0910201F091030109958091C0004C
+:1039B00087FFE9CF8091C600992787FD90951F91D9
+:1039C0000F91FF90EF900895982F8091C00085FF90
+:1039D000FCCF9093C60008950E94B81C803271F00D
+:1039E000809104018F5F80930401853009F0089570
+:1039F000E0910201F09103010995089584E10E948C
+:103A0000E41C80E10E94E41C08951F93182F0E947B
+:103A1000B81C803269F0809104018F5F80930401AB
+:103A2000853079F4E0910201F0910301099509C014
+:103A300084E10E94E41C812F0E94E41C80E10E942A
+:103A4000E41C1F910895282F882351F090E0809165
+:103A5000C00087FFFCCF8091C6009F5F2917B9F790
+:103A60000895CFEFD4E0DEBFCDBF000083E38093A5
+:103A7000C4001092C50088E18093C10086E0809365
+:103A8000C2005098589A259A83E00E94511C0E94C7
+:103A9000B81C8033B1F18133B9F1803409F454C0DA
+:103AA000813409F45AC0823409F469C0853409F4B8
+:103AB0006CC0803531F1813521F1823511F18535C8
+:103AC00009F4B2C0863509F4BAC0843609F463C07B
+:103AD000843709F4BBC0853709F40EC1863709F471
+:103AE0004AC0809104018F5F80930401853079F68C
+:103AF000E0910201F091030109950E94B81C803306
+:103B000051F60E94EC1CC3CF0E94B81C803249F7CA
+:103B100084E10E94E41C81E40E94E41C86E50E948A
+:103B2000E41C82E50E94E41C80E20E94E41C89E41B
+:103B30000E94E41C83E50E94E41C80E50E94E41CD2
+:103B400080E10E94E41CA3CF0E94B81C8638C8F212
+:103B50000E94B81C0E94EC1C9ACF0E94B81C8038AE
+:103B600009F4F7C0813809F4F8C0823809F4F9C0C3
+:103B7000883909F4BDC080E00E94051D88CF84E12A
+:103B80000E94231D0E94EC1C82CF85E00E94231D11
+:103B90000E94EC1C7CCF0E94B81C809309020E94FA
+:103BA000B81C8093080280910C028E7F80930C02D7
+:103BB0000E94B81C853409F4C6C080910802909117
+:103BC0000902892B09F0ADC00E94B81C803209F0AF
+:103BD00088CF80910C0280FFC8C08091080290912C
+:103BE00009020097D1F02091060130910701E8E029
+:103BF000F1E0AC014E0F5F1FF999FECF32BD21BD40
+:103C0000819180BDFA9AF99A2F5F3F4F4E175F0757
+:103C100099F7309307012093060184E10E94E41C88
+:103C200080E10E94E41C33CF0E94B81C80930601FF
+:103C30000E94B81C809307010E94EC1C28CF84E0EE
+:103C40000E94231D80E00E94051D21CF0E94B81C08
+:103C5000809309020E94B81C809308020E94B81C3D
+:103C6000853409F4F4C080910C028E7F80930C029D
+:103C70008091060190910701880F991F9093070189
+:103C8000809306010E94B81C803209F000CF84E1C5
+:103C90000E94E41C2091080230910902211531058F
+:103CA00019F1C0E0D0E0E0910601F09107018091A8
+:103CB0000C0280FFC4C0F999FECFF2BDE1BDF89AB5
+:103CC00080B50E94E41CE0910601F0910701319655
+:103CD000F0930701E0930601209108023091090258
+:103CE0002196C217D30718F380E10E94E41CCFCEBF
+:103CF00083E00E94051DCBCE0E94B81C803209F0E3
+:103D0000F0CE84E10E94E41C8EE10E94E41C84E970
+:103D10000E94E41C86E00E94E41C80E10E94E41CF6
+:103D2000B6CEC0E0D0E008E011E00E94B81CF80177
+:103D300081938F0121968091080290910902C81702
+:103D4000D90798F341CF80910C02816080930C02D7
+:103D500034CF82E00E94051D9ACE81E00E94051DAD
+:103D600096CE80E10E94051D92CE8091070187FDCD
+:103D700080C010920B028091060190910701880F7C
+:103D8000991F90930701809306018091080280FF9C
+:103D900009C080910802909109020196909309024E
+:103DA00080930802F894F999FECF1127E09106015B
+:103DB000F0910701C8E0D1E08091080290910902DA
+:103DC000103091F40091570001700130D9F303E0F5
+:103DD00000935700E8950091570001700130D9F326
+:103DE00001E100935700E895099019900091570060
+:103DF00001700130D9F301E000935700E895139565
+:103E0000103498F011270091570001700130D9F358
+:103E100005E000935700E8950091570001700130CC
+:103E2000D9F301E100935700E8953296029709F023
+:103E3000C7CF103011F00296E5CF1124EECE81FFEE
+:103E40000CC03196F0930701E093060149CF8091B1
+:103E50000C02816080930C0215CF84910E94E41CB7
+:103E60002091080230910902E0910601F0910701CA
+:103E7000E8CF81E080930B027ECF0F931F930E94C7
+:103E8000B81C182F0E94E41C0E94B81C082F0E9426
+:103E9000E41C11362CF0175501363CF0075508C0CC
+:103EA0001033D4F310530136CCF700330CF0005329
+:103EB0001295107F100F812F992787FD90951F91E4
+:103EC0000F9108951F93282F992787FD9095807F44
+:103ED00090709595879595958795959587959595E6
+:103EE00087958A304CF0982F995A822F8F708A309C
+:103EF0004CF0182F195A08C0982F905D822F8F70A0
+:103F00008A30BCF7182F105D892F0E94E41C812F86
+:083F10000E94E41C1F910895BA
+:023F1800800027
+:0400000300003800C1
+:00000001FF
diff --git a/bootloaders/atmega168/Makefile b/bootloaders/atmega168/Makefile
new file mode 100755
index 0000000..669879b
--- /dev/null
+++ b/bootloaders/atmega168/Makefile
@@ -0,0 +1,92 @@
+# Makefile for ATmegaBOOT
+# E.Lins, 18.7.2005
+# $Id$
+
+# Instructions
+#
+# To build the bootloader for the Diecimila:
+# make diecimila
+#
+# To build the bootloader for the NG/Mini:
+# make ng
+#
+# To burn the bootloader:
+# make TARGET=diecimila isp
+# make TARGET=ng isp
+
+# program name should not be changed...
+PROGRAM = ATmegaBOOT_168
+
+# enter the target CPU frequency
+AVR_FREQ = 16000000L
+
+# enter the parameters for the avrdude isp tool
+ISPTOOL = stk500v2
+ISPPORT = usb
+ISPSPEED = -b 115200
+
+MCU_TARGET = atmega168
+LDSECTION = --section-start=.text=0x3800
+
+# the efuse should really be 0xf8; since, however, only the lower
+# three bits of that byte are used on the atmega168, avrdude gets
+# confused if you specify 1's for the higher bits, see:
+# http://tinker.it/now/2007/02/24/the-tale-of-avrdude-atmega168-and-extended-bits-fuses/
+#
+# similarly, the lock bits should be 0xff instead of 0x3f (to
+# unlock the bootloader section) and 0xcf instead of 0x0f (to
+# lock it), but since the high two bits of the lock byte are
+# unused, avrdude would get confused.
+ISPFUSES = avrdude -c $(ISPTOOL) -p m168 -P $(ISPPORT) $(ISPSPEED) -e -u -U lock:w:0x3f:m -U efuse:w:0x00:m -U hfuse:w:0xdd:m -U lfuse:w:0xff:m
+ISPFLASH = avrdude -c $(ISPTOOL) -p m168 -P $(ISPPORT) $(ISPSPEED) -U flash:w:$(PROGRAM)_$(TARGET).hex -U lock:w:0x0f:m
+
+
+OBJ = $(PROGRAM).o
+OPTIMIZE = -O2
+
+DEFS =
+LIBS =
+
+CC = avr-gcc
+
+
+# Override is only needed by avr-lib build system.
+
+override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) -DF_CPU=$(AVR_FREQ) $(DEFS)
+override LDFLAGS = -Wl,$(LDSECTION)
+#override LDFLAGS = -Wl,-Map,$(PROGRAM).map,$(LDSECTION)
+
+OBJCOPY = avr-objcopy
+OBJDUMP = avr-objdump
+
+all:
+
+diecimila: TARGET = diecimila
+diecimila: CFLAGS += '-DMAX_TIME_COUNT=F_CPU>>4' '-DNUM_LED_FLASHES=1'
+diecimila: $(PROGRAM)_diecimila.hex
+
+ng: TARGET = ng
+ng: CFLAGS += '-DMAX_TIME_COUNT=F_CPU>>1' '-DNUM_LED_FLASHES=3'
+ng: $(PROGRAM)_ng.hex
+
+isp: $(PROGRAM)_$(TARGET).hex
+ $(ISPFUSES)
+ $(ISPFLASH)
+
+%.elf: $(OBJ)
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
+
+clean:
+ rm -rf *.o *.elf *.lst *.map *.sym *.lss *.eep *.srec *.bin *.hex
+
+%.lst: %.elf
+ $(OBJDUMP) -h -S $< > $@
+
+%.hex: %.elf
+ $(OBJCOPY) -j .text -j .data -O ihex $< $@
+
+%.srec: %.elf
+ $(OBJCOPY) -j .text -j .data -O srec $< $@
+
+%.bin: %.elf
+ $(OBJCOPY) -j .text -j .data -O binary $< $@
diff --git a/bootloaders/atmega8/ATmegaBOOT.c b/bootloaders/atmega8/ATmegaBOOT.c
new file mode 100755
index 0000000..17977e6
--- /dev/null
+++ b/bootloaders/atmega8/ATmegaBOOT.c
@@ -0,0 +1,507 @@
+/**********************************************************/
+/* Serial Bootloader for Atmel mega8 AVR Controller */
+/* */
+/* ATmegaBOOT.c */
+/* */
+/* Copyright (c) 2003, Jason P. Kyle */
+/* */
+/* Hacked by DojoCorp - ZGZ - MMX - IVR */
+/* Hacked by David A. Mellis */
+/* */
+/* This program is free software; you can redistribute it */
+/* and/or modify it under the terms of the GNU General */
+/* Public License as published by the Free Software */
+/* Foundation; either version 2 of the License, or */
+/* (at your option) any later version. */
+/* */
+/* This program is distributed in the hope that it will */
+/* be useful, but WITHOUT ANY WARRANTY; without even the */
+/* implied warranty of MERCHANTABILITY or FITNESS FOR A */
+/* PARTICULAR PURPOSE. See the GNU General Public */
+/* License for more details. */
+/* */
+/* You should have received a copy of the GNU General */
+/* Public License along with this program; if not, write */
+/* to the Free Software Foundation, Inc., */
+/* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+/* */
+/* Licence can be viewed at */
+/* http://www.fsf.org/licenses/gpl.txt */
+/* */
+/* Target = Atmel AVR m8 */
+/**********************************************************/
+
+#include <inttypes.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/eeprom.h>
+#include <avr/interrupt.h>
+#include <avr/delay.h>
+
+//#define F_CPU 16000000
+
+/* We, Malmoitians, like slow interaction
+ * therefore the slow baud rate ;-)
+ */
+//#define BAUD_RATE 9600
+
+/* 6.000.000 is more or less 8 seconds at the
+ * speed configured here
+ */
+//#define MAX_TIME_COUNT 6000000
+#define MAX_TIME_COUNT (F_CPU>>1)
+///#define MAX_TIME_COUNT_MORATORY 1600000
+
+/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
+#define HW_VER 0x02
+#define SW_MAJOR 0x01
+#define SW_MINOR 0x12
+
+// AVR-GCC compiler compatibility
+// avr-gcc compiler v3.1.x and older doesn't support outb() and inb()
+// if necessary, convert outb and inb to outp and inp
+#ifndef outb
+ #define outb(sfr,val) (_SFR_BYTE(sfr) = (val))
+#endif
+#ifndef inb
+ #define inb(sfr) _SFR_BYTE(sfr)
+#endif
+
+/* defines for future compatibility */
+#ifndef cbi
+ #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
+#endif
+#ifndef sbi
+ #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
+#endif
+
+/* Adjust to suit whatever pin your hardware uses to enter the bootloader */
+#define eeprom_rb(addr) eeprom_read_byte ((uint8_t *)(addr))
+#define eeprom_rw(addr) eeprom_read_word ((uint16_t *)(addr))
+#define eeprom_wb(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))
+
+/* Onboard LED is connected to pin PB5 */
+#define LED_DDR DDRB
+#define LED_PORT PORTB
+#define LED_PIN PINB
+#define LED PINB5
+
+
+#define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
+#define SIG2 0x93
+#define SIG3 0x07
+#define PAGE_SIZE 0x20U //32 words
+
+
+void putch(char);
+char getch(void);
+void getNch(uint8_t);
+void byte_response(uint8_t);
+void nothing_response(void);
+
+union address_union {
+ uint16_t word;
+ uint8_t byte[2];
+} address;
+
+union length_union {
+ uint16_t word;
+ uint8_t byte[2];
+} length;
+
+struct flags_struct {
+ unsigned eeprom : 1;
+ unsigned rampz : 1;
+} flags;
+
+uint8_t buff[256];
+//uint8_t address_high;
+
+uint8_t pagesz=0x80;
+
+uint8_t i;
+//uint8_t bootuart0=0,bootuart1=0;
+
+
+void (*app_start)(void) = 0x0000;
+
+int main(void)
+{
+ uint8_t ch,ch2;
+ uint16_t w;
+
+ //cbi(BL_DDR,BL);
+ //sbi(BL_PORT,BL);
+
+ asm volatile("nop\n\t");
+
+ /* check if flash is programmed already, if not start bootloader anyway */
+ //if(pgm_read_byte_near(0x0000) != 0xFF) {
+
+ /* check if bootloader pin is set low */
+ //if(bit_is_set(BL_PIN,BL)) app_start();
+ //}
+
+ /* initialize UART(s) depending on CPU defined */
+ /* m8 */
+ UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
+ UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
+ UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & Tx
+ UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
+
+ //UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ //UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ //UCSRA = 0x00;
+ //UCSRC = 0x86;
+ //UCSRB = _BV(TXEN)|_BV(RXEN);
+
+
+ /* this was giving uisp problems, so I removed it; without it, the boot
+ works on with uisp and avrdude on the mac (at least). */
+ //putch('\0');
+
+ //uint32_t l;
+ //uint32_t time_count;
+ //time_count=0;
+
+ /* set LED pin as output */
+ sbi(LED_DDR,LED);
+ for (i = 0; i < 16; i++) {
+ outb(LED_PORT, inb(LED_PORT) ^ _BV(LED));
+ _delay_loop_2(0);
+ }
+
+ //for (l=0; l<40000000; l++)
+ //outb(LED_PORT, inb(LED_PORT) ^= _BV(LED));
+
+ /* flash onboard LED three times to signal entering of bootloader */
+ //for(i=0; i<3; ++i) {
+ //for(l=0; l<40000000; ++l);
+ //sbi(LED_PORT,LED);
+ //for(l=0; l<40000000; ++l);
+ //cbi(LED_PORT,LED);
+ //}
+
+ /* see comment at previous call to putch() */
+ //putch('\0'); // this line is needed for the synchronization of the programmer
+
+ /* forever */
+ for (;;) {
+ //if((inb(UCSRA) & _BV(RXC))){
+ /* get character from UART */
+ ch = getch();
+
+ /* A bunch of if...else if... gives smaller code than switch...case ! */
+
+ /* Hello is anyone home ? */
+ if(ch=='0') {
+ nothing_response();
+ }
+
+ /* Request programmer ID */
+ /* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
+ /* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
+ else if(ch=='1') {
+ if (getch() == ' ') {
+ putch(0x14);
+ putch('A');
+ putch('V');
+ putch('R');
+ putch(' ');
+ putch('I');
+ putch('S');
+ putch('P');
+ putch(0x10);
+ }
+ }
+
+ /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
+ else if(ch=='@') {
+ ch2 = getch();
+ if (ch2>0x85) getch();
+ nothing_response();
+ }
+
+ /* AVR ISP/STK500 board requests */
+ else if(ch=='A') {
+ ch2 = getch();
+ if(ch2==0x80) byte_response(HW_VER); // Hardware version
+ else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
+ else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
+ //else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
+ else byte_response(0x00); // Covers various unnecessary responses we don't care about
+ }
+
+ /* Device Parameters DON'T CARE, DEVICE IS FIXED */
+ else if(ch=='B') {
+ getNch(20);
+ nothing_response();
+ }
+
+ /* Parallel programming stuff DON'T CARE */
+ else if(ch=='E') {
+ getNch(5);
+ nothing_response();
+ }
+
+ /* Enter programming mode */
+ else if(ch=='P') {
+ nothing_response();
+ // FIXME: modified only here by DojoCorp, Mumbai, India, 20050626
+ //time_count=0; // exted the delay once entered prog.mode
+ }
+
+ /* Leave programming mode */
+ else if(ch=='Q') {
+ nothing_response();
+ //time_count=MAX_TIME_COUNT_MORATORY; // once the programming is done,
+ // we should start the application
+ // but uisp has problems with this,
+ // therefore we just change the times
+ // and give the programmer 1 sec to react
+ }
+
+ /* Erase device, don't care as we will erase one page at a time anyway. */
+ else if(ch=='R') {
+ nothing_response();
+ }
+
+ /* Set address, little endian. EEPROM in bytes, FLASH in words */
+ /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
+ /* This might explain why little endian was used here, big endian used everywhere else. */
+ else if(ch=='U') {
+ address.byte[0] = getch();
+ address.byte[1] = getch();
+ nothing_response();
+ }
+
+ /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
+ else if(ch=='V') {
+ getNch(4);
+ byte_response(0x00);
+ }
+
+ /* Write memory, length is big endian and is in bytes */
+ else if(ch=='d') {
+ length.byte[1] = getch();
+ length.byte[0] = getch();
+ flags.eeprom = 0;
+ if (getch() == 'E') flags.eeprom = 1;
+ for (w=0;w<length.word;w++) {
+ buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
+ }
+ if (getch() == ' ') {
+ if (flags.eeprom) { //Write to EEPROM one byte at a time
+ for(w=0;w<length.word;w++) {
+ eeprom_wb(address.word,buff[w]);
+ address.word++;
+ }
+ } else { //Write to FLASH one page at a time
+ //if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
+ //else address_high = 0x00;
+
+ //address.word = address.word << 1; //address * 2 -> byte location
+ //if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
+ cli(); //Disable interrupts, just to be sure
+ while(bit_is_set(EECR,EEWE)); //Wait for previous EEPROM writes to complete
+ asm volatile(
+ "clr r17 \n\t" //page_word_count
+ "lds r30,address \n\t" //Address of FLASH location (in words)
+ "lds r31,address+1 \n\t"
+ "lsl r30 \n\t" //address * 2 -> byte location
+ "rol r31 \n\t"
+ "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
+ "ldi r29,hi8(buff) \n\t"
+ "lds r24,length \n\t" //Length of data to be written (in bytes)
+ "lds r25,length+1 \n\t"
+ "sbrs r24,0 \n\t" //Even up an odd number of bytes
+ "rjmp length_loop \n\t"
+ "adiw r24,1 \n\t"
+ "length_loop: \n\t" //Main loop, repeat for number of words in block
+ "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
+ "brne no_page_erase \n\t"
+ "rcall wait_spm \n\t"
+// "wait_spm1: \n\t"
+// "lds r16,%0 \n\t" //Wait for previous spm to complete
+// "andi r16,1 \n\t"
+// "cpi r16,1 \n\t"
+// "breq wait_spm1 \n\t"
+ "ldi r16,0x03 \n\t" //Erase page pointed to by Z
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+ "rcall wait_spm \n\t"
+// "wait_spm2: \n\t"
+// "lds r16,%0 \n\t" //Wait for previous spm to complete
+// "andi r16,1 \n\t"
+// "cpi r16,1 \n\t"
+// "breq wait_spm2 \n\t"
+ "ldi r16,0x11 \n\t" //Re-enable RWW section
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+ "no_page_erase: \n\t"
+ "ld r0,Y+ \n\t" //Write 2 bytes into page buffer
+ "ld r1,Y+ \n\t"
+
+ "rcall wait_spm \n\t"
+// "wait_spm3: \n\t"
+// "lds r16,%0 \n\t" //Wait for previous spm to complete
+// "andi r16,1 \n\t"
+// "cpi r16,1 \n\t"
+// "breq wait_spm3 \n\t"
+ "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+
+ "inc r17 \n\t" //page_word_count++
+ "cpi r17,%1 \n\t"
+ "brlo same_page \n\t" //Still same page in FLASH
+ "write_page: \n\t"
+ "clr r17 \n\t" //New page, write current one first
+ "rcall wait_spm \n\t"
+// "wait_spm4: \n\t"
+// "lds r16,%0 \n\t" //Wait for previous spm to complete
+// "andi r16,1 \n\t"
+// "cpi r16,1 \n\t"
+// "breq wait_spm4 \n\t"
+ "ldi r16,0x05 \n\t" //Write page pointed to by Z
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+ "rcall wait_spm \n\t"
+// "wait_spm5: \n\t"
+// "lds r16,%0 \n\t" //Wait for previous spm to complete
+// "andi r16,1 \n\t"
+// "cpi r16,1 \n\t"
+// "breq wait_spm5 \n\t"
+ "ldi r16,0x11 \n\t" //Re-enable RWW section
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+ "same_page: \n\t"
+ "adiw r30,2 \n\t" //Next word in FLASH
+ "sbiw r24,2 \n\t" //length-2
+ "breq final_write \n\t" //Finished
+ "rjmp length_loop \n\t"
+
+ "wait_spm: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm \n\t"
+ "ret \n\t"
+
+ "final_write: \n\t"
+ "cpi r17,0 \n\t"
+ "breq block_done \n\t"
+ "adiw r24,2 \n\t" //length+2, fool above check on length after short page write
+ "rjmp write_page \n\t"
+ "block_done: \n\t"
+ "clr __zero_reg__ \n\t" //restore zero register
+ : "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31");
+
+ /* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
+ /* exit the bootloader without a power cycle anyhow */
+ }
+ putch(0x14);
+ putch(0x10);
+ }
+ }
+
+ /* Read memory block mode, length is big endian. */
+ else if(ch=='t') {
+ length.byte[1] = getch();
+ length.byte[0] = getch();
+ if (getch() == 'E') flags.eeprom = 1;
+ else {
+ flags.eeprom = 0;
+ address.word = address.word << 1; // address * 2 -> byte location
+ }
+ if (getch() == ' ') { // Command terminator
+ putch(0x14);
+ for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
+ if (flags.eeprom) { // Byte access EEPROM read
+ putch(eeprom_rb(address.word));
+ address.word++;
+ } else {
+ if (!flags.rampz) putch(pgm_read_byte_near(address.word));
+ address.word++;
+ }
+ }
+ putch(0x10);
+ }
+ }
+
+ /* Get device signature bytes */
+ else if(ch=='u') {
+ if (getch() == ' ') {
+ putch(0x14);
+ putch(SIG1);
+ putch(SIG2);
+ putch(SIG3);
+ putch(0x10);
+ }
+ }
+
+ /* Read oscillator calibration byte */
+ else if(ch=='v') {
+ byte_response(0x00);
+ }
+// } else {
+// time_count++;
+// if (time_count>=MAX_TIME_COUNT) {
+// app_start();
+// }
+// }
+ } /* end of forever loop */
+}
+
+void putch(char ch)
+{
+ /* m8 */
+ while (!(inb(UCSRA) & _BV(UDRE)));
+ outb(UDR,ch);
+}
+
+char getch(void)
+{
+ /* m8 */
+ uint32_t count = 0;
+ while(!(inb(UCSRA) & _BV(RXC))) {
+ /* HACKME:: here is a good place to count times*/
+ count++;
+ if (count > MAX_TIME_COUNT)
+ app_start();
+ }
+ return (inb(UDR));
+}
+
+void getNch(uint8_t count)
+{
+ uint8_t i;
+ for(i=0;i<count;i++) {
+ /* m8 */
+ //while(!(inb(UCSRA) & _BV(RXC)));
+ //inb(UDR);
+ getch(); // need to handle time out
+ }
+}
+
+void byte_response(uint8_t val)
+{
+ if (getch() == ' ') {
+ putch(0x14);
+ putch(val);
+ putch(0x10);
+ }
+}
+
+void nothing_response(void)
+{
+ if (getch() == ' ') {
+ putch(0x14);
+ putch(0x10);
+ }
+}
+
+/* end of file ATmegaBOOT.c */
+
+
+
diff --git a/bootloaders/atmega8/ATmegaBOOT.hex b/bootloaders/atmega8/ATmegaBOOT.hex
new file mode 100644
index 0000000..6190d48
--- /dev/null
+++ b/bootloaders/atmega8/ATmegaBOOT.hex
@@ -0,0 +1,66 @@
+:101C000012C02BC02AC029C028C027C026C025C0AA
+:101C100024C023C022C021C020C01FC01EC01DC0C0
+:101C20001CC01BC01AC011241FBECFE5D4E0DEBF0C
+:101C3000CDBF10E0A0E6B0E0E8EEFFE102C0059005
+:101C40000D92A236B107D9F711E0A2E6B0E001C0CB
+:101C50001D92AA36B107E1F74FC0D2CFEF92FF92A3
+:101C60000F931F93EE24FF24870113C00894E11CF7
+:101C7000F11C011D111D81E0E81682E1F8068AE7DA
+:101C8000080780E0180728F0E0916200F0916300F7
+:101C900009955F9BEBCF8CB1992787FD90951F919C
+:101CA0000F91FF90EF9008955D9BFECF8CB9089542
+:101CB000D5DF803221F484E1F7DF80E1F5DF08959C
+:101CC0001F93182FCBDF803231F484E1EDDF812FB9
+:101CD000EBDF80E1E9DF1F9108951F93CF93DF933E
+:101CE000182FC0E0D0E002C0B9DF2196C117E0F3A1
+:101CF000DF91CF911F910895CFE5D4E0DEBFCDBF36
+:101D0000000010BC83E389B988E18AB986E880BD08
+:101D1000BD9A1092680130E2E0E0F0E02FE088B375
+:101D2000832788BBCF010197F1F7215027FFF7CF19
+:101D300020E12093680192DF803381F1813399F4AF
+:101D40008DDF8032C1F784E1AFDF81E4ADDF86E56E
+:101D5000ABDF82E5A9DF80E2A7DF89E4A5DF83E5C9
+:101D6000A3DF80E5C7C0803429F478DF8638B0F07F
+:101D700075DF14C0813471F471DF803811F482E0B2
+:101D80001DC1813811F481E019C1823809F015C1F3
+:101D900082E114C1823421F484E19FDF89DFCBCF5B
+:101DA000853411F485E0F9CF8035C1F38135B1F385
+:101DB0008235A1F3853539F451DF809364004EDF1D
+:101DC00080936500EBCF863519F484E086DFF5C09B
+:101DD000843609F093C042DF809367013FDF809330
+:101DE0006601809169018E7F8093690137DF8534B8
+:101DF00029F480916901816080936901C0E0D0E09D
+:101E000006E610E005C02ADFF80181938F012196D4
+:101E10008091660190916701C817D907A0F31EDF72
+:101E2000803209F088CF8091690180FF1FC020E0D7
+:101E300030E0E6E6F0E012C0A0916400B0916500E9
+:101E40008191082EC5D08091640090916500019623
+:101E500090936500809364002F5F3F4F80916601EF
+:101E6000909167012817390738F343C0F894E19936
+:101E7000FECF1127E0916400F0916500EE0FFF1F87
+:101E8000C6E6D0E0809166019091670180FF01C0B5
+:101E90000196103051F422D003E000935700E895EA
+:101EA0001DD001E100935700E8950990199016D0D4
+:101EB00001E000935700E8951395103258F0112770
+:101EC0000DD005E000935700E89508D001E100939C
+:101ED0005700E8953296029739F0DBCF0091570012
+:101EE00001700130D9F30895103011F00296E7CF58
+:101EF000112484E1D9DE80E1D7DE1DCF843709F0DB
+:101F00004BC0ACDE80936701A9DE80936601A6DE3C
+:101F100090916901853421F49160909369010DC01D
+:101F20009E7F909369018091640090916500880F75
+:101F3000991F909365008093640090DE803209F0D1
+:101F4000FACE84E1B1DEC0E0D0E01EC0809169012C
+:101F500080FF07C0A0916400B091650031D0802D52
+:101F600008C081FD07C0E0916400F0916500E49134
+:101F70008E2F9ADE80916400909165000196909377
+:101F800065008093640021968091660190916701BD
+:101F9000C817D907D8F2AFCF853761F45FDE80323A
+:101FA00009F0C9CE84E180DE8EE17EDE83E97CDE4D
+:101FB00087E0A0CF863709F0BECE80E081DEBBCEC1
+:101FC000E199FECFBFBBAEBBE09A11960DB208956A
+:101FD000E199FECFBFBBAEBB0DBA11960FB6F89418
+:081FE000E29AE19A0FBE089598
+:021FE800800077
+:0400000300001C00DD
+:00000001FF
diff --git a/bootloaders/atmega8/Makefile b/bootloaders/atmega8/Makefile
new file mode 100644
index 0000000..8c0edd3
--- /dev/null
+++ b/bootloaders/atmega8/Makefile
@@ -0,0 +1,88 @@
+# Makefile for ATmegaBOOT
+# E.Lins, 2004-10-14
+
+# program name should not be changed...
+PROGRAM = ATmegaBOOT
+
+PRODUCT=atmega8
+
+# enter the parameters for the UISP isp tool
+ISPPARAMS = -dprog=stk500 -dserial=$(SERIAL) -dspeed=115200
+
+
+#DIRAVR = /usr/local/avr
+DIRAVRBIN = $(DIRAVR)/bin
+DIRAVRUTILS = $(DIRAVR)/utils/bin
+DIRINC = $(DIRAVR)/include
+DIRLIB = $(DIRAVR)/avr/lib
+
+
+MCU_TARGET = atmega8
+LDSECTION = --section-start=.text=0x1c00
+FUSE_L = 0xdf
+FUSE_H = 0xca
+ISPFUSES = $(DIRAVRBIN)/uisp -dpart=ATmega8 $(ISPPARAMS) --wr_fuse_l=$(FUSE_L) --wr_fuse_h=$(FUSE_H)
+ISPFLASH = $(DIRAVRBIN)/uisp -dpart=ATmega8 $(ISPPARAMS) --erase --upload if=$(PROGRAM).hex -v
+
+
+OBJ = $(PROGRAM).o
+OPTIMIZE = -Os
+
+DEFS = -DF_CPU=16000000 -DBAUD_RATE=19200
+LIBS =
+
+CC = $(DIRAVRBIN)/avr-gcc
+
+
+# Override is only needed by avr-lib build system.
+
+override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) -D$(PRODUCT) $(DEFS) -I$(DIRINC)
+override LDFLAGS = -Wl,-Map,$(PROGRAM).map,$(LDSECTION)
+
+OBJCOPY = $(DIRAVRBIN)/avr-objcopy
+OBJDUMP = $(DIRAVRBIN)/avr-objdump
+SIZE = $(DIRAVRBIN)/avr-size
+
+all: $(PROGRAM).elf lst text asm size
+
+isp: $(PROGRAM).hex
+ $(ISPFUSES)
+ $(ISPFLASH)
+
+$(PROGRAM).elf: $(OBJ)
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
+
+clean:
+ rm -rf *.s
+ rm -rf *.o *.elf
+ rm -rf *.lst *.map
+
+asm: $(PROGRAM).s
+
+%.s: %.c
+ $(CC) -S $(CFLAGS) -g1 $^
+
+lst: $(PROGRAM).lst
+
+%.lst: %.elf
+ $(OBJDUMP) -h -S $< > $@
+
+size: $(PROGRAM).hex
+ $(SIZE) $^
+
+# Rules for building the .text rom images
+
+text: hex bin srec
+
+hex: $(PROGRAM).hex
+bin: $(PROGRAM).bin
+srec: $(PROGRAM).srec
+
+%.hex: %.elf
+ $(OBJCOPY) -j .text -j .data -O ihex $< $@
+
+%.srec: %.elf
+ $(OBJCOPY) -j .text -j .data -O srec $< $@
+
+%.bin: %.elf
+ $(OBJCOPY) -j .text -j .data -O binary $< $@
diff --git a/bootloaders/bt/ATmegaBOOT_168.c b/bootloaders/bt/ATmegaBOOT_168.c
new file mode 100644
index 0000000..a85dc9a
--- /dev/null
+++ b/bootloaders/bt/ATmegaBOOT_168.c
@@ -0,0 +1,1032 @@
+/**********************************************************/
+/* Serial Bootloader for Atmel megaAVR Controllers */
+/* */
+/* tested with ATmega8, ATmega128 and ATmega168 */
+/* should work with other mega's, see code for details */
+/* */
+/* ATmegaBOOT.c */
+/* */
+/* build: 050815 */
+/* date : 15.08.2005 */
+/* */
+/* 20060802: hacked for Arduino by D. Cuartielles */
+/* based on a previous hack by D. Mellis */
+/* and D. Cuartielles */
+/* */
+/* Monitor and debug functions were added to the original */
+/* code by Dr. Erik Lins, chip45.com. (See below) */
+/* */
+/* Thanks to Karl Pitrich for fixing a bootloader pin */
+/* problem and more informative LED blinking! */
+/* */
+/* For the latest version see: */
+/* http://www.chip45.com/ */
+/* */
+/* ------------------------------------------------------ */
+/* */
+/* based on stk500boot.c */
+/* Copyright (c) 2003, Jason P. Kyle */
+/* All rights reserved. */
+/* see avr1.org for original file and information */
+/* */
+/* This program is free software; you can redistribute it */
+/* and/or modify it under the terms of the GNU General */
+/* Public License as published by the Free Software */
+/* Foundation; either version 2 of the License, or */
+/* (at your option) any later version. */
+/* */
+/* This program is distributed in the hope that it will */
+/* be useful, but WITHOUT ANY WARRANTY; without even the */
+/* implied warranty of MERCHANTABILITY or FITNESS FOR A */
+/* PARTICULAR PURPOSE. See the GNU General Public */
+/* License for more details. */
+/* */
+/* You should have received a copy of the GNU General */
+/* Public License along with this program; if not, write */
+/* to the Free Software Foundation, Inc., */
+/* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+/* */
+/* Licence can be viewed at */
+/* http://www.fsf.org/licenses/gpl.txt */
+/* */
+/* Target = Atmel AVR m128,m64,m32,m16,m8,m162,m163,m169, */
+/* m8515,m8535. ATmega161 has a very small boot block so */
+/* isn't supported. */
+/* */
+/* Tested with m128,m8,m163 - feel free to let me know */
+/* how/if it works for you. */
+/* */
+/**********************************************************/
+
+
+/* some includes */
+#include <inttypes.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/interrupt.h>
+#include <avr/wdt.h>
+
+
+#define set_output(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
+#define set_input(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
+
+
+#define high(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
+#define low(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
+
+
+
+
+/* the current avr-libc eeprom functions do not support the ATmega168 */
+/* own eeprom write/read functions are used instead */
+#ifndef __AVR_ATmega168__
+#include <avr/eeprom.h>
+#endif
+
+/* define F_CPU according to AVR_FREQ set in Makefile */
+/* Is there a better way to pass such a parameter from Makefile to source code ? */
+
+#define F_CPU 16000000L
+
+#include <util/delay.h>
+
+
+/* 20060803: hacked by DojoCorp */
+/* set the waiting time for the bootloader */
+#define MAX_TIME_COUNT (F_CPU>>1)
+
+/* set the UART baud rate */
+/* 20060803: hacked by DojoCorp */
+#define BAUD_RATE 115200
+
+
+/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
+/* never allow AVR Studio to do an update !!!! */
+#define HW_VER 0x02
+#define SW_MAJOR 0x01
+#define SW_MINOR 0x0f
+
+
+/* Adjust to suit whatever pin your hardware uses to enter the bootloader */
+/* ATmega128 has two UARTS so two pins are used to enter bootloader and select UART */
+/* BL0... means UART0, BL1... means UART1 */
+#ifdef __AVR_ATmega128__
+#define BL_DDR DDRF
+#define BL_PORT PORTF
+#define BL_PIN PINF
+#define BL0 PINF7
+#define BL1 PINF6
+#else
+/* other ATmegas have only one UART, so only one pin is defined to enter bootloader */
+#define BL_DDR DDRD
+#define BL_PORT PORTD
+#define BL_PIN PIND
+#define BL PIND6
+#endif
+
+
+/* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */
+/* if monitor functions are included, LED goes on after monitor was entered */
+#ifdef __AVR_ATmega128__
+/* Onboard LED is connected to pin PB7 (e.g. Crumb128, PROBOmega128, Savvy128) */
+#define LED_DDR DDRB
+#define LED_PORT PORTB
+#define LED_PIN PINB
+#define LED PINB7
+#else
+/* Onboard LED is connected to pin PB2 (e.g. Crumb8, Crumb168) */
+#define LED_DDR DDRB
+#define LED_PORT PORTB
+#define LED_PIN PINB
+/* 20060803: hacked by DojoCorp, LED pin is B5 in Arduino */
+/* #define LED PINB2 */
+#define LED PINB5
+#endif
+
+
+/* monitor functions will only be compiled when using ATmega128, due to bootblock size constraints */
+#ifdef __AVR_ATmega128__
+#define MONITOR
+#endif
+
+
+/* define various device id's */
+/* manufacturer byte is always the same */
+#define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
+
+#if defined __AVR_ATmega128__
+#define SIG2 0x97
+#define SIG3 0x02
+#define PAGE_SIZE 0x80U //128 words
+
+#elif defined __AVR_ATmega64__
+#define SIG2 0x96
+#define SIG3 0x02
+#define PAGE_SIZE 0x80U //128 words
+
+#elif defined __AVR_ATmega32__
+#define SIG2 0x95
+#define SIG3 0x02
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega16__
+#define SIG2 0x94
+#define SIG3 0x03
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega8__
+#define SIG2 0x93
+#define SIG3 0x07
+#define PAGE_SIZE 0x20U //32 words
+
+#elif defined __AVR_ATmega88__
+#define SIG2 0x93
+#define SIG3 0x0a
+#define PAGE_SIZE 0x20U //32 words
+
+#elif defined __AVR_ATmega168__
+#define SIG2 0x94
+#define SIG3 0x06
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega162__
+#define SIG2 0x94
+#define SIG3 0x04
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega163__
+#define SIG2 0x94
+#define SIG3 0x02
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega169__
+#define SIG2 0x94
+#define SIG3 0x05
+#define PAGE_SIZE 0x40U //64 words
+
+#elif defined __AVR_ATmega8515__
+#define SIG2 0x93
+#define SIG3 0x06
+#define PAGE_SIZE 0x20U //32 words
+
+#elif defined __AVR_ATmega8535__
+#define SIG2 0x93
+#define SIG3 0x08
+#define PAGE_SIZE 0x20U //32 words
+#endif
+
+
+/* function prototypes */
+void putch(char);
+char getch(void);
+void getNch(uint8_t);
+void byte_response(uint8_t);
+void nothing_response(void);
+char gethex(void);
+void puthex(char);
+void flash_led(uint8_t);
+
+/* some variables */
+union address_union {
+ uint16_t word;
+ uint8_t byte[2];
+} address;
+
+union length_union {
+ uint16_t word;
+ uint8_t byte[2];
+} length;
+
+struct flags_struct {
+ unsigned eeprom : 1;
+ unsigned rampz : 1;
+} flags;
+
+uint8_t buff[256];
+uint8_t address_high;
+
+uint8_t pagesz=0x80;
+
+uint8_t i;
+uint8_t bootuart = 0;
+
+void (*app_start)(void) = 0x0000;
+
+
+/* main program starts here */
+int main(void)
+{
+ uint8_t ch,ch2;
+ uint16_t w;
+
+ asm volatile("nop\n\t");
+
+ /* set pin direction for bootloader pin and enable pullup */
+ /* for ATmega128, two pins need to be initialized */
+#ifdef __AVR_ATmega128__
+ BL_DDR &= ~_BV(BL0);
+ BL_DDR &= ~_BV(BL1);
+ BL_PORT |= _BV(BL0);
+ BL_PORT |= _BV(BL1);
+#else
+ BL_DDR &= ~_BV(BL);
+ BL_PORT |= _BV(BL);
+#endif
+
+
+#ifdef __AVR_ATmega128__
+ /* check which UART should be used for booting */
+ if(bit_is_clear(BL_PIN, BL0)) {
+ bootuart = 1;
+ }
+ else if(bit_is_clear(BL_PIN, BL1)) {
+ bootuart = 2;
+ }
+#endif
+
+ /* check if flash is programmed already, if not start bootloader anyway */
+ if(pgm_read_byte_near(0x0000) != 0xFF) {
+
+#ifdef __AVR_ATmega128__
+ /* no UART was selected, start application */
+ if(!bootuart) {
+ app_start();
+ }
+#else
+ /* check if bootloader pin is set low */
+ /* we don't start this part neither for the m8, nor m168 */
+ //if(bit_is_set(BL_PIN, BL)) {
+ // app_start();
+ // }
+#endif
+ }
+
+#ifdef __AVR_ATmega128__
+ /* no bootuart was selected, default to uart 0 */
+ if(!bootuart) {
+ bootuart = 1;
+ }
+#endif
+
+
+ /* initialize UART(s) depending on CPU defined */
+#ifdef __AVR_ATmega128__
+ if(bootuart == 1) {
+ UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ UCSR0A = 0x00;
+ UCSR0C = 0x06;
+ UCSR0B = _BV(TXEN0)|_BV(RXEN0);
+ }
+ if(bootuart == 2) {
+ UBRR1L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ UBRR1H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ UCSR1A = 0x00;
+ UCSR1C = 0x06;
+ UCSR1B = _BV(TXEN1)|_BV(RXEN1);
+ }
+#elif defined __AVR_ATmega163__
+ UBRR = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ UBRRHI = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ UCSRA = 0x00;
+ UCSRB = _BV(TXEN)|_BV(RXEN);
+#elif defined __AVR_ATmega168__
+ UBRR0H = ((F_CPU / 16 + BAUD_RATE / 2) / BAUD_RATE - 1) >> 8;
+ UBRR0L = ((F_CPU / 16 + BAUD_RATE / 2) / BAUD_RATE - 1);
+
+
+ //UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ //UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ UCSR0B = (1<<RXEN0) | (1<<TXEN0);
+ UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
+#elif defined __AVR_ATmega8__
+ /* m8 */
+ UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
+ UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
+ UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & Tx
+ UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
+#else
+ /* m16,m32,m169,m8515,m8535 */
+ UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
+ UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
+ UCSRA = 0x00;
+ UCSRC = 0x06;
+ UCSRB = _BV(TXEN)|_BV(RXEN);
+#endif
+
+ /* set LED pin as output */
+ LED_DDR |= _BV(LED);
+
+
+
+ set_output(DDRD,PIND7);
+ high(PORTD,PD7);
+ for (i = 0; i < 16; i++) {
+
+ _delay_loop_2(0);
+ }
+
+
+ low(PORTD,PD7);
+
+
+ /* flash onboard LED to signal entering of bootloader */
+#ifdef __AVR_ATmega128__
+ // 4x for UART0, 5x for UART1
+ flash_led(3 + bootuart);
+#else
+ flash_led(3);
+#endif
+
+ /* 20050803: by DojoCorp, this is one of the parts provoking the
+ system to stop listening, cancelled from the original */
+ //putch('\0');
+
+
+ //message("SET BT PAGEMODE 3 2000 1");
+putch('S');
+putch('E');
+putch('T');
+putch(' ');
+putch('B');
+putch('T');
+putch(' ');
+putch('P');
+putch('A');
+putch('G');
+putch('E');
+putch('M');
+putch('O');
+putch('D');
+putch('E');
+putch(' ');
+putch('3');
+putch(' ');
+putch('2');
+putch('0');
+putch('0');
+putch('0');
+putch(' ');
+putch('1');
+putch(0x0D);
+
+
+ //put_s("SET BT ROLE 0 f 7d00");
+ putch('S');
+ putch('E');
+ putch('T');
+ putch(' ');
+ putch('B');
+ putch('T');
+ putch(' ');
+ putch('R');
+ putch('O');
+ putch('L');
+ putch('E');
+ putch(' ');
+ putch('0');
+ putch(' ');
+ putch('f');
+ putch(' ');
+ putch('7');
+ putch('d');
+ putch('0');
+ putch('0');
+ putch(0x0D);
+
+
+
+
+
+
+ /* forever loop */
+ for (;;) {
+
+ /* get character from UART */
+ ch = getch();
+
+ /* A bunch of if...else if... gives smaller code than switch...case ! */
+
+ /* Hello is anyone home ? */
+ if(ch=='0') {
+ nothing_response();
+ }
+
+
+ /* Request programmer ID */
+ /* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
+ /* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
+ else if(ch=='1') {
+ if (getch() == ' ') {
+ putch(0x14);
+ putch('A');
+ putch('V');
+ putch('R');
+ putch(' ');
+ putch('I');
+ putch('S');
+ putch('P');
+ putch(0x10);
+ }
+ }
+
+
+ /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
+ else if(ch=='@') {
+ ch2 = getch();
+ if (ch2>0x85) getch();
+ nothing_response();
+ }
+
+
+ /* AVR ISP/STK500 board requests */
+ else if(ch=='A') {
+ ch2 = getch();
+ if(ch2==0x80) byte_response(HW_VER); // Hardware version
+ else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
+ else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
+ else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
+ else byte_response(0x00); // Covers various unnecessary responses we don't care about
+ }
+
+
+ /* Device Parameters DON'T CARE, DEVICE IS FIXED */
+ else if(ch=='B') {
+ getNch(20);
+ nothing_response();
+ }
+
+
+ /* Parallel programming stuff DON'T CARE */
+ else if(ch=='E') {
+ getNch(5);
+ nothing_response();
+ }
+
+
+ /* Enter programming mode */
+ else if(ch=='P') {
+ nothing_response();
+ }
+
+
+ /* Leave programming mode */
+ else if(ch=='Q') {
+ nothing_response();
+ }
+
+
+ /* Erase device, don't care as we will erase one page at a time anyway. */
+ else if(ch=='R') {
+ nothing_response();
+ }
+
+
+ /* Set address, little endian. EEPROM in bytes, FLASH in words */
+ /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
+ /* This might explain why little endian was used here, big endian used everywhere else. */
+ else if(ch=='U') {
+ address.byte[0] = getch();
+ address.byte[1] = getch();
+ nothing_response();
+ }
+
+
+ /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
+ else if(ch=='V') {
+ getNch(4);
+ byte_response(0x00);
+ }
+
+
+ /* Write memory, length is big endian and is in bytes */
+ else if(ch=='d') {
+ length.byte[1] = getch();
+ length.byte[0] = getch();
+ flags.eeprom = 0;
+ if (getch() == 'E') flags.eeprom = 1;
+ for (w=0;w<length.word;w++) {
+ buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
+ }
+ if (getch() == ' ') {
+ if (flags.eeprom) { //Write to EEPROM one byte at a time
+ for(w=0;w<length.word;w++) {
+#ifdef __AVR_ATmega168__
+ while(EECR & (1<<EEPE));
+ EEAR = (uint16_t)(void *)address.word;
+ EEDR = buff[w];
+ EECR |= (1<<EEMPE);
+ EECR |= (1<<EEPE);
+#else
+ eeprom_write_byte((void *)address.word,buff[w]);
+#endif
+ address.word++;
+ }
+ }
+ else { //Write to FLASH one page at a time
+ if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
+ else address_high = 0x00;
+#ifdef __AVR_ATmega128__
+ RAMPZ = address_high;
+#endif
+ address.word = address.word << 1; //address * 2 -> byte location
+ /* if ((length.byte[0] & 0x01) == 0x01) length.word++; //Even up an odd number of bytes */
+ if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
+ cli(); //Disable interrupts, just to be sure
+ // HACKME: EEPE used to be EEWE
+ while(bit_is_set(EECR,EEPE)); //Wait for previous EEPROM writes to complete
+ asm volatile(
+ "clr r17 \n\t" //page_word_count
+ "lds r30,address \n\t" //Address of FLASH location (in bytes)
+ "lds r31,address+1 \n\t"
+ "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
+ "ldi r29,hi8(buff) \n\t"
+ "lds r24,length \n\t" //Length of data to be written (in bytes)
+ "lds r25,length+1 \n\t"
+ "length_loop: \n\t" //Main loop, repeat for number of words in block
+ "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
+ "brne no_page_erase \n\t"
+ "wait_spm1: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm1 \n\t"
+ "ldi r16,0x03 \n\t" //Erase page pointed to by Z
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+#ifdef __AVR_ATmega163__
+ ".word 0xFFFF \n\t"
+ "nop \n\t"
+#endif
+ "wait_spm2: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm2 \n\t"
+
+ "ldi r16,0x11 \n\t" //Re-enable RWW section
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+#ifdef __AVR_ATmega163__
+ ".word 0xFFFF \n\t"
+ "nop \n\t"
+#endif
+ "no_page_erase: \n\t"
+ "ld r0,Y+ \n\t" //Write 2 bytes into page buffer
+ "ld r1,Y+ \n\t"
+
+ "wait_spm3: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm3 \n\t"
+ "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+
+ "inc r17 \n\t" //page_word_count++
+ "cpi r17,%1 \n\t"
+ "brlo same_page \n\t" //Still same page in FLASH
+ "write_page: \n\t"
+ "clr r17 \n\t" //New page, write current one first
+ "wait_spm4: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm4 \n\t"
+#ifdef __AVR_ATmega163__
+ "andi r30,0x80 \n\t" // m163 requires Z6:Z1 to be zero during page write
+#endif
+ "ldi r16,0x05 \n\t" //Write page pointed to by Z
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+#ifdef __AVR_ATmega163__
+ ".word 0xFFFF \n\t"
+ "nop \n\t"
+ "ori r30,0x7E \n\t" // recover Z6:Z1 state after page write (had to be zero during write)
+#endif
+ "wait_spm5: \n\t"
+ "lds r16,%0 \n\t" //Wait for previous spm to complete
+ "andi r16,1 \n\t"
+ "cpi r16,1 \n\t"
+ "breq wait_spm5 \n\t"
+ "ldi r16,0x11 \n\t" //Re-enable RWW section
+ "sts %0,r16 \n\t"
+ "spm \n\t"
+#ifdef __AVR_ATmega163__
+ ".word 0xFFFF \n\t"
+ "nop \n\t"
+#endif
+ "same_page: \n\t"
+ "adiw r30,2 \n\t" //Next word in FLASH
+ "sbiw r24,2 \n\t" //length-2
+ "breq final_write \n\t" //Finished
+ "rjmp length_loop \n\t"
+ "final_write: \n\t"
+ "cpi r17,0 \n\t"
+ "breq block_done \n\t"
+ "adiw r24,2 \n\t" //length+2, fool above check on length after short page write
+ "rjmp write_page \n\t"
+ "block_done: \n\t"
+ "clr __zero_reg__ \n\t" //restore zero register
+#if defined __AVR_ATmega168__
+ : "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
+#else
+ : "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
+#endif
+ );
+ /* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
+ /* exit the bootloader without a power cycle anyhow */
+ }
+ putch(0x14);
+ putch(0x10);
+ }
+ }
+
+
+ /* Read memory block mode, length is big endian. */
+ else if(ch=='t') {
+ length.byte[1] = getch();
+ length.byte[0] = getch();
+#if defined __AVR_ATmega128__
+ if (address.word>0x7FFF) flags.rampz = 1; // No go with m256, FIXME
+ else flags.rampz = 0;
+#endif
+ if (getch() == 'E') flags.eeprom = 1;
+ else {
+ flags.eeprom = 0;
+ address.word = address.word << 1; // address * 2 -> byte location
+ }
+ if (getch() == ' ') { // Command terminator
+ putch(0x14);
+ for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
+ if (flags.eeprom) { // Byte access EEPROM read
+#ifdef __AVR_ATmega168__
+ while(EECR & (1<<EEPE));
+ EEAR = (uint16_t)(void *)address.word;
+ EECR |= (1<<EERE);
+ putch(EEDR);
+#else
+ putch(eeprom_read_byte((void *)address.word));
+#endif
+ address.word++;
+ }
+ else {
+
+ if (!flags.rampz) putch(pgm_read_byte_near(address.word));
+#if defined __AVR_ATmega128__
+ else putch(pgm_read_byte_far(address.word + 0x10000));
+ // Hmmmm, yuck FIXME when m256 arrvies
+#endif
+ address.word++;
+ }
+ }
+ putch(0x10);
+ }
+ }
+
+
+ /* Get device signature bytes */
+ else if(ch=='u') {
+ if (getch() == ' ') {
+ putch(0x14);
+ putch(SIG1);
+ putch(SIG2);
+ putch(SIG3);
+ putch(0x10);
+ }
+ }
+
+
+ /* Read oscillator calibration byte */
+ else if(ch=='v') {
+ byte_response(0x00);
+ }
+
+
+#ifdef MONITOR
+
+ /* here come the extended monitor commands by Erik Lins */
+
+ /* check for three times exclamation mark pressed */
+ else if(ch=='!') {
+ ch = getch();
+ if(ch=='!') {
+ ch = getch();
+ if(ch=='!') {
+
+#ifdef __AVR_ATmega128__
+ uint16_t extaddr;
+#endif
+ uint8_t addrl, addrh;
+
+#ifdef CRUMB128
+ PGM_P welcome = {"ATmegaBOOT / Crumb128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
+#elif defined PROBOMEGA128
+ PGM_P welcome = {"ATmegaBOOT / PROBOmega128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
+#elif defined SAVVY128
+ PGM_P welcome = {"ATmegaBOOT / Savvy128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
+#endif
+
+ /* turn on LED */
+ LED_DDR |= _BV(LED);
+ LED_PORT &= ~_BV(LED);
+
+ /* print a welcome message and command overview */
+ for(i=0; welcome[i] != '\0'; ++i) {
+ putch(welcome[i]);
+ }
+
+ /* test for valid commands */
+ for(;;) {
+ putch('\n');
+ putch('\r');
+ putch(':');
+ putch(' ');
+
+ ch = getch();
+ putch(ch);
+
+ /* toggle LED */
+ if(ch == 't') {
+ if(bit_is_set(LED_PIN,LED)) {
+ LED_PORT &= ~_BV(LED);
+ putch('1');
+ } else {
+ LED_PORT |= _BV(LED);
+ putch('0');
+ }
+
+ }
+
+ /* read byte from address */
+ else if(ch == 'r') {
+ ch = getch(); putch(ch);
+ addrh = gethex();
+ addrl = gethex();
+ putch('=');
+ ch = *(uint8_t *)((addrh << 8) + addrl);
+ puthex(ch);
+ }
+
+ /* write a byte to address */
+ else if(ch == 'w') {
+ ch = getch(); putch(ch);
+ addrh = gethex();
+ addrl = gethex();
+ ch = getch(); putch(ch);
+ ch = gethex();
+ *(uint8_t *)((addrh << 8) + addrl) = ch;
+
+ }
+
+ /* read from uart and echo back */
+ else if(ch == 'u') {
+ for(;;) {
+ putch(getch());
+ }
+ }
+#ifdef __AVR_ATmega128__
+ /* external bus loop */
+ else if(ch == 'b') {
+ putch('b');
+ putch('u');
+ putch('s');
+ MCUCR = 0x80;
+ XMCRA = 0;
+ XMCRB = 0;
+ extaddr = 0x1100;
+ for(;;) {
+ ch = *(volatile uint8_t *)extaddr;
+ if(++extaddr == 0) {
+ extaddr = 0x1100;
+ }
+ }
+ }
+#endif
+
+ else if(ch == 'j') {
+ app_start();
+ }
+
+ }
+ /* end of monitor functions */
+
+ }
+ }
+ }
+ /* end of monitor */
+#endif
+
+
+ }
+ /* end of forever loop */
+
+}
+
+
+char gethex(void) {
+ char ah,al;
+
+ ah = getch(); putch(ah);
+ al = getch(); putch(al);
+ if(ah >= 'a') {
+ ah = ah - 'a' + 0x0a;
+ } else if(ah >= '0') {
+ ah -= '0';
+ }
+ if(al >= 'a') {
+ al = al - 'a' + 0x0a;
+ } else if(al >= '0') {
+ al -= '0';
+ }
+ return (ah << 4) + al;
+}
+
+
+void puthex(char ch) {
+ char ah,al;
+
+ ah = (ch & 0xf0) >> 4;
+ if(ah >= 0x0a) {
+ ah = ah - 0x0a + 'a';
+ } else {
+ ah += '0';
+ }
+ al = (ch & 0x0f);
+ if(al >= 0x0a) {
+ al = al - 0x0a + 'a';
+ } else {
+ al += '0';
+ }
+ putch(ah);
+ putch(al);
+}
+
+
+void putch(char ch)
+{
+#ifdef __AVR_ATmega128__
+ if(bootuart == 1) {
+ while (!(UCSR0A & _BV(UDRE0)));
+ UDR0 = ch;
+ }
+ else if (bootuart == 2) {
+ while (!(UCSR1A & _BV(UDRE1)));
+ UDR1 = ch;
+ }
+#elif defined __AVR_ATmega168__
+ while (!(UCSR0A & _BV(UDRE0)));
+ UDR0 = ch;
+#else
+ /* m8,16,32,169,8515,8535,163 */
+ while (!(UCSRA & _BV(UDRE)));
+ UDR = ch;
+#endif
+}
+
+
+char getch(void)
+{
+#ifdef __AVR_ATmega128__
+ if(bootuart == 1) {
+ while(!(UCSR0A & _BV(RXC0)));
+ return UDR0;
+ }
+ else if(bootuart == 2) {
+ while(!(UCSR1A & _BV(RXC1)));
+ return UDR1;
+ }
+ return 0;
+#elif defined __AVR_ATmega168__
+ uint32_t count = 0;
+ while(!(UCSR0A & _BV(RXC0))){
+ /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
+ /* HACKME:: here is a good place to count times*/
+ count++;
+ if (count > MAX_TIME_COUNT)
+ app_start();
+ }
+ return UDR0;
+#else
+ /* m8,16,32,169,8515,8535,163 */
+ uint32_t count = 0;
+ while(!(UCSRA & _BV(RXC))){
+ /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
+ /* HACKME:: here is a good place to count times*/
+ count++;
+ if (count > MAX_TIME_COUNT)
+ app_start();
+ }
+ return UDR;
+#endif
+}
+
+
+void getNch(uint8_t count)
+{
+ uint8_t i;
+ for(i=0;i<count;i++) {
+#ifdef __AVR_ATmega128__
+ if(bootuart == 1) {
+ while(!(UCSR0A & _BV(RXC0)));
+ UDR0;
+ }
+ else if(bootuart == 2) {
+ while(!(UCSR1A & _BV(RXC1)));
+ UDR1;
+ }
+#elif defined __AVR_ATmega168__
+ while(!(UCSR0A & _BV(RXC0)));
+ UDR0;
+#else
+ /* m8,16,32,169,8515,8535,163 */
+ /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
+ //while(!(UCSRA & _BV(RXC)));
+ //UDR;
+ uint8_t i;
+ for(i=0;i<count;i++) {
+ getch(); // need to handle time out
+ }
+#endif
+ }
+}
+
+
+void byte_response(uint8_t val)
+{
+ if (getch() == ' ') {
+ putch(0x14);
+ putch(val);
+ putch(0x10);
+ }
+}
+
+
+void nothing_response(void)
+{
+ if (getch() == ' ') {
+ putch(0x14);
+ putch(0x10);
+ }
+}
+
+void flash_led(uint8_t count)
+{
+ /* flash onboard LED three times to signal entering of bootloader */
+ uint32_t l;
+
+ if (count == 0) {
+ count = 3;
+ }
+
+ for (i = 0; i < count; ++i) {
+ LED_PORT |= _BV(LED);
+ for(l = 0; l < (2 * F_CPU); ++l);
+ LED_PORT &= ~_BV(LED);
+ for(l = 0; l < (F_CPU / 5); ++l);
+ }
+}
+
+
+/* end of file ATmegaBOOT.c */
diff --git a/bootloaders/bt/ATmegaBOOT_168.hex b/bootloaders/bt/ATmegaBOOT_168.hex
new file mode 100644
index 0000000..036ae54
--- /dev/null
+++ b/bootloaders/bt/ATmegaBOOT_168.hex
@@ -0,0 +1,121 @@
+:103800000C94341C0C944F1C0C944F1C0C944F1CA7
+:103810000C944F1C0C944F1C0C944F1C0C944F1C7C
+:103820000C944F1C0C944F1C0C944F1C0C944F1C6C
+:103830000C944F1C0C944F1C0C944F1C0C944F1C5C
+:103840000C944F1C0C944F1C0C944F1C0C944F1C4C
+:103850000C944F1C0C944F1C0C944F1C0C944F1C3C
+:103860000C944F1C0C944F1C11241FBECFEFD4E0BE
+:10387000DEBFCDBF11E0A0E0B1E0E0E6FFE302C0B3
+:1038800005900D92A230B107D9F712E0A2E0B1E0A5
+:1038900001C01D92AC30B107E1F70C94D61C0C941A
+:1038A000001C882309F483E01092090290E0981725
+:1038B000F0F4692F2D9A2FEF37E448EE51E02253B0
+:1038C00030404040504057FFFACF2D982FEF33ED56
+:1038D00040E350E0225330404040504057FFFACF81
+:1038E000962F9F5F692F981728F3909309020895E8
+:1038F000982F8091C00085FFFCCF9093C60008955B
+:10390000EF92FF920F931F93EE24FF248701809183
+:10391000C00087FD17C00894E11CF11C011D111D9A
+:1039200081E0E81682E1F8068AE7080780E01807D8
+:1039300070F3E0910201F091030109958091C000BC
+:1039400087FFE9CF8091C600992787FD90951F9149
+:103950000F91FF90EF9008950E94801C803209F033
+:10396000089584E10E94781C80E10E94781C0895EB
+:10397000CF93C82F0E94801C803249F484E10E94BA
+:10398000781C8C2F0E94781C80E10E94781CCF91BB
+:103990000895282F90E007C08091C0008823E4F7A5
+:1039A0008091C6009F5F9217B8F30895CFEFD4E0DF
+:1039B000DEBFCDBF000056985E9A1092C50088E029
+:1039C0008093C40088E18093C10086E08093C200A8
+:1039D000259A579A5F9A109209022FE080E090E0B2
+:1039E0000197F1F7215027FFF9CF20E12093090239
+:1039F0005F9883E00E94511C83E50E94781C85E457
+:103A00000E94781C84E50E94781C80E20E94781C49
+:103A100082E40E94781C84E50E94781C80E20E9467
+:103A2000781C80E50E94781C81E40E94781C87E461
+:103A30000E94781C85E40E94781C8DE40E94781C0A
+:103A40008FE40E94781C84E40E94781C85E40E9424
+:103A5000781C80E20E94781C83E30E94781C80E23C
+:103A60000E94781C82E30E94781C80E30E94781CEC
+:103A700080E30E94781C80E30E94781C80E20E9410
+:103A8000781C81E30E94781C8DE00E94781C83E5FD
+:103A90000E94781C85E40E94781C84E50E94781CB2
+:103AA00080E20E94781C82E40E94781C84E50E94D7
+:103AB000781C80E20E94781C82E50E94781C8FE4CA
+:103AC0000E94781C8CE40E94781C85E40E94781C7B
+:103AD00080E20E94781C80E30E94781C80E20E94B1
+:103AE000781C86E60E94781C80E20E94781C87E39E
+:103AF0000E94781C84E60E94781C80E30E94781C57
+:103B000080E30E94781C8DE00E94781C0E94801C3B
+:103B1000803361F1813369F1803409F449C0813423
+:103B200009F44FC0823409F45DC0853409F460C0E3
+:103B30008035E1F08135D1F08235C1F0853509F469
+:103B40005BC0863509F463C0843609F465C08437E8
+:103B500009F4B9C0853709F414C18637B9F680E095
+:103B60000E94B81C0E94801C8033A1F60E94AC1CED
+:103B7000CDCF0E94801CC82F803241F684E10E9484
+:103B8000781C81E40E94781C86E50E94781C82E5FE
+:103B90000E94781C8C2F0E94781C89E40E94781C5B
+:103BA00083E50E94781C80E50E94781C80E1ACCF00
+:103BB0000E94801C8638D0F20E94801C0E94AC1C9F
+:103BC000A5CF0E94801C803809F4EDC0813809F42B
+:103BD000EEC0823809F4EFC0883909F683E00E940C
+:103BE000B81CC0CF84E10E94C91C0E94AC1C8ECFBF
+:103BF00085E00E94C91CF9CF0E94801C80930501BA
+:103C00000E94801C809306010E94AC1C7FCF84E040
+:103C10000E94C91C80E0A4CF0E94801C80930802EF
+:103C20000E94801C8093070280910B028E7F8093FC
+:103C30000B020E94801C853409F4C1C000E010E032
+:103C400080910702909108021816190670F4C7E0D7
+:103C5000D1E00E94801C89930F5F1F4F8091070263
+:103C60009091080208171907A0F30E94801C803267
+:103C700009F04CCF80910B0280FFADC000E010E056
+:103C8000209107023091080212161306C0F4E09149
+:103C90000501F0910601A7E0B1E0F999FECFF2BD70
+:103CA000E1BD8D9180BDFA9AF99A31960F5F1F4F51
+:103CB0000217130790F3F0930601E093050184E1E6
+:103CC0000E94781C73CF0E94801C809308020E947F
+:103CD000801C809307020E94801C853409F475C003
+:103CE00080910B028E7F80930B0280910501909151
+:103CF0000601880F991F90930601809305010E9489
+:103D0000801C803209F002CF84E10E94781C00E020
+:103D100010E020910702309108021216130608F0F5
+:103D200045CFE0910501F091060180910B0280FFE3
+:103D30001FC0F999FECFF2BDE1BDF89A80B50E948F
+:103D4000781CE0910501F09106013196F09306018F
+:103D5000E093050120910702309108020F5F1F4F89
+:103D60000217130708F022CF80910B0280FDE1CFEC
+:103D7000869580FF9BC03196F0930601E093050184
+:103D8000EDCF0E94801C803209F0C0CE84E10E94F9
+:103D9000781C8EE10E94781C84E90E94781C86E0E1
+:103DA0000E94781C03CF82E00E94B81CDBCE81E029
+:103DB0000E94B81CD7CE8FE00E94B81CD3CE809151
+:103DC0000B02816080930B0239CF80910B028160DE
+:103DD00080930B0294CF8091060187FD73C01092EF
+:103DE0000A028091050190910601880F991F909316
+:103DF0000601809305018091070280FF09C0809130
+:103E0000070290910802019690930802809307029E
+:103E1000F894F999FECF1127E0910501F091060180
+:103E2000C7E0D1E08091070290910802103091F430
+:103E30000091570001700130D9F303E0009357005F
+:103E4000E8950091570001700130D9F301E100932A
+:103E50005700E895099019900091570001700130C2
+:103E6000D9F301E000935700E8951395103498F0CA
+:103E700011270091570001700130D9F305E000933C
+:103E80005700E8950091570001700130D9F301E126
+:103E900000935700E8953296029709F0C7CF10308B
+:103EA00011F00296E5CF112484E10ACF84910E949B
+:103EB000781C2091070230910802E0910501F091F1
+:103EC000060159CF81E080930A028BCF1F93CF93D5
+:103ED0000E94801CC82F0E94781C0E94801C182FF2
+:103EE0000E94781CC1362CF0C75511363CF017558E
+:103EF00008C0C033D4F3C0531136CCF710330CF0E4
+:103F00001053C295C07FC10F8C2F992787FD9095C4
+:103F1000CF911F910895CF93282F992787FD9095D2
+:103F2000807F9070959587959595879595958795C0
+:103F3000959587958A303CF0895AC22FCF70CA3048
+:103F40003CF0C95A06C0805DC22FCF70CA30CCF792
+:103F5000C05D0E94781C8C2F0E94781CCF91089520
+:023F60008000DF
+:0400000300003800C1
+:00000001FF