HAL UCPD How to Use ¶
- group UCPD_How_To_Use
-
How to use the UCPD HAL module driver ¶
Usage ¶
-
Declare a hal_ucpd_handle_t handle structure. and initialize the UCPDx driver with an UCPD hardware instance by calling the HAL_UCPD_Init() . The UCPDx clock is enabled inside the HAL_UCPD_Init() if USE_HAL_UCPD_CLK_ENABLE_MODEL > HAL_CLK_ENABLE_NO.
-
Initialize the UCPD low level resources by implementing the UCPD_MspInit API:
-
Enable the UCPDx interface clock (if not enabled in HAL_UCPD_Init() when UCPD_CLK_ENABLE_MODEL is set to CLK_ENABLE_NO)
-
UCPD pins configuration:
-
Enable the clock for the UCPD GPIOs
-
Configure UCPD pins as alternate function open-drain
-
-
NVIC configuration if you need to use interrupt process
-
Configure the UCPDx interrupt priority
-
Enable the NVIC UCPD IRQ Channel
-
-
DMA Configuration
-
Declare a hal_dma_handle_t handle structure for the transmit or receive channel
-
Enable the DMAx interface clock
-
Configure the DMA handle parameters
-
Configure the DMA Tx or Rx channel
-
Associate the initialized DMA handle to the hucpd DMA Tx or Rx handle
-
Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx channel
-
-
-
-
Configure the ucpd_clk prescaler, half-bit clock, transition window clock, and inter-frame gap timer division ratios, as well as accepted ordered set types and CC line enabling by calling HAL_UCPD_SetConfig() .
-
Configure and/or enable advanced static configuration features. For instance, HAL_UCPD_SetRxPreFilterSampling() , HAL_UCPD_EnableRxPreFilter() , HAL_UCPD_EnableWakeupMode() , etc. All these advanced configurations are optional (not mandatory), and can only be set before starting the driver with HAL_UCPD_Start() .
-
End the static configuration and start the driver with HAL_UCPD_Start() .
-
Apply any dynamic control and configuration function. For instance, HAL_UCPD_SetRole() , HAL_UCPD_SetRxMode() , HAL_UCPD_SetTxOrderedSet() , HAL_UCPD_ApplyTrimmingRp() , HAL_UCPD_ApplyTrimmingRd() , etc.
-
For UCPD IO operations, one operation mode is available within this driver:
-
DMA mode IO operation
-
Transmit an amount of data in non-blocking mode (DMA) using HAL_UCPD_Transmit_DMA() .
-
At transmission end of transfer, HAL_UCPD_TxCpltCallback() is executed and users can add their own code by customization of function pointer HAL_UCPD_TxCpltCallback() .
-
-
Receive an amount of data in non-blocking mode (DMA) using HAL_UCPD_Receive_DMA()
-
HAL_UCPD_Receive_DMA() will also prepare the Tx DMA to quickly allowing transmission of a GoodCRC message.
-
At reception completion, HAL_UCPD_RxCpltCallback() is executed and users can add their own code by customization of function pointer HAL_UCPD_RxCpltCallback() . At this point user must prepare the GoodCRC data to send by calling HAL_UCPD_SetGoodCRCData() .
-
After returning from HAL_UCPD_RxCpltCallback() , quick transmission of GoodCRC message will occur in the interrupt context.
-
At GoodCRC transmission end of transfer, HAL_UCPD_TxGoodCRCCpltCallback() is executed and users can add their own code by customization of function pointer HAL_UCPD_TxGoodCRCCpltCallback() .
-
-
In case of transfer Error, HAL_UCPD_ErrorCallback() function is executed and users can add their own code by customization of function pointer HAL_UCPD_ErrorCallback() .
-
Abort an UCPD process communication in Interrupt mode using HAL_UCPD_Abort_IT()
-
At end of abort process, HAL_UCPD_AbortCpltCallback() is executed and users can add their own code by customization of function pointer HAL_UCPD_AbortCpltCallback() .
-
In case of Tx abort, the UCPD peripheral continues to send the remaining data but will ensure the CRC is incorrect, so that the receiver can end the reception and discard the message.
-
-
-
Callback registration ¶
-
When the compilation flag USE_HAL_UCPD_REGISTER_CALLBACKS is set to 1, it allows the user to configure dynamically the driver callbacks instead of weak functions. Functions allows to register callbacks:
-
HAL_UCPD_RegisterTxCpltCallback() for transmission end of transfer callback registration.
-
HAL_UCPD_RegisterRxCpltCallback() for reception end of transfer callback registration.
-
HAL_UCPD_RegisterErrorCallback() for error callback registration.
-
etc
-
UCPD bus Acquire/Release ¶
-
When the compilation flag USE_HAL_MUTEX is set to 1, it allows the user to acquire/reserve the whole UCPD bus for executing process . The HAL Acquire/Release are based on the HAL OS abstraction layer:
-
HAL_UCPD_AcquireBus() for acquiring the bus or wait for it.
-
HAL_UCPD_ReleaseBus() for releasing the bus.
-
-
When the compilation flag USE_HAL_MUTEX is set to 0 or not defined, HAL_UCPD_AcquireBus() / HAL_UCPD_ReleaseBus() are not available.
-