Skip to content

Commit 6ae9d41

Browse files
committed
Update USBHID and USBCDC for new AsyncOp
Update USB classes to use the new AsyncOp API.
1 parent 84d0b60 commit 6ae9d41

File tree

4 files changed

+224
-252
lines changed

4 files changed

+224
-252
lines changed

usb/device/USBHID/USBHID.cpp

Lines changed: 94 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,91 @@
2121

2222
class USBHID::AsyncSend: public AsyncOp {
2323
public:
24-
AsyncSend(const HID_REPORT *report): AsyncOp(NULL), report(report), result(false)
24+
AsyncSend(USBHID *hid, const HID_REPORT *report): hid(hid), report(report), result(false)
2525
{
2626

2727
}
28+
29+
~AsyncSend()
30+
{
31+
32+
}
33+
34+
virtual bool process()
35+
{
36+
if (!hid->configured()) {
37+
result = false;
38+
return true;
39+
}
40+
41+
if (hid->send_nb(report)) {
42+
result = true;
43+
return true;
44+
}
45+
46+
return false;
47+
}
48+
49+
USBHID *hid;
2850
const HID_REPORT *report;
2951
bool result;
3052
};
3153

3254
class USBHID::AsyncRead: public AsyncOp {
3355
public:
34-
AsyncRead(HID_REPORT *report): AsyncOp(NULL), report(report), result(false)
56+
AsyncRead(USBHID *hid, HID_REPORT *report): hid(hid), report(report), result(false)
57+
{
58+
59+
}
60+
61+
~AsyncRead()
3562
{
3663

3764
}
65+
66+
virtual bool process()
67+
{
68+
if (!hid->configured()) {
69+
result = false;
70+
return true;
71+
}
72+
73+
if (hid->read_nb(report)) {
74+
result = true;
75+
return true;
76+
}
77+
78+
return false;
79+
}
80+
81+
USBHID *hid;
3882
HID_REPORT *report;
3983
bool result;
4084
};
4185

86+
class USBHID::AsyncWait: public AsyncOp {
87+
public:
88+
AsyncWait(USBHID *hid): hid(hid)
89+
{
90+
91+
}
92+
93+
~AsyncWait()
94+
{
95+
96+
}
97+
98+
virtual bool process()
99+
{
100+
if (hid->configured()) {
101+
return true;
102+
}
103+
104+
return false;
105+
}
106+
107+
USBHID *hid;
108+
};
42109

