Skip to content

Commit 89928b4

Browse files
facchinmcmaglie
authored andcommitted
rework PUSBCallbacks initialization
1 parent ada0e4c commit 89928b4

File tree

4 files changed

+50
-41
lines changed

4 files changed

+50
-41
lines changed

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

+8-20
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@
2828
static u8 lastIf = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT;
2929
static u8 lastEp = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT;
3030

31-
class PUSBListNode {
32-
public:
33-
PUSBListNode *next = NULL;
34-
PUSBCallbacks cb;
35-
};
36-
3731
extern u8 _initEndpoints[];
3832

3933
//PUSBCallbacks cbs[MAX_MODULES];
@@ -47,7 +41,7 @@ int8_t PUSB_GetInterface(u8* interfaceNum)
4741
int8_t ret = 0;
4842
PUSBListNode* node = rootNode;
4943
for (u8 i=0; i<modules_count; i++) {
50-
ret = node->cb.getInterface(interfaceNum);
44+
ret = node->cb->getInterface(interfaceNum);
5145
node = node->next;
5246
}
5347
return ret;
@@ -58,7 +52,7 @@ int8_t PUSB_GetDescriptor(int8_t t)
5852
int8_t ret = 0;
5953
PUSBListNode* node = rootNode;
6054
for (u8 i=0; i<modules_count && ret == 0; i++) {
61-
ret = node->cb.getDescriptor(t);
55+
ret = node->cb->getDescriptor(t);
6256
node = node->next;
6357
}
6458
return ret;
@@ -69,24 +63,18 @@ bool PUSB_Setup(Setup& setup, u8 j)
6963
bool ret = false;
7064
PUSBListNode* node = rootNode;
7165
for (u8 i=0; i<modules_count && ret == false; i++) {
72-
ret = node->cb.setup(setup, j);
66+
ret = node->cb->setup(setup, j);
7367
node = node->next;
7468
}
7569
return ret;
7670
}
7771

78-
int8_t PUSB_AddFunction(PUSBCallbacks *cb, u8* interface)
72+
int8_t PUSB_AddFunction(PUSBListNode *node, u8* interface)
7973
{
8074
if (modules_count >= MAX_MODULES) {
8175
return 0;
8276
}
8377

84-
PUSBListNode *node = new PUSBListNode;
85-
86-
node->cb.setup = cb->setup;
87-
node->cb.getInterface = cb->getInterface;
88-
node->cb.getDescriptor = cb->getDescriptor;
89-
9078
if (modules_count == 0) {
9179
rootNode = node;
9280
lastNode = node;
@@ -95,13 +83,13 @@ int8_t PUSB_AddFunction(PUSBCallbacks *cb, u8* interface)
9583
}
9684

9785
*interface = lastIf;
98-
lastIf += cb->numInterfaces;
99-
for ( u8 i = 0; i< cb->numEndpoints; i++) {
100-
_initEndpoints[lastEp] = cb->endpointType[i];
86+
lastIf += node->cb->numInterfaces;
87+
for ( u8 i = 0; i< node->cb->numEndpoints; i++) {
88+
_initEndpoints[lastEp] = node->cb->endpointType[i];
10189
lastEp++;
10290
}
10391
modules_count++;
104-
return lastEp - cb->numEndpoints;
92+
return lastEp - node->cb->numEndpoints;
10593
// restart USB layer???
10694
}
10795

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ typedef struct
3232
int8_t (*getDescriptor)(int8_t t);
3333
int8_t numEndpoints;
3434
int8_t numInterfaces;
35-
u8 endpointType[];
35+
uint8_t *endpointType;
3636
} PUSBCallbacks;
3737

3838
typedef struct
@@ -41,7 +41,14 @@ typedef struct
4141
u8 firstEndpoint;
4242
} PUSBReturn;
4343

44-
int8_t PUSB_AddFunction(PUSBCallbacks *cb, u8 *interface);
44+
class PUSBListNode {
45+
public:
46+
PUSBListNode *next = NULL;
47+
PUSBCallbacks *cb;
48+
PUSBListNode(PUSBCallbacks *ncb) {cb = ncb;}
49+
};
50+
51+
int8_t PUSB_AddFunction(PUSBListNode *node, u8 *interface);
4552

