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 +++++++++++++++--- bootloaders/diskloader/src/USBCore.cpp | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'bootloaders/diskloader/src') 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 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') 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/Platform.h | 5 +++- bootloaders/diskloader/src/USBCore.cpp | 10 ++++---- bootloaders/diskloader/src/USBDesc.cpp | 12 ++++------ bootloaders/diskloader/src/USBDesc.h | 43 +++++++++++++++------------------- 4 files changed, 32 insertions(+), 38 deletions(-) (limited to 'bootloaders/diskloader/src') diff --git a/bootloaders/diskloader/src/Platform.h b/bootloaders/diskloader/src/Platform.h index 04c5b79..fcce0a1 100644 --- a/bootloaders/diskloader/src/Platform.h +++ b/bootloaders/diskloader/src/Platform.h @@ -43,7 +43,10 @@ void Transfer(u8 ep, const u8* data, int len); void Recv(u8 ep, u8* dst, u8 len); void Program(u8 ep, u16 page, u8 count); -#define CDC_ENABLED +/* HID is not fully-supported in the bootloader - can be enabled + for testing, but note the descriptor report and other parts are + not complete */ +//#define HID_ENABLED #include "USBCore.h" #include "USBDesc.h" 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) { diff --git a/bootloaders/diskloader/src/USBDesc.cpp b/bootloaders/diskloader/src/USBDesc.cpp index ade072a..b143687 100644 --- a/bootloaders/diskloader/src/USBDesc.cpp +++ b/bootloaders/diskloader/src/USBDesc.cpp @@ -47,18 +47,12 @@ const u16 STRING_IMANUFACTURER[12] = { }; -//#ifdef CDC_ENABLED -DeviceDescriptor USB_DeviceDescriptorA = D_DEVICE(0X02,0X00,0X00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1); -//#else -DeviceDescriptor USB_DeviceDescriptor = D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1); -//#endif - +DeviceDescriptor USB_DeviceDescriptor = D_DEVICE(0X02,0X00,0X00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1); Config USB_ConfigDescriptor = { D_CONFIG(sizeof(Config),INTERFACE_COUNT), -#ifdef CDC_ENABLED // CDC { D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1), @@ -76,12 +70,14 @@ Config USB_ConfigDescriptor = D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0), D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0) }, -#endif + +#ifdef HID_ENABLED // HID { D_INTERFACE(HID_INTERFACE,1,3,0,0), D_HIDREPORT(30), D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x40) } +#endif }; diff --git a/bootloaders/diskloader/src/USBDesc.h b/bootloaders/diskloader/src/USBDesc.h index a970fa4..3a7fc55 100644 --- a/bootloaders/diskloader/src/USBDesc.h +++ b/bootloaders/diskloader/src/USBDesc.h @@ -16,36 +16,31 @@ ** SOFTWARE. */ - -#ifdef CDC_ENABLED - -#define CDC_ACM_INTERFACE 0 // CDC ACM -#define CDC_DATA_INTERFACE 1 // CDC Data -#define CDC_ENDPOINT_ACM 1 -#define CDC_ENDPOINT_OUT 2 -#define CDC_ENDPOINT_IN 3 - -#define HID_INTERFACE 2 // HID Interface -#define HID_ENDPOINT_INT 4 - -#define INTERFACE_COUNT 3 // 2 for cdc + 1 for hid - -#else - -#define HID_INTERFACE 2 // HID Interface -#define HID_ENDPOINT_INT 4 - -#define INTERFACE_COUNT 1 // 1 for hid - -#endif +#ifndef HID_ENABLED // HID is NOT enabled + #define CDC_ACM_INTERFACE 0 // CDC ACM + #define CDC_DATA_INTERFACE 1 // CDC Data + #define CDC_ENDPOINT_ACM 1 + #define CDC_ENDPOINT_OUT 2 + #define CDC_ENDPOINT_IN 3 + #define INTERFACE_COUNT 2 // 2 for CDC +#else // HID is enabled + #define CDC_ACM_INTERFACE 0 // CDC ACM + #define CDC_DATA_INTERFACE 1 // CDC Data + #define CDC_ENDPOINT_ACM 1 + #define CDC_ENDPOINT_OUT 2 + #define CDC_ENDPOINT_IN 3 + #define HID_INTERFACE 2 // HID Interface + #define HID_ENDPOINT_INT 4 + #define INTERFACE_COUNT 3 // 2 for CDC + 1 for hid +#endif typedef struct { ConfigDescriptor config; -#ifdef CDC_ENABLED CDCDescriptor cdc; -#endif +#ifdef HID_ENABLED HIDDescriptor hid; +#endif } Config; extern Config USB_ConfigDescriptor PROGMEM; -- 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/DiskLoader.cpp | 14 +++++++------- bootloaders/diskloader/src/Platform.h | 12 ++++++------ bootloaders/diskloader/src/USBCore.cpp | 12 ++++++------ 3 files changed, 19 insertions(+), 19 deletions(-) (limited to 'bootloaders/diskloader/src') 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 diff --git a/bootloaders/diskloader/src/Platform.h b/bootloaders/diskloader/src/Platform.h index fcce0a1..2e90334 100644 --- a/bootloaders/diskloader/src/Platform.h +++ b/bootloaders/diskloader/src/Platform.h @@ -28,12 +28,12 @@ typedef unsigned long u32; #define OEM_NAME 'l','e','o','n','a','r','d','o' // 8 chars #define BOARD_INIT() DDRC |= (1<<7); DDRB |= (1<<0); DDRD |= (1<<5); CPU_PRESCALE(0); DISABLE_JTAG(); -#define LED0 PORTC &= ~(1<<7) -#define LED1 PORTC |= (1<<7) -#define TXLED0 PORTD |= (1<<5) -#define TXLED1 PORTD &= ~(1<<5) -#define RXLED0 PORTB |= (1<<0) -#define RXLED1 PORTB &= ~(1<<0) +#define L_LED_OFF() PORTC &= ~(1<<7) +#define L_LED_ON() PORTC |= (1<<7) +#define TX_LED_OFF() PORTD |= (1<<5) +#define TX_LED_ON() PORTD &= ~(1<<5) +#define RX_LED_OFF() PORTB |= (1<<0) +#define RX_LED_ON() PORTB &= ~(1<<0) #define TRANSFER_PGM 0x80 #define TRANSFER_RELEASE 0x40 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/DiskLoader.cpp | 60 +++++++------- bootloaders/diskloader/src/Platform.h | 11 +-- bootloaders/diskloader/src/USBCore.cpp | 118 ++++++++++++++-------------- bootloaders/diskloader/src/USBCore.h | 126 +++++++++++++++--------------- bootloaders/diskloader/src/USBDesc.cpp | 8 +- bootloaders/diskloader/src/USBDesc.h | 8 +- 6 files changed, 163 insertions(+), 168 deletions(-) (limited to 'bootloaders/diskloader/src') 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 (;;) ; } diff --git a/bootloaders/diskloader/src/Platform.h b/bootloaders/diskloader/src/Platform.h index 2e90334..57dc7d5 100644 --- a/bootloaders/diskloader/src/Platform.h +++ b/bootloaders/diskloader/src/Platform.h @@ -11,11 +11,6 @@ #include #include -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned long u32; - - #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) #define DISABLE_JTAG() MCUCR = (1 << JTD) | (1 << IVCE) | (0 << PUD); MCUCR = (1 << JTD) | (0 << IVSEL) | (0 << IVCE) | (0 << PUD); @@ -39,9 +34,9 @@ typedef unsigned long u32; #define TRANSFER_RELEASE 0x40 #define TRANSFER_ZERO 0x20 -void Transfer(u8 ep, const u8* data, int len); -void Recv(u8 ep, u8* dst, u8 len); -void Program(u8 ep, u16 page, u8 count); +void Transfer(uint8_t ep, const uint8_t* data, int len); +void Recv(uint8_t ep, uint8_t* dst, uint8_t len); +void Program(uint8_t ep, uint16_t page, uint8_t count); /* HID is not fully-supported in the bootloader - can be enabled for testing, but note the descriptor report and other parts are 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; diff --git a/bootloaders/diskloader/src/USBCore.h b/bootloaders/diskloader/src/USBCore.h index 537c0ec..313ae3b 100644 --- a/bootloaders/diskloader/src/USBCore.h +++ b/bootloaders/diskloader/src/USBCore.h @@ -97,32 +97,32 @@ // Device typedef struct { - u8 len; // 18 - u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE - u16 usbVersion; // 0x200 - u8 deviceClass; - u8 deviceSubClass; - u8 deviceProtocol; - u8 packetSize0; // Packet 0 - u16 idVendor; - u16 idProduct; - u16 deviceVersion; // 0x100 - u8 iManufacturer; - u8 iProduct; - u8 iSerialNumber; - u8 bNumConfigurations; + uint8_t len; // 18 + uint8_t dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE + uint16_t usbVersion; // 0x200 + uint8_t deviceClass; + uint8_t deviceSubClass; + uint8_t deviceProtocol; + uint8_t packetSize0; // Packet 0 + uint16_t idVendor; + uint16_t idProduct; + uint16_t deviceVersion; // 0x100 + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t bNumConfigurations; } DeviceDescriptor; // Config typedef struct { - u8 len; // 9 - u8 dtype; // 2 - u16 clen; // total length - u8 numInterfaces; - u8 config; - u8 iconfig; - u8 attributes; - u8 maxPower; + uint8_t len; // 9 + uint8_t dtype; // 2 + uint16_t clen; // total length + uint8_t numInterfaces; + uint8_t config; + uint8_t iconfig; + uint8_t attributes; + uint8_t maxPower; } ConfigDescriptor; // String @@ -130,58 +130,58 @@ typedef struct { // Interface typedef struct { - u8 len; // 9 - u8 dtype; // 4 - u8 number; - u8 alternate; - u8 numEndpoints; - u8 interfaceClass; - u8 interfaceSubClass; - u8 protocol; - u8 iInterface; + uint8_t len; // 9 + uint8_t dtype; // 4 + uint8_t number; + uint8_t alternate; + uint8_t numEndpoints; + uint8_t interfaceClass; + uint8_t interfaceSubClass; + uint8_t protocol; + uint8_t iInterface; } InterfaceDescriptor; // Endpoint typedef struct { - u8 len; // 7 - u8 dtype; // 5 - u8 addr; - u8 attr; - u16 packetSize; - u8 interval; + uint8_t len; // 7 + uint8_t dtype; // 5 + uint8_t addr; + uint8_t attr; + uint16_t packetSize; + uint8_t interval; } EndpointDescriptor; // Interface Association Descriptor // Used to bind 2 interfaces together in CDC compostite device typedef struct { - u8 len; // 8 - u8 dtype; // 11 - u8 firstInterface; - u8 interfaceCount; - u8 functionClass; - u8 funtionSubClass; - u8 functionProtocol; - u8 iInterface; + uint8_t len; // 8 + uint8_t dtype; // 11 + uint8_t firstInterface; + uint8_t interfaceCount; + uint8_t functionClass; + uint8_t funtionSubClass; + uint8_t functionProtocol; + uint8_t iInterface; } IADDescriptor; // CDC CS interface descriptor typedef struct { - u8 len; // 5 - u8 dtype; // 0x24 - u8 subtype; - u8 d0; - u8 d1; + uint8_t len; // 5 + uint8_t dtype; // 0x24 + uint8_t subtype; + uint8_t d0; + uint8_t d1; } CDCCSInterfaceDescriptor; typedef struct { - u8 len; // 4 - u8 dtype; // 0x24 - u8 subtype; - u8 d0; + uint8_t len; // 4 + uint8_t dtype; // 0x24 + uint8_t subtype; + uint8_t d0; } CDCCSInterfaceDescriptor4; typedef struct @@ -204,15 +204,15 @@ typedef struct typedef struct { - u8 len; // 9 - u8 dtype; // 0x21 - u8 addr; - u8 versionL; // 0x101 - u8 versionH; // 0x101 - u8 country; - u8 desctype; // 0x22 report - u8 descLenL; - u8 descLenH; + uint8_t len; // 9 + uint8_t dtype; // 0x21 + uint8_t addr; + uint8_t versionL; // 0x101 + uint8_t versionH; // 0x101 + uint8_t country; + uint8_t desctype; // 0x22 report + uint8_t descLenL; + uint8_t descLenH; } HIDDescDescriptor; typedef struct diff --git a/bootloaders/diskloader/src/USBDesc.cpp b/bootloaders/diskloader/src/USBDesc.cpp index b143687..7160868 100644 --- a/bootloaders/diskloader/src/USBDesc.cpp +++ b/bootloaders/diskloader/src/USBDesc.cpp @@ -22,17 +22,17 @@ //==================================================================================================== // Actual device descriptors -const u16 STRING_LANGUAGE[2] = { +const uint16_t STRING_LANGUAGE[2] = { (3<<8) | (2+2), 0x0409 // English }; -const u16 STRING_SERIAL[13] = { +const uint16_t STRING_SERIAL[13] = { (3<<8) | (2+2*12), USB_SERIAL_STRING }; -const u16 STRING_IPRODUCT[28] = { +const uint16_t STRING_IPRODUCT[28] = { (3<<8) | (2+2*27), #if USB_PID == USB_PID_LEONARDO 'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o',' ','b','o','o','t','l','o','a','d','e','r' @@ -41,7 +41,7 @@ const u16 STRING_IPRODUCT[28] = { #endif }; -const u16 STRING_IMANUFACTURER[12] = { +const uint16_t STRING_IMANUFACTURER[12] = { (3<<8) | (2+2*11), 'A','r','d','u','i','n','o',' ','L','L','C' }; diff --git a/bootloaders/diskloader/src/USBDesc.h b/bootloaders/diskloader/src/USBDesc.h index 3a7fc55..fa962e9 100644 --- a/bootloaders/diskloader/src/USBDesc.h +++ b/bootloaders/diskloader/src/USBDesc.h @@ -47,10 +47,10 @@ extern Config USB_ConfigDescriptor PROGMEM; extern DeviceDescriptor USB_DeviceDescriptor PROGMEM; extern DeviceDescriptor USB_DeviceDescriptorA PROGMEM; -extern const u16 STRING_LANGUAGE[2] PROGMEM; -extern const u16 STRING_IPRODUCT[28] PROGMEM; -extern const u16 STRING_IMANUFACTURER[12] PROGMEM; -extern const u16 STRING_SERIAL[13] PROGMEM; +extern const uint16_t STRING_LANGUAGE[2] PROGMEM; +extern const uint16_t STRING_IPRODUCT[28] PROGMEM; +extern const uint16_t STRING_IMANUFACTURER[12] PROGMEM; +extern const uint16_t STRING_SERIAL[13] PROGMEM; #define IMANUFACTURER 1 #define IPRODUCT 2 -- 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') 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:09:56 -0500 Subject: changed USB PID for Leonardo and Micro bootloaders sketch and bootloader will use different PIDs to distinguish one from the other. --- bootloaders/diskloader/src/Platform.h | 4 ++-- bootloaders/diskloader/src/USBDesc.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'bootloaders/diskloader/src') diff --git a/bootloaders/diskloader/src/Platform.h b/bootloaders/diskloader/src/Platform.h index 57dc7d5..9d18e80 100644 --- a/bootloaders/diskloader/src/Platform.h +++ b/bootloaders/diskloader/src/Platform.h @@ -14,8 +14,8 @@ #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) #define DISABLE_JTAG() MCUCR = (1 << JTD) | (1 << IVCE) | (0 << PUD); MCUCR = (1 << JTD) | (0 << IVSEL) | (0 << IVCE) | (0 << PUD); -#define USB_PID_LEONARDO 0x0034 -#define USB_PID_MICRO 0x0035 +#define USB_PID_LEONARDO_BOOTLOADER 0x0030 +#define USB_PID_MICRO_BOOTLOADER 0x0031 #define USB_VID 0x2341 // arduino LLC vid #define USB_PID ARDUINO_MODEL_PID // passed in by Makefile - 0x0034 for Leonardo, 0x0035 for MIcro diff --git a/bootloaders/diskloader/src/USBDesc.cpp b/bootloaders/diskloader/src/USBDesc.cpp index 7160868..a634d4d 100644 --- a/bootloaders/diskloader/src/USBDesc.cpp +++ b/bootloaders/diskloader/src/USBDesc.cpp @@ -34,9 +34,9 @@ const uint16_t STRING_SERIAL[13] = { const uint16_t STRING_IPRODUCT[28] = { (3<<8) | (2+2*27), -#if USB_PID == USB_PID_LEONARDO +#if USB_PID == USB_PID_LEONARDO_BOOTLOADER 'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o',' ','b','o','o','t','l','o','a','d','e','r' -#elif USB_PID == USB_PID_MICRO +#elif USB_PID == USB_PID_MICRO_BOOTLOADER 'A','r','d','u','i','n','o',' ','M','i','c','r','o',' ','b','o','o','t','l','o','a','d','e','r',' ',' ',' ' #endif }; -- 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/DiskLoader.cpp | 251 --------------- bootloaders/diskloader/src/Platform.h | 49 --- bootloaders/diskloader/src/USBCore.cpp | 510 ------------------------------ bootloaders/diskloader/src/USBCore.h | 246 -------------- bootloaders/diskloader/src/USBDesc.cpp | 83 ----- bootloaders/diskloader/src/USBDesc.h | 60 ---- 6 files changed, 1199 deletions(-) delete mode 100644 bootloaders/diskloader/src/DiskLoader.cpp delete mode 100644 bootloaders/diskloader/src/Platform.h delete mode 100644 bootloaders/diskloader/src/USBCore.cpp delete mode 100644 bootloaders/diskloader/src/USBCore.h delete mode 100644 bootloaders/diskloader/src/USBDesc.cpp delete mode 100644 bootloaders/diskloader/src/USBDesc.h (limited to 'bootloaders/diskloader/src') 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 (;;) - ; -} diff --git a/bootloaders/diskloader/src/Platform.h b/bootloaders/diskloader/src/Platform.h deleted file mode 100644 index 9d18e80..0000000 --- a/bootloaders/diskloader/src/Platform.h +++ /dev/null @@ -1,49 +0,0 @@ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) -#define DISABLE_JTAG() MCUCR = (1 << JTD) | (1 << IVCE) | (0 << PUD); MCUCR = (1 << JTD) | (0 << IVSEL) | (0 << IVCE) | (0 << PUD); - -#define USB_PID_LEONARDO_BOOTLOADER 0x0030 -#define USB_PID_MICRO_BOOTLOADER 0x0031 -#define USB_VID 0x2341 // arduino LLC vid -#define USB_PID ARDUINO_MODEL_PID // passed in by Makefile - 0x0034 for Leonardo, 0x0035 for MIcro - -#define USB_SERIAL_STRING '0','0','0','0','0','0','0','0','1','7','0','1' - -#define OEM_NAME 'l','e','o','n','a','r','d','o' // 8 chars -#define BOARD_INIT() DDRC |= (1<<7); DDRB |= (1<<0); DDRD |= (1<<5); CPU_PRESCALE(0); DISABLE_JTAG(); -#define L_LED_OFF() PORTC &= ~(1<<7) -#define L_LED_ON() PORTC |= (1<<7) -#define TX_LED_OFF() PORTD |= (1<<5) -#define TX_LED_ON() PORTD &= ~(1<<5) -#define RX_LED_OFF() PORTB |= (1<<0) -#define RX_LED_ON() PORTB &= ~(1<<0) - -#define TRANSFER_PGM 0x80 -#define TRANSFER_RELEASE 0x40 -#define TRANSFER_ZERO 0x20 - -void Transfer(uint8_t ep, const uint8_t* data, int len); -void Recv(uint8_t ep, uint8_t* dst, uint8_t len); -void Program(uint8_t ep, uint16_t page, uint8_t count); - -/* HID is not fully-supported in the bootloader - can be enabled - for testing, but note the descriptor report and other parts are - not complete */ -//#define HID_ENABLED - -#include "USBCore.h" -#include "USBDesc.h" - - 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<> 8) & 0xFF) - -#define CDC_V1_10 0x0110 -#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02 - -#define CDC_CALL_MANAGEMENT 0x01 -#define CDC_ABSTRACT_CONTROL_MODEL 0x02 -#define CDC_HEADER 0x00 -#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02 -#define CDC_UNION 0x06 -#define CDC_CS_INTERFACE 0x24 -#define CDC_CS_ENDPOINT 0x25 -#define CDC_DATA_INTERFACE_CLASS 0x0A - - -// Device -typedef struct { - uint8_t len; // 18 - uint8_t dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE - uint16_t usbVersion; // 0x200 - uint8_t deviceClass; - uint8_t deviceSubClass; - uint8_t deviceProtocol; - uint8_t packetSize0; // Packet 0 - uint16_t idVendor; - uint16_t idProduct; - uint16_t deviceVersion; // 0x100 - uint8_t iManufacturer; - uint8_t iProduct; - uint8_t iSerialNumber; - uint8_t bNumConfigurations; -} DeviceDescriptor; - -// Config -typedef struct { - uint8_t len; // 9 - uint8_t dtype; // 2 - uint16_t clen; // total length - uint8_t numInterfaces; - uint8_t config; - uint8_t iconfig; - uint8_t attributes; - uint8_t maxPower; -} ConfigDescriptor; - -// String - -// Interface -typedef struct -{ - uint8_t len; // 9 - uint8_t dtype; // 4 - uint8_t number; - uint8_t alternate; - uint8_t numEndpoints; - uint8_t interfaceClass; - uint8_t interfaceSubClass; - uint8_t protocol; - uint8_t iInterface; -} InterfaceDescriptor; - -// Endpoint -typedef struct -{ - uint8_t len; // 7 - uint8_t dtype; // 5 - uint8_t addr; - uint8_t attr; - uint16_t packetSize; - uint8_t interval; -} EndpointDescriptor; - -// Interface Association Descriptor -// Used to bind 2 interfaces together in CDC compostite device -typedef struct -{ - uint8_t len; // 8 - uint8_t dtype; // 11 - uint8_t firstInterface; - uint8_t interfaceCount; - uint8_t functionClass; - uint8_t funtionSubClass; - uint8_t functionProtocol; - uint8_t iInterface; -} IADDescriptor; - -// CDC CS interface descriptor -typedef struct -{ - uint8_t len; // 5 - uint8_t dtype; // 0x24 - uint8_t subtype; - uint8_t d0; - uint8_t d1; -} CDCCSInterfaceDescriptor; - -typedef struct -{ - uint8_t len; // 4 - uint8_t dtype; // 0x24 - uint8_t subtype; - uint8_t d0; -} CDCCSInterfaceDescriptor4; - -typedef struct -{ - IADDescriptor iad; // Only needed on compound device - - // Control - InterfaceDescriptor cif; // - CDCCSInterfaceDescriptor header; - CDCCSInterfaceDescriptor callManagement; - CDCCSInterfaceDescriptor4 controlManagement; - CDCCSInterfaceDescriptor functionalDescriptor; - EndpointDescriptor cifin; - - // Data - InterfaceDescriptor dif; - EndpointDescriptor in; - EndpointDescriptor out; -} CDCDescriptor; - -typedef struct -{ - uint8_t len; // 9 - uint8_t dtype; // 0x21 - uint8_t addr; - uint8_t versionL; // 0x101 - uint8_t versionH; // 0x101 - uint8_t country; - uint8_t desctype; // 0x22 report - uint8_t descLenL; - uint8_t descLenH; -} HIDDescDescriptor; - -typedef struct -{ - InterfaceDescriptor hid; - HIDDescDescriptor desc; - EndpointDescriptor in; -} HIDDescriptor; - -#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \ - { 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs } - -#define D_CONFIG(_totalLength,_interfaces) \ - { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(100) } - -#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \ - { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 } - -#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \ - { 7, 5, _addr,_attr,_packetSize, _interval } - -#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \ - { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 } - -#define D_HIDREPORT(_descriptorLength) \ - { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 } - -#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 } -#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 } - -#endif \ No newline at end of file diff --git a/bootloaders/diskloader/src/USBDesc.cpp b/bootloaders/diskloader/src/USBDesc.cpp deleted file mode 100644 index a634d4d..0000000 --- a/bootloaders/diskloader/src/USBDesc.cpp +++ /dev/null @@ -1,83 +0,0 @@ - - -/* Copyright (c) 2011, 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" - -//==================================================================================================== -//==================================================================================================== -// Actual device descriptors - -const uint16_t STRING_LANGUAGE[2] = { - (3<<8) | (2+2), - 0x0409 // English -}; - -const uint16_t STRING_SERIAL[13] = { - (3<<8) | (2+2*12), - USB_SERIAL_STRING -}; - -const uint16_t STRING_IPRODUCT[28] = { - (3<<8) | (2+2*27), -#if USB_PID == USB_PID_LEONARDO_BOOTLOADER - 'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o',' ','b','o','o','t','l','o','a','d','e','r' -#elif USB_PID == USB_PID_MICRO_BOOTLOADER - 'A','r','d','u','i','n','o',' ','M','i','c','r','o',' ','b','o','o','t','l','o','a','d','e','r',' ',' ',' ' -#endif -}; - -const uint16_t STRING_IMANUFACTURER[12] = { - (3<<8) | (2+2*11), - 'A','r','d','u','i','n','o',' ','L','L','C' -}; - - -DeviceDescriptor USB_DeviceDescriptor = D_DEVICE(0X02,0X00,0X00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1); - -Config USB_ConfigDescriptor = -{ - D_CONFIG(sizeof(Config),INTERFACE_COUNT), - - // CDC - { - D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1), - - // CDC communication interface - D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0), - D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd) - D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management - D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,2), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported - D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0 - D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40), - - // CDC data interface - D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0), - D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0), - D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0) - }, - -#ifdef HID_ENABLED - // HID - { - D_INTERFACE(HID_INTERFACE,1,3,0,0), - D_HIDREPORT(30), - D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x40) - } -#endif -}; - diff --git a/bootloaders/diskloader/src/USBDesc.h b/bootloaders/diskloader/src/USBDesc.h deleted file mode 100644 index fa962e9..0000000 --- a/bootloaders/diskloader/src/USBDesc.h +++ /dev/null @@ -1,60 +0,0 @@ - - -/* Copyright (c) 2011, 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. -*/ - -#ifndef HID_ENABLED // HID is NOT enabled - #define CDC_ACM_INTERFACE 0 // CDC ACM - #define CDC_DATA_INTERFACE 1 // CDC Data - #define CDC_ENDPOINT_ACM 1 - #define CDC_ENDPOINT_OUT 2 - #define CDC_ENDPOINT_IN 3 - #define INTERFACE_COUNT 2 // 2 for CDC -#else // HID is enabled - #define CDC_ACM_INTERFACE 0 // CDC ACM - #define CDC_DATA_INTERFACE 1 // CDC Data - #define CDC_ENDPOINT_ACM 1 - #define CDC_ENDPOINT_OUT 2 - #define CDC_ENDPOINT_IN 3 - #define HID_INTERFACE 2 // HID Interface - #define HID_ENDPOINT_INT 4 - #define INTERFACE_COUNT 3 // 2 for CDC + 1 for hid -#endif - -typedef struct -{ - ConfigDescriptor config; - CDCDescriptor cdc; -#ifdef HID_ENABLED - HIDDescriptor hid; -#endif -} Config; - -extern Config USB_ConfigDescriptor PROGMEM; -extern DeviceDescriptor USB_DeviceDescriptor PROGMEM; -extern DeviceDescriptor USB_DeviceDescriptorA PROGMEM; - -extern const uint16_t STRING_LANGUAGE[2] PROGMEM; -extern const uint16_t STRING_IPRODUCT[28] PROGMEM; -extern const uint16_t STRING_IMANUFACTURER[12] PROGMEM; -extern const uint16_t STRING_SERIAL[13] PROGMEM; - -#define IMANUFACTURER 1 -#define IPRODUCT 2 -#define ISERIAL 3 - -#define CDC_TX CDC_ENDPOINT_IN -#define CDC_RX CDC_ENDPOINT_OUT \ No newline at end of file -- cgit v1.2.3-18-g5258