You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

730 lines
23 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_uart_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended UART HAL module driver.
  6. * This file provides firmware functions to manage the following extended
  7. * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. *
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### UART peripheral extended features #####
  15. ==============================================================================
  16. (#) Declare a UART_HandleTypeDef handle structure.
  17. (#) For the UART RS485 Driver Enable mode, initialize the UART registers
  18. by calling the HAL_RS485Ex_Init() API.
  19. (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming.
  20. -@- When UART operates in FIFO mode, FIFO mode must be enabled prior
  21. starting RX/TX transfers. Also RX/TX FIFO thresholds must be
  22. configured prior starting RX/TX transfers.
  23. @endverbatim
  24. ******************************************************************************
  25. * @attention
  26. *
  27. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  28. * All rights reserved.</center></h2>
  29. *
  30. * This software component is licensed by ST under BSD 3-Clause license,
  31. * the "License"; You may not use this file except in compliance with the
  32. * License. You may obtain a copy of the License at:
  33. * opensource.org/licenses/BSD-3-Clause
  34. *
  35. ******************************************************************************
  36. */
  37. /* Includes ------------------------------------------------------------------*/
  38. #include "stm32wbxx_hal.h"
  39. /** @addtogroup STM32WBxx_HAL_Driver
  40. * @{
  41. */
  42. /** @defgroup UARTEx UARTEx
  43. * @brief UART Extended HAL module driver
  44. * @{
  45. */
  46. #ifdef HAL_UART_MODULE_ENABLED
  47. /* Private typedef -----------------------------------------------------------*/
  48. /* Private define ------------------------------------------------------------*/
  49. /** @defgroup UARTEX_Private_Constants UARTEx Private Constants
  50. * @{
  51. */
  52. /* UART RX FIFO depth */
  53. #define RX_FIFO_DEPTH 8U
  54. /* UART TX FIFO depth */
  55. #define TX_FIFO_DEPTH 8U
  56. /**
  57. * @}
  58. */
  59. /* Private macros ------------------------------------------------------------*/
  60. /* Private variables ---------------------------------------------------------*/
  61. /* Private function prototypes -----------------------------------------------*/
  62. /** @defgroup UARTEx_Private_Functions UARTEx Private Functions
  63. * @{
  64. */
  65. static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
  66. static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart);
  67. /**
  68. * @}
  69. */
  70. /* Exported functions --------------------------------------------------------*/
  71. /** @defgroup UARTEx_Exported_Functions UARTEx Exported Functions
  72. * @{
  73. */
  74. /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions
  75. * @brief Extended Initialization and Configuration Functions
  76. *
  77. @verbatim
  78. ===============================================================================
  79. ##### Initialization and Configuration functions #####
  80. ===============================================================================
  81. [..]
  82. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  83. in asynchronous mode.
  84. (+) For the asynchronous mode the parameters below can be configured:
  85. (++) Baud Rate
  86. (++) Word Length
  87. (++) Stop Bit
  88. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  89. in the data register is transmitted but is changed by the parity bit.
  90. (++) Hardware flow control
  91. (++) Receiver/transmitter modes
  92. (++) Over Sampling Method
  93. (++) One-Bit Sampling Method
  94. (+) For the asynchronous mode, the following advanced features can be configured as well:
  95. (++) TX and/or RX pin level inversion
  96. (++) data logical level inversion
  97. (++) RX and TX pins swap
  98. (++) RX overrun detection disabling
  99. (++) DMA disabling on RX error
  100. (++) MSB first on communication line
  101. (++) auto Baud rate detection
  102. [..]
  103. The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration
  104. procedures (details for the procedures are available in reference manual).
  105. @endverbatim
  106. Depending on the frame length defined by the M1 and M0 bits (7-bit,
  107. 8-bit or 9-bit), the possible UART formats are listed in the
  108. following table.
  109. Table 1. UART frame format.
  110. +-----------------------------------------------------------------------+
  111. | M1 bit | M0 bit | PCE bit | UART frame |
  112. |---------|---------|-----------|---------------------------------------|
  113. | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  114. |---------|---------|-----------|---------------------------------------|
  115. | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  116. |---------|---------|-----------|---------------------------------------|
  117. | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  118. |---------|---------|-----------|---------------------------------------|
  119. | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  120. |---------|---------|-----------|---------------------------------------|
  121. | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  122. |---------|---------|-----------|---------------------------------------|
  123. | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  124. +-----------------------------------------------------------------------+
  125. * @{
  126. */
  127. /**
  128. * @brief Initialize the RS485 Driver enable feature according to the specified
  129. * parameters in the UART_InitTypeDef and creates the associated handle.
  130. * @param huart UART handle.
  131. * @param Polarity Select the driver enable polarity.
  132. * This parameter can be one of the following values:
  133. * @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
  134. * @arg @ref UART_DE_POLARITY_LOW DE signal is active low
  135. * @param AssertionTime Driver Enable assertion time:
  136. * 5-bit value defining the time between the activation of the DE (Driver Enable)
  137. * signal and the beginning of the start bit. It is expressed in sample time
  138. * units (1/8 or 1/16 bit time, depending on the oversampling rate)
  139. * @param DeassertionTime Driver Enable deassertion time:
  140. * 5-bit value defining the time between the end of the last stop bit, in a
  141. * transmitted message, and the de-activation of the DE (Driver Enable) signal.
  142. * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
  143. * oversampling rate).
  144. * @retval HAL status
  145. */
  146. HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
  147. uint32_t DeassertionTime)
  148. {
  149. uint32_t temp;
  150. /* Check the UART handle allocation */
  151. if (huart == NULL)
  152. {
  153. return HAL_ERROR;
  154. }
  155. /* Check the Driver Enable UART instance */
  156. assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
  157. /* Check the Driver Enable polarity */
  158. assert_param(IS_UART_DE_POLARITY(Polarity));
  159. /* Check the Driver Enable assertion time */
  160. assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
  161. /* Check the Driver Enable deassertion time */
  162. assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
  163. if (huart->gState == HAL_UART_STATE_RESET)
  164. {
  165. /* Allocate lock resource and initialize it */
  166. huart->Lock = HAL_UNLOCKED;
  167. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  168. UART_InitCallbacksToDefault(huart);
  169. if (huart->MspInitCallback == NULL)
  170. {
  171. huart->MspInitCallback = HAL_UART_MspInit;
  172. }
  173. /* Init the low level hardware */
  174. huart->MspInitCallback(huart);
  175. #else
  176. /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  177. HAL_UART_MspInit(huart);
  178. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  179. }
  180. huart->gState = HAL_UART_STATE_BUSY;
  181. /* Disable the Peripheral */
  182. __HAL_UART_DISABLE(huart);
  183. /* Set the UART Communication parameters */
  184. if (UART_SetConfig(huart) == HAL_ERROR)
  185. {
  186. return HAL_ERROR;
  187. }
  188. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  189. {
  190. UART_AdvFeatureConfig(huart);
  191. }
  192. /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
  193. SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
  194. /* Set the Driver Enable polarity */
  195. MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
  196. /* Set the Driver Enable assertion and deassertion times */
  197. temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
  198. temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
  199. MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);
  200. /* Enable the Peripheral */
  201. __HAL_UART_ENABLE(huart);
  202. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  203. return (UART_CheckIdleState(huart));
  204. }
  205. /**
  206. * @}
  207. */
  208. /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions
  209. * @brief Extended functions
  210. *
  211. @verbatim
  212. ===============================================================================
  213. ##### IO operation functions #####
  214. ===============================================================================
  215. This subsection provides a set of Wakeup and FIFO mode related callback functions.
  216. (#) Wakeup from Stop mode Callback:
  217. (+) HAL_UARTEx_WakeupCallback()
  218. (#) TX/RX Fifos Callbacks:
  219. (+) HAL_UARTEx_RxFifoFullCallback()
  220. (+) HAL_UARTEx_TxFifoEmptyCallback()
  221. @endverbatim
  222. * @{
  223. */
  224. /**
  225. * @brief UART wakeup from Stop mode callback.
  226. * @param huart UART handle.
  227. * @retval None
  228. */
  229. __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
  230. {
  231. /* Prevent unused argument(s) compilation warning */
  232. UNUSED(huart);
  233. /* NOTE : This function should not be modified, when the callback is needed,
  234. the HAL_UARTEx_WakeupCallback can be implemented in the user file.
  235. */
  236. }
  237. /**
  238. * @brief UART RX Fifo full callback.
  239. * @param huart UART handle.
  240. * @retval None
  241. */
  242. __weak void HAL_UARTEx_RxFifoFullCallback(UART_HandleTypeDef *huart)
  243. {
  244. /* Prevent unused argument(s) compilation warning */
  245. UNUSED(huart);
  246. /* NOTE : This function should not be modified, when the callback is needed,
  247. the HAL_UARTEx_RxFifoFullCallback can be implemented in the user file.
  248. */
  249. }
  250. /**
  251. * @brief UART TX Fifo empty callback.
  252. * @param huart UART handle.
  253. * @retval None
  254. */
  255. __weak void HAL_UARTEx_TxFifoEmptyCallback(UART_HandleTypeDef *huart)
  256. {
  257. /* Prevent unused argument(s) compilation warning */
  258. UNUSED(huart);
  259. /* NOTE : This function should not be modified, when the callback is needed,
  260. the HAL_UARTEx_TxFifoEmptyCallback can be implemented in the user file.
  261. */
  262. }
  263. /**
  264. * @}
  265. */
  266. /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions
  267. * @brief Extended Peripheral Control functions
  268. *
  269. @verbatim
  270. ===============================================================================
  271. ##### Peripheral Control functions #####
  272. ===============================================================================
  273. [..] This section provides the following functions:
  274. (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
  275. detection length to more than 4 bits for multiprocessor address mark wake up.
  276. (+) HAL_UARTEx_StopModeWakeUpSourceConfig() API defines the wake-up from stop mode
  277. trigger: address match, Start Bit detection or RXNE bit status.
  278. (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode
  279. (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
  280. (+) HAL_UARTEx_EnableFifoMode() API enables the FIFO mode
  281. (+) HAL_UARTEx_DisableFifoMode() API disables the FIFO mode
  282. (+) HAL_UARTEx_SetTxFifoThreshold() API sets the TX FIFO threshold
  283. (+) HAL_UARTEx_SetRxFifoThreshold() API sets the RX FIFO threshold
  284. @endverbatim
  285. * @{
  286. */
  287. /**
  288. * @brief By default in multiprocessor mode, when the wake up method is set
  289. * to address mark, the UART handles only 4-bit long addresses detection;
  290. * this API allows to enable longer addresses detection (6-, 7- or 8-bit
  291. * long).
  292. * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
  293. * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
  294. * @param huart UART handle.
  295. * @param AddressLength This parameter can be one of the following values:
  296. * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
  297. * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
  298. * @retval HAL status
  299. */
  300. HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
  301. {
  302. /* Check the UART handle allocation */
  303. if (huart == NULL)
  304. {
  305. return HAL_ERROR;
  306. }
  307. /* Check the address length parameter */
  308. assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
  309. huart->gState = HAL_UART_STATE_BUSY;
  310. /* Disable the Peripheral */
  311. __HAL_UART_DISABLE(huart);
  312. /* Set the address length */
  313. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
  314. /* Enable the Peripheral */
  315. __HAL_UART_ENABLE(huart);
  316. /* TEACK and/or REACK to check before moving huart->gState to Ready */
  317. return (UART_CheckIdleState(huart));
  318. }
  319. /**
  320. * @brief Set Wakeup from Stop mode interrupt flag selection.
  321. * @note It is the application responsibility to enable the interrupt used as
  322. * usart_wkup interrupt source before entering low-power mode.
  323. * @param huart UART handle.
  324. * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status.
  325. * This parameter can be one of the following values:
  326. * @arg @ref UART_WAKEUP_ON_ADDRESS
  327. * @arg @ref UART_WAKEUP_ON_STARTBIT
  328. * @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
  329. * @retval HAL status
  330. */
  331. HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  332. {
  333. HAL_StatusTypeDef status = HAL_OK;
  334. uint32_t tickstart;
  335. /* check the wake-up from stop mode UART instance */
  336. assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
  337. /* check the wake-up selection parameter */
  338. assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
  339. /* Process Locked */
  340. __HAL_LOCK(huart);
  341. huart->gState = HAL_UART_STATE_BUSY;
  342. /* Disable the Peripheral */
  343. __HAL_UART_DISABLE(huart);
  344. /* Set the wake-up selection scheme */
  345. MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
  346. if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
  347. {
  348. UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
  349. }
  350. /* Enable the Peripheral */
  351. __HAL_UART_ENABLE(huart);
  352. /* Init tickstart for timeout managment*/
  353. tickstart = HAL_GetTick();
  354. /* Wait until REACK flag is set */
  355. if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  356. {
  357. status = HAL_TIMEOUT;
  358. }
  359. else
  360. {
  361. /* Initialize the UART State */
  362. huart->gState = HAL_UART_STATE_READY;
  363. }
  364. /* Process Unlocked */
  365. __HAL_UNLOCK(huart);
  366. return status;
  367. }
  368. /**
  369. * @brief Enable UART Stop Mode.
  370. * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE.
  371. * @param huart UART handle.
  372. * @retval HAL status
  373. */
  374. HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
  375. {
  376. /* Process Locked */
  377. __HAL_LOCK(huart);
  378. /* Set UESM bit */
  379. SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
  380. /* Process Unlocked */
  381. __HAL_UNLOCK(huart);
  382. return HAL_OK;
  383. }
  384. /**
  385. * @brief Disable UART Stop Mode.
  386. * @param huart UART handle.
  387. * @retval HAL status
  388. */
  389. HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
  390. {
  391. /* Process Locked */
  392. __HAL_LOCK(huart);
  393. /* Clear UESM bit */
  394. CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
  395. /* Process Unlocked */
  396. __HAL_UNLOCK(huart);
  397. return HAL_OK;
  398. }
  399. /**
  400. * @brief Enable the FIFO mode.
  401. * @param huart UART handle.
  402. * @retval HAL status
  403. */
  404. HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef *huart)
  405. {
  406. uint32_t tmpcr1;
  407. /* Check parameters */
  408. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  409. /* Process Locked */
  410. __HAL_LOCK(huart);
  411. huart->gState = HAL_UART_STATE_BUSY;
  412. /* Save actual UART configuration */
  413. tmpcr1 = READ_REG(huart->Instance->CR1);
  414. /* Disable UART */
  415. __HAL_UART_DISABLE(huart);
  416. /* Enable FIFO mode */
  417. SET_BIT(tmpcr1, USART_CR1_FIFOEN);
  418. huart->FifoMode = UART_FIFOMODE_ENABLE;
  419. /* Restore UART configuration */
  420. WRITE_REG(huart->Instance->CR1, tmpcr1);
  421. /* Determine the number of data to process during RX/TX ISR execution */
  422. UARTEx_SetNbDataToProcess(huart);
  423. huart->gState = HAL_UART_STATE_READY;
  424. /* Process Unlocked */
  425. __HAL_UNLOCK(huart);
  426. return HAL_OK;
  427. }
  428. /**
  429. * @brief Disable the FIFO mode.
  430. * @param huart UART handle.
  431. * @retval HAL status
  432. */
  433. HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart)
  434. {
  435. uint32_t tmpcr1;
  436. /* Check parameters */
  437. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  438. /* Process Locked */
  439. __HAL_LOCK(huart);
  440. huart->gState = HAL_UART_STATE_BUSY;
  441. /* Save actual UART configuration */
  442. tmpcr1 = READ_REG(huart->Instance->CR1);
  443. /* Disable UART */
  444. __HAL_UART_DISABLE(huart);
  445. /* Enable FIFO mode */
  446. CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
  447. huart->FifoMode = UART_FIFOMODE_DISABLE;
  448. /* Restore UART configuration */
  449. WRITE_REG(huart->Instance->CR1, tmpcr1);
  450. huart->gState = HAL_UART_STATE_READY;
  451. /* Process Unlocked */
  452. __HAL_UNLOCK(huart);
  453. return HAL_OK;
  454. }
  455. /**
  456. * @brief Set the TXFIFO threshold.
  457. * @param huart UART handle.
  458. * @param Threshold TX FIFO threshold value
  459. * This parameter can be one of the following values:
  460. * @arg @ref UART_TXFIFO_THRESHOLD_1_8
  461. * @arg @ref UART_TXFIFO_THRESHOLD_1_4
  462. * @arg @ref UART_TXFIFO_THRESHOLD_1_2
  463. * @arg @ref UART_TXFIFO_THRESHOLD_3_4
  464. * @arg @ref UART_TXFIFO_THRESHOLD_7_8
  465. * @arg @ref UART_TXFIFO_THRESHOLD_8_8
  466. * @retval HAL status
  467. */
  468. HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
  469. {
  470. uint32_t tmpcr1;
  471. /* Check parameters */
  472. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  473. assert_param(IS_UART_TXFIFO_THRESHOLD(Threshold));
  474. /* Process Locked */
  475. __HAL_LOCK(huart);
  476. huart->gState = HAL_UART_STATE_BUSY;
  477. /* Save actual UART configuration */
  478. tmpcr1 = READ_REG(huart->Instance->CR1);
  479. /* Disable UART */
  480. __HAL_UART_DISABLE(huart);
  481. /* Update TX threshold configuration */
  482. MODIFY_REG(huart->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
  483. /* Determine the number of data to process during RX/TX ISR execution */
  484. UARTEx_SetNbDataToProcess(huart);
  485. /* Restore UART configuration */
  486. WRITE_REG(huart->Instance->CR1, tmpcr1);
  487. huart->gState = HAL_UART_STATE_READY;
  488. /* Process Unlocked */
  489. __HAL_UNLOCK(huart);
  490. return HAL_OK;
  491. }
  492. /**
  493. * @brief Set the RXFIFO threshold.
  494. * @param huart UART handle.
  495. * @param Threshold RX FIFO threshold value
  496. * This parameter can be one of the following values:
  497. * @arg @ref UART_RXFIFO_THRESHOLD_1_8
  498. * @arg @ref UART_RXFIFO_THRESHOLD_1_4
  499. * @arg @ref UART_RXFIFO_THRESHOLD_1_2
  500. * @arg @ref UART_RXFIFO_THRESHOLD_3_4
  501. * @arg @ref UART_RXFIFO_THRESHOLD_7_8
  502. * @arg @ref UART_RXFIFO_THRESHOLD_8_8
  503. * @retval HAL status
  504. */
  505. HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
  506. {
  507. uint32_t tmpcr1;
  508. /* Check the parameters */
  509. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  510. assert_param(IS_UART_RXFIFO_THRESHOLD(Threshold));
  511. /* Process Locked */
  512. __HAL_LOCK(huart);
  513. huart->gState = HAL_UART_STATE_BUSY;
  514. /* Save actual UART configuration */
  515. tmpcr1 = READ_REG(huart->Instance->CR1);
  516. /* Disable UART */
  517. __HAL_UART_DISABLE(huart);
  518. /* Update RX threshold configuration */
  519. MODIFY_REG(huart->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
  520. /* Determine the number of data to process during RX/TX ISR execution */
  521. UARTEx_SetNbDataToProcess(huart);
  522. /* Restore UART configuration */
  523. WRITE_REG(huart->Instance->CR1, tmpcr1);
  524. huart->gState = HAL_UART_STATE_READY;
  525. /* Process Unlocked */
  526. __HAL_UNLOCK(huart);
  527. return HAL_OK;
  528. }
  529. /**
  530. * @}
  531. */
  532. /**
  533. * @}
  534. */
  535. /** @addtogroup UARTEx_Private_Functions
  536. * @{
  537. */
  538. /**
  539. * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
  540. * @param huart UART handle.
  541. * @param WakeUpSelection UART wake up from stop mode parameters.
  542. * @retval None
  543. */
  544. static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  545. {
  546. assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
  547. /* Set the USART address length */
  548. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
  549. /* Set the USART address node */
  550. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
  551. }
  552. /**
  553. * @brief Calculate the number of data to process in RX/TX ISR.
  554. * @note The RX FIFO depth and the TX FIFO depth is extracted from
  555. * the UART configuration registers.
  556. * @param huart UART handle.
  557. * @retval None
  558. */
  559. static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart)
  560. {
  561. uint8_t rx_fifo_depth;
  562. uint8_t tx_fifo_depth;
  563. uint8_t rx_fifo_threshold;
  564. uint8_t tx_fifo_threshold;
  565. uint8_t numerator[] = {1U, 1U, 1U, 3U, 7U, 1U, 0U, 0U};
  566. uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U, 1U, 1U};
  567. if (huart->FifoMode == UART_FIFOMODE_DISABLE)
  568. {
  569. huart->NbTxDataToProcess = 1U;
  570. huart->NbRxDataToProcess = 1U;
  571. }
  572. else
  573. {
  574. rx_fifo_depth = RX_FIFO_DEPTH;
  575. tx_fifo_depth = TX_FIFO_DEPTH;
  576. rx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
  577. tx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
  578. huart->NbTxDataToProcess = ((uint16_t)tx_fifo_depth * numerator[tx_fifo_threshold]) / (uint16_t)denominator[tx_fifo_threshold];
  579. huart->NbRxDataToProcess = ((uint16_t)rx_fifo_depth * numerator[rx_fifo_threshold]) / (uint16_t)denominator[rx_fifo_threshold];
  580. }
  581. }
  582. /**
  583. * @}
  584. */
  585. #endif /* HAL_UART_MODULE_ENABLED */
  586. /**
  587. * @}
  588. */
  589. /**
  590. * @}
  591. */
  592. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/