STM32H735xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32h7xx_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) 2017 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 "stm32h7xx_hal.h" 00044 00045 /** @addtogroup STM32H7xx_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 16U 00062 00063 /* USART TX FIFO depth */ 00064 #define TX_FIFO_DEPTH 16U 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. 00220 * @retval HAL status 00221 */ 00222 HAL_StatusTypeDef HAL_USARTEx_DisableSlaveMode(USART_HandleTypeDef *husart) 00223 { 00224 uint32_t tmpcr1; 00225 00226 /* Check parameters */ 00227 assert_param(IS_UART_SPI_SLAVE_INSTANCE(husart->Instance)); 00228 00229 /* Process Locked */ 00230 __HAL_LOCK(husart); 00231 00232 husart->State = HAL_USART_STATE_BUSY; 00233 00234 /* Save actual USART configuration */ 00235 tmpcr1 = READ_REG(husart->Instance->CR1); 00236 00237 /* Disable USART */ 00238 __HAL_USART_DISABLE(husart); 00239 00240 /* Disable SPI slave mode */ 00241 CLEAR_BIT(husart->Instance->CR2, USART_CR2_SLVEN); 00242 00243 /* Restore USART configuration */ 00244 WRITE_REG(husart->Instance->CR1, tmpcr1); 00245 00246 husart->SlaveMode = USART_SLAVEMODE_DISABLE; 00247 00248 husart->State = HAL_USART_STATE_READY; 00249 00250 /* Process Unlocked */ 00251 __HAL_UNLOCK(husart); 00252 00253 return HAL_OK; 00254 } 00255 00256 /** 00257 * @brief Configure the Slave Select input pin (NSS). 00258 * @note Software NSS management: SPI slave will always be selected and NSS 00259 * input pin will be ignored. 00260 * @note Hardware NSS management: the SPI slave selection depends on NSS 00261 * input pin. The slave is selected when NSS is low and deselected when 00262 * NSS is high. 00263 * @param husart USART handle. 00264 * @param NSSConfig NSS configuration. 00265 * This parameter can be one of the following values: 00266 * @arg @ref USART_NSS_HARD 00267 * @arg @ref USART_NSS_SOFT 00268 * @retval HAL status 00269 */ 00270 HAL_StatusTypeDef HAL_USARTEx_ConfigNSS(USART_HandleTypeDef *husart, uint32_t NSSConfig) 00271 { 00272 uint32_t tmpcr1; 00273 00274 /* Check parameters */ 00275 assert_param(IS_UART_SPI_SLAVE_INSTANCE(husart->Instance)); 00276 assert_param(IS_USART_NSS(NSSConfig)); 00277 00278 /* Process Locked */ 00279 __HAL_LOCK(husart); 00280 00281 husart->State = HAL_USART_STATE_BUSY; 00282 00283 /* Save actual USART configuration */ 00284 tmpcr1 = READ_REG(husart->Instance->CR1); 00285 00286 /* Disable USART */ 00287 __HAL_USART_DISABLE(husart); 00288 00289 /* Program DIS_NSS bit in the USART_CR2 register */ 00290 MODIFY_REG(husart->Instance->CR2, USART_CR2_DIS_NSS, NSSConfig); 00291 00292 /* Restore USART configuration */ 00293 WRITE_REG(husart->Instance->CR1, tmpcr1); 00294 00295 husart->State = HAL_USART_STATE_READY; 00296 00297 /* Process Unlocked */ 00298 __HAL_UNLOCK(husart); 00299 00300 return HAL_OK; 00301 } 00302 00303 /** 00304 * @brief Enable the FIFO mode. 00305 * @param husart USART handle. 00306 * @retval HAL status 00307 */ 00308 HAL_StatusTypeDef HAL_USARTEx_EnableFifoMode(USART_HandleTypeDef *husart) 00309 { 00310 uint32_t tmpcr1; 00311 00312 /* Check parameters */ 00313 assert_param(IS_UART_FIFO_INSTANCE(husart->Instance)); 00314 00315 /* Process Locked */ 00316 __HAL_LOCK(husart); 00317 00318 husart->State = HAL_USART_STATE_BUSY; 00319 00320 /* Save actual USART configuration */ 00321 tmpcr1 = READ_REG(husart->Instance->CR1); 00322 00323 /* Disable USART */ 00324 __HAL_USART_DISABLE(husart); 00325 00326 /* Enable FIFO mode */ 00327 SET_BIT(tmpcr1, USART_CR1_FIFOEN); 00328 husart->FifoMode = USART_FIFOMODE_ENABLE; 00329 00330 /* Restore USART configuration */ 00331 WRITE_REG(husart->Instance->CR1, tmpcr1); 00332 00333 /* Determine the number of data to process during RX/TX ISR execution */ 00334 USARTEx_SetNbDataToProcess(husart); 00335 00336 husart->State = HAL_USART_STATE_READY; 00337 00338 /* Process Unlocked */ 00339 __HAL_UNLOCK(husart); 00340 00341 return HAL_OK; 00342 } 00343 00344 /** 00345 * @brief Disable the FIFO mode. 00346 * @param husart USART handle. 00347 * @retval HAL status 00348 */ 00349 HAL_StatusTypeDef HAL_USARTEx_DisableFifoMode(USART_HandleTypeDef *husart) 00350 { 00351 uint32_t tmpcr1; 00352 00353 /* Check parameters */ 00354 assert_param(IS_UART_FIFO_INSTANCE(husart->Instance)); 00355 00356 /* Process Locked */ 00357 __HAL_LOCK(husart); 00358 00359 husart->State = HAL_USART_STATE_BUSY; 00360 00361 /* Save actual USART configuration */ 00362 tmpcr1 = READ_REG(husart->Instance->CR1); 00363 00364 /* Disable USART */ 00365 __HAL_USART_DISABLE(husart); 00366 00367 /* Enable FIFO mode */ 00368 CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN); 00369 husart->FifoMode = USART_FIFOMODE_DISABLE; 00370 00371 /* Restore USART configuration */ 00372 WRITE_REG(husart->Instance->CR1, tmpcr1); 00373 00374 husart->State = HAL_USART_STATE_READY; 00375 00376 /* Process Unlocked */ 00377 __HAL_UNLOCK(husart); 00378 00379 return HAL_OK; 00380 } 00381 00382 /** 00383 * @brief Set the TXFIFO threshold. 00384 * @param husart USART handle. 00385 * @param Threshold TX FIFO threshold value 00386 * This parameter can be one of the following values: 00387 * @arg @ref USART_TXFIFO_THRESHOLD_1_8 00388 * @arg @ref USART_TXFIFO_THRESHOLD_1_4 00389 * @arg @ref USART_TXFIFO_THRESHOLD_1_2 00390 * @arg @ref USART_TXFIFO_THRESHOLD_3_4 00391 * @arg @ref USART_TXFIFO_THRESHOLD_7_8 00392 * @arg @ref USART_TXFIFO_THRESHOLD_8_8 00393 * @retval HAL status 00394 */ 00395 HAL_StatusTypeDef HAL_USARTEx_SetTxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold) 00396 { 00397 uint32_t tmpcr1; 00398 00399 /* Check parameters */ 00400 assert_param(IS_UART_FIFO_INSTANCE(husart->Instance)); 00401 assert_param(IS_USART_TXFIFO_THRESHOLD(Threshold)); 00402 00403 /* Process Locked */ 00404 __HAL_LOCK(husart); 00405 00406 husart->State = HAL_USART_STATE_BUSY; 00407 00408 /* Save actual USART configuration */ 00409 tmpcr1 = READ_REG(husart->Instance->CR1); 00410 00411 /* Disable USART */ 00412 __HAL_USART_DISABLE(husart); 00413 00414 /* Update TX threshold configuration */ 00415 MODIFY_REG(husart->Instance->CR3, USART_CR3_TXFTCFG, Threshold); 00416 00417 /* Determine the number of data to process during RX/TX ISR execution */ 00418 USARTEx_SetNbDataToProcess(husart); 00419 00420 /* Restore USART configuration */ 00421 WRITE_REG(husart->Instance->CR1, tmpcr1); 00422 00423 husart->State = HAL_USART_STATE_READY; 00424 00425 /* Process Unlocked */ 00426 __HAL_UNLOCK(husart); 00427 00428 return HAL_OK; 00429 } 00430 00431 /** 00432 * @brief Set the RXFIFO threshold. 00433 * @param husart USART handle. 00434 * @param Threshold RX FIFO threshold value 00435 * This parameter can be one of the following values: 00436 * @arg @ref USART_RXFIFO_THRESHOLD_1_8 00437 * @arg @ref USART_RXFIFO_THRESHOLD_1_4 00438 * @arg @ref USART_RXFIFO_THRESHOLD_1_2 00439 * @arg @ref USART_RXFIFO_THRESHOLD_3_4 00440 * @arg @ref USART_RXFIFO_THRESHOLD_7_8 00441 * @arg @ref USART_RXFIFO_THRESHOLD_8_8 00442 * @retval HAL status 00443 */ 00444 HAL_StatusTypeDef HAL_USARTEx_SetRxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold) 00445 { 00446 uint32_t tmpcr1; 00447 00448 /* Check the parameters */ 00449 assert_param(IS_UART_FIFO_INSTANCE(husart->Instance)); 00450 assert_param(IS_USART_RXFIFO_THRESHOLD(Threshold)); 00451 00452 /* Process Locked */ 00453 __HAL_LOCK(husart); 00454 00455 husart->State = HAL_USART_STATE_BUSY; 00456 00457 /* Save actual USART configuration */ 00458 tmpcr1 = READ_REG(husart->Instance->CR1); 00459 00460 /* Disable USART */ 00461 __HAL_USART_DISABLE(husart); 00462 00463 /* Update RX threshold configuration */ 00464 MODIFY_REG(husart->Instance->CR3, USART_CR3_RXFTCFG, Threshold); 00465 00466 /* Determine the number of data to process during RX/TX ISR execution */ 00467 USARTEx_SetNbDataToProcess(husart); 00468 00469 /* Restore USART configuration */ 00470 WRITE_REG(husart->Instance->CR1, tmpcr1); 00471 00472 husart->State = HAL_USART_STATE_READY; 00473 00474 /* Process Unlocked */ 00475 __HAL_UNLOCK(husart); 00476 00477 return HAL_OK; 00478 } 00479 00480 /** 00481 * @} 00482 */ 00483 00484 /** 00485 * @} 00486 */ 00487 00488 /** @addtogroup USARTEx_Private_Functions 00489 * @{ 00490 */ 00491 00492 /** 00493 * @brief Calculate the number of data to process in RX/TX ISR. 00494 * @note The RX FIFO depth and the TX FIFO depth is extracted from 00495 * the USART configuration registers. 00496 * @param husart USART handle. 00497 * @retval None 00498 */ 00499 static void USARTEx_SetNbDataToProcess(USART_HandleTypeDef *husart) 00500 { 00501 uint8_t rx_fifo_depth; 00502 uint8_t tx_fifo_depth; 00503 uint8_t rx_fifo_threshold; 00504 uint8_t tx_fifo_threshold; 00505 /* 2 0U/1U added for MISRAC2012-Rule-18.1_b and MISRAC2012-Rule-18.1_d */ 00506 static const uint8_t numerator[] = {1U, 1U, 1U, 3U, 7U, 1U, 0U, 0U}; 00507 static const uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U, 1U, 1U}; 00508 00509 if (husart->FifoMode == USART_FIFOMODE_DISABLE) 00510 { 00511 husart->NbTxDataToProcess = 1U; 00512 husart->NbRxDataToProcess = 1U; 00513 } 00514 else 00515 { 00516 rx_fifo_depth = RX_FIFO_DEPTH; 00517 tx_fifo_depth = TX_FIFO_DEPTH; 00518 rx_fifo_threshold = (uint8_t)((READ_BIT(husart->Instance->CR3, 00519 USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos) & 0xFFU); 00520 tx_fifo_threshold = (uint8_t)((READ_BIT(husart->Instance->CR3, 00521 USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos) & 0xFFU); 00522 husart->NbTxDataToProcess = ((uint16_t)tx_fifo_depth * numerator[tx_fifo_threshold]) / 00523 (uint16_t)denominator[tx_fifo_threshold]; 00524 husart->NbRxDataToProcess = ((uint16_t)rx_fifo_depth * numerator[rx_fifo_threshold]) / 00525 (uint16_t)denominator[rx_fifo_threshold]; 00526 } 00527 } 00528 /** 00529 * @} 00530 */ 00531 00532 #endif /* HAL_USART_MODULE_ENABLED */ 00533 00534 /** 00535 * @} 00536 */ 00537 00538 /** 00539 * @} 00540 */ 00541