update Makefile

This commit is contained in:
Olaf Rempel 2014-10-05 14:29:34 +02:00
parent 61d25b1b7d
commit 699502dca9
3 changed files with 121 additions and 107 deletions

1
.gitignore vendored
View File

@ -3,4 +3,5 @@
*.bin *.bin
*.hex *.hex
*.lst *.lst
*.lss
*.map *.map

View File

@ -1,63 +1,60 @@
PRG = mpmboot CC := avr-gcc
OBJ = main.o LD := avr-ld
MCU_TARGET = atmega32 OBJCOPY := avr-objcopy
OPTIMIZE = -Os OBJDUMP := avr-objdump
SIZE := avr-size
#AVRDUDE_PROG = -c avr910 -b 115200 -P /dev/ttyUSB2 TARGET = mpmboot
AVRDUDE_PROG = -c dragon_isp -P usb SOURCE = $(wildcard *.c)
ifeq ($(MCU_TARGET), atmega32) # select MCU
# hfuse = 0x?A (2k bootloader) MCU = atmega32
BOOTLOADER_START=0x7800
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_MCU=m32
AVRDUDE_FUSES=lfuse:w:0xd4:m hfuse:w:0xda:m
BOOTLOADER_START=0x7800
endif 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. CFLAGS = -pipe -g -Os -mmcu=$(MCU) -Wall -fdata-sections -ffunction-sections
override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) CFLAGS += -Wa,-adhlns=$(*F).lst -DBOOTLOADER_START=$(BOOTLOADER_START)
override LDFLAGS = -Wl,-Map,$(PRG).map,--section-start=.text=$(BOOTLOADER_START) LDFLAGS = -Wl,-Map,$(@:.elf=.map),--cref,--relax,--gc-sections,--section-start=.text=$(BOOTLOADER_START)
CC = avr-gcc CFLAGS += -DOSCCAL_VALUE=0xA8 -DOSCCAL_CHECK=0 -DMPM_ADDRESS=0x11
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
all: $(PRG).elf lst text # ---------------------------------------------------------------------------
$(SIZE) -x -A $(PRG).elf
$(PRG).elf: $(OBJ) $(TARGET): $(TARGET).elf
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) @$(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) %.o: %.c $(MAKEFILE_LIST)
$(CC) $(CFLAGS) -c $< -o $@ @echo " Building file: $<"
@$(CC) $(CFLAGS) -o $@ -c $<
clean: 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 install: $(TARGET).elf
avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U flash:w:$(<:.elf=.hex)
%.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
fuses: fuses:
avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U hfuse:w:0xda:m avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) $(patsubst %,-U %, $(AVRDUDE_FUSES))
avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U lfuse:w:0xd4:m
osccal: osccal:
avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U calibration:r:-:h avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U calibration:r:-:h

142
main.c
View File

