Skip to content

Commit aa92165

Browse files
committed
Merge branch 'phid-class'
2 parents cc4874b + 5b1b033 commit aa92165

File tree

11 files changed

+259
-254
lines changed

11 files changed

+259
-254
lines changed

hardware/arduino/avr/cores/arduino/PluggableUSB.cpp

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,78 +23,84 @@
2323
#if defined(USBCON)
2424
#ifdef PLUGGABLE_USB_ENABLED
2525

26-
#define MAX_MODULES 6
26+
extern uint8_t _initEndpoints[];
2727

28-
static u8 lastIf = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT;
29-
static u8 lastEp = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT;
30-
31-
extern u8 _initEndpoints[];
32-
33-
//PUSBCallbacks cbs[MAX_MODULES];
34-
static u8 modules_count = 0;
35-
36-
static PUSBListNode* rootNode = NULL;
37-
38-
int PUSB_GetInterface(u8* interfaceNum)
28+
int PluggableUSB_::getInterface(uint8_t* interfaceCount)
3929
{
40-
int ret = 0;
41-
PUSBListNode* node = rootNode;
42-
for (u8 i=0; i<modules_count; i++) {
43-
ret = node->cb->getInterface(interfaceNum);
44-
node = node->next;
30+
int sent = 0;
31+
PUSBListNode* node;
32+
for (node = rootNode; node; node = node->next) {
33+
int res = node->getInterface(interfaceCount);
34+
if (res < 0)
35+
return -1;
36+
sent += res;
4537
}
46-
return ret;
38+
return sent;
4739
}
4840

49-
int PUSB_GetDescriptor(int8_t t)
41+
int PluggableUSB_::getDescriptor(int8_t type)
5042
{
51-
int ret = 0;
52-
PUSBListNode* node = rootNode;
53-
for (u8 i=0; i<modules_count && ret == 0; i++) {
54-
ret = node->cb->getDescriptor(t);
55-
node = node->next;
43+
PUSBListNode* node;
44+
for (node = rootNode; node; node = node->next) {
45+
int ret = node->getDescriptor(type);
46+
// ret!=0 -> request has been processed
47+
if (ret)
48+
return ret;
5649
}
57-
return ret;
50+
return 0;
5851
}
5952

60-
bool PUSB_Setup(USBSetup& setup, u8 j)
53+
bool PluggableUSB_::setup(USBSetup& setup, uint8_t interfaceNum)
6154
{
62-
bool ret = false;
63-
PUSBListNode* node = rootNode;
64-
for (u8 i=0; i<modules_count && ret == false; i++) {
65-
ret = node->cb->setup(setup, j);
66-
node = node->next;
55+
PUSBListNode* node;
56+
for (node = rootNode; node; node = node->next) {
57+
if (node->setup(setup, interfaceNum)) {
58+
return true;
59+
}
6760
}
68-
return ret;
61+
return false;
6962
}
7063

71-
int8_t PUSB_AddFunction(PUSBListNode *node, u8* interface)
64+
bool PluggableUSB_::plug(PUSBListNode *node)
7265
{
73-
if (modules_count >= MAX_MODULES) {
74-
return 0;
66+
if ((lastEp + node->numEndpoints) > USB_ENDPOINTS) {
67+
return false;
7568
}
7669

77-
if (modules_count == 0) {
70+
if (!rootNode) {
7871
rootNode = node;
7972
} else {
8073
PUSBListNode *current = rootNode;
81-
while(current->next != NULL) {
74+
while (current->next) {
8275
current = current->next;
8376
}
8477
current->next = node;
8578
}
8679

87-
*interface = lastIf;
88-
lastIf += node->cb->numInterfaces;
89-
for ( u8 i = 0; i< node->cb->numEndpoints; i++) {
90-
_initEndpoints[lastEp] = node->cb->endpointType[i];
80+
node->pluggedInterface = lastIf;
81+
node->pluggedEndpoint = lastEp;
82+
lastIf += node->numInterfaces;
83+
for (uint8_t i = 0; i < node->numEndpoints; i++) {
84+
_initEndpoints[lastEp] = node->endpointType[i];
9185
lastEp++;
9286
}
93-
modules_count++;
94-
return lastEp - node->cb->numEndpoints;
87+
return true;
9588
// restart USB layer???
9689
}
9790

91+
PluggableUSB_& PluggableUSB()
92+
{
93+
static PluggableUSB_ obj;
94+
return obj;
95+
}
96+
97+
PluggableUSB_::PluggableUSB_() : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT),
98+
lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT),
99+
rootNode(NULL)
100+
{
101+
// Empty
102+
}
103+
98104
#endif
99105

100-
#endif /* if defined(USBCON) */
106+
#endif /* if defined(USBCON) */

hardware/arduino/avr/cores/arduino/PluggableUSB.h

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,50 @@
2525

2626
#if defined(USBCON)
2727

28-
typedef struct __attribute__((packed))
29-
{
30-
bool (*setup)(USBSetup& setup, u8 i);
31-
int (*getInterface)(u8* interfaceNum);
32-
int (*getDescriptor)(int8_t t);
33-
int8_t numEndpoints;
34-
int8_t numInterfaces;
35-
uint8_t *endpointType;
36-
} PUSBCallbacks;
37-
3828
class PUSBListNode {
3929
public:
30+
PUSBListNode(int8_t numEps, int8_t numIfs, uint8_t *epType) :
31+
numEndpoints(numEps), numInterfaces(numIfs), endpointType(epType)
32+
{ }
33+
34+
inline uint8_t interface() const { return pluggedInterface; }
35+
inline int8_t endpoint() const { return pluggedEndpoint; }
36+
37+
protected:
38+
virtual bool setup(USBSetup& setup, uint8_t interfaceNum) = 0;
39+
virtual int getInterface(uint8_t* interfaceCount) = 0;
40+
virtual int getDescriptor(int8_t t) = 0;
41+
42+
uint8_t pluggedInterface;
43+
int8_t pluggedEndpoint;
44+
45+
const int8_t numEndpoints;
46+
const int8_t numInterfaces;
47+
const uint8_t *endpointType;
48+
4049
PUSBListNode *next = NULL;
41-
PUSBCallbacks *cb;
42-
PUSBListNode(PUSBCallbacks *ncb) {cb = ncb;}
43-
};
4450

45-
int8_t PUSB_AddFunction(PUSBListNode *node, u8 *interface);
51+
friend class PluggableUSB_;
52+
};
4653

47-
int PUSB_GetInterface(u8* interfaceNum);
54+
class PluggableUSB_ {
55+
public:
56+
PluggableUSB_();
57+
bool plug(PUSBListNode *node);
58+
int getInterface(uint8_t* interfaceCount);
59+
int getDescriptor(int8_t type);
60+
bool setup(USBSetup& setup, uint8_t interfaceNum);
4861

49-
int PUSB_GetDescriptor(int8_t t);
62+
private:
63+
uint8_t lastIf;
64+
uint8_t lastEp;
65+
PUSBListNode* rootNode;
66+
};
5067

51-
bool PUSB_Setup(USBSetup& setup, u8 i);
68+
// Replacement for global singleton.
69+
// This function prevents static-initialization-order-fiasco
70+
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
71+
PluggableUSB_& PluggableUSB();
5272

5373
#endif
5474

hardware/arduino/avr/cores/arduino/USBCore.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,15 @@ int USB_Send(u8 ep, const void* d, int len)
308308
return r;
309309
}
310310

311-
u8 _initEndpoints[] =
311+
u8 _initEndpoints[USB_ENDPOINTS] =
312312
{
313-
0,
313+
0, // Control Endpoint
314314

315-
EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
316-
EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
317-
EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
315+
EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
316+
EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
317+
EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
318318

319-
#ifdef PLUGGABLE_USB_ENABLED
320-
//allocate 3 endpoints and remove const so they can be changed by the user
321-
0,
322-
0,
323-
0,
324-
#endif
319+
// Following endpoints are automatically initialized to 0
325320
};
326321

327322
#define EP_SINGLE_64 0x32 // EP0
@@ -367,7 +362,7 @@ bool ClassInterfaceRequest(USBSetup& setup)
367362
return CDC_Setup(setup);
368363

369364
#ifdef PLUGGABLE_USB_ENABLED
370-
return PUSB_Setup(setup, i);
365+
return PluggableUSB().setup(setup, i);
371366
#endif
372367
return false;
373368
}
@@ -445,7 +440,7 @@ static u8 SendInterfaces()
445440
CDC_GetInterface(&interfaces);
446441

447442
#ifdef PLUGGABLE_USB_ENABLED
448-
PUSB_GetInterface(&interfaces);
443+
PluggableUSB().getInterface(&interfaces);
449444
#endif
450445

451446
return interfaces;
@@ -481,7 +476,7 @@ bool SendDescriptor(USBSetup& setup)
481476

482477
InitControl(setup.wLength);
483478
#ifdef PLUGGABLE_USB_ENABLED
484-
ret = PUSB_GetDescriptor(t);
479+
ret = PluggableUSB().getDescriptor(t);
485480
if (ret != 0) {
486481
return (ret > 0 ? true : false);
487482
}

hardware/arduino/avr/cores/arduino/USBCore.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@
9999

100100
// bEndpointAddress in Endpoint Descriptor
101101
#define USB_ENDPOINT_DIRECTION_MASK 0x80
102-
#define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
103-
#define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
102+
#define USB_ENDPOINT_OUT(addr) (lowByte((addr) | 0x00))
103+
#define USB_ENDPOINT_IN(addr) (lowByte((addr) | 0x80))
104104

105105
#define USB_ENDPOINT_TYPE_MASK 0x03
106106
#define USB_ENDPOINT_TYPE_CONTROL 0x00
@@ -277,4 +277,4 @@ typedef struct
277277
#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
278278

279279

280-
#endif
280+
#endif

hardware/arduino/avr/cores/arduino/USBDesc.h

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
2-
3-
/* Copyright (c) 2011, Peter Barrett
4-
**
5-
** Permission to use, copy, modify, and/or distribute this software for
6-
** any purpose with or without fee is hereby granted, provided that the
7-
** above copyright notice and this permission notice appear in all copies.
8-
**
9-
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10-
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11-
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12-
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13-
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14-
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15-
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16-
** SOFTWARE.
17-
*/
1+
/*
2+
Copyright (c) 2011, Peter Barrett
3+
Copyright (c) 2015, Arduino LLC
4+
5+
Permission to use, copy, modify, and/or distribute this software for
6+
any purpose with or without fee is hereby granted, provided that the
7+
above copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10+
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11+
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12+
BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13+
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15+
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16+
SOFTWARE.
17+
*/
1818

1919
#define PLUGGABLE_USB_ENABLED
2020

21+
#if defined(EPRST6)
22+
#define USB_ENDPOINTS 7 // AtMegaxxU4
23+
#else
24+
#define USB_ENDPOINTS 5 // AtMegaxxU2
25+
#endif
2126

2227
#define CDC_INTERFACE_COUNT 2
2328
#define CDC_ENPOINT_COUNT 3

0 commit comments

Comments
 (0)