diff --git a/.gitignore b/.gitignore index 6d36148..d53dd72 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ *.bin *.hex *.lst +*.lss *.map diff --git a/Makefile b/Makefile index ccdc80e..338c668 100644 --- a/Makefile +++ b/Makefile @@ -1,63 +1,60 @@ -PRG = mpmboot -OBJ = main.o -MCU_TARGET = atmega32 -OPTIMIZE = -Os +CC := avr-gcc +LD := avr-ld +OBJCOPY := avr-objcopy +OBJDUMP := avr-objdump +SIZE := avr-size -#AVRDUDE_PROG = -c avr910 -b 115200 -P /dev/ttyUSB2 -AVRDUDE_PROG = -c dragon_isp -P usb +TARGET = mpmboot +SOURCE = $(wildcard *.c) -ifeq ($(MCU_TARGET), atmega32) -# hfuse = 0x?A (2k bootloader) -BOOTLOADER_START=0x7800 +# select MCU +MCU = atmega32 + +AVRDUDE_PROG := -c avr910 -b 115200 -P /dev/ttyUSB0 +#AVRDUDE_PROG := -c dragon_isp -P usb + +# --------------------------------------------------------------------------- + +ifeq ($(MCU), atmega32) +# (8Mhz internal RC-Osz.) AVRDUDE_MCU=m32 +AVRDUDE_FUSES=lfuse:w:0xd4:m hfuse:w:0xda:m + +BOOTLOADER_START=0x7800 endif -DEFS = -DBOOTLOADER_START=$(BOOTLOADER_START) -DOSCCAL_VALUE=0xA8 -DOSCCAL_CHECK=0 -DMPM_ADDRESS=0x11 -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=$(BOOTLOADER_START) +CFLAGS = -pipe -g -Os -mmcu=$(MCU) -Wall -fdata-sections -ffunction-sections +CFLAGS += -Wa,-adhlns=$(*F).lst -DBOOTLOADER_START=$(BOOTLOADER_START) +LDFLAGS = -Wl,-Map,$(@:.elf=.map),--cref,--relax,--gc-sections,--section-start=.text=$(BOOTLOADER_START) -CC = avr-gcc -OBJCOPY = avr-objcopy -OBJDUMP = avr-objdump -SIZE = avr-size +CFLAGS += -DOSCCAL_VALUE=0xA8 -DOSCCAL_CHECK=0 -DMPM_ADDRESS=0x11 -all: $(PRG).elf lst text - $(SIZE) -x -A $(PRG).elf +# --------------------------------------------------------------------------- -$(PRG).elf: $(OBJ) - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) +$(TARGET): $(TARGET).elf + @$(SIZE) -B -x --mcu=$(MCU) $< + +$(TARGET).elf: $(SOURCE:.c=.o) + @echo " Linking file: $@" + @$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ + @$(OBJDUMP) -h -S $@ > $(@:.elf=.lss) + @$(OBJCOPY) -j .text -j .data -O ihex $@ $(@:.elf=.hex) + @$(OBJCOPY) -j .text -j .data -O binary $@ $(@:.elf=.bin) %.o: %.c $(MAKEFILE_LIST) - $(CC) $(CFLAGS) -c $< -o $@ + @echo " Building file: $<" + @$(CC) $(CFLAGS) -o $@ -c $< clean: - rm -rf *.o $(PRG).lst $(PRG).map $(PRG).elf $(PRG).hex $(PRG).bin + rm -rf $(SOURCE:.c=.o) $(SOURCE:.c=.lst) $(addprefix $(TARGET), .elf .map .lss .hex .bin) -lst: $(PRG).lst - -%.lst: %.elf - $(OBJDUMP) -h -S $< > $@ - -text: hex bin - -hex: $(PRG).hex -bin: $(PRG).bin - -%.hex: %.elf - $(OBJCOPY) -j .text -j .data -O ihex $< $@ - -%.bin: %.elf - $(OBJCOPY) -j .text -j .data -O binary $< $@ - -install: text - avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -V -U flash:w:$(PRG).hex +install: $(TARGET).elf + avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U flash:w:$(<:.elf=.hex) fuses: - avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U hfuse:w:0xda:m - avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U lfuse:w:0xd4:m + avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) $(patsubst %,-U %, $(AVRDUDE_FUSES)) osccal: avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U calibration:r:-:h diff --git a/main.c b/main.c index af0900a..5ca298f 100644 --- a/main.c +++ b/main.c @@ -42,8 +42,14 @@ /* 40 * 25ms = 1s */ #define TIMEOUT 40 -#define RXTX PORTD2 -#define LED PORTD3 +#define RXTX_DDR DDRD +#define RXTX_PORT PORTD +#define RXTX_NUM PORTD2 + +#define LED_INIT() DDRD |= (1<> 8); + EEARL = para_address; + EEARH = (para_address >> 8); EECR |= (1<> 8); - address++; + EEARL = para_address; + EEARH = (para_address >> 8); + para_address++; EEDR = *data++; #if defined (__AVR_ATmega32__) EECR |= (1< (BOOTLOADER_START - SPM_PAGESIZE)) { + if (mem_address > (BOOTLOADER_START - SPM_PAGESIZE)) { tx_length = 0; tx_cause = CAUSE_INVALID_PARAMETER; /* writes must pagesize aligned */ } else if (tx_cmd == CMD_WRITE_MEMORY) { - if (((para_address & (SPM_PAGESIZE -1)) == 0x00) && + if (((mem_address & (SPM_PAGESIZE -1)) == 0x00) && (para_size <= SPM_PAGESIZE) ) { - write_flash_page(para_address, pagebuf, para_size); - para_address += para_size; + while (para_size < SPM_PAGESIZE) { + pagebuf[para_size] = 0xFF; + } + write_flash_page(); } else { tx_cause = CAUSE_INVALID_PARAMETER; @@ -332,12 +343,11 @@ ISR(USART_RXC_vect) } #if (EEPROM_SUPPORT) } else if (para_memtype == MEMTYPE_EEPROM) { - if ((para_address > (E2END +1)) || ((para_address + para_size) > (E2END +1))) { + if ((mem_address > (E2END +1)) || ((mem_address + para_size) > (E2END +1))) { tx_cause = CAUSE_INVALID_PARAMETER; } else if (tx_cmd == CMD_WRITE_MEMORY) { - write_eeprom_page(para_address, pagebuf, para_size); - para_address += para_size; + write_eeprom_page(para_size); } #endif /*(EEPROM_SUPPORT) */ } else { @@ -363,7 +373,7 @@ ISR(USART_UDRE_vect) { if (tx_bcnt == 0) { /* enable RS485 transmitter */ - PORTD |= (1<