From ed03da415cdb5415a1bca2c3c11789602505a3c2 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sat, 26 Sep 2015 18:36:03 +0200 Subject: [PATCH 1/3] Added new Pluggable USB structure This commit was required to test an IDE bug --- .../avr/cores/arduino/PluggableUSB.cpp | 12 ++++++++- .../arduino/avr/cores/arduino/PluggableUSB.h | 15 +++++++++++ .../arduino/avr/cores/arduino/USBDevice.cpp | 16 +++++++++++ .../arduino/avr/cores/arduino/USBDevice.h | 27 +++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 hardware/arduino/avr/cores/arduino/USBDevice.cpp create mode 100644 hardware/arduino/avr/cores/arduino/USBDevice.h diff --git a/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp b/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp index 857a27fe9d7..5f8b59163c7 100644 --- a/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp +++ b/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp @@ -25,6 +25,16 @@ #define MAX_MODULES 6 +#include "PluggableUSB.h" +#include "USBDevice.h" + +PUSB_ PUSB; + +PUSB_::PUSB_(void) +{ + +} + static u8 lastIf = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT; static u8 lastEp = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT; @@ -97,4 +107,4 @@ int8_t PUSB_AddFunction(PUSBListNode *node, u8* interface) #endif -#endif /* if defined(USBCON) */ \ No newline at end of file +#endif /* if defined(USBCON) */ diff --git a/hardware/arduino/avr/cores/arduino/PluggableUSB.h b/hardware/arduino/avr/cores/arduino/PluggableUSB.h index d89040eb41d..3cb7aae07e9 100644 --- a/hardware/arduino/avr/cores/arduino/PluggableUSB.h +++ b/hardware/arduino/avr/cores/arduino/PluggableUSB.h @@ -48,6 +48,21 @@ class PUSBListNode { PUSBListNode(PUSBCallbacks *ncb) {cb = ncb;} }; +class USBDevice; + +class PUSB_ +{ +public: + PUSB_(void); + + // Only access this class via the USBDevice +private: + friend USBDevice; + void AppendDescriptor(USBDevice* device); + + // TODO add root device, search functions etc +}; + int8_t PUSB_AddFunction(PUSBListNode *node, u8 *interface); int PUSB_GetInterface(u8* interfaceNum); diff --git a/hardware/arduino/avr/cores/arduino/USBDevice.cpp b/hardware/arduino/avr/cores/arduino/USBDevice.cpp new file mode 100644 index 00000000000..ad87b65d200 --- /dev/null +++ b/hardware/arduino/avr/cores/arduino/USBDevice.cpp @@ -0,0 +1,16 @@ + +#include "USBDevice.h" +#include "PluggableUSB.h" + +#if defined(USBCON) +#ifdef PLUGGABLE_USB_ENABLED + +USBDevice::USBDevice(void) +{ + //PUSB.AppendDescriptor(this); +} + + +#endif + +#endif /* if defined(USBCON) */ diff --git a/hardware/arduino/avr/cores/arduino/USBDevice.h b/hardware/arduino/avr/cores/arduino/USBDevice.h new file mode 100644 index 00000000000..5a70dbdf41a --- /dev/null +++ b/hardware/arduino/avr/cores/arduino/USBDevice.h @@ -0,0 +1,27 @@ +// Include guard +#pragma once + +#define HID_h + +#include +#include + +//http://stackoverflow.com/questions/1837165/can-two-classes-see-each-other-using-c + +class PUSB_; +extern PUSB_ PUSB; + +class USBDevice +{ +public: + USBDevice(void); + + // Needs to be public for static PUSB_ function access + // Inherit this device private and everything should be fine +//private: + USBDevice* next = NULL; + + +protected: + +}; From af6d5bcf8a042aaca81afe52b50edc548e47ad80 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 29 Sep 2015 17:41:37 +0200 Subject: [PATCH 2/3] Moved all Pluggable functions into a class Wrappers are still there and needs to be removed. Append wrapper will not work --- .../avr/cores/arduino/PluggableUSB.cpp | 97 +++++++++++++------ .../arduino/avr/cores/arduino/PluggableUSB.h | 38 ++++++-- .../arduino/avr/cores/arduino/USBDevice.cpp | 2 +- .../arduino/avr/cores/arduino/USBDevice.h | 17 +++- 4 files changed, 110 insertions(+), 44 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp b/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp index 5f8b59163c7..32178142a0a 100644 --- a/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp +++ b/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp @@ -17,15 +17,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "USBAPI.h" #include "PluggableUSB.h" #if defined(USBCON) #ifdef PLUGGABLE_USB_ENABLED -#define MAX_MODULES 6 - -#include "PluggableUSB.h" #include "USBDevice.h" PUSB_ PUSB; @@ -35,51 +31,93 @@ PUSB_::PUSB_(void) } -static u8 lastIf = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT; -static u8 lastEp = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT; - -extern u8 _initEndpoints[]; - -//PUSBCallbacks cbs[MAX_MODULES]; -static u8 modules_count = 0; - -static PUSBListNode* rootNode = NULL; - -int PUSB_GetInterface(u8* interfaceNum) +int PUSB_::PUSB_GetInterface(u8* interfaceNum) { int ret = 0; - PUSBListNode* node = rootNode; - for (u8 i=0; icb->getInterface(interfaceNum); - node = node->next; + CUSBDevice* device = rootDevice; + for (u8 i=0; i < numModules; i++) { + ret = device->getInterface(interfaceNum); + device = device->next; } return ret; } -int PUSB_GetDescriptor(int8_t t) +int PUSB_::PUSB_GetDescriptor(int8_t t) { int ret = 0; - PUSBListNode* node = rootNode; - for (u8 i=0; icb->getDescriptor(t); - node = node->next; + CUSBDevice* device = rootDevice; + for (u8 i=0; i < numModules; i++) { + ret = device->getDescriptor(t); + if(ret) + break; + device = device->next; } return ret; } -bool PUSB_Setup(USBSetup& setup, u8 j) +bool PUSB_::PUSB_Setup(USBSetup& setup, u8 j) { bool ret = false; - PUSBListNode* node = rootNode; - for (u8 i=0; icb->setup(setup, j); - node = node->next; + CUSBDevice* device = rootDevice; + for (u8 i=0; i < numModules; i++) { + ret = device->setup(setup, j); + if(ret) + break; + device = device->next; } return ret; } +int8_t PUSB_::PUSB_AddFunction(CUSBDevice* device) +{ + if (numModules >= MAX_MODULES) { + return 0; + } + + if (numModules == 0) { + rootDevice = device; + } + else { + CUSBDevice *current = rootDevice; + while(current->next != NULL) { + current = current->next; + } + current->next = device; + } + + *device->endpointType = lastInterface; + lastInterface += device->numInterfaces; + for (u8 i = 0; i < device->numEndpoints; i++) { + _initEndpoints[lastEndpoint] = device->endpointType[i]; + lastEndpoint++; + } + numModules++; + return lastEndpoint - device->numEndpoints; + // restart USB layer??? +} + + +// TODO remove wrappers +int PUSB_GetInterface(u8* interfaceNum) +{ + return PUSB.PUSB_GetInterface(interfaceNum); +} + +int PUSB_GetDescriptor(int8_t t) +{ + return PUSB.PUSB_GetDescriptor(t); +} + +bool PUSB_Setup(USBSetup& setup, u8 j) +{ + return PUSB.PUSB_Setup(setup, j); +} + int8_t PUSB_AddFunction(PUSBListNode *node, u8* interface) { + // TODO not implemented, incompatible with "old" API + return 0; + /* if (modules_count >= MAX_MODULES) { return 0; } @@ -103,6 +141,7 @@ int8_t PUSB_AddFunction(PUSBListNode *node, u8* interface) modules_count++; return lastEp - node->cb->numEndpoints; // restart USB layer??? + */ } #endif diff --git a/hardware/arduino/avr/cores/arduino/PluggableUSB.h b/hardware/arduino/avr/cores/arduino/PluggableUSB.h index f44065fa099..2dbb946fce2 100644 --- a/hardware/arduino/avr/cores/arduino/PluggableUSB.h +++ b/hardware/arduino/avr/cores/arduino/PluggableUSB.h @@ -17,14 +17,21 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef PUSB_h -#define PUSB_h +// Include Guard +#pragma once #include "USBAPI.h" -#include #if defined(USBCON) +// TODO different for u2 series +#define MAX_MODULES 6 + +// TODO where defined?? +extern u8 _initEndpoints[]; + + +// TODO remove old node style typedef struct __attribute__((packed)) { bool (*setup)(USBSetup& setup, u8 i); @@ -42,21 +49,36 @@ class PUSBListNode { PUSBListNode(PUSBCallbacks *ncb) {cb = ncb;} }; -class USBDevice; +class CUSBDevice; class PUSB_ { public: PUSB_(void); + // Called from USB-Core.cpp + // Needs to be public + int PUSB_GetInterface(u8* interfaceNum); + int PUSB_GetDescriptor(int8_t t); + bool PUSB_Setup(USBSetup& setup, u8 i); + // Only access this class via the USBDevice private: - friend USBDevice; - void AppendDescriptor(USBDevice* device); + friend CUSBDevice; + int8_t PUSB_AddFunction(CUSBDevice* device); + + // Variables used to calculate endpoints etc + uint8_t numModules = 0; + u8 lastInterface = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT; + u8 lastEndpoint = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT; + + CUSBDevice* rootDevice = NULL; // TODO add root device, search functions etc }; +// TODO remove old wrappers + int8_t PUSB_AddFunction(PUSBListNode *node, u8 *interface); int PUSB_GetInterface(u8* interfaceNum); @@ -65,8 +87,4 @@ int PUSB_GetDescriptor(int8_t t); bool PUSB_Setup(USBSetup& setup, u8 i); -void PUSB_Begin(); - -#endif - #endif diff --git a/hardware/arduino/avr/cores/arduino/USBDevice.cpp b/hardware/arduino/avr/cores/arduino/USBDevice.cpp index ad87b65d200..fb2300a6339 100644 --- a/hardware/arduino/avr/cores/arduino/USBDevice.cpp +++ b/hardware/arduino/avr/cores/arduino/USBDevice.cpp @@ -5,7 +5,7 @@ #if defined(USBCON) #ifdef PLUGGABLE_USB_ENABLED -USBDevice::USBDevice(void) +CUSBDevice::CUSBDevice(void) { //PUSB.AppendDescriptor(this); } diff --git a/hardware/arduino/avr/cores/arduino/USBDevice.h b/hardware/arduino/avr/cores/arduino/USBDevice.h index 5a70dbdf41a..9e5fb7631e8 100644 --- a/hardware/arduino/avr/cores/arduino/USBDevice.h +++ b/hardware/arduino/avr/cores/arduino/USBDevice.h @@ -1,7 +1,7 @@ // Include guard #pragma once -#define HID_h +#define USBDEVICE_INCLUDED #include #include @@ -11,15 +11,24 @@ class PUSB_; extern PUSB_ PUSB; -class USBDevice +// Important note: USBDevice is somehow reserved, do not use the name. +class CUSBDevice { public: - USBDevice(void); + CUSBDevice(void); // Needs to be public for static PUSB_ function access // Inherit this device private and everything should be fine //private: - USBDevice* next = NULL; + CUSBDevice* next = NULL; + + virtual bool setup(USBSetup& setup, u8 i) = 0; + virtual int getInterface(u8* interfaceNum) = 0; + virtual int getDescriptor(int8_t t) = 0; + void test(){}; + int8_t numEndpoints; + int8_t numInterfaces; + uint8_t *endpointType; protected: From a1d4fe2cbadfae09da0ee2503300f511c9b6ad52 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 29 Sep 2015 19:08:47 +0200 Subject: [PATCH 3/3] dev state 1 --- .../avr/cores/arduino/PluggableUSB.cpp | 34 +++++++++++------- .../arduino/avr/cores/arduino/PluggableUSB.h | 35 +++---------------- .../arduino/avr/cores/arduino/USBCore.cpp | 2 +- .../arduino/avr/cores/arduino/USBDevice.cpp | 5 +-- .../arduino/avr/cores/arduino/USBDevice.h | 12 ++++--- 5 files changed, 38 insertions(+), 50 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp b/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp index 32178142a0a..544178661dc 100644 --- a/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp +++ b/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp @@ -26,7 +26,9 @@ PUSB_ PUSB; -PUSB_::PUSB_(void) +PUSB_::PUSB_(void) : +numModules(0), lastInterface(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT), +lastEndpoint(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT), rootDevice(NULL) { } @@ -68,12 +70,15 @@ bool PUSB_::PUSB_Setup(USBSetup& setup, u8 j) return ret; } -int8_t PUSB_::PUSB_AddFunction(CUSBDevice* device) +void PUSB_::PUSB_AddFunction(CUSBDevice* device) { + // All modules already used + // TODO check endpoint count rather than moduls? if (numModules >= MAX_MODULES) { - return 0; + return; } - + + // Add a new module if (numModules == 0) { rootDevice = device; } @@ -84,16 +89,21 @@ int8_t PUSB_::PUSB_AddFunction(CUSBDevice* device) } current->next = device; } + numModules++; - *device->endpointType = lastInterface; + // Save new interface count position + device->firstInterface = lastInterface; lastInterface += device->numInterfaces; + + // Configure endpoints and count position + device->firstEndpoint = lastEndpoint; + // TODO check max enpoints for (u8 i = 0; i < device->numEndpoints; i++) { _initEndpoints[lastEndpoint] = device->endpointType[i]; lastEndpoint++; } - numModules++; - return lastEndpoint - device->numEndpoints; - // restart USB layer??? + + // TODO restart USB layer??? } @@ -112,12 +122,12 @@ bool PUSB_Setup(USBSetup& setup, u8 j) { return PUSB.PUSB_Setup(setup, j); } - +/* int8_t PUSB_AddFunction(PUSBListNode *node, u8* interface) { // TODO not implemented, incompatible with "old" API return 0; - /* + if (modules_count >= MAX_MODULES) { return 0; } @@ -141,8 +151,8 @@ int8_t PUSB_AddFunction(PUSBListNode *node, u8* interface) modules_count++; return lastEp - node->cb->numEndpoints; // restart USB layer??? - */ -} + +}*/ #endif diff --git a/hardware/arduino/avr/cores/arduino/PluggableUSB.h b/hardware/arduino/avr/cores/arduino/PluggableUSB.h index 2dbb946fce2..380b6bbe8ba 100644 --- a/hardware/arduino/avr/cores/arduino/PluggableUSB.h +++ b/hardware/arduino/avr/cores/arduino/PluggableUSB.h @@ -30,25 +30,6 @@ // TODO where defined?? extern u8 _initEndpoints[]; - -// TODO remove old node style -typedef struct __attribute__((packed)) -{ - bool (*setup)(USBSetup& setup, u8 i); - int (*getInterface)(u8* interfaceNum); - int (*getDescriptor)(int8_t t); - int8_t numEndpoints; - int8_t numInterfaces; - uint8_t *endpointType; -} PUSBCallbacks; - -class PUSBListNode { -public: - PUSBListNode *next = NULL; - PUSBCallbacks *cb; - PUSBListNode(PUSBCallbacks *ncb) {cb = ncb;} -}; - class CUSBDevice; class PUSB_ @@ -65,22 +46,16 @@ class PUSB_ // Only access this class via the USBDevice private: friend CUSBDevice; - int8_t PUSB_AddFunction(CUSBDevice* device); + void PUSB_AddFunction(CUSBDevice* device); // Variables used to calculate endpoints etc - uint8_t numModules = 0; - u8 lastInterface = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT; - u8 lastEndpoint = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT; - - CUSBDevice* rootDevice = NULL; - - // TODO add root device, search functions etc + uint8_t numModules; + u8 lastInterface; + u8 lastEndpoint; + CUSBDevice* rootDevice; }; // TODO remove old wrappers - -int8_t PUSB_AddFunction(PUSBListNode *node, u8 *interface); - int PUSB_GetInterface(u8* interfaceNum); int PUSB_GetDescriptor(int8_t t); diff --git a/hardware/arduino/avr/cores/arduino/USBCore.cpp b/hardware/arduino/avr/cores/arduino/USBCore.cpp index 733a178c909..7d02c83fd10 100644 --- a/hardware/arduino/avr/cores/arduino/USBCore.cpp +++ b/hardware/arduino/avr/cores/arduino/USBCore.cpp @@ -310,7 +310,7 @@ int USB_Send(u8 ep, const void* d, int len) u8 _initEndpoints[] = { - 0, + 0, // Control EP EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT diff --git a/hardware/arduino/avr/cores/arduino/USBDevice.cpp b/hardware/arduino/avr/cores/arduino/USBDevice.cpp index fb2300a6339..174e13b694f 100644 --- a/hardware/arduino/avr/cores/arduino/USBDevice.cpp +++ b/hardware/arduino/avr/cores/arduino/USBDevice.cpp @@ -5,9 +5,10 @@ #if defined(USBCON) #ifdef PLUGGABLE_USB_ENABLED -CUSBDevice::CUSBDevice(void) +CUSBDevice::CUSBDevice(const int8_t numEP, const int8_t numIF, const uint8_t* epType) : +numEndpoints(numEP), numInterfaces(numIF), firstEndpoint(-1), firstInterface(-1), endpointType(epType) { - //PUSB.AppendDescriptor(this); + PUSB.PUSB_AddFunction(this); } diff --git a/hardware/arduino/avr/cores/arduino/USBDevice.h b/hardware/arduino/avr/cores/arduino/USBDevice.h index 9e5fb7631e8..8df4ec888fb 100644 --- a/hardware/arduino/avr/cores/arduino/USBDevice.h +++ b/hardware/arduino/avr/cores/arduino/USBDevice.h @@ -15,7 +15,7 @@ extern PUSB_ PUSB; class CUSBDevice { public: - CUSBDevice(void); + CUSBDevice(const int8_t numEP, const int8_t numIF, const uint8_t* epType); // Needs to be public for static PUSB_ function access // Inherit this device private and everything should be fine @@ -25,10 +25,12 @@ class CUSBDevice virtual bool setup(USBSetup& setup, u8 i) = 0; virtual int getInterface(u8* interfaceNum) = 0; virtual int getDescriptor(int8_t t) = 0; - void test(){}; - int8_t numEndpoints; - int8_t numInterfaces; - uint8_t *endpointType; + + const int8_t numEndpoints; + const int8_t numInterfaces; + int8_t firstEndpoint; + int8_t firstInterface; + const uint8_t *endpointType; protected: