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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
|
/*
* This file is part of the libusbhost library
* hosted at http://github.com/libusbhost/libusbhost
*
* Copyright (C) 2015 Amir Hammad <amir.hammad@hotmail.com>
*
*
* libusbhost is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "usbh_config.h"
#include "usbh_lld_stm32f4.h"
#include "driver/usbh_device_driver.h"
#include "usart_helpers.h"
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/usb/usbstd.h>
#include <stddef.h>
static struct {
bool enumeration_run;
const usbh_low_level_driver_t * const *lld_drivers;
const usbh_dev_driver_t * const *dev_drivers;
int8_t address_temporary;
} usbh_data = {};
static void set_enumeration(void)
{
usbh_data.enumeration_run = true;
}
static void reset_enumeration(void)
{
usbh_data.enumeration_run = false;
}
static bool enumeration(void)
{
return usbh_data.enumeration_run;
}
void device_remove(usbh_device_t *dev)
{
if (dev->drv && dev->drvdata) {
dev->drv->remove(dev->drvdata);
}
dev->address = -1;
dev->drv = NULL;
dev->drvdata = NULL;
}
/**
*
*/
static bool find_driver(usbh_device_t *dev, const usbh_dev_driver_info_t * device_info)
{
#define CHECK_PARTIAL_COMPATIBILITY(what) \
if (usbh_data.dev_drivers[i]->info->what != -1\
&& device_info->what != usbh_data.dev_drivers[i]->info->what) {\
i++;\
continue;\
}
int i = 0;
while (usbh_data.dev_drivers[i]) {
CHECK_PARTIAL_COMPATIBILITY(ifaceClass);
CHECK_PARTIAL_COMPATIBILITY(ifaceSubClass);
CHECK_PARTIAL_COMPATIBILITY(ifaceProtocol);
CHECK_PARTIAL_COMPATIBILITY(deviceClass);
CHECK_PARTIAL_COMPATIBILITY(deviceSubClass);
CHECK_PARTIAL_COMPATIBILITY(deviceProtocol);
CHECK_PARTIAL_COMPATIBILITY(idVendor);
CHECK_PARTIAL_COMPATIBILITY(idProduct);
dev->drv = usbh_data.dev_drivers[i];
dev->drvdata = dev->drv->init(dev);
if (!dev->drvdata) {
LOG_PRINTF("Unable to initialize device driver at index %d\n", i);
i++;
continue;
}
return true;
}
return false;
#undef CHECK_PARTIAL_COMPATIBILITY
}
static void device_register(void *descriptors, uint16_t descriptors_len, usbh_device_t *dev)
{
uint32_t i = 0;
uint8_t *buf = (uint8_t *)descriptors;
dev->drv = NULL;
dev->drvdata = NULL;
uint8_t desc_len = buf[i];
uint8_t desc_type = buf[i + 1];
usbh_dev_driver_info_t device_info;
if (desc_type == USB_DT_DEVICE) {
struct usb_device_descriptor *device_desc = (void*)&buf[i];
LOG_PRINTF("DEVICE DESCRIPTOR\n");
device_info.deviceClass = device_desc->bDeviceClass;
device_info.deviceSubClass = device_desc->bDeviceSubClass;
device_info.deviceProtocol = device_desc->bDeviceProtocol;
device_info.idVendor = device_desc->idVendor;
device_info.idProduct = device_desc->idProduct;
} else {
LOG_PRINTF("INVALID descriptors pointer - fatal error");
return;
}
while (i < descriptors_len) {
desc_len = buf[i];
desc_type = buf[i + 1];
switch (desc_type) {
case USB_DT_INTERFACE:
{
LOG_PRINTF("INTERFACE_DESCRIPTOR\n");
struct usb_interface_descriptor *iface = (void*)&buf[i];
device_info.ifaceClass = iface->bInterfaceClass;
device_info.ifaceSubClass = iface->bInterfaceSubClass;
device_info.ifaceProtocol = iface->bInterfaceProtocol;
if (find_driver(dev, &device_info)) {
int k = 0;
while (k < descriptors_len) {
desc_len = buf[k];
void *drvdata = dev->drvdata;
LOG_PRINTF("[%d]", buf[k+1]);
if (dev->drv->analyze_descriptor(drvdata, &buf[k])) {
LOG_PRINTF("Device Initialized\n");
return;
}
if (desc_len == 0) {
LOG_PRINTF("Problem occured while parsing complete configuration descriptor");
return;
}
k += desc_len;
}
LOG_PRINTF("Device driver isn't compatible with this device\n");
device_remove(dev);
} else {
LOG_PRINTF("No compatible driver has been found for interface #%d\n", iface->bInterfaceNumber);
}
}
break;
default:
break;
}
if (desc_len == 0) {
LOG_PRINTF("PROBLEM WITH PARSE %d\n",i);
return;
}
i += desc_len;
}
LOG_PRINTF("Device NOT Initialized\n");
}
void usbh_init(const usbh_low_level_driver_t * const low_level_drivers[], const usbh_dev_driver_t * const device_drivers[])
{
if (!low_level_drivers) {
return;
}
usbh_data.lld_drivers = (const usbh_low_level_driver_t **)low_level_drivers;
usbh_data.dev_drivers = device_drivers;
uint32_t k = 0;
while (usbh_data.lld_drivers[k]) {
LOG_PRINTF("Initialization low-level driver with index=%d\n", k);
usbh_device_t * usbh_device =
((usbh_generic_data_t *)(usbh_data.lld_drivers[k])->driver_data)->usbh_device;
uint32_t i;
for (i = 0; i < USBH_MAX_DEVICES; i++) {
//~ LOG_PRINTF("%p ", &usbh_device[i]);
usbh_device[i].address = -1;
usbh_device[i].drv = 0;
usbh_device[i].drvdata = 0;
}
usbh_data.lld_drivers[k]->init(usbh_data.lld_drivers[k]->driver_data);
k++;
}
}
static void device_xfer_control_write_setup(const void *data, uint16_t datalen, usbh_packet_callback_t callback, usbh_device_t *dev)
{
usbh_packet_t packet;
packet.data.out = data;
packet.datalen = datalen;
packet.address = dev->address;
packet.endpoint_address = 0;
packet.endpoint_size_max = dev->packet_size_max0;
packet.endpoint_type = USBH_ENDPOINT_TYPE_CONTROL;
packet.control_type = USBH_CONTROL_TYPE_SETUP;
packet.speed = dev->speed;
packet.callback = callback;
packet.callback_arg = dev;
packet.toggle = &dev->toggle0;
usbh_write(dev, &packet);
LOG_PRINTF("WR-setup@device...%d \n", dev->address);
}
static void device_xfer_control_write_data(const void *data, uint16_t datalen, usbh_packet_callback_t callback, usbh_device_t *dev)
{
usbh_packet_t packet;
packet.data.out = data;
packet.datalen = datalen;
packet.address = dev->address;
packet.endpoint_address = 0;
packet.endpoint_size_max = dev->packet_size_max0;
packet.endpoint_type = USBH_ENDPOINT_TYPE_CONTROL;
packet.control_type = USBH_CONTROL_TYPE_DATA;
packet.speed = dev->speed;
packet.callback = callback;
packet.callback_arg = dev;
packet.toggle = &dev->toggle0;
usbh_write(dev, &packet);
LOG_PRINTF("WR-data@device...%d \n", dev->address);
}
static void device_xfer_control_read(void *data, uint16_t datalen, usbh_packet_callback_t callback, usbh_device_t *dev)
{
usbh_packet_t packet;
packet.data.in = data;
packet.datalen = datalen;
packet.address = dev->address;
packet.endpoint_address = 0;
packet.endpoint_size_max = dev->packet_size_max0;
packet.endpoint_type = USBH_ENDPOINT_TYPE_CONTROL;
packet.speed = dev->speed;
packet.callback = callback;
packet.callback_arg = dev;
packet.toggle = &dev->toggle0;
usbh_read(dev, &packet);
LOG_PRINTF("RD@device...%d | \n", dev->address);
}
static void control_state_machine(usbh_device_t *dev, usbh_packet_callback_data_t cb_data)
{
switch (dev->control.state) {
case USBH_CONTROL_STATE_SETUP:
if (cb_data.status != USBH_PACKET_CALLBACK_STATUS_OK) {
dev->control.state = USBH_CONTROL_STATE_NONE;
// Unable to deliver setup control packet - this is a fatal error
usbh_packet_callback_data_t ret_data;
ret_data.status = USBH_PACKET_CALLBACK_STATUS_EFATAL;
ret_data.transferred_length = 0;
dev->control.callback(dev, ret_data);
break;
}
if (dev->control.setup_data.bmRequestType & USB_REQ_TYPE_IN) {
dev->control.state = USBH_CONTROL_STATE_DATA;
device_xfer_control_read(dev->control.data.in, dev->control.data_length, control_state_machine, dev);
} else {
if (dev->control.data_length == 0) {
dev->control.state = USBH_CONTROL_STATE_STATUS;
device_xfer_control_read(NULL, 0, control_state_machine, dev);
} else {
dev->control.state = USBH_CONTROL_STATE_DATA;
device_xfer_control_write_data(dev->control.data.out, dev->control.data_length, control_state_machine, dev);
}
}
break;
case USBH_CONTROL_STATE_DATA:
if (dev->control.setup_data.bmRequestType & USB_REQ_TYPE_IN) {
dev->control.state = USBH_CONTROL_STATE_NONE;
dev->control.callback(dev, cb_data);
} else {
if (cb_data.status != USBH_PACKET_CALLBACK_STATUS_OK) {
dev->control.state = USBH_CONTROL_STATE_NONE;
// Unable to deliver data control packet - this is a fatal error
usbh_packet_callback_data_t ret_data;
ret_data.status = USBH_PACKET_CALLBACK_STATUS_EFATAL;
ret_data.transferred_length = 0;
dev->control.callback(dev, ret_data);
break;
}
if (dev->control.data_length == 0) {
// we should be in status state when the length of data is zero
LOG_PRINTF("Control logic error\n");
dev->control.state = USBH_CONTROL_STATE_NONE;
dev->control.callback(dev, cb_data);
} else {
dev->control.state = USBH_CONTROL_STATE_STATUS;
device_xfer_control_read(NULL, 0, control_state_machine, dev);
}
}
break;
case USBH_CONTROL_STATE_STATUS:
dev->control.state = USBH_CONTROL_STATE_NONE;
dev->control.callback(dev, cb_data);
break;
default:
break;
}
}
void device_control(usbh_device_t *dev, usbh_packet_callback_t callback, const struct usb_setup_data *setup_data, void *data)
{
if (dev->control.state != USBH_CONTROL_STATE_NONE) {
LOG_PRINTF("ERROR: Use of control state machine while not idle\n");
return;
}
dev->control.state = USBH_CONTROL_STATE_SETUP;
dev->control.callback = callback;
dev->control.data.out = data;
dev->control.data_length = setup_data->wLength;
dev->control.setup_data = *setup_data;
device_xfer_control_write_setup(&dev->control.setup_data, sizeof(dev->control.setup_data), control_state_machine, dev);
}
bool usbh_enum_available(void)
{
return !enumeration();
}
/**
* Returns 0 on error
* device otherwise
*/
usbh_device_t *usbh_get_free_device(const usbh_device_t *dev)
{
const usbh_low_level_driver_t *lld = dev->lld;
usbh_generic_data_t *lld_data = lld->driver_data;
usbh_device_t *usbh_device = lld_data->usbh_device;
uint8_t i;
LOG_PRINTF("DEV ADDRESS%d\n", dev->address);
for (i = 0; i < USBH_MAX_DEVICES; i++) {
if (usbh_device[i].address < 0) {
LOG_PRINTF("\t\t\t\t\tFOUND: %d", i);
usbh_device[i].address = i+1;
return &usbh_device[i];
} else {
LOG_PRINTF("address: %d\n\n\n", usbh_device[i].address);
}
}
return 0;
}
static void device_enumeration_finish(usbh_device_t *dev)
{
reset_enumeration();
dev->state = USBH_ENUM_STATE_FIRST;
}
static void device_enumeration_terminate(usbh_device_t *dev)
{
dev->address = -1;
device_enumeration_finish(dev);
}
#define CONTINUE_WITH(en) \
dev->state = en;\
device_enumerate(dev, cb_data);
static void device_enumerate(usbh_device_t *dev, usbh_packet_callback_data_t cb_data)
{
const usbh_low_level_driver_t *lld = dev->lld;
usbh_generic_data_t *lld_data = lld->driver_data;
uint8_t *usbh_buffer = lld_data->usbh_buffer;
// LOG_PRINTF("\nSTATE: %d\n", state);
switch (dev->state) {
case USBH_ENUM_STATE_SET_ADDRESS:
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
if (dev->address == 0) {
dev->address = usbh_data.address_temporary;
LOG_PRINTF("Assigned address: %d\n", dev->address);
}
CONTINUE_WITH(USBH_ENUM_STATE_DEVICE_DT_READ_SETUP);
break;
default:
device_enumeration_terminate(dev);
ERROR(cb_data.status);
break;
}
break;
case USBH_ENUM_STATE_DEVICE_DT_READ_SETUP:
{
struct usb_setup_data setup_data;
setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_DEVICE;
setup_data.bRequest = USB_REQ_GET_DESCRIPTOR;
setup_data.wValue = USB_DT_DEVICE << 8;
setup_data.wIndex = 0;
setup_data.wLength = USB_DT_DEVICE_SIZE;
dev->state = USBH_ENUM_STATE_DEVICE_DT_READ_COMPLETE;
device_control(dev, device_enumerate, &setup_data, &usbh_buffer[0]);
}
break;
case USBH_ENUM_STATE_DEVICE_DT_READ_COMPLETE:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
struct usb_device_descriptor *ddt =
(struct usb_device_descriptor *)&usbh_buffer[0];
dev->packet_size_max0 = ddt->bMaxPacketSize0;
LOG_PRINTF("Found device with vid=0x%04x pid=0x%04x\n", ddt->idVendor, ddt->idProduct);
LOG_PRINTF("class=0x%02x subclass=0x%02x protocol=0x%02x\n", ddt->bDeviceClass, ddt->bDeviceSubClass, ddt->bDeviceProtocol);
CONTINUE_WITH(USBH_ENUM_STATE_CONFIGURATION_DT_HEADER_READ_SETUP)
}
break;
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
if (cb_data.transferred_length >= 8) {
struct usb_device_descriptor *ddt =
(struct usb_device_descriptor *)&usbh_buffer[0];
dev->packet_size_max0 = ddt->bMaxPacketSize0;
CONTINUE_WITH(USBH_ENUM_STATE_DEVICE_DT_READ_SETUP);
} else {
device_enumeration_terminate(dev);
ERROR(cb_data.status);
}
break;
default:
device_enumeration_terminate(dev);
ERROR(cb_data.status);
break;
}
}
break;
case USBH_ENUM_STATE_CONFIGURATION_DT_HEADER_READ_SETUP:
{
struct usb_setup_data setup_data;
setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_DEVICE;
setup_data.bRequest = USB_REQ_GET_DESCRIPTOR;
setup_data.wValue = USB_DT_CONFIGURATION << 8;
setup_data.wIndex = 0;
setup_data.wLength = dev->packet_size_max0;
dev->state = USBH_ENUM_STATE_CONFIGURATION_DT_HEADER_READ;
device_xfer_control_write_setup(&setup_data, sizeof(setup_data),
device_enumerate, dev);
}
break;
case USBH_ENUM_STATE_CONFIGURATION_DT_HEADER_READ:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
dev->state = USBH_ENUM_STATE_CONFIGURATION_DT_HEADER_READ_COMPLETE;
device_xfer_control_read(&usbh_buffer[USB_DT_DEVICE_SIZE],
dev->packet_size_max0, device_enumerate, dev);
break;
default:
device_enumeration_terminate(dev);
ERROR(cb_data.status);
break;
}
}
break;
case USBH_ENUM_STATE_CONFIGURATION_DT_HEADER_READ_COMPLETE:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
CONTINUE_WITH(USBH_ENUM_STATE_CONFIGURATION_DT_READ_SETUP);
break;
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
if (cb_data.transferred_length >= USB_DT_CONFIGURATION_SIZE) {
struct usb_config_descriptor *cdt =
(struct usb_config_descriptor *)&usbh_buffer[USB_DT_DEVICE_SIZE];
if (cb_data.transferred_length == cdt->wTotalLength) {
LOG_PRINTF("Configuration descriptor read complete. length: %d\n", cdt->wTotalLength);
CONTINUE_WITH(USBH_ENUM_STATE_SET_CONFIGURATION_SETUP);
}
}
break;
default:
device_enumeration_terminate(dev);
ERROR(cb_data.status);
break;
}
}
break;
case USBH_ENUM_STATE_CONFIGURATION_DT_READ_SETUP:
{
struct usb_config_descriptor *cdt =
(struct usb_config_descriptor *)&usbh_buffer[USB_DT_DEVICE_SIZE];
struct usb_setup_data setup_data;
LOG_PRINTF("Getting complete configuration descriptor of length: %d bytes\n", cdt->wTotalLength);
setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_DEVICE;
setup_data.bRequest = USB_REQ_GET_DESCRIPTOR;
setup_data.wValue = USB_DT_CONFIGURATION << 8;
setup_data.wIndex = 0;
setup_data.wLength = cdt->wTotalLength;
dev->state = USBH_ENUM_STATE_CONFIGURATION_DT_READ;
device_xfer_control_write_setup(&setup_data, sizeof(setup_data),
device_enumerate, dev);
}
break;
case USBH_ENUM_STATE_CONFIGURATION_DT_READ:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
struct usb_config_descriptor *cdt =
(struct usb_config_descriptor *)&usbh_buffer[USB_DT_DEVICE_SIZE];
dev->state = USBH_ENUM_STATE_CONFIGURATION_DT_READ_COMPLETE;
device_xfer_control_read(&usbh_buffer[USB_DT_DEVICE_SIZE],
cdt->wTotalLength, device_enumerate, dev);
}
break;
default:
device_enumeration_terminate(dev);
ERROR(cb_data.status);
break;
}
}
break;
case USBH_ENUM_STATE_CONFIGURATION_DT_READ_COMPLETE:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
struct usb_config_descriptor *cdt =
(struct usb_config_descriptor *)&usbh_buffer[USB_DT_DEVICE_SIZE];
LOG_PRINTF("Configuration descriptor read complete. length: %d\n", cdt->wTotalLength);
CONTINUE_WITH(USBH_ENUM_STATE_SET_CONFIGURATION_SETUP);
}
break;
default:
device_enumeration_terminate(dev);
ERROR(cb_data.status);
break;
}
}
break;
case USBH_ENUM_STATE_SET_CONFIGURATION_SETUP:
{
struct usb_config_descriptor *cdt =
(struct usb_config_descriptor *)&usbh_buffer[USB_DT_DEVICE_SIZE];
struct usb_setup_data setup_data;
setup_data.bmRequestType = USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE;
setup_data.bRequest = USB_REQ_SET_CONFIGURATION;
setup_data.wValue = cdt->bConfigurationValue;
setup_data.wIndex = 0;
setup_data.wLength = 0;
dev->state = USBH_ENUM_STATE_SET_CONFIGURATION_COMPLETE;
device_control(dev, device_enumerate, &setup_data, 0);
}
break;
case USBH_ENUM_STATE_SET_CONFIGURATION_COMPLETE:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
CONTINUE_WITH(USBH_ENUM_STATE_FIND_DRIVER);
break;
default:
device_enumeration_terminate(dev);
ERROR(cb_data.status);
break;
}
}
break;
case USBH_ENUM_STATE_FIND_DRIVER:
{
struct usb_config_descriptor *cdt =
(struct usb_config_descriptor *)&usbh_buffer[USB_DT_DEVICE_SIZE];
device_register(usbh_buffer, cdt->wTotalLength + USB_DT_DEVICE_SIZE, dev);
device_enumeration_finish(dev);
}
break;
default:
LOG_PRINTF("Error: Unknown state "__FILE__"/%d\n", __LINE__);
break;
}
}
void device_enumeration_start(usbh_device_t *dev)
{
set_enumeration();
// save address
uint8_t address = dev->address;
dev->address = 0;
if (dev->speed == USBH_SPEED_LOW) {
dev->packet_size_max0 = 8;
} else {
dev->packet_size_max0 = 64;
}
usbh_data.address_temporary = address;
LOG_PRINTF("\n\n\n ENUMERATION OF DEVICE@%d STARTED \n\n", address);
dev->state = USBH_ENUM_STATE_SET_ADDRESS;
struct usb_setup_data setup_data;
setup_data.bmRequestType = USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE;
setup_data.bRequest = USB_REQ_SET_ADDRESS;
setup_data.wValue = address;
setup_data.wIndex = 0;
setup_data.wLength = 0;
device_control(dev, device_enumerate, &setup_data, 0);
}
/**
* Should be called with at least 1kHz frequency
*
*/
void usbh_poll(uint32_t time_curr_us)
{
uint32_t k = 0;
while (usbh_data.lld_drivers[k]) {
usbh_device_t *usbh_device =
((usbh_generic_data_t *)(usbh_data.lld_drivers[k]->driver_data))->usbh_device;
usbh_generic_data_t *lld_data = usbh_data.lld_drivers[k]->driver_data;
enum USBH_POLL_STATUS poll_status =
usbh_data.lld_drivers[k]->poll(lld_data, time_curr_us);
switch (poll_status) {
case USBH_POLL_STATUS_DEVICE_CONNECTED:
// New device found
LOG_PRINTF("\nDEVICE FOUND\n");
usbh_device[0].lld = usbh_data.lld_drivers[k];
usbh_device[0].speed = usbh_data.lld_drivers[k]->root_speed(lld_data);
usbh_device[0].address = 1;
usbh_device[0].control.state = USBH_CONTROL_STATE_NONE;
device_enumeration_start(&usbh_device[0]);
break;
case USBH_POLL_STATUS_DEVICE_DISCONNECTED:
{
usbh_device[0].control.state = USBH_CONTROL_STATE_NONE;
uint32_t i;
for (i = 0; i < USBH_MAX_DEVICES; i++) {
device_remove(&usbh_device[i]);
}
}
break;
default:
break;
}
if (lld_data->usbh_device[0].drv && usbh_device[0].drvdata) {
usbh_device[0].drv->poll(usbh_device[0].drvdata, time_curr_us);
}
k++;
}
}
void usbh_read(usbh_device_t *dev, usbh_packet_t *packet)
{
const usbh_low_level_driver_t *lld = dev->lld;
lld->read(lld->driver_data, packet);
}
void usbh_write(usbh_device_t *dev, const usbh_packet_t *packet)
{
const usbh_low_level_driver_t *lld = dev->lld;
lld->write(lld->driver_data, packet);
}
|