USB Component  Version 5.1
MDK-Professional Middleware for USB Device and Host
 All Data Structures Functions Variables Enumerations Enumerator Groups Pages
rl_usbh.h
1 /*------------------------------------------------------------------------------
2  * MDK Middleware - Component ::USB:Host
3  * Copyright (c) 2004-2013 ARM Germany GmbH. All rights reserved.
4  *------------------------------------------------------------------------------
5  * Name: rl_usbh.h
6  * Purpose: USB Host header file
7  * Rev.: V5.10
8  *----------------------------------------------------------------------------*/
9 
10 #ifndef __RL_USBH_H__
11 #define __RL_USBH_H__
12 
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include "cmsis_os.h"
16 #include "rl_usb.h"
17 #include "usb_def.h"
18 #include "Driver_USBH.h"
19 
20 
21 // ==== USB Host Constants and Defines ====
22 
24 #ifdef __BIG_ENDIAN
25  #define U32_LE(v) (uint32_t)(__rev(v))
26  #define U16_LE(v) (uint16_t)(__rev(v) >> 16)
27  #define U32_BE(v) (uint32_t)(v)
28  #define U16_BE(v) (uint16_t)(v)
29 #else
30  #define U32_BE(v) (uint32_t)(__rev(v))
31  #define U16_BE(v) (uint16_t)(__rev(v) >> 16)
32  #define U32_LE(v) (uint32_t)(v)
33  #define U16_LE(v) (uint16_t)(v)
34 #endif
35 
37 #define PREPARE_SETUP_PACKET(ptr_urb, ptr_data) \
38  ptr_urb->buf = ptr_data; \
39  ptr_urb->len = 8; \
40  ptr_urb->packet_type = USB_PID_SETUP; \
41  ptr_urb->toggle_bit = 0; \
42  ptr_urb->toggle_force = 1;
43 
45 #define PREPARE_SETUP_PACKET_DATA(ptr_sp, dir, typ, rcpnt, req, val, idx, len) \
46  ptr_sp->bmRequestType.Dir = dir; \
47  ptr_sp->bmRequestType.Type = typ; \
48  ptr_sp->bmRequestType.Recipient = rcpnt; \
49  ptr_sp->bRequest = req; \
50  ptr_sp->wValue = U16_LE(val); \
51  ptr_sp->wIndex = U16_LE(idx); \
52  ptr_sp->wLength = U16_LE(len);
53 
55 #define PREPARE_IN_DATA1_PACKET(ptr_urb, ptr_data, data_len) \
56  ptr_urb->buf = ptr_data; \
57  ptr_urb->len = data_len; \
58  ptr_urb->packet_type = USB_PID_IN; \
59  ptr_urb->toggle_bit = 1; \
60  ptr_urb->toggle_force = 1;
61 
63 #define PREPARE_IN_DATAx_PACKET(ptr_urb, ptr_data, data_len) \
64  ptr_urb->buf = ptr_data; \
65  ptr_urb->len = data_len; \
66  ptr_urb->packet_type = USB_PID_IN; \
67  ptr_urb->toggle_force = 0;
68 
70 #define PREPARE_OUT_DATA1_PACKET(ptr_urb, ptr_data, data_len) \
71  ptr_urb->buf = ptr_data; \
72  ptr_urb->len = data_len; \
73  ptr_urb->packet_type = USB_PID_OUT; \
74  ptr_urb->toggle_bit = 1; \
75  ptr_urb->toggle_force = 1;
76 
78 #define PREPARE_OUT_DATAx_PACKET(ptr_urb, ptr_data, data_len) \
79  ptr_urb->buf = ptr_data; \
80  ptr_urb->len = data_len; \
81  ptr_urb->packet_type = USB_PID_OUT; \
82  ptr_urb->toggle_force = 0;
83 
85 #define PREPARE_PING_PACKET(ptr_urb) \
86  ptr_urb->len = 0; \
87  ptr_urb->packet_type = USB_PID_PING; \
88  ptr_urb->toggle_force = 0;
89 
91 #define PREPARE_MSC_CBW(ptr_cbw, sig, tag, len, flg, lun, cb_len, cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9, cb10, cb11, cb12, cb13, cb14, cb15)\
92  ptr_cbw->dSignature = U32_LE(sig); \
93  ptr_cbw->dTag = U32_LE(++tag); \
94  ptr_cbw->dDataLength = len; \
95  ptr_cbw->bmFlags = flg; \
96  ptr_cbw->bLUN = lun; \
97  ptr_cbw->bCBLength = cb_len; \
98  ptr_cbw->CB[0] = cb0; \
99  ptr_cbw->CB[1] = cb1; \
100  ptr_cbw->CB[2] = cb2; \
101  ptr_cbw->CB[3] = cb3; \
102  ptr_cbw->CB[4] = cb4; \
103  ptr_cbw->CB[5] = cb5; \
104  ptr_cbw->CB[6] = cb6; \
105  ptr_cbw->CB[7] = cb7; \
106  ptr_cbw->CB[8] = cb8; \
107  ptr_cbw->CB[9] = cb9; \
108  ptr_cbw->CB[10] = cb10; \
109  ptr_cbw->CB[11] = cb11; \
110  ptr_cbw->CB[12] = cb12; \
111  ptr_cbw->CB[13] = cb13; \
112  ptr_cbw->CB[14] = cb14; \
113  ptr_cbw->CB[15] = cb15;
114 
115 
116 // ==== USB Host Structures ====
117 
118 /* ------------------- Start of section using anonymous unions ------------------ */
119 #if defined(__CC_ARM)
120  #pragma push
121  #pragma anon_unions
122 #elif defined(__ICCARM__)
123  #pragma language=extended
124 #elif defined(__GNUC__)
125  /* anonymous unions are enabled by default */
126 #elif defined(__TMS470__)
127  /* anonymous unions are enabled by default */
128 #elif defined(__TASKING__)
129  #pragma warning 586
130 #else
131  #warning Not supported compiler type
132 #endif
133 
135 typedef struct {
136  osThreadId thread_id;
137  ARM_USBH_URB urb;
138  ARM_USBH_EP_HANDLE handle;
139  __packed struct {
140  uint8_t address : 8;
141  uint8_t speed : 8;
142  };
143  USB_ENDPOINT_DESCRIPTOR descriptor;
144 } USBH_EP;
145 
147 typedef struct _USBH_OHCI_t {
148  uint32_t *ptr_OHCI;
149  uint32_t port_mask;
150  uint16_t max_ED;
151  uint16_t max_TD;
152  uint16_t max_ITD;
153  uint32_t *ptr_HCCA;
154  uint32_t *ptr_ED;
155  uint32_t *ptr_TD;
156  uint32_t *ptr_ITD;
157  uint32_t *ptr_TDURB;
158 } const USBH_OHCI_t;
159 
161 typedef struct _USBH_EHCI_t {
162  uint32_t *ptr_EHCI;
163  uint32_t port_mask;
164  uint16_t max_qH;
165  uint16_t max_qTD;
166  uint16_t max_iTD;
167  uint16_t max_siTD;
168  uint16_t max_FSTN;
169  uint32_t *ptr_PFL;
170  uint32_t *ptr_qH;
171  uint32_t *ptr_qTD;
172  uint32_t *ptr_iTD;
173  uint32_t *ptr_siTD;
174  uint32_t *ptr_FSTN;
175  uint32_t *ptr_qTDURB;
176 } const USBH_EHCI_t;
177 
179 typedef struct {
180  uint32_t port_con_phy;
181  uint32_t port_con_phy_ex;
182  uint32_t port_con;
183  uint32_t port_act;
184  uint32_t port_oc;
187  uint8_t last_dev_addr;
188  uint32_t dev_addr_mask[4];
190  usbStatus last_error;
191 } USBH_HCI;
192 
194 typedef struct {
195  uint8_t class;
196  uint8_t subclass;
197  uint8_t protocol;
198  uint8_t ctrl;
199  uint8_t port;
200  uint8_t address;
201  uint8_t speed;
202  struct {
203  uint8_t configured : 1;
204  uint8_t initialized : 1;
205  };
206  usbStatus last_error;
207 } USBH_DEV;
208 
210 typedef struct {
214  uint8_t do_ping;
215  uint8_t max_lun;
216  uint32_t tag;
217  bool media_ok;
218 } USBH_MSC;
219 
221 typedef struct {
223  ARM_USBH_URB int_in_urb;
224  ARM_USBH_URB int_out_urb;
227  uint8_t *ptr_int_in_buf;
229  uint16_t report_desc_len;
230  uint8_t report_in_data_buf[8];
233 } USBH_HID;
234 
236 typedef struct {
237  USBH_DEV *ptr_dev;
238 
239 } USBH_CLS;
240 
242 typedef struct {
243  uint8_t ClassID;
244 
245  int8_t (*Configure ) (uint8_t ctrl, uint8_t port, uint8_t speed, int8_t address, USB_CONFIGURATION_DESCRIPTOR *ptr_cfg_desc);
246  usbStatus (*Unconfigure ) (int8_t instance);
247  usbStatus (*Initialize ) (int8_t instance);
248  usbStatus (*Uninitialize) (int8_t instance);
249 } USBH_DCD;
250 
251 /* -------------------- End of section using anonymous unions ------------------- */
252 #if defined(__CC_ARM)
253  #pragma pop
254 #elif defined(__ICCARM__)
255  /* leave anonymous unions enabled */
256 #elif defined(__GNUC__)
257  /* anonymous unions are enabled by default */
258 #elif defined(__TMS470__)
259  /* anonymous unions are enabled by default */
260 #elif defined(__TASKING__)
261  #pragma warning restore
262 #else
263  #warning Not supported compiler type
264 #endif
265 
266 
267 // ==== USB Host Library Functions ====
268 
273 extern usbStatus USBH_SendSetup (uint8_t ctrl, uint8_t *ptr_data);
274 
280 extern usbStatus USBH_SendData (uint8_t ctrl, uint8_t *ptr_data, uint32_t data_len);
281 
287 extern usbStatus USBH_ReceiveData (uint8_t ctrl, uint8_t *ptr_data, uint32_t data_len);
288 
295 extern usbStatus USBH_GetStatus (uint8_t ctrl, uint8_t recipient, uint8_t index, uint8_t *ptr_stat_dat);
296 
303 extern usbStatus USBH_ClearFeature (uint8_t ctrl, uint8_t recipient, uint8_t index, uint8_t feature_selector);
304 
311 extern usbStatus USBH_SetFeature (uint8_t ctrl, uint8_t recipient, uint8_t index, uint8_t feature_selector);
312 
317 extern usbStatus USBH_SetAddress (uint8_t ctrl, uint8_t device_address);
318 
328 extern usbStatus USBH_GetDescriptor (uint8_t ctrl, uint8_t recipient, uint8_t descriptor_type, uint8_t descriptor_index, uint8_t language_id, uint8_t *descriptor_data, uint16_t descriptor_length);
329 
339 extern usbStatus USBH_SetDescriptor (uint8_t ctrl, uint8_t recipient, uint8_t descriptor_type, uint8_t descriptor_index, uint8_t language_id, uint8_t *descriptor_data, uint16_t descriptor_length);
340 
345 extern usbStatus USBH_GetConfiguration (uint8_t ctrl, uint8_t *ptr_configuration);
346 
351 extern usbStatus USBH_SetConfiguration (uint8_t ctrl, uint8_t configuration);
352 
358 extern usbStatus USBH_GetInterface (uint8_t ctrl, uint8_t idx, uint8_t *ptr_alternate);
359 
365 extern usbStatus USBH_SetInterface (uint8_t ctrl, uint8_t index, uint8_t alternate);
366 
372 extern usbStatus USBH_SyncFrame (uint8_t ctrl, uint8_t index, uint8_t *ptr_frame_number);
373 
379 extern usbStatus USBH_MemoryAllocate (uint8_t ctrl, uint8_t **ptr, uint32_t size);
380 
385 extern usbStatus USBH_MemoryFree (uint8_t ctrl, uint8_t *ptr);
386 
392 extern usbStatus USBH_Transfer (uint8_t ctrl, USBH_EP *ptr_ep, uint32_t timeout);
393 
398 extern void USBH_HID_ParseReportDescriptor (int8_t instance, uint8_t *ptr_hid_report_desc);
399 
404 extern void USBH_HID_DataReceived (int8_t instance, uint16_t len);
405 
406 #endif