@ -42,8 +42,14 @@
/* 40 * 25ms = 1s */ /* 40 * 25ms = 1s */
#define TIMEOUT 40 #define TIMEOUT 40
#define RXTX PORTD2 #define RXTX_DDR DDRD
#define LED PORTD3 #define RXTX_PORT PORTD
#define RXTX_NUM PORTD2
#define LED_INIT() DDRD |= (1<<PORTD3)
#define LED_OFF() PORTD |= (1<<PORTD3)
#define LED_ON() PORTD &= ~(1<<PORTD3)
#define LED_TOGGLE() PORTD ^= (1<<PORTD3)
#define BAUDRATE 115200 #define BAUDRATE 115200
#ifndef MPM_ADDRESS #ifndef MPM_ADDRESS
@ -52,28 +58,6 @@
#define EEPROM_SUPPORT 1 #define EEPROM_SUPPORT 1
#define CMD_WAIT 0x00
#define CMD_SWITCH_MODE 0x01
#define CMD_GET_VERSION 0x02
#define CMD_GET_CHIPINFO 0x03
#define CMD_READ_MEMORY 0x11
#define CMD_WRITE_MEMORY 0x12
#define CAUSE_SUCCESS 0x00
#define CAUSE_NOT_SUPPORTED 0xF0
#define CAUSE_INVALID_PARAMETER 0xF1
#define CAUSE_UNSPECIFIED_ERROR 0xFF
#define BOOTMODE_BOOTLOADER 0x00
#define BOOTMODE_APPLICATION 0x80
#define MEMTYPE_FLASH 0x01
#define MEMTYPE_EEPROM 0x02
#define BOOTWAIT_EXPIRED 0x00
#define BOOTWAIT_RUNNING 0x01
#define BOOTWAIT_INTERRUPTED 0x02
#define UART_CALC_BAUDRATE(baudRate) (((uint32_t)F_CPU) / (((uint32_t)baudRate)*16) -1) #define UART_CALC_BAUDRATE(baudRate) (((uint32_t)F_CPU) / (((uint32_t)baudRate)*16) -1)
/* /*
@ -136,6 +120,28 @@
* *
*/ */
#define CMD_WAIT 0x00
#define CMD_SWITCH_MODE 0x01
#define CMD_GET_VERSION 0x02
#define CMD_GET_CHIPINFO 0x03
#define CMD_READ_MEMORY 0x11
#define CMD_WRITE_MEMORY 0x12
#define CAUSE_SUCCESS 0x00
#define CAUSE_NOT_SUPPORTED 0xF0
#define CAUSE_INVALID_PARAMETER 0xF1
#define CAUSE_UNSPECIFIED_ERROR 0xFF
#define BOOTMODE_BOOTLOADER 0x00
#define BOOTMODE_APPLICATION 0x80
#define MEMTYPE_FLASH 0x01
#define MEMTYPE_EEPROM 0x02
#define BOOTWAIT_EXPIRED 0x00
#define BOOTWAIT_INTERRUPTED 0xFF
const static uint8_t info[16] = VERSION_STRING; const static uint8_t info[16] = VERSION_STRING;
const static uint8_t chipinfo[8] = { const static uint8_t chipinfo[8] = {
SIGNATURE_BYTES, SIGNATURE_BYTES,
@ -149,7 +155,6 @@ const static uint8_t chipinfo[8] = {
}; };
volatile static uint8_t boot_timeout = TIMEOUT; volatile static uint8_t boot_timeout = TIMEOUT;
volatile static uint8_t boot_wait = BOOTWAIT_RUNNING;
static uint8_t rx_addressed; static uint8_t rx_addressed;
@ -168,12 +173,14 @@ static uint16_t para_address;
static uint16_t para_size; static uint16_t para_size;
/* write buffer */ /* write buffer */
static uint16_t mem_address;
static uint8_t pagebuf[SPM_PAGESIZE]; static uint8_t pagebuf[SPM_PAGESIZE];
static void write_flash_page(uint16_t pagestart, uint8_t *data, uint8_t size) static void write_flash_page(void)
{ {
uint16_t address = pagestart; uint16_t pagestart = para_address;
uint8_t pagesize = SPM_PAGESIZE; uint8_t pagesize = SPM_PAGESIZE;
uint8_t *data = pagebuf;
boot_page_erase(pagestart); boot_page_erase(pagestart);
boot_spm_busy_wait(); boot_spm_busy_wait();
@ -181,11 +188,11 @@ static void write_flash_page(uint16_t pagestart, uint8_t *data, uint8_t size)
do { do {
uint16_t dataword; uint16_t dataword;
dataword = (size-- != 0) ? *data++ : 0xFF; dataword = (*data++);
dataword |= ((size-- != 0) ? *data++ : 0xFF) << 8; dataword |= (*data++) << 8;
boot_page_fill(address, dataword); boot_page_fill(para_address, dataword);
address += 2; para_address += 2;
pagesize -= 2; pagesize -= 2;
} while (pagesize); } while (pagesize);
@ -195,20 +202,22 @@ static void write_flash_page(uint16_t pagestart, uint8_t *data, uint8_t size)
} }
#if (EEPROM_SUPPORT) #if (EEPROM_SUPPORT)
static uint8_t read_eeprom_byte(uint16_t address) static uint8_t read_eeprom_byte(void)
{ {
EEARL = address; EEARL = para_address;
EEARH = (address >> 8); EEARH = (para_address >> 8);
EECR |= (1<<EERE); EECR |= (1<<EERE);
return EEDR; return EEDR;
} }
static void write_eeprom_page(uint16_t address, uint8_t *data, uint16_t size) static void write_eeprom_page(uint16_t size)
{ {
uint8_t *data = pagebuf;
while (size--) { while (size--) {
EEARL = address; EEARL = para_address;
EEARH = (address >> 8); EEARH = (para_address >> 8);
address++; para_address++;
EEDR = *data++; EEDR = *data++;
#if defined (__AVR_ATmega32__) #if defined (__AVR_ATmega32__)
EECR |= (1<<EEMWE); EECR |= (1<<EEMWE);
@ -230,13 +239,13 @@ ISR(USART_RXC_vect)
if (data == MPM_ADDRESS) { if (data == MPM_ADDRESS) {
#if 0 #if 0
/* stay in bootloader */ /* stay in bootloader */
boot_wait = BOOTWAIT_INTERRUPTED; boot_timeout = BOOTWAIT_INTERRUPTED;
#else #else
/* restart timeout */ /* restart timeout */
boot_timeout = TIMEOUT; boot_timeout = TIMEOUT;
#endif #endif
/* enable LED */ /* enable LED */
PORTD &= ~(1<<LED); LED_ON();
UCSRA &= ~(1<<MPCM); UCSRA &= ~(1<<MPCM);
rx_addressed = 1; rx_addressed = 1;
@ -267,7 +276,7 @@ ISR(USART_RXC_vect)
case 1: case 1:
case 2: case 2:
para_address = (para_address << 8) | data; mem_address = (mem_address << 8) | data;
break; break;
case 3: case 3:
@ -314,17 +323,19 @@ ISR(USART_RXC_vect)
case CMD_WRITE_MEMORY: case CMD_WRITE_MEMORY:
if (para_memtype == MEMTYPE_FLASH) { if (para_memtype == MEMTYPE_FLASH) {
/* only access application area */ /* only access application area */
if (para_address > (BOOTLOADER_START - SPM_PAGESIZE)) { if (mem_address > (BOOTLOADER_START - SPM_PAGESIZE)) {
tx_length = 0; tx_length = 0;
tx_cause = CAUSE_INVALID_PARAMETER; tx_cause = CAUSE_INVALID_PARAMETER;
/* writes must pagesize aligned */ /* writes must pagesize aligned */
} else if (tx_cmd == CMD_WRITE_MEMORY) { } 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) (para_size <= SPM_PAGESIZE)
) { ) {
write_flash_page(para_address, pagebuf, para_size); while (para_size < SPM_PAGESIZE) {
para_address += para_size; pagebuf[para_size] = 0xFF;
}
write_flash_page();
} else { } else {
tx_cause = CAUSE_INVALID_PARAMETER; tx_cause = CAUSE_INVALID_PARAMETER;
@ -332,12 +343,11 @@ ISR(USART_RXC_vect)
} }
#if (EEPROM_SUPPORT) #if (EEPROM_SUPPORT)
} else if (para_memtype == MEMTYPE_EEPROM) { } 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; tx_cause = CAUSE_INVALID_PARAMETER;
} else if (tx_cmd == CMD_WRITE_MEMORY) { } else if (tx_cmd == CMD_WRITE_MEMORY) {
write_eeprom_page(para_address, pagebuf, para_size); write_eeprom_page(para_size);
para_address += para_size;
} }
#endif /*(EEPROM_SUPPORT) */ #endif /*(EEPROM_SUPPORT) */
} else { } else {
@ -363,7 +373,7 @@ ISR(USART_UDRE_vect)
{ {
if (tx_bcnt == 0) { if (tx_bcnt == 0) {
/* enable RS485 transmitter */ /* enable RS485 transmitter */
PORTD |= (1<<RXTX); RXTX_PORT |= (1<<RXTX_NUM);
UCSRB &= ~(1<<TXB8); UCSRB &= ~(1<<TXB8);
UDR = tx_cmd; UDR = tx_cmd;
@ -389,10 +399,11 @@ ISR(USART_UDRE_vect)
} else if (tx_cmd == CMD_READ_MEMORY) { } else if (tx_cmd == CMD_READ_MEMORY) {
if (para_memtype == MEMTYPE_FLASH) { if (para_memtype == MEMTYPE_FLASH) {
data = pgm_read_byte_near(para_address++); data = pgm_read_byte_near(mem_address++);
#if (EEPROM_SUPPORT) #if (EEPROM_SUPPORT)
} else if (para_memtype == MEMTYPE_EEPROM) { } else if (para_memtype == MEMTYPE_EEPROM) {
data = read_eeprom_byte(para_address++); data = read_eeprom_byte();
mem_address++;
#endif /* (EEPROM_SUPPORT) */ #endif /* (EEPROM_SUPPORT) */
} }
} }
@ -410,10 +421,10 @@ ISR(USART_UDRE_vect)
ISR(USART_TXC_vect) ISR(USART_TXC_vect)
{ {
/* disable LED */ /* disable LED */
PORTD |= (1<<LED); LED_OFF();
/* disable RS485 transmitter */ /* disable RS485 transmitter */
PORTD &= ~(1<<RXTX); RXTX_PORT &= ~(1<<RXTX_NUM);
/* enable MP mode again */ /* enable MP mode again */
UCSRA |= (1<<MPCM); UCSRA |= (1<<MPCM);
@ -421,7 +432,7 @@ ISR(USART_TXC_vect)
/* switch to application after everything is transmitted */ /* switch to application after everything is transmitted */
if ((tx_cmd == CMD_SWITCH_MODE) && (para_mode == BOOTMODE_APPLICATION)) { if ((tx_cmd == CMD_SWITCH_MODE) && (para_mode == BOOTMODE_APPLICATION)) {
boot_wait = BOOTWAIT_EXPIRED; boot_timeout = BOOTWAIT_EXPIRED;
} }
} }
@ -430,13 +441,17 @@ ISR(TIMER0_OVF_vect)
/* restart timer */ /* restart timer */
TCNT0 = TIMER_RELOAD; TCNT0 = TIMER_RELOAD;
if (boot_wait == BOOTWAIT_RUNNING) { switch (boot_timeout) {
PORTD ^= (1<<LED); default:
boot_timeout--;
/* fall-through */
boot_timeout--; case BOOTWAIT_INTERRUPTED:
if (boot_timeout == 0) { LED_TOGGLE();
boot_wait = BOOTWAIT_EXPIRED; /* fall-through */
}
case BOOTWAIT_EXPIRED:
break;
} }
} }
@ -456,7 +471,8 @@ int main(void) __attribute__ ((noreturn));
int main(void) int main(void)
{ {
/* LED and TXEN are outputs */ /* LED and TXEN are outputs */
DDRD |= (1<<LED) | (1<<RXTX); LED_INIT();
RXTX_DDR |= (1<<RXTX_NUM);
#if defined(OSCCAL_VALUE) #if defined(OSCCAL_VALUE)
OSCCAL = OSCCAL_VALUE; OSCCAL = OSCCAL_VALUE;
@ -491,7 +507,7 @@ int main(void)
sei(); sei();
while (boot_wait != BOOTWAIT_EXPIRED); while (boot_timeout != BOOTWAIT_EXPIRED);
cli(); cli();
@ -506,7 +522,7 @@ int main(void)
#endif #endif
/* disable LED */ /* disable LED */
PORTD |= (1<<LED); LED_OFF();
uint16_t wait = 0x0000; uint16_t wait = 0x0000;
do { do {