43110
USBHID::USBHID(bool connect_blocking, uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
44111
: USBDevice(get_usb_phy(), vendor_id, product_id, product_release)
@@ -88,39 +155,25 @@ void USBHID::wait_ready()
88155
{
89156
lock();
90157

91-
AsyncOp wait_op(NULL);
92-
wait_op.start(&_connect_list);
93-
if (configured()) {
94-
wait_op.complete();
95-
}
158+
AsyncWait wait_op(this);
159+
_connect_list.add(&wait_op);
96160

97161
unlock();
98162

99-
wait_op.wait();
163+
wait_op.wait(NULL);
100164
}
101165

102166

103167
bool USBHID::send(const HID_REPORT *report)
104168
{
105169
lock();
106170

107-
if (!configured()) {
108-
unlock();
109-
return false;
110-
}
111-
112-
if (send_nb(report)) {
113-
unlock();
114-
return true;
115-
}
116-
117-
AsyncSend send_op(report);
118-
send_op.start(&_send_list);
171+
AsyncSend send_op(this, report);
172+
_send_list.add(&send_op);
119173

120174
unlock();
121175

122-
send_op.wait();
123-
176+
send_op.wait(NULL);
124177
return send_op.result;
125178
}
126179

@@ -149,23 +202,12 @@ bool USBHID::read(HID_REPORT *report)
149202
{
150203
lock();
151204

152-
if (!configured()) {
153-
unlock();
154-
return false;
155-
}
156-
157-
if (read_nb(report)) {
158-
unlock();
159-
return true;
160-
}
161-
162-
AsyncRead read_op(report);
163-
read_op.start(&_read_list);
205+
AsyncRead read_op(this, report);
206+
_read_list.add(&read_op);
164207

165208
unlock();
166209

167-
read_op.wait();
168-
210+
read_op.wait(NULL);
169211
return read_op.result;
170212
}
171213

@@ -198,13 +240,8 @@ void USBHID::_send_isr(usb_ep_t endpoint)
198240
write_finish(_int_in);
199241
_send_idle = true;
200242

201-
AsyncSend *send_op = _send_list.head();
202-
if (send_op != NULL) {
203-
if (send_nb(send_op->report)) {
204-
send_op->result = true;
205-
send_op->complete();
206-
}
207-
} else {
243+
_send_list.process();
244+
if (_send_idle) {
208245
report_tx();
209246
}
210247

@@ -217,60 +254,12 @@ void USBHID::_read_isr(usb_ep_t endpoint)
217254
_output_report.length = read_finish(_int_out);
218255
_read_idle = true;
219256

220-
AsyncRead *read_op = _read_list.head();
221-
if (read_op != NULL) {
222-
if (read_nb(read_op->report)) {
223-
read_op->result = true;
224-
read_op->complete();
225-
}
226-
} else {
257+
_read_list.process();
258+
if (_read_idle) {
227259
report_rx();
228260
}
229261
}
230262

231-
void USBHID::_connect_wake_all()
232-
{
233-
assert_locked();
234-
235-
AsyncOp *wait_op = _connect_list.head();
236-
while (wait_op != NULL) {
237-
wait_op->complete();
238-
wait_op = _connect_list.head();
239-
}
240-
}
241-
242-
void USBHID::_send_abort_all()
243-
{
244-
assert_locked();
245-
246-
if (!_send_idle) {
247-
endpoint_abort(_int_in);
248-
_send_idle = true;
249-
}
250-
AsyncSend *tx_cur = _send_list.head();
251-
while (tx_cur != NULL) {
252-
tx_cur->result = false;
253-
tx_cur->complete();
254-
tx_cur = _send_list.head();
255-
}
256-
}
257-
258-
void USBHID::_read_abort_all()
259-
{
260-
assert_locked();
261-
262-
if (!_read_idle) {
263-
endpoint_abort(_int_out);
264-
_read_idle = true;
265-
}
266-
AsyncRead *rx_cur = _read_list.head();
267-
while (rx_cur != NULL) {
268-
rx_cur->result = false;
269-
rx_cur->complete();
270-
rx_cur = _read_list.head();
271-
}
272-
}
273-
274263
uint16_t USBHID::report_desc_length()
275264
{
276265
report_desc();
@@ -280,12 +269,19 @@ uint16_t USBHID::report_desc_length()
280269

281270
void USBHID::callback_state_change(DeviceState new_state)
282271
{
283-
if (new_state == Configured) {
284-
_connect_wake_all();
285-
} else {
286-
_send_abort_all();
287-
_read_abort_all();
272+
if (new_state != Configured) {
273+
if (!_send_idle) {
274+
endpoint_abort(_int_in);
275+
_send_idle = true;
276+
}
277+
if (!_read_idle) {
278+
endpoint_abort(_int_out);
279+
_read_idle = true;
280+
}
288281
}
282+
_send_list.process();
283+
_read_list.process();
284+
_connect_list.process();
289285
}
290286

291287
//

usb/device/USBHID/USBHID.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
#include "USBDevice.h"
2323

2424
#include "USBHID_Types.h"
25-
#include "AsyncOp.h"
26-
#include "LinkedList.h"
25+
#include "OperationList.h"
2726

2827

2928

@@ -249,17 +248,14 @@ class USBHID: public USBDevice {
249248
void _send_isr(usb_ep_t endpoint);
250249
void _read_isr(usb_ep_t endpoint);
251250

252-
void _connect_wake_all();
253-
void _send_abort_all();
254-
void _read_abort_all();
255-
256251
class AsyncSend;
257252
class AsyncRead;
253+
class AsyncWait;
258254

259-
LinkedList<AsyncOp> _connect_list;
260-
LinkedList<AsyncSend> _send_list;
255+
OperationList<AsyncWait> _connect_list;
256+
OperationList<AsyncSend> _send_list;
261257
bool _send_idle;
262-
LinkedList<AsyncRead> _read_list;
258+
OperationList<AsyncRead> _read_list;
263259
bool _read_idle;
264260

265261
uint8_t _configuration_descriptor[41];

0 commit comments

Comments
 (0)