1 /* 2 * SPDX-FileCopyrightText: 2016 STMicroelectronics 3 * SPDX-FileCopyrightText: 2019-2025 SiFli Technologies(Nanjing) Co., Ltd 4 * 5 * SPDX-License-Identifier: BSD-3-Clause AND Apache-2.0 6 */ 7 8 #ifndef __BF0_HAL_PCD_H 9 #define __BF0_HAL_PCD_H 10 11 #ifdef __cplusplus 12 extern "C" { 13 #endif 14 15 /* Includes ------------------------------------------------------------------*/ 16 #include "bf0_hal_def.h" 17 18 19 typedef USBC_X_Typedef PCD_TypeDef; 20 21 22 /** @defgroup USB_LL_EP_Type USB Low Layer EP Type 23 * @ingroup PCD 24 * @{ 25 */ 26 #define EP_TYPE_CTRL 0x14U 27 #define EP_TYPE_BULKOUT 0x12U 28 #define EP_TYPE_BULKIN 0x11U 29 /** 30 * @} 31 */ 32 33 34 /** @addtogroup PCD USB Device 35 * @ingroup BF0_HAL_Driver 36 * @{ 37 */ 38 39 /* Exported types ------------------------------------------------------------*/ 40 /** @defgroup PCD_Exported_Types PCD Exported Types 41 * @{ 42 */ 43 44 /** 45 * @brief PCD State structure definition 46 */ 47 typedef enum 48 { 49 HAL_PCD_STATE_RESET = 0x00U, 50 HAL_PCD_STATE_READY = 0x01U, 51 HAL_PCD_STATE_ERROR = 0x02U, 52 HAL_PCD_STATE_BUSY = 0x03U, 53 HAL_PCD_STATE_TIMEOUT = 0x04U 54 } PCD_StateTypeDef; 55 56 57 /** 58 * @brief PCD PHY State 59 */ 60 typedef enum 61 { 62 OTG_STATE_A_SUSPEND, 63 OTG_STATE_A_HOST, 64 OTG_STATE_A_WAIT_BCON, 65 OTG_STATE_A_WAIT_VFALL, 66 OTG_STATE_A_WAIT_VRISE, 67 OTG_STATE_B_HOST, 68 OTG_STATE_B_PERIPHERALS, 69 OTG_STATE_B_WAIT_ACON, 70 OTG_STATE_B_IDLE, 71 } PCD_PhyStateTypeDef; 72 73 /** 74 * @brief PCD Endpont 0 State structure definition 75 */ 76 typedef enum 77 { 78 HAL_PCD_EP0_IDLE, /*!< Idle, waiting for setup*/ 79 HAL_PCD_EP0_SETUP, /*!< Received setup*/ 80 HAL_PCD_EP0_TX, /*!< IN data*/ 81 HAL_PCD_EP0_RX, /*!< OUT data*/ 82 HAL_PCD_EP0_STATUSIN, /*!< (After OUT data) */ 83 HAL_PCD_EP0_STATUSOUT, /*!< (After IN data) */ 84 HAL_PCD_EP0_ACKWAIT, /*!< After zlp, before statusin*/ 85 } PCD_EP0_StateTypeDef; 86 87 /** 88 * @brief PCD Initialization Structure definition 89 */ 90 typedef struct 91 { 92 uint32_t dev_endpoints; /*!< Device Endpoints number. 93 This parameter depends on the used USB core. 94 This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ 95 96 uint32_t speed; /*!< USB Core speed. 97 This parameter can be any value of @ref PCD_Core_Speed */ 98 99 uint32_t ep0_mps; /*!< Set the Endpoint 0 Max Packet size. 100 This parameter can be any value of @ref PCD_EP0_MPS */ 101 102 uint32_t phy_itface; /*!< Select the used PHY interface. 103 This parameter can be any value of @ref PCD_Core_PHY */ 104 105 uint32_t Sof_enable; /*!< Enable or disable the output of the SOF signal. 106 This parameter can be set to ENABLE or DISABLE */ 107 108 uint32_t low_power_enable; /*!< Enable or disable Low Power mode 109 This parameter can be set to ENABLE or DISABLE */ 110 111 uint32_t lpm_enable; /*!< Enable or disable the Link Power Management . 112 This parameter can be set to ENABLE or DISABLE */ 113 114 uint32_t battery_charging_enable; /*!< Enable or disable Battery charging. 115 This parameter can be set to ENABLE or DISABLE */ 116 117 } PCD_InitTypeDef; 118 119 typedef struct 120 { 121 uint8_t num; /*!< Endpoint number 122 This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ 123 124 uint8_t is_in; /*!< Endpoint direction 125 This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 126 127 uint8_t is_stall; /*!< Endpoint stall condition 128 This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 129 130 uint8_t type; /*!< Endpoint type 131 This parameter can be any value of @ref PCD_EP_Type */ 132 133 void *dma_cfg; /*< DMA Config */ 134 uint32_t pmaadress; 135 uint32_t maxpacket; /*!< Endpoint Max packet size 136 This parameter must be a number between Min_Data = 0 and Max_Data = 64KB */ 137 138 uint8_t *xfer_buff; /*!< Pointer to transfer buffer */ 139 140 uint32_t xfer_len; /*!< Current transfer length */ 141 142 uint32_t xfer_count; /*!< Partial transfer length in case of multi packet transfer */ 143 144 } PCD_EPTypeDef; 145 146 147 /** 148 * @brief PCD Handle Structure definition 149 */ 150 typedef struct 151 { 152 PCD_TypeDef *Instance; /*!< Register base address */ 153 PCD_InitTypeDef Init; /*!< PCD required parameters */ 154 __IO uint8_t USB_Address; /*!< USB Address */ 155 PCD_EPTypeDef IN_ep[8]; /*!< IN endpoint parameters */ 156 PCD_EPTypeDef OUT_ep[8]; /*!< OUT endpoint parameters */ 157 HAL_LockTypeDef Lock; /*!< PCD peripheral status */ 158 __IO PCD_StateTypeDef State; /*!< PCD communication state */ 159 uint32_t Setup[12]; /*!< Setup packet buffer */ 160 void *pData; /*!< Pointer to upper stack Handler */ 161 PCD_EP0_StateTypeDef ep0_state; /*!< EP0 state */ 162 PCD_PhyStateTypeDef phy_state; /*!< PHY state */ 163 uint16_t ackpend; /*!< Pending ACK, EP0 only */ 164 } PCD_HandleTypeDef; 165 166 /** 167 * @} 168 */ 169 170 171 172 /* Exported constants --------------------------------------------------------*/ 173 /** @defgroup PCD_Exported_Constants PCD Exported Constants 174 * @{ 175 */ 176 177 /** @defgroup PCD_Core_Speed PCD Core Speed 178 * @{ 179 */ 180 #define PCD_SPEED_HIGH 0 /* Not Supported */ 181 #define PCD_SPEED_FULL 2 182 /** 183 * @} 184 */ 185 186 /** @defgroup PCD_EP0_MPS PCD EP0 Max Packet Size 187 * @{ 188 */ 189 #define PCD_EP0_MPSIZE 16 /* Not Supported */ 190 /** 191 * @} 192 */ 193 194 /** @defgroup PCD_Core_PHY PCD Core PHY 195 * @{ 196 */ 197 #define PCD_PHY_EMBEDDED 2 198 /** 199 * @} 200 */ 201 202 #define USB_USBCFG_AVALID (1<<3) 203 #define USB_USBCFG_AVALID_DR (1<<2) 204 205 // TODO: Check when have analog PHY 206 #define USB_ENABLE_PHY(hpcd) hpcd->Instance->usbcfg|=(USB_USBCFG_AVALID|USB_USBCFG_AVALID_DR) 207 208 // Each endpoint is single buffer (Not PINGPONG) 209 #define USB_DISABLE_DOUBLE_BUFFER(hpcd) { \ 210 hpcd->Instance->dpbrxdisl=0xFE; \ 211 hpcd->Instance->dpbtxdisl=0xFE; \ 212 } 213 214 215 216 /** 217 * @} 218 */ 219 220 /* Exported functions --------------------------------------------------------*/ 221 /** @addtogroup PCD_Exported_Functions PCD Exported Functions 222 * @{ 223 */ 224 225 /* Initialization/de-initialization functions ********************************/ 226 /** @addtogroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions 227 * @{ 228 */ 229 230 /** 231 * @brief Initializes the PCD according to the specified 232 * parameters in the PCD_InitTypeDef and create the associated handle. 233 * @param hpcd PCD handle 234 * @retval HAL status 235 */ 236 HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd); 237 238 /** 239 * @brief DeInitializes the PCD peripheral 240 * @param hpcd PCD handle 241 * @retval HAL status 242 */ 243 HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd); 244 245 /** 246 * @brief Initializes the PCD MSP. 247 * @param hpcd PCD handle 248 * @retval None 249 */ 250 void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd); 251 252 /** 253 * @brief DeInitializes PCD MSP. 254 * @param hpcd PCD handle 255 * @retval None 256 */ 257 void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd); 258 259 /** 260 * @brief Enable/disable PCD. 261 * @param enable 1: enable, 0: disable 262 * @retval None 263 */ 264 void HAL_PCD_Enable(uint8_t enable); 265 266 /** 267 * @} 268 */ 269 270 /* I/O operation functions ***************************************************/ 271 /* Non-Blocking mode: Interrupt */ 272 /** @addtogroup PCD_Exported_Functions_Group2 IO operation functions 273 * @{ 274 */ 275 276 /** 277 * @brief Start the USB device. 278 * @param hpcd PCD handle 279 * @retval HAL status 280 */ 281 HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd); 282 283 /** 284 * @brief Stop the USB device. 285 * @param hpcd PCD handle 286 * @retval HAL status 287 */ 288 HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd); 289 290 /** 291 * @brief This function handles PCD interrupt request. 292 * @param hpcd PCD handle 293 * @retval HAL status 294 */ 295 void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd); 296 297 /** 298 * @brief Data out stage callbacks 299 * @param hpcd PCD handle 300 * @param epnum endpoint number 301 * @retval None 302 */ 303 void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum); 304 305 /** 306 * @brief Data IN stage callbacks 307 * @param hpcd PCD handle 308 * @param epnum endpoint number 309 * @retval None 310 */ 311 void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum); 312 313 /** 314 * @brief Setup stage callback 315 * @param hpcd PCD handle 316 * @retval None 317 */ 318 void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd); 319 320 /** 321 * @brief USB Start Of Frame callbacks 322 * @param hpcd PCD handle 323 * @retval None 324 */ 325 void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd); 326 327 /** 328 * @brief USB Reset callbacks 329 * @param hpcd PCD handle 330 * @retval None 331 */ 332 void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd); 333 334 /** 335 * @brief Suspend event callbacks 336 * @param hpcd PCD handle 337 * @retval None 338 */ 339 void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd); 340 341 /** 342 * @brief Resume event callbacks 343 * @param hpcd PCD handle 344 * @retval None 345 */ 346 void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd); 347 348 /** 349 * @brief Incomplete ISO OUT callbacks 350 * @param hpcd PCD handle 351 * @param epnum endpoint number 352 * @retval None 353 */ 354 void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum); 355 356 /** 357 * @brief Incomplete ISO IN callbacks 358 * @param hpcd PCD handle 359 * @param epnum endpoint number 360 * @retval None 361 */ 362 void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum); 363 364 /** 365 * @brief Connection event callbacks 366 * @param hpcd PCD handle 367 * @retval None 368 */ 369 void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd); 370 371 /** 372 * @brief Disconnection event callbacks 373 * @param hpcd PCD handle 374 * @retval None 375 */ 376 void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd); 377 /** 378 * @} 379 */ 380 381 /* Peripheral Control functions **********************************************/ 382 /** @addtogroup PCD_Exported_Functions_Group3 Peripheral Control functions 383 * @{ 384 */ 385 /** 386 * @brief Set the USB Device address 387 * @param hpcd PCD handle 388 * @param address new device address 389 * @retval HAL status 390 */ 391 HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address); 392 393 /** 394 * @brief Open and configure an endpoint 395 * @param hpcd PCD handle 396 * @param ep_addr endpoint address 397 * @param ep_mps endpoint max packert size 398 * @param ep_type endpoint type 399 * @retval HAL status 400 */ 401 HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type); 402 403 /** 404 * @brief Deactivate an endpoint 405 * @param hpcd PCD handle 406 * @param ep_addr endpoint address 407 * @retval HAL status 408 */ 409 HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr); 410 411 412 /** 413 * @brief Prepare to receive an amount of data 414 * @param hpcd PCD handle 415 * @param ep_addr endpoint address 416 * @param pBuf pointer to the reception buffer 417 * @param len amount of data to be received 418 * @retval HAL status 419 */ 420 HAL_StatusTypeDef HAL_PCD_EP_Prepare_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len); 421 422 423 /** 424 * @brief Receive an amount of data 425 * @param hpcd PCD handle 426 * @param ep_addr endpoint address 427 * @param pBuf pointer to the reception buffer 428 * @retval received packet size. 429 */ 430 uint32_t HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf); 431 432 433 /** 434 * @brief Send an amount of data 435 * @param hpcd PCD handle 436 * @param ep_addr endpoint address 437 * @param pBuf pointer to the transmission buffer 438 * @param len amount of data to be sent 439 * @retval HAL status 440 */ 441 HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len); 442 443 /** 444 * @brief Get Received Data Size 445 * @param hpcd PCD handle 446 * @param ep_addr endpoint address 447 * @retval Data Size 448 */ 449 uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr); 450 451 /** 452 * @brief Set a STALL condition over an endpoint 453 * @param hpcd PCD handle 454 * @param ep_addr endpoint address 455 * @retval HAL status 456 */ 457 HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr); 458 459 460 /** 461 * @brief Clear a STALL condition over in an endpoint 462 * @param hpcd PCD handle 463 * @param ep_addr endpoint address 464 * @retval HAL status 465 */ 466 HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr); 467 468 /** 469 * @brief Flush an endpoint 470 * @param hpcd PCD handle 471 * @param ep_addr endpoint address 472 * @retval HAL status 473 */ 474 HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr); 475 /** 476 * @} 477 */ 478 479 /* Peripheral State functions ************************************************/ 480 /** @addtogroup PCD_Exported_Functions_Group4 Peripheral State functions 481 * @{ 482 */ 483 484 /** 485 * @brief Return the PCD state 486 * @param hpcd PCD handle 487 * @retval HAL state 488 */ 489 PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd); 490 491 /** @addtogroup PCDEx_Exported_Functions_Group1 Peripheral Control functions 492 * @{ 493 */ 494 HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, 495 uint16_t ep_addr, 496 void *dma_config); 497 498 499 /** 500 * @brief Set test mode 501 * @param hpcd PCD handle 502 * @param tm test mode see USB 2.0 spec, section 9.4.9 wIndex 503 * @param data used in TEST_PACKET 504 * @param len length of data 505 * @retval HAL status 506 */ 507 HAL_StatusTypeDef HAL_PCD_TestMode(PCD_HandleTypeDef *hpcd, uint16_t tm, uint8_t *data, uint8_t len); 508 509 void HAL_PCD_Set_RxbuffControl(uint8_t ep_num, uint8_t flag); 510 void HAL_PCD_Set_RxscrACK(uint8_t epnum); 511 512 /** 513 * @} 514 */ 515 516 /** 517 * @} 518 */ 519 520 /* Private constants ---------------------------------------------------------*/ 521 /** @defgroup PCD_Private_Constants PCD Private Constants 522 * @{ 523 */ 524 525 /** @defgroup PCD_EP_Type PCD EP Type 526 * @{ 527 */ 528 #define PCD_EP_TYPE_CTRL 0 529 #define PCD_EP_TYPE_ISOC 1 530 #define PCD_EP_TYPE_BULK 2 531 #define PCD_EP_TYPE_INTR 3 532 /** 533 * @} 534 */ 535 536 /** @defgroup PCD_ENDP PCD ENDP 537 * @{ 538 */ 539 #define PCD_ENDP0 ((uint8_t)0U) 540 #define PCD_ENDP1 ((uint8_t)1U) 541 #define PCD_ENDP2 ((uint8_t)2U) 542 #define PCD_ENDP3 ((uint8_t)3U) 543 #define PCD_ENDP4 ((uint8_t)4U) 544 #define PCD_ENDP5 ((uint8_t)5U) 545 #define PCD_ENDP6 ((uint8_t)6U) 546 #define PCD_ENDP7 ((uint8_t)7U) 547 /** 548 * @} 549 */ 550 551 /** 552 * @} 553 */ 554 555 556 557 /** 558 * @} 559 */ 560 561 /** 562 * @} 563 */ 564 565 566 #ifdef __cplusplus 567 } 568 #endif 569 570 571 #endif /* __BF0_HAL_PCD_H */