stm32l4xx_hal_uart_ex.c 24 KB

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