-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathteeny_usb_host.h
More file actions
253 lines (220 loc) · 9.13 KB
/
teeny_usb_host.h
File metadata and controls
253 lines (220 loc) · 9.13 KB
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
/*
* _______ _ _ _____ ____
* |__ __| | | | |/ ____| _ \
* | | ___ ___ _ __ _ _| | | | (___ | |_) |
* | |/ _ \/ _ \ '_ \| | | | | | |\___ \| _ <
* | | __/ __/ | | | |_| | |__| |____) | |_) |
* |_|\___|\___|_| |_|\__, |\____/|_____/|____/
* __/ |
* |___/
*
* TeenyUSB - light weight usb stack for micro controllers
*
* Copyright (c) 2020 XToolBox - admin@xtoolbox.org
* www.tusb.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __TEENY_USB_HOST_H__
#define __TEENY_USB_HOST_H__
#include "stdint.h"
#include "teeny_usb_config.h"
#include "teeny_usb_def.h"
#include "teeny_usb_host_driver.h"
/** Type define for USB host */
typedef struct _tusbh_periodic_xfer tusbh_periodic_xfer_t;
typedef struct _tusb_host{
tusb_host_driver_t* host_drv; /**< host driver */
void* user_data; /**< User data for host */
tusbh_periodic_xfer_t* periodic_queue; /**< periodic xfer queue */
uint16_t last_frame; /**< last frame number for periodic xfer */
uint16_t periodic_pending; /**< periodic action pending count */
}tusb_host_t;
typedef struct _tusbh_transfer tusbh_transfer_t;
typedef struct _tusbh_drv_xfer_data tusbh_drv_xfer_data_t;
#define SPLIT_ISO_MIDDLE 0x00
#define SPLIT_ISO_BEGIN 0x02
#define SPLIT_ISO_END 0x01
#define SPLIT_ISO_FULL 0x03
typedef enum
{
TS_IDLE = 0,
TS_ACK,
TS_NAK,
TS_NYET,
TS_STALL,
TS_ERROR,
TS_TIMEOUT,
TS_NO_CHANNEL,
} tusb_transfer_state_t;
struct _tusbh_transfer{
void* data; /**< transfer data pointer */
uint32_t total_length; /**< total data length */
uint32_t actual_length; /**< actual transfer data length */
tusbh_drv_xfer_data_t* drv_data; /**< transfer data used by driver */
void* context; /**< user data */
void (*cb_complete)(tusbh_transfer_t*); /**< transfer complete callback */
uint8_t hub_addr; /**< hub address for split transfer */
uint8_t hub_port; /**< hub port for split transfer */
uint8_t is_split:1; /**< is split transfer or not */
uint8_t is_sc:1; /**< is split complete transfer or not */
uint8_t is_setup:1; /**< is setup packet or not */
uint8_t st_stage:5; /**< split trasnfer stage */
uint8_t split_iso_type; /**< split ISO out transfer type,
SPLIT_ISO_MIDDLE,SPLIT_ISO_BEGIN,SPLIT_ISO_END,SPLIT_ISO_FULL */
uint8_t dev_addr; /**< device address */
uint8_t dev_speed; /**< device speed, PORT_SPEED_HIGH, PORT_SPEED_FULL, PORT_SPEED_LOW */
uint8_t ep_num; /**< endpoint number, bit0-6: endpoint address, bit 7: direction*/
uint8_t ep_type; /**< endpoint type,
USBD_EP_TYPE_CTRL, USBD_EP_TYPE_ISOC, USBD_EP_TYPE_BULK, USBD_EP_TYPE_INTR*/
uint16_t ep_mps; /**< endpoint max packet size */
uint16_t padding2; /**< padding to 32bit boundary */
tusb_transfer_state_t state; /**< transfer state */
};
/* Enums: */
/** Enums for \ref tusb_host_port_changed new_state parameter
* indicating the port state
*/
typedef enum {
TUSB_HOST_PORT_DUMMY = 0, /**< Initial state */
TUSB_HOST_PORT_DISCONNECTED, /**< Host port is disconnected */
TUSB_HOST_PORT_CONNECTED, /**< Host port connected */
TUSB_HOST_PORT_ENABLED, /**< Host port enabled, this is done by fire a reset when in connected state */
TUSB_HOST_PORT_DISABLED, /**< Host port disabled */
}host_port_state_t;
//////////////////////////////////////////////
// Host functions, used in host only mode
//////////////////////////////////////////////
/** Open USB host
*
* \ingroup Group_Device
*
* \param[in] host USB host handle
* \param[in] driver_param parameter pass to the driver
*
* \return 0 for success, otherwise fail
*/
int tusb_open_host(tusb_host_t* host, const tusb_hardware_param_t* driver_param);
/** Close USB host
*
* \ingroup Group_Host
*
* \param host USB host handle, init by \ref tusb_open_host
*
* \return 0 for success, otherwise fail
*/
static inline int tusb_close_host(tusb_host_t* host)
{ return tusb_host_drv_close(host->host_drv); }
/** Reset USB host port
*
* \ingroup Group_Host
*
* \param host USB host handle, init by \ref tusb_open_host
* \param port port id
*
* \return 0 for success, otherwise fail
*/
static inline int tusb_host_reset(tusb_host_t* host, int port)
{ return tusb_host_drv_reset(host->host_drv, port); }
/** Get USB host port speed
*
* \ingroup Group_Host
*
* \param[in] host USB host handle, init by \ref tusb_open_host
* \param[in] port port id
*
* \return Port attached device speed: \ref PORT_SPEED_HIGH, \ref PORT_SPEED_FULL, \ref PORT_SPEED_LOW
*/
static inline uint8_t tusb_host_get_speed(tusb_host_t* host, uint8_t port)
{ return tusb_host_drv_get_speed(host->host_drv, port); }
/** Get USB host frame number
*
* \ingroup Group_Host
*
* \param[in] host USB host handle, init by \ref tusb_open_host
*
* \return current frame number
*/
static inline uint32_t tusb_host_get_frame_number(tusb_host_t* host)
{ return tusb_host_drv_get_frame_number(host->host_drv); }
/** Add a transfer to host process queue
*
* \ingroup Group_Host
*
* \param[in] host USB host handle, init by \ref tusb_open_host
* \param[in] transfer transfer info
*
* \return 0 success, other error
*
* \remark we can add a same transfer more than one time, if we don't remove it, the driver
* will try to use the same transfer pipe
* e.g.
* transfer = malloc(...);
* fill_the_transfer
* tusb_host_transfer_add(transfer)
* // add again without remove, driver will use the same pipe
* tusb_host_transfer_add(transfer)
* // remove the transfer
* tusb_host_transfer_remove(transfer)
* re fill_the_transfer
* // driver may use new pipe to handle the transfer
* tusb_host_transfer_add(transfer)
* // free the transfer from driver
* tusb_host_transfer_remove(transfer)
*
*/
static inline int tusb_host_transfer_add(tusb_host_t* host, tusbh_transfer_t* transfer)
{ return tusb_host_drv_transfer_add(host->host_drv, transfer); }
/** Remove a transfer from host process queue
*
* \ingroup Group_Host
*
* \param[in] host USB host handle, init by \ref tusb_open_host
* \param[in] transfer transfer info
*
* \return 0 success, other error
*/
static inline int tusb_host_transfer_remove(tusb_host_t* host, tusbh_transfer_t* transfer)
{ return tusb_host_drv_transfer_remove(host->host_drv, transfer); }
//////////////////////////////////
// host callback functions
//////////////////////////////////
/** Called by driver when host port state changed
*
* \ingroup Group_Host
* \ingroup Group_Diver_Event
*
* \param[in] drv USB host driver handle
* \param[in] port port number
* \param[in] new_state new port state
*
* \return 0 for success, other fail
*/
int tusb_host_port_changed(tusb_host_driver_t* drv, int port, host_port_state_t new_state);
/** Called by driver when SOF event fired
*
* \ingroup Group_Host
* \ingroup Group_Diver_Event
*
* \param[in] drv USB host driver handle
*
* \return 0 for success, other fail
*/
int tusb_host_sof_event(tusb_host_driver_t* drv);
#endif