From 6151972b7438a30c015e91541cb8b9e4778b49c8 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Wed, 7 Oct 2015 18:45:10 +0200 Subject: [PATCH 1/4] [PUSB] Changed Interface + Endpoint to unsigned variables The iterations in the for loop also use unsigned and the setup struct etc as well. There was no change in HID required since we just init the inherited variables via constructor and the type is never mentioned. --- hardware/arduino/avr/cores/arduino/PluggableUSB.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/PluggableUSB.h b/hardware/arduino/avr/cores/arduino/PluggableUSB.h index 433e23efd09..b7c52d61062 100644 --- a/hardware/arduino/avr/cores/arduino/PluggableUSB.h +++ b/hardware/arduino/avr/cores/arduino/PluggableUSB.h @@ -27,12 +27,12 @@ class PUSBListNode { public: - PUSBListNode(int8_t numEps, int8_t numIfs, uint8_t *epType) : + PUSBListNode(uint8_t numEps, uint8_t numIfs, uint8_t *epType) : numEndpoints(numEps), numInterfaces(numIfs), endpointType(epType) { } inline uint8_t interface() const { return pluggedInterface; } - inline int8_t endpoint() const { return pluggedEndpoint; } + inline uint8_t endpoint() const { return pluggedEndpoint; } protected: virtual bool setup(USBSetup& setup, uint8_t interfaceNum) = 0; @@ -40,10 +40,10 @@ class PUSBListNode { virtual int getDescriptor(int8_t t) = 0; uint8_t pluggedInterface; - int8_t pluggedEndpoint; + uint8_t pluggedEndpoint; - const int8_t numEndpoints; - const int8_t numInterfaces; + const uint8_t numEndpoints; + const uint8_t numInterfaces; const uint8_t *endpointType; PUSBListNode *next = NULL; From 0f9f63f2a5c25124b4f5bf20762382f4b0482216 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Wed, 7 Oct 2015 19:02:40 +0200 Subject: [PATCH 2/4] [PUSB] Made getDescriptor() and setup() more flexible Alternatively we can only pass the wIndex to getDescriptor but I suggest to just pass the pointer aka reference of the whole setup. In guess (havent tested this) that this results in more or less the code size but its a) idential with the other functions and b) we late have more flexibility here. The Code got a quick SerialKeyboard.ino test --- .../avr/cores/arduino/PluggableUSB.cpp | 8 ++--- .../arduino/avr/cores/arduino/PluggableUSB.h | 8 ++--- .../arduino/avr/cores/arduino/USBCore.cpp | 4 +-- hardware/arduino/avr/libraries/HID/HID.cpp | 29 +++++++++---------- hardware/arduino/avr/libraries/HID/HID.h | 4 +-- 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp b/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp index 95d40791c13..21dc7a89dc7 100644 --- a/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp +++ b/hardware/arduino/avr/cores/arduino/PluggableUSB.cpp @@ -38,11 +38,11 @@ int PluggableUSB_::getInterface(uint8_t* interfaceCount) return sent; } -int PluggableUSB_::getDescriptor(int8_t type) +int PluggableUSB_::getDescriptor(USBSetup& setup) { PUSBListNode* node; for (node = rootNode; node; node = node->next) { - int ret = node->getDescriptor(type); + int ret = node->getDescriptor(setup); // ret!=0 -> request has been processed if (ret) return ret; @@ -50,11 +50,11 @@ int PluggableUSB_::getDescriptor(int8_t type) return 0; } -bool PluggableUSB_::setup(USBSetup& setup, uint8_t interfaceNum) +bool PluggableUSB_::setup(USBSetup& setup) { PUSBListNode* node; for (node = rootNode; node; node = node->next) { - if (node->setup(setup, interfaceNum)) { + if (node->setup(setup)) { return true; } } diff --git a/hardware/arduino/avr/cores/arduino/PluggableUSB.h b/hardware/arduino/avr/cores/arduino/PluggableUSB.h index b7c52d61062..d12c4c4320b 100644 --- a/hardware/arduino/avr/cores/arduino/PluggableUSB.h +++ b/hardware/arduino/avr/cores/arduino/PluggableUSB.h @@ -35,9 +35,9 @@ class PUSBListNode { inline uint8_t endpoint() const { return pluggedEndpoint; } protected: - virtual bool setup(USBSetup& setup, uint8_t interfaceNum) = 0; + virtual bool setup(USBSetup& setup) = 0; virtual int getInterface(uint8_t* interfaceCount) = 0; - virtual int getDescriptor(int8_t t) = 0; + virtual int getDescriptor(USBSetup& setup) = 0; uint8_t pluggedInterface; uint8_t pluggedEndpoint; @@ -56,8 +56,8 @@ class PluggableUSB_ { PluggableUSB_(); bool plug(PUSBListNode *node); int getInterface(uint8_t* interfaceCount); - int getDescriptor(int8_t type); - bool setup(USBSetup& setup, uint8_t interfaceNum); + int getDescriptor(USBSetup& setup); + bool setup(USBSetup& setup); private: uint8_t lastIf; diff --git a/hardware/arduino/avr/cores/arduino/USBCore.cpp b/hardware/arduino/avr/cores/arduino/USBCore.cpp index 5db9f521a99..f67bfeabc0c 100644 --- a/hardware/arduino/avr/cores/arduino/USBCore.cpp +++ b/hardware/arduino/avr/cores/arduino/USBCore.cpp @@ -362,7 +362,7 @@ bool ClassInterfaceRequest(USBSetup& setup) return CDC_Setup(setup); #ifdef PLUGGABLE_USB_ENABLED - return PluggableUSB().setup(setup, i); + return PluggableUSB().setup(setup); #endif return false; } @@ -476,7 +476,7 @@ bool SendDescriptor(USBSetup& setup) InitControl(setup.wLength); #ifdef PLUGGABLE_USB_ENABLED - ret = PluggableUSB().getDescriptor(t); + ret = PluggableUSB().getDescriptor(setup); if (ret != 0) { return (ret > 0 ? true : false); } diff --git a/hardware/arduino/avr/libraries/HID/HID.cpp b/hardware/arduino/avr/libraries/HID/HID.cpp index b6b9ceadc1a..500eec9da1b 100644 --- a/hardware/arduino/avr/libraries/HID/HID.cpp +++ b/hardware/arduino/avr/libraries/HID/HID.cpp @@ -38,22 +38,21 @@ int HID_::getInterface(uint8_t* interfaceCount) return USB_SendControl(0, &hidInterface, sizeof(hidInterface)); } -int HID_::getDescriptor(int8_t type) +int HID_::getDescriptor(USBSetup& setup) { - if (HID_REPORT_DESCRIPTOR_TYPE == type) { - int total = 0; - HIDDescriptorListNode* node; - for (node = rootNode; node; node = node->next) { - int res = USB_SendControl(TRANSFER_PGM, node->data, node->length); - if (res == -1) - return -1; - total += res; - } - return total; + if (interface() != setup.wIndex) { + return 0; } - // Ignored - return 0; + int total = 0; + HIDDescriptorListNode* node; + for (node = rootNode; node; node = node->next) { + int res = USB_SendControl(TRANSFER_PGM, node->data, node->length); + if (res == -1) + return -1; + total += res; + } + return total; } void HID_::AppendDescriptor(HIDDescriptorListNode *node) @@ -76,9 +75,9 @@ void HID_::SendReport(uint8_t id, const void* data, int len) USB_Send(endpoint() | TRANSFER_RELEASE, data, len); } -bool HID_::setup(USBSetup& setup, uint8_t interfaceNum) +bool HID_::setup(USBSetup& setup) { - if (interface() != interfaceNum) { + if (interface() != setup.wIndex) { return false; } diff --git a/hardware/arduino/avr/libraries/HID/HID.h b/hardware/arduino/avr/libraries/HID/HID.h index e54e45e1957..b282c20d5aa 100644 --- a/hardware/arduino/avr/libraries/HID/HID.h +++ b/hardware/arduino/avr/libraries/HID/HID.h @@ -80,8 +80,8 @@ class HID_ : public PUSBListNode protected: // Implementation of the PUSBListNode int getInterface(uint8_t* interfaceCount); - int getDescriptor(int8_t type); - bool setup(USBSetup& setup, uint8_t interfaceNum); + int getDescriptor(USBSetup& setup); + bool setup(USBSetup& setup); private: uint8_t epType[1]; From c8867462a8a8d1d0df921b21642c07483ec8118a Mon Sep 17 00:00:00 2001 From: NicoHood Date: Wed, 7 Oct 2015 19:11:23 +0200 Subject: [PATCH 3/4] [PHID] Added SetReport function This commit just shows other that this option is available and not implemented. You may use this to determine the Led Lights state of a keyboard or transmit data via RAWHID from the PC. Quick usage guide: int length = ((setup.wValueH << 8) | setup.wLength); USB_RecvControl((uint8_t* data, USB_EP_SIZE); // Needs to be splitted into USB_EP_SIZE packets, not shown here. See HID Project. --- hardware/arduino/avr/libraries/HID/HID.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hardware/arduino/avr/libraries/HID/HID.cpp b/hardware/arduino/avr/libraries/HID/HID.cpp index 500eec9da1b..fe88ecd3405 100644 --- a/hardware/arduino/avr/libraries/HID/HID.cpp +++ b/hardware/arduino/avr/libraries/HID/HID.cpp @@ -106,6 +106,9 @@ bool HID_::setup(USBSetup& setup) idle = setup.wValueL; return true; } + if (request == HID_SET_REPORT) + { + } } return false; From 05477fc85dec2a1a150d05d696bc8a8c5f5f0f1c Mon Sep 17 00:00:00 2001 From: NicoHood Date: Wed, 7 Oct 2015 20:39:50 +0200 Subject: [PATCH 4/4] [PUSB] Removed unnecessary endpoint and interface function --- hardware/arduino/avr/cores/arduino/PluggableUSB.h | 3 --- hardware/arduino/avr/libraries/HID/HID.cpp | 12 ++++++------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/PluggableUSB.h b/hardware/arduino/avr/cores/arduino/PluggableUSB.h index d12c4c4320b..1f3057ad221 100644 --- a/hardware/arduino/avr/cores/arduino/PluggableUSB.h +++ b/hardware/arduino/avr/cores/arduino/PluggableUSB.h @@ -31,9 +31,6 @@ class PUSBListNode { numEndpoints(numEps), numInterfaces(numIfs), endpointType(epType) { } - inline uint8_t interface() const { return pluggedInterface; } - inline uint8_t endpoint() const { return pluggedEndpoint; } - protected: virtual bool setup(USBSetup& setup) = 0; virtual int getInterface(uint8_t* interfaceCount) = 0; diff --git a/hardware/arduino/avr/libraries/HID/HID.cpp b/hardware/arduino/avr/libraries/HID/HID.cpp index fe88ecd3405..762d1708027 100644 --- a/hardware/arduino/avr/libraries/HID/HID.cpp +++ b/hardware/arduino/avr/libraries/HID/HID.cpp @@ -31,16 +31,16 @@ int HID_::getInterface(uint8_t* interfaceCount) { *interfaceCount += 1; // uses 1 HIDDescriptor hidInterface = { - D_INTERFACE(interface(), 1, 3, 0, 0), + D_INTERFACE(pluggedInterface, 1, 3, 0, 0), D_HIDREPORT(descriptorSize), - D_ENDPOINT(USB_ENDPOINT_IN(endpoint()), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01) + D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01) }; return USB_SendControl(0, &hidInterface, sizeof(hidInterface)); } int HID_::getDescriptor(USBSetup& setup) { - if (interface() != setup.wIndex) { + if (pluggedInterface != setup.wIndex) { return 0; } @@ -71,13 +71,13 @@ void HID_::AppendDescriptor(HIDDescriptorListNode *node) void HID_::SendReport(uint8_t id, const void* data, int len) { - USB_Send(endpoint(), &id, 1); - USB_Send(endpoint() | TRANSFER_RELEASE, data, len); + USB_Send(pluggedEndpoint, &id, 1); + USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, len); } bool HID_::setup(USBSetup& setup) { - if (interface() != setup.wIndex) { + if (pluggedInterface != setup.wIndex) { return false; }