mikroSDK Reference Manual
usbh.h
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27#ifndef _TUSB_USBH_H_
28#define _TUSB_USBH_H_
29
30#ifdef __cplusplus
31 extern "C" {
32#endif
33
34#include "common/tusb_common.h"
35
36//--------------------------------------------------------------------+
37// MACRO CONSTANT TYPEDEF
38//--------------------------------------------------------------------+
39
40// forward declaration
41struct tuh_xfer_s;
42typedef struct tuh_xfer_s tuh_xfer_t;
43
44typedef void (*tuh_xfer_cb_t)(tuh_xfer_t* xfer);
45
46// Note1: layout and order of this will be changed in near future
47// it is advised to initialize it using member name
48// Note2: not all field is available/meaningful in callback,
49// some info is not saved by usbh to save SRAM
51{
52 uint8_t daddr;
53 uint8_t ep_addr;
54 uint8_t TU_RESERVED; // reserved
55 xfer_result_t result;
56
57 uint32_t actual_len; // excluding setup packet
58
59 union
60 {
61 tusb_control_request_t const* setup; // setup packet pointer if control transfer
62 uint32_t buflen; // expected length if not control transfer (not available in callback)
63 };
64
65 uint8_t* buffer; // not available in callback if not control transfer
66 tuh_xfer_cb_t complete_cb;
67 uintptr_t user_data;
68
69 // uint32_t timeout_ms; // place holder, not supported yet
70};
71
72// Subject to change
73typedef struct
74{
75 uint8_t daddr;
78
79// ConfigID for tuh_config()
80enum
81{
82 TUH_CFGID_RPI_PIO_USB_CONFIGURATION = OPT_MCU_RP2040 << 8 // cfg_param: pio_usb_configuration_t
83};
84
85//--------------------------------------------------------------------+
86// APPLICATION CALLBACK
87//--------------------------------------------------------------------+
88
89//TU_ATTR_WEAK uint8_t tuh_attach_cb (tusb_desc_device_t const *desc_device);
90
91// Invoked when a device is mounted (configured)
92TU_ATTR_WEAK void tuh_mount_cb (uint8_t daddr);
93
94// Invoked when a device failed to mount during enumeration process
95// TU_ATTR_WEAK void tuh_mount_failed_cb (uint8_t daddr);
96
98TU_ATTR_WEAK void tuh_umount_cb(uint8_t daddr);
99
100//--------------------------------------------------------------------+
101// APPLICATION API
102//--------------------------------------------------------------------+
103
104// Configure host stack behavior with dynamic or port-specific parameters.
105// Should be called before tuh_init()
106// - cfg_id : configure ID (TBD)
107// - cfg_param: configure data, structure depends on the ID
108bool tuh_configure(uint8_t controller_id, uint32_t cfg_id, const void* cfg_param);
109
110// Init host stack
111bool tuh_init(uint8_t controller_id);
112
113// Check if host stack is already initialized
114bool tuh_inited(void);
115
116// Task function should be called in main/rtos loop, extended version of tuh_task()
117// - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever
118// - in_isr: if function is called in ISR
119void tuh_task_ext(uint32_t timeout_ms, bool in_isr);
120
121// Task function should be called in main/rtos loop
122TU_ATTR_ALWAYS_INLINE static inline
123void tuh_task(void)
124{
125 tuh_task_ext(UINT32_MAX, false);
126}
127
128// Check if there is pending events need processing by tuh_task()
129bool tuh_task_event_ready(void);
130
131#ifndef _TUSB_HCD_H_
132extern void hcd_int_handler(uint8_t rhport);
133#endif
134
135// Interrupt handler, name alias to HCD
136#define tuh_int_handler hcd_int_handler
137
138bool tuh_vid_pid_get(uint8_t daddr, uint16_t* vid, uint16_t* pid);
139
140tusb_speed_t tuh_speed_get(uint8_t daddr);
141
142// Check if device is connected and configured
143bool tuh_mounted(uint8_t daddr);
144
145// Check if device is suspended
146TU_ATTR_ALWAYS_INLINE static inline
147bool tuh_suspended(uint8_t daddr)
148{
149 // TODO implement suspend & resume on host
150 (void) daddr;
151 return false;
152}
153
154// Check if device is ready to communicate with
155TU_ATTR_ALWAYS_INLINE static inline
156bool tuh_ready(uint8_t daddr)
157{
158 return tuh_mounted(daddr) && !tuh_suspended(daddr);
159}
160
161//--------------------------------------------------------------------+
162// Transfer API
163//--------------------------------------------------------------------+
164
165// Submit a control transfer
166// - async: complete callback invoked when finished.
167// - sync : blocking if complete callback is NULL.
168bool tuh_control_xfer(tuh_xfer_t* xfer);
169
170// Submit a bulk/interrupt transfer
171// - async: complete callback invoked when finished.
172// - sync : blocking if complete callback is NULL.
173bool tuh_edpt_xfer(tuh_xfer_t* xfer);
174
175// Open a non-control endpoint
176bool tuh_edpt_open(uint8_t daddr, tusb_desc_endpoint_t const * desc_ep);
177
178// Abort a queued transfer. Note: it can only abort transfer that has not been started
179// Return true if a queued transfer is aborted, false if there is no transfer to abort
180bool tuh_edpt_abort_xfer(uint8_t daddr, uint8_t ep_addr);
181
182// Set Configuration (control transfer)
183// config_num = 0 will un-configure device. Note: config_num = config_descriptor_index + 1
184// true on success, false if there is on-going control transfer or incorrect parameters
185// if complete_cb == NULL i.e blocking, user_data should be pointed to xfer_reuslt_t*
186bool tuh_configuration_set(uint8_t daddr, uint8_t config_num,
187 tuh_xfer_cb_t complete_cb, uintptr_t user_data);
188
189// Set Interface (control transfer)
190// true on success, false if there is on-going control transfer or incorrect parameters
191// if complete_cb == NULL i.e blocking, user_data should be pointed to xfer_reuslt_t*
192bool tuh_interface_set(uint8_t daddr, uint8_t itf_num, uint8_t itf_alt,
193 tuh_xfer_cb_t complete_cb, uintptr_t user_data);
194
195//--------------------------------------------------------------------+
196// Descriptors Asynchronous (non-blocking)
197//--------------------------------------------------------------------+
198
199// Get an descriptor (control transfer)
200// true on success, false if there is on-going control transfer or incorrect parameters
201bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index, void* buffer, uint16_t len,
202 tuh_xfer_cb_t complete_cb, uintptr_t user_data);
203
204// Get device descriptor (control transfer)
205// true on success, false if there is on-going control transfer or incorrect parameters
206bool tuh_descriptor_get_device(uint8_t daddr, void* buffer, uint16_t len,
207 tuh_xfer_cb_t complete_cb, uintptr_t user_data);
208
209// Get configuration descriptor (control transfer)
210// true on success, false if there is on-going control transfer or incorrect parameters
211bool tuh_descriptor_get_configuration(uint8_t daddr, uint8_t index, void* buffer, uint16_t len,
212 tuh_xfer_cb_t complete_cb, uintptr_t user_data);
213
214// Get HID report descriptor (control transfer)
215// true on success, false if there is on-going control transfer or incorrect parameters
216bool tuh_descriptor_get_hid_report(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, uint8_t index, void* buffer, uint16_t len,
217 tuh_xfer_cb_t complete_cb, uintptr_t user_data);
218
219// Get string descriptor (control transfer)
220// true on success, false if there is on-going control transfer or incorrect parameters
221// Blocking if complete callback is NULL, in this case 'user_data' must contain xfer_result_t variable
222bool tuh_descriptor_get_string(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len,
223 tuh_xfer_cb_t complete_cb, uintptr_t user_data);
224
225// Get manufacturer string descriptor (control transfer)
226// true on success, false if there is on-going control transfer or incorrect parameters
227bool tuh_descriptor_get_manufacturer_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
228 tuh_xfer_cb_t complete_cb, uintptr_t user_data);
229
230// Get product string descriptor (control transfer)
231// true on success, false if there is on-going control transfer or incorrect parameters
232bool tuh_descriptor_get_product_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
233 tuh_xfer_cb_t complete_cb, uintptr_t user_data);
234
235// Get serial string descriptor (control transfer)
236// true on success, false if there is on-going control transfer or incorrect parameters
237bool tuh_descriptor_get_serial_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
238 tuh_xfer_cb_t complete_cb, uintptr_t user_data);
239
240//--------------------------------------------------------------------+
241// Descriptors Synchronous (blocking)
242//--------------------------------------------------------------------+
243
244// Sync (blocking) version of tuh_descriptor_get()
245// return transfer result
246uint8_t tuh_descriptor_get_sync(uint8_t daddr, uint8_t type, uint8_t index, void* buffer, uint16_t len);
247
248// Sync (blocking) version of tuh_descriptor_get_device()
249// return transfer result
250uint8_t tuh_descriptor_get_device_sync(uint8_t daddr, void* buffer, uint16_t len);
251
252// Sync (blocking) version of tuh_descriptor_get_configuration()
253// return transfer result
254uint8_t tuh_descriptor_get_configuration_sync(uint8_t daddr, uint8_t index, void* buffer, uint16_t len);
255
256// Sync (blocking) version of tuh_descriptor_get_hid_report()
257// return transfer result
258uint8_t tuh_descriptor_get_hid_report_sync(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, uint8_t index, void* buffer, uint16_t len);
259
260// Sync (blocking) version of tuh_descriptor_get_string()
261// return transfer result
262uint8_t tuh_descriptor_get_string_sync(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len);
263
264// Sync (blocking) version of tuh_descriptor_get_manufacturer_string()
265// return transfer result
266uint8_t tuh_descriptor_get_manufacturer_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);
267
268// Sync (blocking) version of tuh_descriptor_get_product_string()
269// return transfer result
270uint8_t tuh_descriptor_get_product_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);
271
272// Sync (blocking) version of tuh_descriptor_get_serial_string()
273// return transfer result
274uint8_t tuh_descriptor_get_serial_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);
275
276#ifdef __cplusplus
277 }
278#endif
279
280#endif
tusb_speed_t
defined base on EHCI specs value for Endpoint Speed
Definition tusb_types.h:48
AUDIO Channel Cluster Descriptor (4.1)
Definition audio.h:647
Definition usbh.h:74
Definition usbh.h:51