port to atmega644 and reconfigure watchdog

This commit is contained in:
David Voswinkel 2009-07-20 23:12:01 +02:00
parent 8bee3f786f
commit 9e1a9fffc5
3 changed files with 46 additions and 28 deletions

View File

@ -1,7 +1,7 @@
# microcontroller and project specific settings
TARGET = bootloader
F_CPU = 20000000UL
F_CPU = 20000000
MCU = atmega644
SRC = bootloader.c
@ -11,7 +11,7 @@ OBJECTS += $(patsubst %.S,%.o,${ASRC})
HEADERS += $(shell echo *.h)
# CFLAGS += -Werror
LDFLAGS += -L/usr/local/avr/avr/lib
CFLAGS += -Iusbdrv -I.
CFLAGS += -Iusbdrv -I. -Os
CFLAGS += -DHARDWARE_REV=$(HARDWARE_REV)
ASFLAGS += -x assembler-with-cpp
ASFLAGS += -Iusbdrv -I.
@ -37,8 +37,11 @@ else ifeq ($(MCU),atmega88)
BOOT_SECTION_START = 0x1800
else ifeq ($(MCU),atmega644)
# atmega88 with 1024 words bootloader:
# bootloader section starts at 0x7e00 (word-address) == 0xFC00 (byte-address)
BOOT_SECTION_START = 0xFC00
# bootloader section starts at 0x7c00 (word-address) == 0xF800 (byte-address)
# atmega88 with 2024 words bootloader:
# bootloader section starts at 0x7800 (word-address) == 0xF000 (byte-address)
BOOT_SECTION_START = 0xf000
endif
LDFLAGS += -Wl,--section-start=.text=$(BOOT_SECTION_START)
@ -54,7 +57,7 @@ all: $(TARGET).hex $(TARGET).lss
@echo "$(TARGET) compiled for: $(MCU)"
@echo -n "size is: "
@$(SIZE) -A $(TARGET).hex | grep "\.sec1" | tr -s " " | cut -d" " -f2
@echo "max size:2048 bytes"
@echo "max size:4096 bytes"
@echo "==============================="
$(TARGET): $(OBJECTS) $(TARGET).o

View File

@ -1,5 +1,5 @@
# Programmer used for In System Programming
ISP_PROG = usbap
ISP_PROG = usbasp
# device the ISP programmer is connected to
ISP_DEV = /dev/tty.PL2303-00002126
# Programmer used for serial programming (using the bootloader)
@ -39,7 +39,7 @@ endif
AVRDUDE_FLAGS += -p $(AVRDUDE_MCU) -b $(AVRDUDE_BAUDRATE)
AVRDUDE_FLAGS += -p $(AVRDUDE_MCU)
# flags for the compiler
CFLAGS += -g -Os -finline-limit=800 -mmcu=$(MCU) -DF_CPU=$(F_CPU) -std=gnu99
@ -76,8 +76,8 @@ interactive-serial:
.PHONY: all clean interactive-isp interactive-serial launch-bootloader
program-isp-%: %.hex
$(AVRDUDE) $(AVRDUDE_FLAGS) -c $(ISP_PROG) -P $(ISP_DEV) -U flash:w:$<
flash: bootloader.hex
$(AVRDUDE) $(AVRDUDE_FLAGS) -c $(ISP_PROG) -U flash:w:$<
program-isp-eeprom-%: %.eep.hex
$(AVRDUDE) $(AVRDUDE_FLAGS) -c $(ISP_PROG) -P $(ISP_DEV) -U eeprom:w:$<

View File

@ -64,8 +64,8 @@
(LED_DIR &=~ (1 << LED_PIN))); }
#define DLED_OFF {((LED_PORT &=~ (1 << LED_PIN)),\
(LED_DIR |= (1 << LED_PIN))); }
#define DLED_TGL {((LED_PORT ^= (1 << LED_PIN)),\
(LED_DIR |= (1 << LED_PIN)));}
#define DLED_TGL {((LED_PORT &=~ (1 << LED_PIN)),\
(LED_DIR ^= (1 << LED_PIN)));}
@ -318,8 +318,8 @@ void leave_bootloader(void)
/* disconnect usb */
usbDeviceDisconnect();
for (uint8_t i = 0; i < 38; i++)
_delay_loop_2(0); /* 0 means 0x10000, 38*1/f*0x10000 =~ 498ms */
for (uint8_t i = 0; i < 50; i++)
_delay_ms(10); /* 0 means 0x10000, 38*1/f*0x10000 =~ 498ms */
/* enable watchdog to soft-reset the uC for clean startup of new application */
wdt_enable(WDTO_15MS);
@ -334,36 +334,46 @@ int __attribute__ ((noreturn,OS_main)) main(void)
{
/* start bootloader */
/* init LED-Pin */
PORTB = 0;
#ifdef DEBUG_UART
/* init uart (115200 baud, at 20mhz) */
UBRR0L = 10;
UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
UCSR0B = _BV(TXEN0);
putc('b');
#endif
putc('a');
wdt_disable();
uint8_t reset = MCUSR;
reset=0;
uint16_t delay =0;
timeout = TIMEOUT;
/* if power-on reset, quit bootloader via watchdog reset */
if (reset & _BV(PORF)){
putc('p');
MCUSR = 0;
leave_bootloader();
}
/* if watchdog reset, disable watchdog and jump to app */
else if(reset & _BV(WDRF)){
MCUSR = 0;
//WDTCSR |= (1<<WDCE);
//WDTCSR &= ~((1<<WDIE) | (1<<WDE));
wdt_disable();
putc('r');
while(1);
jump_to_app();
}
while(1);
/* else: enter programming mode */
putc('u');
/* clear external reset flags */
MCUSR = 0;
@ -372,40 +382,45 @@ int __attribute__ ((noreturn,OS_main)) main(void)
request_exit = 0;
/* move interrupts to boot section */
MCUCR = (1 << IVCE);
MCUCR = (1 << IVSEL);
/* enable interrupts */
sei();
cli();
/* initialize usb pins */
usbInit();
/* disconnect for ~500ms, so that the host re-enumerates this device */
putc('d');
usbDeviceDisconnect();
for (uint8_t i = 0; i < 38; i++)
_delay_loop_2(0); /* 0 means 0x10000, 38*1/f*0x10000 =~ 498ms */
for (uint8_t i = 0; i < 50; i++)
_delay_ms(10); /* 0 means 0x10000, 38*1/f*0x10000 =~ 498ms */
usbDeviceConnect();
uint16_t delay;
timeout = TIMEOUT;
putc('c');
/* enable interrupts */
sei();
while(1) {
//wdt_reset();
usbPoll();
delay++;
/* do some led blinking, so that it is visible that the bootloader is still running */
if (delay == 0) {
putc('p');
DLED_TGL;
if (timeout < 255)
timeout--;
}
if (request_exit || timeout == 0) {
_delay_loop_2(0);
putc('e');
_delay_ms(10);
leave_bootloader();
}
}
putc('l');
leave_bootloader();
}