stm32f7xx_hal_uart_ex.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_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. @endverbatim
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f7xx_hal.h"
  35. /** @addtogroup STM32F7xx_HAL_Driver
  36. * @{
  37. */
  38. /** @defgroup UARTEx UARTEx
  39. * @brief UART Extended HAL module driver
  40. * @{
  41. */
  42. #ifdef HAL_UART_MODULE_ENABLED
  43. /* Private typedef -----------------------------------------------------------*/
  44. /* Private define ------------------------------------------------------------*/
  45. /* Private macros ------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private function prototypes -----------------------------------------------*/
  48. /** @defgroup UARTEx_Private_Functions UARTEx Private Functions
  49. * @{
  50. */
  51. #if defined(USART_CR1_UESM)
  52. static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
  53. #endif /* USART_CR1_UESM */
  54. /**
  55. * @}
  56. */
  57. /* Exported functions --------------------------------------------------------*/
  58. /** @defgroup UARTEx_Exported_Functions UARTEx Exported Functions
  59. * @{
  60. */
  61. /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions
  62. * @brief Extended Initialization and Configuration Functions
  63. *
  64. @verbatim
  65. ===============================================================================
  66. ##### Initialization and Configuration functions #####
  67. ===============================================================================
  68. [..]
  69. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  70. in asynchronous mode.
  71. (+) For the asynchronous mode the parameters below can be configured:
  72. (++) Baud Rate
  73. (++) Word Length
  74. (++) Stop Bit
  75. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  76. in the data register is transmitted but is changed by the parity bit.
  77. (++) Hardware flow control
  78. (++) Receiver/transmitter modes
  79. (++) Over Sampling Method
  80. (++) One-Bit Sampling Method
  81. (+) For the asynchronous mode, the following advanced features can be configured as well:
  82. (++) TX and/or RX pin level inversion
  83. (++) data logical level inversion
  84. (++) RX and TX pins swap
  85. (++) RX overrun detection disabling
  86. (++) DMA disabling on RX error
  87. (++) MSB first on communication line
  88. (++) auto Baud rate detection
  89. [..]
  90. The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration
  91. procedures (details for the procedures are available in reference manual).
  92. @endverbatim
  93. Depending on the frame length defined by the M1 and M0 bits (7-bit,
  94. 8-bit or 9-bit), the possible UART formats are listed in the
  95. following table.
  96. Table 1. UART frame format.
  97. +-----------------------------------------------------------------------+
  98. | M1 bit | M0 bit | PCE bit | UART frame |
  99. |---------|---------|-----------|---------------------------------------|
  100. | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  101. |---------|---------|-----------|---------------------------------------|
  102. | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  103. |---------|---------|-----------|---------------------------------------|
  104. | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  105. |---------|---------|-----------|---------------------------------------|
  106. | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  107. |---------|---------|-----------|---------------------------------------|
  108. | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  109. |---------|---------|-----------|---------------------------------------|
  110. | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  111. +-----------------------------------------------------------------------+
  112. * @{
  113. */
  114. /**
  115. * @brief Initialize the RS485 Driver enable feature according to the specified
  116. * parameters in the UART_InitTypeDef and creates the associated handle.
  117. * @param huart UART handle.
  118. * @param Polarity Select the driver enable polarity.
  119. * This parameter can be one of the following values:
  120. * @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
  121. * @arg @ref UART_DE_POLARITY_LOW DE signal is active low
  122. * @param AssertionTime Driver Enable assertion time:
  123. * 5-bit value defining the time between the activation of the DE (Driver Enable)
  124. * signal and the beginning of the start bit. It is expressed in sample time
  125. * units (1/8 or 1/16 bit time, depending on the oversampling rate)
  126. * @param DeassertionTime Driver Enable deassertion time:
  127. * 5-bit value defining the time between the end of the last stop bit, in a
  128. * transmitted message, and the de-activation of the DE (Driver Enable) signal.
  129. * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
  130. * oversampling rate).
  131. * @retval HAL status
  132. */
  133. HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
  134. uint32_t DeassertionTime)
  135. {
  136. uint32_t temp;
  137. /* Check the UART handle allocation */
  138. if (huart == NULL)
  139. {
  140. return HAL_ERROR;
  141. }
  142. /* Check the Driver Enable UART instance */
  143. assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
  144. /* Check the Driver Enable polarity */
  145. assert_param(IS_UART_DE_POLARITY(Polarity));
  146. /* Check the Driver Enable assertion time */
  147. assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
  148. /* Check the Driver Enable deassertion time */
  149. assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
  150. if (huart->gState == HAL_UART_STATE_RESET)
  151. {
  152. /* Allocate lock resource and initialize it */
  153. huart->Lock = HAL_UNLOCKED;
  154. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  155. UART_InitCallbacksToDefault(huart);
  156. if (huart->MspInitCallback == NULL)
  157. {
  158. huart->MspInitCallback = HAL_UART_MspInit;
  159. }
  160. /* Init the low level hardware */
  161. huart->MspInitCallback(huart);
  162. #else
  163. /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  164. HAL_UART_MspInit(huart);
  165. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  166. }
  167. huart->gState = HAL_UART_STATE_BUSY;
  168. /* Disable the Peripheral */
  169. __HAL_UART_DISABLE(huart);
  170. /* Set the UART Communication parameters */
  171. if (UART_SetConfig(huart) == HAL_ERROR)
  172. {
  173. return HAL_ERROR;
  174. }
  175. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  176. {
  177. UART_AdvFeatureConfig(huart);
  178. }
  179. /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
  180. SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
  181. /* Set the Driver Enable polarity */
  182. MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
  183. /* Set the Driver Enable assertion and deassertion times */
  184. temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
  185. temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
  186. MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);
  187. /* Enable the Peripheral */
  188. __HAL_UART_ENABLE(huart);
  189. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  190. return (UART_CheckIdleState(huart));
  191. }
  192. /**
  193. * @}
  194. */
  195. /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions
  196. * @brief Extended Peripheral Control functions
  197. *
  198. @verbatim
  199. ===============================================================================
  200. ##### Peripheral Control functions #####
  201. ===============================================================================
  202. [..] This section provides the following functions:
  203. (+) HAL_UARTEx_EnableClockStopMode() API enables the UART clock (HSI or LSE only) during stop mode
  204. (+) HAL_UARTEx_DisableClockStopMode() API disables the above functionality
  205. (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
  206. detection length to more than 4 bits for multiprocessor address mark wake up.
  207. #if defined(USART_CR1_UESM)
  208. (+) HAL_UARTEx_StopModeWakeUpSourceConfig() API defines the wake-up from stop mode
  209. trigger: address match, Start Bit detection or RXNE bit status.
  210. (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode
  211. (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
  212. #endif
  213. [..] This subsection also provides a set of additional functions providing enhanced reception
  214. services to user. (For example, these functions allow application to handle use cases
  215. where number of data to be received is unknown).
  216. (#) Compared to standard reception services which only consider number of received
  217. data elements as reception completion criteria, these functions also consider additional events
  218. as triggers for updating reception status to caller :
  219. (+) Detection of inactivity period (RX line has not been active for a given period).
  220. (++) RX inactivity detected by IDLE event, i.e. RX line has been in idle state (normally high state)
  221. for 1 frame time, after last received byte.
  222. (++) RX inactivity detected by RTO, i.e. line has been in idle state
  223. for a programmable time, after last received byte.
  224. (+) Detection that a specific character has been received.
  225. (#) There are two mode of transfer:
  226. (+) Blocking mode: The reception is performed in polling mode, until either expected number of data is received,
  227. or till IDLE event occurs. Reception is handled only during function execution.
  228. When function exits, no data reception could occur. HAL status and number of actually received data elements,
  229. are returned by function after finishing transfer.
  230. (+) Non-Blocking mode: The reception is performed using Interrupts or DMA.
  231. These API's return the HAL status.
  232. The end of the data processing will be indicated through the
  233. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when using DMA mode.
  234. The HAL_UARTEx_RxEventCallback() user callback will be executed during Receive process
  235. The HAL_UART_ErrorCallback()user callback will be executed when a reception error is detected.
  236. (#) Blocking mode API:
  237. (+) HAL_UARTEx_ReceiveToIdle()
  238. (#) Non-Blocking mode API with Interrupt:
  239. (+) HAL_UARTEx_ReceiveToIdle_IT()
  240. (#) Non-Blocking mode API with DMA:
  241. (+) HAL_UARTEx_ReceiveToIdle_DMA()
  242. @endverbatim
  243. * @{
  244. */
  245. #if defined(USART_CR3_UCESM)
  246. /**
  247. * @brief Keep UART Clock enabled when in Stop Mode.
  248. * @note When the USART clock source is configured to be LSE or HSI, it is possible to keep enabled
  249. * this clock during STOP mode by setting the UCESM bit in USART_CR3 control register.
  250. * @note When LPUART is used to wakeup from stop with LSE is selected as LPUART clock source,
  251. * and desired baud rate is 9600 baud, the bit UCESM bit in LPUART_CR3 control register must be set.
  252. * @param huart UART handle.
  253. * @retval HAL status
  254. */
  255. HAL_StatusTypeDef HAL_UARTEx_EnableClockStopMode(UART_HandleTypeDef *huart)
  256. {
  257. /* Process Locked */
  258. __HAL_LOCK(huart);
  259. /* Set UCESM bit */
  260. ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_UCESM);
  261. /* Process Unlocked */
  262. __HAL_UNLOCK(huart);
  263. return HAL_OK;
  264. }
  265. /**
  266. * @brief Disable UART Clock when in Stop Mode.
  267. * @param huart UART handle.
  268. * @retval HAL status
  269. */
  270. HAL_StatusTypeDef HAL_UARTEx_DisableClockStopMode(UART_HandleTypeDef *huart)
  271. {
  272. /* Process Locked */
  273. __HAL_LOCK(huart);
  274. /* Clear UCESM bit */
  275. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_UCESM);
  276. /* Process Unlocked */
  277. __HAL_UNLOCK(huart);
  278. return HAL_OK;
  279. }
  280. #endif /* USART_CR3_UCESM */
  281. /**
  282. * @brief By default in multiprocessor mode, when the wake up method is set
  283. * to address mark, the UART handles only 4-bit long addresses detection;
  284. * this API allows to enable longer addresses detection (6-, 7- or 8-bit
  285. * long).
  286. * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
  287. * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
  288. * @param huart UART handle.
  289. * @param AddressLength This parameter can be one of the following values:
  290. * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
  291. * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
  292. * @retval HAL status
  293. */
  294. HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
  295. {
  296. /* Check the UART handle allocation */
  297. if (huart == NULL)
  298. {
  299. return HAL_ERROR;
  300. }
  301. /* Check the address length parameter */
  302. assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
  303. huart->gState = HAL_UART_STATE_BUSY;
  304. /* Disable the Peripheral */
  305. __HAL_UART_DISABLE(huart);
  306. /* Set the address length */
  307. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
  308. /* Enable the Peripheral */
  309. __HAL_UART_ENABLE(huart);
  310. /* TEACK and/or REACK to check before moving huart->gState to Ready */
  311. return (UART_CheckIdleState(huart));
  312. }
  313. #if defined(USART_CR1_UESM)
  314. /**
  315. * @brief Set Wakeup from Stop mode interrupt flag selection.
  316. * @note It is the application responsibility to enable the interrupt used as
  317. * usart_wkup interrupt source before entering low-power mode.
  318. * @param huart UART handle.
  319. * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status.
  320. * This parameter can be one of the following values:
  321. * @arg @ref UART_WAKEUP_ON_ADDRESS
  322. * @arg @ref UART_WAKEUP_ON_STARTBIT
  323. * @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
  324. * @retval HAL status
  325. */
  326. HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  327. {
  328. HAL_StatusTypeDef status = HAL_OK;
  329. uint32_t tickstart;
  330. /* check the wake-up from stop mode UART instance */
  331. assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
  332. /* check the wake-up selection parameter */
  333. assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
  334. /* Process Locked */
  335. __HAL_LOCK(huart);
  336. huart->gState = HAL_UART_STATE_BUSY;
  337. /* Disable the Peripheral */
  338. __HAL_UART_DISABLE(huart);
  339. #if defined(USART_CR3_WUS)
  340. /* Set the wake-up selection scheme */
  341. MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
  342. #endif /* USART_CR3_WUS */
  343. if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
  344. {
  345. UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
  346. }
  347. /* Enable the Peripheral */
  348. __HAL_UART_ENABLE(huart);
  349. /* Init tickstart for timeout management */
  350. tickstart = HAL_GetTick();
  351. /* Wait until REACK flag is set */
  352. if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  353. {
  354. status = HAL_TIMEOUT;
  355. }
  356. else
  357. {
  358. /* Initialize the UART State */
  359. huart->gState = HAL_UART_STATE_READY;
  360. }
  361. /* Process Unlocked */
  362. __HAL_UNLOCK(huart);
  363. return status;
  364. }
  365. /**
  366. * @brief Enable UART Stop Mode.
  367. * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE.
  368. * @param huart UART handle.
  369. * @retval HAL status
  370. */
  371. HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
  372. {
  373. /* Process Locked */
  374. __HAL_LOCK(huart);
  375. /* Set UESM bit */
  376. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
  377. /* Process Unlocked */
  378. __HAL_UNLOCK(huart);
  379. return HAL_OK;
  380. }
  381. /**
  382. * @brief Disable UART Stop Mode.
  383. * @param huart UART handle.
  384. * @retval HAL status
  385. */
  386. HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
  387. {
  388. /* Process Locked */
  389. __HAL_LOCK(huart);
  390. /* Clear UESM bit */
  391. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
  392. /* Process Unlocked */
  393. __HAL_UNLOCK(huart);
  394. return HAL_OK;
  395. }
  396. #endif /* USART_CR1_UESM */
  397. /**
  398. * @brief Receive an amount of data in blocking mode till either the expected number of data
  399. * is received or an IDLE event occurs.
  400. * @note HAL_OK is returned if reception is completed (expected number of data has been received)
  401. * or if reception is stopped after IDLE event (less than the expected number of data has been received)
  402. * In this case, RxLen output parameter indicates number of data available in reception buffer.
  403. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  404. * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
  405. * of uint16_t available through pData.
  406. * @param huart UART handle.
  407. * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
  408. * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
  409. * @param RxLen Number of data elements finally received
  410. * (could be lower than Size, in case reception ends on IDLE event)
  411. * @param Timeout Timeout duration expressed in ms (covers the whole reception sequence).
  412. * @retval HAL status
  413. */
  414. HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen,
  415. uint32_t Timeout)
  416. {
  417. uint8_t *pdata8bits;
  418. uint16_t *pdata16bits;
  419. uint16_t uhMask;
  420. uint32_t tickstart;
  421. /* Check that a Rx process is not already ongoing */
  422. if (huart->RxState == HAL_UART_STATE_READY)
  423. {
  424. if ((pData == NULL) || (Size == 0U))
  425. {
  426. return HAL_ERROR;
  427. }
  428. huart->ErrorCode = HAL_UART_ERROR_NONE;
  429. huart->RxState = HAL_UART_STATE_BUSY_RX;
  430. huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
  431. huart->RxEventType = HAL_UART_RXEVENT_TC;
  432. /* Init tickstart for timeout management */
  433. tickstart = HAL_GetTick();
  434. huart->RxXferSize = Size;
  435. huart->RxXferCount = Size;
  436. /* Computation of UART mask to apply to RDR register */
  437. UART_MASK_COMPUTATION(huart);
  438. uhMask = huart->Mask;
  439. /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  440. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  441. {
  442. pdata8bits = NULL;
  443. pdata16bits = (uint16_t *) pData;
  444. }
  445. else
  446. {
  447. pdata8bits = pData;
  448. pdata16bits = NULL;
  449. }
  450. /* Initialize output number of received elements */
  451. *RxLen = 0U;
  452. /* as long as data have to be received */
  453. while (huart->RxXferCount > 0U)
  454. {
  455. /* Check if IDLE flag is set */
  456. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
  457. {
  458. /* Clear IDLE flag in ISR */
  459. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  460. /* If Set, but no data ever received, clear flag without exiting loop */
  461. /* If Set, and data has already been received, this means Idle Event is valid : End reception */
  462. if (*RxLen > 0U)
  463. {
  464. huart->RxEventType = HAL_UART_RXEVENT_IDLE;
  465. huart->RxState = HAL_UART_STATE_READY;
  466. return HAL_OK;
  467. }
  468. }
  469. /* Check if RXNE flag is set */
  470. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE))
  471. {
  472. if (pdata8bits == NULL)
  473. {
  474. *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
  475. pdata16bits++;
  476. }
  477. else
  478. {
  479. *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  480. pdata8bits++;
  481. }
  482. /* Increment number of received elements */
  483. *RxLen += 1U;
  484. huart->RxXferCount--;
  485. }
  486. /* Check for the Timeout */
  487. if (Timeout != HAL_MAX_DELAY)
  488. {
  489. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  490. {
  491. huart->RxState = HAL_UART_STATE_READY;
  492. return HAL_TIMEOUT;
  493. }
  494. }
  495. }
  496. /* Set number of received elements in output parameter : RxLen */
  497. *RxLen = huart->RxXferSize - huart->RxXferCount;
  498. /* At end of Rx process, restore huart->RxState to Ready */
  499. huart->RxState = HAL_UART_STATE_READY;
  500. return HAL_OK;
  501. }
  502. else
  503. {
  504. return HAL_BUSY;
  505. }
  506. }
  507. /**
  508. * @brief Receive an amount of data in interrupt mode till either the expected number of data
  509. * is received or an IDLE event occurs.
  510. * @note Reception is initiated by this function call. Further progress of reception is achieved thanks
  511. * to UART interrupts raised by RXNE and IDLE events. Callback is called at end of reception indicating
  512. * number of received data elements.
  513. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  514. * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
  515. * of uint16_t available through pData.
  516. * @param huart UART handle.
  517. * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
  518. * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
  519. * @retval HAL status
  520. */
  521. HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  522. {
  523. HAL_StatusTypeDef status;
  524. /* Check that a Rx process is not already ongoing */
  525. if (huart->RxState == HAL_UART_STATE_READY)
  526. {
  527. if ((pData == NULL) || (Size == 0U))
  528. {
  529. return HAL_ERROR;
  530. }
  531. /* Set Reception type to reception till IDLE Event*/
  532. huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
  533. huart->RxEventType = HAL_UART_RXEVENT_TC;
  534. status = UART_Start_Receive_IT(huart, pData, Size);
  535. /* Check Rx process has been successfully started */
  536. if (status == HAL_OK)
  537. {
  538. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  539. {
  540. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  541. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  542. }
  543. else
  544. {
  545. /* In case of errors already pending when reception is started,
  546. Interrupts may have already been raised and lead to reception abortion.
  547. (Overrun error for instance).
  548. In such case Reception Type has been reset to HAL_UART_RECEPTION_STANDARD. */
  549. status = HAL_ERROR;
  550. }
  551. }
  552. return status;
  553. }
  554. else
  555. {
  556. return HAL_BUSY;
  557. }
  558. }
  559. /**
  560. * @brief Receive an amount of data in DMA mode till either the expected number
  561. * of data is received or an IDLE event occurs.
  562. * @note Reception is initiated by this function call. Further progress of reception is achieved thanks
  563. * to DMA services, transferring automatically received data elements in user reception buffer and
  564. * calling registered callbacks at half/end of reception. UART IDLE events are also used to consider
  565. * reception phase as ended. In all cases, callback execution will indicate number of received data elements.
  566. * @note When the UART parity is enabled (PCE = 1), the received data contain
  567. * the parity bit (MSB position).
  568. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  569. * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
  570. * of uint16_t available through pData.
  571. * @param huart UART handle.
  572. * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
  573. * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
  574. * @retval HAL status
  575. */
  576. HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  577. {
  578. HAL_StatusTypeDef status;
  579. /* Check that a Rx process is not already ongoing */
  580. if (huart->RxState == HAL_UART_STATE_READY)
  581. {
  582. if ((pData == NULL) || (Size == 0U))
  583. {
  584. return HAL_ERROR;
  585. }
  586. /* Set Reception type to reception till IDLE Event*/
  587. huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
  588. huart->RxEventType = HAL_UART_RXEVENT_TC;
  589. status = UART_Start_Receive_DMA(huart, pData, Size);
  590. /* Check Rx process has been successfully started */
  591. if (status == HAL_OK)
  592. {
  593. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  594. {
  595. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  596. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  597. }
  598. else
  599. {
  600. /* In case of errors already pending when reception is started,
  601. Interrupts may have already been raised and lead to reception abortion.
  602. (Overrun error for instance).
  603. In such case Reception Type has been reset to HAL_UART_RECEPTION_STANDARD. */
  604. status = HAL_ERROR;
  605. }
  606. }
  607. return status;
  608. }
  609. else
  610. {
  611. return HAL_BUSY;
  612. }
  613. }
  614. /**
  615. * @brief Provide Rx Event type that has lead to RxEvent callback execution.
  616. * @note When HAL_UARTEx_ReceiveToIdle_IT() or HAL_UARTEx_ReceiveToIdle_DMA() API are called, progress
  617. * of reception process is provided to application through calls of Rx Event callback (either default one
  618. * HAL_UARTEx_RxEventCallback() or user registered one). As several types of events could occur (IDLE event,
  619. * Half Transfer, or Transfer Complete), this function allows to retrieve the Rx Event type that has lead
  620. * to Rx Event callback execution.
  621. * @note This function is expected to be called within the user implementation of Rx Event Callback,
  622. * in order to provide the accurate value :
  623. * In Interrupt Mode :
  624. * - HAL_UART_RXEVENT_TC : when Reception has been completed (expected nb of data has been received)
  625. * - HAL_UART_RXEVENT_IDLE : when Idle event occurred prior reception has been completed (nb of
  626. * received data is lower than expected one)
  627. * In DMA Mode :
  628. * - HAL_UART_RXEVENT_TC : when Reception has been completed (expected nb of data has been received)
  629. * - HAL_UART_RXEVENT_HT : when half of expected nb of data has been received
  630. * - HAL_UART_RXEVENT_IDLE : when Idle event occurred prior reception has been completed (nb of
  631. * received data is lower than expected one).
  632. * In DMA mode, RxEvent callback could be called several times;
  633. * When DMA is configured in Normal Mode, HT event does not stop Reception process;
  634. * When DMA is configured in Circular Mode, HT, TC or IDLE events don't stop Reception process;
  635. * @param huart UART handle.
  636. * @retval Rx Event Type (return vale will be a value of @ref UART_RxEvent_Type_Values)
  637. */
  638. HAL_UART_RxEventTypeTypeDef HAL_UARTEx_GetRxEventType(UART_HandleTypeDef *huart)
  639. {
  640. /* Return Rx Event type value, as stored in UART handle */
  641. return(huart->RxEventType);
  642. }
  643. /**
  644. * @}
  645. */
  646. /**
  647. * @}
  648. */
  649. /** @addtogroup UARTEx_Private_Functions
  650. * @{
  651. */
  652. #if defined(USART_CR1_UESM)
  653. /**
  654. * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
  655. * @param huart UART handle.
  656. * @param WakeUpSelection UART wake up from stop mode parameters.
  657. * @retval None
  658. */
  659. static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  660. {
  661. assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
  662. /* Set the USART address length */
  663. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
  664. /* Set the USART address node */
  665. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
  666. }
  667. #endif /* USART_CR1_UESM */
  668. /**
  669. * @}
  670. */
  671. #endif /* HAL_UART_MODULE_ENABLED */
  672. /**
  673. * @}
  674. */
  675. /**
  676. * @}
  677. */