123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900 |
- /**
- ******************************************************************************
- * @file stm32l4xx_hal_uart_ex.c
- * @author MCD Application Team
- * @brief Extended UART HAL module driver.
- * This file provides firmware functions to manage the following extended
- * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
- * + Initialization and de-initialization functions
- * + Peripheral Control functions
- *
- *
- @verbatim
- ==============================================================================
- ##### UART peripheral extended features #####
- ==============================================================================
- (#) Declare a UART_HandleTypeDef handle structure.
- (#) For the UART RS485 Driver Enable mode, initialize the UART registers
- by calling the HAL_RS485Ex_Init() API.
- (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming.
- -@- When USART operates in FIFO mode, FIFO mode must be enabled prior
- starting RX/TX transfers. Also RX/TX FIFO thresholds must be
- configured prior starting RX/TX transfers.
- (#) Slave mode enabling/disabling and NSS pin configuration.
- -@- When USART operates in Slave mode, Slave mode must be enabled prior
- starting RX/TX transfers.
- @endverbatim
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32l4xx_hal.h"
- /** @addtogroup STM32L4xx_HAL_Driver
- * @{
- */
- /** @defgroup UARTEx UARTEx
- * @brief UART Extended HAL module driver
- * @{
- */
- #ifdef HAL_UART_MODULE_ENABLED
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macros ------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /** @defgroup UARTEx_Private_Functions UARTEx Private Functions
- * @{
- */
- static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
- #if defined(USART_CR1_FIFOEN)
- static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart);
- #endif
- /**
- * @}
- */
- /* Exported functions --------------------------------------------------------*/
- /** @defgroup UARTEx_Exported_Functions UARTEx Exported Functions
- * @{
- */
- /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions
- * @brief Extended Initialization and Configuration Functions
- *
- @verbatim
- ===============================================================================
- ##### Initialization and Configuration functions #####
- ===============================================================================
- [..]
- This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
- in asynchronous mode.
- (+) For the asynchronous mode the parameters below can be configured:
- (++) Baud Rate
- (++) Word Length
- (++) Stop Bit
- (++) Parity: If the parity is enabled, then the MSB bit of the data written
- in the data register is transmitted but is changed by the parity bit.
- (++) Hardware flow control
- (++) Receiver/transmitter modes
- (++) Over Sampling Method
- (++) One-Bit Sampling Method
- (+) For the asynchronous mode, the following advanced features can be configured as well:
- (++) TX and/or RX pin level inversion
- (++) data logical level inversion
- (++) RX and TX pins swap
- (++) RX overrun detection disabling
- (++) DMA disabling on RX error
- (++) MSB first on communication line
- (++) auto Baud rate detection
- [..]
- The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration
- procedures (details for the procedures are available in reference manual).
- @endverbatim
- Depending on the frame length defined by the M1 and M0 bits (7-bit,
- 8-bit or 9-bit), the possible UART formats are listed in the
- following table.
-
- Table 1. UART frame format.
- +-----------------------------------------------------------------------+
- | M1 bit | M0 bit | PCE bit | UART frame |
- |---------|---------|-----------|---------------------------------------|
- | 0 | 0 | 0 | | SB | 8 bit data | STB | |
- |---------|---------|-----------|---------------------------------------|
- | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
- |---------|---------|-----------|---------------------------------------|
- | 0 | 1 | 0 | | SB | 9 bit data | STB | |
- |---------|---------|-----------|---------------------------------------|
- | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
- |---------|---------|-----------|---------------------------------------|
- | 1 | 0 | 0 | | SB | 7 bit data | STB | |
- |---------|---------|-----------|---------------------------------------|
- | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
- +-----------------------------------------------------------------------+
- * @{
- */
- /**
- * @brief Initialize the RS485 Driver enable feature according to the specified
- * parameters in the UART_InitTypeDef and creates the associated handle.
- * @param huart UART handle.
- * @param Polarity Select the driver enable polarity.
- * This parameter can be one of the following values:
- * @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
- * @arg @ref UART_DE_POLARITY_LOW DE signal is active low
- * @param AssertionTime Driver Enable assertion time:
- * 5-bit value defining the time between the activation of the DE (Driver Enable)
- * signal and the beginning of the start bit. It is expressed in sample time
- * units (1/8 or 1/16 bit time, depending on the oversampling rate)
- * @param DeassertionTime Driver Enable deassertion time:
- * 5-bit value defining the time between the end of the last stop bit, in a
- * transmitted message, and the de-activation of the DE (Driver Enable) signal.
- * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
- * oversampling rate).
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
- {
- uint32_t temp = 0x0;
-
- /* Check the UART handle allocation */
- if(huart == NULL)
- {
- return HAL_ERROR;
- }
- /* Check the Driver Enable UART instance */
- assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
-
- /* Check the Driver Enable polarity */
- assert_param(IS_UART_DE_POLARITY(Polarity));
-
- /* Check the Driver Enable assertion time */
- assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
-
- /* Check the Driver Enable deassertion time */
- assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
-
- if(huart->gState == HAL_UART_STATE_RESET)
- {
- /* Allocate lock resource and initialize it */
- huart->Lock = HAL_UNLOCKED;
-
- /* Init the low level hardware : GPIO, CLOCK, CORTEX */
- HAL_UART_MspInit(huart);
- }
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Disable the Peripheral */
- __HAL_UART_DISABLE(huart);
-
- /* Set the UART Communication parameters */
- if (UART_SetConfig(huart) == HAL_ERROR)
- {
- return HAL_ERROR;
- }
-
- if(huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
- {
- UART_AdvFeatureConfig(huart);
- }
-
- /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
- SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
-
- /* Set the Driver Enable polarity */
- MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
-
- /* Set the Driver Enable assertion and deassertion times */
- temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
- temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
- MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT|USART_CR1_DEAT), temp);
-
- /* Enable the Peripheral */
- __HAL_UART_ENABLE(huart);
-
- /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
- return (UART_CheckIdleState(huart));
- }
- /**
- * @}
- */
- /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions
- * @brief Extended functions
- *
- @verbatim
- ===============================================================================
- ##### IO operation functions #####
- ===============================================================================
- This subsection provides a set of Wakeup and FIFO mode related callback functions.
- (#) Wakeup from Stop mode Callback:
- (+) HAL_UARTEx_WakeupCallback()
- (#) TX/RX Fifos Callbacks:
- (+) HAL_UARTEx_RxFifoFullCallback()
- (+) HAL_UARTEx_TxFifoEmptyCallback()
- @endverbatim
- * @{
- */
- /**
- * @brief UART wakeup from Stop mode callback.
- * @param huart UART handle.
- * @retval None
- */
- __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(huart);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_UARTEx_WakeupCallback can be implemented in the user file.
- */
- }
- #if defined(USART_CR1_FIFOEN)
- /**
- * @brief UART RX Fifo full callback.
- * @param huart UART handle.
- * @retval None
- */
- __weak void HAL_UARTEx_RxFifoFullCallback (UART_HandleTypeDef *huart)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(huart);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_UARTEx_RxFifoFullCallback can be implemented in the user file.
- */
- }
- /**
- * @brief UART TX Fifo empty callback.
- * @param huart UART handle.
- * @retval None
- */
- __weak void HAL_UARTEx_TxFifoEmptyCallback (UART_HandleTypeDef *huart)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(huart);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_UARTEx_TxFifoEmptyCallback can be implemented in the user file.
- */
- }
- #endif
- /**
- * @}
- */
- /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions
- * @brief Extended Peripheral Control functions
- *
- @verbatim
- ===============================================================================
- ##### Peripheral Control functions #####
- ===============================================================================
- [..] This section provides the following functions:
- (+) HAL_UARTEx_EnableClockStopMode() API enables the UART clock (HSI or LSE only) during stop mode
- (+) HAL_UARTEx_DisableClockStopMode() API disables the above functionality
- (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
- detection length to more than 4 bits for multiprocessor address mark wake up.
- (+) HAL_UARTEx_StopModeWakeUpSourceConfig() API defines the wake-up from stop mode
- trigger: address match, Start Bit detection or RXNE bit status.
- (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode
- (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
- (+) HAL_UARTEx_WakeupCallback() called upon UART wakeup interrupt
- (+) HAL_UARTEx_EnableSPISlaveMode() API enables the SPI slave mode
- (+) HAL_UARTEx_DisableSPISlaveMode() API disables the SPI slave mode
- (+) HAL_UARTEx_ConfigNSS API configures the Slave Select input pin (NSS)
- (+) HAL_UARTEx_EnableFifoMode() API enables the FIFO mode
- (+) HAL_UARTEx_DisableFifoMode() API disables the FIFO mode
- (+) HAL_UARTEx_SetTxFifoThreshold() API sets the TX FIFO threshold
- (+) HAL_UARTEx_SetRxFifoThreshold() API sets the RX FIFO threshold
- @endverbatim
- * @{
- */
- /**
- * @brief By default in multiprocessor mode, when the wake up method is set
- * to address mark, the UART handles only 4-bit long addresses detection;
- * this API allows to enable longer addresses detection (6-, 7- or 8-bit
- * long).
- * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
- * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
- * @param huart UART handle.
- * @param AddressLength This parameter can be one of the following values:
- * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
- * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
- {
- /* Check the UART handle allocation */
- if(huart == NULL)
- {
- return HAL_ERROR;
- }
-
- /* Check the address length parameter */
- assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Disable the Peripheral */
- __HAL_UART_DISABLE(huart);
-
- /* Set the address length */
- MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
-
- /* Enable the Peripheral */
- __HAL_UART_ENABLE(huart);
-
- /* TEACK and/or REACK to check before moving huart->gState to Ready */
- return (UART_CheckIdleState(huart));
- }
- /**
- * @brief Set Wakeup from Stop mode interrupt flag selection.
- * @note It is the application responsibility to enable the interrupt used as
- * usart_wkup interrupt source before entering low-power mode.
- * @param huart UART handle.
- * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status.
- * This parameter can be one of the following values:
- * @arg @ref UART_WAKEUP_ON_ADDRESS
- * @arg @ref UART_WAKEUP_ON_STARTBIT
- * @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
- {
- HAL_StatusTypeDef status = HAL_OK;
- uint32_t tickstart = 0;
-
- /* check the wake-up from stop mode UART instance */
- assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
- /* check the wake-up selection parameter */
- assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
-
- /* Process Locked */
- __HAL_LOCK(huart);
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Disable the Peripheral */
- __HAL_UART_DISABLE(huart);
-
- /* Set the wake-up selection scheme */
- MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
-
- if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
- {
- UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
- }
-
- /* Enable the Peripheral */
- __HAL_UART_ENABLE(huart);
-
- /* Init tickstart for timeout managment*/
- tickstart = HAL_GetTick();
-
- /* Wait until REACK flag is set */
- if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
- {
- status = HAL_TIMEOUT;
- }
- else
- {
- /* Initialize the UART State */
- huart->gState = HAL_UART_STATE_READY;
- }
-
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
-
- return status;
- }
- /**
- * @brief Enable UART Stop Mode.
- * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE.
- * @param huart UART handle.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
- {
- /* Process Locked */
- __HAL_LOCK(huart);
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Set UESM bit */
- SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
-
- huart->gState = HAL_UART_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
-
- return HAL_OK;
- }
- /**
- * @brief Disable UART Stop Mode.
- * @param huart UART handle.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
- {
- /* Process Locked */
- __HAL_LOCK(huart);
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Clear UESM bit */
- CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
-
- huart->gState = HAL_UART_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
-
- return HAL_OK;
- }
- #if defined(USART_CR2_SLVEN)
- /**
- * @brief Enable the SPI slave mode.
- * @note When the UART operates in SPI slave mode, it handles data flow using
- * the serial interface clock derived from the external SCLK signal
- * provided by the external master SPI device.
- * @note In SPI slave mode, the UART must be enabled before starting the master
- * communications (or between frames while the clock is stable). Otherwise,
- * if the UART slave is enabled while the master is in the middle of a
- * frame, it will become desynchronized with the master.
- * @note The data register of the slave needs to be ready before the first edge
- * of the communication clock or before the end of the ongoing communication,
- * otherwise the SPI slave will transmit zeros.
- * @param huart UART handle.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UARTEx_EnableSlaveMode(UART_HandleTypeDef *huart)
- {
- uint32_t tmpcr1 = 0;
- /* Check parameters */
- assert_param(IS_UART_SPI_SLAVE_INSTANCE(huart->Instance));
-
- /* Process Locked */
- __HAL_LOCK(huart);
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Save actual UART configuration */
- tmpcr1 = READ_REG(huart->Instance->CR1);
-
- /* Disable UART */
- __HAL_UART_DISABLE(huart);
-
- /* In SPI slave mode mode, the following bits must be kept cleared:
- - LINEN and CLKEN bit in the USART_CR2 register
- - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/
- CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
- CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
-
- /* Enable SPI slave mode */
- SET_BIT(huart->Instance->CR2, USART_CR2_SLVEN);
-
- /* Restore UART configuration */
- WRITE_REG(huart->Instance->CR1, tmpcr1);
-
- huart->SlaveMode = UART_SLAVEMODE_ENABLE;
-
- huart->gState = HAL_UART_STATE_READY;
-
- /* Enable UART */
- __HAL_UART_ENABLE(huart);
-
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
-
- return HAL_OK;
- }
- /**
- * @brief Disable the SPI slave mode.
- * @param huart UART handle.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UARTEx_DisableSlaveMode(UART_HandleTypeDef *huart)
- {
- uint32_t tmpcr1 = 0;
- /* Check parameters */
- assert_param(IS_UART_SPI_SLAVE_INSTANCE(huart->Instance));
-
- /* Process Locked */
- __HAL_LOCK(huart);
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Save actual UART configuration */
- tmpcr1 = READ_REG(huart->Instance->CR1);
-
- /* Disable UART */
- __HAL_UART_DISABLE(huart);
- /* Disable SPI slave mode */
- CLEAR_BIT(huart->Instance->CR2, USART_CR2_SLVEN);
-
- /* Restore UART configuration */
- WRITE_REG(huart->Instance->CR1, tmpcr1);
- huart->SlaveMode = UART_SLAVEMODE_ENABLE;
-
- huart->gState = HAL_UART_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
-
- return HAL_OK;
- }
- /**
- * @brief Configure the Slave Select input pin (NSS).
- * @note Software NSS management: SPI slave will always be selected and NSS
- * input pin will be ignored.
- * @note Hardware NSS management: the SPI slave selection depends on NSS
- * input pin. The slave is selected when NSS is low and deselected when
- * NSS is high.
- * @param huart UART handle.
- * @param NSSConfig NSS configuration.
- * This parameter can be one of the following values:
- * @arg @ref UART_NSS_HARD
- * @arg @ref UART_NSS_SOFT
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UARTEx_ConfigNSS(UART_HandleTypeDef *huart, uint32_t NSSConfig)
- {
- uint32_t tmpcr1 = 0;
- /* Check parameters */
- assert_param(IS_UART_SPI_SLAVE_INSTANCE(huart->Instance));
- assert_param(IS_UART_NSS(NSSConfig));
-
- /* Process Locked */
- __HAL_LOCK(huart);
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Save actual UART configuration */
- tmpcr1 = READ_REG(huart->Instance->CR1);
-
- /* Disable UART */
- __HAL_UART_DISABLE(huart);
- /* Program DIS_NSS bit in the USART_CR2 register */
- MODIFY_REG(huart->Instance->CR2, USART_CR2_DIS_NSS, NSSConfig);
-
- /* Restore UART configuration */
- WRITE_REG(huart->Instance->CR1, tmpcr1);
-
- huart->gState = HAL_UART_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
- return HAL_OK;
- }
- #endif
- #if defined(USART_CR1_FIFOEN)
- /**
- * @brief Enable the FIFO mode.
- * @param huart UART handle.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef *huart)
- {
- uint32_t tmpcr1 = 0;
-
- /* Check parameters */
- assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
- /* Process Locked */
- __HAL_LOCK(huart);
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Save actual UART configuration */
- tmpcr1 = READ_REG(huart->Instance->CR1);
-
- /* Disable UART */
- __HAL_UART_DISABLE(huart);
-
- /* Enable FIFO mode */
- SET_BIT(tmpcr1, USART_CR1_FIFOEN);
- huart->FifoMode = UART_FIFOMODE_ENABLE;
-
- /* Restore UART configuration */
- WRITE_REG(huart->Instance->CR1, tmpcr1);
-
- /* Determine the number of data to process during RX/TX ISR execution */
- UARTEx_SetNbDataToProcess(huart);
-
- huart->gState = HAL_UART_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
-
- return HAL_OK;
- }
- /**
- * @brief Disable the FIFO mode.
- * @param huart UART handle.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart)
- {
- uint32_t tmpcr1 = 0;
- /* Check parameters */
- assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
- /* Process Locked */
- __HAL_LOCK(huart);
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Save actual UART configuration */
- tmpcr1 = READ_REG(huart->Instance->CR1);
-
- /* Disable UART */
- __HAL_UART_DISABLE(huart);
-
- /* Enable FIFO mode */
- CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
- huart->FifoMode = UART_FIFOMODE_DISABLE;
-
- /* Restore UART configuration */
- WRITE_REG(huart->Instance->CR1, tmpcr1);
-
- huart->gState = HAL_UART_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
-
- return HAL_OK;
- }
- /**
- * @brief Set the TXFIFO threshold.
- * @param huart UART handle.
- * @param Threshold TX FIFO threshold value
- * This parameter can be one of the following values:
- * @arg @ref UART_TXFIFO_THRESHOLD_1_8
- * @arg @ref UART_TXFIFO_THRESHOLD_1_4
- * @arg @ref UART_TXFIFO_THRESHOLD_1_2
- * @arg @ref UART_TXFIFO_THRESHOLD_3_4
- * @arg @ref UART_TXFIFO_THRESHOLD_7_8
- * @arg @ref UART_TXFIFO_THRESHOLD_8_8
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
- {
- uint32_t tmpcr1 = 0;
-
- /* Check parameters */
- assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
- assert_param(IS_UART_TXFIFO_THRESHOLD(Threshold));
- /* Process Locked */
- __HAL_LOCK(huart);
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Save actual UART configuration */
- tmpcr1 = READ_REG(huart->Instance->CR1);
-
- /* Disable UART */
- __HAL_UART_DISABLE(huart);
-
- /* Update TX threshold configuration */
- MODIFY_REG(huart->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
-
- /* Determine the number of data to process during RX/TX ISR execution */
- UARTEx_SetNbDataToProcess(huart);
-
- /* Restore UART configuration */
- WRITE_REG(huart->Instance->CR1, tmpcr1);
-
- huart->gState = HAL_UART_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
-
- return HAL_OK;
- }
- /**
- * @brief Set the RXFIFO threshold.
- * @param huart UART handle.
- * @param Threshold RX FIFO threshold value
- * This parameter can be one of the following values:
- * @arg @ref UART_RXFIFO_THRESHOLD_1_8
- * @arg @ref UART_RXFIFO_THRESHOLD_1_4
- * @arg @ref UART_RXFIFO_THRESHOLD_1_2
- * @arg @ref UART_RXFIFO_THRESHOLD_3_4
- * @arg @ref UART_RXFIFO_THRESHOLD_7_8
- * @arg @ref UART_RXFIFO_THRESHOLD_8_8
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
- {
- uint32_t tmpcr1 = 0;
-
- /* Check the parameters */
- assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
- assert_param(IS_UART_RXFIFO_THRESHOLD(Threshold));
- /* Process Locked */
- __HAL_LOCK(huart);
-
- huart->gState = HAL_UART_STATE_BUSY;
-
- /* Save actual UART configuration */
- tmpcr1 = READ_REG(huart->Instance->CR1);
-
- /* Disable UART */
- __HAL_UART_DISABLE(huart);
-
- /* Update RX threshold configuration */
- MODIFY_REG(huart->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
-
- /* Determine the number of data to process during RX/TX ISR execution */
- UARTEx_SetNbDataToProcess(huart);
-
- /* Restore UART configuration */
- WRITE_REG(huart->Instance->CR1, tmpcr1);
-
- huart->gState = HAL_UART_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
-
- return HAL_OK;
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /** @addtogroup UARTEx_Private_Functions
- * @{
- */
- /**
- * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
- * @param huart UART handle.
- * @param WakeUpSelection UART wake up from stop mode parameters.
- * @retval None
- */
- static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
- {
- assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
-
- /* Set the USART address length */
- MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
-
- /* Set the USART address node */
- MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
- }
- #if defined(USART_CR1_FIFOEN)
- /**
- * @brief Calculate the number of data to process in RX/TX ISR.
- * @note The RX FIFO depth and the TX FIFO depth is extracted from
- * the UART configuration registers.
- * @param huart UART handle.
- * @retval None
- */
- void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart)
- {
- uint8_t rx_fifo_depth;
- uint8_t tx_fifo_depth;
- uint8_t rx_fifo_threshold;
- uint8_t tx_fifo_threshold;
- uint8_t numerator[] = {1, 1, 1, 3, 7, 1};
- uint8_t denominator[] = {8, 4, 2, 4, 8, 1};
-
- if (huart->FifoMode == UART_FIFOMODE_DISABLE)
- {
- huart->NbTxDataToProcess = 1;
- huart->NbRxDataToProcess = 1;
- }
- else
- {
- rx_fifo_depth = 8; /* RX Fifo size */
- tx_fifo_depth = 8; /* TX Fifo size */
- rx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
- tx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
- huart->NbTxDataToProcess = (uint8_t)(tx_fifo_depth * numerator[tx_fifo_threshold])/denominator[tx_fifo_threshold];
- huart->NbRxDataToProcess = (uint8_t)(rx_fifo_depth * numerator[rx_fifo_threshold])/denominator[rx_fifo_threshold];
- }
- }
- #endif
- /**
- * @}
- */
- #endif /* HAL_UART_MODULE_ENABLED */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|