|
STM32G474xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32g4xx_hal_usart_ex.c 00004 * @author MCD Application Team 00005 * @brief Extended USART HAL module driver. 00006 * This file provides firmware functions to manage the following extended 00007 * functionalities of the Universal Synchronous Receiver Transmitter Peripheral (USART). 00008 * + Peripheral Control functions 00009 * 00010 * 00011 ****************************************************************************** 00012 * @attention 00013 * 00014 * Copyright (c) 2019 STMicroelectronics. 00015 * All rights reserved. 00016 * 00017 * This software is licensed under terms that can be found in the LICENSE file 00018 * in the root directory of this software component. 00019 * If no LICENSE file comes with this software, it is provided AS-IS. 00020 * 00021 ****************************************************************************** 00022 @verbatim 00023 ============================================================================== 00024 ##### USART peripheral extended features ##### 00025 ============================================================================== 00026 00027 (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming. 00028 00029 -@- When USART operates in FIFO mode, FIFO mode must be enabled prior 00030 starting RX/TX transfers. Also RX/TX FIFO thresholds must be 00031 configured prior starting RX/TX transfers. 00032 00033 (#) Slave mode enabling/disabling and NSS pin configuration. 00034 00035 -@- When USART operates in Slave mode, Slave mode must be enabled prior 00036 starting RX/TX transfers. 00037 00038 @endverbatim 00039 ****************************************************************************** 00040 */ 00041 00042 /* Includes ------------------------------------------------------------------*/ 00043 #include "stm32g4xx_hal.h" 00044 00045 /** @addtogroup STM32G4xx_HAL_Driver 00046 * @{ 00047 */ 00048 00049 /** @defgroup USARTEx USARTEx 00050 * @brief USART Extended HAL module driver 00051 * @{ 00052 */ 00053 00054 #ifdef HAL_USART_MODULE_ENABLED 00055 00056 /* Private typedef -----------------------------------------------------------*/ 00057 /** @defgroup USARTEx_Private_Constants USARTEx Private Constants 00058 * @{ 00059 */ 00060 /* USART RX FIFO depth */ 00061 #define RX_FIFO_DEPTH 8U 00062 00063 /* USART TX FIFO depth */ 00064 #define TX_FIFO_DEPTH 8U 00065 /** 00066 * @} 00067 */ 00068 00069 /* Private define ------------------------------------------------------------*/ 00070 /* Private macros ------------------------------------------------------------*/ 00071 /* Private variables ---------------------------------------------------------*/ 00072 /* Private function prototypes -----------------------------------------------*/ 00073 /** @defgroup USARTEx_Private_Functions USARTEx Private Functions 00074 * @{ 00075 */ 00076 static void USARTEx_SetNbDataToProcess(USART_HandleTypeDef *husart); 00077 /** 00078 * @} 00079 */ 00080 00081 /* Exported functions --------------------------------------------------------*/ 00082 00083 /** @defgroup USARTEx_Exported_Functions USARTEx Exported Functions 00084 * @{ 00085 */ 00086 00087 /** @defgroup USARTEx_Exported_Functions_Group1 IO operation functions 00088 * @brief Extended USART Transmit/Receive functions 00089 * 00090 @verbatim 00091 =============================================================================== 00092 ##### IO operation functions ##### 00093 =============================================================================== 00094 This subsection provides a set of FIFO mode related callback functions. 00095 00096 (#) TX/RX Fifos Callbacks: 00097 (+) HAL_USARTEx_RxFifoFullCallback() 00098 (+) HAL_USARTEx_TxFifoEmptyCallback() 00099 00100 @endverbatim 00101 * @{ 00102 */ 00103 00104 /** 00105 * @brief USART RX Fifo full callback. 00106 * @param husart USART handle. 00107 * @retval None 00108 */ 00109 __weak void HAL_USARTEx_RxFifoFullCallback(USART_HandleTypeDef *husart) 00110 { 00111 /* Prevent unused argument(s) compilation warning */ 00112 UNUSED(husart); 00113 00114 /* NOTE : This function should not be modified, when the callback is needed, 00115 the HAL_USARTEx_RxFifoFullCallback can be implemented in the user file. 00116 */ 00117 } 00118 00119 /** 00120 * @brief USART TX Fifo empty callback. 00121 * @param husart USART handle. 00122 * @retval None 00123 */ 00124 __weak void HAL_USARTEx_TxFifoEmptyCallback(USART_HandleTypeDef *husart) 00125 { 00126 /* Prevent unused argument(s) compilation warning */ 00127 UNUSED(husart); 00128 00129 /* NOTE : This function should not be modified, when the callback is needed, 00130 the HAL_USARTEx_TxFifoEmptyCallback can be implemented in the user file. 00131 */ 00132 } 00133 00134 /** 00135 * @} 00136 */ 00137 00138 /** @defgroup USARTEx_Exported_Functions_Group2 Peripheral Control functions 00139 * @brief Extended Peripheral Control functions 00140 * 00141 @verbatim 00142 =============================================================================== 00143 ##### Peripheral Control functions ##### 00144 =============================================================================== 00145 [..] This section provides the following functions: 00146 (+) HAL_USARTEx_EnableSPISlaveMode() API enables the SPI slave mode 00147 (+) HAL_USARTEx_DisableSPISlaveMode() API disables the SPI slave mode 00148 (+) HAL_USARTEx_ConfigNSS API configures the Slave Select input pin (NSS) 00149 (+) HAL_USARTEx_EnableFifoMode() API enables the FIFO mode 00150 (+) HAL_USARTEx_DisableFifoMode() API disables the FIFO mode 00151 (+) HAL_USARTEx_SetTxFifoThreshold() API sets the TX FIFO threshold 00152 (+) HAL_USARTEx_SetRxFifoThreshold() API sets the RX FIFO threshold 00153 00154 00155 @endverbatim 00156 * @{ 00157 */ 00158 00159 /** 00160 * @brief Enable the SPI slave mode. 00161 * @note When the USART operates in SPI slave mode, it handles data flow using 00162 * the serial interface clock derived from the external SCLK signal 00163 * provided by the external master SPI device. 00164 * @note In SPI slave mode, the USART must be enabled before starting the master 00165 * communications (or between frames while the clock is stable). Otherwise, 00166 * if the USART slave is enabled while the master is in the middle of a 00167 * frame, it will become desynchronized with the master. 00168 * @note The data register of the slave needs to be ready before the first edge 00169 * of the communication clock or before the end of the ongoing communication, 00170 * otherwise the SPI slave will transmit zeros. 00171 * @param husart USART handle. 00172 * @retval HAL status 00173 */ 00174 HAL_StatusTypeDef HAL_USARTEx_EnableSlaveMode(USART_HandleTypeDef *husart) 00175 { 00176 uint32_t tmpcr1; 00177 00178 /* Check parameters */ 00179 assert_param(IS_UART_SPI_SLAVE_INSTANCE(husart->Instance)); 00180 00181 /* Process Locked */ 00182 __HAL_LOCK(husart); 00183 00184 husart->State = HAL_USART_STATE_BUSY; 00185 00186 /* Save actual USART configuration */ 00187 tmpcr1 = READ_REG(husart->Instance->CR1); 00188 00189 /* Disable USART */ 00190 __HAL_USART_DISABLE(husart); 00191 00192 /* In SPI slave mode mode, the following bits must be kept cleared: 00193 - LINEN and CLKEN bit in the USART_CR2 register 00194 - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/ 00195 CLEAR_BIT(husart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00196 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); 00197 00198 /* Enable SPI slave mode */ 00199 SET_BIT(husart->Instance->CR2, USART_CR2_SLVEN); 00200 00201 /* Restore USART configuration */ 00202 WRITE_REG(husart->Instance->CR1, tmpcr1); 00203 00204 husart->SlaveMode = USART_SLAVEMODE_ENABLE; 00205 00206 husart->State = HAL_USART_STATE_READY; 00207 00208 /* Enable USART */ 00209 __HAL_USART_ENABLE(husart); 00210 00211 /* Process Unlocked */ 00212 __HAL_UNLOCK(husart); 00213 00214 return HAL_OK; 00215 } 00216 00217 /** 00218 * @brief Disable the SPI slave mode. 00219 * @param husart USART handle.