-
Notifications
You must be signed in to change notification settings - Fork 1
/
usb.c
564 lines (462 loc) · 18.3 KB
/
usb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//
// TODO;
// proper suspend/wakeup detection
// string descriptors?
//
#include "usb.h"
#include "stm32f103usb.h"
#if 0
#include "printf.h"
extern size_t u1puts(const char* buf, size_t len); // in main
static const char* _statstr[] = { "DISABLED", "STALL", "NAK", "VALID" };
static void dbg_epr(const char* msg, uint16_t epr) {
cbprintf(u1puts, "%s rx:%s%s %s tx:%s%s %s %s\n", msg,
(epr&USB_EPRx_CTR_RX) ? " CTR":"", (epr&USB_EPRx_DTOG_RX) ? " D1" : " D0", _statstr[(epr>>12)&3],
(epr&USB_EPRx_CTR_TX) ? " CTR":"", (epr&USB_EPRx_DTOG_TX) ? " D1" : " D0", _statstr[(epr>>4)&3],
(epr&USB_EPRx_EP_KIND) ? " STATUS_OUT" : "" );
}
#endif
static enum usb_state_t _usb_state = USB_UNATTACHED;
//static enum usb_state_t _usb_state_saved = USB_UNATTACHED; // when state is suspended, the state to return to on wakeup
enum usb_state_t usb_state() { return _usb_state; }
static const char* _sstr[] = {
"UNATTACHED", "DEFAULT", "ADDRESS", "CONFIGURED", "SUSPENDED",
};
const char* usb_state_str(enum usb_state_t s) {
size_t ss = s;
if (ss < sizeof _sstr)
return _sstr[ss];
return "UNDEFINED";
}
void usb_init() {
_usb_state = USB_UNATTACHED;
USB.CNTR = USB_CNTR_FRES; // hold in reset, clear power down
USB.ISTR = 0;
USB.DADDR = 0;
USB.BTABLE = 0;
for (int i = 0; i < 8; ++i) {
usb_ep_reset(i);
}
// 512 bytes in USB_PMA
// btable: 8 * 4 * 2 = 64 bytes, in ADDR_xX units
USB_PMA.btable[0].ADDR_TX = 1 * 64; // control endpoint 0x00/0x80
USB_PMA.btable[0].ADDR_RX = 2 * 64;
usb_ep_set_rx_size(0, 64);
USB_PMA.btable[1].ADDR_TX = 3 * 64; // bulk endpoint 0x01/0x81
USB_PMA.btable[1].ADDR_RX = 4 * 64;
usb_ep_set_rx_size(1, 64);
// bring out of reset and enable interrupts
USB.CNTR = USB_CNTR_CTRM | USB_CNTR_RESETM; //| USB_CNTR_WKUPM | USB_CNTR_SUSPM;
}
void usb_shutdown() {
USB.CNTR = USB_CNTR_FRES;
USB.ISTR = 0;
USB.CNTR = USB_CNTR_FRES | USB_CNTR_PDWN | USB_CNTR_LPMODE;
_usb_state = USB_UNATTACHED;
}
static inline uint16_t read_le16(const uint8_t* src) { return ((uint16_t)(src[0])) | (((uint16_t)(src[1])) << 8); }
// copy buf[:min(len,sz)] from the rx packet buffer of endpoint ep
static size_t read_buffer(uint8_t ep, uint8_t* buf, size_t sz) {
uint16_t len = usb_ep_get_rx_count(ep);
if (len > sz) {
len = sz;
}
const uint16_t* src = usb_ep_rx_buf(ep);
for (size_t i = 0; 2 * i + 1 < len; ++i) {
buf[2 * i] = src[2 * i];
buf[2 * i + 1] = src[2 * i] >> 8;
}
if (len & 1) {
buf[len - 1] = src[len - 1];
}
return len;
}
// copy buf[:sz] to the tx packet buffer of endpoint ep
static size_t write_buffer(uint8_t ep, const uint8_t* buf, size_t len) {
if (len > 64) {
len = 64;
}
uint16_t* dst = usb_ep_tx_buf(ep);
for (size_t i = 0; 2 * i + 1 < len; ++i) {
dst[2 * i] = read_le16(buf + 2 * i);
}
if (len & 1) {
dst[len - 1] = buf[len - 1];
}
usb_ep_set_tx_count(ep, len);
return len;
}
static void handle_ep0(void); // below
size_t usb_recv(uint8_t* buf, size_t sz) {
uint16_t istr = USB.ISTR;
USB.ISTR &= ~(USB_ISTR_SOF | USB_ISTR_ESOF | USB_ISTR_ERR | USB_ISTR_PMAOVR);
if (istr & USB_ISTR_RESET) {
USB.ISTR &= ~(USB_ISTR_RESET | USB_ISTR_WKUP | USB_ISTR_SUSP);
USB.CNTR &= ~(USB_CNTR_RESUME | USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_PDWN | USB_CNTR_FRES);
usb_ep_config(0, USB_EP_TYPE_CONTROL, 0);
usb_ep_set_stat_tx(0, USB_EP_STAT_STALL); // only setup will succeed
usb_ep_set_stat_rx(0, USB_EP_STAT_STALL);
for (int i = 1; i < 8; ++i) {
usb_ep_reset(i);
}
usb_daddr_set_add(0);
USB.DADDR |= USB_DADDR_EF;
_usb_state = USB_DEFAULT;
return 0;
}
#if 0
// USB2.0 sec 9.1.1.6: When suspended, the USB device maintains any internal
// status, including its address and configuration.
// The device must draw less than 2.5mA from the bus in this configuration.
// TODO: facility to sleep/wkup the entire F103?
if (istr & USB_ISTR_WKUP) {
USB.CNTR &= ~USB_CNTR_FSUSP;
USB.ISTR &= ~USB_ISTR_WKUP;
// According to RMA008 23.4.5, table 172, the RXDP line
// must be checked for spurious wakeups through noise on the bus.
if ((USB.FNR & (USB_FNR_RXDP|USB_FNR_RXDM)) != USB_FNR_RXDM) {
// spurious?
}
_usb_state = _usb_state_saved;
return 0;
}
if (istr & USB_ISTR_SUSP) {
USB.CNTR |= USB_CNTR_FSUSP;
USB.ISTR &= ~USB_ISTR_SUSP;
_usb_state_saved = _usb_state;
_usb_state = USB_SUSPENDED;
return 0;
}
#endif
if (istr & USB_ISTR_CTR) {
uint8_t ep = usb_istr_get_ep_id();
if (ep == 0) {
handle_ep0();
return 0;
}
// assert (ep == 1)
if (USB.EPR[ep] & USB_EPRx_CTR_TX) {
// IN: last transmit succeeded.
// assert(usb_ep_get_stat_tx() == USB_EP_STAT_NAK)
// hardware will have toggled DTOG
usb_ep_clr_ctr_tx(ep);
}
if (USB.EPR[ep] & USB_EPRx_CTR_RX) {
// OUT transaction
// assert(usb_ep_get_stat_rx() == USB_EP_STAT_NAK)
usb_ep_clr_ctr_rx(ep);
size_t len = read_buffer(ep, buf, sz);
usb_ep_set_stat_rx(ep, USB_EP_STAT_VALID);
return len;
}
}
return 0;
}
size_t usb_send(const uint8_t* buf, size_t len) {
// still not done with the previous one, or perhaps unconfigured
if (usb_ep_get_stat_tx(1) != USB_EP_STAT_NAK) {
return 0;
}
len = write_buffer(1, buf, len);
usb_ep_set_stat_tx(1, USB_EP_STAT_VALID);
return len;
}
// Setup and standard request handling
static uint8_t _deviceDescriptor[] = {
18, // length of this descriptor
0x01, // DEVICE Descriptor Type
0x00, 0x02, // USB version 2.00
2, // Device Class = CDC
0, 0, // subclass, protocol 0,0
64, // Max Packet Size ep0
0x83, 0x04, // VendorID = 0x0483 (STMicroelectronics)
0x40, 0x57, // ProductID = 0x5740 (Virtual COM Port)
0x00, 0x02, // Device Version 2.0
0, 0, 0, // Manufacturer/Product/SerialNumber strings not set
1, // NumConfigurations
};
static uint8_t _configDescriptor[] = {
// Config 0 header
9, // Length
0x02, // CONFIGURATION Descriptor Type
9 + 9 + 5 + 4 + 5 + 9 + 7 + 7, 0, // TotalLength
2, // NumInterfaces
1, // ConfigurationValue
0, // Configuration string not set
0x80, // Attributes 0x80 for historical reasons
50, // MaxPower 100mA
// interface 0
9, // Length
0x04, // INTERFACE Descriptor Type
0, 0, // Interface Number, Alternate Setting
0, // Num Endpoints
0x02, // InterfaceClass: CDC
0x02, // InterfaceSubClass: ACM
0, // InterfaceProtocol: NONE
0, // Interface string not set
// CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
5, // bFunctionLength
0x24, // bDescriptorType CS_INTERFACE
0x00, // bDescriptorSubtype USB_CDC_TYPE_HEADER
0x10, 0x01, // bcdCDC version 1.10
// Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
4, // bFunctionLength
0x24, // bDescriptorType CS_INTERFACE
0x02, // bDescriptorSubtype USB_CDC_TYPE_ACM
0x00, // bmCapabilities: none
// Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
5, // bFunctionLength
0x24, // bDescriptorType CS_INTERFACE
0x06, // bDescriptorSubtype USB_CDC_TYPE_UNION
0, // bMasterInterface
1, // bSlaveInterface0
// interface 1
9, // Length
0x04, // INTERFACE Descriptor Type
1, 0, // Interface Number, Alternate Setting
2, // Num Endpoints
0x0A, // InterfaceClass: USB_CLASS_DATA
0, // InterfaceSubClass
0, // InterfaceProtocol
0, // Interface string not set
// endpoint 0x1
7, // Length
0x05, // ENDPOINT Descriptor Type
0x01, // Endpoint Address: 1-OUT
0x02, // Attributes: BULK
64, 0, // MaxPacketSize
0, // Interval, ignored for BULK
// endpoint 0x81
7, // Length
0x05, // ENDPOINT Descriptor Type
0x81, // Endpoint Address 1-IN
0x02, // Attributes: BULK
64, 0, // MaxPacketSize
0, // Interval, ignored for BULK
};
enum {
REQ_TYPE_TX = 1 << 7, // bit 7 direction: 1: device->host
// REQ_TYPE_VENDOR = 1<<6, // bits 6..5 : type
// REQ_TYPE_CLASS = 1<<5, // 00 = standard, 11 is reserved
// REQ_TYPE_DEVICE = 0x00,
REQ_TYPE_INTERFACE = 0x01,
REQ_TYPE_ENDPOINT = 0x02,
REQ_TYPE_OTHER = 0x03,
// USB Standard Request Codes - Table 9-4
REQ_GET_STATUS = (0 << 8) | REQ_TYPE_TX, // return 1: self-powered, 2: remote wakeup
REQ_CLR_FEATURE = (1 << 8), // 1 remote wakeup-enable, 2: test mode (high-speed only)
REQ_SET_FEATURE = (3 << 8),
REQ_SET_ADDRESS = (5 << 8), // device only
REQ_GET_DESCRIPTOR = (6 << 8) | REQ_TYPE_TX, // device only
// REQ_SET_DESCRIPTOR = (7<<8), // device only // we don't support
REQ_GET_CONFIGURATION = (8 << 8) | REQ_TYPE_TX, // device only // return state == USB_CONFIGURED ? 1 : 0
REQ_SET_CONFIGURATION = (9 << 8), // device only // 0-> state to ADDRESS, 1 -> state to CONFIGURED (and configure)
REQ_GET_INTERFACE = (10 << 8) | REQ_TYPE_TX | REQ_TYPE_INTERFACE, // interface only
REQ_SET_INTERFACE = (11 << 8) | REQ_TYPE_INTERFACE, // interface only
// REQ_SYNC_FRAME = (12<<8) | REQ_TYPE_ENDPOINT, // endpoint, synch mode only, not supported
REQ_GET_STATUS_INTERFACE = REQ_GET_STATUS | REQ_TYPE_INTERFACE, // return 0x0000
REQ_CLR_FEATURE_INTERFACE = REQ_CLR_FEATURE | REQ_TYPE_INTERFACE, // noop
REQ_SET_FEATURE_INTERFACE = REQ_SET_FEATURE | REQ_TYPE_INTERFACE, // noop
REQ_GET_STATUS_ENDPOINT = REQ_GET_STATUS | REQ_TYPE_ENDPOINT, // return 0x1 if feature 'HALT' is set
REQ_CLR_FEATURE_ENDPOINT = REQ_CLR_FEATURE | REQ_TYPE_ENDPOINT, // clear HALT (wvalue = 0), windex = 0x008f (dir/epnr)
REQ_SET_FEATURE_ENDPOINT = REQ_SET_FEATURE | REQ_TYPE_ENDPOINT, // set HALT
};
// Cf. USB2.0 sections 5.5.5 and 8.5.3 there are 3 possible control flows:
// req.len > 0 && (req.req & REQ_TYPE_TX) rx:SETUP (-> tx:DATA-IN-PART)* -> tx:DATA-IN -> rx:STATUS-OUT
// req.len > 0 && !(req.req & REQ_TYPE_TX) rx:SETUP (-> rx:DATA-OUT-PART)* -> rx:DATA-OUT -> tx:STATUS-IN (zero lenght packet)
// (req.len == 0) rx:SETUP -> tx:STATUS-IN (zero lenght packet)
// Cf. USB2.0 section 9.4, the second one (DATA-OUT/STATUS-IN) we don't need to support because it is only
// required by SET_DESCRIPTOR(device), which we don't support.
// Another simplification follows from the fact that the responses for the first flow are 1, 2 or
// len(descriptor) bytes, which in our case is always shorter than 64 (the pipe packet size),
// so we always can send the response in 1 go.
// Therefore, we have 3 events: rx:SETUP, tx:DATA-IN, rx:STATUS-OUT
// The most recently received SETUP request
struct {
uint16_t req; // lower byte: Type, upper byte request code
uint16_t val;
uint16_t idx;
uint16_t len;
} _ctrl_req = {0, 0, 0, 0};
// false on failure, true on success
static int handle_set_request() {
switch (_ctrl_req.req) {
case REQ_SET_ADDRESS:
// actually setting the address is handled after the ACK has been sent
// here we just check for validity
return (_usb_state != USB_CONFIGURED) && (_ctrl_req.val < 128) && (_ctrl_req.idx == 0);
case REQ_SET_CONFIGURATION:
switch (_usb_state) {
case USB_ADDRESS:
switch (_ctrl_req.val) {
case 1:
// configure our endpoint 0x01/0x81
usb_ep_config(1, USB_EP_TYPE_BULK, 0x01);
usb_ep_set_stat_rx(1, USB_EP_STAT_VALID);
usb_ep_set_stat_tx(1, USB_EP_STAT_NAK);
_usb_state = USB_CONFIGURED;
// fallthrough
case 0:
return 1;
}
return 0;
case USB_CONFIGURED:
switch (_ctrl_req.val) {
case 0:
// unconfigure our endpoints 0x01/0x81
usb_ep_reset(1);
_usb_state = USB_ADDRESS;
// fallthrough
case 1:
return 1;
}
default:
break;
}
return 0;
case REQ_SET_INTERFACE:
return (_usb_state == USB_CONFIGURED) && (_ctrl_req.idx == 0);
// case REQ_CLR_FEATURE:
// case REQ_SET_FEATURE:
// case REQ_CLR_FEATURE_INTERFACE:
// case REQ_SET_FEATURE_INTERFACE:
// return 0; // no features implemented at device or interface level
case REQ_CLR_FEATURE_ENDPOINT:
if ((_usb_state != USB_CONFIGURED) || ((_ctrl_req.idx & 0xf) != 1))
return 0;
if (_ctrl_req.idx & 0x80) {
usb_ep_set_stat_tx(1, USB_EP_STAT_NAK);
usb_ep_clr_dtog_tx(1);
} else {
usb_ep_set_stat_rx(1, USB_EP_STAT_NAK);
usb_ep_clr_dtog_rx(1);
}
return 1;
case REQ_SET_FEATURE_ENDPOINT:
if ((_usb_state != USB_CONFIGURED) || ((_ctrl_req.idx & 0xf) != 1))
return 0;
if (_ctrl_req.idx & 0x80) {
usb_ep_set_stat_tx(1, USB_EP_STAT_STALL);
} else {
usb_ep_set_stat_rx(1, USB_EP_STAT_STALL);
}
return 1;
}
return 0;
}
// false on failure, true on success
static int handle_get_request() {
uint8_t data[2] = {0, 0};
size_t len = 0;
switch (_ctrl_req.req) {
case REQ_GET_DESCRIPTOR:
if (_ctrl_req.idx != 0)
return 0;
switch (_ctrl_req.val) {
case 0x0100:
len = write_buffer(0, _deviceDescriptor, sizeof _deviceDescriptor);
break;
case 0x0200:
len = write_buffer(0, _configDescriptor, sizeof _configDescriptor);
break;
default:
return 0;
}
if (len > _ctrl_req.len)
usb_ep_set_tx_count(0, _ctrl_req.len);
return 1;
case REQ_GET_STATUS:
if ((_usb_state == USB_DEFAULT) || (_ctrl_req.val != 0) || (_ctrl_req.idx != 0) || (_ctrl_req.len != 2))
return 0;
break;
case REQ_GET_STATUS_INTERFACE:
if ((_usb_state == USB_DEFAULT) || (_ctrl_req.val != 0) || (_ctrl_req.idx != 0) || (_ctrl_req.len != 2))
return 0;
break;
case REQ_GET_STATUS_ENDPOINT:
if ((_usb_state == USB_DEFAULT) || (_ctrl_req.val != 0) || (_ctrl_req.len != 2))
return 0;
switch (_ctrl_req.idx) {
case 0x00:
data[0] = (usb_ep_get_stat_rx(0) == USB_EP_STAT_STALL) ? 1 : 0;
break;
case 0x80:
data[0] = (usb_ep_get_stat_tx(0) == USB_EP_STAT_STALL) ? 1 : 0;
break;
case 0x01:
data[0] = (usb_ep_get_stat_rx(1) == USB_EP_STAT_STALL) ? 1 : 0;
break;
case 0x81:
data[0] = (usb_ep_get_stat_tx(1) == USB_EP_STAT_STALL) ? 1 : 0;
break;
default:
return 0;
}
break;
case REQ_GET_CONFIGURATION:
if ((_usb_state == USB_DEFAULT) || (_ctrl_req.len != 1))
return 0;
data[0] = (_usb_state == USB_CONFIGURED) ? 1 : 0;
break;
case REQ_GET_INTERFACE:
if ((_usb_state != USB_CONFIGURED) || (_ctrl_req.len != 1))
return 0;
break;
}
return write_buffer(0, data, _ctrl_req.len) == _ctrl_req.len;
}
static void handle_ep0(void) {
switch (USB.EPR[0] & (USB_EPRx_CTR_RX | USB_EPRx_SETUP | USB_EPRx_CTR_TX)) {
case USB_EPRx_CTR_RX | USB_EPRx_SETUP:
// assert(usb_ep_get_stat_tx() == USB_EP_STAT_NAK)
// assert(usb_ep_get_stat_rx() == USB_EP_STAT_NAK)
if (usb_ep_get_rx_count(0) != 8)
break;
const uint16_t* src = usb_ep_rx_buf(0);
_ctrl_req.req = src[0];
_ctrl_req.val = src[2];
_ctrl_req.idx = src[4];
_ctrl_req.len = src[6];
usb_ep_clr_ctr_rx(0);
// if non-zero length request and direction is OUT
// there's no request we can handle so bail out straightaway
if ((_ctrl_req.len > 0) && !(_ctrl_req.req & REQ_TYPE_TX))
break;
if (_ctrl_req.len == 0) {
if (!handle_set_request())
break;
usb_ep_set_tx_count(0, 0); // ZLP status-in reply
} else {
if (!handle_get_request()) // sets up reply buffer
break;
}
usb_ep_set_stat_tx(0, USB_EP_STAT_VALID);
return;
case USB_EPRx_CTR_TX: // USB IN: sent the reply for the most recent GET or the ACK for the most recent SET
usb_ep_clr_ctr_tx(0);
if (_ctrl_req.len == 0) {
// last request was a SET, so we are here because we sent the ACK
// if the request was set_address, we should execute it here
if (_ctrl_req.req == REQ_SET_ADDRESS) {
usb_daddr_set_add(_ctrl_req.val);
USB.DADDR |= USB_DADDR_EF;
_usb_state = (_ctrl_req.val == 0) ? USB_DEFAULT : USB_ADDRESS;
}
} else {
// last request was a GET, so we are here because we sent the reply
// next thing should be the RX of the host's STATUS_OUT
// the hardware has a special mechanism to deal with this when we set the 'kind' bit
usb_ep_set_stat_rx(0, USB_EP_STAT_VALID);
}
return;
case USB_EPRx_CTR_RX: // RX, USB OUT the final status zero lenght reply from the host to the GET
if (usb_ep_get_rx_count(0) != 0)
break;
usb_ep_clr_ctr_rx(0); // should be done automatically by STATUS_OUT mechanism, but apparently not
return;
}
// if we got here we didn't handle the transfer, abort it
usb_ep_set_stat_rx(0, USB_EP_STAT_STALL);
usb_ep_set_stat_tx(0, USB_EP_STAT_STALL);
return;
}