1 /**
2   **************************************************************************
3   * @file     usb_std.h
4   * @brief    usb standard header file
5   **************************************************************************
6   *                       Copyright notice & Disclaimer
7   *
8   * The software Board Support Package (BSP) that is made available to
9   * download from Artery official website is the copyrighted work of Artery.
10   * Artery authorizes customers to use, copy, and distribute the BSP
11   * software and its related documentation for the purpose of design and
12   * development in conjunction with Artery microcontrollers. Use of the
13   * software is governed by this copyright notice and the following disclaimer.
14   *
15   * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
16   * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
17   * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
18   * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
19   * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
21   *
22   **************************************************************************
23   */
24 
25 /* define to prevent recursive inclusion -------------------------------------*/
26 #ifndef __USB_STD_H
27 #define __USB_STD_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /* includes ------------------------------------------------------------------*/
34 #include "drv_config.h"
35 
36 /** @addtogroup USB_standard
37   * @{
38   */
39 
40 /** @defgroup USB_standard_define
41   * @{
42   */
43 
44 /**
45   * @brief usb request recipient
46   */
47 #define USB_REQ_RECIPIENT_DEVICE         0x00 /*!< usb request recipient device */
48 #define USB_REQ_RECIPIENT_INTERFACE      0x01 /*!< usb request recipient interface */
49 #define USB_REQ_RECIPIENT_ENDPOINT       0x02 /*!< usb request recipient endpoint */
50 #define USB_REQ_RECIPIENT_OTHER          0x03 /*!< usb request recipient other */
51 #define USB_REQ_RECIPIENT_MASK           0x1F /*!< usb request recipient mask */
52 
53 /**
54   * @brief usb request type
55   */
56 #define USB_REQ_TYPE_STANDARD            0x00 /*!< usb request type standard */
57 #define USB_REQ_TYPE_CLASS               0x20 /*!< usb request type class */
58 #define USB_REQ_TYPE_VENDOR              0x40 /*!< usb request type vendor */
59 #define USB_REQ_TYPE_RESERVED            0x60 /*!< usb request type reserved */
60 
61 /**
62   * @brief usb request data transfer direction
63   */
64 #define USB_REQ_DIR_HTD                  0x00 /*!< usb request data transfer direction host to device */
65 #define USB_REQ_DIR_DTH                  0x80 /*!< usb request data transfer direction device to host */
66 
67 /**
68   * @brief usb standard device requests codes
69   */
70 #define USB_STD_REQ_GET_STATUS           0 /*!< usb request code status */
71 #define USB_STD_REQ_CLEAR_FEATURE        1 /*!< usb request code clear feature */
72 #define USB_STD_REQ_SET_FEATURE          3 /*!< usb request code feature */
73 #define USB_STD_REQ_SET_ADDRESS          5 /*!< usb request code address */
74 #define USB_STD_REQ_GET_DESCRIPTOR       6 /*!< usb request code get descriptor */
75 #define USB_STD_REQ_SET_DESCRIPTOR       7 /*!< usb request code set descriptor */
76 #define USB_STD_REQ_GET_CONFIGURATION    8 /*!< usb request code get configuration */
77 #define USB_STD_REQ_SET_CONFIGURATION    9 /*!< usb request code set configuration */
78 #define USB_STD_REQ_GET_INTERFACE        10 /*!< usb request code get interface */
79 #define USB_STD_REQ_SET_INTERFACE        11 /*!< usb request code set interface */
80 #define USB_STD_REQ_SYNCH_FRAME          12 /*!< usb request code synch frame */
81 
82 /**
83   * @brief usb standard device type
84   */
85 #define USB_DESCIPTOR_TYPE_DEVICE        1 /*!< usb standard device type device */
86 #define USB_DESCIPTOR_TYPE_CONFIGURATION 2 /*!< usb standard device type configuration */
87 #define USB_DESCIPTOR_TYPE_STRING        3 /*!< usb standard device type string */
88 #define USB_DESCIPTOR_TYPE_INTERFACE     4 /*!< usb standard device type interface */
89 #define USB_DESCIPTOR_TYPE_ENDPOINT      5 /*!< usb standard device type endpoint */
90 #define USB_DESCIPTOR_TYPE_DEVICE_QUALIFIER     6 /*!< usb standard device type qualifier */
91 #define USB_DESCIPTOR_TYPE_OTHER_SPEED   7 /*!< usb standard device type other speed */
92 #define USB_DESCIPTOR_TYPE_INTERFACE_POWER       8 /*!< usb standard device type interface power */
93 
94 /**
95   * @brief usb standard string type
96   */
97 #define  USB_LANGID_STRING               0 /*!< usb standard string type lang id */
98 #define  USB_MFC_STRING                  1 /*!< usb standard string type mfc */
99 #define  USB_PRODUCT_STRING              2 /*!< usb standard string type product */
100 #define  USB_SERIAL_STRING               3 /*!< usb standard string type serial */
101 #define  USB_CONFIG_STRING               4 /*!< usb standard string type config */
102 #define  USB_INTERFACE_STRING            5 /*!< usb standard string type interface */
103 
104 /**
105   * @brief usb configuration attributes
106   */
107 #define USB_CONF_REMOTE_WAKEUP           2 /*!< usb configuration attributes remote wakeup */
108 #define USB_CONF_SELF_POWERED            1 /*!< usb configuration attributes self powered */
109 
110 /**
111   * @brief usb standard feature selectors
112   */
113 #define USB_FEATURE_EPT_HALT             0 /*!< usb standard feature selectors endpoint halt */
114 #define USB_FEATURE_REMOTE_WAKEUP        1 /*!< usb standard feature selectors remote wakeup */
115 
116 /**
117   * @brief usb device connect state
118   */
119 typedef enum
120 {
121   USB_CONN_STATE_DEFAULT                =1, /*!< usb device connect state default */
122   USB_CONN_STATE_ADDRESSED,                 /*!< usb device connect state address */
123   USB_CONN_STATE_CONFIGURED,                /*!< usb device connect state configured */
124   USB_CONN_STATE_SUSPENDED                  /*!< usb device connect state suspend */
125 }usbd_conn_state;
126 
127 /**
128   * @brief endpoint 0 state
129   */
130 #define USB_EPT0_IDLE                    0 /*!< usb endpoint state idle */
131 #define USB_EPT0_SETUP                   1 /*!< usb endpoint state setup */
132 #define USB_EPT0_DATA_IN                 2 /*!< usb endpoint state data in */
133 #define USB_EPT0_DATA_OUT                3 /*!< usb endpoint state data out */
134 #define USB_EPT0_STATUS_IN               4 /*!< usb endpoint state status in */
135 #define USB_EPT0_STATUS_OUT              5 /*!< usb endpoint state status out */
136 #define USB_EPT0_STALL                   6 /*!< usb endpoint state stall */
137 
138 /**
139   * @brief usb descriptor length
140   */
141 #define USB_DEVICE_QUALIFIER_DESC_LEN    0x0A /*!< usb qualifier descriptor length */
142 #define USB_DEVICE_DESC_LEN              0x12 /*!< usb device descriptor length */
143 #define USB_DEVICE_CFG_DESC_LEN          0x09 /*!< usb configuration descriptor length */
144 #define USB_DEVICE_IF_DESC_LEN           0x09 /*!< usb interface descriptor length */
145 #define USB_DEVICE_EPT_LEN               0x07 /*!< usb endpoint descriptor length */
146 #define USB_DEVICE_OTG_DESC_LEN          0x03 /*!< usb otg descriptor length */
147 #define USB_DEVICE_LANGID_STR_DESC_LEN   0x04 /*!< usb lang id string descriptor length */
148 #define USB_DEVICE_OTHER_SPEED_DESC_SIZ_LEN 0x09 /*!< usb other speed descriptor length */
149 
150 /**
151   * @brief usb class code
152   */
153 #define USB_CLASS_CODE_AUDIO             0x01 /*!< usb class code audio */
154 #define USB_CLASS_CODE_CDC               0x02 /*!< usb class code cdc */
155 #define USB_CLASS_CODE_HID               0x03 /*!< usb class code hid */
156 #define USB_CLASS_CODE_PRINTER           0x07 /*!< usb class code printer */
157 #define USB_CLASS_CODE_MSC               0x08 /*!< usb class code msc */
158 #define USB_CLASS_CODE_HUB               0x09 /*!< usb class code hub */
159 #define USB_CLASS_CODE_CDCDATA           0x0A /*!< usb class code cdc data */
160 #define USB_CLASS_CODE_CCID              0x0B /*!< usb class code ccid */
161 #define USB_CLASS_CODE_VIDEO             0x0E /*!< usb class code video */
162 #define USB_CLASS_CODE_VENDOR            0xFF /*!< usb class code vendor */
163 
164 /**
165   * @brief usb endpoint type
166   */
167 #define USB_EPT_DESC_CONTROL             0x00 /*!< usb endpoint description type control */
168 #define USB_EPT_DESC_ISO                 0x01 /*!< usb endpoint description type iso */
169 #define USB_EPT_DESC_BULK                0x02 /*!< usb endpoint description type bulk */
170 #define USB_EPT_DESC_INTERRUPT           0x03 /*!< usb endpoint description type interrupt */
171 
172 #define USB_EPT_DESC_NSYNC               0x00 /*!< usb endpoint description nsync */
173 #define USB_ETP_DESC_ASYNC               0x04 /*!< usb endpoint description async */
174 #define USB_ETP_DESC_ADAPTIVE            0x08 /*!< usb endpoint description adaptive */
175 #define USB_ETP_DESC_SYNC                0x0C /*!< usb endpoint description sync */
176 
177 #define USB_EPT_DESC_DATA_EPT            0x00 /*!< usb endpoint description data */
178 #define USB_EPT_DESC_FD_EPT              0x10 /*!< usb endpoint description fd */
179 #define USB_EPT_DESC_FDDATA_EPT          0x20 /*!< usb endpoint description fddata */
180 
181 /**
182   * @brief usb cdc class descriptor define
183   */
184 #define USBD_CDC_CS_INTERFACE             0x24
185 #define USBD_CDC_CS_ENDPOINT              0x25
186 
187 /**
188   * @brief usb cdc class sub-type define
189   */
190 #define USBD_CDC_SUBTYPE_HEADER           0x00
191 #define USBD_CDC_SUBTYPE_CMF              0x01
192 #define USBD_CDC_SUBTYPE_ACM              0x02
193 #define USBD_CDC_SUBTYPE_UFD              0x06
194 
195 /**
196   * @brief usb cdc class request code define
197   */
198 #define SET_LINE_CODING                   0x20
199 #define GET_LINE_CODING                   0x21
200 
201 /**
202   * @brief usb cdc class set line coding struct
203   */
204 typedef struct
205 {
206   uint32_t bitrate;                      /* line coding baud rate */
207   uint8_t format;                        /* line coding foramt */
208   uint8_t parity;                        /* line coding parity */
209   uint8_t data;                          /* line coding data bit */
210 }linecoding_type;
211 
212 /**
213   * @brief usb hid class descriptor define
214   */
215 #define HID_CLASS_DESC_HID               0x21
216 #define HID_CLASS_DESC_REPORT            0x22
217 #define HID_CLASS_DESC_PHYSICAL          0x23
218 
219 /**
220   * @brief usb hid class request code define
221   */
222 #define HID_REQ_SET_PROTOCOL             0x0B
223 #define HID_REQ_GET_PROTOCOL             0x03
224 #define HID_REQ_SET_IDLE                 0x0A
225 #define HID_REQ_GET_IDLE                 0x02
226 #define HID_REQ_SET_REPORT               0x09
227 #define HID_REQ_GET_REPORT               0x01
228 #define HID_DESCRIPTOR_TYPE              0x21
229 #define HID_REPORT_DESC                  0x22
230 
231 /**
232   * @brief endpoint 0 max size
233   */
234 #define USB_MAX_EP0_SIZE                 64 /*!< usb endpoint 0 max size */
235 
236 /**
237   * @brief usb swap address
238   */
239 #define SWAPBYTE(addr)        (uint16_t)(((uint16_t)(*((uint8_t *)(addr)))) + \
240                                (((uint16_t)(*(((uint8_t *)(addr)) + 1))) << 8)) /*!< swap address */
241 
242 /**
243   * @brief min and max define
244   */
245 #ifndef MIN
246 #define MIN(a, b)  (uint16_t)(((a) < (b)) ? (a) : (b)) /*!< min define*/
247 #endif
248 #ifndef MAX
249 #define MAX(a, b)  (uint16_t)(((a) > (b)) ? (a) : (b)) /*!< max define*/
250 #endif
251 
252 /**
253   * @brief low byte and high byte define
254   */
255 #define LBYTE(x)  ((uint8_t)(x & 0x00FF))        /*!< low byte define */
256 #define HBYTE(x)  ((uint8_t)((x & 0xFF00) >>8))  /*!< high byte define*/
257 
258 /**
259   * @brief usb return status
260   */
261 typedef enum
262 {
263   USB_OK,              /*!< usb status ok */
264   USB_FAIL,            /*!< usb status fail */
265   USB_WAIT,            /*!< usb status wait */
266   USB_NOT_SUPPORT,     /*!< usb status not support */
267   USB_ERROR,           /*!< usb status error */
268 }usb_sts_type;
269 
270 
271 /**
272   * @brief format of usb setup data
273   */
274 typedef struct
275 {
276   uint8_t                                bmRequestType;                 /*!< characteristics of request */
277   uint8_t                                bRequest;                      /*!< specific request */
278   uint16_t                               wValue;                        /*!< word-sized field that varies according to request */
279   uint16_t                               wIndex;                        /*!< word-sized field that varies according to request
280                                                                            typically used to pass an index or offset */
281   uint16_t                               wLength;                       /*!< number of bytes to transfer if there is a data stage */
282 } usb_setup_type;
283 
284 /**
285   * @brief format of standard device descriptor
286   */
287 typedef struct
288 {
289   uint8_t                                bLength;                       /*!< size of this descriptor in bytes */
290   uint8_t                                bDescriptorType;               /*!< device descriptor type */
291   uint16_t                               bcdUSB;                        /*!< usb specification release number */
292   uint8_t                                bDeviceClass;                  /*!< class code (assigned by the usb-if) */
293   uint8_t                                bDeviceSubClass;               /*!< subclass code (assigned by the usb-if) */
294   uint8_t                                bDeviceProtocol;               /*!< protocol code ((assigned by the usb-if)) */
295   uint8_t                                bMaxPacketSize0;               /*!< maximum packet size for endpoint zero */
296   uint16_t                               idVendor;                      /*!< verndor id ((assigned by the usb-if)) */
297   uint16_t                               idProduct;                     /*!< product id ((assigned by the usb-if)) */
298   uint16_t                               bcdDevice;                     /*!< device release number in binary-coded decimal */
299   uint8_t                                iManufacturer;                 /*!< index of string descriptor describing manufacturer */
300   uint8_t                                iProduct;                      /*!< index of string descriptor describing product */
301   uint8_t                                iSerialNumber;                 /*!< index of string descriptor describing serial number */
302   uint8_t                                bNumConfigurations;            /*!< number of possible configurations */
303 } usb_device_desc_type;
304 
305 /**
306   * @brief format of standard configuration descriptor
307   */
308 typedef struct
309 {
310   uint8_t                                bLength;                        /*!< size of this descriptor in bytes */
311   uint8_t                                bDescriptorType;                /*!< configuration descriptor type */
312   uint16_t                               wTotalLength;                   /*!< total length of data returned for this configuration */
313   uint8_t                                bNumInterfaces;                 /*!< number of interfaces supported by this configuration */
314   uint8_t                                bConfigurationValue;            /*!< value to use as an argument to the SetConfiguration() request */
315   uint8_t                                iConfiguration;                 /*!< index of string descriptor describing this configuration */
316   uint8_t                                bmAttributes;                   /*!< configuration characteristics
317                                                                             D7 reserved
318                                                                             D6 self-powered
319                                                                             D5 remote wakeup
320                                                                             D4~D0 reserved */
321   uint8_t                                bMaxPower;                      /*!< maximum power consumption of the usb device from the bus */
322 
323 
324 }usb_configuration_desc_type;
325 
326 /**
327   * @brief format of standard interface descriptor
328   */
329 typedef struct
330 {
331   uint8_t                                bLength;                        /*!< size of this descriptor in bytes */
332   uint8_t                                bDescriptorType;                /*!< interface descriptor type */
333   uint8_t                                bInterfaceNumber;               /*!< number of this interface */
334   uint8_t                                bAlternateSetting;              /*!< value used to select this alternate setting for the interface */
335   uint8_t                                bNumEndpoints;                  /*!< number of endpoints used by this interface */
336   uint8_t                                bInterfaceClass;                /*!< class code (assigned by the usb-if) */
337   uint8_t                                bInterfaceSubClass;             /*!< subclass code (assigned by the usb-if) */
338   uint8_t                                bInterfaceProtocol;             /*!< protocol code (assigned by the usb-if) */
339   uint8_t                                iInterface;                     /*!< index of string descriptor describing this interface */
340 } usb_interface_desc_type;
341 
342 /**
343   * @brief format of standard endpoint descriptor
344   */
345 typedef struct
346 {
347   uint8_t                                bLength;                        /*!< size of this descriptor in bytes */
348   uint8_t                                bDescriptorType;                /*!< endpoint descriptor type */
349   uint8_t                                bEndpointAddress;               /*!< the address of the endpoint on the usb device described by this descriptor */
350   uint8_t                                bmAttributes;                   /*!< describes the endpoints attributes when it is configured using bConfiguration value */
351   uint16_t                               wMaxPacketSize;                 /*!< maximum packet size this endpoint */
352   uint8_t                                bInterval;                      /*!< interval for polling endpoint for data transfers */
353 } usb_endpoint_desc_type;
354 
355 /**
356   * @brief format of header
357   */
358 typedef struct
359 {
360   uint8_t                                bLength;                        /*!< size of this descriptor in bytes */
361   uint8_t                                bDescriptorType;                /*!< descriptor type */
362 } usb_header_desc_type;
363 
364 
365 /**
366   * @}
367   */
368 
369 /**
370   * @}
371   */
372 
373 #ifdef __cplusplus
374 }
375 #endif
376 
377 #endif
378 
379