Skip to content

Commit e42d7d6

Browse files
committed
[PUSB] Fixed check for available endpoints
The check for available slot in PluggableUSB is done on the endpoint and not on the number of plugged modules. The modulesCount field is no longer useful and it has been removed.
1 parent c0f9296 commit e42d7d6

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

cores/arduino/PluggableUSB.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,56 @@
2323
#if defined(USBCON)
2424
#ifdef PLUGGABLE_USB_ENABLED
2525

26-
#define MAX_MODULES 6
26+
// TODO: set correct value for different CPUs
27+
#define MAX_EP 6
2728

2829
extern uint8_t _initEndpoints[];
2930

30-
//PUSBCallbacks cbs[MAX_MODULES];
31-
3231
PluggableUSB_ PluggableUSB;
3332

3433
int PluggableUSB_::getInterface(uint8_t* interfaceNum)
3534
{
3635
int ret = 0;
37-
PUSBListNode* node = rootNode;
38-
for (uint8_t i=0; i<modulesCount; i++) {
36+
PUSBListNode* node;
37+
for (node = rootNode; node; node = node->next) {
3938
ret = node->getInterface(interfaceNum);
40-
node = node->next;
4139
}
4240
return ret;
4341
}
4442

4543
int PluggableUSB_::getDescriptor(int8_t t)
4644
{
47-
int ret = 0;
48-
PUSBListNode* node = rootNode;
49-
for (uint8_t i=0; i<modulesCount && ret == 0; i++) {
50-
ret = node->getDescriptor(t);
51-
node = node->next;
45+
PUSBListNode* node;
46+
for (node = rootNode; node; node = node->next) {
47+
int ret = node->getDescriptor(t);
48+
if (ret)
49+
return ret;
5250
}
53-
return ret;
51+
return 0;
5452
}
5553

5654
bool PluggableUSB_::setup(USBSetup& setup, uint8_t j)
5755
{
58-
bool ret = false;
59-
PUSBListNode* node = rootNode;
60-
for (uint8_t i=0; i<modulesCount && ret == false; i++) {
61-
ret = node->setup(setup, j);
62-
node = node->next;
56+
PUSBListNode* node;
57+
for (node = rootNode; node; node = node->next) {
58+
if (node->setup(setup, j)) {
59+
return true;
60+
}
6361
}
64-
return ret;
62+
return false;
6563
}
6664

6765
bool PluggableUSB_::plug(PUSBListNode *node)
6866
{
69-
if (modulesCount >= MAX_MODULES) {
67+
if ((lastEp + node->numEndpoints) >= MAX_EP) {
7068
return false;
7169
}
7270

73-
if (modulesCount == 0) {
71+
if (!rootNode) {
7472
rootNode = node;
7573
} else {
7674
PUSBListNode *current = rootNode;
77-
while(current->next != NULL) {
75+
while (current->next) {
7876
current = current->next;
7977
}
8078
current->next = node;
@@ -83,18 +81,17 @@ bool PluggableUSB_::plug(PUSBListNode *node)
8381
node->pluggedInterface = lastIf;
8482
node->pluggedEndpoint = lastEp;
8583
lastIf += node->numInterfaces;
86-
for (uint8_t i=0; i<node->numEndpoints; i++) {
84+
for (uint8_t i = 0; i < node->numEndpoints; i++) {
8785
_initEndpoints[lastEp] = node->endpointType[i];
8886
lastEp++;
8987
}
90-
modulesCount++;
9188
return true;
9289
// restart USB layer???
9390
}
9491

9592
PluggableUSB_::PluggableUSB_() : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT),
9693
lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT),
97-
modulesCount(0), rootNode(NULL)
94+
rootNode(NULL)
9895
{
9996
// Empty
10097
}

cores/arduino/PluggableUSB.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class PluggableUSB_ {
6363
private:
6464
uint8_t lastIf;
6565
uint8_t lastEp;
66-
uint8_t modulesCount;
6766
PUSBListNode* rootNode;
6867
};
6968

0 commit comments

Comments
 (0)