stm32f0xx_hal_uart_ex.c 31 KB

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