-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathhal_usb_lld.c
791 lines (666 loc) · 24.7 KB
/
hal_usb_lld.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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
ChibiOS - Copyright (C) 2015 RedoX https://github.com/RedoXyde/
(C) 2015-2016 flabbergast <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @file USBHSv1/hal_usb_lld.c
* @brief MIMXRT1062 USB subsystem low level driver source.
* @note page 2211 in https://www.pjrc.com/teensy/IMXRT1060RM_rev2.pdf
* @note page 2203: chapter 42 USB
* @note https://github.com/kylemanna/kinetis-sdk2/blob/master/docs/usb/MCUXpresso%20SDK%20USB%20Stack%20Device%20Reference%20Manual.pdf
*
* @addtogroup USB
* @{
*/
#include <string.h>
#include "hal.h"
#include "usb_device_config.h"
#include "usb.h"
#include "usb_device.h"
#include "usb_device_dci.h"
#include "usb_device_ehci.h"
#include "usb_phy.h"
usb_device_handle handle;
#include "printf_debug.h"
#include "fsl_clock.h"
#if HAL_USE_USB || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/** @brief USB1 driver identifier.*/
#if MIMXRT1062_USB_USE_USB1 || defined(__DOXYGEN__)
USBDriver USBD1;
#endif
/*===========================================================================*/
/* Driver local variables and types. */
/*===========================================================================*/
/**
* @brief IN EP0 state.
*/
USBInEndpointState ep0in;
/**
* @brief OUT EP0 state.
*/
USBOutEndpointState ep0out;
/**
* @brief Buffer for the EP0 setup packets.
*/
static uint8_t ep0setup_buffer[8];
/**
* @brief EP0 initialization structure.
*/
static const USBEndpointConfig ep0config = {
USB_EP_MODE_TYPE_CTRL,
_usb_ep0setup,
_usb_ep0in,
_usb_ep0out,
64,
64,
&ep0in,
&ep0out,
1,
ep0setup_buffer
};
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
static uint8_t epaddr(usbep_t ep, uint8_t direction) {
return ep | (direction << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT);
}
static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam) {
(void)handle;
(void)eventParam;
USBDriver *usbp = &USBD1;
printf_debug("usb_device_callback(event=%d)", callbackEvent);
switch (callbackEvent) {
case kUSB_DeviceEventBusReset:
printf_debug(" bus reset");
_usb_reset(usbp);
break;
case kUSB_DeviceEventSuspend:
printf_debug(" suspend--nxp");
// Call USB_DeviceSetStatus() to enable the “detect resume” interrupt.
usb_device_struct_t *dev_handle = (usb_device_struct_t *)handle;
(void)USB_DeviceSetStatus(dev_handle, kUSB_DeviceStatusBusSuspend, NULL);
printf_debug(" suspend--chibi");
_usb_suspend(usbp);
break;
case kUSB_DeviceEventResume:
printf_debug(" resume--chibi");
_usb_wakeup(usbp);
break;
}
return kStatus_USB_Success;
}
/*===========================================================================*/
/* Driver interrupt handlers. */
/*============================================================================*/
#if MIMXRT1062_USB_USE_USB1 || defined(__DOXYGEN__)
/**
* @brief USB interrupt handler.
* @note → page 2210, “Interrupts”
*
* @isr
*/
OSAL_IRQ_HANDLER(MIMXRT1062_USB_OTG1_IRQ_VECTOR) {
USBDriver *usbp = &USBD1;
OSAL_IRQ_PROLOGUE();
// Do not print as long as SOF interrupt is enabled to avoid slowing down the
// application due to serial print latency:
//printf_debug("ISR\n");
// inlined from USB_DeviceEhciIsrFunction(handle) so that we can dispatch the
// SOF (start of frame) interrupt:
usb_device_struct_t *dev_handle = (usb_device_struct_t *)handle;
usb_device_ehci_state_struct_t *ehciState;
uint32_t status;
ehciState = (usb_device_ehci_state_struct_t *)(dev_handle->controllerHandle);
status = ehciState->registerBase->USBSTS;
status &= ehciState->registerBase->USBINTR;
USB_DeviceEhciIsrFunction(handle);
// Some ChibiOS components require the SOF interrupt, such as the USB serial.
if ((status & USBHS_USBSTS_SRI_MASK)) {
_usb_isr_invoke_sof_cb(usbp);
}
OSAL_IRQ_EPILOGUE();
}
#endif /* MIMXRT1062_USB_USE_USB1 */
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Low level USB driver initialization.
*
* @notapi
*/
void usb_lld_init(void) {
USBDriver *usbp = &USBD1;
/* Driver initialization.*/
usbObjectInit(usbp);
#if MIMXRT1062_USB_USE_USB1
/* USB PHY configuration */
#ifndef BOARD_USB_PHY_D_CAL
#define BOARD_USB_PHY_D_CAL (0x0CU)
#endif
#ifndef BOARD_USB_PHY_TXCAL45DP
#define BOARD_USB_PHY_TXCAL45DP (0x06U)
#endif
#ifndef BOARD_USB_PHY_TXCAL45DM
#define BOARD_USB_PHY_TXCAL45DM (0x06U)
#endif
usb_phy_config_struct_t phyConfig = {
BOARD_USB_PHY_D_CAL, BOARD_USB_PHY_TXCAL45DP, BOARD_USB_PHY_TXCAL45DM,
};
uint32_t notUsed = 0;
CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
CLOCK_EnableUsbhs0Clock(kCLOCK_Usb480M, 480000000U);
USB_EhciPhyInit(kUSB_ControllerEhci0, notUsed, &phyConfig);
if (USB_DeviceInit(kUSB_ControllerEhci0, usb_device_callback, &handle) != kStatus_USB_Success) {
printf_debug("allocating handle failed");
return;
}
printf_debug("handle allocated");
#endif /* MIMXRT1062_USB_USE_USB1 */
}
/**
* @brief Configures and activates the USB peripheral.
*
* @param[in] usbp pointer to the @p USBDriver object
*
* @notapi
*/
void usb_lld_start(USBDriver *usbp) {
if (usbp->state != USB_STOP) {
printf_debug("already started!");
return; // already started
}
#if MIMXRT1062_USB_USE_USB1
if (usbp != &USBD1) {
return; // unknown usbp
}
printf_debug(" usb_lld_start() enter");
USB_DeviceRun(handle);
printf_debug("device running");
nvicEnableVector(USB_OTG1_IRQn, MIMXRT1062_USB_USB1_IRQ_PRIORITY);
printf_debug("interrupt enabled");
#endif /* MIMXRT1062_USB_USE_USB1 */
}
/**
* @brief Deactivates the USB peripheral.
*
* @param[in] usbp pointer to the @p USBDriver object
*
* @notapi
*/
void usb_lld_stop(USBDriver *usbp) {
if (usbp->state == USB_STOP) {
#if MIMXRT1062_USB_USE_USB1
if (&USBD1 == usbp) {
printf_debug("usb_lld_stop()");
USB_DeviceStop(handle);
nvicDisableVector(USB_OTG1_IRQn);
}
#endif /* MIMXRT1062_USB_USE_USB1 */
}
}
/**
* Called when the USB transfer completed
*
*/
static usb_status_t device_ep0_control_callback(usb_device_handle handle,
usb_device_endpoint_callback_message_struct_t *message,
void *callbackParam,
uint8_t direction) {
(void)handle;
(void)callbackParam;
USBDriver *usbp = &USBD1;
printf_debug("device_ep0_control_callback");
const uint8_t ep = 0;
const USBEndpointConfig *epc = usbp->epc[ep];
if (message->isSetup) {
printf_debug("setup message for endpoint 0");
/* Clear receiving in the chibios state machine */
(usbp)->receiving &= ~(1 << ep);
(usbp)->transmitting &= ~(1 << ep);
memcpy(usbp->setup, message->buffer, message->length);
/* Call SETUP function (ChibiOS core), which prepares
* for send or receive and releases the buffer
*/
if (epc->setup_cb != NULL) {
_usb_isr_invoke_setup_cb(usbp, ep);
// -> will call usb_lld_read_setup
// -> will call e.g. usb_lld_start_in
}
return kStatus_USB_Success;
}
printf_debug("transfer done: buffer %x, len=%d!", message->buffer, message->length);
if (message->length == USB_CANCELLED_TRANSFER_LENGTH) {
printf_debug("USB transfer cancelled");
return kStatus_USB_InvalidRequest;
}
if (direction == USB_OUT) {
printf_debug(" complete, OUT ep=%d (OUT), rxbuf=%x", ep, epc->out_state->rxbuf);
USBOutEndpointState *osp = epc->out_state;
osp->rxcnt = message->length;
printf_debug(" received %d bytes", osp->rxcnt);
/* Endpoint Receive Complete Event */
/* Transfer Direction OUT */
printf_debug(" invoking out_cb for ep %d", ep);
_usb_isr_invoke_out_cb(usbp, ep);
} else if (direction == USB_IN) {
printf_debug(" complete, IN ep=%d (IN), txbuf=%x", ep, epc->in_state->txbuf);
/* Endpoint Transmit Complete Event */
/* Transfer Direction IN */
printf_debug(" invoking in_cb for ep %d", ep);
_usb_isr_invoke_in_cb(usbp, ep);
}
return kStatus_USB_Success;
}
static usb_status_t device_ep0_control_callback_in(usb_device_handle handle,
usb_device_endpoint_callback_message_struct_t *message,
void *callbackParam) {
return device_ep0_control_callback(handle, message, callbackParam, USB_IN);
}
static usb_status_t device_ep0_control_callback_out(usb_device_handle handle,
usb_device_endpoint_callback_message_struct_t *message,
void *callbackParam) {
return device_ep0_control_callback(handle, message, callbackParam, USB_OUT);
}
/**
* @brief USB low level reset routine.
*
* @param[in] usbp pointer to the @p USBDriver object
*
* @notapi
*/
void usb_lld_reset(USBDriver *usbp) {
usbp->epc[0] = &ep0config;
usb_device_endpoint_init_struct_t epInitStruct;
usb_device_endpoint_callback_struct_t epCallback;
usb_status_t status;
epCallback.callbackFn = device_ep0_control_callback_in;
epCallback.callbackParam = NULL;
/* ChibiOS handles zero-length transmissions (ZLT) in software, so disable the
* hardware functionality for it:
* https://github.com/ChibiOS/ChibiOS/blob/525f89f0e322bb410e0674e4945bf8196d92df65/os/hal/src/hal_usb.c#L891-L901
*
* See also:
* https://community.nxp.com/t5/MQX-Software-Solutions/MQX-USBHS-Control-Endpoint-Zero-Length-Termination/m-p/375730
*/
epInitStruct.zlt = 0;
epInitStruct.transferType = USB_ENDPOINT_CONTROL;
epInitStruct.interval = 0;
epInitStruct.endpointAddress = epaddr(USB_CONTROL_ENDPOINT, USB_IN);
epInitStruct.maxPacketSize = USB_CONTROL_MAX_PACKET_SIZE;
/* Initialize the control IN pipe */
status = USB_DeviceInitEndpoint(handle, &epInitStruct, &epCallback);
if (kStatus_USB_Success != status) {
printf_debug("USB_DeviceInitEndpoint() failed: %d", status);
return;
}
epCallback.callbackFn = device_ep0_control_callback_out;
epCallback.callbackParam = NULL;
epInitStruct.endpointAddress = epaddr(USB_CONTROL_ENDPOINT, USB_OUT);
/* Initialize the control OUT pipe */
status = USB_DeviceInitEndpoint(handle, &epInitStruct, &epCallback);
if (kStatus_USB_Success != status) {
(void)USB_DeviceDeinitEndpoint(handle, epInitStruct.endpointAddress);
printf_debug("USB_DeviceInitEndpoint() failed: %d", status);
return;
}
}
/**
* @brief Sets the USB address.
*
* @param[in] usbp pointer to the @p USBDriver object
*
* @notapi
*/
void usb_lld_set_address(USBDriver *usbp) {
#if MIMXRT1062_USB_USE_USB1
printf_debug("usb_lld_set_address(%d)", usbp->address);
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
printf_debug(" DeviceStatusDeviceState=%x", state);
if (((uint8_t)kUSB_DeviceStateAddressing != state) && ((uint8_t)kUSB_DeviceStateAddress != state) &&
((uint8_t)kUSB_DeviceStateDefault != state) && ((uint8_t)kUSB_DeviceStateConfigured != state)) {
printf_debug(" invalid state!");
return;
}
if ((uint8_t)kUSB_DeviceStateAddressing != state) {
/* If the device address is not setting, pass the address and the device state will change to
* kUSB_DeviceStateAddressing internally. */
state = (uint8_t)(usbp->address & 0xFF);
error = USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, &state);
} else {
/* If the device address is setting, set device address and the address will be write into the controller
* internally. */
error = USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL);
/* And then change the device state to kUSB_DeviceStateAddress. */
if (kStatus_USB_Success == error) {
state = (uint8_t)kUSB_DeviceStateAddress;
error = USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
}
}
#endif /* MIMXRT1062_USB_USE_USB1 */
}
/**
* Called when the USB transfer completed
*
*/
static usb_status_t device_epN_control_callback(usb_device_handle handle,
usb_device_endpoint_callback_message_struct_t *message,
void *callbackParam,
uint8_t direction) {
(void)handle;
USBDriver *usbp = &USBD1;
uint8_t ep = (uint8_t)((uint32_t)callbackParam & 0xFF);
printf_debug("device_epN_control_callback");
if (message->isSetup) {
printf_debug("discarding unexpected setup message for endpoint ep=%d", ep);
return kStatus_USB_Error;
}
if (message->length == USB_CANCELLED_TRANSFER_LENGTH) {
printf_debug("USB transfer cancelled");
return kStatus_USB_InvalidRequest;
}
printf_debug("transfer done: buffer %x, len=%d!", message->buffer, message->length);
const USBEndpointConfig *epc = usbp->epc[ep];
if (direction == USB_OUT) {
printf_debug(" complete, OUT ep=%d (OUT), rxbuf=%x", ep, epc->out_state->rxbuf);
USBOutEndpointState *osp = epc->out_state;
osp->rxcnt = message->length;
printf_debug(" received %d bytes", osp->rxcnt);
/* Endpoint Receive Complete Event */
/* Transfer Direction OUT */
printf_debug(" invoking out_cb for ep %d", ep);
_usb_isr_invoke_out_cb(usbp, ep);
} else if (direction == USB_IN) {
printf_debug(" complete, IN ep=%d (IN), txbuf=%x", ep, epc->in_state->txbuf);
/* Endpoint Transmit Complete Event */
/* Transfer Direction IN */
printf_debug(" invoking in_cb for ep %d", ep);
_usb_isr_invoke_in_cb(usbp, ep);
}
return kStatus_USB_Success;
}
static usb_status_t device_epN_control_callback_out(usb_device_handle handle,
usb_device_endpoint_callback_message_struct_t *message,
void *callbackParam) {
return device_epN_control_callback(handle, message, callbackParam, USB_OUT);
}
static usb_status_t device_epN_control_callback_in(usb_device_handle handle,
usb_device_endpoint_callback_message_struct_t *message,
void *callbackParam) {
return device_epN_control_callback(handle, message, callbackParam, USB_IN);
}
/**
* @brief Enables an endpoint.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
*
* @notapi
*/
void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) {
if(ep > MIMXRT1062_USB_ENDPOINTS)
return;
printf_debug("usb_lld_init_endpoint(ep=%d)", ep);
const USBEndpointConfig *epc = usbp->epc[ep];
if (epc->out_state == NULL && epc->in_state == NULL) {
printf_debug(" BUG: unknown endpoint type?!");
return;
}
usb_device_endpoint_init_struct_t epInitStruct;
usb_device_endpoint_callback_struct_t epCallback;
usb_status_t status;
epCallback.callbackFn = device_epN_control_callback_in;
epCallback.callbackParam = (void*)((uint32_t)ep);
epInitStruct.transferType = epc->ep_mode;
epInitStruct.zlt = 0;
epInitStruct.interval = 0;
if (epc->in_state != NULL) {
epInitStruct.endpointAddress = epaddr(ep, USB_IN);
epInitStruct.maxPacketSize = epc->in_maxsize;
/* Initialize the endpoint IN pipe */
if ((status = USB_DeviceInitEndpoint(handle, &epInitStruct, &epCallback)) != kStatus_USB_Success) {
printf_debug("USB_DeviceInitEndpoint() failed: %d", status);
return;
}
}
if (epc->out_state != NULL) {
epCallback.callbackFn = device_epN_control_callback_out;
epInitStruct.endpointAddress = epaddr(ep, USB_OUT);
epInitStruct.maxPacketSize = epc->out_maxsize;
/* Initialize the endpoint OUT pipe */
if ((status = USB_DeviceInitEndpoint(handle, &epInitStruct, &epCallback)) != kStatus_USB_Success) {
USB_DeviceDeinitEndpoint(handle, USB_CONTROL_ENDPOINT | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
printf_debug("USB_DeviceInitEndpoint() failed: %d", status);
return;
}
}
return;
#if MIMXRT1062_USB_USE_USB1
#endif /* MIMXRT1062_USB_USE_USB1 */
}
/**
* @brief Disables all the active endpoints except the endpoint zero.
*
* @param[in] usbp pointer to the @p USBDriver object
*
* @notapi
*/
void usb_lld_disable_endpoints(USBDriver *usbp) {
(void)usbp;
printf_debug("TODO: usb_lld_disable_endpoints()");
#if MIMXRT1062_USB_USE_USB1
// TODO: find active endpoints and disable
// USB_DeviceDeinitEndpoint(usb_device_handle handle, uint8_t endpointAddress);
#endif /* MIMXRT1062_USB_USE_USB1 */
}
static usbepstatus_t usb_lld_get_status(USBDriver *usbp, usbep_t ep, uint8_t direction) {
(void)usbp;
/* The NXP USB middleware does not expose any API to query whether an endpoint
* was initialized, so we just always assume an endpoint is active: */
// TODO: maybe check if epCallback[].callbackFn != NULL?
usbepstatus_t epstatus = EP_STATUS_ACTIVE;
usb_status_t status;
usb_device_endpoint_status_struct_t endpointStatus;
endpointStatus.endpointAddress = epaddr(ep, direction);
if ((status = USB_DeviceGetStatus(handle, kUSB_DeviceStatusEndpoint, &endpointStatus)) != kStatus_USB_Success) {
printf_debug("USB_DeviceGetStatus() = %d", status);
return epstatus;
}
if (endpointStatus.endpointStatus == kUSB_DeviceEndpointStateStalled) {
return EP_STATUS_STALLED;
}
return epstatus;
}
/**
* @brief Returns the status of an OUT endpoint.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
* @return The endpoint status.
* @retval EP_STATUS_DISABLED The endpoint is not active.
* @retval EP_STATUS_STALLED The endpoint is stalled.
* @retval EP_STATUS_ACTIVE The endpoint is active.
*
* @notapi
*/
usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep) {
(void)usbp;
if(ep > USB_MAX_ENDPOINTS)
return EP_STATUS_DISABLED;
#if MIMXRT1062_USB_USE_USB1
printf_debug("usb_lld_get_status_out(ep=%d)", ep);
return usb_lld_get_status(usbp, ep, USB_OUT);
#endif /* MIMXRT1062_USB_USE_USB1 */
}
/**
* @brief Returns the status of an IN endpoint.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
* @return The endpoint status.
* @retval EP_STATUS_DISABLED The endpoint is not active.
* @retval EP_STATUS_STALLED The endpoint is stalled.
* @retval EP_STATUS_ACTIVE The endpoint is active.
*
* @notapi
*/
usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep) {
(void)usbp;
if(ep > USB_MAX_ENDPOINTS)
return EP_STATUS_DISABLED;
#if MIMXRT1062_USB_USE_USB1
printf_debug("usb_lld_get_status_in(ep=%d)", ep);
return usb_lld_get_status(usbp, ep, USB_IN);
#endif /* MIMXRT1062_USB_USE_USB1 */
}
/**
* @brief Reads a setup packet from the dedicated packet buffer.
* @details This function must be invoked in the context of the @p setup_cb
* callback in order to read the received setup packet.
* @pre In order to use this function the endpoint must have been
* initialized as a control endpoint.
* @post The endpoint is ready to accept another packet.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
* @param[out] buf buffer where to copy the packet data
*
* @notapi
*/
void usb_lld_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf) {
(void)usbp;
(void)ep;
(void)buf;
// We already populated usbp->setup in device_ep0_control_callback(), so no
// extra steps are required in this function.
}
/**
* @brief Starts a receive operation on an OUT endpoint.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
*
* @notapi
*/
void usb_lld_start_out(USBDriver *usbp, usbep_t ep) {
const USBEndpointConfig *epc = usbp->epc[ep];
USBOutEndpointState *osp = epc->out_state;
printf_debug(" usb_lld_start_out(ep=%d) (receive)", ep);
if (osp->rxsize > epc->out_maxsize) {
osp->rxsize = epc->out_maxsize;
}
USB_DeviceRecvRequest(handle, epaddr(ep, USB_OUT), osp->rxbuf, osp->rxsize);
}
/**
* @brief Starts a transmit operation on an IN endpoint.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
*
* @note Called from ISR and locked zone.
* @notapi
*/
void usb_lld_start_in(USBDriver *usbp, usbep_t ep) {
const USBEndpointConfig *epc = usbp->epc[ep];
USBInEndpointState *isp = epc->in_state;
printf_debug(" usb_lld_start_in(ep=%d, txbuf=%x) (transmit)", ep, isp->txbuf);
// Notably, at least with Linux, isp->txsize may exceed epc->in_maxsize, and
// capping it makes device enumeration fail.
// Silence a warning about a mismatch in const-correctness between ChibiOS and
// the NXP SDK: ChibiOS uses const for isp->txbuf (but not for rxbuf, of
// course), but the NXP SDK APIs do not use const at all.
uint32_t p = (uint32_t)isp->txbuf;
uint8_t *buf = (uint8_t*)p;
USB_DeviceSendRequest(handle, epaddr(ep, USB_IN), buf, isp->txsize);
}
/**
* @brief Brings an OUT endpoint in the stalled state.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
*
* @notapi
*/
void usb_lld_stall_out(USBDriver *usbp, usbep_t ep) {
(void)usbp;
printf_debug("usb_lld_stall_out(ep=%d)", ep);
#if MIMXRT1062_USB_USE_USB1
usb_status_t status;
if ((status = USB_DeviceStallEndpoint(handle, epaddr(ep, USB_OUT))) != kStatus_USB_Success) {
printf_debug(" stalling failed: %d", status);
}
#endif /* MIMXRT1062_USB_USE_USB1 */
}
/**
* @brief Brings an IN endpoint in the stalled state.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
*
* @notapi
*/
void usb_lld_stall_in(USBDriver *usbp, usbep_t ep) {
(void)usbp;
printf_debug("usb_lld_stall_in(ep=%d)", ep);
#if MIMXRT1062_USB_USE_USB1
usb_status_t status;
if ((status = USB_DeviceStallEndpoint(handle, epaddr(ep, USB_IN))) != kStatus_USB_Success) {
printf_debug(" stalling failed: %d", status);
}
#endif /* MIMXRT1062_USB_USE_USB1 */
}
/**
* @brief Brings an OUT endpoint in the active state.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
*
* @notapi
*/
void usb_lld_clear_out(USBDriver *usbp, usbep_t ep) {
(void)usbp;
printf_debug("usb_lld_clear_out(ep=%d)", ep);
#if MIMXRT1062_USB_USE_USB1
usb_status_t status;
if ((status = USB_DeviceUnstallEndpoint(handle, epaddr(ep, USB_OUT))) != kStatus_USB_Success) {
printf_debug(" unstalling failed: %d", status);
}
#endif /* MIMXRT1062_USB_USE_USB1 */
}
/**
* @brief Brings an IN endpoint in the active state.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] ep endpoint number
*
* @notapi
*/
void usb_lld_clear_in(USBDriver *usbp, usbep_t ep) {
(void)usbp;
printf_debug("usb_lld_clear_in(ep=%d)", ep);
#if MIMXRT1062_USB_USE_USB1
usb_status_t status;
if ((status = USB_DeviceUnstallEndpoint(handle, epaddr(ep, USB_IN))) != kStatus_USB_Success) {
printf_debug(" unstalling failed: %d", status);
}
#endif /* MIMXRT1062_USB_USE_USB1 */
}
#endif /* HAL_USE_USB */
/** @} */