1 /**
2   ******************************************************************************
3   * @file    usbh_def.h
4   * @author  MCD Application Team
5   * @version V3.0.0
6   * @date    18-February-2014
7   * @brief   Definitions used in the USB host library
8   ******************************************************************************
9   * @attention
10   *
11   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
12   *
13   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
14   * You may not use this file except in compliance with the License.
15   * You may obtain a copy of the License at:
16   *
17   *        http://www.st.com/software_license_agreement_liberty_v2
18   *
19   * Unless required by applicable law or agreed to in writing, software
20   * distributed under the License is distributed on an "AS IS" BASIS,
21   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   * See the License for the specific language governing permissions and
23   * limitations under the License.
24   *
25   ******************************************************************************
26   */
27 
28 /** @addtogroup USBH_LIB
29   * @{
30   */
31 
32 /** @addtogroup USBH_LIB_CORE
33 * @{
34 */
35 
36 /** @defgroup USBH_DEF
37   * @brief This file is includes USB descriptors
38   * @{
39   */
40 
41 #ifndef  USBH_DEF_H
42 #define  USBH_DEF_H
43 
44 #include "usbh_conf.h"
45 
46 #ifndef NULL
47 #define NULL ((void *)0)
48 #endif
49 
50 #ifndef FALSE
51 #define FALSE 0
52 #endif
53 
54 #ifndef TRUE
55 #define TRUE 1
56 #endif
57 
58 
59 #define ValBit(VAR,POS)                               (VAR & (1 << POS))
60 #define SetBit(VAR,POS)                               (VAR |= (1 << POS))
61 #define ClrBit(VAR,POS)                               (VAR &= ((1 << POS)^255))
62 
63 #define  LE16(addr)             (((uint16_t)(*((uint8_t *)(addr))))\
64                                 + (((uint16_t)(*(((uint8_t *)(addr)) + 1))) << 8))
65 
66 #define  LE16S(addr)              (uint16_t)(LE16((addr)))
67 
68 #define  LE32(addr)              ((((uint32_t)(*(((uint8_t *)(addr)) + 0))) + \
69                                               (((uint32_t)(*(((uint8_t *)(addr)) + 1))) << 8) + \
70                                               (((uint32_t)(*(((uint8_t *)(addr)) + 2))) << 16) + \
71                                               (((uint32_t)(*(((uint8_t *)(addr)) + 3))) << 24)))
72 
73 #define  LE64(addr)              ((((uint64_t)(*(((uint8_t *)(addr)) + 0))) + \
74                                               (((uint64_t)(*(((uint8_t *)(addr)) + 1))) <<  8) +\
75                                               (((uint64_t)(*(((uint8_t *)(addr)) + 2))) << 16) +\
76                                               (((uint64_t)(*(((uint8_t *)(addr)) + 3))) << 24) +\
77                                               (((uint64_t)(*(((uint8_t *)(addr)) + 4))) << 32) +\
78                                               (((uint64_t)(*(((uint8_t *)(addr)) + 5))) << 40) +\
79                                               (((uint64_t)(*(((uint8_t *)(addr)) + 6))) << 48) +\
80                                               (((uint64_t)(*(((uint8_t *)(addr)) + 7))) << 56)))
81 
82 
83 #define  LE24(addr)              ((((uint32_t)(*(((uint8_t *)(addr)) + 0))) + \
84                                               (((uint32_t)(*(((uint8_t *)(addr)) + 1))) << 8) + \
85                                               (((uint32_t)(*(((uint8_t *)(addr)) + 2))) << 16)))
86 
87 
88 #define  LE32S(addr)              (int32_t)(LE32((addr)))
89 
90 
91 
92 #define  USB_LEN_DESC_HDR                               0x02
93 #define  USB_LEN_DEV_DESC                               0x12
94 #define  USB_LEN_CFG_DESC                               0x09
95 #define  USB_LEN_IF_DESC                                0x09
96 #define  USB_LEN_EP_DESC                                0x07
97 #define  USB_LEN_OTG_DESC                               0x03
98 #define  USB_LEN_SETUP_PKT                              0x08
99 
100 /* bmRequestType :D7 Data Phase Transfer Direction  */
101 #define  USB_REQ_DIR_MASK                               0x80
102 #define  USB_H2D                                        0x00
103 #define  USB_D2H                                        0x80
104 
105 /* bmRequestType D6..5 Type */
106 #define  USB_REQ_TYPE_STANDARD                          0x00
107 #define  USB_REQ_TYPE_CLASS                             0x20
108 #define  USB_REQ_TYPE_VENDOR                            0x40
109 #define  USB_REQ_TYPE_RESERVED                          0x60
110 
111 /* bmRequestType D4..0 Recipient */
112 #define  USB_REQ_RECIPIENT_DEVICE                       0x00
113 #define  USB_REQ_RECIPIENT_INTERFACE                    0x01
114 #define  USB_REQ_RECIPIENT_ENDPOINT                     0x02
115 #define  USB_REQ_RECIPIENT_OTHER                        0x03
116 
117 /* Table 9-4. Standard Request Codes  */
118 /* bRequest , Value */
119 #define  USB_REQ_GET_STATUS                             0x00
120 #define  USB_REQ_CLEAR_FEATURE                          0x01
121 #define  USB_REQ_SET_FEATURE                            0x03
122 #define  USB_REQ_SET_ADDRESS                            0x05
123 #define  USB_REQ_GET_DESCRIPTOR                         0x06
124 #define  USB_REQ_SET_DESCRIPTOR                         0x07
125 #define  USB_REQ_GET_CONFIGURATION                      0x08
126 #define  USB_REQ_SET_CONFIGURATION                      0x09
127 #define  USB_REQ_GET_INTERFACE                          0x0A
128 #define  USB_REQ_SET_INTERFACE                          0x0B
129 #define  USB_REQ_SYNCH_FRAME                            0x0C
130 
131 /* Table 9-5. Descriptor Types of USB Specifications */
132 #define  USB_DESC_TYPE_DEVICE                              1
133 #define  USB_DESC_TYPE_CONFIGURATION                       2
134 #define  USB_DESC_TYPE_STRING                              3
135 #define  USB_DESC_TYPE_INTERFACE                           4
136 #define  USB_DESC_TYPE_ENDPOINT                            5
137 #define  USB_DESC_TYPE_DEVICE_QUALIFIER                    6
138 #define  USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION           7
139 #define  USB_DESC_TYPE_INTERFACE_POWER                     8
140 #define  USB_DESC_TYPE_HID                                 0x21
141 #define  USB_DESC_TYPE_HID_REPORT                          0x22
142 
143 
144 #define USB_DEVICE_DESC_SIZE                               18
145 #define USB_CONFIGURATION_DESC_SIZE                        9
146 #define USB_HID_DESC_SIZE                                  9
147 #define USB_INTERFACE_DESC_SIZE                            9
148 #define USB_ENDPOINT_DESC_SIZE                             7
149 
150 /* Descriptor Type and Descriptor Index  */
151 /* Use the following values when calling the function USBH_GetDescriptor  */
152 #define  USB_DESC_DEVICE                    ((USB_DESC_TYPE_DEVICE << 8) & 0xFF00)
153 #define  USB_DESC_CONFIGURATION             ((USB_DESC_TYPE_CONFIGURATION << 8) & 0xFF00)
154 #define  USB_DESC_STRING                    ((USB_DESC_TYPE_STRING << 8) & 0xFF00)
155 #define  USB_DESC_INTERFACE                 ((USB_DESC_TYPE_INTERFACE << 8) & 0xFF00)
156 #define  USB_DESC_ENDPOINT                  ((USB_DESC_TYPE_INTERFACE << 8) & 0xFF00)
157 #define  USB_DESC_DEVICE_QUALIFIER          ((USB_DESC_TYPE_DEVICE_QUALIFIER << 8) & 0xFF00)
158 #define  USB_DESC_OTHER_SPEED_CONFIGURATION ((USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION << 8) & 0xFF00)
159 #define  USB_DESC_INTERFACE_POWER           ((USB_DESC_TYPE_INTERFACE_POWER << 8) & 0xFF00)
160 #define  USB_DESC_HID_REPORT                ((USB_DESC_TYPE_HID_REPORT << 8) & 0xFF00)
161 #define  USB_DESC_HID                       ((USB_DESC_TYPE_HID << 8) & 0xFF00)
162 
163 
164 #define  USB_EP_TYPE_CTRL                               0x00
165 #define  USB_EP_TYPE_ISOC                               0x01
166 #define  USB_EP_TYPE_BULK                               0x02
167 #define  USB_EP_TYPE_INTR                               0x03
168 
169 #define  USB_EP_DIR_OUT                                 0x00
170 #define  USB_EP_DIR_IN                                  0x80
171 #define  USB_EP_DIR_MSK                                 0x80
172 
173 #define USBH_MAX_PIPES_NBR                              15
174 
175 
176 
177 #define USBH_DEVICE_ADDRESS_DEFAULT                     0
178 #define USBH_MAX_ERROR_COUNT                            2
179 #define USBH_DEVICE_ADDRESS                             1
180 
181 
182 /**
183   * @}
184   */
185 
186 
187 #define USBH_CONFIGURATION_DESCRIPTOR_SIZE (USB_CONFIGURATION_DESC_SIZE \
188                                            + USB_INTERFACE_DESC_SIZE\
189                                            + (USBH_MAX_NUM_ENDPOINTS * USB_ENDPOINT_DESC_SIZE))
190 
191 
192 #define CONFIG_DESC_wTOTAL_LENGTH (ConfigurationDescriptorData.ConfigDescfield.\
193                                           ConfigurationDescriptor.wTotalLength)
194 
195 
196 typedef union
197 {
198   uint16_t w;
199   struct BW
200   {
201     uint8_t msb;
202     uint8_t lsb;
203   }
204   bw;
205 }
206 uint16_t_uint8_t;
207 
208 
209 typedef union _USB_Setup
210 {
211   uint32_t d8[2];
212 
213   struct _SetupPkt_Struc
214   {
215     uint8_t           bmRequestType;
216     uint8_t           bRequest;
217     uint16_t_uint8_t  wValue;
218     uint16_t_uint8_t  wIndex;
219     uint16_t_uint8_t  wLength;
220   } b;
221 }
222 USB_Setup_TypeDef;
223 
224 typedef  struct  _DescHeader
225 {
226     uint8_t  bLength;
227     uint8_t  bDescriptorType;
228 }
229 USBH_DescHeader_t;
230 
231 typedef struct _DeviceDescriptor
232 {
233   uint8_t   bLength;
234   uint8_t   bDescriptorType;
235   uint16_t  bcdUSB;        /* USB Specification Number which device complies too */
236   uint8_t   bDeviceClass;
237   uint8_t   bDeviceSubClass;
238   uint8_t   bDeviceProtocol;
239   /* If equal to Zero, each interface specifies its own class
240   code if equal to 0xFF, the class code is vendor specified.
241   Otherwise field is valid Class Code.*/
242   uint8_t   bMaxPacketSize;
243   uint16_t  idVendor;      /* Vendor ID (Assigned by USB Org) */
244   uint16_t  idProduct;     /* Product ID (Assigned by Manufacturer) */
245   uint16_t  bcdDevice;     /* Device Release Number */
246   uint8_t   iManufacturer;  /* Index of Manufacturer String Descriptor */
247   uint8_t   iProduct;       /* Index of Product String Descriptor */
248   uint8_t   iSerialNumber;  /* Index of Serial Number String Descriptor */
249   uint8_t   bNumConfigurations; /* Number of Possible Configurations */
250 }
251 USBH_DevDescTypeDef;
252 
253 typedef struct _EndpointDescriptor
254 {
255   uint8_t   bLength;
256   uint8_t   bDescriptorType;
257   uint8_t   bEndpointAddress;   /* indicates what endpoint this descriptor is describing */
258   uint8_t   bmAttributes;       /* specifies the transfer type. */
259   uint16_t  wMaxPacketSize;    /* Maximum Packet Size this endpoint is capable of sending or receiving */
260   uint8_t   bInterval;          /* is used to specify the polling interval of certain transfers. */
261 }
262 USBH_EpDescTypeDef;
263 
264 typedef struct _InterfaceDescriptor
265 {
266   uint8_t bLength;
267   uint8_t bDescriptorType;
268   uint8_t bInterfaceNumber;
269   uint8_t bAlternateSetting;    /* Value used to select alternative setting */
270   uint8_t bNumEndpoints;        /* Number of Endpoints used for this interface */
271   uint8_t bInterfaceClass;      /* Class Code (Assigned by USB Org) */
272   uint8_t bInterfaceSubClass;   /* Subclass Code (Assigned by USB Org) */
273   uint8_t bInterfaceProtocol;   /* Protocol Code */
274   uint8_t iInterface;           /* Index of String Descriptor Describing this interface */
275   USBH_EpDescTypeDef               Ep_Desc[USBH_MAX_NUM_ENDPOINTS];
276 }
277 USBH_InterfaceDescTypeDef;
278 
279 
280 typedef struct _ConfigurationDescriptor
281 {
282   uint8_t   bLength;
283   uint8_t   bDescriptorType;
284   uint16_t  wTotalLength;        /* Total Length of Data Returned */
285   uint8_t   bNumInterfaces;       /* Number of Interfaces */
286   uint8_t   bConfigurationValue;  /* Value to use as an argument to select this configuration*/
287   uint8_t   iConfiguration;       /*Index of String Descriptor Describing this configuration */
288   uint8_t   bmAttributes;         /* D7 Bus Powered , D6 Self Powered, D5 Remote Wakeup , D4..0 Reserved (0)*/
289   uint8_t   bMaxPower;            /*Maximum Power Consumption */
290   USBH_InterfaceDescTypeDef        Itf_Desc[USBH_MAX_NUM_INTERFACES];
291 }
292 USBH_CfgDescTypeDef;
293 
294 
295 /* Following USB Host status */
296 typedef enum
297 {
298   USBH_OK   = 0,
299   USBH_BUSY,
300   USBH_FAIL,
301   USBH_NOT_SUPPORTED,
302   USBH_UNRECOVERED_ERROR,
303   USBH_ERROR_SPEED_UNKNOWN,
304 }USBH_StatusTypeDef;
305 
306 
307 /** @defgroup USBH_CORE_Exported_Types
308   * @{
309   */
310 
311 typedef enum
312 {
313   USBH_SPEED_HIGH  = 0,
314   USBH_SPEED_FULL  = 1,
315   USBH_SPEED_LOW   = 2,
316 
317 }USBH_SpeedTypeDef;
318 
319 /* Following states are used for gState */
320 typedef enum
321 {
322   HOST_IDLE =0,
323   HOST_DEV_WAIT_FOR_ATTACHMENT,
324   HOST_DEV_ATTACHED,
325   HOST_DEV_DISCONNECTED,
326   HOST_DETECT_DEVICE_SPEED,
327   HOST_ENUMERATION,
328   HOST_CLASS_REQUEST,
329   HOST_INPUT,
330   HOST_SET_CONFIGURATION,
331   HOST_CHECK_CLASS,
332   HOST_CLASS,
333   HOST_SUSPENDED,
334   HOST_ABORT_STATE,
335 }HOST_StateTypeDef;
336 
337 /* Following states are used for EnumerationState */
338 typedef enum
339 {
340   ENUM_IDLE = 0,
341   ENUM_GET_FULL_DEV_DESC,
342   ENUM_SET_ADDR,
343   ENUM_GET_CFG_DESC,
344   ENUM_GET_FULL_CFG_DESC,
345   ENUM_GET_MFC_STRING_DESC,
346   ENUM_GET_PRODUCT_STRING_DESC,
347   ENUM_GET_SERIALNUM_STRING_DESC,
348 } ENUM_StateTypeDef;
349 
350 /* Following states are used for CtrlXferStateMachine */
351 typedef enum
352 {
353   CTRL_IDLE =0,
354   CTRL_SETUP,
355   CTRL_SETUP_WAIT,
356   CTRL_DATA_IN,
357   CTRL_DATA_IN_WAIT,
358   CTRL_DATA_OUT,
359   CTRL_DATA_OUT_WAIT,
360   CTRL_STATUS_IN,
361   CTRL_STATUS_IN_WAIT,
362   CTRL_STATUS_OUT,
363   CTRL_STATUS_OUT_WAIT,
364   CTRL_ERROR,
365   CTRL_STALLED,
366   CTRL_COMPLETE
367 }CTRL_StateTypeDef;
368 
369 
370 /* Following states are used for RequestState */
371 typedef enum
372 {
373   CMD_IDLE =0,
374   CMD_SEND,
375   CMD_WAIT
376 } CMD_StateTypeDef;
377 
378 typedef enum {
379   USBH_URB_IDLE = 0,
380   USBH_URB_DONE,
381   USBH_URB_NOTREADY,
382   USBH_URB_NYET,
383   USBH_URB_ERROR,
384   USBH_URB_STALL
385 }USBH_URBStateTypeDef;
386 
387 typedef enum
388 {
389   USBH_PORT_EVENT = 1,
390   USBH_URB_EVENT,
391   USBH_CONTROL_EVENT,
392   USBH_CLASS_EVENT,
393   USBH_STATE_CHANGED_EVENT,
394 }
395 USBH_OSEventTypeDef;
396 
397 /* Control request structure */
398 typedef struct
399 {
400   uint8_t               pipe_in;
401   uint8_t               pipe_out;
402   uint8_t               pipe_size;
403   uint8_t               *buff;
404   uint16_t              length;
405   uint16_t              timer;
406   USB_Setup_TypeDef     setup;
407   CTRL_StateTypeDef     state;
408   uint8_t               errorcount;
409 
410 } USBH_CtrlTypeDef;
411 
412 /* Attached device structure */
413 typedef struct
414 {
415 #if (USBH_KEEP_CFG_DESCRIPTOR == 1)
416   uint8_t                           CfgDesc_Raw[USBH_MAX_SIZE_CONFIGURATION];
417 #endif
418   uint8_t                           Data[USBH_MAX_DATA_BUFFER];
419   uint8_t                           address;
420   uint8_t                           speed;
421   __IO uint8_t                      is_connected;
422   uint8_t                           current_interface;
423   USBH_DevDescTypeDef               DevDesc;
424   USBH_CfgDescTypeDef               CfgDesc;
425 
426 }USBH_DeviceTypeDef;
427 
428 struct _USBH_HandleTypeDef;
429 
430 /* USB Host Class structure */
431 typedef struct
432 {
433   const char          *Name;
434   uint8_t              ClassCode;
435   USBH_StatusTypeDef  (*Init)        (struct _USBH_HandleTypeDef *phost);
436   USBH_StatusTypeDef  (*DeInit)      (struct _USBH_HandleTypeDef *phost);
437   USBH_StatusTypeDef  (*Requests)    (struct _USBH_HandleTypeDef *phost);
438   USBH_StatusTypeDef  (*BgndProcess) (struct _USBH_HandleTypeDef *phost);
439   USBH_StatusTypeDef  (*SOFProcess) (struct _USBH_HandleTypeDef *phost);
440   void*                pData;
441 } USBH_ClassTypeDef;
442 
443 /* USB Host handle structure */
444 typedef struct _USBH_HandleTypeDef
445 {
446   __IO HOST_StateTypeDef     gState;       /*  Host State Machine Value */
447   ENUM_StateTypeDef     EnumState;    /* Enumeration state Machine */
448   CMD_StateTypeDef      RequestState;
449   USBH_CtrlTypeDef      Control;
450   USBH_DeviceTypeDef    device;
451   USBH_ClassTypeDef*    pClass[USBH_MAX_NUM_SUPPORTED_CLASS];
452   USBH_ClassTypeDef*    pActiveClass;
453   uint32_t              ClassNumber;
454   uint32_t              Pipes[15];
455   __IO uint32_t         Timer;
456   uint8_t               id;
457   void*                 pData;
458   void                 (* pUser )(struct _USBH_HandleTypeDef *pHandle, uint8_t id);
459 
460 #if (USBH_USE_OS == 1)
461   osMessageQId          os_event;
462   osThreadId            thread;
463 #endif
464 
465 } USBH_HandleTypeDef;
466 
467 
468 #if  defined ( __GNUC__ )
469   #ifndef __weak
470     #define __weak   __attribute__((weak))
471   #endif /* __weak */
472   #ifndef __packed
473     #define __packed __attribute__((__packed__))
474   #endif /* __packed */
475 #endif /* __GNUC__ */
476 
477 #endif
478 
479 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
480 
481