mikroSDK Reference Manual
hcd.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_HCD_H_
28#define _TUSB_HCD_H_
29
30#include "common/tusb_common.h"
31#include "osal/osal.h"
32#include "common/tusb_fifo.h"
33
34#ifdef __cplusplus
35 extern "C" {
36#endif
37
38//--------------------------------------------------------------------+
39// Configuration
40//--------------------------------------------------------------------+
41
42// Max number of endpoints per device
43// TODO optimize memory usage
44#ifndef CFG_TUH_ENDPOINT_MAX
45 #define CFG_TUH_ENDPOINT_MAX 16
46// #ifdef TUP_HCD_ENDPOINT_MAX
47// #define CFG_TUH_ENDPPOINT_MAX TUP_HCD_ENDPOINT_MAX
48// #else
49// #define
50// #endif
51#endif
52
53//--------------------------------------------------------------------+
54// MACRO CONSTANT TYPEDEF
55//--------------------------------------------------------------------+
56typedef enum
57{
58 HCD_EVENT_DEVICE_ATTACH,
59 HCD_EVENT_DEVICE_REMOVE,
60 HCD_EVENT_XFER_COMPLETE,
61
62 // Not an HCD event, just a convenient way to defer ISR function
63 USBH_EVENT_FUNC_CALL,
64
65 HCD_EVENT_COUNT
66} hcd_eventid_t;
67
68typedef struct
69{
70 uint8_t rhport;
71 uint8_t event_id;
72 uint8_t dev_addr;
73
74 union
75 {
76 // Attach, Remove
77 struct {
78 uint8_t hub_addr;
79 uint8_t hub_port;
80 uint8_t speed;
81 } connection;
82
83 // XFER_COMPLETE
84 struct {
85 uint8_t ep_addr;
86 uint8_t result;
87 uint32_t len;
88 } xfer_complete;
89
90 // FUNC_CALL
91 struct {
92 void (*func) (void*);
93 void* param;
94 }func_call;
95 };
96
98
99typedef struct
100{
101 uint8_t rhport;
102 uint8_t hub_addr;
103 uint8_t hub_port;
104 uint8_t speed;
106
107//--------------------------------------------------------------------+
108// Memory API
109//--------------------------------------------------------------------+
110
111// clean/flush data cache: write cache -> memory.
112// Required before an DMA TX transfer to make sure data is in memory
113bool hcd_dcache_clean(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
114
115// invalidate data cache: mark cache as invalid, next read will read from memory
116// Required BOTH before and after an DMA RX transfer
117bool hcd_dcache_invalidate(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
118
119// clean and invalidate data cache
120// Required before an DMA transfer where memory is both read/write by DMA
121bool hcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
122
123//--------------------------------------------------------------------+
124// Controller API
125//--------------------------------------------------------------------+
126
127// optional hcd configuration, called by tuh_configure()
128bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param) TU_ATTR_WEAK;
129
130// Initialize controller to host mode
131bool hcd_init(uint8_t rhport);
132
133// Interrupt Handler
134void hcd_int_handler(uint8_t rhport);
135
136// Enable USB interrupt
137void hcd_int_enable (uint8_t rhport);
138
139// Disable USB interrupt
140void hcd_int_disable(uint8_t rhport);
141
142// Get frame number (1ms)
143uint32_t hcd_frame_number(uint8_t rhport);
144
145//--------------------------------------------------------------------+
146// Port API
147//--------------------------------------------------------------------+
148
149// Get the current connect status of roothub port
150bool hcd_port_connect_status(uint8_t rhport);
151
152// Reset USB bus on the port
153void hcd_port_reset(uint8_t rhport);
154
155// TODO implement later
156void hcd_port_reset_end(uint8_t rhport);
157
158// Get port link speed
159tusb_speed_t hcd_port_speed_get(uint8_t rhport);
160
161// HCD closes all opened endpoints belong to this device
162void hcd_device_close(uint8_t rhport, uint8_t dev_addr);
163
164//--------------------------------------------------------------------+
165// Endpoints API
166//--------------------------------------------------------------------+
167
168// Open an endpoint
169bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc);
170
171// Submit a transfer, when complete hcd_event_xfer_complete() must be invoked
172bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen);
173
174// Abort a queued transfer. Note: it can only abort transfer that has not been started
175// Return true if a queued transfer is aborted, false if there is no transfer to abort
176bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr);
177
178// Submit a special transfer to send 8-byte Setup Packet, when complete hcd_event_xfer_complete() must be invoked
179bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]);
180
181// clear stall, data toggle is also reset to DATA0
182bool hcd_edpt_clear_stall(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr);
183
184//--------------------------------------------------------------------+
185// USBH implemented API
186//--------------------------------------------------------------------+
187
188// Get device tree information of a device
189// USB device tree can be complicated and manged by USBH, this help HCD to retrieve
190// needed topology info to carry out its work
191extern void hcd_devtree_get_info(uint8_t dev_addr, hcd_devtree_info_t* devtree_info);
192
193//------------- Event API -------------//
194
195// Called by HCD to notify stack
196extern void hcd_event_handler(hcd_event_t const* event, bool in_isr);
197
198// Helper to send device attach event
199TU_ATTR_ALWAYS_INLINE static inline
200void hcd_event_device_attach(uint8_t rhport, bool in_isr)
201{
202 hcd_event_t event;
203 event.rhport = rhport;
204 event.event_id = HCD_EVENT_DEVICE_ATTACH;
205 event.connection.hub_addr = 0;
206 event.connection.hub_port = 0;
207
208 hcd_event_handler(&event, in_isr);
209}
210
211// Helper to send device removal event
212TU_ATTR_ALWAYS_INLINE static inline
213void hcd_event_device_remove(uint8_t rhport, bool in_isr)
214{
215 hcd_event_t event;
216 event.rhport = rhport;
217 event.event_id = HCD_EVENT_DEVICE_REMOVE;
218 event.connection.hub_addr = 0;
219 event.connection.hub_port = 0;
220
221 hcd_event_handler(&event, in_isr);
222}
223
224// Helper to send USB transfer event
225TU_ATTR_ALWAYS_INLINE static inline
226void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t xferred_bytes, xfer_result_t result, bool in_isr)
227{
228 hcd_event_t event =
229 {
230 .rhport = 0, // TODO correct rhport
231 .event_id = HCD_EVENT_XFER_COMPLETE,
232 .dev_addr = dev_addr,
233 };
234 event.xfer_complete.ep_addr = ep_addr;
235 event.xfer_complete.result = result;
236 event.xfer_complete.len = xferred_bytes;
237
238 hcd_event_handler(&event, in_isr);
239}
240
241#ifdef __cplusplus
242 }
243#endif
244
245#endif /* _TUSB_HCD_H_ */
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 hcd.h:100
Definition hcd.h:69