1 /*!
2  * @file        usbd_core.h
3  *
4  * @brief       USB protocol core handler head file
5  *
6  * @version     V1.0.1
7  *
8  * @date        2022-09-20
9  *
10  * @attention
11  *
12  *  Copyright (C) 2020-2022 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* Define to prevent recursive inclusion */
27 #ifndef __USBD_CORE_H_
28 #define __USBD_CORE_H_
29 
30 /* Includes */
31 #include "drv_usb_device.h"
32 
33 /** @addtogroup USB_Driver_Library USB Driver Library
34   @{
35 */
36 
37 /** @addtogroup Core_Device Core Device
38   @{
39 */
40 
41 /** @addtogroup Standrad
42   @{
43 */
44 
45 /** @addtogroup Core
46   @{
47 */
48 
49 /** @defgroup Core_Macros Macros
50   @{
51 */
52 
53 /* Get minimum value */
54 #define USB_MIN(a, b)      (a >= b ? b : a)
55 
56 /* Get maximum value */
57 #define USB_MAX(a, b)      (a >= b ? a : b)
58 
59 /* control status function */
60 #define USBD_CtrlTxStatus()         USBD_CtrlInData(NULL, 0)
61 #define USBD_CtrlRxStatus()         USBD_CtrlOutData(NULL, 0)
62 
63 /**@} end of group Core_Macros */
64 
65 /** @defgroup Core_Enumerations Enumerations
66   @{
67 */
68 
69 /**
70  * @brief   USB request type
71  */
72 enum
73 {
74     USBD_REQ_TYPE_STANDARD   = 0,
75     USBD_REQ_TYPE_CLASS      = 1,
76     USBD_REQ_TYPE_VENDOR     = 2,
77     USBD_REQ_TYPE_RESERVED   = 3
78 };
79 
80 /**
81  * @brief   USB recipient
82  */
83 enum
84 {
85     USBD_RECIPIENT_DEVICE    = 0,
86     USBD_RECIPIENT_INTERFACE = 1,
87     USBD_RECIPIENT_ENDPOINT  = 2,
88     USBD_RECIPIENT_OTHER     = 3,
89 };
90 
91 /**
92  * @brief   USB standard device requests
93  */
94 enum
95 {
96     USBD_GET_STATUS          = 0,
97     USBD_CLEAR_FEATURE       = 1,
98     USBD_SET_FEATURE         = 3,
99     USBD_SET_ADDRESS         = 5,
100     USBD_GET_DESCRIPTOR      = 6,
101     USBD_SET_DESCRIPTOR      = 7,
102     USBD_GET_CONFIGURATION   = 8,
103     USBD_SET_CONFIGURATION   = 9,
104     USBD_GET_INTERFACE       = 10,
105     USBD_SET_INTERFACE       = 11,
106     USBD_SYNCH_FRAME         = 12,
107 };
108 
109 /**
110  * @brief   USB descriptor types
111  */
112 enum
113 {
114     USBD_DESC_DEVICE             = 1,
115     USBD_DESC_CONFIGURATION      = 2,
116     USBD_DESC_STRING             = 3,
117     USBD_DESC_INTERFACE          = 4,
118     USBD_DESC_ENDPOINT           = 5,
119     USBD_DESC_DEVICE_QUALIFIER   = 6,
120     USBD_DESC_OTHER_SPEED        = 7,
121     USBD_INTERFACE_POWER         = 8,
122 };
123 
124 /**
125  * @brief   USB standard feature
126  */
127 enum
128 {
129     USBD_FEATURE_ENDPOINT_HALT   = 0,
130     USBD_FEATURE_REMOTE_WAKEUP   = 1,
131     USBD_FEATURE_TEST_MODE       = 2
132 };
133 
134 /**
135  * @brief   USB internal state machine
136  */
137 typedef enum
138 {
139     USBD_CTRL_STATE_WAIT_SETUP,
140     USBD_CTRL_STATE_DATA_IN,
141     USBD_CTRL_STATE_DATA_OUT,
142     USBD_CTRL_STATE_WAIT_STATUS_IN,
143     USBD_CTRL_STATE_WAIT_STATUS_OUT,
144     USBD_CTRL_STATE_STALLED,
145 } USBD_CTRL_STATE_T;
146 
147 /**
148  * @brief   USBD Endpoint type for USB protocol
149  */
150 typedef enum
151 {
152     USBD_EP_TYPE_CONTROL,
153     USBD_EP_TYPE_ISO,
154     USBD_EP_TYPE_BULK,
155     USBD_EP_TYPE_INTERRUPT
156 } USBD_EP_TYPE_T;
157 
158 /**@} end of group Core_Enumerations */
159 
160 /** @defgroup Core_Structures Structures
161   @{
162 */
163 
164 /**
165  * @brief   USB request type
166  */
167 typedef union
168 {
169     uint8_t byte;
170 
171     struct
172     {
173         uint8_t recipient       : 5;
174         uint8_t type            : 2;
175         uint8_t dir             : 1;
176     } bit;
177 } USBD_REQ_TYPE_T;
178 
179 /**
180  * @brief   USB device request data
181  */
182 typedef struct
183 {
184     union
185     {
186         uint8_t pack[8];
187 
188         struct
189         {
190             USBD_REQ_TYPE_T bmRequestType;
191             uint8_t         bRequest;
192             uint8_t         wValue[2];
193             uint8_t         wIndex[2];
194             uint8_t         wLength[2];
195         } byte;
196     };
197 } USBD_DevReqData_T;
198 
199 
200 /* USB standard request callback handler */
201 typedef void (*USBD_StdReqHandler_T)(void);
202 
203 /* USB request handler */
204 typedef void (*USBD_ReqHandler_T)(USBD_DevReqData_T*);
205 
206 /* Ctrl Tx Status handler function define */
207 typedef void (*USBD_CtrlTxStatusHandler_T)(void);
208 
209 /* Ctrl Rx Status handler function define */
210 typedef void (*USBD_CtrlRxStatusHandler_T)(void);
211 
212 /* Endpoint handler */
213 typedef void (*USBD_EPHandler_T)(uint8_t ep);
214 
215 /* Reset handler */
216 typedef void (*USBD_ResetHandler_T)(void);
217 
218 /* Interrupt handler function define */
219 typedef void (*USBD_InterruptHandler_T)(void);
220 
221 /**
222  * @brief   Descriptor structure
223  */
224 typedef struct
225 {
226     const uint8_t* pDesc;
227     uint8_t size;
228 } USBD_Descriptor_T;
229 
230 /**
231  * @brief   USB Class Request handler
232  */
233 typedef struct
234 {
235     USBD_StdReqHandler_T getConfigurationHandler;
236     USBD_StdReqHandler_T getDescriptorHandler;
237     USBD_StdReqHandler_T getInterfaceHandler;
238     USBD_StdReqHandler_T getStatusHandler;
239     USBD_StdReqHandler_T setAddressHandler;
240 
241     USBD_StdReqHandler_T setConfigurationHandler;
242     USBD_StdReqHandler_T setDescriptorHandler;
243     USBD_StdReqHandler_T setFeatureHandler;
244     USBD_StdReqHandler_T setInterfaceHandler;
245     USBD_StdReqHandler_T clearFeatureHandler;
246 } USBD_StdReqCallback_T;
247 
248 /**
249  * @brief   Control transfer buffer
250  */
251 typedef struct
252 {
253     uint8_t* pBuf;         /*!< Data buffer */
254     uint32_t bufLen;       /*!< Length of the data buffer */
255     uint8_t  packNum;      /*!< Packet number of the data */
256     uint8_t  zeroPackFill; /*!< Fill a zero pack for IN transfer or not */
257     uint16_t maxPackSize;  /*!< Max pack size of this endpoint */
258     uint32_t xferCnt;      /*!< Data count of one pack on from tansfer */
259 } USBD_CtrlBuf_T;
260 
261 /**
262  * @brief   USB init parameter
263  */
264 typedef struct
265 {
266     USBD_Descriptor_T* pDeviceDesc;              /*!< Device descriptor pointer */
267     USBD_Descriptor_T* pConfigurationDesc;       /*!< Configuration descriptor pointer */
268     USBD_Descriptor_T* pStringDesc;              /*!< String descriptor pointer */
269     USBD_Descriptor_T* pQualifierDesc;           /*!< Device Qualifier descriptor pointer */
270     USBD_Descriptor_T* pHidReportDesc;           /*!< HID report descriptor pointer */
271 
272 
273     USBD_StdReqCallback_T* pStdReqCallback;
274     USBD_ReqHandler_T stdReqExceptionHandler;    /*!< Standard request exception handler */
275     USBD_ReqHandler_T classReqHandler;           /*!< Class request handler */
276     USBD_ReqHandler_T vendorReqHandler;          /*!< vendor request handler */
277 
278     USBD_CtrlTxStatusHandler_T txStatusHandler;  /*!< Send IN status early handler */
279     USBD_CtrlRxStatusHandler_T rxStatusHandler;  /*!< Receive OUT status early handler */
280 
281     USBD_EPHandler_T    outEpHandler;            /*!< OUT EP transfer done handler except EP0 */
282     USBD_EPHandler_T    inEpHandler;             /*!< IN EP transfer done handler except EP0 */
283     USBD_ResetHandler_T resetHandler;            /*!< Reset handler */
284     USBD_InterruptHandler_T intHandler;          /*!< Hadler the rest of interrupt. */
285 } USBD_InitParam_T;
286 
287 /**
288  * @brief   USB infomation
289  */
290 typedef struct
291 {
292     USBD_CTRL_STATE_T ctrlState;
293 
294     uint8_t curFeature;
295     uint8_t curInterface;
296     uint8_t curAlternateSetting;
297     uint8_t curConfiguration;
298     uint8_t configurationNum;
299 
300     /* Setup request data buffer */
301     USBD_DevReqData_T reqData;
302 
303     /* Endpoint buffer management */
304     USBD_CtrlBuf_T inBuf[USB_EP_MAX_NUM];
305     USBD_CtrlBuf_T outBuf[USB_EP_MAX_NUM];
306 
307     /* Descriptor pointer */
308     USBD_Descriptor_T* pDeviceDesc;
309     USBD_Descriptor_T* pConfigurationDesc;
310     USBD_Descriptor_T* pStringDesc;
311     USBD_Descriptor_T* pQualifierDesc;
312     USBD_Descriptor_T* pHidReportDesc;
313 
314     /* Setup request callback handler */
315     USBD_StdReqCallback_T* pStdReqCallback;
316     USBD_ReqHandler_T     stdReqExceptionHandler;
317     USBD_ReqHandler_T     classReqHandler;
318     USBD_ReqHandler_T     vendorReqHandler;
319 
320     /* Control transfer status stage handler */
321     USBD_CtrlTxStatusHandler_T txStatusHandler;
322     USBD_CtrlRxStatusHandler_T rxStatusHandler;
323 
324     /* Endpoint transfer done handler */
325     USBD_EPHandler_T      outEpHandler;
326     USBD_EPHandler_T      inEpHandler;
327 
328     USBD_ResetHandler_T   resetHandler;
329     USBD_InterruptHandler_T intHandler;
330 } USBD_Info_T;
331 
332 /**@} end of group Core_Structures */
333 
334 /** @defgroup Core_Variables Variables
335   @{
336 */
337 
338 
339 extern USBD_Info_T g_usbDev;
340 
341 /**@} end of group Core_Variables */
342 
343 /** @defgroup Core_Functions Functions
344   @{
345 */
346 
347 /* Handler Endpoint 0 control transfer */
348 void USBD_SetupProcess(void);
349 void USBD_CtrlInProcess(void);
350 void USBD_CtrlOutProcess(void);
351 void USBD_CtrlOutData(uint8_t* buf, uint32_t len);
352 void USBD_CtrlInData(uint8_t* buf, uint32_t len);
353 
354 /* Handler other Endpoint data transfer */
355 void USBD_DataInProcess(USBD_EP_T ep);
356 void USBD_DataOutProcess(USBD_EP_T ep);
357 void USBD_TxData(uint8_t ep, uint8_t* buf, uint32_t len);
358 void USBD_RxData(uint8_t ep, uint8_t* buf, uint32_t len);
359 
360 #endif
361 
362 /**@} end of group Core_Functions */
363 /**@} end of group Core */
364 /**@} end of group Standard */
365 /**@} end of group Core_Device */
366 /**@} end of group USB_Driver_Library */
367