From deb59fd6054ce105487bd107dec014fab116cc3a Mon Sep 17 00:00:00 2001 From: NicoHood Date: Fri, 7 Aug 2015 19:36:28 +0200 Subject: [PATCH 1/6] Made Magic Key Settings more flexible --- cores/arduino/CDC.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp index d694a2d..63983d9 100644 --- a/cores/arduino/CDC.cpp +++ b/cores/arduino/CDC.cpp @@ -92,11 +92,24 @@ bool CDC_Setup(USBSetup& setup) // with a relatively long period so it can finish housekeeping tasks // like servicing endpoints before the sketch ends +#ifndef MAGIC_KEY +#define MAGIC_KEY 0x7777 +#endif +#ifndef MAGIC_KEY_POS +#define MAGIC_KEY_POS 0x0800 +#endif + // We check DTR state to determine if host port is open (bit 0 of lineState). if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0) { - *(uint16_t *)(RAMEND-1) = *(uint16_t *)0x0800; - *(uint16_t *)0x0800 = 0x7777; +#if MAGIC_KEY_POS != (RAMEND-1) + *(uint16_t *)(RAMEND-1) = *(uint16_t *)MAGIC_KEY_POS; + *(uint16_t *)MAGIC_KEY_POS = MAGIC_KEY; +#else + // for future boards save the key in the inproblematic RAMEND + // which is reserved for the main() return value (which will never return) + *(uint16_t *)MAGIC_KEY_POS = MAGIC_KEY; +#endif wdt_enable(WDTO_120MS); } else @@ -108,7 +121,11 @@ bool CDC_Setup(USBSetup& setup) wdt_disable(); wdt_reset(); - *(uint16_t *)0x0800 = *(uint16_t *)(RAMEND-1); +#if MAGIC_KEY_POS != (RAMEND-1) + *(uint16_t *)MAGIC_KEY_POS = *(uint16_t *)(RAMEND-1); +#else + *(uint16_t *)MAGIC_KEY_POS = 0x0000; +#endif } } return true; From 77cc20f11904cb46b509f69ad9e856b5ad9eb253 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Fri, 7 Aug 2015 19:39:06 +0200 Subject: [PATCH 2/6] Added missing static inline to USB Recv function --- cores/arduino/USBCore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp index 8237ccb..05e2f24 100644 --- a/cores/arduino/USBCore.cpp +++ b/cores/arduino/USBCore.cpp @@ -110,7 +110,7 @@ static inline void ClearOUT(void) UEINTX = ~(1< Date: Fri, 7 Aug 2015 19:42:04 +0200 Subject: [PATCH 3/6] Fixed HID Reports >255 bytes --- libraries/HID/HID.cpp | 4 ++-- libraries/HID/HID.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/HID/HID.cpp b/libraries/HID/HID.cpp index 0d2133e..04b5731 100644 --- a/libraries/HID/HID.cpp +++ b/libraries/HID/HID.cpp @@ -43,7 +43,7 @@ static u8 HID_INTERFACE; HIDDescriptor _hidInterface; static HIDDescriptorListNode* rootNode = NULL; -static uint8_t sizeof_hidReportDescriptor = 0; +static uint16_t sizeof_hidReportDescriptor = 0; static uint8_t modules_count = 0; //================================================================================ //================================================================================ @@ -91,7 +91,7 @@ void HID_::AppendDescriptor(HIDDescriptorListNode *node) current->next = node; } modules_count++; - sizeof_hidReportDescriptor += node->cb->length; + sizeof_hidReportDescriptor += (uint16_t)node->cb->length; } void HID_::SendReport(u8 id, const void* data, int len) diff --git a/libraries/HID/HID.h b/libraries/HID/HID.h index 89832a9..b9f29b4 100644 --- a/libraries/HID/HID.h +++ b/libraries/HID/HID.h @@ -45,7 +45,7 @@ #define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23 typedef struct __attribute__((packed)) { - u8 length; + uint16_t length; const void* descriptor; } HID_Descriptor; @@ -88,10 +88,10 @@ typedef struct #define HID_TX HID_ENDPOINT_INT #define D_HIDREPORT(_descriptorLength) \ - { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 } + { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength & 0xFF, _descriptorLength >> 8 } #define WEAK __attribute__ ((weak)) #endif -#endif \ No newline at end of file +#endif From d1fe40060e3c9b1f060e86221aeabf7f70a66316 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Fri, 7 Aug 2015 19:45:18 +0200 Subject: [PATCH 4/6] Added 16 byte endpoint support --- cores/arduino/CDC.cpp | 4 ++-- cores/arduino/USBAPI.h | 6 ++++++ cores/arduino/USBCore.cpp | 9 ++++++++- libraries/HID/HID.cpp | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp index 63983d9..7c7eacf 100644 --- a/cores/arduino/CDC.cpp +++ b/cores/arduino/CDC.cpp @@ -49,8 +49,8 @@ const CDCDescriptor _cdcInterface = // 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) + D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0), + D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0) }; int CDC_GetInterface(u8* interfaceNum) diff --git a/cores/arduino/USBAPI.h b/cores/arduino/USBAPI.h index 4abd961..5caacc5 100644 --- a/cores/arduino/USBAPI.h +++ b/cores/arduino/USBAPI.h @@ -32,6 +32,12 @@ typedef unsigned long u32; #include "Arduino.h" +// This definitions is usefull if you want to reduce the EP_SIZE to 16 +// at the moment only 64 and 16 as EP_SIZE for all EPs are supported except the control endpoint +#ifndef USB_EP_SIZE +#define USB_EP_SIZE 64 +#endif + #if defined(USBCON) #include "USBDesc.h" diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp index 05e2f24..fd25793 100644 --- a/cores/arduino/USBCore.cpp +++ b/cores/arduino/USBCore.cpp @@ -253,7 +253,7 @@ u8 USB_SendSpace(u8 ep) LockEP lock(ep); if (!ReadWriteAllowed()) return 0; - return 64 - FifoByteCount(); + return USB_EP_SIZE - FifoByteCount(); } // Blocking Send of data to an endpoint @@ -326,6 +326,7 @@ u8 _initEndpoints[] = #define EP_SINGLE_64 0x32 // EP0 #define EP_DOUBLE_64 0x36 // Other endpoints +#define EP_SINGLE_16 0x12 static void InitEP(u8 index, u8 type, u8 size) @@ -344,7 +345,13 @@ void InitEndpoints() UENUM = i; UECONX = (1< Date: Fri, 7 Aug 2015 19:45:51 +0200 Subject: [PATCH 5/6] Precised USB Endpoint definitions --- cores/arduino/USBAPI.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cores/arduino/USBAPI.h b/cores/arduino/USBAPI.h index 5caacc5..7f468a6 100644 --- a/cores/arduino/USBAPI.h +++ b/cores/arduino/USBAPI.h @@ -47,13 +47,13 @@ typedef unsigned long u32; //================================================================================ // USB -#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 +#define EP_TYPE_CONTROL (0x00) +#define EP_TYPE_BULK_IN ((1< Date: Fri, 7 Aug 2015 19:47:37 +0200 Subject: [PATCH 6/6] Added u2 Series support --- cores/arduino/USBCore.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp index fd25793..f12d326 100644 --- a/cores/arduino/USBCore.cpp +++ b/cores/arduino/USBCore.cpp @@ -627,13 +627,19 @@ void USB_Flush(u8 ep) static inline void USB_ClockDisable() { +#if defined(OTGPADE) USBCON = (USBCON & ~(1<