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/USBCore.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bootloaders/diskloader/src/USBCore.cpp') diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp index 208121e..0467e46 100644 --- a/bootloaders/diskloader/src/USBCore.cpp +++ b/bootloaders/diskloader/src/USBCore.cpp @@ -34,7 +34,7 @@ u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */ u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */ -void Reboot(); +void Reset(); //================================================================== //================================================================== @@ -500,7 +500,7 @@ int USBGetChar() } if (!--_timeout) { - Reboot(); // USB not connected, run firmware + Reset(); } _delay_us(100); // stretch out the bootloader period to about 5 seconds after enumeration -- cgit v1.2.3-18-g5258 From 59ef51d752db28757184565c452640d323440480 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Tue, 10 Jan 2012 14:25:40 -0500 Subject: fix bug in handling of long descriptors for bootloader USB (thank you, Todd Krein, Limor Fried, and Phillip Torrone) descriptors longer than 255 bytes were being shortened, causing problems with enumeration under Windows --- bootloaders/diskloader/src/USBCore.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'bootloaders/diskloader/src/USBCore.cpp') diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp index 0467e46..a234596 100644 --- a/bootloaders/diskloader/src/USBCore.cpp +++ b/bootloaders/diskloader/src/USBCore.cpp @@ -351,7 +351,7 @@ u8 _cdcComposite = 0; bool SendDescriptor() { Setup& setup = _setup; - u8 desc_length = 0; + u16 desc_length = 0; const u8* desc_addr = 0; u8 t = setup.wValueH; @@ -387,13 +387,13 @@ bool SendDescriptor() if (desc_length == 0) desc_length = pgm_read_byte(desc_addr); - if ((u8)setup.wLength < desc_length) // bit of a cheat limiting to 255 bytes TODO (saved 8 bytes) - desc_length = (u8)setup.wLength; + if (setup.wLength < desc_length) + desc_length = setup.wLength; // Send descriptor // EP0 is 64 bytes long // RWAL and FIFOCON don't work on EP0 - u8 n = 0; + u16 n = 0; do { if (!WaitForINOrOUT()) -- cgit v1.2.3-18-g5258 From 648dd85e945a7e0a2db284987377a97936ee77e4 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Tue, 10 Jan 2012 15:31:56 -0500 Subject: added conditional compilation for HID, removed conditional compilation for CDC (is always used). disabled HID by default. also always enumerates as composite now. the bootloader must always have a CDC interface. HID is optional and not even complete to reduce size. --- bootloaders/diskloader/src/USBCore.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'bootloaders/diskloader/src/USBCore.cpp') diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp index a234596..64f5852 100644 --- a/bootloaders/diskloader/src/USBCore.cpp +++ b/bootloaders/diskloader/src/USBCore.cpp @@ -255,13 +255,13 @@ const u8 _initEndpoints[] = { 0, -#ifdef CDC_ENABLED EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN -#endif +#ifdef HID_ENABLED EP_TYPE_INTERRUPT_IN, // HID_ENDPOINT_INT +#endif }; static void InitEndpoints() @@ -357,13 +357,13 @@ bool SendDescriptor() u8 t = setup.wValueH; if (0x22 == t) { +#ifdef HID_ENABLED desc_addr = _rawHID; desc_length = sizeof(desc_length); +#endif } else if (USB_DEVICE_DESCRIPTOR_TYPE == t) { - if (setup.wLength == 8) - _cdcComposite = 1; - desc_addr = _cdcComposite ? (const u8*)&USB_DeviceDescriptorA : (const u8*)&USB_DeviceDescriptor; + desc_addr = (const u8*)&USB_DeviceDescriptor; } else if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t) { -- cgit v1.2.3-18-g5258 From dbec0f0058fdf33ef95b57f130c5ff8986f5c359 Mon Sep 17 00:00:00 2001 From: Zach Eveland 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/USBCore.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'bootloaders/diskloader/src/USBCore.cpp') diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp index 64f5852..160e6cf 100644 --- a/bootloaders/diskloader/src/USBCore.cpp +++ b/bootloaders/diskloader/src/USBCore.cpp @@ -89,7 +89,7 @@ static inline void ClearOUT(void) static void Send(volatile const u8* data, u8 count) { - TXLED1; // light the TX LED + TX_LED_ON(); // light the TX LED TxLEDPulse = TX_RX_LED_PULSE_MS; while (count--) UEDATX = *data++; @@ -97,7 +97,7 @@ void Send(volatile const u8* data, u8 count) void Recv(volatile u8* data, u8 count) { - RXLED1; // light the RX LED + RX_LED_ON(); // light the RX LED RxLEDPulse = TX_RX_LED_PULSE_MS; while (count--) *data++ = UEDATX; @@ -105,14 +105,14 @@ void Recv(volatile u8* data, u8 count) static inline u8 Recv8() { - RXLED1; // light the RX LED + RX_LED_ON(); // light the RX LED RxLEDPulse = TX_RX_LED_PULSE_MS; return UEDATX; } static inline void Send8(u8 d) { - TXLED1; // light the TX LED + TX_LED_ON(); // light the TX LED TxLEDPulse = TX_RX_LED_PULSE_MS; UEDATX = d; } @@ -473,9 +473,9 @@ void USBGeneralInterrupt() { // check whether the one-shot period has elapsed. if so, turn off the LED if (TxLEDPulse && !(--TxLEDPulse)) - TXLED0; + TX_LED_OFF(); if (RxLEDPulse && !(--RxLEDPulse)) - RXLED0; + RX_LED_OFF(); if (!_ejected) _timeout = 0; -- 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/USBCore.cpp | 118 ++++++++++++++++----------------- 1 file changed, 59 insertions(+), 59 deletions(-) (limited to 'bootloaders/diskloader/src/USBCore.cpp') diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp index 160e6cf..ac5d081 100644 --- a/bootloaders/diskloader/src/USBCore.cpp +++ b/bootloaders/diskloader/src/USBCore.cpp @@ -31,8 +31,8 @@ /** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */ #define TX_RX_LED_PULSE_MS 100 -u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */ -u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */ +uint8_t TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */ +uint8_t RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */ void Reset(); @@ -41,11 +41,11 @@ void Reset(); typedef struct { - u32 dwDTERate; - u8 bCharFormat; - u8 bParityType; - u8 bDataBits; - u8 lineState; + uint32_t dwDTERate; + uint8_t bCharFormat; + uint8_t bParityType; + uint8_t bDataBits; + uint8_t lineState; } LineInfo; static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 }; @@ -54,9 +54,9 @@ static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 }; //================================================================== // 4 bytes of RAM -volatile u8 _usbConfiguration; -volatile u8 _ejected; -volatile u16 _timeout; +volatile uint8_t _usbConfiguration; +volatile uint8_t _ejected; +volatile uint16_t _timeout; static inline void WaitIN(void) { @@ -74,7 +74,7 @@ static inline void WaitOUT(void) ; } -static inline u8 WaitForINOrOUT() +static inline uint8_t WaitForINOrOUT() { while (!(UEINTX & ((1<> 8) @@ -324,7 +324,7 @@ extern const u8 _rawHID[] PROGMEM; #define RAWHID_TX_SIZE 64 #define RAWHID_RX_SIZE 64 -const u8 _rawHID[] = +const uint8_t _rawHID[] = { // RAW HID 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30 @@ -346,15 +346,15 @@ const u8 _rawHID[] = 0xC0 // end collection }; -u8 _cdcComposite = 0; +uint8_t _cdcComposite = 0; bool SendDescriptor() { Setup& setup = _setup; - u16 desc_length = 0; - const u8* desc_addr = 0; + uint16_t desc_length = 0; + const uint8_t* desc_addr = 0; - u8 t = setup.wValueH; + uint8_t t = setup.wValueH; if (0x22 == t) { #ifdef HID_ENABLED @@ -363,23 +363,23 @@ bool SendDescriptor() #endif } else if (USB_DEVICE_DESCRIPTOR_TYPE == t) { - desc_addr = (const u8*)&USB_DeviceDescriptor; + desc_addr = (const uint8_t*)&USB_DeviceDescriptor; } else if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t) { - desc_addr = (const u8*)&USB_ConfigDescriptor; + desc_addr = (const uint8_t*)&USB_ConfigDescriptor; desc_length = sizeof(USB_ConfigDescriptor); } else if (USB_STRING_DESCRIPTOR_TYPE == t) { if (setup.wValueL == 0) - desc_addr = (const u8*)&STRING_LANGUAGE; + desc_addr = (const uint8_t*)&STRING_LANGUAGE; else if (setup.wValueL == IPRODUCT) - desc_addr = (const u8*)&STRING_IPRODUCT; + desc_addr = (const uint8_t*)&STRING_IPRODUCT; else if (setup.wValueL == ISERIAL) - desc_addr = (const u8*)&STRING_SERIAL; + desc_addr = (const uint8_t*)&STRING_SERIAL; else if (setup.wValueL == IMANUFACTURER) - desc_addr = (const u8*)&STRING_IMANUFACTURER; + desc_addr = (const uint8_t*)&STRING_IMANUFACTURER; else return false; } else @@ -393,13 +393,13 @@ bool SendDescriptor() // Send descriptor // EP0 is 64 bytes long // RWAL and FIFOCON don't work on EP0 - u16 n = 0; + uint16_t n = 0; do { if (!WaitForINOrOUT()) return false; Send8(pgm_read_byte(&desc_addr[n++])); - u8 clr = n & 0x3F; + uint8_t clr = n & 0x3F; if (!clr) ClearIN(); // Fifo is full, release this packet } while (n < desc_length); @@ -413,7 +413,7 @@ void USBSetupInterrupt() return; Setup& setup = _setup; // global saves ~30 bytes - Recv((u8*)&setup,8); + Recv((uint8_t*)&setup,8); ClearSetupInt(); if (setup.bmRequestType & DEVICETOHOST) @@ -422,7 +422,7 @@ void USBSetupInterrupt() ClearIN(); bool ok = true; - u8 r = setup.bRequest; + uint8_t r = setup.bRequest; if (SET_ADDRESS == r) { WaitIN(); @@ -458,7 +458,7 @@ void USBSetupInterrupt() void USBGeneralInterrupt() { - u8 udint = UDINT; + uint8_t udint = UDINT; UDINT = 0; // End of Reset @@ -493,7 +493,7 @@ int USBGetChar() // Read a char if (HasData(CDC_RX)) { - u8 c = Recv8(); + uint8_t c = Recv8(); if (!ReadWriteAllowed()) ReleaseRX(); return c; -- cgit v1.2.3-18-g5258 From 0ed2d3c95309ecf5905ec06624fc68955fdbd2d0 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Tue, 10 Jan 2012 16:29:47 -0500 Subject: renamed DiskLoader to Caterina (after Leonardo's mom) --- bootloaders/diskloader/src/USBCore.cpp | 510 --------------------------------- 1 file changed, 510 deletions(-) delete mode 100644 bootloaders/diskloader/src/USBCore.cpp (limited to 'bootloaders/diskloader/src/USBCore.cpp') diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp deleted file mode 100644 index ac5d081..0000000 --- a/bootloaders/diskloader/src/USBCore.cpp +++ /dev/null @@ -1,510 +0,0 @@ - - -/* Copyright (c) 2010, Peter Barrett -** -** Permission to use, copy, modify, and/or distribute this software for -** any purpose with or without fee is hereby granted, provided that the -** above copyright notice and this permission notice appear in all copies. -** -** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL -** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED -** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR -** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES -** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, -** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS -** SOFTWARE. -*/ - -#include "Platform.h" - -#define CDC_TX CDC_ENDPOINT_IN -#define CDC_RX CDC_ENDPOINT_OUT - -#define EP_TYPE_CONTROL 0x00 -#define EP_TYPE_BULK_IN 0x81 -#define EP_TYPE_BULK_OUT 0x80 -#define EP_TYPE_INTERRUPT_IN 0xC1 -#define EP_TYPE_INTERRUPT_OUT 0xC0 -#define EP_TYPE_ISOCHRONOUS_IN 0x41 -#define EP_TYPE_ISOCHRONOUS_OUT 0x40 - -/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */ -#define TX_RX_LED_PULSE_MS 100 -uint8_t TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */ -uint8_t RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */ - -void Reset(); - -//================================================================== -//================================================================== - -typedef struct -{ - uint32_t dwDTERate; - uint8_t bCharFormat; - uint8_t bParityType; - uint8_t bDataBits; - uint8_t lineState; -} LineInfo; - -static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 }; - -//================================================================== -//================================================================== - -// 4 bytes of RAM -volatile uint8_t _usbConfiguration; -volatile uint8_t _ejected; -volatile uint16_t _timeout; - -static inline void WaitIN(void) -{ - while (!(UEINTX & (1<> 8) - -#define RAWHID_USAGE_PAGE 0xFFC0 -#define RAWHID_USAGE 0x0C00 -#define RAWHID_TX_SIZE 64 -#define RAWHID_RX_SIZE 64 - -const uint8_t _rawHID[] = -{ - // RAW HID - 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30 - 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE), - - 0xA1, 0x01, // Collection 0x01 - 0x85, 0x03, // REPORT_ID (3) - 0x75, 0x08, // report size = 8 bits - 0x15, 0x00, // logical minimum = 0 - 0x26, 0xFF, 0x00, // logical maximum = 255 - - 0x95, 64, // report count TX - 0x09, 0x01, // usage - 0x81, 0x02, // Input (array) - - 0x95, 64, // report count RX - 0x09, 0x02, // usage - 0x91, 0x02, // Output (array) - 0xC0 // end collection -}; - -uint8_t _cdcComposite = 0; - -bool SendDescriptor() -{ - Setup& setup = _setup; - uint16_t desc_length = 0; - const uint8_t* desc_addr = 0; - - uint8_t t = setup.wValueH; - if (0x22 == t) - { -#ifdef HID_ENABLED - desc_addr = _rawHID; - desc_length = sizeof(desc_length); -#endif - } else if (USB_DEVICE_DESCRIPTOR_TYPE == t) - { - desc_addr = (const uint8_t*)&USB_DeviceDescriptor; - } - else if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t) - { - desc_addr = (const uint8_t*)&USB_ConfigDescriptor; - desc_length = sizeof(USB_ConfigDescriptor); - } - else if (USB_STRING_DESCRIPTOR_TYPE == t) - { - if (setup.wValueL == 0) - desc_addr = (const uint8_t*)&STRING_LANGUAGE; - else if (setup.wValueL == IPRODUCT) - desc_addr = (const uint8_t*)&STRING_IPRODUCT; - else if (setup.wValueL == ISERIAL) - desc_addr = (const uint8_t*)&STRING_SERIAL; - else if (setup.wValueL == IMANUFACTURER) - desc_addr = (const uint8_t*)&STRING_IMANUFACTURER; - else - return false; - } else - return false; - - if (desc_length == 0) - desc_length = pgm_read_byte(desc_addr); - if (setup.wLength < desc_length) - desc_length = setup.wLength; - - // Send descriptor - // EP0 is 64 bytes long - // RWAL and FIFOCON don't work on EP0 - uint16_t n = 0; - do - { - if (!WaitForINOrOUT()) - return false; - Send8(pgm_read_byte(&desc_addr[n++])); - uint8_t clr = n & 0x3F; - if (!clr) - ClearIN(); // Fifo is full, release this packet - } while (n < desc_length); - return true; -} - -void USBSetupInterrupt() -{ - SetEP(0); - if (!ReceivedSetupInt()) - return; - - Setup& setup = _setup; // global saves ~30 bytes - Recv((uint8_t*)&setup,8); - ClearSetupInt(); - - if (setup.bmRequestType & DEVICETOHOST) - WaitIN(); - else - ClearIN(); - - bool ok = true; - uint8_t r = setup.bRequest; - if (SET_ADDRESS == r) - { - WaitIN(); - UDADDR = setup.wValueL | (1<