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 a UCPD hardware instance by calling 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 processing
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(), 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() also prepares Tx DMA to enable quick GoodCRC message transmission.
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 HAL_UCPD_RxCpltCallback() returns, the ISR transmits the GoodCRC message 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().
During a Tx abort, the peripheral continues sending remaining bytes but forces an incorrect CRC so the receiver discards 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 allow registering 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.
See the complete list in the callback registration function group.
UCPD bus Acquire/Release ¶
When the compilation flag USE_HAL_MUTEX is set to 1, it allows the user to acquire/reserve the UCPD bus for a transaction. The HAL Acquire/Release are based on the HAL OS abstraction layer:
HAL_UCPD_AcquireBus() acquires the bus or waits until it is available.
HAL_UCPD_ReleaseBus() releases 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.