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