4653
int8_t PUSB_GetInterface(u8* interfaceNum);
4754

libraries/HID/HID.cpp

+16-9
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,22 @@ bool WEAK HID_Setup(Setup& setup, u8 i)
114114
// to be called by begin(), will trigger USB disconnection and reconnection
115115
int8_t HID_Plug(void)
116116
{
117-
PUSBCallbacks cb;
118-
119-
cb.setup = &HID_Setup;
120-
cb.getInterface = &HID_GetInterface;
121-
cb.getDescriptor = &HID_GetDescriptor;
122-
cb.numEndpoints = 1;
123-
cb.numInterfaces = 1;
124-
cb.endpointType[0] = EP_TYPE_INTERRUPT_IN;
125-
HID_ENDPOINT_INT = PUSB_AddFunction(&cb, &HID_INTERFACE);
117+
static uint8_t endpointType[1];
118+
119+
endpointType[0] = EP_TYPE_INTERRUPT_IN;
120+
121+
static PUSBCallbacks cb = {
122+
.setup = &HID_Setup,
123+
.getInterface = &HID_GetInterface,
124+
.getDescriptor = &HID_GetDescriptor,
125+
.numEndpoints = 1,
126+
.numInterfaces = 1,
127+
.endpointType = endpointType,
128+
};
129+
130+
static PUSBListNode node(&cb);
131+
132+
HID_ENDPOINT_INT = PUSB_AddFunction(&node, &HID_INTERFACE);
126133

127134
_hidInterface =
128135
{

libraries/MIDIUSB/MIDIUSB.cpp

+17-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "PluggableUSB.h"
1717
#include "MIDIUSB.h"
1818

19-
#define MIDI_BUFFER_SIZE 128
19+
#define MIDI_BUFFER_SIZE 16
2020

2121

2222
static u8 MIDI_AC_INTERFACE; // MIDI AC Interface
@@ -173,17 +173,24 @@ void MIDI_::sendMIDI(midiEventPacket_t event)
173173

174174
int8_t MIDI_plug(void)
175175
{
176-
PUSBCallbacks cb;
177176

178-
cb.setup = &MIDI_Setup;
179-
cb.getInterface = &MIDI_GetInterface;
180-
cb.getDescriptor = &MIDI_GetDescriptor;
181-
cb.numEndpoints = 2;
182-
cb.numInterfaces = 2;
183-
cb.endpointType[0] = EP_TYPE_BULK_OUT_MIDI; // MIDI_ENDPOINT_OUT
184-
cb.endpointType[1] = EP_TYPE_BULK_IN_MIDI; // MIDI_ENDPOINT_IN
177+
static uint8_t endpointType[2];
185178

186-
MIDI_ENDPOINT_OUT = PUSB_AddFunction(&cb, &MIDI_AC_INTERFACE);
179+
endpointType[0] = EP_TYPE_BULK_OUT_MIDI; // MIDI_ENDPOINT_OUT
180+
endpointType[1] = EP_TYPE_BULK_IN_MIDI; // MIDI_ENDPOINT_IN
181+
182+
static PUSBCallbacks cb = {
183+
.setup = &MIDI_Setup,
184+
.getInterface = &MIDI_GetInterface,
185+
.getDescriptor = &MIDI_GetDescriptor,
186+
.numEndpoints = 1,
187+
.numInterfaces = 2,
188+
.endpointType = endpointType,
189+
};
190+
191+
static PUSBListNode node(&cb);
192+
193+
MIDI_ENDPOINT_OUT = PUSB_AddFunction(&node, &MIDI_AC_INTERFACE);
187194
MIDI_ENDPOINT_IN = MIDI_ENDPOINT_OUT + 1;
188195
MIDI_INTERFACE = MIDI_AC_INTERFACE + 1;
189196

0 commit comments

Comments
 (0)