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 //#define USB_FEATURE_TEST_MODE            2 /*!< usb standard feature selectors test mode */
116 
117 /**
118   * @brief usb device connect state
119   */
120 typedef enum
121 {
122   USB_CONN_STATE_DEFAULT                =1, /*!< usb device connect state default */
123   USB_CONN_STATE_ADDRESSED,                 /*!< usb device connect state address */
124   USB_CONN_STATE_CONFIGURED,                /*!< usb device connect state configured */
125   USB_CONN_STATE_SUSPENDED                  /*!< usb device connect state suspend */
126 }usbd_conn_state;
127 
128 /**
129   * @brief endpoint 0 state
130   */
131 #define USB_EPT0_IDLE                    0 /*!< usb endpoint state idle */
132 #define USB_EPT0_SETUP                   1 /*!< usb endpoint state setup */
133 #define USB_EPT0_DATA_IN                 2 /*!< usb endpoint state data in */
134 #define USB_EPT0_DATA_OUT                3 /*!< usb endpoint state data out */
135 #define USB_EPT0_STATUS_IN               4 /*!< usb endpoint state status in */
136 #define USB_EPT0_STATUS_OUT              5 /*!< usb endpoint state status out */
137 #define USB_EPT0_STALL                   6 /*!< usb endpoint state stall */
138 
139 /**
140   * @brief usb descriptor length
141   */
142 #define USB_DEVICE_QUALIFIER_DESC_LEN    0x0A /*!< usb qualifier descriptor length */
143 #define USB_DEVICE_DESC_LEN              0x12 /*!< usb device descriptor length */
144 #define USB_DEVICE_CFG_DESC_LEN          0x09 /*!< usb configuration descriptor length */
145 #define USB_DEVICE_IF_DESC_LEN           0x09 /*!< usb interface descriptor length */
146 #define USB_DEVICE_EPT_LEN               0x07 /*!< usb endpoint descriptor length */
147 #define USB_DEVICE_OTG_DESC_LEN          0x03 /*!< usb otg descriptor length */
148 #define USB_DEVICE_LANGID_STR_DESC_LEN   0x04 /*!< usb lang id string descriptor length */
149 #define USB_DEVICE_OTHER_SPEED_DESC_SIZ_LEN 0x09 /*!< usb other speed descriptor length */
150 
151 /**
152   * @brief usb class code
153   */
154 #define USB_CLASS_CODE_AUDIO             0x01 /*!< usb class code audio */
155 #define USB_CLASS_CODE_CDC               0x02 /*!< usb class code cdc */
156 #define USB_CLASS_CODE_HID               0x03 /*!< usb class code hid */
157 #define USB_CLASS_CODE_PRINTER           0x07 /*!< usb class code printer */
158 #define USB_CLASS_CODE_MSC               0x08 /*!< usb class code msc */
159 #define USB_CLASS_CODE_HUB               0x09 /*!< usb class code hub */
160 #define USB_CLASS_CODE_CDCDATA           0x0A /*!< usb class code cdc data */
161 #define USB_CLASS_CODE_CCID              0x0B /*!< usb class code ccid */
162 #define USB_CLASS_CODE_VIDEO             0x0E /*!< usb class code video */
163 #define USB_CLASS_CODE_VENDOR            0xFF /*!< usb class code vendor */
164 
165 /**
166   * @brief usb endpoint type
167   */
168 #define USB_EPT_DESC_CONTROL             0x00 /*!< usb endpoint description type control */
169 #define USB_EPT_DESC_ISO                 0x01 /*!< usb endpoint description type iso */
170 #define USB_EPT_DESC_BULK                0x02 /*!< usb endpoint description type bulk */
171 #define USB_EPT_DESC_INTERRUPT           0x03 /*!< usb endpoint description type interrupt */
172 
173 #define USB_EPT_DESC_NSYNC               0x00 /*!< usb endpoint description nsync */
174 #define USB_ETP_DESC_ASYNC               0x04 /*!< usb endpoint description async */
175 #define USB_ETP_DESC_ADAPTIVE            0x08 /*!< usb endpoint description adaptive */
176 #define USB_ETP_DESC_SYNC                0x0C /*!< usb endpoint description sync */
177 
178 #define USB_EPT_DESC_DATA_EPT            0x00 /*!< usb endpoint description data */
179 #define USB_EPT_DESC_FD_EPT              0x10 /*!< usb endpoint description fd */
180 #define USB_EPT_DESC_FDDATA_EPT          0x20 /*!< usb endpoint description fddata */
181 
182 /**
183   * @brief usb cdc class descriptor define
184   */
185 #define USBD_CDC_CS_INTERFACE             0x24
186 #define USBD_CDC_CS_ENDPOINT              0x25
187 
188 /**
189   * @brief usb cdc class sub-type define
190   */
191 #define USBD_CDC_SUBTYPE_HEADER           0x00
192 #define USBD_CDC_SUBTYPE_CMF              0x01
193 #define USBD_CDC_SUBTYPE_ACM              0x02
194 #define USBD_CDC_SUBTYPE_UFD              0x06
195 
196 /**
197   * @brief usb cdc class request code define
198   */
199 #define SET_LINE_CODING                   0x20
200 #define GET_LINE_CODING                   0x21
201 
202 /**
203   * @brief usb cdc class set line coding struct
204   */
205 typedef struct
206 {
207   uint32_t bitrate;                      /* line coding baud rate */
208   uint8_t format;                        /* line coding foramt */
209   uint8_t parity;                        /* line coding parity */
210   uint8_t data;                          /* line coding data bit */
211 }linecoding_type;
212 
213 /**
214   * @brief usb hid class descriptor define
215   */
216 #define HID_CLASS_DESC_HID               0x21
217 #define HID_CLASS_DESC_REPORT            0x22
218 #define HID_CLASS_DESC_PHYSICAL          0x23
219 
220 /**
221   * @brief usb hid class request code define
222   */
223 #define HID_REQ_SET_PROTOCOL             0x0B
224 #define HID_REQ_GET_PROTOCOL             0x03
225 #define HID_REQ_SET_IDLE                 0x0A
226 #define HID_REQ_GET_IDLE                 0x02
227 #define HID_REQ_SET_REPORT               0x09
228 #define HID_REQ_GET_REPORT               0x01
229 #define HID_DESCRIPTOR_TYPE              0x21
230 #define HID_REPORT_DESC                  0x22
231 
232 /**
233   * @brief endpoint 0 max size
234   */
235 #define USB_MAX_EP0_SIZE                 64 /*!< usb endpoint 0 max size */
236 
237 /**
238   * @brief usb swap address
239   */
240 #define SWAPBYTE(addr)        (uint16_t)(((uint16_t)(*((uint8_t *)(addr)))) + \
241                                (((uint16_t)(*(((uint8_t *)(addr)) + 1))) << 8)) /*!< swap address */
242 
243 /**
244   * @brief min and max define
245   */
246 #ifndef MIN
247 #define MIN(a, b)  (uint16_t)(((a) < (b)) ? (a) : (b)) /*!< min define*/
248 #endif
249 #ifndef MAX
250 #define MAX(a, b)  (uint16_t)(((a) > (b)) ? (a) : (b)) /*!< max define*/
251 #endif
252 
253 /**
254   * @brief low byte and high byte define
255   */
256 #define LBYTE(x)  ((uint8_t)(x & 0x00FF))        /*!< low byte define */
257 #define HBYTE(x)  ((uint8_t)((x & 0xFF00) >>8))  /*!< high byte define*/
258 
259 /**
260   * @}
261   */
262 
263 /** @defgroup USB_standard_exported_types
264   * @{
265   */
266 
267 /**
268   * @brief usb return status
269   */
270 typedef enum
271 {
272   USB_OK,              /*!< usb status ok */
273   USB_FAIL,            /*!< usb status fail */
274   USB_WAIT,            /*!< usb status wait */
275   USB_NOT_SUPPORT,     /*!< usb status not support */
276   USB_ERROR,           /*!< usb status error */
277 }usb_sts_type;
278 
279 /**
280   * @brief format of usb setup data
281   */
282 typedef struct
283 {
284   uint8_t                                bmRequestType;                 /*!< characteristics of request */
285   uint8_t                                bRequest;                      /*!< specific request */
286   uint16_t                               wValue;                        /*!< word-sized field that varies according to request */
287   uint16_t                               wIndex;                        /*!< word-sized field that varies according to request
288                                                                            typically used to pass an index or offset */
289   uint16_t                               wLength;                       /*!< number of bytes to transfer if there is a data stage */
290 }usb_setup_type;
291 
292 /**
293   * @brief format of standard device descriptor
294   */
295 typedef struct
296 {
297   uint8_t                                bLength;                       /*!< size of this descriptor in bytes */
298   uint8_t                                bDescriptorType;               /*!< device descriptor type */
299   uint16_t                               bcdUSB;                        /*!< usb specification release number */
300   uint8_t                                bDeviceClass;                  /*!< class code (assigned by the usb-if) */
301   uint8_t                                bDeviceSubClass;               /*!< subclass code (assigned by the usb-if) */
302   uint8_t                                bDeviceProtocol;               /*!< protocol code ((assigned by the usb-if)) */
303   uint8_t                                bMaxPacketSize0;               /*!< maximum packet size for endpoint zero */
304   uint16_t                               idVendor;                      /*!< verndor id ((assigned by the usb-if)) */
305   uint16_t                               idProduct;                     /*!< product id ((assigned by the usb-if)) */
306   uint16_t                               bcdDevice;                     /*!< device release number in binary-coded decimal */
307   uint8_t                                iManufacturer;                 /*!< index of string descriptor describing manufacturer */
308   uint8_t                                iProduct;                      /*!< index of string descriptor describing product */
309   uint8_t                                iSerialNumber;                 /*!< index of string descriptor describing serial number */
310   uint8_t                                bNumConfigurations;            /*!< number of possible configurations */
311 }usb_device_desc_type;
312 
313 /**
314   * @brief format of standard configuration descriptor
315   */
316 typedef struct
317 {
318   uint8_t                                bLength;                        /*!< size of this descriptor in bytes */
319   uint8_t                                bDescriptorType;                /*!< configuration descriptor type */
320   uint16_t                               wTotalLength;                   /*!< total length of data returned for this configuration */
321   uint8_t                                bNumInterfaces;                 /*!< number of interfaces supported by this configuration */
322   uint8_t                                bConfigurationValue;            /*!< value to use as an argument to the SetConfiguration() request */
323   uint8_t                                iConfiguration;                 /*!< index of string descriptor describing this configuration */
324   uint8_t                                bmAttributes;                   /*!< configuration characteristics
325                                                                             D7 reserved
326                                                                             D6 self-powered
327                                                                             D5 remote wakeup
328                                                                             D4~D0 reserved */
329   uint8_t                                bMaxPower;                      /*!< maximum power consumption of the usb device from the bus */
330 
331 
332 }usb_configuration_desc_type;
333 
334 /**
335   * @brief format of standard interface descriptor
336   */
337 typedef struct
338 {
339   uint8_t                                bLength;                        /*!< size of this descriptor in bytes */
340   uint8_t                                bDescriptorType;                /*!< interface descriptor type */
341   uint8_t                                bInterfaceNumber;               /*!< number of this interface */
342   uint8_t                                bAlternateSetting;              /*!< value used to select this alternate setting for the interface */
343   uint8_t                                bNumEndpoints;                  /*!< number of endpoints used by this interface */
344   uint8_t                                bInterfaceClass;                /*!< class code (assigned by the usb-if) */
345   uint8_t                                bInterfaceSubClass;             /*!< subclass code (assigned by the usb-if) */
346   uint8_t                                bInterfaceProtocol;             /*!< protocol code (assigned by the usb-if) */
347   uint8_t                                iInterface;                     /*!< index of string descriptor describing this interface */
348 }usb_interface_desc_type;
349 
350 /**
351   * @brief format of standard endpoint descriptor
352   */
353 typedef struct
354 {
355   uint8_t                                bLength;                        /*!< size of this descriptor in bytes */
356   uint8_t                                bDescriptorType;                /*!< endpoint descriptor type */
357   uint8_t                                bEndpointAddress;               /*!< the address of the endpoint on the usb device described by this descriptor */
358   uint8_t                                bmAttributes;                   /*!< describes the endpoints attributes when it is configured using bConfiguration value */
359   uint16_t                               wMaxPacketSize;                 /*!< maximum packet size this endpoint */
360   uint8_t                                bInterval;                      /*!< interval for polling endpoint for data transfers */
361 }usb_endpoint_desc_type;
362 
363 /**
364   * @brief format of header
365   */
366 typedef struct
367 {
368   uint8_t                                bLength;                        /*!< size of this descriptor in bytes */
369   uint8_t                                bDescriptorType;                /*!< descriptor type */
370 }usb_header_desc_type;
371 
372 
373 /**
374   * @}
375   */
376 
377 /**
378   * @}
379   */
380 
381 #ifdef __cplusplus
382 }
383 #endif
384 
385 #endif
386 
387