stm32l4xx_hal_uart_ex.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2017 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ==============================================================================
  25. ##### UART peripheral extended features #####
  26. ==============================================================================
  27. (#) Declare a UART_HandleTypeDef handle structure.
  28. (#) For the UART RS485 Driver Enable mode, initialize the UART registers
  29. by calling the HAL_RS485Ex_Init() API.
  30. (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming.
  31. -@- When UART operates in FIFO mode, FIFO mode must be enabled prior
  32. starting RX/TX transfers. Also RX/TX FIFO thresholds must be
  33. configured prior starting RX/TX transfers.
  34. @endverbatim
  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. [..] This subsection also provides a set of additional functions providing enhanced reception
  293. services to user. (For example, these functions allow application to handle use cases
  294. where number of data to be received is unknown).
  295. (#) Compared to standard reception services which only consider number of received
  296. data elements as reception completion criteria, these functions also consider additional events
  297. as triggers for updating reception status to caller :
  298. (+) Detection of inactivity period (RX line has not been active for a given period).
  299. (++) RX inactivity detected by IDLE event, i.e. RX line has been in idle state (normally high state)
  300. for 1 frame time, after last received byte.
  301. (++) RX inactivity detected by RTO, i.e. line has been in idle state
  302. for a programmable time, after last received byte.
  303. (+) Detection that a specific character has been received.
  304. (#) There are two mode of transfer:
  305. (+) Blocking mode: The reception is performed in polling mode, until either expected number of data is received,
  306. or till IDLE event occurs. Reception is handled only during function execution.
  307. When function exits, no data reception could occur. HAL status and number of actually received data elements,
  308. are returned by function after finishing transfer.
  309. (+) Non-Blocking mode: The reception is performed using Interrupts or DMA.
  310. These API's return the HAL status.
  311. The end of the data processing will be indicated through the
  312. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when using DMA mode.
  313. The HAL_UARTEx_RxEventCallback() user callback will be executed during Receive process
  314. The HAL_UART_ErrorCallback()user callback will be executed when a reception error is detected.
  315. (#) Blocking mode API:
  316. (+) HAL_UARTEx_ReceiveToIdle()
  317. (#) Non-Blocking mode API with Interrupt:
  318. (+) HAL_UARTEx_ReceiveToIdle_IT()
  319. (#) Non-Blocking mode API with DMA:
  320. (+) HAL_UARTEx_ReceiveToIdle_DMA()
  321. @endverbatim
  322. * @{
  323. */
  324. #if defined(USART_CR3_UCESM)
  325. /**
  326. * @brief Keep UART Clock enabled when in Stop Mode.
  327. * @note When the USART clock source is configured to be LSE or HSI, it is possible to keep enabled
  328. * this clock during STOP mode by setting the UCESM bit in USART_CR3 control register.
  329. * @note When LPUART is used to wakeup from stop with LSE is selected as LPUART clock source,
  330. * and desired baud rate is 9600 baud, the bit UCESM bit in LPUART_CR3 control register must be set.
  331. * @param huart UART handle.
  332. * @retval HAL status
  333. */
  334. HAL_StatusTypeDef HAL_UARTEx_EnableClockStopMode(UART_HandleTypeDef *huart)
  335. {
  336. /* Process Locked */
  337. __HAL_LOCK(huart);
  338. /* Set UCESM bit */
  339. ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_UCESM);
  340. /* Process Unlocked */
  341. __HAL_UNLOCK(huart);
  342. return HAL_OK;
  343. }
  344. /**
  345. * @brief Disable UART Clock when in Stop Mode.
  346. * @param huart UART handle.
  347. * @retval HAL status
  348. */
  349. HAL_StatusTypeDef HAL_UARTEx_DisableClockStopMode(UART_HandleTypeDef *huart)
  350. {
  351. /* Process Locked */
  352. __HAL_LOCK(huart);
  353. /* Clear UCESM bit */
  354. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_UCESM);
  355. /* Process Unlocked */
  356. __HAL_UNLOCK(huart);
  357. return HAL_OK;
  358. }
  359. #endif /* USART_CR3_UCESM */
  360. /**
  361. * @brief By default in multiprocessor mode, when the wake up method is set
  362. * to address mark, the UART handles only 4-bit long addresses detection;
  363. * this API allows to enable longer addresses detection (6-, 7- or 8-bit
  364. * long).
  365. * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
  366. * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
  367. * @param huart UART handle.
  368. * @param AddressLength This parameter can be one of the following values:
  369. * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
  370. * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
  371. * @retval HAL status
  372. */
  373. HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
  374. {
  375. /* Check the UART handle allocation */
  376. if (huart == NULL)
  377. {
  378. return HAL_ERROR;
  379. }
  380. /* Check the address length parameter */
  381. assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
  382. huart->gState = HAL_UART_STATE_BUSY;
  383. /* Disable the Peripheral */
  384. __HAL_UART_DISABLE(huart);
  385. /* Set the address length */
  386. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
  387. /* Enable the Peripheral */
  388. __HAL_UART_ENABLE(huart);
  389. /* TEACK and/or REACK to check before moving huart->gState to Ready */
  390. return (UART_CheckIdleState(huart));
  391. }
  392. /**
  393. * @brief Set Wakeup from Stop mode interrupt flag selection.
  394. * @note It is the application responsibility to enable the interrupt used as
  395. * usart_wkup interrupt source before entering low-power mode.
  396. * @param huart UART handle.
  397. * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status.
  398. * This parameter can be one of the following values:
  399. * @arg @ref UART_WAKEUP_ON_ADDRESS
  400. * @arg @ref UART_WAKEUP_ON_STARTBIT
  401. * @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
  402. * @retval HAL status
  403. */
  404. HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  405. {
  406. HAL_StatusTypeDef status = HAL_OK;
  407. uint32_t tickstart;
  408. /* check the wake-up from stop mode UART instance */
  409. assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
  410. /* check the wake-up selection parameter */
  411. assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
  412. /* Process Locked */
  413. __HAL_LOCK(huart);
  414. huart->gState = HAL_UART_STATE_BUSY;
  415. /* Disable the Peripheral */
  416. __HAL_UART_DISABLE(huart);
  417. /* Set the wake-up selection scheme */
  418. MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
  419. if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
  420. {
  421. UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
  422. }
  423. /* Enable the Peripheral */
  424. __HAL_UART_ENABLE(huart);
  425. /* Init tickstart for timeout management */
  426. tickstart = HAL_GetTick();
  427. /* Wait until REACK flag is set */
  428. if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  429. {
  430. status = HAL_TIMEOUT;
  431. }
  432. else
  433. {
  434. /* Initialize the UART State */
  435. huart->gState = HAL_UART_STATE_READY;
  436. }
  437. /* Process Unlocked */
  438. __HAL_UNLOCK(huart);
  439. return status;
  440. }
  441. /**
  442. * @brief Enable UART Stop Mode.
  443. * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE.
  444. * @param huart UART handle.
  445. * @retval HAL status
  446. */
  447. HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
  448. {
  449. /* Process Locked */
  450. __HAL_LOCK(huart);
  451. /* Set UESM bit */
  452. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
  453. /* Process Unlocked */
  454. __HAL_UNLOCK(huart);
  455. return HAL_OK;
  456. }
  457. /**
  458. * @brief Disable UART Stop Mode.
  459. * @param huart UART handle.
  460. * @retval HAL status
  461. */
  462. HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
  463. {
  464. /* Process Locked */
  465. __HAL_LOCK(huart);
  466. /* Clear UESM bit */
  467. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
  468. /* Process Unlocked */
  469. __HAL_UNLOCK(huart);
  470. return HAL_OK;
  471. }
  472. #if defined(USART_CR1_FIFOEN)
  473. /**
  474. * @brief Enable the FIFO mode.
  475. * @param huart UART handle.
  476. * @retval HAL status
  477. */
  478. HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(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. SET_BIT(tmpcr1, USART_CR1_FIFOEN);
  492. huart->FifoMode = UART_FIFOMODE_ENABLE;
  493. /* Restore UART configuration */
  494. WRITE_REG(huart->Instance->CR1, tmpcr1);
  495. /* Determine the number of data to process during RX/TX ISR execution */
  496. UARTEx_SetNbDataToProcess(huart);
  497. huart->gState = HAL_UART_STATE_READY;
  498. /* Process Unlocked */
  499. __HAL_UNLOCK(huart);
  500. return HAL_OK;
  501. }
  502. /**
  503. * @brief Disable the FIFO mode.
  504. * @param huart UART handle.
  505. * @retval HAL status
  506. */
  507. HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart)
  508. {
  509. uint32_t tmpcr1;
  510. /* Check parameters */
  511. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  512. /* Process Locked */
  513. __HAL_LOCK(huart);
  514. huart->gState = HAL_UART_STATE_BUSY;
  515. /* Save actual UART configuration */
  516. tmpcr1 = READ_REG(huart->Instance->CR1);
  517. /* Disable UART */
  518. __HAL_UART_DISABLE(huart);
  519. /* Enable FIFO mode */
  520. CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
  521. huart->FifoMode = UART_FIFOMODE_DISABLE;
  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. * @brief Set the TXFIFO threshold.
  531. * @param huart UART handle.
  532. * @param Threshold TX FIFO threshold value
  533. * This parameter can be one of the following values:
  534. * @arg @ref UART_TXFIFO_THRESHOLD_1_8
  535. * @arg @ref UART_TXFIFO_THRESHOLD_1_4
  536. * @arg @ref UART_TXFIFO_THRESHOLD_1_2
  537. * @arg @ref UART_TXFIFO_THRESHOLD_3_4
  538. * @arg @ref UART_TXFIFO_THRESHOLD_7_8
  539. * @arg @ref UART_TXFIFO_THRESHOLD_8_8
  540. * @retval HAL status
  541. */
  542. HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
  543. {
  544. uint32_t tmpcr1;
  545. /* Check parameters */
  546. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  547. assert_param(IS_UART_TXFIFO_THRESHOLD(Threshold));
  548. /* Process Locked */
  549. __HAL_LOCK(huart);
  550. huart->gState = HAL_UART_STATE_BUSY;
  551. /* Save actual UART configuration */
  552. tmpcr1 = READ_REG(huart->Instance->CR1);
  553. /* Disable UART */
  554. __HAL_UART_DISABLE(huart);
  555. /* Update TX threshold configuration */
  556. MODIFY_REG(huart->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
  557. /* Determine the number of data to process during RX/TX ISR execution */
  558. UARTEx_SetNbDataToProcess(huart);
  559. /* Restore UART configuration */
  560. WRITE_REG(huart->Instance->CR1, tmpcr1);
  561. huart->gState = HAL_UART_STATE_READY;
  562. /* Process Unlocked */
  563. __HAL_UNLOCK(huart);
  564. return HAL_OK;
  565. }
  566. /**
  567. * @brief Set the RXFIFO threshold.
  568. * @param huart UART handle.
  569. * @param Threshold RX FIFO threshold value
  570. * This parameter can be one of the following values:
  571. * @arg @ref UART_RXFIFO_THRESHOLD_1_8
  572. * @arg @ref UART_RXFIFO_THRESHOLD_1_4
  573. * @arg @ref UART_RXFIFO_THRESHOLD_1_2
  574. * @arg @ref UART_RXFIFO_THRESHOLD_3_4
  575. * @arg @ref UART_RXFIFO_THRESHOLD_7_8
  576. * @arg @ref UART_RXFIFO_THRESHOLD_8_8
  577. * @retval HAL status
  578. */
  579. HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
  580. {
  581. uint32_t tmpcr1;
  582. /* Check the parameters */
  583. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  584. assert_param(IS_UART_RXFIFO_THRESHOLD(Threshold));
  585. /* Process Locked */
  586. __HAL_LOCK(huart);
  587. huart->gState = HAL_UART_STATE_BUSY;
  588. /* Save actual UART configuration */
  589. tmpcr1 = READ_REG(huart->Instance->CR1);
  590. /* Disable UART */
  591. __HAL_UART_DISABLE(huart);
  592. /* Update RX threshold configuration */
  593. MODIFY_REG(huart->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
  594. /* Determine the number of data to process during RX/TX ISR execution */
  595. UARTEx_SetNbDataToProcess(huart);
  596. /* Restore UART configuration */
  597. WRITE_REG(huart->Instance->CR1, tmpcr1);
  598. huart->gState = HAL_UART_STATE_READY;
  599. /* Process Unlocked */
  600. __HAL_UNLOCK(huart);
  601. return HAL_OK;
  602. }
  603. #endif /* USART_CR1_FIFOEN */
  604. /**
  605. * @brief Receive an amount of data in blocking mode till either the expected number of data
  606. * is received or an IDLE event occurs.
  607. * @note HAL_OK is returned if reception is completed (expected number of data has been received)
  608. * or if reception is stopped after IDLE event (less than the expected number of data has been received)
  609. * In this case, RxLen output parameter indicates number of data available in reception buffer.
  610. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  611. * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
  612. * of uint16_t available through pData.
  613. * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
  614. * is not empty. Read operations from the RDR register are performed when
  615. * RXFNE flag is set. From hardware perspective, RXFNE flag and
  616. * RXNE are mapped on the same bit-field.
  617. * @param huart UART handle.
  618. * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
  619. * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
  620. * @param RxLen Number of data elements finally received
  621. * (could be lower than Size, in case reception ends on IDLE event)
  622. * @param Timeout Timeout duration expressed in ms (covers the whole reception sequence).
  623. * @retval HAL status
  624. */
  625. HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen,
  626. uint32_t Timeout)
  627. {
  628. uint8_t *pdata8bits;
  629. uint16_t *pdata16bits;
  630. uint16_t uhMask;
  631. uint32_t tickstart;
  632. /* Check that a Rx process is not already ongoing */
  633. if (huart->RxState == HAL_UART_STATE_READY)
  634. {
  635. if ((pData == NULL) || (Size == 0U))
  636. {
  637. return HAL_ERROR;
  638. }
  639. __HAL_LOCK(huart);
  640. huart->ErrorCode = HAL_UART_ERROR_NONE;
  641. huart->RxState = HAL_UART_STATE_BUSY_RX;
  642. huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
  643. /* Init tickstart for timeout management */
  644. tickstart = HAL_GetTick();
  645. huart->RxXferSize = Size;
  646. huart->RxXferCount = Size;
  647. /* Computation of UART mask to apply to RDR register */
  648. UART_MASK_COMPUTATION(huart);
  649. uhMask = huart->Mask;
  650. /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  651. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  652. {
  653. pdata8bits = NULL;
  654. pdata16bits = (uint16_t *) pData;
  655. }
  656. else
  657. {
  658. pdata8bits = pData;
  659. pdata16bits = NULL;
  660. }
  661. __HAL_UNLOCK(huart);
  662. /* Initialize output number of received elements */
  663. *RxLen = 0U;
  664. /* as long as data have to be received */
  665. while (huart->RxXferCount > 0U)
  666. {
  667. /* Check if IDLE flag is set */
  668. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
  669. {
  670. /* Clear IDLE flag in ISR */
  671. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  672. /* If Set, but no data ever received, clear flag without exiting loop */
  673. /* If Set, and data has already been received, this means Idle Event is valid : End reception */
  674. if (*RxLen > 0U)
  675. {
  676. huart->RxState = HAL_UART_STATE_READY;
  677. return HAL_OK;
  678. }
  679. }
  680. /* Check if RXNE flag is set */
  681. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE))
  682. {
  683. if (pdata8bits == NULL)
  684. {
  685. *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
  686. pdata16bits++;
  687. }
  688. else
  689. {
  690. *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  691. pdata8bits++;
  692. }
  693. /* Increment number of received elements */
  694. *RxLen += 1U;
  695. huart->RxXferCount--;
  696. }
  697. /* Check for the Timeout */
  698. if (Timeout != HAL_MAX_DELAY)
  699. {
  700. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  701. {
  702. huart->RxState = HAL_UART_STATE_READY;
  703. return HAL_TIMEOUT;
  704. }
  705. }
  706. }
  707. /* Set number of received elements in output parameter : RxLen */
  708. *RxLen = huart->RxXferSize - huart->RxXferCount;
  709. /* At end of Rx process, restore huart->RxState to Ready */
  710. huart->RxState = HAL_UART_STATE_READY;
  711. return HAL_OK;
  712. }
  713. else
  714. {
  715. return HAL_BUSY;
  716. }
  717. }
  718. /**
  719. * @brief Receive an amount of data in interrupt mode till either the expected number of data
  720. * is received or an IDLE event occurs.
  721. * @note Reception is initiated by this function call. Further progress of reception is achieved thanks
  722. * to UART interrupts raised by RXNE and IDLE events. Callback is called at end of reception indicating
  723. * number of received data elements.
  724. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  725. * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
  726. * of uint16_t available through pData.
  727. * @param huart UART handle.
  728. * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
  729. * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
  730. * @retval HAL status
  731. */
  732. HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  733. {
  734. HAL_StatusTypeDef status;
  735. /* Check that a Rx process is not already ongoing */
  736. if (huart->RxState == HAL_UART_STATE_READY)
  737. {
  738. if ((pData == NULL) || (Size == 0U))
  739. {
  740. return HAL_ERROR;
  741. }
  742. __HAL_LOCK(huart);
  743. /* Set Reception type to reception till IDLE Event*/
  744. huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
  745. status = UART_Start_Receive_IT(huart, pData, Size);
  746. /* Check Rx process has been successfully started */
  747. if (status == HAL_OK)
  748. {
  749. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  750. {
  751. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  752. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  753. }
  754. else
  755. {
  756. /* In case of errors already pending when reception is started,
  757. Interrupts may have already been raised and lead to reception abortion.
  758. (Overrun error for instance).
  759. In such case Reception Type has been reset to HAL_UART_RECEPTION_STANDARD. */
  760. status = HAL_ERROR;
  761. }
  762. }
  763. return status;
  764. }
  765. else
  766. {
  767. return HAL_BUSY;
  768. }
  769. }
  770. /**
  771. * @brief Receive an amount of data in DMA mode till either the expected number
  772. * of data is received or an IDLE event occurs.
  773. * @note Reception is initiated by this function call. Further progress of reception is achieved thanks
  774. * to DMA services, transferring automatically received data elements in user reception buffer and
  775. * calling registered callbacks at half/end of reception. UART IDLE events are also used to consider
  776. * reception phase as ended. In all cases, callback execution will indicate number of received data elements.
  777. * @note When the UART parity is enabled (PCE = 1), the received data contain
  778. * the parity bit (MSB position).
  779. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  780. * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
  781. * of uint16_t available through pData.
  782. * @param huart UART handle.
  783. * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
  784. * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
  785. * @retval HAL status
  786. */
  787. HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  788. {
  789. HAL_StatusTypeDef status;
  790. /* Check that a Rx process is not already ongoing */
  791. if (huart->RxState == HAL_UART_STATE_READY)
  792. {
  793. if ((pData == NULL) || (Size == 0U))
  794. {
  795. return HAL_ERROR;
  796. }
  797. __HAL_LOCK(huart);
  798. /* Set Reception type to reception till IDLE Event*/
  799. huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
  800. status = UART_Start_Receive_DMA(huart, pData, Size);
  801. /* Check Rx process has been successfully started */
  802. if (status == HAL_OK)
  803. {
  804. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  805. {
  806. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  807. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  808. }
  809. else
  810. {
  811. /* In case of errors already pending when reception is started,
  812. Interrupts may have already been raised and lead to reception abortion.
  813. (Overrun error for instance).
  814. In such case Reception Type has been reset to HAL_UART_RECEPTION_STANDARD. */
  815. status = HAL_ERROR;
  816. }
  817. }
  818. return status;
  819. }
  820. else
  821. {
  822. return HAL_BUSY;
  823. }
  824. }
  825. /**
  826. * @}
  827. */
  828. /**
  829. * @}
  830. */
  831. /** @addtogroup UARTEx_Private_Functions
  832. * @{
  833. */
  834. /**
  835. * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
  836. * @param huart UART handle.
  837. * @param WakeUpSelection UART wake up from stop mode parameters.
  838. * @retval None
  839. */
  840. static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  841. {
  842. assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
  843. /* Set the USART address length */
  844. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
  845. /* Set the USART address node */
  846. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
  847. }
  848. #if defined(USART_CR1_FIFOEN)
  849. /**
  850. * @brief Calculate the number of data to process in RX/TX ISR.
  851. * @note The RX FIFO depth and the TX FIFO depth is extracted from
  852. * the UART configuration registers.
  853. * @param huart UART handle.
  854. * @retval None
  855. */
  856. static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart)
  857. {
  858. uint8_t rx_fifo_depth;
  859. uint8_t tx_fifo_depth;
  860. uint8_t rx_fifo_threshold;
  861. uint8_t tx_fifo_threshold;
  862. static const uint8_t numerator[] = {1U, 1U, 1U, 3U, 7U, 1U, 0U, 0U};
  863. static const uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U, 1U, 1U};
  864. if (huart->FifoMode == UART_FIFOMODE_DISABLE)
  865. {
  866. huart->NbTxDataToProcess = 1U;
  867. huart->NbRxDataToProcess = 1U;
  868. }
  869. else
  870. {
  871. rx_fifo_depth = RX_FIFO_DEPTH;
  872. tx_fifo_depth = TX_FIFO_DEPTH;
  873. rx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
  874. tx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
  875. huart->NbTxDataToProcess = ((uint16_t)tx_fifo_depth * numerator[tx_fifo_threshold]) /
  876. (uint16_t)denominator[tx_fifo_threshold];
  877. huart->NbRxDataToProcess = ((uint16_t)rx_fifo_depth * numerator[rx_fifo_threshold]) /
  878. (uint16_t)denominator[rx_fifo_threshold];
  879. }
  880. }
  881. #endif /* USART_CR1_FIFOEN */
  882. /**
  883. * @}
  884. */
  885. #endif /* HAL_UART_MODULE_ENABLED */
  886. /**
  887. * @}
  888. */
  889. /**
  890. * @}
  891. */