From 28e9e122af24e63e84d5bcd2c619b76c490970fc Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Sat, 31 Dec 2011 12:19:08 -0500 Subject: reset the Leonardo board when leaving the bootloader, before starting the sketch Done so all IO and other registers are properly reinitialized when a new sketch is uploaded or when an existing sketch is started. Uses a watchdog timeout with a 15 ms period to accomplish the reset. Bootloader checks the reason for reset and only enumerates as bootloader and enters the programming loop if reset was NOT caused by WDT. --- bootloaders/diskloader/src/DiskLoader.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'bootloaders/diskloader/src/DiskLoader.cpp') diff --git a/bootloaders/diskloader/src/DiskLoader.cpp b/bootloaders/diskloader/src/DiskLoader.cpp index 6580618..09f59a7 100644 --- a/bootloaders/diskloader/src/DiskLoader.cpp +++ b/bootloaders/diskloader/src/DiskLoader.cpp @@ -56,7 +56,7 @@ void Program(u8 ep, u16 page, u8 count) boot_rww_enable (); } - +void StartSketch(); int USBGetChar(); #define getch USBGetChar @@ -111,10 +111,15 @@ int main(void) __attribute__ ((naked)); // STK500v1 main loop, very similar to optiboot in protocol and implementation int main() { + uint8_t MCUSR_state = MCUSR; // store the reason for the reset + MCUSR &= ~(1 << WDRF); // must clear the watchdog reset flag before disabling and reenabling WDT wdt_disable(); TXLED0; RXLED0; - LED0; + LED0; + if (MCUSR_state & (1< Date: Tue, 10 Jan 2012 15:38:26 -0500 Subject: made the bootloader's LED control macro names less offensive TX_LED_OFF() instead of TXLED0, etc. --- bootloaders/diskloader/src/DiskLoader.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'bootloaders/diskloader/src/DiskLoader.cpp') diff --git a/bootloaders/diskloader/src/DiskLoader.cpp b/bootloaders/diskloader/src/DiskLoader.cpp index 09f59a7..f5df8a4 100644 --- a/bootloaders/diskloader/src/DiskLoader.cpp +++ b/bootloaders/diskloader/src/DiskLoader.cpp @@ -114,9 +114,9 @@ int main() uint8_t MCUSR_state = MCUSR; // store the reason for the reset MCUSR &= ~(1 << WDRF); // must clear the watchdog reset flag before disabling and reenabling WDT wdt_disable(); - TXLED0; - RXLED0; - LED0; + TX_LED_OFF(); + RX_LED_OFF(); + L_LED_OFF(); if (MCUSR_state & (1< p) - LED0; + L_LED_OFF(); else - LED1; + L_LED_ON(); } void StartSketch() { - TXLED0; // switch off the RX and TX LEDs before starting the user sketch - RXLED0; + TX_LED_OFF(); // switch off the RX and TX LEDs before starting the user sketch + RX_LED_OFF(); UDCON = 1; // Detatch USB UDIEN = 0; asm volatile ( // Reset vector to run firmware -- cgit v1.2.3-18-g5258 From 94443a2a7700976c148843c2bb7d46e05744cff8 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Tue, 10 Jan 2012 15:51:44 -0500 Subject: got rid of u8, u16, u32 typedefs in Diskloader done to bring types in line with others in Arduino core --- bootloaders/diskloader/src/DiskLoader.cpp | 60 +++++++++++++++---------------- 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'bootloaders/diskloader/src/DiskLoader.cpp') diff --git a/bootloaders/diskloader/src/DiskLoader.cpp b/bootloaders/diskloader/src/DiskLoader.cpp index f5df8a4..d14c7fb 100644 --- a/bootloaders/diskloader/src/DiskLoader.cpp +++ b/bootloaders/diskloader/src/DiskLoader.cpp @@ -26,15 +26,15 @@ void entrypoint(void) ::); } -u8 _flashbuf[128]; -u8 _inSync; -u8 _ok; -extern volatile u8 _ejected; -extern volatile u16 _timeout; +uint8_t _flashbuf[128]; +uint8_t _inSync; +uint8_t _ok; +extern volatile uint8_t _ejected; +extern volatile uint16_t _timeout; -void Program(u8 ep, u16 page, u8 count) +void Program(uint8_t ep, uint16_t page, uint8_t count) { - u8 write = page < 30*1024; // Don't write over firmware please + uint8_t write = page < 30*1024; // Don't write over firmware please if (write) boot_page_erase(page); @@ -46,9 +46,9 @@ void Program(u8 ep, u16 page, u8 count) boot_spm_busy_wait(); // Wait until the memory is erased. count >>= 1; - u16* p = (u16*)page; - u16* b = (u16*)_flashbuf; - for (u8 i = 0; i < count; i++) + uint16_t* p = (uint16_t*)page; + uint16_t* b = (uint16_t*)_flashbuf; + for (uint8_t i = 0; i < count; i++) boot_page_fill(p++, b[i]); boot_page_write(page); @@ -78,8 +78,8 @@ int USBGetChar(); #define STK_READ_PAGE 0x74 // 't' #define STK_READ_SIGN 0x75 // 'u' -extern const u8 _readSize[] PROGMEM; -const u8 _readSize[] = +extern const uint8_t _readSize[] PROGMEM; +const uint8_t _readSize[] = { STK_GET_PARAMETER, 1, STK_SET_DEVICE, 20, @@ -91,8 +91,8 @@ const u8 _readSize[] = 0,0 }; -extern const u8 _consts[] PROGMEM; -const u8 _consts[] = +extern const uint8_t _consts[] PROGMEM; +const uint8_t _consts[] = { SIGNATURE_0, SIGNATURE_1, @@ -131,18 +131,18 @@ int main() for(;;) { - u8* packet = _flashbuf; - u16 address = 0; + uint8_t* packet = _flashbuf; + uint16_t address = 0; for (;;) { - u8 cmd = getch(); + uint8_t cmd = getch(); // Read packet contents - u8 len; - const u8* rs = _readSize; + uint8_t len; + const uint8_t* rs = _readSize; for(;;) { - u8 c = pgm_read_byte(rs++); + uint8_t c = pgm_read_byte(rs++); len = pgm_read_byte(rs++); if (c == cmd || c == 0) break; @@ -152,11 +152,11 @@ int main() Recv(CDC_RX,packet,len); // Send a response - u8 send = 0; - const u8* pgm = _consts+7; // 0 + uint8_t send = 0; + const uint8_t* pgm = _consts+7; // 0 if (STK_GET_PARAMETER == cmd) { - u8 i = packet[0] - 0x80; + uint8_t i = packet[0] - 0x80; if (i > 2) i = (i == 0x18) ? 3 : 4; // 0x80:HW_VER,0x81:SW_MAJOR,0x82:SW_MINOR,0x18:3 or 0 pgm = _consts + i + 3; @@ -179,7 +179,7 @@ int main() else if (STK_LOAD_ADDRESS == cmd) { - address = *((u16*)packet); // word addresses + address = *((uint16_t*)packet); // word addresses address += address; } @@ -191,7 +191,7 @@ int main() else if (STK_READ_PAGE == cmd) { send = packet[1]; - pgm = (const u8*)address; + pgm = (const uint8_t*)address; address += send; // not sure of this is required } @@ -216,15 +216,15 @@ int main() } // Nice breathing LED indicates we are in the firmware -u16 _pulse; +uint16_t _pulse; void LEDPulse() { _pulse += 4; - u8 p = _pulse >> 9; + uint8_t p = _pulse >> 9; if (p > 63) p = 127-p; p += p; - if (((u8)_pulse) > p) + if (((uint8_t)_pulse) > p) L_LED_OFF(); else L_LED_ON(); @@ -234,7 +234,7 @@ void StartSketch() { TX_LED_OFF(); // switch off the RX and TX LEDs before starting the user sketch RX_LED_OFF(); - UDCON = 1; // Detatch USB + UDCON = 1; // Detach USB UDIEN = 0; asm volatile ( // Reset vector to run firmware "clr r30\n" @@ -245,7 +245,7 @@ void StartSketch() void Reset() { - wdt_enable(WDTO_15MS); + wdt_enable(WDTO_15MS); // reset the microcontroller to reinitialize all IO and other registers for (;;) ; } -- cgit v1.2.3-18-g5258 From 0ea882bc247b3cca8788f329d9fe802107fa8960 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Tue, 10 Jan 2012 16:00:46 -0500 Subject: fixed warning when comparing pgm_read_word(0) to -1 (thanks, Limor Fried and Phillip Torrone) --- bootloaders/diskloader/src/DiskLoader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bootloaders/diskloader/src/DiskLoader.cpp') diff --git a/bootloaders/diskloader/src/DiskLoader.cpp b/bootloaders/diskloader/src/DiskLoader.cpp index d14c7fb..7ef6fa8 100644 --- a/bootloaders/diskloader/src/DiskLoader.cpp +++ b/bootloaders/diskloader/src/DiskLoader.cpp @@ -117,7 +117,7 @@ int main() TX_LED_OFF(); RX_LED_OFF(); L_LED_OFF(); - if (MCUSR_state & (1< Date: Tue, 10 Jan 2012 16:29:47 -0500 Subject: renamed DiskLoader to Caterina (after Leonardo's mom) --- bootloaders/diskloader/src/DiskLoader.cpp | 251 ------------------------------ 1 file changed, 251 deletions(-) delete mode 100644 bootloaders/diskloader/src/DiskLoader.cpp (limited to 'bootloaders/diskloader/src/DiskLoader.cpp') diff --git a/bootloaders/diskloader/src/DiskLoader.cpp b/bootloaders/diskloader/src/DiskLoader.cpp deleted file mode 100644 index 7ef6fa8..0000000 --- a/bootloaders/diskloader/src/DiskLoader.cpp +++ /dev/null @@ -1,251 +0,0 @@ - - -#include "Platform.h" - -// This bootloader creates a composite Serial device -// -// The serial interface supports a STK500v1 protocol that is very similar to optiboot -// -// The bootloader will timeout and start the firmware after a few hundred milliseconds -// if a usb connection is not detected. -// -// The tweakier code is to keep the bootloader below 2k (no interrupt table, for example) - -extern "C" -void entrypoint(void) __attribute__ ((naked)) __attribute__ ((section (".vectors"))); -void entrypoint(void) -{ - asm volatile ( - "eor r1, r1\n" // Zero register - "out 0x3F, r1\n" // SREG - "ldi r28, 0xFF\n" - "ldi r29, 0x0A\n" - "out 0x3E, r29\n" // SPH - "out 0x3D, r28\n" // SPL - "rjmp main" // Stack is all set up, start the main code - ::); -} - -uint8_t _flashbuf[128]; -uint8_t _inSync; -uint8_t _ok; -extern volatile uint8_t _ejected; -extern volatile uint16_t _timeout; - -void Program(uint8_t ep, uint16_t page, uint8_t count) -{ - uint8_t write = page < 30*1024; // Don't write over firmware please - if (write) - boot_page_erase(page); - - Recv(ep,_flashbuf,count); // Read while page is erasing - - if (!write) - return; - - boot_spm_busy_wait(); // Wait until the memory is erased. - - count >>= 1; - uint16_t* p = (uint16_t*)page; - uint16_t* b = (uint16_t*)_flashbuf; - for (uint8_t i = 0; i < count; i++) - boot_page_fill(p++, b[i]); - - boot_page_write(page); - boot_spm_busy_wait(); - boot_rww_enable (); -} - -void StartSketch(); -int USBGetChar(); -#define getch USBGetChar - -#define HW_VER 0x02 -#define SW_MAJOR 0x01 -#define SW_MINOR 0x10 - -#define STK_OK 0x10 -#define STK_INSYNC 0x14 // ' ' -#define CRC_EOP 0x20 // 'SPACE' -#define STK_GET_SYNC 0x30 // '0' - -#define STK_GET_PARAMETER 0x41 // 'A' -#define STK_SET_DEVICE 0x42 // 'B' -#define STK_SET_DEVICE_EXT 0x45 // 'E' -#define STK_LOAD_ADDRESS 0x55 // 'U' -#define STK_UNIVERSAL 0x56 // 'V' -#define STK_PROG_PAGE 0x64 // 'd' -#define STK_READ_PAGE 0x74 // 't' -#define STK_READ_SIGN 0x75 // 'u' - -extern const uint8_t _readSize[] PROGMEM; -const uint8_t _readSize[] = -{ - STK_GET_PARAMETER, 1, - STK_SET_DEVICE, 20, - STK_SET_DEVICE_EXT, 5, - STK_UNIVERSAL, 4, - STK_LOAD_ADDRESS, 2, - STK_PROG_PAGE, 3, - STK_READ_PAGE, 3, - 0,0 -}; - -extern const uint8_t _consts[] PROGMEM; -const uint8_t _consts[] = -{ - SIGNATURE_0, - SIGNATURE_1, - SIGNATURE_2, - HW_VER, // Hardware version - SW_MAJOR, // Software major version - SW_MINOR, // Software minor version - 0x03, // Unknown but seems to be required by avr studio 3.56 - 0x00, // -}; - - -void USBInit(void); -int main(void) __attribute__ ((naked)); - -// STK500v1 main loop, very similar to optiboot in protocol and implementation -int main() -{ - uint8_t MCUSR_state = MCUSR; // store the reason for the reset - MCUSR &= ~(1 << WDRF); // must clear the watchdog reset flag before disabling and reenabling WDT - wdt_disable(); - TX_LED_OFF(); - RX_LED_OFF(); - L_LED_OFF(); - if (MCUSR_state & (1< 2) - i = (i == 0x18) ? 3 : 4; // 0x80:HW_VER,0x81:SW_MAJOR,0x82:SW_MINOR,0x18:3 or 0 - pgm = _consts + i + 3; - send = 1; - } - - else if (STK_UNIVERSAL == cmd) - { - if (packet[0] == 0x30) - pgm = _consts + packet[2]; // read signature - send = 1; - } - - // Read signature bytes - else if (STK_READ_SIGN == cmd) - { - pgm = _consts; - send = 3; - } - - else if (STK_LOAD_ADDRESS == cmd) - { - address = *((uint16_t*)packet); // word addresses - address += address; - } - - else if (STK_PROG_PAGE == cmd) - { - Program(CDC_RX,address,packet[1]); - } - - else if (STK_READ_PAGE == cmd) - { - send = packet[1]; - pgm = (const uint8_t*)address; - address += send; // not sure of this is required - } - - // Check sync - if (getch() != ' ') - break; - Transfer(CDC_TX,&_inSync,1); - - // Send result - if (send) - Transfer(CDC_TX|TRANSFER_PGM,pgm,send); // All from pgm memory - - // Send ok - Transfer(CDC_TX|TRANSFER_RELEASE,&_ok,1); - - if (cmd == 'Q') - break; - } - _timeout = 500; // wait a moment before exiting the bootloader - may need to finish responding to 'Q' for example - _ejected = 1; - } -} - -// Nice breathing LED indicates we are in the firmware -uint16_t _pulse; -void LEDPulse() -{ - _pulse += 4; - uint8_t p = _pulse >> 9; - if (p > 63) - p = 127-p; - p += p; - if (((uint8_t)_pulse) > p) - L_LED_OFF(); - else - L_LED_ON(); -} - -void StartSketch() -{ - TX_LED_OFF(); // switch off the RX and TX LEDs before starting the user sketch - RX_LED_OFF(); - UDCON = 1; // Detach USB - UDIEN = 0; - asm volatile ( // Reset vector to run firmware - "clr r30\n" - "clr r31\n" - "ijmp\n" - ::); -} - -void Reset() -{ - wdt_enable(WDTO_15MS); // reset the microcontroller to reinitialize all IO and other registers - for (;;) - ; -} -- cgit v1.2.3-18-g5258