stm32h7xx_hal_uart_ex.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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 "stm32h7xx_hal.h"
  39. /** @addtogroup STM32H7xx_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 16U
  54. /* UART TX FIFO depth */
  55. #define TX_FIFO_DEPTH 16U
  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. /* Perform advanced settings configuration */
  184. /* For some items, configuration requires to be done prior TE and RE bits are set */
  185. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  186. {
  187. UART_AdvFeatureConfig(huart);
  188. }
  189. /* Set the UART Communication parameters */
  190. if (UART_SetConfig(huart) == HAL_ERROR)
  191. {
  192. return HAL_ERROR;
  193. }
  194. /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
  195. SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
  196. /* Set the Driver Enable polarity */
  197. MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
  198. /* Set the Driver Enable assertion and deassertion times */
  199. temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
  200. temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
  201. MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);
  202. /* Enable the Peripheral */
  203. __HAL_UART_ENABLE(huart);
  204. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  205. return (UART_CheckIdleState(huart));
  206. }
  207. /**
  208. * @}
  209. */
  210. /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions
  211. * @brief Extended functions
  212. *
  213. @verbatim
  214. ===============================================================================
  215. ##### IO operation functions #####
  216. ===============================================================================
  217. This subsection provides a set of Wakeup and FIFO mode related callback functions.
  218. (#) Wakeup from Stop mode Callback:
  219. (+) HAL_UARTEx_WakeupCallback()
  220. (#) TX/RX Fifos Callbacks:
  221. (+) HAL_UARTEx_RxFifoFullCallback()
  222. (+) HAL_UARTEx_TxFifoEmptyCallback()
  223. @endverbatim
  224. * @{
  225. */
  226. /**
  227. * @brief UART wakeup from Stop mode callback.
  228. * @param huart UART handle.
  229. * @retval None
  230. */
  231. __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
  232. {
  233. /* Prevent unused argument(s) compilation warning */
  234. UNUSED(huart);
  235. /* NOTE : This function should not be modified, when the callback is needed,
  236. the HAL_UARTEx_WakeupCallback can be implemented in the user file.
  237. */
  238. }
  239. /**
  240. * @brief UART RX Fifo full callback.
  241. * @param huart UART handle.
  242. * @retval None
  243. */
  244. __weak void HAL_UARTEx_RxFifoFullCallback(UART_HandleTypeDef *huart)
  245. {
  246. /* Prevent unused argument(s) compilation warning */
  247. UNUSED(huart);
  248. /* NOTE : This function should not be modified, when the callback is needed,
  249. the HAL_UARTEx_RxFifoFullCallback can be implemented in the user file.
  250. */
  251. }
  252. /**
  253. * @brief UART TX Fifo empty callback.
  254. * @param huart UART handle.
  255. * @retval None
  256. */
  257. __weak void HAL_UARTEx_TxFifoEmptyCallback(UART_HandleTypeDef *huart)
  258. {
  259. /* Prevent unused argument(s) compilation warning */
  260. UNUSED(huart);
  261. /* NOTE : This function should not be modified, when the callback is needed,
  262. the HAL_UARTEx_TxFifoEmptyCallback can be implemented in the user file.
  263. */
  264. }
  265. /**
  266. * @}
  267. */
  268. /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions
  269. * @brief Extended Peripheral Control functions
  270. *
  271. @verbatim
  272. ===============================================================================
  273. ##### Peripheral Control functions #####
  274. ===============================================================================
  275. [..] This section provides the following functions:
  276. (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
  277. detection length to more than 4 bits for multiprocessor address mark wake up.
  278. (+) HAL_UARTEx_StopModeWakeUpSourceConfig() API defines the wake-up from stop mode
  279. trigger: address match, Start Bit detection or RXNE bit status.
  280. (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode
  281. (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
  282. (+) HAL_UARTEx_EnableFifoMode() API enables the FIFO mode
  283. (+) HAL_UARTEx_DisableFifoMode() API disables the FIFO mode
  284. (+) HAL_UARTEx_SetTxFifoThreshold() API sets the TX FIFO threshold
  285. (+) HAL_UARTEx_SetRxFifoThreshold() API sets the RX FIFO threshold
  286. [..] This subsection also provides a set of additional functions providing enhanced reception
  287. services to user. (For example, these functions allow application to handle use cases
  288. where number of data to be received is unknown).
  289. (#) Compared to standard reception services which only consider number of received
  290. data elements as reception completion criteria, these functions also consider additional events
  291. as triggers for updating reception status to caller :
  292. (+) Detection of inactivity period (RX line has not been active for a given period).
  293. (++) RX inactivity detected by IDLE event, i.e. RX line has been in idle state (normally high state)
  294. for 1 frame time, after last received byte.
  295. (++) RX inactivity detected by RTO, i.e. line has been in idle state
  296. for a programmable time, after last received byte.
  297. (+) Detection that a specific character has been received.
  298. (#) There are two mode of transfer:
  299. (+) Blocking mode: The reception is performed in polling mode, until either expected number of data is received,
  300. or till IDLE event occurs. Reception is handled only during function execution.
  301. When function exits, no data reception could occur. HAL status and number of actually received data elements,
  302. are returned by function after finishing transfer.
  303. (+) Non-Blocking mode: The reception is performed using Interrupts or DMA.
  304. These API's return the HAL status.
  305. The end of the data processing will be indicated through the
  306. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when using DMA mode.
  307. The HAL_UARTEx_RxEventCallback() user callback will be executed during Receive process
  308. The HAL_UART_ErrorCallback()user callback will be executed when a reception error is detected.
  309. (#) Blocking mode API:
  310. (+) HAL_UARTEx_ReceiveToIdle()
  311. (#) Non-Blocking mode API with Interrupt:
  312. (+) HAL_UARTEx_ReceiveToIdle_IT()
  313. (#) Non-Blocking mode API with DMA:
  314. (+) HAL_UARTEx_ReceiveToIdle_DMA()
  315. @endverbatim
  316. * @{
  317. */
  318. /**
  319. * @brief By default in multiprocessor mode, when the wake up method is set
  320. * to address mark, the UART handles only 4-bit long addresses detection;
  321. * this API allows to enable longer addresses detection (6-, 7- or 8-bit
  322. * long).
  323. * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
  324. * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
  325. * @param huart UART handle.
  326. * @param AddressLength This parameter can be one of the following values:
  327. * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
  328. * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
  329. * @retval HAL status
  330. */
  331. HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
  332. {
  333. /* Check the UART handle allocation */
  334. if (huart == NULL)
  335. {
  336. return HAL_ERROR;
  337. }
  338. /* Check the address length parameter */
  339. assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
  340. huart->gState = HAL_UART_STATE_BUSY;
  341. /* Disable the Peripheral */
  342. __HAL_UART_DISABLE(huart);
  343. /* Set the address length */
  344. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
  345. /* Enable the Peripheral */
  346. __HAL_UART_ENABLE(huart);
  347. /* TEACK and/or REACK to check before moving huart->gState to Ready */
  348. return (UART_CheckIdleState(huart));
  349. }
  350. /**
  351. * @brief Set Wakeup from Stop mode interrupt flag selection.
  352. * @note It is the application responsibility to enable the interrupt used as
  353. * usart_wkup interrupt source before entering low-power mode.
  354. * @param huart UART handle.
  355. * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status.
  356. * This parameter can be one of the following values:
  357. * @arg @ref UART_WAKEUP_ON_ADDRESS
  358. * @arg @ref UART_WAKEUP_ON_STARTBIT
  359. * @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
  360. * @retval HAL status
  361. */
  362. HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  363. {
  364. HAL_StatusTypeDef status = HAL_OK;
  365. uint32_t tickstart;
  366. /* check the wake-up from stop mode UART instance */
  367. assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
  368. /* check the wake-up selection parameter */
  369. assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
  370. /* Process Locked */
  371. __HAL_LOCK(huart);
  372. huart->gState = HAL_UART_STATE_BUSY;
  373. /* Disable the Peripheral */
  374. __HAL_UART_DISABLE(huart);
  375. /* Set the wake-up selection scheme */
  376. MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
  377. if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
  378. {
  379. UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
  380. }
  381. /* Enable the Peripheral */
  382. __HAL_UART_ENABLE(huart);
  383. /* Init tickstart for timeout management */
  384. tickstart = HAL_GetTick();
  385. /* Wait until REACK flag is set */
  386. if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  387. {
  388. status = HAL_TIMEOUT;
  389. }
  390. else
  391. {
  392. /* Initialize the UART State */
  393. huart->gState = HAL_UART_STATE_READY;
  394. }
  395. /* Process Unlocked */
  396. __HAL_UNLOCK(huart);
  397. return status;
  398. }
  399. /**
  400. * @brief Enable UART Stop Mode.
  401. * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE.
  402. * @param huart UART handle.
  403. * @retval HAL status
  404. */
  405. HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
  406. {
  407. /* Process Locked */
  408. __HAL_LOCK(huart);
  409. /* Set UESM bit */
  410. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
  411. /* Process Unlocked */
  412. __HAL_UNLOCK(huart);
  413. return HAL_OK;
  414. }
  415. /**
  416. * @brief Disable UART Stop Mode.
  417. * @param huart UART handle.
  418. * @retval HAL status
  419. */
  420. HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
  421. {
  422. /* Process Locked */
  423. __HAL_LOCK(huart);
  424. /* Clear UESM bit */
  425. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
  426. /* Process Unlocked */
  427. __HAL_UNLOCK(huart);
  428. return HAL_OK;
  429. }
  430. /**
  431. * @brief Enable the FIFO mode.
  432. * @param huart UART handle.
  433. * @retval HAL status
  434. */
  435. HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef *huart)
  436. {
  437. uint32_t tmpcr1;
  438. /* Check parameters */
  439. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  440. /* Process Locked */
  441. __HAL_LOCK(huart);
  442. huart->gState = HAL_UART_STATE_BUSY;
  443. /* Save actual UART configuration */
  444. tmpcr1 = READ_REG(huart->Instance->CR1);
  445. /* Disable UART */
  446. __HAL_UART_DISABLE(huart);
  447. /* Enable FIFO mode */
  448. SET_BIT(tmpcr1, USART_CR1_FIFOEN);
  449. huart->FifoMode = UART_FIFOMODE_ENABLE;
  450. /* Restore UART configuration */
  451. WRITE_REG(huart->Instance->CR1, tmpcr1);
  452. /* Determine the number of data to process during RX/TX ISR execution */
  453. UARTEx_SetNbDataToProcess(huart);
  454. huart->gState = HAL_UART_STATE_READY;
  455. /* Process Unlocked */
  456. __HAL_UNLOCK(huart);
  457. return HAL_OK;
  458. }
  459. /**
  460. * @brief Disable the FIFO mode.
  461. * @param huart UART handle.
  462. * @retval HAL status
  463. */
  464. HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart)
  465. {
  466. uint32_t tmpcr1;
  467. /* Check parameters */
  468. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  469. /* Process Locked */
  470. __HAL_LOCK(huart);
  471. huart->gState = HAL_UART_STATE_BUSY;
  472. /* Save actual UART configuration */
  473. tmpcr1 = READ_REG(huart->Instance->CR1);
  474. /* Disable UART */
  475. __HAL_UART_DISABLE(huart);
  476. /* Enable FIFO mode */
  477. CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
  478. huart->FifoMode = UART_FIFOMODE_DISABLE;
  479. /* Restore UART configuration */
  480. WRITE_REG(huart->Instance->CR1, tmpcr1);
  481. huart->gState = HAL_UART_STATE_READY;
  482. /* Process Unlocked */
  483. __HAL_UNLOCK(huart);
  484. return HAL_OK;
  485. }
  486. /**
  487. * @brief Set the TXFIFO threshold.
  488. * @param huart UART handle.
  489. * @param Threshold TX FIFO threshold value
  490. * This parameter can be one of the following values:
  491. * @arg @ref UART_TXFIFO_THRESHOLD_1_8
  492. * @arg @ref UART_TXFIFO_THRESHOLD_1_4
  493. * @arg @ref UART_TXFIFO_THRESHOLD_1_2
  494. * @arg @ref UART_TXFIFO_THRESHOLD_3_4
  495. * @arg @ref UART_TXFIFO_THRESHOLD_7_8
  496. * @arg @ref UART_TXFIFO_THRESHOLD_8_8
  497. * @retval HAL status
  498. */
  499. HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
  500. {
  501. uint32_t tmpcr1;
  502. /* Check parameters */
  503. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  504. assert_param(IS_UART_TXFIFO_THRESHOLD(Threshold));
  505. /* Process Locked */
  506. __HAL_LOCK(huart);
  507. huart->gState = HAL_UART_STATE_BUSY;
  508. /* Save actual UART configuration */
  509. tmpcr1 = READ_REG(huart->Instance->CR1);
  510. /* Disable UART */
  511. __HAL_UART_DISABLE(huart);
  512. /* Update TX threshold configuration */
  513. MODIFY_REG(huart->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
  514. /* Determine the number of data to process during RX/TX ISR execution */
  515. UARTEx_SetNbDataToProcess(huart);
  516. /* Restore UART configuration */
  517. WRITE_REG(huart->Instance->CR1, tmpcr1);
  518. huart->gState = HAL_UART_STATE_READY;
  519. /* Process Unlocked */
  520. __HAL_UNLOCK(huart);
  521. return HAL_OK;
  522. }
  523. /**
  524. * @brief Set the RXFIFO threshold.
  525. * @param huart UART handle.
  526. * @param Threshold RX FIFO threshold value
  527. * This parameter can be one of the following values:
  528. * @arg @ref UART_RXFIFO_THRESHOLD_1_8
  529. * @arg @ref UART_RXFIFO_THRESHOLD_1_4
  530. * @arg @ref UART_RXFIFO_THRESHOLD_1_2
  531. * @arg @ref UART_RXFIFO_THRESHOLD_3_4
  532. * @arg @ref UART_RXFIFO_THRESHOLD_7_8
  533. * @arg @ref UART_RXFIFO_THRESHOLD_8_8
  534. * @retval HAL status
  535. */
  536. HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
  537. {
  538. uint32_t tmpcr1;
  539. /* Check the parameters */
  540. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  541. assert_param(IS_UART_RXFIFO_THRESHOLD(Threshold));
  542. /* Process Locked */
  543. __HAL_LOCK(huart);
  544. huart->gState = HAL_UART_STATE_BUSY;
  545. /* Save actual UART configuration */
  546. tmpcr1 = READ_REG(huart->Instance->CR1);
  547. /* Disable UART */
  548. __HAL_UART_DISABLE(huart);
  549. /* Update RX threshold configuration */
  550. MODIFY_REG(huart->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
  551. /* Determine the number of data to process during RX/TX ISR execution */
  552. UARTEx_SetNbDataToProcess(huart);
  553. /* Restore UART configuration */
  554. WRITE_REG(huart->Instance->CR1, tmpcr1);
  555. huart->gState = HAL_UART_STATE_READY;
  556. /* Process Unlocked */
  557. __HAL_UNLOCK(huart);
  558. return HAL_OK;
  559. }
  560. /**
  561. * @brief Receive an amount of data in blocking mode till either the expected number of data
  562. * is received or an IDLE event occurs.
  563. * @note HAL_OK is returned if reception is completed (expected number of data has been received)
  564. * or if reception is stopped after IDLE event (less than the expected number of data has been received)
  565. * In this case, RxLen output parameter indicates number of data available in reception buffer.
  566. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  567. * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
  568. * of uint16_t available through pData.
  569. * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
  570. * is not empty. Read operations from the RDR register are performed when
  571. * RXFNE flag is set. From hardware perspective, RXFNE flag and
  572. * RXNE are mapped on the same bit-field.
  573. * @param huart UART handle.
  574. * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
  575. * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
  576. * @param RxLen Number of data elements finally received
  577. * (could be lower than Size, in case reception ends on IDLE event)
  578. * @param Timeout Timeout duration expressed in ms (covers the whole reception sequence).
  579. * @retval HAL status
  580. */
  581. HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen,
  582. uint32_t Timeout)
  583. {
  584. uint8_t *pdata8bits;
  585. uint16_t *pdata16bits;
  586. uint16_t uhMask;
  587. uint32_t tickstart;
  588. /* Check that a Rx process is not already ongoing */
  589. if (huart->RxState == HAL_UART_STATE_READY)
  590. {
  591. if ((pData == NULL) || (Size == 0U))
  592. {
  593. return HAL_ERROR;
  594. }
  595. huart->ErrorCode = HAL_UART_ERROR_NONE;
  596. huart->RxState = HAL_UART_STATE_BUSY_RX;
  597. huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
  598. huart->RxEventType = HAL_UART_RXEVENT_TC;
  599. /* Init tickstart for timeout management */
  600. tickstart = HAL_GetTick();
  601. huart->RxXferSize = Size;
  602. huart->RxXferCount = Size;
  603. /* Computation of UART mask to apply to RDR register */
  604. UART_MASK_COMPUTATION(huart);
  605. uhMask = huart->Mask;
  606. /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  607. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  608. {
  609. pdata8bits = NULL;
  610. pdata16bits = (uint16_t *) pData;
  611. }
  612. else
  613. {
  614. pdata8bits = pData;
  615. pdata16bits = NULL;
  616. }
  617. /* Initialize output number of received elements */
  618. *RxLen = 0U;
  619. /* as long as data have to be received */
  620. while (huart->RxXferCount > 0U)
  621. {
  622. /* Check if IDLE flag is set */
  623. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
  624. {
  625. /* Clear IDLE flag in ISR */
  626. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  627. /* If Set, but no data ever received, clear flag without exiting loop */
  628. /* If Set, and data has already been received, this means Idle Event is valid : End reception */
  629. if (*RxLen > 0U)
  630. {
  631. huart->RxEventType = HAL_UART_RXEVENT_IDLE;
  632. huart->RxState = HAL_UART_STATE_READY;
  633. return HAL_OK;
  634. }
  635. }
  636. /* Check if RXNE flag is set */
  637. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE))
  638. {
  639. if (pdata8bits == NULL)
  640. {
  641. *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
  642. pdata16bits++;
  643. }
  644. else
  645. {
  646. *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  647. pdata8bits++;
  648. }
  649. /* Increment number of received elements */
  650. *RxLen += 1U;
  651. huart->RxXferCount--;
  652. }
  653. /* Check for the Timeout */
  654. if (Timeout != HAL_MAX_DELAY)
  655. {
  656. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  657. {
  658. huart->RxState = HAL_UART_STATE_READY;
  659. return HAL_TIMEOUT;
  660. }
  661. }
  662. }
  663. /* Set number of received elements in output parameter : RxLen */
  664. *RxLen = huart->RxXferSize - huart->RxXferCount;
  665. /* At end of Rx process, restore huart->RxState to Ready */
  666. huart->RxState = HAL_UART_STATE_READY;
  667. return HAL_OK;
  668. }
  669. else
  670. {
  671. return HAL_BUSY;
  672. }
  673. }
  674. /**
  675. * @brief Receive an amount of data in interrupt mode till either the expected number of data
  676. * is received or an IDLE event occurs.
  677. * @note Reception is initiated by this function call. Further progress of reception is achieved thanks
  678. * to UART interrupts raised by RXNE and IDLE events. Callback is called at end of reception indicating
  679. * number of received data elements.
  680. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  681. * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
  682. * of uint16_t available through pData.
  683. * @param huart UART handle.
  684. * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
  685. * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
  686. * @retval HAL status
  687. */
  688. HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  689. {
  690. HAL_StatusTypeDef status = HAL_OK;
  691. /* Check that a Rx process is not already ongoing */
  692. if (huart->RxState == HAL_UART_STATE_READY)
  693. {
  694. if ((pData == NULL) || (Size == 0U))
  695. {
  696. return HAL_ERROR;
  697. }
  698. /* Set Reception type to reception till IDLE Event*/
  699. huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
  700. huart->RxEventType = HAL_UART_RXEVENT_TC;
  701. (void)UART_Start_Receive_IT(huart, pData, Size);
  702. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  703. {
  704. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  705. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  706. }
  707. else
  708. {
  709. /* In case of errors already pending when reception is started,
  710. Interrupts may have already been raised and lead to reception abortion.
  711. (Overrun error for instance).
  712. In such case Reception Type has been reset to HAL_UART_RECEPTION_STANDARD. */
  713. status = HAL_ERROR;
  714. }
  715. return status;
  716. }
  717. else
  718. {
  719. return HAL_BUSY;
  720. }
  721. }
  722. /**
  723. * @brief Receive an amount of data in DMA mode till either the expected number
  724. * of data is received or an IDLE event occurs.
  725. * @note Reception is initiated by this function call. Further progress of reception is achieved thanks
  726. * to DMA services, transferring automatically received data elements in user reception buffer and
  727. * calling registered callbacks at half/end of reception. UART IDLE events are also used to consider
  728. * reception phase as ended. In all cases, callback execution will indicate number of received data elements.
  729. * @note When the UART parity is enabled (PCE = 1), the received data contain
  730. * the parity bit (MSB position).
  731. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  732. * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
  733. * of uint16_t available through pData.
  734. * @param huart UART handle.
  735. * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
  736. * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
  737. * @retval HAL status
  738. */
  739. HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  740. {
  741. HAL_StatusTypeDef status;
  742. /* Check that a Rx process is not already ongoing */
  743. if (huart->RxState == HAL_UART_STATE_READY)
  744. {
  745. if ((pData == NULL) || (Size == 0U))
  746. {
  747. return HAL_ERROR;
  748. }
  749. /* Set Reception type to reception till IDLE Event*/
  750. huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
  751. huart->RxEventType = HAL_UART_RXEVENT_TC;
  752. status = UART_Start_Receive_DMA(huart, pData, Size);
  753. /* Check Rx process has been successfully started */
  754. if (status == HAL_OK)
  755. {
  756. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  757. {
  758. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  759. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  760. }
  761. else
  762. {
  763. /* In case of errors already pending when reception is started,
  764. Interrupts may have already been raised and lead to reception abortion.
  765. (Overrun error for instance).
  766. In such case Reception Type has been reset to HAL_UART_RECEPTION_STANDARD. */
  767. status = HAL_ERROR;
  768. }
  769. }
  770. return status;
  771. }
  772. else
  773. {
  774. return HAL_BUSY;
  775. }
  776. }
  777. /**
  778. * @brief Provide Rx Event type that has lead to RxEvent callback execution.
  779. * @note When HAL_UARTEx_ReceiveToIdle_IT() or HAL_UARTEx_ReceiveToIdle_DMA() API are called, progress
  780. * of reception process is provided to application through calls of Rx Event callback (either default one
  781. * HAL_UARTEx_RxEventCallback() or user registered one). As several types of events could occur (IDLE event,
  782. * Half Transfer, or Transfer Complete), this function allows to retrieve the Rx Event type that has lead
  783. * to Rx Event callback execution.
  784. * @note This function is expected to be called within the user implementation of Rx Event Callback,
  785. * in order to provide the accurate value :
  786. * In Interrupt Mode :
  787. * - HAL_UART_RXEVENT_TC : when Reception has been completed (expected nb of data has been received)
  788. * - HAL_UART_RXEVENT_IDLE : when Idle event occurred prior reception has been completed (nb of
  789. * received data is lower than expected one)
  790. * In DMA Mode :
  791. * - HAL_UART_RXEVENT_TC : when Reception has been completed (expected nb of data has been received)
  792. * - HAL_UART_RXEVENT_HT : when half of expected nb of data has been received
  793. * - HAL_UART_RXEVENT_IDLE : when Idle event occurred prior reception has been completed (nb of
  794. * received data is lower than expected one).
  795. * In DMA mode, RxEvent callback could be called several times;
  796. * When DMA is configured in Normal Mode, HT event does not stop Reception process;
  797. * When DMA is configured in Circular Mode, HT, TC or IDLE events don't stop Reception process;
  798. * @param huart UART handle.
  799. * @retval Rx Event Type (return vale will be a value of @ref UART_RxEvent_Type_Values)
  800. */
  801. HAL_UART_RxEventTypeTypeDef HAL_UARTEx_GetRxEventType(const UART_HandleTypeDef *huart)
  802. {
  803. /* Return Rx Event type value, as stored in UART handle */
  804. return (huart->RxEventType);
  805. }
  806. /**
  807. * @}
  808. */
  809. /**
  810. * @}
  811. */
  812. /** @addtogroup UARTEx_Private_Functions
  813. * @{
  814. */
  815. /**
  816. * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
  817. * @param huart UART handle.
  818. * @param WakeUpSelection UART wake up from stop mode parameters.
  819. * @retval None
  820. */
  821. static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  822. {
  823. assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
  824. /* Set the USART address length */
  825. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
  826. /* Set the USART address node */
  827. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
  828. }
  829. /**
  830. * @brief Calculate the number of data to process in RX/TX ISR.
  831. * @note The RX FIFO depth and the TX FIFO depth is extracted from
  832. * the UART configuration registers.
  833. * @param huart UART handle.
  834. * @retval None
  835. */
  836. static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart)
  837. {
  838. uint8_t rx_fifo_depth;
  839. uint8_t tx_fifo_depth;
  840. uint8_t rx_fifo_threshold;
  841. uint8_t tx_fifo_threshold;
  842. static const uint8_t numerator[] = {1U, 1U, 1U, 3U, 7U, 1U, 0U, 0U};
  843. static const uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U, 1U, 1U};
  844. if (huart->FifoMode == UART_FIFOMODE_DISABLE)
  845. {
  846. huart->NbTxDataToProcess = 1U;
  847. huart->NbRxDataToProcess = 1U;
  848. }
  849. else
  850. {
  851. rx_fifo_depth = RX_FIFO_DEPTH;
  852. tx_fifo_depth = TX_FIFO_DEPTH;
  853. rx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
  854. tx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
  855. huart->NbTxDataToProcess = ((uint16_t)tx_fifo_depth * numerator[tx_fifo_threshold]) /
  856. (uint16_t)denominator[tx_fifo_threshold];
  857. huart->NbRxDataToProcess = ((uint16_t)rx_fifo_depth * numerator[rx_fifo_threshold]) /
  858. (uint16_t)denominator[rx_fifo_threshold];
  859. }
  860. }
  861. /**
  862. * @}
  863. */
  864. #endif /* HAL_UART_MODULE_ENABLED */
  865. /**
  866. * @}
  867. */
  868. /**
  869. * @}
  870. */