get bootloader working
This commit is contained in:
parent
31989233b4
commit
68d4ffc7f1
@ -10,9 +10,11 @@ OBJECTS += $(patsubst %.c,%.o,${SRC})
|
||||
OBJECTS += $(patsubst %.S,%.o,${ASRC})
|
||||
HEADERS += $(shell echo *.h)
|
||||
# CFLAGS += -Werror
|
||||
|
||||
LDFLAGS += -L/usr/local/avr/avr/lib
|
||||
CFLAGS += -Iusbdrv -I.
|
||||
CFLAGS += -DHARDWARE_REV=$(HARDWARE_REV)
|
||||
CDEFS += -DF_CPU
|
||||
ASFLAGS += -x assembler-with-cpp
|
||||
ASFLAGS += -Iusbdrv -I.
|
||||
|
||||
@ -36,12 +38,12 @@ else ifeq ($(MCU),atmega88)
|
||||
# bootloader section starts at 0xc00 (word-address) == 0x1800 (byte-address)
|
||||
BOOT_SECTION_START = 0x1800
|
||||
else ifeq ($(MCU),atmega644)
|
||||
# atmega88 with 2048 words bootloader:
|
||||
# atmega644 with 2048 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)
|
||||
LDFLAGS += -Wl,--section-start=.text=$(BOOT_SECTION_START) -Wl,-u,vfprintf
|
||||
CFLAGS += -DBOOT_SECTION_START=$(BOOT_SECTION_START)
|
||||
|
||||
|
||||
|
||||
@ -15,7 +15,6 @@ AS = avr-as
|
||||
CP = cp
|
||||
RM = rm -f
|
||||
AVRDUDE = avrdude
|
||||
AVRDUDE_BAUDRATE = 19200
|
||||
SIZE = avr-size
|
||||
|
||||
-include $(CURDIR)/config.mk
|
||||
|
||||
@ -95,16 +95,38 @@ static const uint8_t signature[4] = {
|
||||
# error "BOOT_SECTION_START undefined!"
|
||||
#endif
|
||||
|
||||
#if defined (__AVR_ATmega644__)
|
||||
/* Due arvdude limitations we can't erase the whole progmem
|
||||
without running into an usb timeount on cleint side. So we
|
||||
we limit the erase section by 0x1000
|
||||
*/
|
||||
#define ERASE_SECTION 0xe000
|
||||
#else
|
||||
#define ERASE_SECTION BOOT_SECTION_START
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef DEBUG_UART
|
||||
static __attribute__ (( __noinline__ )) void putc(uint8_t data) {
|
||||
static __attribute__ (( __noinline__ )) void uart_putc(uint8_t data) {
|
||||
while(!(UCSR0A & _BV(UDRE0)));
|
||||
UDR0 = data;
|
||||
}
|
||||
#else
|
||||
#define putc(x)
|
||||
#define uart_putc(x)
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_UART
|
||||
static __attribute__ (( __noinline__ )) void uart_puts(uint8_t *data) {
|
||||
while(*data){
|
||||
uart_putc(*data);
|
||||
data++;
|
||||
}
|
||||
}
|
||||
#else
|
||||
#define uart_puts(x)
|
||||
#endif
|
||||
|
||||
|
||||
/* supply custom usbDeviceConnect() and usbDeviceDisconnect() macros
|
||||
* which turn the interrupt on and off at the right times,
|
||||
* and prevent the execution of an interrupt while the pullup resistor
|
||||
@ -150,6 +172,8 @@ uint8_t request_exit;
|
||||
|
||||
uint8_t timeout;
|
||||
|
||||
|
||||
|
||||
usbMsgLen_t usbFunctionSetup(uchar data[8])
|
||||
{
|
||||
usbRequest_t *req = (void *)data;
|
||||
@ -204,16 +228,20 @@ usbMsgLen_t usbFunctionSetup(uchar data[8])
|
||||
|
||||
/* catch a chip erase */
|
||||
} else if (data[2] == ISP_CHIP_ERASE1 && data[3] == ISP_CHIP_ERASE2) {
|
||||
uart_puts("\n\rErase Flash");
|
||||
for (flash_address.word = 0;
|
||||
flash_address.word < BOOT_SECTION_START;
|
||||
flash_address.word < ERASE_SECTION;
|
||||
flash_address.word += SPM_PAGESIZE) {
|
||||
|
||||
/* wait and erase page */
|
||||
boot_spm_busy_wait();
|
||||
if (flash_address.word && flash_address.word%1024 == 0 )
|
||||
uart_putc('.');
|
||||
cli();
|
||||
boot_page_erase(flash_address.word);
|
||||
sei();
|
||||
}
|
||||
uart_puts("\n\r");
|
||||
}
|
||||
|
||||
/* in case no data has been filled in by the if's above, just return zeroes */
|
||||
@ -228,10 +256,6 @@ usbMsgLen_t usbFunctionSetup(uchar data[8])
|
||||
#endif
|
||||
} else if (req->bRequest >= USBASP_FUNC_READFLASH) {
|
||||
/* && req->bRequest <= USBASP_FUNC_SETLONGADDRESS */
|
||||
|
||||
putc('R');
|
||||
putc(req->bRequest);
|
||||
|
||||
/* extract address and length */
|
||||
flash_address.word = req->wValue.word;
|
||||
bytes_remaining = req->wLength.bytes[0];
|
||||
@ -248,11 +272,11 @@ uchar usbFunctionWrite(uchar *data, uchar len)
|
||||
if (len > bytes_remaining)
|
||||
len = bytes_remaining;
|
||||
bytes_remaining -= len;
|
||||
|
||||
if (request == USBASP_FUNC_WRITEEEPROM) {
|
||||
for (uint8_t i = 0; i < len; i++)
|
||||
eeprom_write_byte((uint8_t *)flash_address.word++, *data++);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
/* data is handled wordwise, adjust len */
|
||||
len /= 2;
|
||||
len -= 1;
|
||||
@ -334,47 +358,42 @@ int __attribute__ ((noreturn,OS_main)) main(void)
|
||||
{
|
||||
/* start bootloader */
|
||||
|
||||
|
||||
#ifdef DEBUG_UART
|
||||
/* init uart (115200 baud, at 20mhz) */
|
||||
UBRR0L = 10;
|
||||
UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
|
||||
UCSR0B = _BV(TXEN0);
|
||||
#endif
|
||||
putc('0');
|
||||
|
||||
|
||||
|
||||
uint8_t reset = MCUSR;
|
||||
uint16_t delay =0;
|
||||
timeout = TIMEOUT;
|
||||
|
||||
uart_puts("Snesram Bootloader v0.1\n\r");
|
||||
|
||||
/* if power-on reset, quit bootloader via watchdog reset */
|
||||
if (reset & _BV(PORF)){
|
||||
putc('P');
|
||||
uart_puts("Found power on reset\n\r");
|
||||
MCUSR = 0;
|
||||
leave_bootloader();
|
||||
}
|
||||
/* if watchdog reset, disable watchdog and jump to app */
|
||||
else if(reset & _BV(WDRF)){
|
||||
|
||||
MCUSR = 0;
|
||||
uart_puts("Found watchdog reset\n\r");
|
||||
MCUSR = 0;
|
||||
wdt_disable();
|
||||
putc('W');
|
||||
|
||||
DLED_TGL;
|
||||
_delay_ms(500);
|
||||
DLED_TGL;
|
||||
_delay_ms(500);
|
||||
|
||||
jump_to_app();
|
||||
uart_puts("Jump to main\n\r");
|
||||
jump_to_app();
|
||||
}
|
||||
|
||||
|
||||
uart_puts("Enter programming mode\n\r");
|
||||
/* else: enter programming mode */
|
||||
|
||||
|
||||
/* clear external reset flags */
|
||||
MCUSR = 0;
|
||||
|
||||
@ -394,16 +413,11 @@ int __attribute__ ((noreturn,OS_main)) main(void)
|
||||
|
||||
|
||||
/* disconnect for ~500ms, so that the host re-enumerates this device */
|
||||
putc('d');
|
||||
usbDeviceDisconnect();
|
||||
for (uint8_t i = 0; i < 50; i++)
|
||||
_delay_ms(10); /* 0 means 0x10000, 38*1/f*0x10000 =~ 498ms */
|
||||
usbDeviceConnect();
|
||||
putc('c');
|
||||
|
||||
|
||||
|
||||
|
||||
uart_puts("Wait for firmware");
|
||||
while(1) {
|
||||
usbPoll();
|
||||
|
||||
@ -411,14 +425,14 @@ int __attribute__ ((noreturn,OS_main)) main(void)
|
||||
|
||||
/* do some led blinking, so that it is visible that the bootloader is still running */
|
||||
if (delay == 0) {
|
||||
putc('p');
|
||||
uart_putc('.');
|
||||
DLED_TGL;
|
||||
if (timeout < 255)
|
||||
timeout--;
|
||||
}
|
||||
|
||||
if (request_exit || timeout == 0) {
|
||||
putc('e');
|
||||
uart_puts("\n\rExit\n\r");
|
||||
_delay_ms(10);
|
||||
leave_bootloader();
|
||||
}
|
||||
|
||||
@ -16,3 +16,7 @@
|
||||
|
||||
/* uncomment this for debug information via uart */
|
||||
#define DEBUG_UART
|
||||
|
||||
|
||||
#define DEBUG 1
|
||||
#define DEBUG_USB 2
|
||||
|
||||
@ -2,9 +2,9 @@
|
||||
TTY = /dev/tty.PL2303-00002126
|
||||
DEVICE = atmega644
|
||||
F_CPU = 20000000 # in Hz
|
||||
|
||||
TARGET = main
|
||||
AVRDUDE = avrdude -c usbasp -p $(DEVICE)
|
||||
|
||||
SIZE = avr-size
|
||||
CFLAGS = -Iusbdrv -I. -DDEBUG_LEVEL=0
|
||||
|
||||
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o usb_bulk.o uart.o fifo.o sram.o crc.o debug.o dump.o timer.o watchdog.o
|
||||
@ -31,6 +31,11 @@ help:
|
||||
all: hex
|
||||
|
||||
hex: main.hex
|
||||
@echo "==============================="
|
||||
@echo "$(TARGET) compiled for: $(DEVICE)"
|
||||
@echo -n "size is: "
|
||||
@$(SIZE) -A $(TARGET).hex | grep "\.sec1" | tr -s " " | cut -d" " -f2
|
||||
@echo "==============================="
|
||||
|
||||
program: flash fuse
|
||||
|
||||
|
||||
@ -30,9 +30,13 @@ spc: $(SPCOFILES)
|
||||
$(SPCOFILES): $(SPCSFILES)
|
||||
$(SPCAC) $(AFLAGS) $(SPCSFILES)
|
||||
|
||||
header:
|
||||
$(UCON) -smc $(ROMFILE)
|
||||
|
||||
flash:
|
||||
$(FL) $(FLFLAGS) $(ROMFILE)
|
||||
upload:
|
||||
|
||||
upload: header
|
||||
$(UCON) $(UCONFLAGS) $(ROMFILE)
|
||||
run:
|
||||
$(EMU) $(ROMFILE)
|
||||
|
||||
300
snesram.tmproj
300
snesram.tmproj
@ -2,8 +2,6 @@
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>currentDocument</key>
|
||||
<string>avr/usbload/main.c</string>
|
||||
<key>documents</key>
|
||||
<array>
|
||||
<dict>
|
||||
@ -21,194 +19,6 @@
|
||||
<integer>271</integer>
|
||||
<key>metaData</key>
|
||||
<dict>
|
||||
<key>avr/usbload/commandline/snesuploader.c</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>200</integer>
|
||||
</dict>
|
||||
<key>columnSelection</key>
|
||||
<false/>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>172</integer>
|
||||
<key>selectFrom</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>195</integer>
|
||||
</dict>
|
||||
<key>selectTo</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>200</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>avr/usbload/config.h</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>40</integer>
|
||||
<key>line</key>
|
||||
<integer>18</integer>
|
||||
</dict>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>avr/usbload/dump.c</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>1</integer>
|
||||
<key>line</key>
|
||||
<integer>50</integer>
|
||||
</dict>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>avr/usbload/dump.h</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>2</integer>
|
||||
</dict>
|
||||
<key>columnSelection</key>
|
||||
<false/>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>0</integer>
|
||||
<key>selectFrom</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>selectTo</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>2</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>avr/usbload/fifo.h</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>avr/usbload/main.c</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>22</integer>
|
||||
<key>line</key>
|
||||
<integer>25</integer>
|
||||
</dict>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>29</integer>
|
||||
</dict>
|
||||
<key>avr/usbload/requests.h</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>26</integer>
|
||||
</dict>
|
||||
<key>columnSelection</key>
|
||||
<false/>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>0</integer>
|
||||
<key>selectFrom</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>34</integer>
|
||||
<key>line</key>
|
||||
<integer>33</integer>
|
||||
</dict>
|
||||
<key>selectTo</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>26</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>avr/usbload/sram.c</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>avr/usbload/watchdog.c</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>20</integer>
|
||||
<key>line</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>avr/usbload/watchdog.h</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>19</integer>
|
||||
</dict>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>poc/avr_sdcard/fat.h</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
@ -265,50 +75,7 @@
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>211</integer>
|
||||
</dict>
|
||||
<key>tools/ucon64/2.0/src/backup/snesram.c</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>53</integer>
|
||||
<key>line</key>
|
||||
<integer>204</integer>
|
||||
</dict>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>164</integer>
|
||||
</dict>
|
||||
<key>tools/ucon64/2.0/src/backup/snesram.h</key>
|
||||
<dict>
|
||||
<key>caret</key>
|
||||
<dict>
|
||||
<key>column</key>
|
||||
<integer>0</integer>
|
||||
<key>line</key>
|
||||
<integer>32</integer>
|
||||
</dict>
|
||||
<key>firstVisibleColumn</key>
|
||||
<integer>0</integer>
|
||||
<key>firstVisibleLine</key>
|
||||
<integer>19</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>openDocuments</key>
|
||||
<array>
|
||||
<string>avr/usbload/dump.c</string>
|
||||
<string>avr/usbload/sram.c</string>
|
||||
<string>avr/usbload/main.c</string>
|
||||
<string>avr/usbload/watchdog.h</string>
|
||||
<string>avr/usbload/dump.h</string>
|
||||
<string>avr/usbload/watchdog.c</string>
|
||||
<string>avr/usbload/config.h</string>
|
||||
<string>avr/usbload/requests.h</string>
|
||||
<string>tools/ucon64/2.0/src/backup/snesram.h</string>
|
||||
<string>avr/usbload/commandline/snesuploader.c</string>
|
||||
<string>tools/ucon64/2.0/src/backup/snesram.c</string>
|
||||
<string>avr/usbload/fifo.h</string>
|
||||
</array>
|
||||
<key>showFileHierarchyDrawer</key>
|
||||
<false/>
|
||||
<key>showFileHierarchyPanel</key>
|
||||
@ -327,6 +94,21 @@
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict>
|
||||
<key>bootloader</key>
|
||||
<dict>
|
||||
<key>isExpanded</key>
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict>
|
||||
<key>usbdrv</key>
|
||||
<dict>
|
||||
<key>isExpanded</key>
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict/>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>usbload</key>
|
||||
<dict>
|
||||
<key>isExpanded</key>
|
||||
@ -349,46 +131,30 @@
|
||||
<key>isExpanded</key>
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict/>
|
||||
<dict>
|
||||
<key>monitor</key>
|
||||
<dict>
|
||||
<key>isExpanded</key>
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict>
|
||||
<key>routines</key>
|
||||
<dict>
|
||||
<key>isExpanded</key>
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict/>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>tools</key>
|
||||
<dict>
|
||||
<key>isExpanded</key>
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict>
|
||||
<key>ucon64</key>
|
||||
<dict>
|
||||
<key>isExpanded</key>
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict>
|
||||
<key>2.0</key>
|
||||
<dict>
|
||||
<key>isExpanded</key>
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict>
|
||||
<key>src</key>
|
||||
<dict>
|
||||
<key>isExpanded</key>
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict>
|
||||
<key>backup</key>
|
||||
<dict>
|
||||
<key>isExpanded</key>
|
||||
<true/>
|
||||
<key>subItems</key>
|
||||
<dict/>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
<dict/>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user