-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb.c
711 lines (513 loc) · 18.8 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#include <stdio.h>
#include <string.h>
#include "mk20dx128.h"
#include "util.h"
#include "usb.h"
#include "usbserial.h"
__attribute__ ((section(".usbdata.bdt")))
static bdtentry_t bdt[ENDPOINTS_N * 4];
static struct {
__attribute__ ((aligned(4)))
uint8_t setup[8];
__attribute__ ((aligned(4)))
uint8_t notification[2][NOTIFICATION_BUFFER_SIZE];
__attribute__ ((aligned(4)))
uint8_t serial[4][DATA_BUFFER_SIZE];
} buffers;
static volatile struct {
uint8_t *data;
uint16_t length;
} pending, transmit;
static uint8_t oddbits, address;
static volatile uint8_t configuration;
static uint8_t line_state, line_coding[7];
static uint16_t break_duration;
static enum {
NO_CALLBACK,
LINE_STATE_CALLBACK,
LINE_CODING_CALLBACK,
SEND_BREAK_CALLBACK,
} callback;
static enum {
PHASE_SETUP,
PHASE_DATA_IN,
PHASE_DATA_OUT,
PHASE_STATUS_IN,
PHASE_STATUS_OUT,
PHASE_COMPLETE,
} phase;
static usb_data_in_callback data_in_callback;
static usb_line_state_callback line_state_callback;
static usb_line_coding_callback line_coding_callback;
static usb_send_break_callback send_break_callback;
static inline int process_setup_packet(uint16_t request, uint16_t value,
uint16_t index, uint16_t length)
{
switch (request) {
case SETUP_DEVICE_GET_DESCRIPTOR:
/* GET_DESCRIPTOR request. */
switch (DESCRIPTOR_TYPE(value)) {
case DESCRIPTOR_TYPE_DEVICE:
/* GET_DESCRIPTOR for DEVICE. */
pending.data = device_descriptor;
pending.length = 18;
return PHASE_DATA_IN;
case DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
/* GET_DESCRIPTOR for DEVICE QUALIFIER. */
USB0_ENDPT(CONTROL_ENDPOINT) |= USB_ENDPT_EPSTALL;
return PHASE_COMPLETE;
case DESCRIPTOR_TYPE_DEVICE_CONFIGURATION:
/* GET_DESCRIPTOR for DEVICE CONFIGURATION. */
pending.data = configuration_descriptor;
pending.length = sizeof(configuration_descriptor);
return PHASE_DATA_IN;
}
/* We've received a request for an unimplemented device
* descriptor, so we just stall. */
USB0_ENDPT(CONTROL_ENDPOINT) |= USB_ENDPT_EPSTALL;
return PHASE_COMPLETE;
case SETUP_DEVICE_GET_CONFIGURATION:
/* GET_CONFIGURATION request. */
pending.data = (uint8_t *)&configuration;
pending.length = sizeof(configuration);
return PHASE_DATA_IN;
case SETUP_DEVICE_SET_CONFIGURATION:
/* SET_CONFIGURATION request. */
pending.data = NULL;
pending.length = 0;
if (value != 1) {
USB0_ENDPT(CONTROL_ENDPOINT) |= USB_ENDPT_EPSTALL;
return PHASE_COMPLETE;
} else {
int i;
/* Initialize endpoint 1. */
USB0_ENDPT(NOTIFICATION_ENDPOINT) = (USB_ENDPT_EPTXEN |
USB_ENDPT_EPHSHK);
for (i = 0 ; i < 2 ; i += 1) {
bdtentry_t *t;
t = bdt_entry(NOTIFICATION_ENDPOINT, 1, i);
t->buffer = buffers.notification[i];
t->desc = bdt_descriptor (0, 0);
}
reset_oddbit(NOTIFICATION_ENDPOINT, 0);
reset_oddbit(NOTIFICATION_ENDPOINT, 1);
/* Initialize endpoint 2. */
USB0_ENDPT(DATA_ENDPOINT) = (USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN |
USB_ENDPT_EPHSHK);
/* Initialize the buffer pointers. */
reset_oddbit(DATA_ENDPOINT, 0);
reset_oddbit(DATA_ENDPOINT, 1);
for (i = 0 ; i < 2 ; i += 1) {
bdtentry_t *r, *t;
r = bdt_entry(DATA_ENDPOINT, 0, i);
t = bdt_entry(DATA_ENDPOINT, 1, i);
r->buffer = buffers.serial[i];
t->buffer = buffers.serial[2 + i];
/* Prime the first input BDT entry. */
r->desc = bdt_descriptor (DATA_BUFFER_SIZE, i);
if (i == 0) {
r->desc |= BDT_DESC_OWN;
toggle_oddbit (DATA_ENDPOINT, 0);
}
t->desc = bdt_descriptor (0, 0);
}
transmit.data = NULL;
transmit.length = DATA_BUFFER_SIZE;
configuration = (uint8_t)value;
}
return PHASE_DATA_OUT;
case SETUP_DEVICE_SET_ADDRESS:
/* SET_ADDRESS request. */
pending.data = NULL;
pending.length = 0;
address = (uint8_t)value;
return PHASE_DATA_OUT;
case SETUP_SET_CONTROL_LINE_STATE:
/* SET_CONTROL_LINE_STATE request. */
pending.data = NULL;
pending.length = 0;
line_state = (uint8_t)value;
callback = LINE_STATE_CALLBACK;
return PHASE_DATA_OUT;
case SETUP_SET_LINE_CODING:
/* SET_LINE_CODING request. */
pending.data = (uint8_t *)line_coding;
pending.length = (length < sizeof(line_coding) ?
length : sizeof(line_coding));
callback = LINE_CODING_CALLBACK;
return PHASE_DATA_OUT;
case SETUP_SEND_BREAK:
/* SEND_BREAK request. */
break_duration = value;
callback = SEND_BREAK_CALLBACK;
return PHASE_DATA_OUT;
}
/* We've received an unimplemented request, so we
* just stall. */
USB0_ENDPT(CONTROL_ENDPOINT) |= USB_ENDPT_EPSTALL;
return PHASE_COMPLETE;
}
static void handle_data_transfer (uint8_t status)
{
bdtentry_t *entry;
int pid;
entry = &(bdt[status >> 2]);
pid = BDT_DESC_PID(entry->desc);
if (pid == BDT_PID_OUT) {
bdtentry_t *in;
assert (bdt_entry(DATA_ENDPOINT, 0,
!oddbit(DATA_ENDPOINT, 0)) == entry);
if (data_in_callback) {
data_in_callback(entry->buffer, BDT_DESC_BYTE_COUNT(entry->desc));
}
in = bdt_entry(DATA_ENDPOINT, 0, oddbit(DATA_ENDPOINT, 0));
assert(!(in->desc & BDT_DESC_OWN));
in->desc = bdt_descriptor (DATA_BUFFER_SIZE, oddbit(DATA_ENDPOINT, 0));
in->desc |= BDT_DESC_OWN;
toggle_oddbit(DATA_ENDPOINT, 0);
}
}
static void handle_control_transfer (uint8_t status)
{
bdtentry_t *entry;
uint16_t *buffer, length, value, request, index;
int n, pid;
/* Some notes:
* Double-buffering of BDT entries works on the level of
*endpoints* (tx and rx channels are considered separate
endpoints).
* Each time we receieve a TOKDNE interrupt a token has already
been processed. In order for that to happen we need to
prepare and release a BDT entry, in the correct (odd/even)
bank and with the correct data toggle bit (DATA0/1), for the
next token during during each service routine call.
* Each control transaction consists of up to 3 stages: SETUP, DATA
and STATUS.
* Each stage has a data packet.
* The SETUP data packet is always DATA0 and the request determines
if the DATA stage exists and what direction it has.
* The DATA stage data packets alternate between DATA0 and DATA1
and must transfer the amount of data specified in the SETUP
phase.
* The STATUS phase data packet is always of the opposite direction
with respect to the preceding stage and always DATA1. */
entry = &(bdt[status >> 2]);
pid = BDT_DESC_PID(entry->desc);
if (phase == PHASE_SETUP && pid == BDT_PID_SETUP) {
/* Read the payload and release the buffer. */
buffer = (uint16_t *)entry->buffer;
request = buffer[0];
value = buffer[1];
index = buffer[2];
length = buffer[3];
phase = process_setup_packet(request, value, index, length);
/* Make sure we don't return more than the host asked
* for. */
if (phase == PHASE_DATA_IN && length < pending.length) {
pending.length = length;
}
}
if (phase == PHASE_DATA_IN &&
(pid == BDT_PID_IN || pid == BDT_PID_SETUP)) {
/* Determine whether there's anything else to send
* and prepare the buffer, otherwise transition to
* the status phase. */
n = pending.length < CONTROL_BUFFER_SIZE ?
pending.length : CONTROL_BUFFER_SIZE;
if (n > 0) {
bdtentry_t *out;
int data1;
data1 = !(entry->desc & BDT_DESC_DATA1);
out = bdt_entry(CONTROL_ENDPOINT, 1,
oddbit(CONTROL_ENDPOINT, 1));
assert (!(out->desc & BDT_DESC_OWN));
out->buffer = (void *)pending.data;
out->desc = bdt_descriptor (n, data1);
out->desc |= BDT_DESC_OWN;
pending.data += n;
pending.length -= n;
toggle_oddbit (CONTROL_ENDPOINT, 1);
} else {
bdtentry_t *in;
/* Prepare to receive a zero-length DATA1 packet to
* aknowledge proper reception during the preceding
* IN phase. */
in = bdt_entry(CONTROL_ENDPOINT, 0,
oddbit(CONTROL_ENDPOINT, 0));
assert (!(in->desc & BDT_DESC_OWN));
in->buffer = NULL;
in->desc = bdt_descriptor (0, 1);
in->desc |= BDT_DESC_OWN;
toggle_oddbit (CONTROL_ENDPOINT, 0);
phase = PHASE_STATUS_IN;
}
}
if (phase == PHASE_DATA_OUT &&
(pid == BDT_PID_OUT || pid == BDT_PID_SETUP)) {
/* Determine whether there's anything else to receive and
* prepare the buffer, otherwise transition to the status
* phase. */
n = pending.length < CONTROL_BUFFER_SIZE ?
pending.length : CONTROL_BUFFER_SIZE;
if (n > 0) {
bdtentry_t *in;
int data1;
data1 = !(entry->desc & BDT_DESC_DATA1);
in = bdt_entry(CONTROL_ENDPOINT, 0, oddbit(CONTROL_ENDPOINT, 0));
assert (!(in->desc & BDT_DESC_OWN));
in->buffer = (void *)pending.data;
in->desc = bdt_descriptor (n, data1);
in->desc |= BDT_DESC_OWN;
pending.data += n;
pending.length -= n;
toggle_oddbit (CONTROL_ENDPOINT, 0);
} else {
bdtentry_t *out;
/* Prepare to send a zero-length DATA1 packet to
* aknowledge proper reception during the preceding
* OUT phase. */
out = bdt_entry(CONTROL_ENDPOINT, 1, oddbit(CONTROL_ENDPOINT, 1));
assert (!(out->desc & BDT_DESC_OWN));
out->buffer = NULL;
out->desc = bdt_descriptor (0, 1);
out->desc |= BDT_DESC_OWN;
toggle_oddbit (CONTROL_ENDPOINT, 1);
phase = PHASE_STATUS_OUT;
}
}
if (pid == BDT_PID_SETUP) {
USB0_CTL &= ~USB_CTL_TXSUSPENDTOKENBUSY;
}
if ((phase == PHASE_STATUS_IN && pid == BDT_PID_OUT) ||
(phase == PHASE_STATUS_OUT && pid == BDT_PID_IN)) {
/* Set the address. */
if (USB0_ADDR != address) {
assert(phase == PHASE_STATUS_OUT && pid == BDT_PID_IN);
USB0_ADDR = address;
}
phase = PHASE_COMPLETE;
}
if (phase == PHASE_COMPLETE) {
bdtentry_t *in;
/* Prepare an input buffer for the next setup token. */
in = bdt_entry(CONTROL_ENDPOINT, 0, oddbit(CONTROL_ENDPOINT, 0));
assert (!(in->desc & BDT_DESC_OWN));
in->buffer = (void *)buffers.setup;
in->desc = bdt_descriptor (8, 0);
in->desc |= BDT_DESC_OWN;
toggle_oddbit (CONTROL_ENDPOINT, 0);
phase = PHASE_SETUP;
switch (callback) {
case NO_CALLBACK: break;
case LINE_STATE_CALLBACK:
if(line_state_callback) {
line_state_callback(line_state);
}
break;
case LINE_CODING_CALLBACK:
if(line_coding_callback) {
line_coding_callback(line_coding);
}
break;
case SEND_BREAK_CALLBACK:
if(send_break_callback) {
send_break_callback(break_duration);
}
}
}
}
__attribute__((interrupt ("IRQ"))) void usb_isr()
{
if (USB0_ISTAT & USB_ISTAT_USBRST) {
int i;
/* Reset the even/odd BDT toggle bits. */
USB0_CTL |= USB_CTL_ODDRST;
phase = PHASE_SETUP;
address = 0;
pending.data = NULL;
pending.length = 0;
reset_oddbit(CONTROL_ENDPOINT, 0);
reset_oddbit(CONTROL_ENDPOINT, 1);
/* Initialize the BDT entries for endpoint 0. */
for (i = 0 ; i < 2 ; i += 1) {
bdtentry_t *r, *t;
r = bdt_entry(CONTROL_ENDPOINT, 0, i);
t = bdt_entry(CONTROL_ENDPOINT, 1, i);
if (i == 0) {
/* Prepare a buffer for the first setup packet (setup
* packets are always DATA0). */
r->buffer = buffers.setup;
r->desc = bdt_descriptor (8, 0);
r->desc |= BDT_DESC_OWN;
toggle_oddbit(CONTROL_ENDPOINT, 0);
} else {
r->buffer = NULL;
r->desc = bdt_descriptor (0, 0);
}
t->buffer = NULL;
t->desc = bdt_descriptor (0, 0);
}
/* Activate endpoint 0, reset all error flags and reset
* address to 0. */
USB0_ENDPT(CONTROL_ENDPOINT) = (USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN |
USB_ENDPT_EPHSHK);
USB0_ERRSTAT = ~0;
USB0_ADDR = 0;
/* Enable interesting interrupts. */
USB0_ERREN = ~0;
USB0_INTEN = (USB_INTEN_USBRSTEN |
USB_INTEN_ERROREN |
USB_INTEN_TOKDNEEN |
USB_INTEN_SLEEPEN |
USB_INTEN_STALLEN);
USB0_CTL &= ~(USB_CTL_ODDRST | USB_CTL_TXSUSPENDTOKENBUSY);
USB0_ISTAT |= USB_ISTAT_USBRST;
} else if (USB0_ISTAT & USB_ISTAT_ERROR) {
assert(0);
USB0_ERRSTAT = ~0;
USB0_ISTAT |= USB_ISTAT_ERROR;
} else if (USB0_ISTAT & USB_ISTAT_STALL) {
int i;
for (i = 0 ; i < ENDPOINTS_N ; i += 1) {
USB0_ENDPT(i) &= ~USB_ENDPT_EPSTALL;
}
USB0_ISTAT |= USB_ISTAT_STALL;
} else if (USB0_ISTAT & USB_ISTAT_SLEEP) {
USB0_ISTAT |= USB_ISTAT_SLEEP;
} else while (USB0_ISTAT & USB_ISTAT_TOKDNE) {
uint8_t status;
int n;
/* A new token has been received. */
status = USB0_STAT;
n = USB_STAT_ENDP(status);
switch (n) {
case CONTROL_ENDPOINT: handle_control_transfer(status); break;
case DATA_ENDPOINT: handle_data_transfer(status); break;
case NOTIFICATION_ENDPOINT: break;
default:
USB0_ENDPT(n) |= USB_ENDPT_EPSTALL;
}
USB0_ISTAT |= USB_ISTAT_TOKDNE;
}
}
void usb_initialize()
{
/* Enable the USB interrupt but give it a low priority. */
enable_interrupt(35);
prioritize_interrupt(35, 4);
/* Set the USB clock source to MGCPLL with a divider for 48Mhz and
* enable the USB clock. */
SIM_CLKDIV2 |= SIM_CLKDIV2_USBDIV(1);
SIM_SOPT2 |= SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL;
SIM_SCGC4 |= SIM_SCGC4_USBOTG;
/* Reset the USB module. */
USB0_USBTRC0 |= USB_USBTRC0_USBRESET;
while (USB0_USBTRC0 & USB_USBTRC0_USBRESET);
/* Set the BDT base address which is aligned on a 512-byte
* boundary and hence its first 9 bits are identically zero. */
USB0_BDTPAGE1 = (uint8_t)((((uint32_t)bdt) >> 8) & 0xfe);
USB0_BDTPAGE2 = (uint8_t)(((uint32_t)bdt) >> 16);
USB0_BDTPAGE3 = (uint8_t)(((uint32_t)bdt) >> 24);
/* Clear all USB ISR flags and enable weak pull-downs. */
USB0_ISTAT = ~0;
USB0_ERRSTAT = ~0;
USB0_OTGISTAT = ~0;
USB0_USBCTRL = USB_USBCTRL_PDE;
/* Enable the USB module. */
USB0_CTL = USB_CTL_USBENSOFEN;
/* Enable USB reset interrupt. */
USB0_INTEN = USB_INTEN_USBRSTEN;
/* Enable d+ pull-up. */
USB0_CONTROL |= USB_CONTROL_DPPULLUPNONOTG;
/* { */
/* usbserial_await_rts(); */
/* usbserial_trace("%x %x %x %x %x %x %x\n", */
/* line_coding[0], line_coding[1], line_coding[2], */
/* line_coding[3], line_coding[4], line_coding[5], */
/* line_coding[6]); */
/* } */
}
int usb_enumerated()
{
return configuration;
}
void usb_await_enumeration()
{
sleep_while(configuration == 0);
}
int usb_interrupt(uint8_t *buffer, int n)
{
bdtentry_t *out;
out = bdt_entry(NOTIFICATION_ENDPOINT, 1,
oddbit(NOTIFICATION_ENDPOINT, 1));
sleep_while (out->desc & BDT_DESC_OWN);
memcpy(out->buffer, buffer, n);
out->desc = bdt_descriptor (n, oddbit(NOTIFICATION_ENDPOINT, 1));
out->desc |= BDT_DESC_OWN;
toggle_oddbit (NOTIFICATION_ENDPOINT, 1);
return 0;
}
int usb_write(const char *s, int n, int flush)
{
volatile bdtentry_t *out;
int m;
if (configuration == 0) {
return -1;
}
/* If the current buffer has been filled and flushed to the USB
* module fetch a new one. */
if (transmit.length == DATA_BUFFER_SIZE) {
out = bdt_entry(DATA_ENDPOINT, 1, oddbit (DATA_ENDPOINT, 1));
transmit.data = out->buffer;
transmit.length = 0;
sleep_while (out->desc & BDT_DESC_OWN);
assert(transmit.length != DATA_BUFFER_SIZE);
}
/* Break the write up if it's too large to fit in the current
* buffer. */
if (n > 0) {
m = DATA_BUFFER_SIZE - transmit.length < n ?
DATA_BUFFER_SIZE - transmit.length : n;
} else {
m = 0;
}
assert(m > 0 || n == 0);
if (m > 0) {
memcpy(transmit.data, s, m);
transmit.data += m;
transmit.length += m;
}
/* Send the current buffer to the USB module. */
if (flush || transmit.length == DATA_BUFFER_SIZE) {
out = bdt_entry(DATA_ENDPOINT, 1, oddbit (DATA_ENDPOINT, 1));
assert(!(out->desc & BDT_DESC_OWN));
out->desc = bdt_descriptor (transmit.length,
oddbit (DATA_ENDPOINT, 1));
out->desc |= BDT_DESC_OWN;
toggle_oddbit (DATA_ENDPOINT, 1);
}
if (flush) {
transmit.length = DATA_BUFFER_SIZE;
}
if (n > m) {
return usb_write(s + m, n - m, flush);
} else {
return 0;
}
}
void usb_set_data_in_callback(usb_data_in_callback new_callback)
{
data_in_callback = new_callback;
}
void usb_set_line_state_callback(usb_line_state_callback new_callback)
{
line_state_callback = new_callback;
}
void usb_set_line_coding_callback(usb_line_coding_callback new_callback)
{
line_coding_callback = new_callback;
}
void usb_set_send_break_callback(usb_send_break_callback new_callback)
{
send_break_callback = new_callback;
}