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
|
Host and Device Drivers
=======================================
.. note:: Some older interface examples omit busid or other parameters. Use the declarations in the current version's headers when calling these interfaces.
Place setup and transfer buffers in memory accessible to the controller's DMA, and meet its address and cache-line alignment requirements. Use non-cacheable memory or the existing cache maintenance interfaces according to the configuration.
device controller(dcd)
-------------------------
usb_dc_init
""""""""""""""""""""""""""""""""""""
``usb_dc_init`` is used to initialize USB device controller registers, set USB pins, clock, interrupts, etc. **This function is not open to users**.
.. code-block:: C
int usb_dc_init(void);
- **return** Returns 0 for success, other values indicate error
usb_dc_deinit
""""""""""""""""""""""""""""""""""""
``usb_dc_deinit`` is used to de-initialize USB device controller registers. **This function is not open to users**.
.. code-block:: C
int usb_dc_deinit(void);
- **return** Returns 0 for success, other values indicate error
usbd_set_address
""""""""""""""""""""""""""""""""""""
``usbd_set_address`` sets the device address. **This function is not open to users**.
.. code-block:: C
int usbd_set_address(const uint8_t addr);
- **addr** Device address
- **return** Returns 0 for success, other values indicate error
usbd_ep_open
""""""""""""""""""""""""""""""""""""
``usbd_ep_open`` sets endpoint properties and enables corresponding endpoint interrupts. **This function is not open to users**.
.. code-block:: C
int usbd_ep_open(const struct usb_endpoint_descriptor *ep);
- **ep** Endpoint descriptor
- **return** Returns 0 for success, other values indicate error
usbd_ep_close
""""""""""""""""""""""""""""""""""""
``usbd_ep_close`` closes an endpoint. **This function is not open to users**.
.. code-block:: C
int usbd_ep_close(const uint8_t ep);
- **ep** Endpoint address
- **return** Returns 0 for success, other values indicate error
usbd_ep_set_stall
""""""""""""""""""""""""""""""""""""
``usbd_ep_set_stall`` sets an endpoint to stall state and sends a stall handshake packet. **This function is open to users**.
.. code-block:: C
int usbd_ep_set_stall(const uint8_t ep);
- **ep** Endpoint address
- **return** Returns 0 for success, other values indicate error
usbd_ep_clear_stall
""""""""""""""""""""""""""""""""""""
``usbd_ep_clear_stall`` clears the stall state of an endpoint. **This function is not open to users**.
.. code-block:: C
int usbd_ep_clear_stall(const uint8_t ep);
- **ep** Endpoint address
- **return** Returns 0 for success, other values indicate error
usbd_ep_is_stalled
""""""""""""""""""""""""""""""""""""
``usbd_ep_is_stalled`` reads the current stall state of an endpoint. **This function is not open to users**.
.. code-block:: C
int usbd_ep_is_stalled(const uint8_t ep, uint8_t *stalled);
- **ep** Endpoint address
- **return** Returns 1 for stalled, 0 for not stalled
usbd_ep_start_write
""""""""""""""""""""""""""""""""""""
``usbd_ep_start_write`` starts an asynchronous transfer on an IN endpoint. **This function is open to users**.
.. code-block:: C
int usbd_ep_start_write(uint8_t busid, const uint8_t ep, const uint8_t *data, uint32_t data_len);
- **busid** USB bus ID
- **ep** IN endpoint address, including the direction bit
- **data** Transmission buffer
- **data_len** Transmission length; use zero to send a ZLP
- **return** Returns 0 when the transfer starts successfully, or a negative value on failure
The registered IN completion callback runs when the transfer finishes; its ``nbytes`` argument is the actual length. Keep the transmission buffer valid and its contents unchanged until completion. The application serializes transmissions on each IN endpoint and uses the start result to determine whether to wait for a completion notification.
When the transmission length is a multiple of MPS, the class or protocol determines whether to append a ZLP. Follow the corresponding class/demo usage.
usbd_ep_start_read
""""""""""""""""""""""""""""""""""""
``usbd_ep_start_read`` prepares an asynchronous receive transfer on an OUT endpoint. **This function is open to users**.
.. code-block:: C
int usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data, uint32_t data_len);
- **busid** USB bus ID
- **ep** OUT endpoint address
- **data** Reception buffer, which must remain valid until this receive transfer completes
- **data_len** Requested receive length, normally a multiple of the endpoint MPS; the buffer capacity must cover the receive range programmed by the current DCD
- **return** Returns 0 when the transfer starts successfully, or a negative value on failure
The registered OUT completion callback runs when reception reaches the requested length or encounters a short packet, including a ZLP. Its ``nbytes`` argument is the actual received length; use it when processing the data.
Start using ordinary endpoints after ``USBD_EVENT_CONFIGURED`` and prepare the first OUT receive transfer. For continuous reception, prepare another receive transfer after each completion. The application serializes reception on each OUT endpoint and coordinates buffer ownership between data processing and the next receive transfer.
.. note:: For bulk transfers, data_len is normally EP MPS. A protocol may use multiple EP MPS for fixed-length transfers, a custom protocol carrying length information (such as MSC), or host transfers terminated with a ZLP or short packet (such as RNDIS).
Endpoint completion callbacks run in the USB ISR. Use them for brief processing, starting the next transfer, or notifying a worker thread through the RTOS/OSAL. Perform lengthy processing in a thread.
host controller(hcd)
------------------------
usb_hc_init
""""""""""""""""""""""""""""""""""""
``usb_hc_init`` is used to initialize USB host controller registers, set USB pins, clock, interrupts, etc. **This function is not open to users**.
.. code-block:: C
int usb_hc_init(void);
- **return** Returns 0 for success, other values indicate error
usb_hc_deinit
""""""""""""""""""""""""""""""""""""
``usb_hc_deinit`` is used to de-initialize USB host controller registers. **This function is not open to users**.
.. code-block:: C
int usb_hc_deinit(void);
- **return** Returns 0 for success, other values indicate error
usbh_roothub_control
""""""""""""""""""""""""""""""""""""
``usbh_roothub_control`` is used to send requests to the root hub. **This function is not open to users**.
.. code-block:: C
int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf);
- **setup** Request
- **buf** Reception buffer
- **return** Returns 0 for success, other values indicate error
usbh_submit_urb
""""""""""""""""""""""""""""""""""""
``usbh_submit_urb`` submits a transfer to endpoint ``urb->ep`` on the device associated with ``urb->hport``. **This function is open to users**.
.. code-block:: C
int usbh_submit_urb(struct usbh_urb *urb);
- **urb** USB request block containing the port, endpoint, setup, buffer, length, timeout and completion callback parameters
- **return** Returns 0 on success, or a negative value on error; success has different meanings for synchronous and asynchronous transfers, as described below
Before every call to ``usbh_submit_urb()``, call the corresponding fill function, such as ``usbh_control_urb_fill()``, ``usbh_bulk_urb_fill()`` or ``usbh_int_urb_fill()``, to populate the current request parameters. This also applies when resubmitting from a completion callback. Initialize the URB before its first use. Control transfers include a setup packet; interrupt transfers include an interval.
- **Synchronous transfer**: Set ``timeout > 0`` and ``complete = NULL``. The calling thread waits for completion or timeout. A return value of 0 indicates success; read the actual length from ``urb->actual_length``.
- **Asynchronous transfer**: Set ``timeout = 0`` and provide ``complete`` and ``arg``. A return value of 0 indicates successful submission. Completion is reported through ``complete(arg, nbytes)``: a non-negative value is the actual length, and a negative value is an error. Normal transfer completion callbacks run in the USB ISR and are suitable for brief processing, submitting another transfer, or notifying a worker thread.
Each in-flight request has exclusive use of its URB and buffer. Keep the associated setup, buffer and arg objects valid throughout the transfer and cleanup. After completion, call the corresponding fill function to populate the next transfer parameters before submitting it. The HCD manages ``hcpriv``.
Among them, the `urb` structure information is as follows:
.. code-block:: C
struct usbh_urb {
usb_slist_t list;
void *hcpriv;
struct usbh_hubport *hport;
struct usb_endpoint_descriptor *ep;
uint8_t data_toggle;
uint32_t interval;
struct usb_setup_packet *setup;
uint8_t *transfer_buffer;
uint32_t transfer_buffer_length;
int transfer_flags;
uint32_t actual_length;
uint32_t timeout;
int errorcode;
uint32_t num_of_iso_packets;
uint32_t start_frame;
usbh_complete_callback_t complete;
void *arg;
#if defined(__ICCARM__) || defined(__ICCRISCV__) || defined(__ICCRX__)
struct usbh_iso_frame_packet *iso_packet;
#else
struct usbh_iso_frame_packet iso_packet[0];
#endif
};
- **hcpriv** Host controller driver private member
- **hport** The hport used by current URB
- **ep** The endpoint used by current URB
- **data_toggle** Current data toggle
- **interval** URB transfer interval in microseconds. If interval is greater than 1000us, software timer needs to be used for maintenance
- **setup** Setup request buffer, used by endpoint 0
- **transfer_buffer** Transfer data buffer
- **transfer_buffer_length** Transfer length
- **transfer_flags** Flags carried during transfer
- **actual_length** Actual transfer length
- **timeout** Transfer timeout. If 0, the function is non-blocking and can be used in interrupts
- **errorcode** Error code
- **num_of_iso_packets** Number of ISO frames or microframes
- **complete** Transfer completion callback function
- **arg** Parameters carried when transfer completes
- **iso_packet** ISO data packet
.. note:: Set the synchronous wait time according to application requirements. On synchronous timeout, the submit path handles cancellation and cleanup; the caller handles the return result. Asynchronous transfers report their results through the complete callback.
`errorcode` can return the following values:
.. code-block:: C
#define USB_ERR_NOMEM 1
#define USB_ERR_INVAL 2
#define USB_ERR_NODEV 3
#define USB_ERR_NOTCONN 4
#define USB_ERR_NOTSUPP 5
#define USB_ERR_BUSY 6
#define USB_ERR_RANGE 7
#define USB_ERR_STALL 8
#define USB_ERR_BABBLE 9
#define USB_ERR_NAK 10
#define USB_ERR_DT 11
#define USB_ERR_IO 12
#define USB_ERR_SHUTDOWN 13
#define USB_ERR_TIMEOUT 14
Among them, the `iso_packet` structure information is as follows:
.. code-block:: C
struct usbh_iso_frame_packet {
uint8_t *transfer_buffer;
uint32_t transfer_buffer_length;
uint32_t actual_length;
int errorcode;
};
- **transfer_buffer** Transfer data buffer
- **transfer_buffer_length** Transfer length
- **actual_length** Actual transfer length
- **errorcode** Error code
usbh_kill_urb
""""""""""""""""""""""""""""""""""""
``usbh_kill_urb`` cancels the transfer associated with the specified URB. **This function is open to users**.
.. code-block:: C
int usbh_kill_urb(struct usbh_urb *urb);
- **urb** The same USB request block that was previously submitted
- **return** Returns 0 when the cancellation call succeeds, or a negative value on error
Usage: during device disconnection, the enumeration thread cancels the class's in-flight URBs. On synchronous timeout, the submit path handles cancellation and cleanup; the caller handles the submit result.
Cancelling a synchronous request wakes its waiting thread. Cancelling an asynchronous request invokes its error callback. The error callback runs in the kill caller's context and handles the error to end the current transfer; normal success callbacks may submit further transfers as required by the application.
During disconnection, stop application submissions, cancel in-flight requests, terminate the associated worker threads, then release application objects. The caller manages the application memory for URBs and data buffers.
|