From dbef89dc6e2ee6bb4dd31135a08d887d4f983fcd Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Tue, 17 Jun 2008 18:02:07 +0200 Subject: [PATCH 1/5] mega88 version --- Makefile | 4 ++-- main.c | 29 +++++++++++++++-------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index cb6161a..bfede5c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PRG = twiboot OBJ = main.o -MCU_TARGET = atmega8 +MCU_TARGET = atmega88 OPTIMIZE = -Os DEFS = @@ -41,4 +41,4 @@ bin: $(PRG).bin $(OBJCOPY) -j .text -j .data -O binary $< $@ install: text - uisp -dprog=avr910 -dserial=/dev/ttyS0 -dspeed=115200 -dpart=M8 --erase --upload if=$(PRG).hex + uisp -dprog=avr910 -dserial=/dev/ttyS0 -dspeed=115200 -dpart=auto --erase --upload if=$(PRG).hex diff --git a/main.c b/main.c index 363509c..dee79b4 100644 --- a/main.c +++ b/main.c @@ -45,8 +45,9 @@ * LED on PORTB4 blinks with 20Hz (while bootloader is running) * LED on PORTB5 blinks on TWI activity * - * Fuse H: 0xda (512 words bootloader) - * Fuse L: 0x84 (8Mhz internal RC-Osz., 2.7V BOD) + * Fuse E: 0xfa (512 words bootloader) + * Fuse H: 0xdd (2.7V BOD) + * Fuse L: 0xc2 (8Mhz internal RC-Osz.) * * bootloader twi-protocol: * - get info about bootloader: @@ -71,8 +72,8 @@ * SLA+W, 0x1F, STO */ -const static uint8_t info[16] = "TWIBOOT m8-v1.2"; -const static uint8_t signature[4] = { 0x1E, 0x93, 0x07, 0x00 }; +const static uint8_t info[16] = "TWIBOOT m88-v12"; +const static uint8_t signature[4] = { 0x1E, 0x93, 0x0A, 0x00 }; /* wait 40 * 25ms = 1s */ volatile static uint8_t boot_timeout = 40; @@ -115,8 +116,8 @@ void write_eeprom_byte(uint8_t val) EEDR = val; addr++; - EECR |= (1< Date: Sat, 21 Jun 2008 15:40:44 +0200 Subject: [PATCH 2/5] disable watchdog after reset, reduce size --- main.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index dee79b4..5427f5a 100644 --- a/main.c +++ b/main.c @@ -76,7 +76,7 @@ const static uint8_t info[16] = "TWIBOOT m88-v12"; const static uint8_t signature[4] = { 0x1E, 0x93, 0x0A, 0x00 }; /* wait 40 * 25ms = 1s */ -volatile static uint8_t boot_timeout = 40; +static uint8_t boot_timeout = 40; volatile static uint8_t cmd = CMD_WAIT; /* flash buffer */ @@ -267,6 +267,19 @@ ISR(TIMER0_OVF_vect) static void (*jump_to_app)(void) = 0x0000; +/* + * For newer devices (mega88) the watchdog timer remains active even after a + * system reset. So disable it as soon as possible. + * automagically called on startup + */ +void disable_wdt_timer(void) __attribute__((naked, section(".init3"))); +void disable_wdt_timer(void) +{ + MCUSR = 0; + WDTCSR = (1< Date: Sat, 24 Jan 2009 16:52:22 +0100 Subject: [PATCH 3/5] change programmer --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bfede5c..1c0e100 100644 --- a/Makefile +++ b/Makefile @@ -41,4 +41,7 @@ bin: $(PRG).bin $(OBJCOPY) -j .text -j .data -O binary $< $@ install: text - uisp -dprog=avr910 -dserial=/dev/ttyS0 -dspeed=115200 -dpart=auto --erase --upload if=$(PRG).hex + avrdude -c dragon_isp -P usb -p m88 -U flash:w:$(PRG).hex + +fuses: + avrdude -c dragon_isp -P usb -p m88 -U lfuse:w:0xc2:m -U hfuse:w:0xdd:m -U efuse:w:0xfa:m From e013aea33cacc8f4133d9a769cf420511f07806f Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sun, 11 Jul 2010 16:51:50 +0200 Subject: [PATCH 4/5] new protocol --- main.c | 248 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 157 insertions(+), 91 deletions(-) diff --git a/main.c b/main.c index 5427f5a..b0e95be 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 09/2007 by Olaf Rempel * + * Copyright (C) 07/2010 by Olaf Rempel * * razzor@kopf-tisch.de * * * * This program is free software; you can redistribute it and/or modify * @@ -24,66 +24,102 @@ #define F_CPU 8000000 #include +/* + * atmega88: + * Fuse E: 0xfa (512 words bootloader) + * Fuse H: 0xdd (2.7V BOD) + * Fuse L: 0xc2 (8Mhz internal RC-Osz.) + */ + +/* 25ms @8MHz */ +#define TIMER_RELOAD (0xFF - 195) + +/* 40 * 25ms */ +#define TIMEOUT 40 + #define LED_RT (1<> 8); + EECR |= (1<> 8); @@ -123,7 +168,9 @@ void write_eeprom_byte(uint8_t val) ISR(TWI_vect) { - static uint8_t bcnt = 0; + static uint8_t bcnt; + uint8_t data; + uint8_t ack = (1< receive data and ACK */ @@ -135,64 +182,87 @@ ISR(TWI_vect) /* prev. SLA+W, data received, ACK returned -> receive data and ACK */ case 0x80: - if (bcnt == 0) { - cmd = TWDR; - switch (cmd) { - case CMD_GET_INFO: - case CMD_GET_SIGNATURE: - case CMD_WRITE_FLASH: - case CMD_READ_FLASH: - case CMD_WRITE_EEPROM: - case CMD_READ_EEPROM: + data = TWDR; + switch (bcnt) { + case 0: + switch (data) { + case CMD_SWITCH_APPLICATION: + case CMD_WRITE_MEMORY: + bcnt++; + /* no break */ + + case CMD_WAIT: /* abort countdown */ boot_timeout = 0; - - case CMD_BOOT_APPLICATION: - bcnt++; break; default: - cmd = CMD_WAIT; - bcnt = 0; + /* boot app now */ + data = CMD_BOOT_APPLICATION; + ack = (0<> 8)) + case CMD_WRITE_MEMORY: bcnt++; - else - bcnt = 0; + if (data == MEMTYPE_SIGNATURE) { + cmd = CMD_WRITE_SIGNATURE; - } else if (bcnt == 4) { - if (TWDR == (COOKIE & 0xFF)) - bcnt++; - else - bcnt = 0; + } else if (data == MEMTYPE_FLASH) { + cmd = CMD_WRITE_FLASH; - } else if (bcnt >= 5 && cmd == CMD_WRITE_FLASH) { - buf[bcnt -5] = TWDR; - if (bcnt < sizeof(buf) +4) { - bcnt++; + } else if (data == MEMTYPE_EEPROM) { + cmd = CMD_WRITE_EEPROM; - } else { - write_flash_page(); - bcnt = 0; + } else { + ack = (0<= 5 && cmd == CMD_WRITE_EEPROM) { - write_eeprom_byte(TWDR); + case 2: + case 3: + addr <<= 8; + addr |= data; bcnt++; + break; + + default: + switch (cmd) { + case CMD_WRITE_FLASH: + buf[bcnt -4] = data; + if (bcnt < sizeof(buf) +3) { + bcnt++; + } else { + write_flash_page(); + ack = (0< send data */ @@ -203,33 +273,30 @@ ISR(TWI_vect) /* prev. SLA+R, data sent, ACK returned -> send data */ case 0xB8: switch (cmd) { - case CMD_GET_INFO: - TWDR = info[bcnt++]; + case CMD_READ_VERSION: + data = info[bcnt++]; bcnt %= sizeof(info); break; - case CMD_GET_SIGNATURE: - TWDR = signature[bcnt++]; + case CMD_READ_SIGNATURE: + data = signature[bcnt++]; bcnt %= sizeof(signature); break; case CMD_READ_FLASH: - TWDR = pgm_read_byte_near(addr++); + data = pgm_read_byte_near(addr++); break; case CMD_READ_EEPROM: - EEARL = addr; - EEARH = (addr >> 8); - EECR |= (1< Date: Sun, 15 Aug 2010 16:44:54 +0200 Subject: [PATCH 5/5] multicpu support --- Makefile | 26 +++++++++++--- main.c | 101 +++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 97 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 1c0e100..1ae511f 100644 --- a/Makefile +++ b/Makefile @@ -3,12 +3,25 @@ OBJ = main.o MCU_TARGET = atmega88 OPTIMIZE = -Os -DEFS = +ifeq ($(MCU_TARGET), atmega8) +BOOTLOADER_START=0x1C00 +AVRDUDE_MCU=m8 +endif +ifeq ($(MCU_TARGET), atmega88) +BOOTLOADER_START=0x1C00 +AVRDUDE_MCU=m88 +endif +ifeq ($(MCU_TARGET), atmega168) +BOOTLOADER_START=0x3C00 +AVRDUDE_MCU=m168 +endif + +DEFS = -DAPP_END=$(BOOTLOADER_START) LIBS = # Override is only needed by avr-lib build system. override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) -override LDFLAGS = -Wl,-Map,$(PRG).map,--section-start=.text=0x1C00 +override LDFLAGS = -Wl,-Map,$(PRG).map,--section-start=.text=$(BOOTLOADER_START) CC = avr-gcc OBJCOPY = avr-objcopy @@ -21,6 +34,9 @@ all: $(PRG).elf lst text $(PRG).elf: $(OBJ) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) +%.o: %.c $(MAKEFILE_LIST) + $(CC) $(CFLAGS) -c $< -o $@ + clean: rm -rf *.o *.lst *.map $(PRG).elf *.hex *.bin @@ -41,7 +57,9 @@ bin: $(PRG).bin $(OBJCOPY) -j .text -j .data -O binary $< $@ install: text - avrdude -c dragon_isp -P usb -p m88 -U flash:w:$(PRG).hex + avrdude -c dragon_isp -P usb -p $(AVRDUDE_MCU) -U flash:w:$(PRG).hex fuses: - avrdude -c dragon_isp -P usb -p m88 -U lfuse:w:0xc2:m -U hfuse:w:0xdd:m -U efuse:w:0xfa:m + avrdude -c dragon_isp -P usb -p $(AVRDUDE_MCU) -U lfuse:w:0xc2:m + avrdude -c dragon_isp -P usb -p $(AVRDUDE_MCU) -U hfuse:w:0xdd:m + avrdude -c dragon_isp -P usb -p $(AVRDUDE_MCU) -U efuse:w:0xfa:m diff --git a/main.c b/main.c index b0e95be..c258c3e 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 07/2010 by Olaf Rempel * + * Copyright (C) 08/2010 by Olaf Rempel * * razzor@kopf-tisch.de * * * * This program is free software; you can redistribute it and/or modify * @@ -25,12 +25,38 @@ #include /* + * atmega8: + * Fuse E: 0xfa (512 words bootloader) + * Fuse H: 0xdd (2.7V BOD) + * Fuse L: 0xc2 (8Mhz internal RC-Osz.) + * * atmega88: * Fuse E: 0xfa (512 words bootloader) * Fuse H: 0xdd (2.7V BOD) * Fuse L: 0xc2 (8Mhz internal RC-Osz.) + * + * atmega168: + * Fuse E: 0xfa (512 words bootloader) + * Fuse H: 0xdd (2.7V BOD) + * Fuse L: 0xc2 (8Mhz internal RC-Osz.) */ +#if defined (__AVR_ATmega8__) +#define VERSION_STRING "TWIBOOT m8v2.0" +#define SIGNATURE_BYTES 0x1E, 0x93, 0x07 + +#elif defined (__AVR_ATmega88__) +#define VERSION_STRING "TWIBOOT m88v2.0" +#define SIGNATURE_BYTES 0x1E, 0x93, 0x0A + +#elif defined (__AVR_ATmega168__) +#define VERSION_STRING "TWIBOOT m168v2.0" +#define SIGNATURE_BYTES 0x1E, 0x94, 0x06 + +#else +#error MCU not supported +#endif + /* 25ms @8MHz */ #define TIMER_RELOAD (0xFF - 195) @@ -42,17 +68,12 @@ #define TWI_ADDRESS 0x21 -#define APP_END 0x1C00 - -#define VERSION_STRING "TWIBOOT m88-v20" -#define SIGNATURE_BYTES { 0x1E, 0x93, 0x0A, 0x00 } - /* SLA+R */ #define CMD_WAIT 0x00 #define CMD_READ_VERSION 0x01 #define CMD_READ_MEMORY 0x02 /* internal mappings */ -#define CMD_READ_SIGNATURE (0x10 | CMD_READ_MEMORY) +#define CMD_READ_CHIPINFO (0x10 | CMD_READ_MEMORY) #define CMD_READ_FLASH (0x20 | CMD_READ_MEMORY) #define CMD_READ_EEPROM (0x30 | CMD_READ_MEMORY) #define CMD_READ_PARAMETERS (0x40 | CMD_READ_MEMORY) /* only in APP */ @@ -63,7 +84,7 @@ /* internal mappings */ #define CMD_BOOT_BOOTLOADER (0x10 | CMD_SWITCH_APPLICATION) /* only in APP */ #define CMD_BOOT_APPLICATION (0x20 | CMD_SWITCH_APPLICATION) -#define CMD_WRITE_SIGNATURE (0x10 | CMD_WRITE_MEMORY) +#define CMD_WRITE_CHIPINFO (0x10 | CMD_WRITE_MEMORY) /* invalid */ #define CMD_WRITE_FLASH (0x20 | CMD_WRITE_MEMORY) #define CMD_WRITE_EEPROM (0x30 | CMD_WRITE_MEMORY) #define CMD_WRITE_PARAMETERS (0x40 | CMD_WRITE_MEMORY) /* only in APP */ @@ -73,7 +94,7 @@ #define BOOTTYPE_APPLICATION 0x80 /* CMD_{READ|WRITE}_* parameter */ -#define MEMTYPE_SIGNATURE 0x00 +#define MEMTYPE_CHIPINFO 0x00 #define MEMTYPE_FLASH 0x01 #define MEMTYPE_EEPROM 0x02 #define MEMTYPE_PARAMETERS 0x03 /* only in APP */ @@ -92,7 +113,7 @@ * - start application * SLA+W, 0x01, 0x80, STO * - * - read signature bytes + * - read chip info: 3byte signature, 1byte page size, 2byte flash size, 2byte eeprom size * SLA+W, 0x02, 0x00, SLA+R, {4 bytes}, STO * * - read one (or more) flash bytes @@ -109,7 +130,17 @@ */ const static uint8_t info[16] = VERSION_STRING; -const static uint8_t signature[4] = SIGNATURE_BYTES; +const static uint8_t chipinfo[8] = { + SIGNATURE_BYTES, + + SPM_PAGESIZE, + + (APP_END >> 8) & 0xFF, + APP_END & 0xFF, + + (E2END >> 8 & 0xFF), + E2END & 0xFF +}; /* wait 40 * 25ms = 1s */ static uint8_t boot_timeout = TIMEOUT; @@ -160,9 +191,13 @@ static void write_eeprom_byte(uint8_t val) EEARH = (addr >> 8); EEDR = val; addr++; - +#if defined (__AVR_ATmega8__) + EECR |= (1<