Skip to content

Commit 3ba1a35

Browse files
committed
Moved EPHandler in USBDevice headers
The reference to the upper USBDevice class is passed on the EPHandler constructor.
1 parent 7d37753 commit 3ba1a35

File tree

2 files changed

+145
-143
lines changed

2 files changed

+145
-143
lines changed

cores/arduino/USB/SAMD21_USBDevice.h

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class USBDevice_SAMD21G18x {
6262
// USB Interrupts
6363
inline bool isEndOfResetInterrupt() { return usb.INTFLAG.bit.EORST; }
6464
inline void ackEndOfResetInterrupt() { usb.INTFLAG.reg = USB_DEVICE_INTFLAG_EORST; }
65-
inline void enableEndOfResetInterrupt() { usb.INTENSET.bit.EORST = 1; }
66-
inline void disableEndOfResetInterrupt() { usb.INTENCLR.bit.EORST = 1; }
65+
inline void enableEndOfResetInterrupt() { usb.INTENSET.bit.EORST = 1; }
66+
inline void disableEndOfResetInterrupt() { usb.INTENCLR.bit.EORST = 1; }
6767

6868
inline bool isStartOfFrameInterrupt() { return usb.INTFLAG.bit.SOF; }
6969
inline void ackStartOfFrameInterrupt() { usb.INTFLAG.reg = USB_DEVICE_INTFLAG_SOF; }
@@ -195,3 +195,145 @@ void USBDevice_SAMD21G18x::calibrate() {
195195
usb.PADCAL.bit.TRIM = pad_trim;
196196
}
197197

198+
class EPHandler {
199+
public:
200+
virtual void handleEndpoint() = 0;
201+
virtual uint32_t recv(void *_data, uint32_t len) = 0;
202+
virtual uint32_t available() const = 0;
203+
};
204+
205+
class DoubleBufferedEPOutHandler : public EPHandler {
206+
public:
207+
DoubleBufferedEPOutHandler(USBDevice_SAMD21G18x &usbDev, uint32_t endPoint, uint32_t bufferSize) :
208+
usbd(usbDev),
209+
ep(endPoint), size(bufferSize),
210+
current(0), incoming(0),
211+
first0(0), last0(0), ready0(false),
212+
first1(0), last1(0), ready1(false),
213+
notify(false)
214+
{
215+
data0 = reinterpret_cast<uint8_t *>(malloc(size));
216+
data1 = reinterpret_cast<uint8_t *>(malloc(size));
217+
218+
usbd.epBank0SetSize(ep, 64);
219+
usbd.epBank0SetType(ep, 3); // BULK OUT
220+
221+
release();
222+
}
223+
224+
// Read one byte from the buffer, if the buffer is empty -1 is returned
225+
int read() {
226+
if (current == 0) {
227+
if (!ready0) {
228+
return -1;
229+
}
230+
if (first0 == last0) {
231+
first0 = 0;
232+
last0 = 0;
233+
ready0 = false;
234+
if (notify) {
235+
release();
236+
}
237+
current = 1;
238+
return -1;
239+
}
240+
return data0[first0++];
241+
} else {
242+
if (!ready1) {
243+
return -1;
244+
}
245+
if (first1 == last1) {
246+
first1 = 0;
247+
last1 = 0;
248+
ready1 = false;
249+
if (notify) {
250+
release();
251+
}
252+
current = 0;
253+
return -1;
254+
}
255+
return data1[first1++];
256+
}
257+
}
258+
259+
virtual void handleEndpoint()
260+
{
261+
if (usbd.epBank0IsTransferComplete(ep))
262+
{
263+
// Ack Transfer complete
264+
usbd.epBank0AckTransferComplete(ep);
265+
266+
// Update counters and swap banks
267+
if (incoming == 0) {
268+
last0 = usbd.epBank0ByteCount(ep);
269+
ready0 = true;
270+
incoming = 1;
271+
} else {
272+
last1 = usbd.epBank0ByteCount(ep);
273+
ready1 = true;
274+
incoming = 0;
275+
}
276+
release();
277+
}
278+
}
279+
280+
virtual uint32_t recv(void *_data, uint32_t len)
281+
{
282+
uint8_t *data = reinterpret_cast<uint8_t *>(_data);
283+
uint32_t i;
284+
for (i=0; i<len; i++) {
285+
int c = read();
286+
if (c == -1) break;
287+
data[i] = c;
288+
}
289+
return i;
290+
}
291+
292+
// Returns how many bytes are stored in the buffers
293+
virtual uint32_t available() const {
294+
return (last0 - first0) + (last1 - first1);
295+
}
296+
297+
void release() {
298+
if (incoming == 0) {
299+
if (ready0) {
300+
notify = true;
301+
return;
302+
}
303+
usbd.epBank0SetAddress(ep, data0);
304+
} else {
305+
if (ready1) {
306+
notify = true;
307+
return;
308+
}
309+
usbd.epBank0SetAddress(ep, data1);
310+
}
311+
usbd.epBank0AckTransferComplete(ep);
312+
//usbd.epBank0AckTransferFailed(ep);
313+
usbd.epBank0EnableTransferComplete(ep);
314+
315+
// Release OUT EP
316+
usbd.epBank0SetMultiPacketSize(ep, size);
317+
usbd.epBank0SetByteCount(ep, 0);
318+
usbd.epBank0ResetReady(ep);
319+
notify = false;
320+
}
321+
322+
private:
323+
USBDevice_SAMD21G18x &usbd;
324+
325+
const uint32_t ep;
326+
const uint32_t size;
327+
uint32_t current, incoming;
328+
329+
uint8_t *data0;
330+
uint32_t first0, last0;
331+
bool ready0;
332+
333+
uint8_t *data1;
334+
uint32_t first1, last1;
335+
bool ready1;
336+
337+
bool notify;
338+
};
339+

cores/arduino/USB/USBCore.cpp

Lines changed: 1 addition & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -46,146 +46,6 @@ 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-
};
55-
56-
class DoubleBufferedEPOutHandler : public EPHandler {
57-
public:
58-
DoubleBufferedEPOutHandler(uint32_t endPoint, uint32_t bufferSize) :
59-
ep(endPoint), size(bufferSize),
60-
current(0), incoming(0),
61-
first0(0), last0(0), ready0(false),
62-
first1(0), last1(0), ready1(false),
63-
notify(false)
64-
{
65-
data0 = reinterpret_cast<uint8_t *>(malloc(size));
66-
data1 = reinterpret_cast<uint8_t *>(malloc(size));
67-
68-
usbd.epBank0SetSize(ep, 64);
69-
usbd.epBank0SetType(ep, 3); // BULK OUT
70-
71-
release();
72-
}
73-
74-
// Read one byte from the buffer, if the buffer is empty -1 is returned
75-
int read() {
76-
if (current == 0) {
77-
if (!ready0) {
78-
return -1;
79-
}
80-
if (first0 == last0) {
81-
first0 = 0;
82-
last0 = 0;
83-
ready0 = false;
84-
if (notify) {
85-
release();
86-
}
87-
current = 1;
88-
return -1;
89-
}
90-
return data0[first0++];
91-
} else {
92-
if (!ready1) {
93-
return -1;
94-
}
95-
if (first1 == last1) {
96-
first1 = 0;
97-
last1 = 0;
98-
ready1 = false;
99-
if (notify) {
100-
release();
101-
}
102-
current = 0;
103-
return -1;
104-
}
105-
return data1[first1++];
106-
}
107-
}
108-
109-
virtual void handleEndpoint()
110-
{
111-
if (usbd.epBank0IsTransferComplete(ep))
112-
{
113-
// Ack Transfer complete
114-
usbd.epBank0AckTransferComplete(ep);
115-
116-
// Update counters and swap banks
117-
if (incoming == 0) {
118-
last0 = usbd.epBank0ByteCount(ep);
119-
ready0 = true;
120-
incoming = 1;
121-
} else {
122-
last1 = usbd.epBank0ByteCount(ep);
123-
ready1 = true;
124-
incoming = 0;
125-
}
126-
release();
127-
}
128-
}
129-
130-
virtual uint32_t recv(void *_data, uint32_t len)
131-
{
132-
uint8_t *data = reinterpret_cast<uint8_t *>(_data);
133-
uint32_t i;
134-
for (i=0; i<len; i++) {
135-
int c = read();
136-
if (c == -1) break;
137-
data[i] = c;
138-
}
139-
return i;
140-
}
141-
142-
// Returns how many bytes are stored in the buffers
143-
virtual uint32_t available() const {
144-
return (last0 - first0) + (last1 - first1);
145-
}
146-
147-
void release() {
148-
if (incoming == 0) {
149-
if (ready0) {
150-
notify = true;
151-
return;
152-
}
153-
usbd.epBank0SetAddress(ep, data0);
154-
} else {
155-
if (ready1) {
156-
notify = true;
157-
return;
158-
}
159-
usbd.epBank0SetAddress(ep, data1);
160-
}
161-
usbd.epBank0AckTransferComplete(ep);
162-
//usbd.epBank0AckTransferFailed(ep);
163-
usbd.epBank0EnableTransferComplete(ep);
164-
165-
// Release OUT EP
166-
usbd.epBank0SetMultiPacketSize(ep, size);
167-
usbd.epBank0SetByteCount(ep, 0);
168-
usbd.epBank0ResetReady(ep);
169-
notify = false;
170-
}
171-
172-
private:
173-
const uint32_t ep;
174-
const uint32_t size;
175-
uint32_t current, incoming;
176-
177-
uint8_t *data0;
178-
uint32_t first0, last0;
179-
bool ready0;
180-
181-
uint8_t *data1;
182-
uint32_t first1, last1;
183-
bool ready1;
184-
185-
bool notify;
186-
};
187-
188-
18949
const uint16_t STRING_LANGUAGE[2] = {
19050
(3<<8) | (2+2),
19151
0x0409 // English
@@ -564,7 +424,7 @@ void USBDeviceClass::initEP(uint32_t ep, uint32_t config)
564424
}
565425
else if (config == (USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_OUT(0)))
566426
{
567-
epHandlers[ep] = new DoubleBufferedEPOutHandler(ep, 64);
427+
epHandlers[ep] = new DoubleBufferedEPOutHandler(usbd, ep, 64);
568428
}
569429
else if (config == (USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0)))
570430
{

0 commit comments

Comments
 (0)