1 /*********************************************************************************************************//**
2  * @file    ht32f5xxxx_usbd.h
3  * @version $Rev:: 6559         $
4  * @date    $Date:: 2022-12-18 #$
5  * @brief   The header file of the USB Device Driver.
6  *************************************************************************************************************
7  * @attention
8  *
9  * Firmware Disclaimer Information
10  *
11  * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
12  *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
13  *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
14  *    other intellectual property laws.
15  *
16  * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
17  *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
18  *    other than HOLTEK and the customer.
19  *
20  * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
21  *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
22  *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
23  *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
24  *
25  * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
26  ************************************************************************************************************/
27 
28 /* Define to prevent recursive inclusion -------------------------------------------------------------------*/
29 #ifndef __HT32F5XXXX_USBD_H
30 #define __HT32F5XXXX_USBD_H
31 
32 #ifdef __cplusplus
33  extern "C" {
34 #endif
35 
36 /* Includes ------------------------------------------------------------------------------------------------*/
37 #include "ht32.h"
38 
39 #if (LIBCFG_USBD_V2)
40 #include "ht32f5xxxx_02_usbdconf.h"
41 #else
42 #include "ht32f5xxxx_01_usbdconf.h"
43 #endif
44 #include "ht32f5xxxx_usbdinit.h"
45 
46 /** @addtogroup HT32F5xxxx_Peripheral_Driver HT32F5xxxx Peripheral Driver
47   * @{
48   */
49 
50 /** @defgroup USBDevice USB Device
51   * @brief USB Device driver modules
52   * @{
53   */
54 
55 
56 /* Settings ------------------------------------------------------------------------------------------------*/
57 /** @defgroup USBDevice_Settings USB Device settings
58   * @{
59   */
60 #if (LIBCFG_USBD_V2)
61 #define MAX_EP_NUM                  (10)
62 #else
63 #define MAX_EP_NUM                  (8)
64 #endif
65 /**
66   * @}
67   */
68 
69 /* Exported types ------------------------------------------------------------------------------------------*/
70 /** @defgroup USBDevice_Exported_Types USB Device exported types
71   * @{
72   */
73 /* USB Endpoint number                                                                                      */
74 typedef enum
75 {
76   USBD_EPT0  = 0,
77   USBD_EPT1  = 1,
78   USBD_EPT2  = 2,
79   USBD_EPT3  = 3,
80   USBD_EPT4  = 4,
81   USBD_EPT5  = 5,
82   USBD_EPT6  = 6,
83   USBD_EPT7  = 7,
84   #if (LIBCFG_USBD_V2)
85   USBD_EPT8  = 8,
86   USBD_EPT9  = 9,
87   #endif
88   USBD_NOEPT = -1,
89 } USBD_EPTn_Enum;
90 
91 typedef enum
92 {
93   USBD_TCR_0 = 0,
94   USBD_TCR_1 = 16,
95 } USBD_TCR_Enum;
96 
97 typedef enum
98 {
99   USBD_NAK = 0,
100   USBD_ACK = 1
101 } USBD_Handshake_Enum;
102 
103 /* Endpoint CFGR Register                                                                                   */
104 typedef struct _EPTCFGR_BIT
105 {
106   vu32 EPBUFA: 10;
107   vu32 EPLEN : 10;
108   vu32 _RES0 :  3;
109   vu32 SDBS  :  1;
110   vu32 EPADR :  4;
111   vu32 EPDIR :  1;
112   vu32 EPTYPE:  1;
113   vu32 _RES1 :  1;
114   vu32 EPEN  :  1;
115 } USBD_EPTCFGR_Bit;
116 
117 typedef union _EPTCFGR_TYPEDEF
118 {
119   USBD_EPTCFGR_Bit bits;
120   u32 word;
121 } USBD_EPTCFGR_TypeDef;
122 
123 /* Endpoint CFGR and IER Register                                                                           */
124 typedef struct
125 {
126   USBD_EPTCFGR_TypeDef CFGR;
127   u32 IER;
128 } USBD_EPTInit_TypeDef;
129 
130 /* Endpoint 0 ~ MAX_EP_NUM                                                                                  */
131 typedef struct
132 {
133   u32 uInterruptMask;
134   USBD_EPTInit_TypeDef ept[MAX_EP_NUM];
135 } USBD_Driver_TypeDef;
136 /**
137   * @}
138   */
139 
140 /* Exported constants --------------------------------------------------------------------------------------*/
141 /** @defgroup USBDevice_Exported_Constants USB Device exported constants
142   * @{
143   */
144 
145 /* USB Interrupt Enable Register (USBIER)                                                                   */
146 #define UGIE                        ((u32)0x00000001)   /*!< USB global Interrupt Enable                    */
147 #define SOFIE                       ((u32)0x00000002)   /*!< Start Of Frame Interrupt Enable                */
148 #define URSTIE                      ((u32)0x00000004)   /*!< USB Reset Interrupt Enable                     */
149 #define RSMIE                       ((u32)0x00000008)   /*!< Resume Interrupt Enable                        */
150 #define SUSPIE                      ((u32)0x00000010)   /*!< Suspend Interrupt Enable                       */
151 #define ESOFIE                      ((u32)0x00000020)   /*!< Expected Start Of Frame Interrupt Enable       */
152 #define FRESIE                      ((u32)0x00000040)   /*!< Force USB Reset Control Interrupt Enable       */
153 #define EP0IE                       ((u32)0x00000100)   /*!< Endpoint 0 Interrupt Enable                    */
154 #define EP1IE                       ((u32)0x00000200)   /*!< Endpoint 1 Interrupt Enable                    */
155 #define EP2IE                       ((u32)0x00000400)   /*!< Endpoint 2 Interrupt Enable                    */
156 #define EP3IE                       ((u32)0x00000800)   /*!< Endpoint 3 Interrupt Enable                    */
157 #define EP4IE                       ((u32)0x00001000)   /*!< Endpoint 4 Interrupt Enable                    */
158 #define EP5IE                       ((u32)0x00002000)   /*!< Endpoint 5 Interrupt Enable                    */
159 #define EP6IE                       ((u32)0x00004000)   /*!< Endpoint 6 Interrupt Enable                    */
160 #define EP7IE                       ((u32)0x00008000)   /*!< Endpoint 7 Interrupt Enable                    */
161 
162 /* USB Interrupt Status Register (USBISR)                                                                   */
163 #define SOFIF                       ((u32)0x00000002)   /*!< Start Of Frame Interrupt Flag                  */
164 #define URSTIF                      ((u32)0x00000004)   /*!< USB Reset Interrupt Flag                       */
165 #define RSMIF                       ((u32)0x00000008)   /*!< Resume Interrupt Flag                          */
166 #define SUSPIF                      ((u32)0x00000010)   /*!< Suspend Interrupt Flag                         */
167 #define ESOFIF                      ((u32)0x00000020)   /*!< Expected Start Of Frame Interrupt Flag         */
168 #define FRESIF                      ((u32)0x00000040)   /*!< Force USB Reset Control Interrupt Flag         */
169 #define EP0IF                       ((u32)0x00000100)   /*!< Endpoint 0 Interrupt flag                      */
170 #define EP1IF                       ((u32)0x00000200)   /*!< Endpoint 1 Interrupt flag                      */
171 #define EP2IF                       ((u32)0x00000400)   /*!< Endpoint 2 Interrupt flag                      */
172 #define EP3IF                       ((u32)0x00000800)   /*!< Endpoint 3 Interrupt flag                      */
173 #define EP4IF                       ((u32)0x00001000)   /*!< Endpoint 4 Interrupt flag                      */
174 #define EP5IF                       ((u32)0x00002000)   /*!< Endpoint 5 Interrupt flag                      */
175 #define EP6IF                       ((u32)0x00004000)   /*!< Endpoint 6 Interrupt flag                      */
176 #define EP7IF                       ((u32)0x00008000)   /*!< Endpoint 7 Interrupt flag                      */
177 #if (LIBCFG_USBD_V2)
178 #define EP8IF                       ((u32)0x00010000)   /*!< Endpoint 8 Interrupt flag                      */
179 #define EP9IF                       ((u32)0x00020000)   /*!< Endpoint 9 Interrupt flag                      */
180 #define EPnIF                       ((u32)0x0003FF00)   /*!< Endpoint n Interrupt flag                      */
181 #else
182 #define EPnIF                       ((u32)0x0000FF00)   /*!< Endpoint n Interrupt flag                      */
183 #endif
184 
185 
186 /* USB Endpoint n Interrupt Enable Register (USBEPnIER)                                                     */
187 #define OTRXIE                      ((u32)0x00000001)   /*!< OUT Token Received Interrupt Enable            */
188 #define ODRXIE                      ((u32)0x00000002)   /*!< OUT Data Received Interrupt Enable             */
189 #define ODOVIE                      ((u32)0x00000004)   /*!< OUT Data Buffer Overrun Interrupt Enable       */
190 #define ITRXIE                      ((u32)0x00000008)   /*!< IN Token Received Interrupt Enable             */
191 #define IDTXIE                      ((u32)0x00000010)   /*!< IN Data Transmitted Interrupt Enable           */
192 #define NAKIE                       ((u32)0x00000020)   /*!< NAK Transmitted Interrupt Enable               */
193 #define STLIE                       ((u32)0x00000040)   /*!< STALL Transmitted Interrupt Enable             */
194 #define UERIE                       ((u32)0x00000080)   /*!< USB Error Interrupt Enable                     */
195 #define STRXIE                      ((u32)0x00000100)   /*!< SETUP Token Received Interrupt Enable          */
196 #define SDRXIE                      ((u32)0x00000200)   /*!< SETUP Data Received Interrupt Enable           */
197 #define SDERIE                      ((u32)0x00000400)   /*!< SETUP Data Error Interrupt Enable              */
198 #define ZLRXIE                      ((u32)0x00000800)   /*!< Zero Length Data Received Interrupt Enable     */
199 
200 /* USB Endpoint n Interrupt Status Register (USBEPnISR)                                                     */
201 #define OTRXIF                      ((u32)0x00000001)   /*!< OUT Token Received Interrupt Flag              */
202 #define ODRXIF                      ((u32)0x00000002)   /*!< OUT Data Received Interrupt Flag               */
203 #define ODOVIF                      ((u32)0x00000004)   /*!< OUT Data Buffer Overrun Interrupt Flag         */
204 #define ITRXIF                      ((u32)0x00000008)   /*!< IN Token Received Interrupt Flag               */
205 #define IDTXIF                      ((u32)0x00000010)   /*!< IN Data Transmitted Interrupt Flag             */
206 #define NAKIF                       ((u32)0x00000020)   /*!< NAK Transmitted Interrupt Flag                 */
207 #define STLIF                       ((u32)0x00000040)   /*!< STALL Transmitted Interrupt Flag               */
208 #define UERIF                       ((u32)0x00000080)   /*!< USB Error Interrupt Flag                       */
209 #define STRXIF                      ((u32)0x00000100)   /*!< SETUP Token Received Interrupt Flag            */
210 #define SDRXIF                      ((u32)0x00000200)   /*!< SETUP Data Received Interrupt Flag             */
211 #define SDERIF                      ((u32)0x00000400)   /*!< SETUP Data Error Interrupt Flag                */
212 #define ZLRXIF                      ((u32)0x00000800)   /*!< Zero Length Data Received Interrupt Flag       */
213 
214 /* USB Endpoint n Control and Status Register (USBEPnCSR)                                                   */
215 #define DTGTX                       ((u32)0x00000001)   /*!< Data Toggle Status, for IN transfer            */
216 #define NAKTX                       ((u32)0x00000002)   /*!< NAK Status, for IN transfer                    */
217 #define STLTX                       ((u32)0x00000004)   /*!< STALL Status, for IN transfer                  */
218 #define DTGRX                       ((u32)0x00000008)   /*!< Data Toggle Status, for OUT transfer           */
219 #define NAKRX                       ((u32)0x00000010)   /*!< NAK Status, for OUT transfer                   */
220 #define STLRX                       ((u32)0x00000020)   /*!< STALL Status, for OUT transfer                 */
221 
222 /* For USBD_EPTGetTranssferCount function                                                                   */
223 #define USBD_CNTB0                  (USBD_TCR_0)
224 #define USBD_CNTB1                  (USBD_TCR_1)
225 #define USBD_CNTIN                  (USBD_TCR_0)
226 #define USBD_CNTOUT                 (USBD_TCR_1)
227 /**
228   * @}
229   */
230 
231 /* Exported macro ------------------------------------------------------------------------------------------*/
232 /** @defgroup USBDevice_Exported_Macro USB Device exported macro
233   * @{
234   */
235 /* API macro for USB Core - Global event and operation                                                      */
236 #define API_USB_INIT(driver)                      (USBD_Init(driver))
237 #define API_USB_DEINIT()                          (USBD_DeInit())
238 #define API_USB_POWER_UP(driver, power)           (USBD_PowerUp(driver, power))
239 #define API_USB_POWER_OFF()                       (USBD_PowerOff())
240 #define API_USB_POWER_ON()                        (USBD_PowerOn())
241 #define API_USB_REMOTE_WAKEUP()                   (USBD_RemoteWakeup())
242 #define API_USB_READ_SETUP(buffer)                (USBD_ReadSETUPData((u32 *)(buffer)))
243 #define API_USB_SET_ADDR(addr)                    (USBD_SetAddress(addr))
244 #define API_USB_GET_CTRL_IN_LEN()                 (USBD_EPTGetBufferLen(USBD_EPT0))
245 #define API_USB_ENABLE_INT(flag)                  (USBD_EnableINT(flag))
246 #define API_USB_GET_INT()                         (USBD_GetINT())
247 #define API_USB_GET_EPT_NUM(flag)                 (USBD_GetEPTnINTNumber(flag))
248 #define API_USB_IS_SETUP_INT(flag)                (flag & SDRXIF)
249 #define API_USB_CLR_SETUP_INT()                   (USBD_EPTClearINT(USBD_EPT0, SDRXIF))
250 #define API_USB_IS_RESET_INT(flag)                (flag & URSTIF)
251 #define API_USB_CLR_RESET_INT()                   (USBD_ClearINT(URSTIF))
252 #define API_USB_IS_SOF_INT(flag)                  (flag & SOFIF)
253 #define API_USB_CLR_SOF_INT()                     (USBD_ClearINT(SOFIF))
254 #define API_USB_IS_FRES_INT(flag)                 (flag & FRESIF)
255 #define API_USB_CLR_FRES_INT()                    (USBD_ClearINT(FRESIF))
256 #define API_USB_IS_RESUME_INT(flag)               (flag & RSMIF)
257 #define API_USB_CLR_RESUME_INT()                  (USBD_ClearINT(RSMIF))
258 #define API_USB_IS_SUSPEND_INT(flag)              (flag & SUSPIF)
259 #define API_USB_CLR_SUSPEND_INT()                 (USBD_ClearINT(SUSPIF))
260 #define API_USB_IS_EPTn_INT(flag, EPTn)           (flag & (EP0IF << EPTn))
261 #define API_USB_CLR_EPTn_INT(EPTn)                (USBD_ClearINT(EP0IF << EPTn))
262 
263 /* API macro for USB Core - Endpoint event and operation                                                    */
264 #define API_USB_EPTn_INIT(EPTn, driver)           (USBD_EPTInit(EPTn, driver))
265 #define API_USB_EPTn_RESET(EPTn)                  (USBD_EPTReset(EPTn))
266 #define API_USB_EPTn_SEND_STALL(EPTn)             (USBD_EPTSendSTALL(EPTn))
267 #define API_USB_EPTn_GET_INT(EPTn)                (USBD_EPTGetINT(EPTn))
268 #define API_USB_EPTn_IS_IN_INT(flag)              (flag & IDTXIF)
269 #define API_USB_EPTn_CLR_IN_INT(EPTn)             (USBD_EPTClearINT(EPTn, IDTXIF))
270 #define API_USB_EPTn_IS_OUT_INT(flag)             (flag & ODRXIF)
271 #define API_USB_EPTn_CLR_OUT_INT(EPTn)            (USBD_EPTClearINT(EPTn, ODRXIF))
272 #define API_USB_EPTn_IS_INT(flag)                 (flag & (ODRXIF | IDTXIF))
273 #define API_USB_EPTn_CLR_INT(EPTn)                (USBD_EPTClearINT(EPTn, (ODRXIF | IDTXIF)))
274 #define API_USB_EPTn_GET_HALT(EPTn)               (USBD_EPTGetHalt(EPTn))
275 #define API_USB_EPTn_SET_HALT(EPTn)               (USBD_EPTSetHalt(EPTn))
276 #define API_USB_EPTn_CLR_HALT(EPTn)               (USBD_EPTClearHalt(EPTn))
277 #define API_USB_EPTn_WAIT_STALL_SENT(EPTn)        (USBD_EPTWaitSTALLSent(EPTn))
278 #define API_USB_EPTn_CLR_DTG(EPTn)                (USBD_EPTClearDTG(EPTn))
279 #define API_USB_EPTn_GET_BUFFLEN(EPTn)            (USBD_EPTGetBufferLen(EPTn))
280 #define API_USB_EPTn_GET_CNT(EPTn, type)          (USBD_EPTGetTransferCount(EPTn, type))
281 #define API_USB_EPTn_WRITE_IN(EPTn, from, len)    (USBD_EPTWriteINData(EPTn, from, len))
282 #define API_USB_EPTn_READ_OUT(EPTn, to, len)      (USBD_EPTReadOUTData(EPTn, to, len))
283 #define API_USB_EPTn_READ_MEM(EPTn, to, len)      (USBD_EPTReadMemory(EPTn, to, len))
284 /**
285   * @}
286   */
287 
288 /* Exported functions --------------------------------------------------------------------------------------*/
289 /** @defgroup USBDevice_Exported_Functions USB Device exported functions
290   * @{
291   */
292 void USBD_Init(u32 *pDriver);
293 void USBD_PreInit(USBD_Driver_TypeDef *pDriver);
294 void USBD_DPpullupCmd(ControlStatus NewState);
295 void USBD_DPWakeUpCmd(ControlStatus NewState);
296 void USBD_DeInit(void);
297 void USBD_PowerUp(u32 *pDriver, u32 uIsSelfPowered);
298 void USBD_PowerOff(void);
299 void USBD_PowerOn(void);
300 void USBD_SRAMResetConditionCmd(ControlStatus NewState);
301 void USBD_DisableDefaultPull(void);
302 void USBD_RemoteWakeup(void);
303 void USBD_ReadSETUPData(u32 *pBuffer);
304 void USBD_SetAddress(u32 address);
305 void USBD_EnableINT(u32 INTFlag);
306 void USBD_DisableINT(u32 INTFlag);
307 u32  USBD_GetINT(void);
308 void USBD_ClearINT(u32 INTFlag);
309 USBD_EPTn_Enum USBD_GetEPTnINTNumber(u32 INTFlag);
310 
311 void USBD_EPTInit(USBD_EPTn_Enum USBD_EPTn, u32 *pDriver);
312 void USBD_EPTReset(USBD_EPTn_Enum USBD_EPTn);
313 void USBD_EPTEnableINT(USBD_EPTn_Enum USBD_EPTn, u32 INTFlag);
314 u32  USBD_EPTGetINT(USBD_EPTn_Enum USBD_EPTn);
315 void USBD_EPTClearINT(USBD_EPTn_Enum USBD_EPTn, u32 INTFlag);
316 void USBD_EPTSendSTALL(USBD_EPTn_Enum USBD_EPTn);
317 u32  USBD_EPTGetHalt(USBD_EPTn_Enum USBD_EPTn);
318 void USBD_EPTSetHalt(USBD_EPTn_Enum USBD_EPTn);
319 void USBD_EPTClearHalt(USBD_EPTn_Enum USBD_EPTn);
320 void USBD_EPTWaitSTALLSent(USBD_EPTn_Enum USBD_EPTn);
321 void USBD_EPTClearDTG(USBD_EPTn_Enum USBD_EPTn);
322 u32  USBD_EPTGetBuffer0Addr(USBD_EPTn_Enum USBD_EPTn);
323 u32  USBD_EPTGetBuffer1Addr(USBD_EPTn_Enum USBD_EPTn);
324 u32  USBD_EPTGetBufferLen(USBD_EPTn_Enum USBD_EPTn);
325 u32  USBD_EPTGetTransferCount(USBD_EPTn_Enum USBD_EPTn, USBD_TCR_Enum USBD_TCR_0or1);
326 u32  USBD_EPTWriteINData(USBD_EPTn_Enum USBD_EPTn, u32 *pFrom, u32 len);
327 u32  USBD_EPTReadOUTData(USBD_EPTn_Enum USBD_EPTn, u32 *pTo, u32 len);
328 u32  USBD_EPTReadMemory(USBD_EPTn_Enum USBD_EPTn, u32 *pTo, u32 len);
329 /**
330   * @}
331   */
332 
333 
334 /**
335   * @}
336   */
337 
338 /**
339   * @}
340   */
341 
342 #ifdef __cplusplus
343 }
344 #endif
345 
346 #endif
347