stm32l0xx_hal_uart_ex.c 33 KB

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