1 /* 2 * SPDX-FileCopyrightText: 2019-2025 SiFli Technologies(Nanjing) Co., Ltd 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /* Define to prevent recursive inclusion -------------------------------------*/ 8 #ifndef BF0_HAL_HCD_H 9 #define BF0_HAL_HCD_H 10 11 #ifdef __cplusplus 12 extern "C" { 13 #endif 14 /* Includes ------------------------------------------------------------------*/ 15 #include "bf0_hal_def.h" 16 17 /** @addtogroup BF0_HAL_Driver 18 * @{ 19 */ 20 21 /** @addtogroup HCD HCD 22 * @{ 23 */ 24 25 /* Exported types ------------------------------------------------------------*/ 26 /** @defgroup HCD_Exported_Types HCD Exported Types 27 * @{ 28 */ 29 30 /** @defgroup HCD_Exported_Types_Group1 HCD State Structure definition 31 * @{ 32 */ 33 typedef enum 34 { 35 HAL_HCD_STATE_RESET = 0x00, 36 HAL_HCD_STATE_READY = 0x01, 37 HAL_HCD_STATE_ERROR = 0x02, 38 HAL_HCD_STATE_BUSY = 0x03, 39 HAL_HCD_STATE_TIMEOUT = 0x04 40 } HCD_StateTypeDef; 41 42 43 typedef enum 44 { 45 HAL_HCD_EP0_IDLE, /*!< Idle, waiting for setup*/ 46 HAL_HCD_EP0_SETUP, /*!< Sent setup*/ 47 HAL_HCD_EP0_TX, /*!< OUT data*/ 48 HAL_HCD_EP0_RX, /*!< IN data*/ 49 } HCD_EP0_StateTypeDef; 50 51 52 typedef enum 53 { 54 USB_DEVICE_MODE = 0, 55 USB_HOST_MODE = 1, 56 USB_DRD_MODE = 2 57 } USB_OTG_ModeTypeDef; 58 59 /** 60 * @brief URB States definition 61 */ 62 typedef enum 63 { 64 URB_IDLE = 0, 65 URB_DONE, 66 URB_NOTREADY, 67 URB_NYET, 68 URB_ERROR, 69 URB_STALL 70 } USB_OTG_URBStateTypeDef; 71 72 /** 73 * @brief Host channel States definition 74 */ 75 typedef enum 76 { 77 HC_IDLE = 0, 78 HC_XFRC, 79 HC_HALTED, 80 HC_NAK, 81 HC_NYET, 82 HC_STALL, 83 HC_XACTERR, 84 HC_BBLERR, 85 HC_DATATGLERR 86 } USB_OTG_HCStateTypeDef; 87 88 /** 89 * @brief USB Instance Initialization Structure definition 90 */ 91 typedef struct 92 { 93 uint32_t dev_endpoints; /*!< Device Endpoints number. 94 This parameter depends on the used USB core. 95 This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ 96 97 uint32_t Host_channels; /*!< Host Channels number. 98 This parameter Depends on the used USB core. 99 This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ 100 101 uint32_t speed; /*!< USB Core speed. */ 102 103 uint32_t dma_enable; /*!< Enable or disable of the USB embedded DMA used only for OTG HS. */ 104 105 uint32_t ep0_mps; /*!< Set the Endpoint 0 Max Packet size. */ 106 107 uint32_t phy_itface; /*!< Select the used PHY interface. */ 108 109 uint32_t Sof_enable; /*!< Enable or disable the output of the SOF signal. */ 110 111 uint32_t low_power_enable; /*!< Enable or disable the low power mode. */ 112 113 uint32_t lpm_enable; /*!< Enable or disable Link Power Management. */ 114 115 uint32_t battery_charging_enable; /*!< Enable or disable Battery charging. */ 116 117 uint32_t vbus_sensing_enable; /*!< Enable or disable the VBUS Sensing feature. */ 118 119 uint32_t use_dedicated_ep1; /*!< Enable or disable the use of the dedicated EP1 interrupt. */ 120 121 uint32_t use_external_vbus; /*!< Enable or disable the use of the external VBUS. */ 122 123 } USB_OTG_CfgTypeDef; 124 125 typedef struct 126 { 127 uint8_t num; /*!< Endpoint number 128 This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ 129 130 uint8_t is_in; /*!< Endpoint direction 131 This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 132 133 uint8_t is_stall; /*!< Endpoint stall condition 134 This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 135 136 uint8_t type; /*!< Endpoint type */ 137 138 uint8_t data_pid_start; /*!< Initial data PID 139 This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 140 141 uint8_t even_odd_frame; /*!< IFrame parity 142 This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 143 144 uint16_t tx_fifo_num; /*!< Transmission FIFO number 145 This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ 146 147 uint32_t maxpacket; /*!< Endpoint Max packet size 148 This parameter must be a number between Min_Data = 0 and Max_Data = 64KB */ 149 150 uint8_t *xfer_buff; /*!< Pointer to transfer buffer */ 151 152 uint32_t dma_addr; /*!< 32 bits aligned transfer buffer address */ 153 154 uint32_t xfer_len; /*!< Current transfer length */ 155 156 uint32_t xfer_count; /*!< Partial transfer length in case of multi packet transfer */ 157 } USB_OTG_EPTypeDef; 158 159 typedef struct 160 { 161 uint8_t dev_addr; /*!< USB device address. 162 This parameter must be a number between Min_Data = 1 and Max_Data = 255 */ 163 164 uint8_t ch_num; /*!< Host channel number. 165 This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ 166 167 uint8_t ep_num; /*!< Endpoint number. 168 This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ 169 170 uint8_t ep_is_in; /*!< Endpoint direction 171 This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 172 173 uint8_t speed; /*!< USB Host speed. */ 174 175 uint8_t do_ping; /*!< Enable or disable the use of the PING protocol for HS mode. */ 176 177 uint8_t process_ping; /*!< Execute the PING protocol for HS mode. */ 178 179 uint8_t ep_type; /*!< Endpoint Type. */ 180 181 uint16_t max_packet; /*!< Endpoint Max packet size. 182 This parameter must be a number between Min_Data = 0 and Max_Data = 64KB */ 183 uint8_t max_packet_unfinish; /*!< Endpoint Max packet size for the last packet. 184 This parameter must be a number between Min_Data = 0 and Max_Data = 64KB */ 185 186 uint8_t data_pid; /*!< Initial data PID. 187 This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 188 189 uint8_t *xfer_buff; /*!< Pointer to transfer buffer. */ 190 191 uint32_t XferSize; /*!< OTG Channel transfer size. */ 192 193 uint32_t xfer_len; /*!< Current transfer length. */ 194 195 uint32_t xfer_count; /*!< Partial transfer length in case of multi packet transfer. */ 196 197 uint8_t toggle_in; /*!< IN transfer current toggle flag. 198 This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 199 200 uint8_t toggle_out; /*!< OUT transfer current toggle flag 201 This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 202 203 uint32_t dma_addr; /*!< 32 bits aligned transfer buffer address. */ 204 205 uint32_t ErrCnt; /*!< Host channel error count. */ 206 207 USB_OTG_URBStateTypeDef urb_state; /*!< URB state. 208 This parameter can be any value of @ref USB_OTG_URBStateTypeDef */ 209 210 USB_OTG_HCStateTypeDef state; /*!< Host Channel state. 211 This parameter can be any value of @ref USB_OTG_HCStateTypeDef */ 212 } USB_OTG_HCTypeDef; 213 214 typedef USBC_X_Typedef HCD_TypeDef; 215 typedef USBC_X_Typedef USB_OTG_GlobalTypeDef; 216 typedef USB_OTG_CfgTypeDef HCD_InitTypeDef; 217 typedef USB_OTG_HCTypeDef HCD_HCTypeDef; 218 typedef USB_OTG_URBStateTypeDef HCD_URBStateTypeDef; 219 typedef USB_OTG_HCStateTypeDef HCD_HCStateTypeDef; 220 /** 221 * @} 222 */ 223 224 /** @defgroup HCD_Exported_Types_Group2 HCD Handle Structure definition 225 * @{ 226 */ 227 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) 228 typedef struct __HCD_HandleTypeDef 229 #else 230 typedef struct 231 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ 232 { 233 HCD_TypeDef *Instance; /*!< Register base address */ 234 HCD_InitTypeDef Init; /*!< HCD required parameters */ 235 HCD_HCTypeDef hc[16]; /*!< Host channels parameters */ 236 HAL_LockTypeDef Lock; /*!< HCD peripheral status */ 237 __IO HCD_StateTypeDef State; /*!< HCD communication state */ 238 __IO HCD_EP0_StateTypeDef ep0_state; /*!< HCD EP0 state */ 239 __IO uint32_t ErrorCode; /*!< HCD Error code */ 240 void *pData; /*!< Pointer Stack Handler */ 241 uint16_t epmask; /*!< End point enabled */ 242 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) 243 void (* SOFCallback)(struct __HCD_HandleTypeDef *hhcd); /*!< USB OTG HCD SOF callback */ 244 void (* ConnectCallback)(struct __HCD_HandleTypeDef *hhcd); /*!< USB OTG HCD Connect callback */ 245 void (* DisconnectCallback)(struct __HCD_HandleTypeDef *hhcd); /*!< USB OTG HCD Disconnect callback */ 246 void (* PortEnabledCallback)(struct __HCD_HandleTypeDef *hhcd); /*!< USB OTG HCD Port Enable callback */ 247 void (* PortDisabledCallback)(struct __HCD_HandleTypeDef *hhcd); /*!< USB OTG HCD Port Disable callback */ 248 void (* HC_NotifyURBChangeCallback)(struct __HCD_HandleTypeDef *hhcd, uint8_t chnum, 249 HCD_URBStateTypeDef urb_state); /*!< USB OTG HCD Host Channel Notify URB Change callback */ 250 251 void (* MspInitCallback)(struct __HCD_HandleTypeDef *hhcd); /*!< USB OTG HCD Msp Init callback */ 252 void (* MspDeInitCallback)(struct __HCD_HandleTypeDef *hhcd); /*!< USB OTG HCD Msp DeInit callback */ 253 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ 254 } HCD_HandleTypeDef; 255 /** 256 * @} 257 */ 258 259 /** 260 * @} 261 */ 262 263 /* Exported constants --------------------------------------------------------*/ 264 /** @defgroup HCD_Exported_Constants HCD Exported Constants 265 * @{ 266 */ 267 268 /** @defgroup HCD_Speed HCD Speed 269 * @{ 270 */ 271 #define HCD_SPEED_HIGH 0 272 #define HCD_SPEED_FULL 1 273 #define HCD_SPEED_LOW 2 274 275 #define HCD_DEVICE_SPEED_HIGH 0U 276 #define HCD_DEVICE_SPEED_FULL 1U 277 #define HCD_DEVICE_SPEED_LOW 2U 278 279 /** 280 * @} 281 */ 282 283 /** @defgroup HCD_PHY_Module HCD PHY Module 284 * @{ 285 */ 286 #define HCD_PHY_ULPI 1U 287 #define HCD_PHY_EMBEDDED 2U 288 /** 289 * @} 290 */ 291 292 /** @defgroup HCD_Error_Code_definition HCD Error Code definition 293 * @brief HCD Error Code definition 294 * @{ 295 */ 296 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) 297 #define HAL_HCD_ERROR_INVALID_CALLBACK (0x00000010U) /*!< Invalid Callback error */ 298 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ 299 300 /** 301 * @} 302 */ 303 304 /** 305 * @} 306 */ 307 308 /* Exported macro ------------------------------------------------------------*/ 309 /** @defgroup HCD_Exported_Macros HCD Exported Macros 310 * @brief macros to handle interrupts and specific clock configurations 311 * @{ 312 */ 313 #define __HAL_HCD_GET_FLAG(__HANDLE__, __INTERRUPT__) ((USB_ReadInterrupts((__HANDLE__)->Instance) & (__INTERRUPT__)) == (__INTERRUPT__)) 314 #define __HAL_HCD_CLEAR_FLAG(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->GINTSTS) = (__INTERRUPT__)) 315 #define __HAL_HCD_IS_INVALID_INTERRUPT(__HANDLE__) (USB_ReadInterrupts((__HANDLE__)->Instance) == 0U) 316 317 #define __HAL_HCD_CLEAR_HC_INT(chnum, __INTERRUPT__) (USBx_HC(chnum)->HCINT = (__INTERRUPT__)) 318 #define __HAL_HCD_MASK_HALT_HC_INT(chnum) (USBx_HC(chnum)->HCINTMSK &= ~USB_OTG_HCINTMSK_CHHM) 319 #define __HAL_HCD_UNMASK_HALT_HC_INT(chnum) (USBx_HC(chnum)->HCINTMSK |= USB_OTG_HCINTMSK_CHHM) 320 #define __HAL_HCD_MASK_ACK_HC_INT(chnum) (USBx_HC(chnum)->HCINTMSK &= ~USB_OTG_HCINTMSK_ACKM) 321 #define __HAL_HCD_UNMASK_ACK_HC_INT(chnum) (USBx_HC(chnum)->HCINTMSK |= USB_OTG_HCINTMSK_ACKM) 322 /** 323 * @} 324 */ 325 326 /* Exported functions --------------------------------------------------------*/ 327 /** @addtogroup HCD_Exported_Functions HCD Exported Functions 328 * @{ 329 */ 330 331 /** @defgroup HCD_Exported_Functions_Group1 Initialization and de-initialization functions 332 * @{ 333 */ 334 HAL_StatusTypeDef HAL_HCD_Init(HCD_HandleTypeDef *hhcd); 335 HAL_StatusTypeDef HAL_HCD_DeInit(HCD_HandleTypeDef *hhcd); 336 HAL_StatusTypeDef HAL_HCD_HC_Init(HCD_HandleTypeDef *hhcd, uint8_t ch_num, 337 uint8_t epnum, uint8_t dev_address, 338 uint8_t speed, uint8_t ep_type, uint16_t mps); 339 340 HAL_StatusTypeDef HAL_HCD_HC_Halt(HCD_HandleTypeDef *hhcd, uint8_t ch_num); 341 void HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd); 342 void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hhcd); 343 344 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) 345 /** @defgroup HAL_HCD_Callback_ID_enumeration_definition HAL USB OTG HCD Callback ID enumeration definition 346 * @brief HAL USB OTG HCD Callback ID enumeration definition 347 * @{ 348 */ 349 typedef enum 350 { 351 HAL_HCD_SOF_CB_ID = 0x01, /*!< USB HCD SOF callback ID */ 352 HAL_HCD_CONNECT_CB_ID = 0x02, /*!< USB HCD Connect callback ID */ 353 HAL_HCD_DISCONNECT_CB_ID = 0x03, /*!< USB HCD Disconnect callback ID */ 354 HAL_HCD_PORT_ENABLED_CB_ID = 0x04, /*!< USB HCD Port Enable callback ID */ 355 HAL_HCD_PORT_DISABLED_CB_ID = 0x05, /*!< USB HCD Port Disable callback ID */ 356 357 HAL_HCD_MSPINIT_CB_ID = 0x06, /*!< USB HCD MspInit callback ID */ 358 HAL_HCD_MSPDEINIT_CB_ID = 0x07 /*!< USB HCD MspDeInit callback ID */ 359 360 } HAL_HCD_CallbackIDTypeDef; 361 /** 362 * @} 363 */ 364 365 /** @defgroup HAL_HCD_Callback_pointer_definition HAL USB OTG HCD Callback pointer definition 366 * @brief HAL USB OTG HCD Callback pointer definition 367 * @{ 368 */ 369 370 typedef void (*pHCD_CallbackTypeDef)(HCD_HandleTypeDef *hhcd); /*!< pointer to a common USB OTG HCD callback function */ 371 typedef void (*pHCD_HC_NotifyURBChangeCallbackTypeDef)(HCD_HandleTypeDef *hhcd, 372 uint8_t epnum, 373 HCD_URBStateTypeDef urb_state); /*!< pointer to USB OTG HCD host channel callback */ 374 /** 375 * @} 376 */ 377 378 HAL_StatusTypeDef HAL_HCD_RegisterCallback(HCD_HandleTypeDef *hhcd, 379 HAL_HCD_CallbackIDTypeDef CallbackID, 380 pHCD_CallbackTypeDef pCallback); 381 382 HAL_StatusTypeDef HAL_HCD_UnRegisterCallback(HCD_HandleTypeDef *hhcd, 383 HAL_HCD_CallbackIDTypeDef CallbackID); 384 385 HAL_StatusTypeDef HAL_HCD_RegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd, 386 pHCD_HC_NotifyURBChangeCallbackTypeDef pCallback); 387 388 HAL_StatusTypeDef HAL_HCD_UnRegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd); 389 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ 390 /** 391 * @} 392 */ 393 394 /* I/O operation functions ***************************************************/ 395 /** @addtogroup HCD_Exported_Functions_Group2 Input and Output operation functions 396 * @{ 397 */ 398 HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd, uint8_t ch_num, 399 uint8_t direction, uint8_t ep_type, 400 uint8_t token, uint8_t *pbuff, 401 uint16_t length, uint8_t do_ping); 402 403 /* Non-Blocking mode: Interrupt */ 404 void HAL_HCD_IRQHandler(HCD_HandleTypeDef *hhcd); 405 void HAL_HCD_WKUP_IRQHandler(HCD_HandleTypeDef *hhcd); 406 void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd); 407 void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd); 408 void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd); 409 void HAL_HCD_PortEnabled_Callback(HCD_HandleTypeDef *hhcd); 410 void HAL_HCD_PortDisabled_Callback(HCD_HandleTypeDef *hhcd); 411 void HAL_HCD_Timerout_Callback(HCD_HandleTypeDef *hhcd); 412 void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, 413 HCD_URBStateTypeDef urb_state); 414 /** 415 * @} 416 */ 417 418 /* Peripheral Control functions **********************************************/ 419 /** @addtogroup HCD_Exported_Functions_Group3 Peripheral Control functions 420 * @{ 421 */ 422 HAL_StatusTypeDef HAL_HCD_ResetPort(HCD_HandleTypeDef *hhcd); 423 HAL_StatusTypeDef HAL_HCD_Start(HCD_HandleTypeDef *hhcd); 424 HAL_StatusTypeDef HAL_HCD_Stop(HCD_HandleTypeDef *hhcd); 425 /** 426 * @} 427 */ 428 429 /* Peripheral State functions ************************************************/ 430 /** @addtogroup HCD_Exported_Functions_Group4 Peripheral State functions 431 * @{ 432 */ 433 HCD_StateTypeDef HAL_HCD_GetState(HCD_HandleTypeDef *hhcd); 434 HCD_URBStateTypeDef HAL_HCD_HC_GetURBState(HCD_HandleTypeDef *hhcd, uint8_t chnum); 435 HCD_HCStateTypeDef HAL_HCD_HC_GetState(HCD_HandleTypeDef *hhcd, uint8_t chnum); 436 uint32_t HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef *hhcd, uint8_t chnum); 437 uint32_t HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef *hhcd); 438 uint32_t HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef *hhcd); 439 440 /** 441 * @} 442 */ 443 444 /** 445 * @} 446 */ 447 448 /* Private macros ------------------------------------------------------------*/ 449 /** @defgroup HCD_Private_Macros HCD Private Macros 450 * @{ 451 */ 452 /** 453 * @} 454 */ 455 /* Private functions prototypes ----------------------------------------------*/ 456 457 /** 458 * @} 459 */ 460 /** 461 * @} 462 */ 463 464 #ifdef __cplusplus 465 } 466 #endif 467 468 469 #endif /* __BF0_HAL_PCD_H */