1 2 3 4 /* Define to prevent recursive inclusion -------------------------------------------------------------------*/ 5 #ifndef __USBD_CODE_H 6 #define __USBD_CODE_H 7 8 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 /* Includes ------------------------------------------------------------------------------------------------*/ 13 #include "ht32.h" 14 /** @addtogroup HT32_USBD_Library 15 * @{ 16 */ 17 18 /** @addtogroup USBDCore 19 * @{ 20 */ 21 22 23 /* Settings ------------------------------------------------------------------------------------------------*/ 24 /** @defgroup USBDCore_Settings USB Device Core settings 25 * @{ 26 */ 27 /* USBD Debug mode */ 28 //<e0.0> Enable USB Debug mode 29 //<o1.0> Dump USB Debug data 30 #ifndef USBDCORE_DEBUG 31 #define USBDCORE_DEBUG (0) /*!< Enable USB Debug mode */ 32 #define USBDCORE_DEBUG_DATA (0) /*!< Dump USB Debug data */ 33 #endif 34 /** 35 * @} 36 */ 37 38 /* Exported types ------------------------------------------------------------------------------------------*/ 39 /** @defgroup USBDCore_Exported_Type USB Device Core exported types 40 * @{ 41 */ 42 /** 43 * @brief USB Device Request. 44 */ 45 typedef __PACKED_H struct 46 { 47 uc8 bmRequestType; 48 uc8 bRequest; 49 uc8 wValueL; 50 uc8 wValueH; 51 uc16 wIndex; 52 uc16 wLength; 53 } __PACKED_F USBDCore_Request_TypeDef; 54 55 /** 56 * @brief USB Descriptor. 57 */ 58 typedef struct 59 { 60 uc8 *pDeviceDesc; /*!< Device Descriptor */ 61 uc8 *pConfnDesc; /*!< Configuration Descriptor */ 62 uc8 **ppStringDesc; /*!< String Descriptor */ 63 u32 uStringDescNumber; /*!< Count of String Descriptor */ 64 } USBDCore_Desc_TypeDef; 65 66 /** 67 * @brief STALL, control IN or control OUT. 68 */ 69 typedef enum 70 { 71 USB_ACTION_STALL = 0, 72 USB_ACTION_DATAIN = 1, 73 USB_ACTION_DATAOUT = 2, 74 } USBDCore_Action_Enum; 75 76 /** 77 * @brief Call back function. 78 */ 79 typedef struct 80 { 81 void (*func) (u32 uPara); /*!< Call back function pointer */ 82 u32 uPara; /*!< Parameter of call back function */ 83 } USBDCore_CallBack_TypeDef; 84 85 /** 86 * @brief Parameter for control IN/OUT Transfer. 87 */ 88 typedef struct 89 { 90 u8 uBuffer[2]; /*!< Temporary buffer */ 91 uc8 *pData; /*!< Pointer of control IN/OUT Data */ 92 s32 sByteLength; /*!< Total length for control IN/OUT Transfer */ 93 USBDCore_Action_Enum Action; /*!< STALL, control IN or control OUT */ 94 USBDCore_CallBack_TypeDef CallBack_OUT; /*!< Call back function pointer for Control OUT */ 95 } USBDCore_Transfer_TypeDef; 96 97 /** 98 * @brief USB Device. 99 */ 100 typedef struct 101 { 102 USBDCore_Request_TypeDef Request; /*!< USB Device Request */ 103 USBDCore_Desc_TypeDef Desc; /*!< USB Descriptor */ 104 USBDCore_Transfer_TypeDef Transfer; /*!< Parameter for control IN/OUT Transfer */ 105 } USBDCore_Device_TypeDef; 106 107 /** 108 * @brief Bit access for CurrentFeature. 109 */ 110 typedef __PACKED_H struct _FEATURE_TYPEBIT 111 { 112 unsigned bSelfPowered :1; /*!< Remote Wakeup feature */ 113 unsigned bRemoteWakeup :1; /*!< Self Powered */ 114 } __PACKED_F USBDCore_Feature_TypeBit; 115 116 /** 117 * @brief For Set/ClearFeature and GetStatus request. 118 */ 119 typedef __PACKED_H union _FEATURE_TYPEDEF 120 { 121 u8 uByte; /*!< Byte access for CurrentFeature */ 122 USBDCore_Feature_TypeBit Bits; /*!< Bit access for CurrentFeature */ 123 } __PACKED_F USBDCore_Feature_TypeDef; 124 125 /** 126 * @brief Device State. 127 */ 128 typedef enum 129 { 130 USER_USB_STATE_UNCONNECTED = 0, 131 USER_USB_STATE_ATTACHED = 1, 132 USER_USB_STATE_POWERED = 2, 133 USER_USB_STATE_SUSPENDED = 3, 134 USER_USB_STATE_DEFAULT = 4, 135 USER_USB_STATE_ADDRESS = 5, 136 USER_USB_STATE_CONFIGURED = 6, 137 } USBDCore_Status_Enum; 138 139 /** 140 * @brief USB Device information. 141 */ 142 typedef struct 143 { 144 u8 uCurrentConfiguration; /*!< For Set/GetConfiguration request */ 145 u8 uCurrentInterface; /*!< For Set/GetInterface request */ 146 volatile USBDCore_Status_Enum CurrentStatus; /*!< Device State */ 147 USBDCore_Status_Enum LastStatus; /*!< Device State before SUSPEND */ 148 USBDCore_Feature_TypeDef CurrentFeature; /*!< For Set/ClearFeature and GetStatus request */ 149 u32 uIsDiscardClearFeature; /*!< Discard ClearFeature flag for Mass Storage */ 150 } USBDCore_Info_TypeDef; 151 152 typedef void (*USBDCore_CallBackClass_Typedef) (USBDCore_Device_TypeDef *pDev); 153 typedef void (*USBDCore_CallBackVendor_Typedef) (USBDCore_Device_TypeDef *pDev); 154 typedef void (*USBDCore_CallBackEPTn_Typedef) (USBD_EPTn_Enum EPTn); 155 156 /** 157 * @brief USB Class call back function. 158 */ 159 typedef struct 160 { 161 USBDCore_CallBack_TypeDef CallBack_MainRoutine; /*!< Class main routine call back function */ 162 USBDCore_CallBack_TypeDef CallBack_Reset; /*!< Class RESET call back function */ 163 USBDCore_CallBack_TypeDef CallBack_StartOfFrame; /*!< Class SOF call back function */ 164 USBDCore_CallBackClass_Typedef CallBack_ClassGetDescriptor; /*!< Class Get Descriptor call back function */ 165 USBDCore_CallBackClass_Typedef CallBack_ClassSetInterface; /*!< Set Interface call back function */ 166 USBDCore_CallBackClass_Typedef CallBack_ClassGetInterface; /*!< Get Interface call back function */ 167 USBDCore_CallBackClass_Typedef CallBack_ClassRequest; /*!< Class Request call back function */ 168 USBDCore_CallBackVendor_Typedef CallBack_VendorRequest; /*!< Vendor Request call back function */ 169 USBDCore_CallBackEPTn_Typedef CallBack_EPTn[MAX_EP_NUM]; /*!< Endpoint n call back function */ 170 } USBDCore_Class_TypeDef; 171 172 /** 173 * @brief USB Device Power related call back function. 174 */ 175 typedef struct 176 { 177 USBDCore_CallBack_TypeDef CallBack_Suspend; 178 } USBDCore_Power_TypeDef; 179 180 /** 181 * @brief USB EP TypeDef. 182 */ 183 typedef struct 184 { 185 uint16_t maxpacket; /*!< endpoint max packet*/ 186 uint16_t status; /*!< endpoint status */ 187 /* transmission buffer and count */ 188 uint16_t total_len; /*!< endpoint transmission total length */ 189 uint16_t trans_len; /*!< endpoint transmission length*/ 190 uint8_t *trans_buf; /*!< endpoint transmission buffer */ 191 } usb_ept_info; 192 193 194 195 /** 196 * @brief Major structure of USB Library. 197 */ 198 typedef struct 199 { 200 USBDCore_Device_TypeDef Device; /*!< USB Device */ 201 USBDCore_Info_TypeDef Info; /*!< USB Device information */ 202 USBDCore_Class_TypeDef Class; /*!< USB Class call back function */ 203 u32 *pDriver; /*!< USB Device Driver initialization structure */ 204 USBDCore_Power_TypeDef Power; /*!< USB Device Power related call back function */ 205 206 usb_ept_info ept_io[8]; /*!< */ 207 void *pdata; /*!< USB User private pointer */ 208 } USBDCore_TypeDef; 209 210 /*----------------------------------------------------------------------------------------------------------*/ 211 /* Variable architecture of USB Library */ 212 /*----------------------------------------------------------------------------------------------------------*/ 213 /* USBCore - USBDCore_TypeDef Major structure of USB Library */ 214 /* USBCore.Device - USBDCore_Device_TypeDef USB Device */ 215 /* USBCore.Device.Request - USBDCore_Request_TypeDef USB Device Request */ 216 /* USBCore.Device.Request.bmRequestType */ 217 /* USBCore.Device.Request.bRequest */ 218 /* USBCore.Device.Request.wValueL */ 219 /* USBCore.Device.Request.wValueH */ 220 /* USBCore.Device.Request.wIndex */ 221 /* USBCore.Device.Request.wLength */ 222 /* USBCore.Device.Desc - USBDCore_Desc_TypeDef USB Descriptor */ 223 /* USBCore.Device.Desc.pDeviceDesc Device Descriptor */ 224 /* USBCore.Device.Desc.pConfnDesc Configuration Descriptor */ 225 /* USBCore.Device.Desc.pStringDesc[DESC_NUM_STRING] String Descriptor */ 226 /* USBCore.Device.Desc.uStringDescNumber Count of String Descriptor */ 227 /* USBCore.Device.Transfer - USBDCore_Transfer_TypeDef Parameter for control IN/OUT Transfer */ 228 /* USBCore.Device.Transfer.uBuffer[2] Temporary buffer */ 229 /* USBCore.Device.Transfer.pData Pointer of control IN/OUT Data */ 230 /* USBCore.Device.Transfer.sByteLength Total length for control IN/OUT Transfer */ 231 /* USBCore.Device.Transfer.Action - USBDCore_Action_Enum STALL, control IN or control OUT */ 232 /* USBCore.Device.Transfer.CallBack_OUT.func(uPara) Call back function pointer for Control OUT */ 233 /* USBCore.Device.Transfer.CallBack_OUT.uPara Parameter of Control OUT call back function */ 234 /* */ 235 /* USBCore.Info - USBDCore_Info_TypeDef USB Device information */ 236 /* USBCore.Info.uCurrentConfiguration For Set/GetConfiguration request */ 237 /* USBCore.Info.uCurrentInterface For Set/GetInterface request */ 238 /* USBCore.Info.CurrentStatus - USBDCore_Status_Enum Device State */ 239 /* USBCore.Info.LastStatus - USBDCore_Status_Enum Device State before SUSPEND */ 240 /* USBCore.Info.CurrentFeature - USBDCore_Feature_TypeDef For Set/ClearFeature and GetStatus request */ 241 /* USBCore.Info.CurrentFeature.uByte Byte access for CurrentFeature */ 242 /* USBCore.Info.CurrentFeature.Bits.bRemoteWakeup Remote Wakeup feature */ 243 /* USBCore.Info.CurrentFeature.Bits.bSelfPowered Self Powered */ 244 /* USBCore.Info.uIsDiscardClearFeature Discard ClearFeature flag for Mass Storage */ 245 /* */ 246 /* USBCore.Class - USBDCore_Class_TypeDef USB Class call back function */ 247 /* USBCore.Class.CallBack_MainRoutine.func(uPara) Class main routine call back function */ 248 /* USBCore.Class.CallBack_MainRoutine.uPara Parameter of class main routine */ 249 /* USBCore.Class.CallBack_Reset.func(uPara) Class RESET call back function */ 250 /* USBCore.Class.CallBack_Reset.uPara Parameter of RESET call back function */ 251 /* USBCore.Class.CallBack_StartOfFrame.func(uPara) Class SOF call back function */ 252 /* USBCore.Class.CallBack_StartOfFrame.uPara Parameter of SOF call back function */ 253 /* USBCore.Class.CallBack_ClassGetDescriptor(pDev) Class Get Descriptor call back function */ 254 /* USBCore.Class.CallBack_ClassSetInterface(pDev) Set Interface call back function */ 255 /* USBCore.Class.CallBack_ClassGetInterface(pDev) Get Interface call back function */ 256 /* USBCore.Class.CallBack_ClassRequest(pDev) Class Request call back function */ 257 /* USBCore.Class.CallBack_EPTn[MAX_EP_NUM](EPTn) Endpoint n call back function */ 258 /* */ 259 /* USBCore.pDriver USB Device Driver initialization structure */ 260 /* */ 261 /* USBCore.Power - USBDCore_Power_TypeDef USB Device Power related call back function */ 262 /* USBCore.Power.CallBack_Suspend.func(uPara) System low power function for SUSPEND */ 263 /* USBCore.Power.CallBack_Suspend.uPara Parameter of system low power function */ 264 /*----------------------------------------------------------------------------------------------------------*/ 265 266 /** 267 * @} 268 */ 269 270 /* Exported constants --------------------------------------------------------------------------------------*/ 271 /** @defgroup USBDCore_Exported_Constant USB Device Core exported constants 272 * @{ 273 */ 274 275 /** @defgroup USBDCore_Descriptor Definitions for USB descriptor 276 * @{ 277 */ 278 #define DESC_TYPE_01_DEV (0x1) 279 #define DESC_TYPE_02_CONFN (0x2) 280 #define DESC_TYPE_03_STR (0x3) 281 #define DESC_TYPE_04_INF (0x4) 282 #define DESC_TYPE_05_EPT (0x5) 283 #define DESC_TYPE_06_DEV_QLF (0x6) 284 #define DESC_TYPE_08_INF_PWR (0x8) 285 286 #define DESC_CLASS_00_BY_INF (0x00) 287 #define DESC_CLASS_01_AUDIO (0x01) 288 #define DESC_CLASS_02_CDC_CTRL (0x02) 289 #define DESC_CLASS_03_HID (0x03) 290 #define DESC_CLASS_05_PHY (0x05) 291 #define DESC_CLASS_06_STILL_IMG (0x06) 292 #define DESC_CLASS_07_PRINTER (0x07) 293 #define DESC_CLASS_08_MASS_STORAGE (0x08) 294 #define DESC_CLASS_09_HUB (0x09) 295 #define DESC_CLASS_0A_CDC_DATA (0x0A) 296 #define DESC_CLASS_0B_SMART_CARD (0x0B) 297 #define DESC_CLASS_0E_VIDEO (0x0E) 298 #define DESC_CLASS_0F_PHD (0x0F) 299 #define DESC_CLASS_FF_VENDOR (0xFF) 300 301 #define DESC_LEN_DEV ((u32)(18)) 302 #define DESC_LEN_CONFN ((u32)(9)) 303 #define DESC_LEN_INF ((u32)(9)) 304 #define DESC_LEN_EPT ((u32)(7)) 305 /** 306 * @} 307 */ 308 309 /** @defgroup USBDCore_Request Definitions for USB Request 310 * @{ 311 */ 312 #define REQ_DIR_00_H2D (0 << 7) 313 #define REQ_DIR_01_D2H (1 << 7) 314 315 #define REQ_TYPE_00_STD (0 << 5) 316 #define REQ_TYPE_01_CLS (1 << 5) 317 #define REQ_TYPE_02_VND (2 << 5) 318 319 #define REQ_REC_00_DEV (0) 320 #define REQ_REC_01_INF (1) 321 #define REQ_REC_02_EPT (2) 322 /** 323 * @} 324 */ 325 326 /** 327 * @brief For USBDCore_EPTReadOUTData function. 328 */ 329 #define USB_DISCARD_OUT_DATA (0) 330 331 /** 332 * @} 333 */ 334 335 /* Exported macro ------------------------------------------------------------------------------------------*/ 336 /** @defgroup USBDCore_Exported_Macro USB Device Core exported macros 337 * @{ 338 */ 339 #define __DBG_USBPrintf(...) 340 #define __DBG_USBDump(a, b) 341 342 #if (USBDCORE_DEBUG == 1) 343 #ifndef RETARGET_IS_USB 344 extern u32 __DBG_USBCount; 345 #undef __DBG_USBPrintf 346 #define __DBG_USBPrintf printf 347 #if (USBDCORE_DEBUG_DATA == 1) 348 #undef __DBG_USBDump 349 void __DBG_USBDump(uc8 *memory, u32 len); 350 #endif 351 #endif 352 #endif 353 354 /** 355 * @brief Convert Half-Word to Byte for descriptor. 356 */ 357 #define DESC_H2B(Val) ((u8)(Val & 0x00FF)), ((u8)((Val & 0xFF00) >> 8)) 358 359 /** 360 * @brief Padding 0 automatically for String descriptor. 361 */ 362 #define DESC_CHAR(c) (c), (0) 363 364 /** 365 * @brief Calculate String length for String descriptor. 366 */ 367 #define DESC_STRLEN(n) (n * 2 + 2) 368 369 /** 370 * @brief Calculate power for Configuration descriptor. 371 */ 372 #define DESC_POWER(mA) (mA / 2) 373 /** 374 * @} 375 */ 376 377 /* Exported functions --------------------------------------------------------------------------------------*/ 378 /** @defgroup USBDCore_Exported_Functions USB Device Core exported functions 379 * @{ 380 */ 381 #define USBDCore_DeInit API_USB_DEINIT 382 #define USBDCore_EPTReset API_USB_EPTn_RESET 383 #define USBDCore_EPTGetBufferLen API_USB_EPTn_GET_BUFFLEN 384 #define USBDCore_EPTGetTransferCount API_USB_EPTn_GET_CNT 385 #define USBDCore_EPTSetSTALL API_USB_EPTn_SET_HALT 386 #define USBDCore_EPTWaitSTALLSent API_USB_EPTn_WAIT_STALL_SENT 387 #define USBDCore_EPTClearDataToggle API_USB_EPTn_CLR_DTG 388 389 #define USBDCore_EPTWriteINData API_USB_EPTn_WRITE_IN 390 #define USBDCore_EPTReadOUTData API_USB_EPTn_READ_OUT 391 #define USBDCore_EPTReadMemory API_USB_EPTn_READ_MEM 392 393 394 void usbd_irq_handler(USBDCore_TypeDef *pCore); 395 void USBDCore_Init(USBDCore_TypeDef *pCore); 396 void USBDCore_MainRoutine(USBDCore_TypeDef *pCore); 397 398 void usbd_ep_enable(USBDCore_TypeDef *pCore, uint8_t ept_addr); 399 void usbd_ep_disable(USBDCore_TypeDef *pCore, uint8_t ept_addr); 400 void usbd_ept_recv(USBDCore_TypeDef *udev, uint8_t ept_addr, uint8_t *buffer, uint16_t len); 401 //u32 USBDCore_IsSuspend(USBDCore_TypeDef *pCore); 402 //u32 USBDCore_GetRemoteWakeUpFeature(USBDCore_TypeDef *pCore); 403 //void USBDCore_TriggerRemoteWakeup(void); 404 //USBDCore_Status_Enum USBDCore_GetStatus(void); 405 406 void USBDCore_EPTReset(USBD_EPTn_Enum USBD_EPTn); 407 u32 USBDCore_EPTGetBufferLen(USBD_EPTn_Enum USBD_EPTn); 408 u32 USBDCore_EPTGetTransferCount(USBD_EPTn_Enum USBD_EPTn, USBD_TCR_Enum type); 409 void USBDCore_EPTSetSTALL(USBD_EPTn_Enum USBD_EPTn); 410 void USBDCore_EPTWaitSTALLSent(USBD_EPTn_Enum USBD_EPTn); 411 void USBDCore_EPTClearDataToggle(USBD_EPTn_Enum USBD_EPTn); 412 413 u32 USBDCore_EPTWriteINData(USBD_EPTn_Enum USBD_EPTn, u32 *pFrom, u32 len); 414 u32 USBDCore_EPTReadOUTData(USBD_EPTn_Enum USBD_EPTn, u32 *pTo, u32 len); 415 u32 USBDCore_EPTReadMemory(USBD_EPTn_Enum USBD_EPTn, u32 *pTo, u32 len); 416 /** 417 * @} 418 */ 419 420 421 /** 422 * @} 423 */ 424 425 /** 426 * @} 427 */ 428 429 #ifdef __cplusplus 430 } 431 #endif 432 433 #endif /* __USBD_CODE_H -------------------------------------------------------------------------------*/ 434 435