Skip to content

Commit b2ddb22

Browse files
committed
USB: Added interface to possibly setup dynamic USB EP Handler
1 parent 34bf542 commit b2ddb22

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

cores/arduino/USB/USBCore.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ extern "C" void UDD_Handler(void) {
4646
USBDevice.ISRHandler();
4747
}
4848

49+
class EPHandler {
50+
public:
51+
virtual void handleEndpoint() = 0;
52+
virtual uint32_t recv(void *_data, uint32_t len) = 0;
53+
virtual uint32_t available() const = 0;
54+
};
4955

5056

5157
const uint16_t STRING_LANGUAGE[2] = {
@@ -88,6 +94,8 @@ uint8_t udd_ep_out_cache_buffer[7][64];
8894
static __attribute__((__aligned__(4))) //__attribute__((__section__(".bss_hram0")))
8995
uint8_t udd_ep_in_cache_buffer[7][64];
9096

97+
static EPHandler *epHandlers[7];
98+
9199
//==================================================================
92100

93101
// Send a USB descriptor string. The string is stored as a
@@ -532,7 +540,11 @@ uint32_t USBDeviceClass::recvControl(void *_data, uint32_t len)
532540
// Number of bytes, assumes a rx endpoint
533541
uint32_t USBDeviceClass::available(uint32_t ep)
534542
{
535-
return usbd.epBank0ByteCount(ep);
543+
if (epHandlers[ep]) {
544+
return epHandlers[ep]->available();
545+
} else {
546+
return usbd.epBank0ByteCount(ep);
547+
}
536548
}
537549

538550
// Non Blocking receive
@@ -542,6 +554,10 @@ uint32_t USBDeviceClass::recv(uint32_t ep, void *_data, uint32_t len)
542554
if (!_usbConfiguration)
543555
return -1;
544556

557+
if (epHandlers[ep]) {
558+
return epHandlers[ep]->recv(_data, len);
559+
}
560+
545561
if (available(ep) < len)
546562
len = available(ep);
547563

@@ -891,7 +907,11 @@ void USBDeviceClass::ISRHandler()
891907
if (usbd.epBank0IsTransferComplete(i) ||
892908
usbd.epBank1IsTransferComplete(i))
893909
{
894-
handleEndpoint(i);
910+
if (epHandlers[i]) {
911+
epHandlers[i]->handleEndpoint();
912+
} else {
913+
handleEndpoint(i);
914+
}
895915
}
896916
ept_int &= ~(1 << i);
897917
}

0 commit comments

Comments
 (0)