From f73b52bc3c8eb06c752d33847a9a364afce32556 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sun, 15 Aug 2010 16:44:54 +0200 Subject: [PATCH] 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<