You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

4116 lines
136 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_uart.c
  4. * @author MCD Application Team
  5. * @brief UART HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. *
  12. *
  13. @verbatim
  14. ===============================================================================
  15. ##### How to use this driver #####
  16. ===============================================================================
  17. [..]
  18. The UART HAL driver can be used as follows:
  19. (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
  20. (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
  21. (++) Enable the USARTx interface clock.
  22. (++) UART pins configuration:
  23. (+++) Enable the clock for the UART GPIOs.
  24. (+++) Configure these UART pins as alternate function pull-up.
  25. (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
  26. and HAL_UART_Receive_IT() APIs):
  27. (+++) Configure the USARTx interrupt priority.
  28. (+++) Enable the NVIC USART IRQ handle.
  29. (++) UART interrupts handling:
  30. -@@- The specific UART interrupts (Transmission complete interrupt,
  31. RXNE interrupt, RX/TX FIFOs related interrupts and Error Interrupts)
  32. are managed using the macros __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT()
  33. inside the transmit and receive processes.
  34. (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
  35. and HAL_UART_Receive_DMA() APIs):
  36. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  37. (+++) Enable the DMAx interface clock.
  38. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  39. (+++) Configure the DMA Tx/Rx channel.
  40. (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
  41. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  42. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Prescaler value , Hardware
  43. flow control and Mode (Receiver/Transmitter) in the huart handle Init structure.
  44. (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
  45. in the huart handle AdvancedInit structure.
  46. (#) For the UART asynchronous mode, initialize the UART registers by calling
  47. the HAL_UART_Init() API.
  48. (#) For the UART Half duplex mode, initialize the UART registers by calling
  49. the HAL_HalfDuplex_Init() API.
  50. (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers
  51. by calling the HAL_LIN_Init() API.
  52. (#) For the UART Multiprocessor mode, initialize the UART registers
  53. by calling the HAL_MultiProcessor_Init() API.
  54. (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
  55. by calling the HAL_RS485Ex_Init() API.
  56. [..]
  57. (@) These API's (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(),
  58. also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by
  59. calling the customized HAL_UART_MspInit() API.
  60. ##### Callback registration #####
  61. ==================================
  62. [..]
  63. The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
  64. allows the user to configure dynamically the driver callbacks.
  65. [..]
  66. Use Function @ref HAL_UART_RegisterCallback() to register a user callback.
  67. Function @ref HAL_UART_RegisterCallback() allows to register following callbacks:
  68. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  69. (+) TxCpltCallback : Tx Complete Callback.
  70. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  71. (+) RxCpltCallback : Rx Complete Callback.
  72. (+) ErrorCallback : Error Callback.
  73. (+) AbortCpltCallback : Abort Complete Callback.
  74. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  75. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  76. (+) WakeupCallback : Wakeup Callback.
  77. (+) RxFifoFullCallback : Rx Fifo Full Callback.
  78. (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
  79. (+) MspInitCallback : UART MspInit.
  80. (+) MspDeInitCallback : UART MspDeInit.
  81. This function takes as parameters the HAL peripheral handle, the Callback ID
  82. and a pointer to the user callback function.
  83. [..]
  84. Use function @ref HAL_UART_UnRegisterCallback() to reset a callback to the default
  85. weak (surcharged) function.
  86. @ref HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  87. and the Callback ID.
  88. This function allows to reset following callbacks:
  89. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  90. (+) TxCpltCallback : Tx Complete Callback.
  91. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  92. (+) RxCpltCallback : Rx Complete Callback.
  93. (+) ErrorCallback : Error Callback.
  94. (+) AbortCpltCallback : Abort Complete Callback.
  95. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  96. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  97. (+) WakeupCallback : Wakeup Callback.
  98. (+) RxFifoFullCallback : Rx Fifo Full Callback.
  99. (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
  100. (+) MspInitCallback : UART MspInit.
  101. (+) MspDeInitCallback : UART MspDeInit.
  102. [..]
  103. By default, after the @ref HAL_UART_Init() and when the state is HAL_UART_STATE_RESET
  104. all callbacks are set to the corresponding weak (surcharged) functions:
  105. examples @ref HAL_UART_TxCpltCallback(), @ref HAL_UART_RxHalfCpltCallback().
  106. Exception done for MspInit and MspDeInit functions that are respectively
  107. reset to the legacy weak (surcharged) functions in the @ref HAL_UART_Init()
  108. and @ref HAL_UART_DeInit() only when these callbacks are null (not registered beforehand).
  109. If not, MspInit or MspDeInit are not null, the @ref HAL_UART_Init() and @ref HAL_UART_DeInit()
  110. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  111. [..]
  112. Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only.
  113. Exception done MspInit/MspDeInit that can be registered/unregistered
  114. in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user)
  115. MspInit/DeInit callbacks can be used during the Init/DeInit.
  116. In that case first register the MspInit/MspDeInit user callbacks
  117. using @ref HAL_UART_RegisterCallback() before calling @ref HAL_UART_DeInit()
  118. or @ref HAL_UART_Init() function.
  119. [..]
  120. When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or
  121. not defined, the callback registration feature is not available
  122. and weak (surcharged) callbacks are used.
  123. @endverbatim
  124. ******************************************************************************
  125. * @attention
  126. *
  127. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  128. * All rights reserved.</center></h2>
  129. *
  130. * This software component is licensed by ST under BSD 3-Clause license,
  131. * the "License"; You may not use this file except in compliance with the
  132. * License. You may obtain a copy of the License at:
  133. * opensource.org/licenses/BSD-3-Clause
  134. *
  135. ******************************************************************************
  136. */
  137. /* Includes ------------------------------------------------------------------*/
  138. #include "stm32h7xx_hal.h"
  139. /** @addtogroup STM32H7xx_HAL_Driver
  140. * @{
  141. */
  142. /** @defgroup UART UART
  143. * @brief HAL UART module driver
  144. * @{
  145. */
  146. #ifdef HAL_UART_MODULE_ENABLED
  147. /* Private typedef -----------------------------------------------------------*/
  148. /* Private define ------------------------------------------------------------*/
  149. /** @defgroup UART_Private_Constants UART Private Constants
  150. * @{
  151. */
  152. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  153. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8| \
  154. USART_CR1_FIFOEN )) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
  155. #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT| \
  156. USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */
  157. #define LPUART_BRR_MIN 0x00000300U /* LPUART BRR minimum authorized value */
  158. #define LPUART_BRR_MAX 0x000FFFFFU /* LPUART BRR maximum authorized value */
  159. #define UART_BRR_MIN 0x10U /* UART BRR minimum authorized value */
  160. #define UART_BRR_MAX 0x0000FFFFU /* UART BRR maximum authorized value */
  161. /**
  162. * @}
  163. */
  164. /* Private macros ------------------------------------------------------------*/
  165. /* Private variables ---------------------------------------------------------*/
  166. /* Private function prototypes -----------------------------------------------*/
  167. /** @addtogroup UART_Private_Functions
  168. * @{
  169. */
  170. static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
  171. static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
  172. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  173. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  174. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  175. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  176. static void UART_DMAError(DMA_HandleTypeDef *hdma);
  177. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  178. static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  179. static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  180. static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  181. static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  182. static void UART_TxISR_8BIT(UART_HandleTypeDef *huart);
  183. static void UART_TxISR_16BIT(UART_HandleTypeDef *huart);
  184. static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
  185. static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
  186. static void UART_EndTransmit_IT(UART_HandleTypeDef *huart);
  187. static void UART_RxISR_8BIT(UART_HandleTypeDef *huart);
  188. static void UART_RxISR_16BIT(UART_HandleTypeDef *huart);
  189. static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
  190. static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
  191. /**
  192. * @}
  193. */
  194. /* Exported functions --------------------------------------------------------*/
  195. /** @defgroup UART_Exported_Functions UART Exported Functions
  196. * @{
  197. */
  198. /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
  199. * @brief Initialization and Configuration functions
  200. *
  201. @verbatim
  202. ===============================================================================
  203. ##### Initialization and Configuration functions #####
  204. ===============================================================================
  205. [..]
  206. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  207. in asynchronous mode.
  208. (+) For the asynchronous mode the parameters below can be configured:
  209. (++) Baud Rate
  210. (++) Word Length
  211. (++) Stop Bit
  212. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  213. in the data register is transmitted but is changed by the parity bit.
  214. (++) Hardware flow control
  215. (++) Receiver/transmitter modes
  216. (++) Over Sampling Method
  217. (++) One-Bit Sampling Method
  218. (+) For the asynchronous mode, the following advanced features can be configured as well:
  219. (++) TX and/or RX pin level inversion
  220. (++) data logical level inversion
  221. (++) RX and TX pins swap
  222. (++) RX overrun detection disabling
  223. (++) DMA disabling on RX error
  224. (++) MSB first on communication line
  225. (++) auto Baud rate detection
  226. [..]
  227. The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API
  228. follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
  229. and UART multiprocessor mode configuration procedures (details for the procedures
  230. are available in reference manual).
  231. @endverbatim
  232. Depending on the frame length defined by the M1 and M0 bits (7-bit,
  233. 8-bit or 9-bit), the possible UART formats are listed in the
  234. following table.
  235. Table 1. UART frame format.
  236. +-----------------------------------------------------------------------+
  237. | M1 bit | M0 bit | PCE bit | UART frame |
  238. |---------|---------|-----------|---------------------------------------|
  239. | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  240. |---------|---------|-----------|---------------------------------------|
  241. | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  242. |---------|---------|-----------|---------------------------------------|
  243. | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  244. |---------|---------|-----------|---------------------------------------|
  245. | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  246. |---------|---------|-----------|---------------------------------------|
  247. | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  248. |---------|---------|-----------|---------------------------------------|
  249. | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  250. +-----------------------------------------------------------------------+
  251. * @{
  252. */
  253. /**
  254. * @brief Initialize the UART mode according to the specified
  255. * parameters in the UART_InitTypeDef and initialize the associated handle.
  256. * @param huart UART handle.
  257. * @retval HAL status
  258. */
  259. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  260. {
  261. /* Check the UART handle allocation */
  262. if (huart == NULL)
  263. {
  264. return HAL_ERROR;
  265. }
  266. if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  267. {
  268. /* Check the parameters */
  269. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  270. }
  271. else
  272. {
  273. /* Check the parameters */
  274. assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
  275. }
  276. if (huart->gState == HAL_UART_STATE_RESET)
  277. {
  278. /* Allocate lock resource and initialize it */
  279. huart->Lock = HAL_UNLOCKED;
  280. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  281. UART_InitCallbacksToDefault(huart);
  282. if (huart->MspInitCallback == NULL)
  283. {
  284. huart->MspInitCallback = HAL_UART_MspInit;
  285. }
  286. /* Init the low level hardware */
  287. huart->MspInitCallback(huart);
  288. #else
  289. /* Init the low level hardware : GPIO, CLOCK */
  290. HAL_UART_MspInit(huart);
  291. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  292. }
  293. huart->gState = HAL_UART_STATE_BUSY;
  294. __HAL_UART_DISABLE(huart);
  295. /* Set the UART Communication parameters */
  296. if (UART_SetConfig(huart) == HAL_ERROR)
  297. {
  298. return HAL_ERROR;
  299. }
  300. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  301. {
  302. UART_AdvFeatureConfig(huart);
  303. }
  304. /* In asynchronous mode, the following bits must be kept cleared:
  305. - LINEN and CLKEN bits in the USART_CR2 register,
  306. - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
  307. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  308. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  309. __HAL_UART_ENABLE(huart);
  310. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  311. return (UART_CheckIdleState(huart));
  312. }
  313. /**
  314. * @brief Initialize the half-duplex mode according to the specified
  315. * parameters in the UART_InitTypeDef and creates the associated handle.
  316. * @param huart UART handle.
  317. * @retval HAL status
  318. */
  319. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
  320. {
  321. /* Check the UART handle allocation */
  322. if (huart == NULL)
  323. {
  324. return HAL_ERROR;
  325. }
  326. /* Check UART instance */
  327. assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
  328. if (huart->gState == HAL_UART_STATE_RESET)
  329. {
  330. /* Allocate lock resource and initialize it */
  331. huart->Lock = HAL_UNLOCKED;
  332. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  333. UART_InitCallbacksToDefault(huart);
  334. if (huart->MspInitCallback == NULL)
  335. {
  336. huart->MspInitCallback = HAL_UART_MspInit;
  337. }
  338. /* Init the low level hardware */
  339. huart->MspInitCallback(huart);
  340. #else
  341. /* Init the low level hardware : GPIO, CLOCK */
  342. HAL_UART_MspInit(huart);
  343. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  344. }
  345. huart->gState = HAL_UART_STATE_BUSY;
  346. __HAL_UART_DISABLE(huart);
  347. /* Set the UART Communication parameters */
  348. if (UART_SetConfig(huart) == HAL_ERROR)
  349. {
  350. return HAL_ERROR;
  351. }
  352. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  353. {
  354. UART_AdvFeatureConfig(huart);
  355. }
  356. /* In half-duplex mode, the following bits must be kept cleared:
  357. - LINEN and CLKEN bits in the USART_CR2 register,
  358. - SCEN and IREN bits in the USART_CR3 register.*/
  359. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  360. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
  361. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  362. SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  363. __HAL_UART_ENABLE(huart);
  364. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  365. return (UART_CheckIdleState(huart));
  366. }
  367. /**
  368. * @brief Initialize the LIN mode according to the specified
  369. * parameters in the UART_InitTypeDef and creates the associated handle.
  370. * @param huart UART handle.
  371. * @param BreakDetectLength Specifies the LIN break detection length.
  372. * This parameter can be one of the following values:
  373. * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
  374. * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
  375. * @retval HAL status
  376. */
  377. HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
  378. {
  379. /* Check the UART handle allocation */
  380. if (huart == NULL)
  381. {
  382. return HAL_ERROR;
  383. }
  384. /* Check the LIN UART instance */
  385. assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  386. /* Check the Break detection length parameter */
  387. assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
  388. /* LIN mode limited to 16-bit oversampling only */
  389. if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  390. {
  391. return HAL_ERROR;
  392. }
  393. /* LIN mode limited to 8-bit data length */
  394. if (huart->Init.WordLength != UART_WORDLENGTH_8B)
  395. {
  396. return HAL_ERROR;
  397. }
  398. if (huart->gState == HAL_UART_STATE_RESET)
  399. {
  400. /* Allocate lock resource and initialize it */
  401. huart->Lock = HAL_UNLOCKED;
  402. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  403. UART_InitCallbacksToDefault(huart);
  404. if (huart->MspInitCallback == NULL)
  405. {
  406. huart->MspInitCallback = HAL_UART_MspInit;
  407. }
  408. /* Init the low level hardware */
  409. huart->MspInitCallback(huart);
  410. #else
  411. /* Init the low level hardware : GPIO, CLOCK */
  412. HAL_UART_MspInit(huart);
  413. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  414. }
  415. huart->gState = HAL_UART_STATE_BUSY;
  416. __HAL_UART_DISABLE(huart);
  417. /* Set the UART Communication parameters */
  418. if (UART_SetConfig(huart) == HAL_ERROR)
  419. {
  420. return HAL_ERROR;
  421. }
  422. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  423. {
  424. UART_AdvFeatureConfig(huart);
  425. }
  426. /* In LIN mode, the following bits must be kept cleared:
  427. - LINEN and CLKEN bits in the USART_CR2 register,
  428. - SCEN and IREN bits in the USART_CR3 register.*/
  429. CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  430. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
  431. /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  432. SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
  433. /* Set the USART LIN Break detection length. */
  434. MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
  435. __HAL_UART_ENABLE(huart);
  436. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  437. return (UART_CheckIdleState(huart));
  438. }
  439. /**
  440. * @brief Initialize the multiprocessor mode according to the specified
  441. * parameters in the UART_InitTypeDef and initialize the associated handle.
  442. * @param huart UART handle.
  443. * @param Address UART node address (4-, 6-, 7- or 8-bit long).
  444. * @param WakeUpMethod Specifies the UART wakeup method.
  445. * This parameter can be one of the following values:
  446. * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
  447. * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
  448. * @note If the user resorts to idle line detection wake up, the Address parameter
  449. * is useless and ignored by the initialization function.
  450. * @note If the user resorts to address mark wake up, the address length detection
  451. * is configured by default to 4 bits only. For the UART to be able to
  452. * manage 6-, 7- or 8-bit long addresses detection, the API
  453. * HAL_MultiProcessorEx_AddressLength_Set() must be called after
  454. * HAL_MultiProcessor_Init().
  455. * @retval HAL status
  456. */
  457. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
  458. {
  459. /* Check the UART handle allocation */
  460. if (huart == NULL)
  461. {
  462. return HAL_ERROR;
  463. }
  464. /* Check the wake up method parameter */
  465. assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
  466. if (huart->gState == HAL_UART_STATE_RESET)
  467. {
  468. /* Allocate lock resource and initialize it */
  469. huart->Lock = HAL_UNLOCKED;
  470. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  471. UART_InitCallbacksToDefault(huart);
  472. if (huart->MspInitCallback == NULL)
  473. {
  474. huart->MspInitCallback = HAL_UART_MspInit;
  475. }
  476. /* Init the low level hardware */
  477. huart->MspInitCallback(huart);
  478. #else
  479. /* Init the low level hardware : GPIO, CLOCK */
  480. HAL_UART_MspInit(huart);
  481. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  482. }
  483. huart->gState = HAL_UART_STATE_BUSY;
  484. __HAL_UART_DISABLE(huart);
  485. /* Set the UART Communication parameters */
  486. if (UART_SetConfig(huart) == HAL_ERROR)
  487. {
  488. return HAL_ERROR;
  489. }
  490. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  491. {
  492. UART_AdvFeatureConfig(huart);
  493. }
  494. /* In multiprocessor mode, the following bits must be kept cleared:
  495. - LINEN and CLKEN bits in the USART_CR2 register,
  496. - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
  497. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  498. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  499. if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
  500. {
  501. /* If address mark wake up method is chosen, set the USART address node */
  502. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
  503. }
  504. /* Set the wake up method by setting the WAKE bit in the CR1 register */
  505. MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
  506. __HAL_UART_ENABLE(huart);
  507. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  508. return (UART_CheckIdleState(huart));
  509. }
  510. /**
  511. * @brief DeInitialize the UART peripheral.
  512. * @param huart UART handle.
  513. * @retval HAL status
  514. */
  515. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
  516. {
  517. /* Check the UART handle allocation */
  518. if (huart == NULL)
  519. {
  520. return HAL_ERROR;
  521. }
  522. /* Check the parameters */
  523. assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
  524. huart->gState = HAL_UART_STATE_BUSY;
  525. __HAL_UART_DISABLE(huart);
  526. huart->Instance->CR1 = 0x0U;
  527. huart->Instance->CR2 = 0x0U;
  528. huart->Instance->CR3 = 0x0U;
  529. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  530. if (huart->MspDeInitCallback == NULL)
  531. {
  532. huart->MspDeInitCallback = HAL_UART_MspDeInit;
  533. }
  534. /* DeInit the low level hardware */
  535. huart->MspDeInitCallback(huart);
  536. #else
  537. /* DeInit the low level hardware */
  538. HAL_UART_MspDeInit(huart);
  539. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  540. huart->ErrorCode = HAL_UART_ERROR_NONE;
  541. huart->gState = HAL_UART_STATE_RESET;
  542. huart->RxState = HAL_UART_STATE_RESET;
  543. __HAL_UNLOCK(huart);
  544. return HAL_OK;
  545. }
  546. /**
  547. * @brief Initialize the UART MSP.
  548. * @param huart UART handle.
  549. * @retval None
  550. */
  551. __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  552. {
  553. /* Prevent unused argument(s) compilation warning */
  554. UNUSED(huart);
  555. /* NOTE : This function should not be modified, when the callback is needed,
  556. the HAL_UART_MspInit can be implemented in the user file
  557. */
  558. }
  559. /**
  560. * @brief DeInitialize the UART MSP.
  561. * @param huart UART handle.
  562. * @retval None
  563. */
  564. __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  565. {
  566. /* Prevent unused argument(s) compilation warning */
  567. UNUSED(huart);
  568. /* NOTE : This function should not be modified, when the callback is needed,
  569. the HAL_UART_MspDeInit can be implemented in the user file
  570. */
  571. }
  572. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  573. /**
  574. * @brief Register a User UART Callback
  575. * To be used instead of the weak predefined callback
  576. * @param huart uart handle
  577. * @param CallbackID ID of the callback to be registered
  578. * This parameter can be one of the following values:
  579. * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  580. * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  581. * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  582. * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  583. * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
  584. * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  585. * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  586. * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  587. * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
  588. * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
  589. * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
  590. * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
  591. * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
  592. * @param pCallback pointer to the Callback function
  593. * @retval HAL status
  594. */
  595. HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID,
  596. pUART_CallbackTypeDef pCallback)
  597. {
  598. HAL_StatusTypeDef status = HAL_OK;
  599. if (pCallback == NULL)
  600. {
  601. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  602. return HAL_ERROR;
  603. }
  604. __HAL_LOCK(huart);
  605. if (huart->gState == HAL_UART_STATE_READY)
  606. {
  607. switch (CallbackID)
  608. {
  609. case HAL_UART_TX_HALFCOMPLETE_CB_ID :
  610. huart->TxHalfCpltCallback = pCallback;
  611. break;
  612. case HAL_UART_TX_COMPLETE_CB_ID :
  613. huart->TxCpltCallback = pCallback;
  614. break;
  615. case HAL_UART_RX_HALFCOMPLETE_CB_ID :
  616. huart->RxHalfCpltCallback = pCallback;
  617. break;
  618. case HAL_UART_RX_COMPLETE_CB_ID :
  619. huart->RxCpltCallback = pCallback;
  620. break;
  621. case HAL_UART_ERROR_CB_ID :
  622. huart->ErrorCallback = pCallback;
  623. break;
  624. case HAL_UART_ABORT_COMPLETE_CB_ID :
  625. huart->AbortCpltCallback = pCallback;
  626. break;
  627. case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
  628. huart->AbortTransmitCpltCallback = pCallback;
  629. break;
  630. case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
  631. huart->AbortReceiveCpltCallback = pCallback;
  632. break;
  633. case HAL_UART_WAKEUP_CB_ID :
  634. huart->WakeupCallback = pCallback;
  635. break;
  636. case HAL_UART_RX_FIFO_FULL_CB_ID :
  637. huart->RxFifoFullCallback = pCallback;
  638. break;
  639. case HAL_UART_TX_FIFO_EMPTY_CB_ID :
  640. huart->TxFifoEmptyCallback = pCallback;
  641. break;
  642. case HAL_UART_MSPINIT_CB_ID :
  643. huart->MspInitCallback = pCallback;
  644. break;
  645. case HAL_UART_MSPDEINIT_CB_ID :
  646. huart->MspDeInitCallback = pCallback;
  647. break;
  648. default :
  649. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  650. status = HAL_ERROR;
  651. break;
  652. }
  653. }
  654. else if (huart->gState == HAL_UART_STATE_RESET)
  655. {
  656. switch (CallbackID)
  657. {
  658. case HAL_UART_MSPINIT_CB_ID :
  659. huart->MspInitCallback = pCallback;
  660. break;
  661. case HAL_UART_MSPDEINIT_CB_ID :
  662. huart->MspDeInitCallback = pCallback;
  663. break;
  664. default :
  665. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  666. status = HAL_ERROR;
  667. break;
  668. }
  669. }
  670. else
  671. {
  672. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  673. status = HAL_ERROR;
  674. }
  675. __HAL_UNLOCK(huart);
  676. return status;
  677. }
  678. /**
  679. * @brief Unregister an UART Callback
  680. * UART callaback is redirected to the weak predefined callback
  681. * @param huart uart handle
  682. * @param CallbackID ID of the callback to be unregistered
  683. * This parameter can be one of the following values:
  684. * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  685. * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  686. * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  687. * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  688. * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
  689. * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  690. * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  691. * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  692. * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
  693. * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
  694. * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
  695. * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
  696. * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
  697. * @retval HAL status
  698. */
  699. HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID)
  700. {
  701. HAL_StatusTypeDef status = HAL_OK;
  702. __HAL_LOCK(huart);
  703. if (HAL_UART_STATE_READY == huart->gState)
  704. {
  705. switch (CallbackID)
  706. {
  707. case HAL_UART_TX_HALFCOMPLETE_CB_ID :
  708. huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  709. break;
  710. case HAL_UART_TX_COMPLETE_CB_ID :
  711. huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
  712. break;
  713. case HAL_UART_RX_HALFCOMPLETE_CB_ID :
  714. huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  715. break;
  716. case HAL_UART_RX_COMPLETE_CB_ID :
  717. huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
  718. break;
  719. case HAL_UART_ERROR_CB_ID :
  720. huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
  721. break;
  722. case HAL_UART_ABORT_COMPLETE_CB_ID :
  723. huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  724. break;
  725. case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
  726. huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  727. break;
  728. case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
  729. huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
  730. break;
  731. case HAL_UART_WAKEUP_CB_ID :
  732. huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */
  733. break;
  734. case HAL_UART_RX_FIFO_FULL_CB_ID :
  735. huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
  736. break;
  737. case HAL_UART_TX_FIFO_EMPTY_CB_ID :
  738. huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
  739. break;
  740. case HAL_UART_MSPINIT_CB_ID :
  741. huart->MspInitCallback = HAL_UART_MspInit; /* Legacy weak MspInitCallback */
  742. break;
  743. case HAL_UART_MSPDEINIT_CB_ID :
  744. huart->MspDeInitCallback = HAL_UART_MspDeInit; /* Legacy weak MspDeInitCallback */
  745. break;
  746. default :
  747. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  748. status = HAL_ERROR;
  749. break;
  750. }
  751. }
  752. else if (HAL_UART_STATE_RESET == huart->gState)
  753. {
  754. switch (CallbackID)
  755. {
  756. case HAL_UART_MSPINIT_CB_ID :
  757. huart->MspInitCallback = HAL_UART_MspInit;
  758. break;
  759. case HAL_UART_MSPDEINIT_CB_ID :
  760. huart->MspDeInitCallback = HAL_UART_MspDeInit;
  761. break;
  762. default :
  763. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  764. status = HAL_ERROR;
  765. break;
  766. }
  767. }
  768. else
  769. {
  770. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  771. status = HAL_ERROR;
  772. }
  773. __HAL_UNLOCK(huart);
  774. return status;
  775. }
  776. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  777. /**
  778. * @}
  779. */
  780. /** @defgroup UART_Exported_Functions_Group2 IO operation functions
  781. * @brief UART Transmit/Receive functions
  782. *
  783. @verbatim
  784. ===============================================================================
  785. ##### IO operation functions #####
  786. ===============================================================================
  787. This subsection provides a set of functions allowing to manage the UART asynchronous
  788. and Half duplex data transfers.
  789. (#) There are two mode of transfer:
  790. (+) Blocking mode: The communication is performed in polling mode.
  791. The HAL status of all data processing is returned by the same function
  792. after finishing transfer.
  793. (+) Non-Blocking mode: The communication is performed using Interrupts
  794. or DMA, These API's return the HAL status.
  795. The end of the data processing will be indicated through the
  796. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
  797. using DMA mode.
  798. The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
  799. will be executed respectively at the end of the transmit or Receive process
  800. The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
  801. (#) Blocking mode API's are :
  802. (+) HAL_UART_Transmit()
  803. (+) HAL_UART_Receive()
  804. (#) Non-Blocking mode API's with Interrupt are :
  805. (+) HAL_UART_Transmit_IT()
  806. (+) HAL_UART_Receive_IT()
  807. (+) HAL_UART_IRQHandler()
  808. (#) Non-Blocking mode API's with DMA are :
  809. (+) HAL_UART_Transmit_DMA()
  810. (+) HAL_UART_Receive_DMA()
  811. (+) HAL_UART_DMAPause()
  812. (+) HAL_UART_DMAResume()
  813. (+) HAL_UART_DMAStop()
  814. (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
  815. (+) HAL_UART_TxHalfCpltCallback()
  816. (+) HAL_UART_TxCpltCallback()
  817. (+) HAL_UART_RxHalfCpltCallback()
  818. (+) HAL_UART_RxCpltCallback()
  819. (+) HAL_UART_ErrorCallback()
  820. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  821. (+) HAL_UART_Abort()
  822. (+) HAL_UART_AbortTransmit()
  823. (+) HAL_UART_AbortReceive()
  824. (+) HAL_UART_Abort_IT()
  825. (+) HAL_UART_AbortTransmit_IT()
  826. (+) HAL_UART_AbortReceive_IT()
  827. (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  828. (+) HAL_UART_AbortCpltCallback()
  829. (+) HAL_UART_AbortTransmitCpltCallback()
  830. (+) HAL_UART_AbortReceiveCpltCallback()
  831. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  832. Errors are handled as follows :
  833. (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  834. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  835. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  836. and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side.
  837. If user wants to abort it, Abort services should be called by user.
  838. (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  839. This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
  840. Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
  841. -@- In the Half duplex communication, it is forbidden to run the transmit
  842. and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
  843. @endverbatim
  844. * @{
  845. */
  846. /**
  847. * @brief Send an amount of data in blocking mode.
  848. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  849. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  850. * of u16 provided through pData.
  851. * @note When FIFO mode is enabled, writing a data in the TDR register adds one
  852. * data to the TXFIFO. Write operations to the TDR register are performed
  853. * when TXFNF flag is set. From hardware perspective, TXFNF flag and
  854. * TXE are mapped on the same bit-field.
  855. * @param huart UART handle.
  856. * @param pData Pointer to data buffer (u8 or u16 data elements).
  857. * @param Size Amount of data elements (u8 or u16) to be sent.
  858. * @param Timeout Timeout duration.
  859. * @retval HAL status
  860. */
  861. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  862. {
  863. uint8_t *pdata8bits;
  864. uint16_t *pdata16bits;
  865. uint32_t tickstart;
  866. /* Check that a Tx process is not already ongoing */
  867. if (huart->gState == HAL_UART_STATE_READY)
  868. {
  869. if ((pData == NULL) || (Size == 0U))
  870. {
  871. return HAL_ERROR;
  872. }
  873. __HAL_LOCK(huart);
  874. huart->ErrorCode = HAL_UART_ERROR_NONE;
  875. huart->gState = HAL_UART_STATE_BUSY_TX;
  876. /* Init tickstart for timeout managment*/
  877. tickstart = HAL_GetTick();
  878. huart->TxXferSize = Size;
  879. huart->TxXferCount = Size;
  880. /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
  881. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  882. {
  883. pdata8bits = NULL;
  884. pdata16bits = (uint16_t *) pData;
  885. }
  886. else
  887. {
  888. pdata8bits = pData;
  889. pdata16bits = NULL;
  890. }
  891. while (huart->TxXferCount > 0U)
  892. {
  893. if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  894. {
  895. return HAL_TIMEOUT;
  896. }
  897. if (pdata8bits == NULL)
  898. {
  899. huart->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU);
  900. pdata16bits++;
  901. }
  902. else
  903. {
  904. huart->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU);
  905. pdata8bits++;
  906. }
  907. huart->TxXferCount--;
  908. }
  909. if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  910. {
  911. return HAL_TIMEOUT;
  912. }
  913. /* At end of Tx process, restore huart->gState to Ready */
  914. huart->gState = HAL_UART_STATE_READY;
  915. __HAL_UNLOCK(huart);
  916. return HAL_OK;
  917. }
  918. else
  919. {
  920. return HAL_BUSY;
  921. }
  922. }
  923. /**
  924. * @brief Receive an amount of data in blocking mode.
  925. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  926. * the received data is handled as a set of u16. In this case, Size must indicate the number
  927. * of u16 available through pData.
  928. * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
  929. * is not empty. Read operations from the RDR register are performed when
  930. * RXFNE flag is set. From hardware perspective, RXFNE flag and
  931. * RXNE are mapped on the same bit-field.
  932. * @param huart UART handle.
  933. * @param pData Pointer to data buffer (u8 or u16 data elements).
  934. * @param Size Amount of data elements (u8 or u16) to be received.
  935. * @param Timeout Timeout duration.
  936. * @retval HAL status
  937. */
  938. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  939. {
  940. uint8_t *pdata8bits;
  941. uint16_t *pdata16bits;
  942. uint16_t uhMask;
  943. uint32_t tickstart;
  944. /* Check that a Rx process is not already ongoing */
  945. if (huart->RxState == HAL_UART_STATE_READY)
  946. {
  947. if ((pData == NULL) || (Size == 0U))
  948. {
  949. return HAL_ERROR;
  950. }
  951. __HAL_LOCK(huart);
  952. huart->ErrorCode = HAL_UART_ERROR_NONE;
  953. huart->RxState = HAL_UART_STATE_BUSY_RX;
  954. /* Init tickstart for timeout managment*/
  955. tickstart = HAL_GetTick();
  956. huart->RxXferSize = Size;
  957. huart->RxXferCount = Size;
  958. /* Computation of UART mask to apply to RDR register */
  959. UART_MASK_COMPUTATION(huart);
  960. uhMask = huart->Mask;
  961. /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  962. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  963. {
  964. pdata8bits = NULL;
  965. pdata16bits = (uint16_t *) pData;
  966. }
  967. else
  968. {
  969. pdata8bits = pData;
  970. pdata16bits = NULL;
  971. }
  972. /* as long as data have to be received */
  973. while (huart->RxXferCount > 0U)
  974. {
  975. if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  976. {
  977. return HAL_TIMEOUT;
  978. }
  979. if (pdata8bits == NULL)
  980. {
  981. *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
  982. pdata16bits++;
  983. }
  984. else
  985. {
  986. *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  987. pdata8bits++;
  988. }
  989. huart->RxXferCount--;
  990. }
  991. /* At end of Rx process, restore huart->RxState to Ready */
  992. huart->RxState = HAL_UART_STATE_READY;
  993. __HAL_UNLOCK(huart);
  994. return HAL_OK;
  995. }
  996. else
  997. {
  998. return HAL_BUSY;
  999. }
  1000. }
  1001. /**
  1002. * @brief Send an amount of data in interrupt mode.
  1003. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1004. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  1005. * of u16 provided through pData.
  1006. * @param huart UART handle.
  1007. * @param pData Pointer to data buffer (u8 or u16 data elements).
  1008. * @param Size Amount of data elements (u8 or u16) to be sent.
  1009. * @retval HAL status
  1010. */
  1011. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1012. {
  1013. /* Check that a Tx process is not already ongoing */
  1014. if (huart->gState == HAL_UART_STATE_READY)
  1015. {
  1016. if ((pData == NULL) || (Size == 0U))
  1017. {
  1018. return HAL_ERROR;
  1019. }
  1020. __HAL_LOCK(huart);
  1021. huart->pTxBuffPtr = pData;
  1022. huart->TxXferSize = Size;
  1023. huart->TxXferCount = Size;
  1024. huart->TxISR = NULL;
  1025. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1026. huart->gState = HAL_UART_STATE_BUSY_TX;
  1027. /* Configure Tx interrupt processing */
  1028. if (huart->FifoMode == UART_FIFOMODE_ENABLE)
  1029. {
  1030. /* Set the Tx ISR function pointer according to the data word length */
  1031. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1032. {
  1033. huart->TxISR = UART_TxISR_16BIT_FIFOEN;
  1034. }
  1035. else
  1036. {
  1037. huart->TxISR = UART_TxISR_8BIT_FIFOEN;
  1038. }
  1039. __HAL_UNLOCK(huart);
  1040. /* Enable the TX FIFO threshold interrupt */
  1041. SET_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
  1042. }
  1043. else
  1044. {
  1045. /* Set the Tx ISR function pointer according to the data word length */
  1046. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1047. {
  1048. huart->TxISR = UART_TxISR_16BIT;
  1049. }
  1050. else
  1051. {
  1052. huart->TxISR = UART_TxISR_8BIT;
  1053. }
  1054. __HAL_UNLOCK(huart);
  1055. /* Enable the Transmit Data Register Empty interrupt */
  1056. SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  1057. }
  1058. return HAL_OK;
  1059. }
  1060. else
  1061. {
  1062. return HAL_BUSY;
  1063. }
  1064. }
  1065. /**
  1066. * @brief Receive an amount of data in interrupt mode.
  1067. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1068. * the received data is handled as a set of u16. In this case, Size must indicate the number
  1069. * of u16 available through pData.
  1070. * @param huart UART handle.
  1071. * @param pData Pointer to data buffer (u8 or u16 data elements).
  1072. * @param Size Amount of data elements (u8 or u16) to be received.
  1073. * @retval HAL status
  1074. */
  1075. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1076. {
  1077. /* Check that a Rx process is not already ongoing */
  1078. if (huart->RxState == HAL_UART_STATE_READY)
  1079. {
  1080. if ((pData == NULL) || (Size == 0U))
  1081. {
  1082. return HAL_ERROR;
  1083. }
  1084. __HAL_LOCK(huart);
  1085. huart->pRxBuffPtr = pData;
  1086. huart->RxXferSize = Size;
  1087. huart->RxXferCount = Size;
  1088. huart->RxISR = NULL;
  1089. /* Computation of UART mask to apply to RDR register */
  1090. UART_MASK_COMPUTATION(huart);
  1091. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1092. huart->RxState = HAL_UART_STATE_BUSY_RX;
  1093. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1094. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1095. /* Configure Rx interrupt processing*/
  1096. if ((huart->FifoMode == UART_FIFOMODE_ENABLE) && (Size >= huart->NbRxDataToProcess))
  1097. {
  1098. /* Set the Rx ISR function pointer according to the data word length */
  1099. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1100. {
  1101. huart->RxISR = UART_RxISR_16BIT_FIFOEN;
  1102. }
  1103. else
  1104. {
  1105. huart->RxISR = UART_RxISR_8BIT_FIFOEN;
  1106. }
  1107. __HAL_UNLOCK(huart);
  1108. /* Enable the UART Parity Error interrupt and RX FIFO Threshold interrupt */
  1109. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1110. SET_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
  1111. }
  1112. else
  1113. {
  1114. /* Set the Rx ISR function pointer according to the data word length */
  1115. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1116. {
  1117. huart->RxISR = UART_RxISR_16BIT;
  1118. }
  1119. else
  1120. {
  1121. huart->RxISR = UART_RxISR_8BIT;
  1122. }
  1123. __HAL_UNLOCK(huart);
  1124. /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
  1125. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
  1126. }
  1127. return HAL_OK;
  1128. }
  1129. else
  1130. {
  1131. return HAL_BUSY;
  1132. }
  1133. }
  1134. /**
  1135. * @brief Send an amount of data in DMA mode.
  1136. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1137. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  1138. * of u16 provided through pData.
  1139. * @param huart UART handle.
  1140. * @param pData Pointer to data buffer (u8 or u16 data elements).
  1141. * @param Size Amount of data elements (u8 or u16) to be sent.
  1142. * @retval HAL status
  1143. */
  1144. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1145. {
  1146. /* Check that a Tx process is not already ongoing */
  1147. if (huart->gState == HAL_UART_STATE_READY)
  1148. {
  1149. if ((pData == NULL) || (Size == 0U))
  1150. {
  1151. return HAL_ERROR;
  1152. }
  1153. __HAL_LOCK(huart);
  1154. huart->pTxBuffPtr = pData;
  1155. huart->TxXferSize = Size;
  1156. huart->TxXferCount = Size;
  1157. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1158. huart->gState = HAL_UART_STATE_BUSY_TX;
  1159. if (huart->hdmatx != NULL)
  1160. {
  1161. /* Set the UART DMA transfer complete callback */
  1162. huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
  1163. /* Set the UART DMA Half transfer complete callback */
  1164. huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
  1165. /* Set the DMA error callback */
  1166. huart->hdmatx->XferErrorCallback = UART_DMAError;
  1167. /* Set the DMA abort callback */
  1168. huart->hdmatx->XferAbortCallback = NULL;
  1169. /* Enable the UART transmit DMA channel */
  1170. if (HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)huart->pTxBuffPtr, (uint32_t)&huart->Instance->TDR, Size) != HAL_OK)
  1171. {
  1172. /* Set error code to DMA */
  1173. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1174. __HAL_UNLOCK(huart);
  1175. /* Restore huart->gState to ready */
  1176. huart->gState = HAL_UART_STATE_READY;
  1177. return HAL_ERROR;
  1178. }
  1179. }
  1180. /* Clear the TC flag in the ICR register */
  1181. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
  1182. __HAL_UNLOCK(huart);
  1183. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1184. in the UART CR3 register */
  1185. SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1186. return HAL_OK;
  1187. }
  1188. else
  1189. {
  1190. return HAL_BUSY;
  1191. }
  1192. }
  1193. /**
  1194. * @brief Receive an amount of data in DMA mode.
  1195. * @note When the UART parity is enabled (PCE = 1), the received data contain
  1196. * the parity bit (MSB position).
  1197. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1198. * the received data is handled as a set of u16. In this case, Size must indicate the number
  1199. * of u16 available through pData.
  1200. * @param huart UART handle.
  1201. * @param pData Pointer to data buffer (u8 or u16 data elements).
  1202. * @param Size Amount of data elements (u8 or u16) to be received.
  1203. * @retval HAL status
  1204. */
  1205. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1206. {
  1207. /* Check that a Rx process is not already ongoing */
  1208. if (huart->RxState == HAL_UART_STATE_READY)
  1209. {
  1210. if ((pData == NULL) || (Size == 0U))
  1211. {
  1212. return HAL_ERROR;
  1213. }
  1214. __HAL_LOCK(huart);
  1215. huart->pRxBuffPtr = pData;
  1216. huart->RxXferSize = Size;
  1217. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1218. huart->RxState = HAL_UART_STATE_BUSY_RX;
  1219. if (huart->hdmarx != NULL)
  1220. {
  1221. /* Set the UART DMA transfer complete callback */
  1222. huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
  1223. /* Set the UART DMA Half transfer complete callback */
  1224. huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
  1225. /* Set the DMA error callback */
  1226. huart->hdmarx->XferErrorCallback = UART_DMAError;
  1227. /* Set the DMA abort callback */
  1228. huart->hdmarx->XferAbortCallback = NULL;
  1229. /* Enable the DMA channel */
  1230. if (HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, Size) != HAL_OK)
  1231. {
  1232. /* Set error code to DMA */
  1233. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1234. __HAL_UNLOCK(huart);
  1235. /* Restore huart->gState to ready */
  1236. huart->gState = HAL_UART_STATE_READY;
  1237. return HAL_ERROR;
  1238. }
  1239. }
  1240. __HAL_UNLOCK(huart);
  1241. /* Enable the UART Parity Error Interrupt */
  1242. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1243. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1244. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1245. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1246. in the UART CR3 register */
  1247. SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1248. return HAL_OK;
  1249. }
  1250. else
  1251. {
  1252. return HAL_BUSY;
  1253. }
  1254. }
  1255. /**
  1256. * @brief Pause the DMA Transfer.
  1257. * @param huart UART handle.
  1258. * @retval HAL status
  1259. */
  1260. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
  1261. {
  1262. const HAL_UART_StateTypeDef gstate = huart->gState;
  1263. const HAL_UART_StateTypeDef rxstate = huart->RxState;
  1264. __HAL_LOCK(huart);
  1265. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
  1266. (gstate == HAL_UART_STATE_BUSY_TX))
  1267. {
  1268. /* Disable the UART DMA Tx request */
  1269. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1270. }
  1271. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
  1272. (rxstate == HAL_UART_STATE_BUSY_RX))
  1273. {
  1274. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1275. CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1276. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1277. /* Disable the UART DMA Rx request */
  1278. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1279. }
  1280. __HAL_UNLOCK(huart);
  1281. return HAL_OK;
  1282. }
  1283. /**
  1284. * @brief Resume the DMA Transfer.
  1285. * @param huart UART handle.
  1286. * @retval HAL status
  1287. */
  1288. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
  1289. {
  1290. __HAL_LOCK(huart);
  1291. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  1292. {
  1293. /* Enable the UART DMA Tx request */
  1294. SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1295. }
  1296. if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  1297. {
  1298. /* Clear the Overrun flag before resuming the Rx transfer */
  1299. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
  1300. /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1301. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1302. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1303. /* Enable the UART DMA Rx request */
  1304. SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1305. }
  1306. __HAL_UNLOCK(huart);
  1307. return HAL_OK;
  1308. }
  1309. /**
  1310. * @brief Stop the DMA Transfer.
  1311. * @param huart UART handle.
  1312. * @retval HAL status
  1313. */
  1314. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
  1315. {
  1316. /* The Lock is not implemented on this API to allow the user application
  1317. to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
  1318. HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
  1319. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  1320. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  1321. the stream and the corresponding call back is executed. */
  1322. const HAL_UART_StateTypeDef gstate = huart->gState;
  1323. const HAL_UART_StateTypeDef rxstate = huart->RxState;
  1324. /* Stop UART DMA Tx request if ongoing */
  1325. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
  1326. (gstate == HAL_UART_STATE_BUSY_TX))
  1327. {
  1328. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1329. /* Abort the UART DMA Tx channel */
  1330. if (huart->hdmatx != NULL)
  1331. {
  1332. if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
  1333. {
  1334. if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1335. {
  1336. /* Set error code to DMA */
  1337. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1338. return HAL_TIMEOUT;
  1339. }
  1340. }
  1341. }
  1342. UART_EndTxTransfer(huart);
  1343. }
  1344. /* Stop UART DMA Rx request if ongoing */
  1345. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
  1346. (rxstate == HAL_UART_STATE_BUSY_RX))
  1347. {
  1348. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1349. /* Abort the UART DMA Rx channel */
  1350. if (huart->hdmarx != NULL)
  1351. {
  1352. if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
  1353. {
  1354. if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1355. {
  1356. /* Set error code to DMA */
  1357. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1358. return HAL_TIMEOUT;
  1359. }
  1360. }
  1361. }
  1362. UART_EndRxTransfer(huart);
  1363. }
  1364. return HAL_OK;
  1365. }
  1366. /**
  1367. * @brief Abort ongoing transfers (blocking mode).
  1368. * @param huart UART handle.
  1369. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1370. * This procedure performs following operations :
  1371. * - Disable UART Interrupts (Tx and Rx)
  1372. * - Disable the DMA transfer in the peripheral register (if enabled)
  1373. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1374. * - Set handle State to READY
  1375. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1376. * @retval HAL status
  1377. */
  1378. HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
  1379. {
  1380. /* Disable TXE, TC, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1381. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1382. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE);
  1383. /* Disable the UART DMA Tx request if enabled */
  1384. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1385. {
  1386. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1387. /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1388. if (huart->hdmatx != NULL)
  1389. {
  1390. /* Set the UART DMA Abort callback to Null.
  1391. No call back execution at end of DMA abort procedure */
  1392. huart->hdmatx->XferAbortCallback = NULL;
  1393. if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
  1394. {
  1395. if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1396. {
  1397. /* Set error code to DMA */
  1398. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1399. return HAL_TIMEOUT;
  1400. }
  1401. }
  1402. }
  1403. }
  1404. /* Disable the UART DMA Rx request if enabled */
  1405. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1406. {
  1407. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1408. /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1409. if (huart->hdmarx != NULL)
  1410. {
  1411. /* Set the UART DMA Abort callback to Null.
  1412. No call back execution at end of DMA abort procedure */
  1413. huart->hdmarx->XferAbortCallback = NULL;
  1414. if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
  1415. {
  1416. if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1417. {
  1418. /* Set error code to DMA */
  1419. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1420. return HAL_TIMEOUT;
  1421. }
  1422. }
  1423. }
  1424. }
  1425. /* Reset Tx and Rx transfer counters */
  1426. huart->TxXferCount = 0U;
  1427. huart->RxXferCount = 0U;
  1428. /* Clear the Error flags in the ICR register */
  1429. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1430. /* Flush the whole TX FIFO (if needed) */
  1431. if (huart->FifoMode == UART_FIFOMODE_ENABLE)
  1432. {
  1433. __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
  1434. }
  1435. /* Discard the received data */
  1436. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1437. /* Restore huart->gState and huart->RxState to Ready */
  1438. huart->gState = HAL_UART_STATE_READY;
  1439. huart->RxState = HAL_UART_STATE_READY;
  1440. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1441. return HAL_OK;
  1442. }
  1443. /**
  1444. * @brief Abort ongoing Transmit transfer (blocking mode).
  1445. * @param huart UART handle.
  1446. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1447. * This procedure performs following operations :
  1448. * - Disable UART Interrupts (Tx)
  1449. * - Disable the DMA transfer in the peripheral register (if enabled)
  1450. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1451. * - Set handle State to READY
  1452. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1453. * @retval HAL status
  1454. */
  1455. HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
  1456. {
  1457. /* Disable TCIE, TXEIE and TXFTIE interrupts */
  1458. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
  1459. CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
  1460. /* Disable the UART DMA Tx request if enabled */
  1461. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1462. {
  1463. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1464. /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1465. if (huart->hdmatx != NULL)
  1466. {
  1467. /* Set the UART DMA Abort callback to Null.
  1468. No call back execution at end of DMA abort procedure */
  1469. huart->hdmatx->XferAbortCallback = NULL;
  1470. if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
  1471. {
  1472. if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1473. {
  1474. /* Set error code to DMA */
  1475. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1476. return HAL_TIMEOUT;
  1477. }
  1478. }
  1479. }
  1480. }
  1481. /* Reset Tx transfer counter */
  1482. huart->TxXferCount = 0U;
  1483. /* Flush the whole TX FIFO (if needed) */
  1484. if (huart->FifoMode == UART_FIFOMODE_ENABLE)
  1485. {
  1486. __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
  1487. }
  1488. /* Restore huart->gState to Ready */
  1489. huart->gState = HAL_UART_STATE_READY;
  1490. return HAL_OK;
  1491. }
  1492. /**
  1493. * @brief Abort ongoing Receive transfer (blocking mode).
  1494. * @param huart UART handle.
  1495. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1496. * This procedure performs following operations :
  1497. * - Disable UART Interrupts (Rx)
  1498. * - Disable the DMA transfer in the peripheral register (if enabled)
  1499. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1500. * - Set handle State to READY
  1501. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1502. * @retval HAL status
  1503. */
  1504. HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
  1505. {
  1506. /* Disable PEIE, EIE, RXNEIE and RXFTIE interrupts */
  1507. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
  1508. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE);
  1509. /* Disable the UART DMA Rx request if enabled */
  1510. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1511. {
  1512. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1513. /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1514. if (huart->hdmarx != NULL)
  1515. {
  1516. /* Set the UART DMA Abort callback to Null.
  1517. No call back execution at end of DMA abort procedure */
  1518. huart->hdmarx->XferAbortCallback = NULL;
  1519. if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
  1520. {
  1521. if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1522. {
  1523. /* Set error code to DMA */
  1524. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1525. return HAL_TIMEOUT;
  1526. }
  1527. }
  1528. }
  1529. }
  1530. /* Reset Rx transfer counter */
  1531. huart->RxXferCount = 0U;
  1532. /* Clear the Error flags in the ICR register */
  1533. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1534. /* Discard the received data */
  1535. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1536. /* Restore huart->RxState to Ready */
  1537. huart->RxState = HAL_UART_STATE_READY;
  1538. return HAL_OK;
  1539. }
  1540. /**
  1541. * @brief Abort ongoing transfers (Interrupt mode).
  1542. * @param huart UART handle.
  1543. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1544. * This procedure performs following operations :
  1545. * - Disable UART Interrupts (Tx and Rx)
  1546. * - Disable the DMA transfer in the peripheral register (if enabled)
  1547. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1548. * - Set handle State to READY
  1549. * - At abort completion, call user abort complete callback
  1550. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1551. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1552. * @retval HAL status
  1553. */
  1554. HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
  1555. {
  1556. uint32_t abortcplt = 1U;
  1557. /* Disable interrupts */
  1558. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_TCIE | USART_CR1_RXNEIE_RXFNEIE | USART_CR1_TXEIE_TXFNFIE));
  1559. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  1560. /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
  1561. before any call to DMA Abort functions */
  1562. /* DMA Tx Handle is valid */
  1563. if (huart->hdmatx != NULL)
  1564. {
  1565. /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
  1566. Otherwise, set it to NULL */
  1567. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1568. {
  1569. huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
  1570. }
  1571. else
  1572. {
  1573. huart->hdmatx->XferAbortCallback = NULL;
  1574. }
  1575. }
  1576. /* DMA Rx Handle is valid */
  1577. if (huart->hdmarx != NULL)
  1578. {
  1579. /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
  1580. Otherwise, set it to NULL */
  1581. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1582. {
  1583. huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
  1584. }
  1585. else
  1586. {
  1587. huart->hdmarx->XferAbortCallback = NULL;
  1588. }
  1589. }
  1590. /* Disable the UART DMA Tx request if enabled */
  1591. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1592. {
  1593. /* Disable DMA Tx at UART level */
  1594. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1595. /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1596. if (huart->hdmatx != NULL)
  1597. {
  1598. /* UART Tx DMA Abort callback has already been initialised :
  1599. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1600. /* Abort DMA TX */
  1601. if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
  1602. {
  1603. huart->hdmatx->XferAbortCallback = NULL;
  1604. }
  1605. else
  1606. {
  1607. abortcplt = 0U;
  1608. }
  1609. }
  1610. }
  1611. /* Disable the UART DMA Rx request if enabled */
  1612. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1613. {
  1614. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1615. /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1616. if (huart->hdmarx != NULL)
  1617. {
  1618. /* UART Rx DMA Abort callback has already been initialised :
  1619. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1620. /* Abort DMA RX */
  1621. if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1622. {
  1623. huart->hdmarx->XferAbortCallback = NULL;
  1624. abortcplt = 1U;
  1625. }
  1626. else
  1627. {
  1628. abortcplt = 0U;
  1629. }
  1630. }
  1631. }
  1632. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1633. if (abortcplt == 1U)
  1634. {
  1635. /* Reset Tx and Rx transfer counters */
  1636. huart->TxXferCount = 0U;
  1637. huart->RxXferCount = 0U;
  1638. /* Clear ISR function pointers */
  1639. huart->RxISR = NULL;
  1640. huart->TxISR = NULL;
  1641. /* Reset errorCode */
  1642. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1643. /* Clear the Error flags in the ICR register */
  1644. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1645. /* Flush the whole TX FIFO (if needed) */
  1646. if (huart->FifoMode == UART_FIFOMODE_ENABLE)
  1647. {
  1648. __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
  1649. }
  1650. /* Discard the received data */
  1651. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1652. /* Restore huart->gState and huart->RxState to Ready */
  1653. huart->gState = HAL_UART_STATE_READY;
  1654. huart->RxState = HAL_UART_STATE_READY;
  1655. /* As no DMA to be aborted, call directly user Abort complete callback */
  1656. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1657. /* Call registered Abort complete callback */
  1658. huart->AbortCpltCallback(huart);
  1659. #else
  1660. /* Call legacy weak Abort complete callback */
  1661. HAL_UART_AbortCpltCallback(huart);
  1662. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1663. }
  1664. return HAL_OK;
  1665. }
  1666. /**
  1667. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1668. * @param huart UART handle.
  1669. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1670. * This procedure performs following operations :
  1671. * - Disable UART Interrupts (Tx)
  1672. * - Disable the DMA transfer in the peripheral register (if enabled)
  1673. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1674. * - Set handle State to READY
  1675. * - At abort completion, call user abort complete callback
  1676. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1677. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1678. * @retval HAL status
  1679. */
  1680. HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
  1681. {
  1682. /* Disable interrupts */
  1683. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
  1684. CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
  1685. /* Disable the UART DMA Tx request if enabled */
  1686. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1687. {
  1688. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1689. /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1690. if (huart->hdmatx != NULL)
  1691. {
  1692. /* Set the UART DMA Abort callback :
  1693. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1694. huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
  1695. /* Abort DMA TX */
  1696. if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
  1697. {
  1698. /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
  1699. huart->hdmatx->XferAbortCallback(huart->hdmatx);
  1700. }
  1701. }
  1702. else
  1703. {
  1704. /* Reset Tx transfer counter */
  1705. huart->TxXferCount = 0U;
  1706. /* Clear TxISR function pointers */
  1707. huart->TxISR = NULL;
  1708. /* Restore huart->gState to Ready */
  1709. huart->gState = HAL_UART_STATE_READY;
  1710. /* As no DMA to be aborted, call directly user Abort complete callback */
  1711. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1712. /* Call registered Abort Transmit Complete Callback */
  1713. huart->AbortTransmitCpltCallback(huart);
  1714. #else
  1715. /* Call legacy weak Abort Transmit Complete Callback */
  1716. HAL_UART_AbortTransmitCpltCallback(huart);
  1717. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1718. }
  1719. }
  1720. else
  1721. {
  1722. /* Reset Tx transfer counter */
  1723. huart->TxXferCount = 0U;
  1724. /* Clear TxISR function pointers */
  1725. huart->TxISR = NULL;
  1726. /* Flush the whole TX FIFO (if needed) */
  1727. if (huart->FifoMode == UART_FIFOMODE_ENABLE)
  1728. {
  1729. __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
  1730. }
  1731. /* Restore huart->gState to Ready */
  1732. huart->gState = HAL_UART_STATE_READY;
  1733. /* As no DMA to be aborted, call directly user Abort complete callback */
  1734. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1735. /* Call registered Abort Transmit Complete Callback */
  1736. huart->AbortTransmitCpltCallback(huart);
  1737. #else
  1738. /* Call legacy weak Abort Transmit Complete Callback */
  1739. HAL_UART_AbortTransmitCpltCallback(huart);
  1740. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1741. }
  1742. return HAL_OK;
  1743. }
  1744. /**
  1745. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1746. * @param huart UART handle.
  1747. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1748. * This procedure performs following operations :
  1749. * - Disable UART Interrupts (Rx)
  1750. * - Disable the DMA transfer in the peripheral register (if enabled)
  1751. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1752. * - Set handle State to READY
  1753. * - At abort completion, call user abort complete callback
  1754. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1755. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1756. * @retval HAL status
  1757. */
  1758. HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
  1759. {
  1760. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1761. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
  1762. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  1763. /* Disable the UART DMA Rx request if enabled */
  1764. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1765. {
  1766. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1767. /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1768. if (huart->hdmarx != NULL)
  1769. {
  1770. /* Set the UART DMA Abort callback :
  1771. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1772. huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
  1773. /* Abort DMA RX */
  1774. if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1775. {
  1776. /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
  1777. huart->hdmarx->XferAbortCallback(huart->hdmarx);
  1778. }
  1779. }
  1780. else
  1781. {
  1782. /* Reset Rx transfer counter */
  1783. huart->RxXferCount = 0U;
  1784. /* Clear RxISR function pointer */
  1785. huart->pRxBuffPtr = NULL;
  1786. /* Clear the Error flags in the ICR register */
  1787. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1788. /* Discard the received data */
  1789. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1790. /* Restore huart->RxState to Ready */
  1791. huart->RxState = HAL_UART_STATE_READY;
  1792. /* As no DMA to be aborted, call directly user Abort complete callback */
  1793. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1794. /* Call registered Abort Receive Complete Callback */
  1795. huart->AbortReceiveCpltCallback(huart);
  1796. #else
  1797. /* Call legacy weak Abort Receive Complete Callback */
  1798. HAL_UART_AbortReceiveCpltCallback(huart);
  1799. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1800. }
  1801. }
  1802. else
  1803. {
  1804. /* Reset Rx transfer counter */
  1805. huart->RxXferCount = 0U;
  1806. /* Clear RxISR function pointer */
  1807. huart->pRxBuffPtr = NULL;
  1808. /* Clear the Error flags in the ICR register */
  1809. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1810. /* Restore huart->RxState to Ready */
  1811. huart->RxState = HAL_UART_STATE_READY;
  1812. /* As no DMA to be aborted, call directly user Abort complete callback */
  1813. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1814. /* Call registered Abort Receive Complete Callback */
  1815. huart->AbortReceiveCpltCallback(huart);
  1816. #else
  1817. /* Call legacy weak Abort Receive Complete Callback */
  1818. HAL_UART_AbortReceiveCpltCallback(huart);
  1819. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1820. }
  1821. return HAL_OK;
  1822. }
  1823. /**
  1824. * @brief Handle UART interrupt request.
  1825. * @param huart UART handle.
  1826. * @retval None
  1827. */
  1828. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
  1829. {
  1830. uint32_t isrflags = READ_REG(huart->Instance->ISR);
  1831. uint32_t cr1its = READ_REG(huart->Instance->CR1);
  1832. uint32_t cr3its = READ_REG(huart->Instance->CR3);
  1833. uint32_t errorflags;
  1834. uint32_t errorcode;
  1835. /* If no error occurs */
  1836. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
  1837. if (errorflags == 0U)
  1838. {
  1839. /* UART in mode Receiver ---------------------------------------------------*/
  1840. if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
  1841. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1842. || ((cr3its & USART_CR3_RXFTIE) != 0U)))
  1843. {
  1844. if (huart->RxISR != NULL)
  1845. {
  1846. huart->RxISR(huart);
  1847. }
  1848. return;
  1849. }
  1850. }
  1851. /* If some errors occur */
  1852. if ((errorflags != 0U)
  1853. && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
  1854. || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE)) != 0U))))
  1855. {
  1856. /* UART parity error interrupt occurred -------------------------------------*/
  1857. if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
  1858. {
  1859. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
  1860. huart->ErrorCode |= HAL_UART_ERROR_PE;
  1861. }
  1862. /* UART frame error interrupt occurred --------------------------------------*/
  1863. if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1864. {
  1865. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
  1866. huart->ErrorCode |= HAL_UART_ERROR_FE;
  1867. }
  1868. /* UART noise error interrupt occurred --------------------------------------*/
  1869. if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1870. {
  1871. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
  1872. huart->ErrorCode |= HAL_UART_ERROR_NE;
  1873. }
  1874. /* UART Over-Run interrupt occurred -----------------------------------------*/
  1875. if (((isrflags & USART_ISR_ORE) != 0U)
  1876. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) ||
  1877. ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)))
  1878. {
  1879. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
  1880. huart->ErrorCode |= HAL_UART_ERROR_ORE;
  1881. }
  1882. /* UART Receiver Timeout interrupt occurred ---------------------------------*/
  1883. if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
  1884. {
  1885. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
  1886. huart->ErrorCode |= HAL_UART_ERROR_RTO;
  1887. }
  1888. /* Call UART Error Call back function if need be ----------------------------*/
  1889. if (huart->ErrorCode != HAL_UART_ERROR_NONE)
  1890. {
  1891. /* UART in mode Receiver --------------------------------------------------*/
  1892. if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
  1893. && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
  1894. || ((cr3its & USART_CR3_RXFTIE) != 0U)))
  1895. {
  1896. if (huart->RxISR != NULL)
  1897. {
  1898. huart->RxISR(huart);
  1899. }
  1900. }
  1901. /* If Error is to be considered as blocking :
  1902. - Receiver Timeout error in Reception
  1903. - Overrun error in Reception
  1904. - any error occurs in DMA mode reception
  1905. */
  1906. errorcode = huart->ErrorCode;
  1907. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) ||
  1908. ((errorcode & (HAL_UART_ERROR_RTO | HAL_UART_ERROR_ORE)) != 0U))
  1909. {
  1910. /* Blocking error : transfer is aborted
  1911. Set the UART state ready to be able to start again the process,
  1912. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1913. UART_EndRxTransfer(huart);
  1914. /* Disable the UART DMA Rx request if enabled */
  1915. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1916. {
  1917. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1918. /* Abort the UART DMA Rx channel */
  1919. if (huart->hdmarx != NULL)
  1920. {
  1921. /* Set the UART DMA Abort callback :
  1922. will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
  1923. huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
  1924. /* Abort DMA RX */
  1925. if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1926. {
  1927. /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
  1928. huart->hdmarx->XferAbortCallback(huart->hdmarx);
  1929. }
  1930. }
  1931. else
  1932. {
  1933. /* Call user error callback */
  1934. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1935. /*Call registered error callback*/
  1936. huart->ErrorCallback(huart);
  1937. #else
  1938. /*Call legacy weak error callback*/
  1939. HAL_UART_ErrorCallback(huart);
  1940. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1941. }
  1942. }
  1943. else
  1944. {
  1945. /* Call user error callback */
  1946. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1947. /*Call registered error callback*/
  1948. huart->ErrorCallback(huart);
  1949. #else
  1950. /*Call legacy weak error callback*/
  1951. HAL_UART_ErrorCallback(huart);
  1952. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1953. }
  1954. }
  1955. else
  1956. {
  1957. /* Non Blocking error : transfer could go on.
  1958. Error is notified to user through user error callback */
  1959. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1960. /*Call registered error callback*/
  1961. huart->ErrorCallback(huart);
  1962. #else
  1963. /*Call legacy weak error callback*/
  1964. HAL_UART_ErrorCallback(huart);
  1965. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1966. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1967. }
  1968. }
  1969. return;
  1970. } /* End if some error occurs */
  1971. /* UART wakeup from Stop mode interrupt occurred ---------------------------*/
  1972. if (((isrflags & USART_ISR_WUF) != 0U) && ((cr3its & USART_CR3_WUFIE) != 0U))
  1973. {
  1974. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_WUF);
  1975. /* UART Rx state is not reset as a reception process might be ongoing.
  1976. If UART handle state fields need to be reset to READY, this could be done in Wakeup callback */
  1977. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1978. /* Call registered Wakeup Callback */
  1979. huart->WakeupCallback(huart);
  1980. #else
  1981. /* Call legacy weak Wakeup Callback */
  1982. HAL_UARTEx_WakeupCallback(huart);
  1983. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1984. return;
  1985. }
  1986. /* UART in mode Transmitter ------------------------------------------------*/
  1987. if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
  1988. && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
  1989. || ((cr3its & USART_CR3_TXFTIE) != 0U)))
  1990. {
  1991. if (huart->TxISR != NULL)
  1992. {
  1993. huart->TxISR(huart);
  1994. }
  1995. return;
  1996. }
  1997. /* UART in mode Transmitter (transmission end) -----------------------------*/
  1998. if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
  1999. {
  2000. UART_EndTransmit_IT(huart);
  2001. return;
  2002. }
  2003. /* UART TX Fifo Empty occurred ----------------------------------------------*/
  2004. if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
  2005. {
  2006. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2007. /* Call registered Tx Fifo Empty Callback */
  2008. huart->TxFifoEmptyCallback(huart);
  2009. #else
  2010. /* Call legacy weak Tx Fifo Empty Callback */
  2011. HAL_UARTEx_TxFifoEmptyCallback(huart);
  2012. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2013. return;
  2014. }
  2015. /* UART RX Fifo Full occurred ----------------------------------------------*/
  2016. if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
  2017. {
  2018. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2019. /* Call registered Rx Fifo Full Callback */
  2020. huart->RxFifoFullCallback(huart);
  2021. #else
  2022. /* Call legacy weak Rx Fifo Full Callback */
  2023. HAL_UARTEx_RxFifoFullCallback(huart);
  2024. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2025. return;
  2026. }
  2027. }
  2028. /**
  2029. * @brief Tx Transfer completed callback.
  2030. * @param huart UART handle.
  2031. * @retval None
  2032. */
  2033. __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  2034. {
  2035. /* Prevent unused argument(s) compilation warning */
  2036. UNUSED(huart);
  2037. /* NOTE : This function should not be modified, when the callback is needed,
  2038. the HAL_UART_TxCpltCallback can be implemented in the user file.
  2039. */
  2040. }
  2041. /**
  2042. * @brief Tx Half Transfer completed callback.
  2043. * @param huart UART handle.
  2044. * @retval None
  2045. */
  2046. __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
  2047. {
  2048. /* Prevent unused argument(s) compilation warning */
  2049. UNUSED(huart);
  2050. /* NOTE: This function should not be modified, when the callback is needed,
  2051. the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
  2052. */
  2053. }
  2054. /**
  2055. * @brief Rx Transfer completed callback.
  2056. * @param huart UART handle.
  2057. * @retval None
  2058. */
  2059. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  2060. {
  2061. /* Prevent unused argument(s) compilation warning */
  2062. UNUSED(huart);
  2063. /* NOTE : This function should not be modified, when the callback is needed,
  2064. the HAL_UART_RxCpltCallback can be implemented in the user file.
  2065. */
  2066. }
  2067. /**
  2068. * @brief Rx Half Transfer completed callback.
  2069. * @param huart UART handle.
  2070. * @retval None
  2071. */
  2072. __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  2073. {
  2074. /* Prevent unused argument(s) compilation warning */
  2075. UNUSED(huart);
  2076. /* NOTE: This function should not be modified, when the callback is needed,
  2077. the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
  2078. */
  2079. }
  2080. /**
  2081. * @brief UART error callback.
  2082. * @param huart UART handle.
  2083. * @retval None
  2084. */
  2085. __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  2086. {
  2087. /* Prevent unused argument(s) compilation warning */
  2088. UNUSED(huart);
  2089. /* NOTE : This function should not be modified, when the callback is needed,
  2090. the HAL_UART_ErrorCallback can be implemented in the user file.
  2091. */
  2092. }
  2093. /**
  2094. * @brief UART Abort Complete callback.
  2095. * @param huart UART handle.
  2096. * @retval None
  2097. */
  2098. __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart)
  2099. {
  2100. /* Prevent unused argument(s) compilation warning */
  2101. UNUSED(huart);
  2102. /* NOTE : This function should not be modified, when the callback is needed,
  2103. the HAL_UART_AbortCpltCallback can be implemented in the user file.
  2104. */
  2105. }
  2106. /**
  2107. * @brief UART Abort Complete callback.
  2108. * @param huart UART handle.
  2109. * @retval None
  2110. */
  2111. __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart)
  2112. {
  2113. /* Prevent unused argument(s) compilation warning */
  2114. UNUSED(huart);
  2115. /* NOTE : This function should not be modified, when the callback is needed,
  2116. the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
  2117. */
  2118. }
  2119. /**
  2120. * @brief UART Abort Receive Complete callback.
  2121. * @param huart UART handle.
  2122. * @retval None
  2123. */
  2124. __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart)
  2125. {
  2126. /* Prevent unused argument(s) compilation warning */
  2127. UNUSED(huart);
  2128. /* NOTE : This function should not be modified, when the callback is needed,
  2129. the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
  2130. */
  2131. }
  2132. /**
  2133. * @}
  2134. */
  2135. /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
  2136. * @brief UART control functions
  2137. *
  2138. @verbatim
  2139. ===============================================================================
  2140. ##### Peripheral Control functions #####
  2141. ===============================================================================
  2142. [..]
  2143. This subsection provides a set of functions allowing to control the UART.
  2144. (+) HAL_UART_ReceiverTimeout_Config() API allows to configure the receiver timeout value on the fly
  2145. (+) HAL_UART_EnableReceiverTimeout() API enables the receiver timeout feature
  2146. (+) HAL_UART_DisableReceiverTimeout() API disables the receiver timeout feature
  2147. (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
  2148. (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
  2149. (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
  2150. (+) UART_SetConfig() API configures the UART peripheral
  2151. (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
  2152. (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
  2153. (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
  2154. (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
  2155. (+) HAL_LIN_SendBreak() API transmits the break characters
  2156. @endverbatim
  2157. * @{
  2158. */
  2159. /**
  2160. * @brief Update on the fly the receiver timeout value in RTOR register.
  2161. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  2162. * the configuration information for the specified UART module.
  2163. * @param TimeoutValue receiver timeout value in number of baud blocks. The timeout
  2164. * value must be less or equal to 0x0FFFFFFFF.
  2165. * @retval None
  2166. */
  2167. void HAL_UART_ReceiverTimeout_Config(UART_HandleTypeDef *huart, uint32_t TimeoutValue)
  2168. {
  2169. if (!(IS_LPUART_INSTANCE(huart->Instance)))
  2170. {
  2171. assert_param(IS_UART_RECEIVER_TIMEOUT_VALUE(TimeoutValue));
  2172. MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, TimeoutValue);
  2173. }
  2174. }
  2175. /**
  2176. * @brief Enable the UART receiver timeout feature.
  2177. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  2178. * the configuration information for the specified UART module.
  2179. * @retval HAL status
  2180. */
  2181. HAL_StatusTypeDef HAL_UART_EnableReceiverTimeout(UART_HandleTypeDef *huart)
  2182. {
  2183. if (!(IS_LPUART_INSTANCE(huart->Instance)))
  2184. {
  2185. if (huart->gState == HAL_UART_STATE_READY)
  2186. {
  2187. /* Process Locked */
  2188. __HAL_LOCK(huart);
  2189. huart->gState = HAL_UART_STATE_BUSY;
  2190. /* Set the USART RTOEN bit */
  2191. SET_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
  2192. huart->gState = HAL_UART_STATE_READY;
  2193. /* Process Unlocked */
  2194. __HAL_UNLOCK(huart);
  2195. return HAL_OK;
  2196. }
  2197. else
  2198. {
  2199. return HAL_BUSY;
  2200. }
  2201. }
  2202. else
  2203. {
  2204. return HAL_ERROR;
  2205. }
  2206. }
  2207. /**
  2208. * @brief Disable the UART receiver timeout feature.
  2209. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  2210. * the configuration information for the specified UART module.
  2211. * @retval HAL status
  2212. */
  2213. HAL_StatusTypeDef HAL_UART_DisableReceiverTimeout(UART_HandleTypeDef *huart)
  2214. {
  2215. if (!(IS_LPUART_INSTANCE(huart->Instance)))
  2216. {
  2217. if (huart->gState == HAL_UART_STATE_READY)
  2218. {
  2219. /* Process Locked */
  2220. __HAL_LOCK(huart);
  2221. huart->gState = HAL_UART_STATE_BUSY;
  2222. /* Clear the USART RTOEN bit */
  2223. CLEAR_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
  2224. huart->gState = HAL_UART_STATE_READY;
  2225. /* Process Unlocked */
  2226. __HAL_UNLOCK(huart);
  2227. return HAL_OK;
  2228. }
  2229. else
  2230. {
  2231. return HAL_BUSY;
  2232. }
  2233. }
  2234. else
  2235. {
  2236. return HAL_ERROR;
  2237. }
  2238. }
  2239. /**
  2240. * @brief Enable UART in mute mode (does not mean UART enters mute mode;
  2241. * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
  2242. * @param huart UART handle.
  2243. * @retval HAL status
  2244. */
  2245. HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
  2246. {
  2247. __HAL_LOCK(huart);
  2248. huart->gState = HAL_UART_STATE_BUSY;
  2249. /* Enable USART mute mode by setting the MME bit in the CR1 register */
  2250. SET_BIT(huart->Instance->CR1, USART_CR1_MME);
  2251. huart->gState = HAL_UART_STATE_READY;
  2252. return (UART_CheckIdleState(huart));
  2253. }
  2254. /**
  2255. * @brief Disable UART mute mode (does not mean the UART actually exits mute mode
  2256. * as it may not have been in mute mode at this very moment).
  2257. * @param huart UART handle.
  2258. * @retval HAL status
  2259. */
  2260. HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
  2261. {
  2262. __HAL_LOCK(huart);
  2263. huart->gState = HAL_UART_STATE_BUSY;
  2264. /* Disable USART mute mode by clearing the MME bit in the CR1 register */
  2265. CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
  2266. huart->gState = HAL_UART_STATE_READY;
  2267. return (UART_CheckIdleState(huart));
  2268. }
  2269. /**
  2270. * @brief Enter UART mute mode (means UART actually enters mute mode).
  2271. * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
  2272. * @param huart UART handle.
  2273. * @retval None
  2274. */
  2275. void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
  2276. {
  2277. __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
  2278. }
  2279. /**
  2280. * @brief Enable the UART transmitter and disable the UART receiver.
  2281. * @param huart UART handle.
  2282. * @retval HAL status
  2283. */
  2284. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
  2285. {
  2286. __HAL_LOCK(huart);
  2287. huart->gState = HAL_UART_STATE_BUSY;
  2288. /* Clear TE and RE bits */
  2289. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  2290. /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
  2291. SET_BIT(huart->Instance->CR1, USART_CR1_TE);
  2292. huart->gState = HAL_UART_STATE_READY;
  2293. __HAL_UNLOCK(huart);
  2294. return HAL_OK;
  2295. }
  2296. /**
  2297. * @brief Enable the UART receiver and disable the UART transmitter.
  2298. * @param huart UART handle.
  2299. * @retval HAL status.
  2300. */
  2301. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
  2302. {
  2303. __HAL_LOCK(huart);
  2304. huart->gState = HAL_UART_STATE_BUSY;
  2305. /* Clear TE and RE bits */
  2306. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  2307. /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
  2308. SET_BIT(huart->Instance->CR1, USART_CR1_RE);
  2309. huart->gState = HAL_UART_STATE_READY;
  2310. __HAL_UNLOCK(huart);
  2311. return HAL_OK;
  2312. }
  2313. /**
  2314. * @brief Transmit break characters.
  2315. * @param huart UART handle.
  2316. * @retval HAL status
  2317. */
  2318. HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
  2319. {
  2320. /* Check the parameters */
  2321. assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  2322. __HAL_LOCK(huart);
  2323. huart->gState = HAL_UART_STATE_BUSY;
  2324. /* Send break characters */
  2325. __HAL_UART_SEND_REQ(huart, UART_SENDBREAK_REQUEST);
  2326. huart->gState = HAL_UART_STATE_READY;
  2327. __HAL_UNLOCK(huart);
  2328. return HAL_OK;
  2329. }
  2330. /**
  2331. * @}
  2332. */
  2333. /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
  2334. * @brief UART Peripheral State functions
  2335. *
  2336. @verbatim
  2337. ==============================================================================
  2338. ##### Peripheral State and Error functions #####
  2339. ==============================================================================
  2340. [..]
  2341. This subsection provides functions allowing to :
  2342. (+) Return the UART handle state.
  2343. (+) Return the UART handle error code
  2344. @endverbatim
  2345. * @{
  2346. */
  2347. /**
  2348. * @brief Return the UART handle state.
  2349. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  2350. * the configuration information for the specified UART.
  2351. * @retval HAL state
  2352. */
  2353. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
  2354. {
  2355. uint32_t temp1;
  2356. uint32_t temp2;
  2357. temp1 = huart->gState;
  2358. temp2 = huart->RxState;
  2359. return (HAL_UART_StateTypeDef)(temp1 | temp2);
  2360. }
  2361. /**
  2362. * @brief Return the UART handle error code.
  2363. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  2364. * the configuration information for the specified UART.
  2365. * @retval UART Error Code
  2366. */
  2367. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
  2368. {
  2369. return huart->ErrorCode;
  2370. }
  2371. /**
  2372. * @}
  2373. */
  2374. /**
  2375. * @}
  2376. */
  2377. /** @defgroup UART_Private_Functions UART Private Functions
  2378. * @{
  2379. */
  2380. /**
  2381. * @brief Initialize the callbacks to their default values.
  2382. * @param huart UART handle.
  2383. * @retval none
  2384. */
  2385. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2386. void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart)
  2387. {
  2388. /* Init the UART Callback settings */
  2389. huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  2390. huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
  2391. huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  2392. huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
  2393. huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
  2394. huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  2395. huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  2396. huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
  2397. huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */
  2398. huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
  2399. huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
  2400. }
  2401. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2402. /**
  2403. * @brief Configure the UART peripheral.
  2404. * @param huart UART handle.
  2405. * @retval HAL status
  2406. */
  2407. HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
  2408. {
  2409. uint32_t tmpreg;
  2410. uint16_t brrtemp;
  2411. UART_ClockSourceTypeDef clocksource;
  2412. uint32_t usartdiv = 0x00000000U;
  2413. HAL_StatusTypeDef ret = HAL_OK;
  2414. uint32_t lpuart_ker_ck_pres = 0x00000000U;
  2415. PLL2_ClocksTypeDef pll2_clocks;
  2416. PLL3_ClocksTypeDef pll3_clocks;
  2417. uint32_t pclk;
  2418. /* Check the parameters */
  2419. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  2420. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  2421. if (UART_INSTANCE_LOWPOWER(huart))
  2422. {
  2423. assert_param(IS_LPUART_STOPBITS(huart->Init.StopBits));
  2424. }
  2425. else
  2426. {
  2427. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  2428. assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
  2429. }
  2430. assert_param(IS_UART_PARITY(huart->Init.Parity));
  2431. assert_param(IS_UART_MODE(huart->Init.Mode));
  2432. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  2433. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  2434. assert_param(IS_UART_PRESCALER(huart->Init.ClockPrescaler));
  2435. /*-------------------------- USART CR1 Configuration -----------------------*/
  2436. /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
  2437. * the UART Word Length, Parity, Mode and oversampling:
  2438. * set the M bits according to huart->Init.WordLength value
  2439. * set PCE and PS bits according to huart->Init.Parity value
  2440. * set TE and RE bits according to huart->Init.Mode value
  2441. * set OVER8 bit according to huart->Init.OverSampling value */
  2442. tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
  2443. tmpreg |= (uint32_t)huart->FifoMode;
  2444. MODIFY_REG(huart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  2445. /*-------------------------- USART CR2 Configuration -----------------------*/
  2446. /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  2447. * to huart->Init.StopBits value */
  2448. MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  2449. /*-------------------------- USART CR3 Configuration -----------------------*/
  2450. /* Configure
  2451. * - UART HardWare Flow Control: set CTSE and RTSE bits according
  2452. * to huart->Init.HwFlowCtl value
  2453. * - one-bit sampling method versus three samples' majority rule according
  2454. * to huart->Init.OneBitSampling (not applicable to LPUART) */
  2455. tmpreg = (uint32_t)huart->Init.HwFlowCtl;
  2456. if (!(UART_INSTANCE_LOWPOWER(huart)))
  2457. {
  2458. tmpreg |= huart->Init.OneBitSampling;
  2459. }
  2460. MODIFY_REG(huart->Instance->CR3, USART_CR3_FIELDS, tmpreg);
  2461. /*-------------------------- USART PRESC Configuration -----------------------*/
  2462. /* Configure
  2463. * - UART Clock Prescaler : set PRESCALER according to huart->Init.ClockPrescaler value */
  2464. MODIFY_REG(huart->Instance->PRESC, USART_PRESC_PRESCALER, huart->Init.ClockPrescaler);
  2465. /*-------------------------- USART BRR Configuration -----------------------*/
  2466. UART_GETCLOCKSOURCE(huart, clocksource);
  2467. /* Check LPUART instance */
  2468. if (UART_INSTANCE_LOWPOWER(huart))
  2469. {
  2470. /* Retrieve frequency clock */
  2471. switch (clocksource)
  2472. {
  2473. case UART_CLOCKSOURCE_D3PCLK1:
  2474. lpuart_ker_ck_pres = (HAL_RCCEx_GetD3PCLK1Freq() / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
  2475. break;
  2476. case UART_CLOCKSOURCE_PLL2:
  2477. HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
  2478. lpuart_ker_ck_pres = (pll2_clocks.PLL2_Q_Frequency / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
  2479. break;
  2480. case UART_CLOCKSOURCE_PLL3:
  2481. HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
  2482. lpuart_ker_ck_pres = (pll3_clocks.PLL3_Q_Frequency / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
  2483. break;
  2484. case UART_CLOCKSOURCE_HSI:
  2485. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  2486. {
  2487. lpuart_ker_ck_pres = ((uint32_t)(HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)) / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
  2488. }
  2489. else
  2490. {
  2491. lpuart_ker_ck_pres = ((uint32_t) HSI_VALUE / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
  2492. }
  2493. break;
  2494. case UART_CLOCKSOURCE_CSI:
  2495. lpuart_ker_ck_pres = ((uint32_t)CSI_VALUE / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
  2496. break;
  2497. case UART_CLOCKSOURCE_LSE:
  2498. lpuart_ker_ck_pres = ((uint32_t)LSE_VALUE / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
  2499. break;
  2500. default:
  2501. ret = HAL_ERROR;
  2502. break;
  2503. }
  2504. /* if proper clock source reported */
  2505. if (lpuart_ker_ck_pres != 0U)
  2506. {
  2507. /* ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate] */
  2508. if ((lpuart_ker_ck_pres < (3U * huart->Init.BaudRate)) ||
  2509. (lpuart_ker_ck_pres > (4096U * huart->Init.BaudRate)))
  2510. {
  2511. ret = HAL_ERROR;
  2512. }
  2513. else
  2514. {
  2515. switch (clocksource)
  2516. {
  2517. case UART_CLOCKSOURCE_D3PCLK1:
  2518. pclk = HAL_RCCEx_GetD3PCLK1Freq();
  2519. usartdiv = (uint32_t)(UART_DIV_LPUART(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2520. break;
  2521. case UART_CLOCKSOURCE_PLL2:
  2522. HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
  2523. usartdiv = (uint32_t)(UART_DIV_LPUART(pll2_clocks.PLL2_Q_Frequency, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2524. break;
  2525. case UART_CLOCKSOURCE_PLL3:
  2526. HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
  2527. usartdiv = (uint32_t)(UART_DIV_LPUART(pll3_clocks.PLL3_Q_Frequency, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2528. break;
  2529. case UART_CLOCKSOURCE_HSI:
  2530. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  2531. {
  2532. usartdiv = (uint32_t)(UART_DIV_LPUART((uint32_t)(HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)), huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2533. }
  2534. else
  2535. {
  2536. usartdiv = (uint32_t)(UART_DIV_LPUART(HSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2537. }
  2538. break;
  2539. case UART_CLOCKSOURCE_CSI:
  2540. usartdiv = (uint32_t)(UART_DIV_LPUART(CSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2541. break;
  2542. case UART_CLOCKSOURCE_LSE:
  2543. usartdiv = (uint32_t)(UART_DIV_LPUART(LSE_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2544. break;
  2545. default:
  2546. ret = HAL_ERROR;
  2547. break;
  2548. }
  2549. /* It is forbidden to write values lower than 0x300 in the LPUART_BRR register */
  2550. if ((usartdiv >= LPUART_BRR_MIN) && (usartdiv <= LPUART_BRR_MAX))
  2551. {
  2552. huart->Instance->BRR = usartdiv;
  2553. }
  2554. else
  2555. {
  2556. ret = HAL_ERROR;
  2557. }
  2558. } /* if ( (lpuart_ker_ck_pres < (3 * huart->Init.BaudRate) ) || (lpuart_ker_ck_pres > (4096 * huart->Init.BaudRate) )) */
  2559. } /* if (lpuart_ker_ck_pres != 0) */
  2560. }
  2561. /* Check UART Over Sampling to set Baud Rate Register */
  2562. else if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  2563. {
  2564. switch (clocksource)
  2565. {
  2566. case UART_CLOCKSOURCE_D2PCLK1:
  2567. pclk = HAL_RCC_GetPCLK1Freq();
  2568. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2569. break;
  2570. case UART_CLOCKSOURCE_D2PCLK2:
  2571. pclk = HAL_RCC_GetPCLK2Freq();
  2572. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2573. break;
  2574. case UART_CLOCKSOURCE_PLL2:
  2575. HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
  2576. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(pll2_clocks.PLL2_Q_Frequency, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2577. break;
  2578. case UART_CLOCKSOURCE_PLL3:
  2579. HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
  2580. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(pll3_clocks.PLL3_Q_Frequency, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2581. break;
  2582. case UART_CLOCKSOURCE_HSI:
  2583. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  2584. {
  2585. usartdiv = (uint16_t)(UART_DIV_SAMPLING8((HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)), huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2586. }
  2587. else
  2588. {
  2589. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2590. }
  2591. break;
  2592. case UART_CLOCKSOURCE_CSI:
  2593. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(CSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2594. break;
  2595. case UART_CLOCKSOURCE_LSE:
  2596. usartdiv = (uint16_t)(UART_DIV_SAMPLING8((uint32_t)LSE_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2597. break;
  2598. default:
  2599. ret = HAL_ERROR;
  2600. break;
  2601. }
  2602. /* USARTDIV must be greater than or equal to 0d16 */
  2603. if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
  2604. {
  2605. brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
  2606. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
  2607. huart->Instance->BRR = brrtemp;
  2608. }
  2609. else
  2610. {
  2611. ret = HAL_ERROR;
  2612. }
  2613. }
  2614. else
  2615. {
  2616. switch (clocksource)
  2617. {
  2618. case UART_CLOCKSOURCE_D2PCLK1:
  2619. pclk = HAL_RCC_GetPCLK1Freq();
  2620. usartdiv = (uint16_t)(UART_DIV_SAMPLING16(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2621. break;
  2622. case UART_CLOCKSOURCE_D2PCLK2:
  2623. pclk = HAL_RCC_GetPCLK2Freq();
  2624. usartdiv = (uint16_t)(UART_DIV_SAMPLING16(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2625. break;
  2626. case UART_CLOCKSOURCE_PLL2:
  2627. HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
  2628. usartdiv = (uint16_t)(UART_DIV_SAMPLING16(pll2_clocks.PLL2_Q_Frequency, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2629. break;
  2630. case UART_CLOCKSOURCE_PLL3:
  2631. HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
  2632. usartdiv = (uint16_t)(UART_DIV_SAMPLING16(pll3_clocks.PLL3_Q_Frequency, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2633. break;
  2634. case UART_CLOCKSOURCE_HSI:
  2635. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  2636. {
  2637. usartdiv = (uint16_t)(UART_DIV_SAMPLING16((HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)), huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2638. }
  2639. else
  2640. {
  2641. usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2642. }
  2643. break;
  2644. case UART_CLOCKSOURCE_CSI:
  2645. usartdiv = (uint16_t)(UART_DIV_SAMPLING16(CSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2646. break;
  2647. case UART_CLOCKSOURCE_LSE:
  2648. usartdiv = (uint16_t)(UART_DIV_SAMPLING16((uint32_t)LSE_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
  2649. break;
  2650. default:
  2651. ret = HAL_ERROR;
  2652. break;
  2653. }
  2654. /* USARTDIV must be greater than or equal to 0d16 */
  2655. if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
  2656. {
  2657. huart->Instance->BRR = usartdiv;
  2658. }
  2659. else
  2660. {
  2661. ret = HAL_ERROR;
  2662. }
  2663. }
  2664. /* Initialize the number of data to process during RX/TX ISR execution */
  2665. huart->NbTxDataToProcess = 1;
  2666. huart->NbRxDataToProcess = 1;
  2667. /* Clear ISR function pointers */
  2668. huart->RxISR = NULL;
  2669. huart->TxISR = NULL;
  2670. return ret;
  2671. }
  2672. /**
  2673. * @brief Configure the UART peripheral advanced features.
  2674. * @param huart UART handle.
  2675. * @retval None
  2676. */
  2677. void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
  2678. {
  2679. /* Check whether the set of advanced features to configure is properly set */
  2680. assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
  2681. /* if required, configure TX pin active level inversion */
  2682. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
  2683. {
  2684. assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
  2685. MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
  2686. }
  2687. /* if required, configure RX pin active level inversion */
  2688. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
  2689. {
  2690. assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
  2691. MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
  2692. }
  2693. /* if required, configure data inversion */
  2694. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
  2695. {
  2696. assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
  2697. MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
  2698. }
  2699. /* if required, configure RX/TX pins swap */
  2700. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
  2701. {
  2702. assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
  2703. MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
  2704. }
  2705. /* if required, configure RX overrun detection disabling */
  2706. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  2707. {
  2708. assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
  2709. MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
  2710. }
  2711. /* if required, configure DMA disabling on reception error */
  2712. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
  2713. {
  2714. assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
  2715. MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
  2716. }
  2717. /* if required, configure auto Baud rate detection scheme */
  2718. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
  2719. {
  2720. assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
  2721. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
  2722. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
  2723. /* set auto Baudrate detection parameters if detection is enabled */
  2724. if (huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
  2725. {
  2726. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
  2727. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
  2728. }
  2729. }
  2730. /* if required, configure MSB first on communication line */
  2731. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
  2732. {
  2733. assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
  2734. MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
  2735. }
  2736. }
  2737. /**
  2738. * @brief Check the UART Idle State.
  2739. * @param huart UART handle.
  2740. * @retval HAL status
  2741. */
  2742. HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
  2743. {
  2744. uint32_t tickstart;
  2745. /* Initialize the UART ErrorCode */
  2746. huart->ErrorCode = HAL_UART_ERROR_NONE;
  2747. /* Init tickstart for timeout managment*/
  2748. tickstart = HAL_GetTick();
  2749. /* Check if the Transmitter is enabled */
  2750. if ((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  2751. {
  2752. /* Wait until TEACK flag is set */
  2753. if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  2754. {
  2755. /* Timeout occurred */
  2756. return HAL_TIMEOUT;
  2757. }
  2758. }
  2759. /* Check if the Receiver is enabled */
  2760. if ((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  2761. {
  2762. /* Wait until REACK flag is set */
  2763. if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  2764. {
  2765. /* Timeout occurred */
  2766. return HAL_TIMEOUT;
  2767. }
  2768. }
  2769. /* Initialize the UART State */
  2770. huart->gState = HAL_UART_STATE_READY;
  2771. huart->RxState = HAL_UART_STATE_READY;
  2772. __HAL_UNLOCK(huart);
  2773. return HAL_OK;
  2774. }
  2775. /**
  2776. * @brief Handle UART Communication Timeout.
  2777. * @param huart UART handle.
  2778. * @param Flag Specifies the UART flag to check
  2779. * @param Status Flag status (SET or RESET)
  2780. * @param Tickstart Tick start value
  2781. * @param Timeout Timeout duration
  2782. * @retval HAL status
  2783. */
  2784. HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
  2785. uint32_t Tickstart, uint32_t Timeout)
  2786. {
  2787. /* Wait until flag is set */
  2788. while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  2789. {
  2790. /* Check for the Timeout */
  2791. if (Timeout != HAL_MAX_DELAY)
  2792. {
  2793. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  2794. {
  2795. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  2796. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
  2797. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2798. huart->gState = HAL_UART_STATE_READY;
  2799. huart->RxState = HAL_UART_STATE_READY;
  2800. __HAL_UNLOCK(huart);
  2801. return HAL_TIMEOUT;
  2802. }
  2803. if (READ_BIT(huart->Instance->CR1, USART_CR1_RE) != 0U)
  2804. {
  2805. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RTOF) == SET)
  2806. {
  2807. /* Clear Receiver Timeout flag*/
  2808. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
  2809. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  2810. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
  2811. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2812. huart->gState = HAL_UART_STATE_READY;
  2813. huart->RxState = HAL_UART_STATE_READY;
  2814. huart->ErrorCode = HAL_UART_ERROR_RTO;
  2815. /* Process Unlocked */
  2816. __HAL_UNLOCK(huart);
  2817. return HAL_TIMEOUT;
  2818. }
  2819. }
  2820. }
  2821. }
  2822. return HAL_OK;
  2823. }
  2824. /**
  2825. * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
  2826. * @param huart UART handle.
  2827. * @retval None
  2828. */
  2829. static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
  2830. {
  2831. /* Disable TXEIE, TCIE, TXFT interrupts */
  2832. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  2833. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_TXFTIE));
  2834. /* At end of Tx process, restore huart->gState to Ready */
  2835. huart->gState = HAL_UART_STATE_READY;
  2836. }
  2837. /**
  2838. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  2839. * @param huart UART handle.
  2840. * @retval None
  2841. */
  2842. static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
  2843. {
  2844. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  2845. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  2846. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  2847. /* At end of Rx process, restore huart->RxState to Ready */
  2848. huart->RxState = HAL_UART_STATE_READY;
  2849. /* Reset RxIsr function pointer */
  2850. huart->RxISR = NULL;
  2851. }
  2852. /**
  2853. * @brief DMA UART transmit process complete callback.
  2854. * @param hdma DMA handle.
  2855. * @retval None
  2856. */
  2857. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2858. {
  2859. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  2860. /* DMA Normal mode */
  2861. if (hdma->Init.Mode != DMA_CIRCULAR)
  2862. {
  2863. huart->TxXferCount = 0U;
  2864. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  2865. in the UART CR3 register */
  2866. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  2867. /* Enable the UART Transmit Complete Interrupt */
  2868. SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  2869. }
  2870. /* DMA Circular mode */
  2871. else
  2872. {
  2873. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2874. /*Call registered Tx complete callback*/
  2875. huart->TxCpltCallback(huart);
  2876. #else
  2877. /*Call legacy weak Tx complete callback*/
  2878. HAL_UART_TxCpltCallback(huart);
  2879. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2880. }
  2881. }
  2882. /**
  2883. * @brief DMA UART transmit process half complete callback.
  2884. * @param hdma DMA handle.
  2885. * @retval None
  2886. */
  2887. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  2888. {
  2889. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  2890. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2891. /*Call registered Tx Half complete callback*/
  2892. huart->TxHalfCpltCallback(huart);
  2893. #else
  2894. /*Call legacy weak Tx Half complete callback*/
  2895. HAL_UART_TxHalfCpltCallback(huart);
  2896. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2897. }
  2898. /**
  2899. * @brief DMA UART receive process complete callback.
  2900. * @param hdma DMA handle.
  2901. * @retval None
  2902. */
  2903. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2904. {
  2905. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  2906. /* DMA Normal mode */
  2907. if (hdma->Init.Mode != DMA_CIRCULAR)
  2908. {
  2909. huart->RxXferCount = 0U;
  2910. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  2911. CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  2912. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2913. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  2914. in the UART CR3 register */
  2915. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  2916. /* At end of Rx process, restore huart->RxState to Ready */
  2917. huart->RxState = HAL_UART_STATE_READY;
  2918. }
  2919. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2920. /*Call registered Rx complete callback*/
  2921. huart->RxCpltCallback(huart);
  2922. #else
  2923. /*Call legacy weak Rx complete callback*/
  2924. HAL_UART_RxCpltCallback(huart);
  2925. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2926. }
  2927. /**
  2928. * @brief DMA UART receive process half complete callback.
  2929. * @param hdma DMA handle.
  2930. * @retval None
  2931. */
  2932. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  2933. {
  2934. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  2935. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2936. /*Call registered Rx Half complete callback*/
  2937. huart->RxHalfCpltCallback(huart);
  2938. #else
  2939. /*Call legacy weak Rx Half complete callback*/
  2940. HAL_UART_RxHalfCpltCallback(huart);
  2941. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2942. }
  2943. /**
  2944. * @brief DMA UART communication error callback.
  2945. * @param hdma DMA handle.
  2946. * @retval None
  2947. */
  2948. static void UART_DMAError(DMA_HandleTypeDef *hdma)
  2949. {
  2950. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  2951. const HAL_UART_StateTypeDef gstate = huart->gState;
  2952. const HAL_UART_StateTypeDef rxstate = huart->RxState;
  2953. /* Stop UART DMA Tx request if ongoing */
  2954. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
  2955. (gstate == HAL_UART_STATE_BUSY_TX))
  2956. {
  2957. huart->TxXferCount = 0U;
  2958. UART_EndTxTransfer(huart);
  2959. }
  2960. /* Stop UART DMA Rx request if ongoing */
  2961. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
  2962. (rxstate == HAL_UART_STATE_BUSY_RX))
  2963. {
  2964. huart->RxXferCount = 0U;
  2965. UART_EndRxTransfer(huart);
  2966. }
  2967. huart->ErrorCode |= HAL_UART_ERROR_DMA;
  2968. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2969. /*Call registered error callback*/
  2970. huart->ErrorCallback(huart);
  2971. #else
  2972. /*Call legacy weak error callback*/
  2973. HAL_UART_ErrorCallback(huart);
  2974. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2975. }
  2976. /**
  2977. * @brief DMA UART communication abort callback, when initiated by HAL services on Error
  2978. * (To be called at end of DMA Abort procedure following error occurrence).
  2979. * @param hdma DMA handle.
  2980. * @retval None
  2981. */
  2982. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2983. {
  2984. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  2985. huart->RxXferCount = 0U;
  2986. huart->TxXferCount = 0U;
  2987. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2988. /*Call registered error callback*/
  2989. huart->ErrorCallback(huart);
  2990. #else
  2991. /*Call legacy weak error callback*/
  2992. HAL_UART_ErrorCallback(huart);
  2993. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2994. }
  2995. /**
  2996. * @brief DMA UART Tx communication abort callback, when initiated by user
  2997. * (To be called at end of DMA Tx Abort procedure following user abort request).
  2998. * @note When this callback is executed, User Abort complete call back is called only if no
  2999. * Abort still ongoing for Rx DMA Handle.
  3000. * @param hdma DMA handle.
  3001. * @retval None
  3002. */
  3003. static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  3004. {
  3005. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3006. huart->hdmatx->XferAbortCallback = NULL;
  3007. /* Check if an Abort process is still ongoing */
  3008. if (huart->hdmarx != NULL)
  3009. {
  3010. if (huart->hdmarx->XferAbortCallback != NULL)
  3011. {
  3012. return;
  3013. }
  3014. }
  3015. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  3016. huart->TxXferCount = 0U;
  3017. huart->RxXferCount = 0U;
  3018. /* Reset errorCode */
  3019. huart->ErrorCode = HAL_UART_ERROR_NONE;
  3020. /* Clear the Error flags in the ICR register */
  3021. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  3022. /* Flush the whole TX FIFO (if needed) */
  3023. if (huart->FifoMode == UART_FIFOMODE_ENABLE)
  3024. {
  3025. __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
  3026. }
  3027. /* Restore huart->gState and huart->RxState to Ready */
  3028. huart->gState = HAL_UART_STATE_READY;
  3029. huart->RxState = HAL_UART_STATE_READY;
  3030. /* Call user Abort complete callback */
  3031. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3032. /* Call registered Abort complete callback */
  3033. huart->AbortCpltCallback(huart);
  3034. #else
  3035. /* Call legacy weak Abort complete callback */
  3036. HAL_UART_AbortCpltCallback(huart);
  3037. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3038. }
  3039. /**
  3040. * @brief DMA UART Rx communication abort callback, when initiated by user
  3041. * (To be called at end of DMA Rx Abort procedure following user abort request).
  3042. * @note When this callback is executed, User Abort complete call back is called only if no
  3043. * Abort still ongoing for Tx DMA Handle.
  3044. * @param hdma DMA handle.
  3045. * @retval None
  3046. */
  3047. static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  3048. {
  3049. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3050. huart->hdmarx->XferAbortCallback = NULL;
  3051. /* Check if an Abort process is still ongoing */
  3052. if (huart->hdmatx != NULL)
  3053. {
  3054. if (huart->hdmatx->XferAbortCallback != NULL)
  3055. {
  3056. return;
  3057. }
  3058. }
  3059. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  3060. huart->TxXferCount = 0U;
  3061. huart->RxXferCount = 0U;
  3062. /* Reset errorCode */
  3063. huart->ErrorCode = HAL_UART_ERROR_NONE;
  3064. /* Clear the Error flags in the ICR register */
  3065. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  3066. /* Discard the received data */
  3067. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3068. /* Restore huart->gState and huart->RxState to Ready */
  3069. huart->gState = HAL_UART_STATE_READY;
  3070. huart->RxState = HAL_UART_STATE_READY;
  3071. /* Call user Abort complete callback */
  3072. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3073. /* Call registered Abort complete callback */
  3074. huart->AbortCpltCallback(huart);
  3075. #else
  3076. /* Call legacy weak Abort complete callback */
  3077. HAL_UART_AbortCpltCallback(huart);
  3078. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3079. }
  3080. /**
  3081. * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
  3082. * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
  3083. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  3084. * and leads to user Tx Abort Complete callback execution).
  3085. * @param hdma DMA handle.
  3086. * @retval None
  3087. */
  3088. static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  3089. {
  3090. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3091. huart->TxXferCount = 0U;
  3092. /* Flush the whole TX FIFO (if needed) */
  3093. if (huart->FifoMode == UART_FIFOMODE_ENABLE)
  3094. {
  3095. __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
  3096. }
  3097. /* Restore huart->gState to Ready */
  3098. huart->gState = HAL_UART_STATE_READY;
  3099. /* Call user Abort complete callback */
  3100. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3101. /* Call registered Abort Transmit Complete Callback */
  3102. huart->AbortTransmitCpltCallback(huart);
  3103. #else
  3104. /* Call legacy weak Abort Transmit Complete Callback */
  3105. HAL_UART_AbortTransmitCpltCallback(huart);
  3106. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3107. }
  3108. /**
  3109. * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
  3110. * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
  3111. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  3112. * and leads to user Rx Abort Complete callback execution).
  3113. * @param hdma DMA handle.
  3114. * @retval None
  3115. */
  3116. static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  3117. {
  3118. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3119. huart->RxXferCount = 0U;
  3120. /* Clear the Error flags in the ICR register */
  3121. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  3122. /* Discard the received data */
  3123. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3124. /* Restore huart->RxState to Ready */
  3125. huart->RxState = HAL_UART_STATE_READY;
  3126. /* Call user Abort complete callback */
  3127. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3128. /* Call registered Abort Receive Complete Callback */
  3129. huart->AbortReceiveCpltCallback(huart);
  3130. #else
  3131. /* Call legacy weak Abort Receive Complete Callback */
  3132. HAL_UART_AbortReceiveCpltCallback(huart);
  3133. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3134. }
  3135. /**
  3136. * @brief TX interrrupt handler for 7 or 8 bits data word length .
  3137. * @note Function is called under interruption only, once
  3138. * interruptions have been enabled by HAL_UART_Transmit_IT().
  3139. * @param huart UART handle.
  3140. * @retval None
  3141. */
  3142. static void UART_TxISR_8BIT(UART_HandleTypeDef *huart)
  3143. {
  3144. /* Check that a Tx process is ongoing */
  3145. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  3146. {
  3147. if (huart->TxXferCount == 0U)
  3148. {
  3149. /* Disable the UART Transmit Data Register Empty Interrupt */
  3150. CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  3151. /* Enable the UART Transmit Complete Interrupt */
  3152. SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3153. }
  3154. else
  3155. {
  3156. huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
  3157. huart->pTxBuffPtr++;
  3158. huart->TxXferCount--;
  3159. }
  3160. }
  3161. }
  3162. /**
  3163. * @brief TX interrrupt handler for 9 bits data word length.
  3164. * @note Function is called under interruption only, once
  3165. * interruptions have been enabled by HAL_UART_Transmit_IT().
  3166. * @param huart UART handle.
  3167. * @retval None
  3168. */
  3169. static void UART_TxISR_16BIT(UART_HandleTypeDef *huart)
  3170. {
  3171. uint16_t *tmp;
  3172. /* Check that a Tx process is ongoing */
  3173. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  3174. {
  3175. if (huart->TxXferCount == 0U)
  3176. {
  3177. /* Disable the UART Transmit Data Register Empty Interrupt */
  3178. CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  3179. /* Enable the UART Transmit Complete Interrupt */
  3180. SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3181. }
  3182. else
  3183. {
  3184. tmp = (uint16_t *) huart->pTxBuffPtr;
  3185. huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
  3186. huart->pTxBuffPtr += 2U;
  3187. huart->TxXferCount--;
  3188. }
  3189. }
  3190. }
  3191. /**
  3192. * @brief TX interrrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
  3193. * @note Function is called under interruption only, once
  3194. * interruptions have been enabled by HAL_UART_Transmit_IT().
  3195. * @param huart UART handle.
  3196. * @retval None
  3197. */
  3198. static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
  3199. {
  3200. uint16_t nb_tx_data;
  3201. /* Check that a Tx process is ongoing */
  3202. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  3203. {
  3204. for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
  3205. {
  3206. if (huart->TxXferCount == 0U)
  3207. {
  3208. /* Disable the TX FIFO threshold interrupt */
  3209. CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
  3210. /* Enable the UART Transmit Complete Interrupt */
  3211. SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3212. break; /* force exit loop */
  3213. }
  3214. else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
  3215. {
  3216. huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
  3217. huart->pTxBuffPtr++;
  3218. huart->TxXferCount--;
  3219. }
  3220. else
  3221. {
  3222. /* Nothing to do */
  3223. }
  3224. }
  3225. }
  3226. }
  3227. /**
  3228. * @brief TX interrrupt handler for 9 bits data word length and FIFO mode is enabled.
  3229. * @note Function is called under interruption only, once
  3230. * interruptions have been enabled by HAL_UART_Transmit_IT().
  3231. * @param huart UART handle.
  3232. * @retval None
  3233. */
  3234. static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
  3235. {
  3236. uint16_t *tmp;
  3237. uint16_t nb_tx_data;
  3238. /* Check that a Tx process is ongoing */
  3239. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  3240. {
  3241. for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
  3242. {
  3243. if (huart->TxXferCount == 0U)
  3244. {
  3245. /* Disable the TX FIFO threshold interrupt */
  3246. CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
  3247. /* Enable the UART Transmit Complete Interrupt */
  3248. SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3249. break; /* force exit loop */
  3250. }
  3251. else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
  3252. {
  3253. tmp = (uint16_t *) huart->pTxBuffPtr;
  3254. huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
  3255. huart->pTxBuffPtr += 2U;
  3256. huart->TxXferCount--;
  3257. }
  3258. else
  3259. {
  3260. /* Nothing to do */
  3261. }
  3262. }
  3263. }
  3264. }
  3265. /**
  3266. * @brief Wrap up transmission in non-blocking mode.
  3267. * @param huart pointer to a UART_HandleTypeDef structure that contains
  3268. * the configuration information for the specified UART module.
  3269. * @retval None
  3270. */
  3271. static void UART_EndTransmit_IT(UART_HandleTypeDef *huart)
  3272. {
  3273. /* Disable the UART Transmit Complete Interrupt */
  3274. CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3275. /* Tx process is ended, restore huart->gState to Ready */
  3276. huart->gState = HAL_UART_STATE_READY;
  3277. /* Cleat TxISR function pointer */
  3278. huart->TxISR = NULL;
  3279. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3280. /*Call registered Tx complete callback*/
  3281. huart->TxCpltCallback(huart);
  3282. #else
  3283. /*Call legacy weak Tx complete callback*/
  3284. HAL_UART_TxCpltCallback(huart);
  3285. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3286. }
  3287. /**
  3288. * @brief RX interrrupt handler for 7 or 8 bits data word length .
  3289. * @param huart UART handle.
  3290. * @retval None
  3291. */
  3292. static void UART_RxISR_8BIT(UART_HandleTypeDef *huart)
  3293. {
  3294. uint16_t uhMask = huart->Mask;
  3295. uint16_t uhdata;
  3296. /* Check that a Rx process is ongoing */
  3297. if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  3298. {
  3299. uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
  3300. *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
  3301. huart->pRxBuffPtr++;
  3302. huart->RxXferCount--;
  3303. if (huart->RxXferCount == 0U)
  3304. {
  3305. /* Disable the UART Parity Error Interrupt and RXNE interrupts */
  3306. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  3307. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  3308. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  3309. /* Rx process is completed, restore huart->RxState to Ready */
  3310. huart->RxState = HAL_UART_STATE_READY;
  3311. /* Clear RxISR function pointer */
  3312. huart->RxISR = NULL;
  3313. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3314. /*Call registered Rx complete callback*/
  3315. huart->RxCpltCallback(huart);
  3316. #else
  3317. /*Call legacy weak Rx complete callback*/
  3318. HAL_UART_RxCpltCallback(huart);
  3319. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3320. }
  3321. }
  3322. else
  3323. {
  3324. /* Clear RXNE interrupt flag */
  3325. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3326. }
  3327. }
  3328. /**
  3329. * @brief RX interrrupt handler for 9 bits data word length .
  3330. * @note Function is called under interruption only, once
  3331. * interruptions have been enabled by HAL_UART_Receive_IT()
  3332. * @param huart UART handle.
  3333. * @retval None
  3334. */
  3335. static void UART_RxISR_16BIT(UART_HandleTypeDef *huart)
  3336. {
  3337. uint16_t *tmp;
  3338. uint16_t uhMask = huart->Mask;
  3339. uint16_t uhdata;
  3340. /* Check that a Rx process is ongoing */
  3341. if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  3342. {
  3343. uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
  3344. tmp = (uint16_t *) huart->pRxBuffPtr ;
  3345. *tmp = (uint16_t)(uhdata & uhMask);
  3346. huart->pRxBuffPtr += 2U;
  3347. huart->RxXferCount--;
  3348. if (huart->RxXferCount == 0U)
  3349. {
  3350. /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
  3351. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  3352. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  3353. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  3354. /* Rx process is completed, restore huart->RxState to Ready */
  3355. huart->RxState = HAL_UART_STATE_READY;
  3356. /* Clear RxISR function pointer */
  3357. huart->RxISR = NULL;
  3358. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3359. /*Call registered Rx complete callback*/
  3360. huart->RxCpltCallback(huart);
  3361. #else
  3362. /*Call legacy weak Rx complete callback*/
  3363. HAL_UART_RxCpltCallback(huart);
  3364. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3365. }
  3366. }
  3367. else
  3368. {
  3369. /* Clear RXNE interrupt flag */
  3370. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3371. }
  3372. }
  3373. /**
  3374. * @brief RX interrrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
  3375. * @note Function is called under interruption only, once
  3376. * interruptions have been enabled by HAL_UART_Receive_IT()
  3377. * @param huart UART handle.
  3378. * @retval None
  3379. */
  3380. static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
  3381. {
  3382. uint16_t uhMask = huart->Mask;
  3383. uint16_t uhdata;
  3384. uint16_t nb_rx_data;
  3385. uint16_t rxdatacount;
  3386. /* Check that a Rx process is ongoing */
  3387. if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  3388. {
  3389. for (nb_rx_data = huart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
  3390. {
  3391. uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
  3392. *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
  3393. huart->pRxBuffPtr++;
  3394. huart->RxXferCount--;
  3395. if (huart->RxXferCount == 0U)
  3396. {
  3397. /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
  3398. CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  3399. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
  3400. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  3401. /* Rx process is completed, restore huart->RxState to Ready */
  3402. huart->RxState = HAL_UART_STATE_READY;
  3403. /* Clear RxISR function pointer */
  3404. huart->RxISR = NULL;
  3405. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3406. /*Call registered Rx complete callback*/
  3407. huart->RxCpltCallback(huart);
  3408. #else
  3409. /*Call legacy weak Rx complete callback*/
  3410. HAL_UART_RxCpltCallback(huart);
  3411. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3412. }
  3413. }
  3414. /* When remaining number of bytes to receive is less than the RX FIFO
  3415. threshold, next incoming frames are processed as if FIFO mode was
  3416. disabled (i.e. one interrupt per received frame).
  3417. */
  3418. rxdatacount = huart->RxXferCount;
  3419. if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
  3420. {
  3421. /* Disable the UART RXFT interrupt*/
  3422. CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
  3423. /* Update the RxISR function pointer */
  3424. huart->RxISR = UART_RxISR_8BIT;
  3425. /* Enable the UART Data Register Not Empty interrupt */
  3426. SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  3427. }
  3428. }
  3429. else
  3430. {
  3431. /* Clear RXNE interrupt flag */
  3432. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3433. }
  3434. }
  3435. /**
  3436. * @brief RX interrrupt handler for 9 bits data word length and FIFO mode is enabled.
  3437. * @note Function is called under interruption only, once
  3438. * interruptions have been enabled by HAL_UART_Receive_IT()
  3439. * @param huart UART handle.
  3440. * @retval None
  3441. */
  3442. static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
  3443. {
  3444. uint16_t *tmp;
  3445. uint16_t uhMask = huart->Mask;
  3446. uint16_t uhdata;
  3447. uint16_t nb_rx_data;
  3448. uint16_t rxdatacount;
  3449. /* Check that a Rx process is ongoing */
  3450. if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  3451. {
  3452. for (nb_rx_data = huart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
  3453. {
  3454. uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
  3455. tmp = (uint16_t *) huart->pRxBuffPtr ;
  3456. *tmp = (uint16_t)(uhdata & uhMask);
  3457. huart->pRxBuffPtr += 2U;
  3458. huart->RxXferCount--;
  3459. if (huart->RxXferCount == 0U)
  3460. {
  3461. /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
  3462. CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  3463. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
  3464. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  3465. /* Rx process is completed, restore huart->RxState to Ready */
  3466. huart->RxState = HAL_UART_STATE_READY;
  3467. /* Clear RxISR function pointer */
  3468. huart->RxISR = NULL;
  3469. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3470. /*Call registered Rx complete callback*/
  3471. huart->RxCpltCallback(huart);
  3472. #else
  3473. /*Call legacy weak Rx complete callback*/
  3474. HAL_UART_RxCpltCallback(huart);
  3475. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3476. }
  3477. }
  3478. /* When remaining number of bytes to receive is less than the RX FIFO
  3479. threshold, next incoming frames are processed as if FIFO mode was
  3480. disabled (i.e. one interrupt per received frame).
  3481. */
  3482. rxdatacount = huart->RxXferCount;
  3483. if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
  3484. {
  3485. /* Disable the UART RXFT interrupt*/
  3486. CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
  3487. /* Update the RxISR function pointer */
  3488. huart->RxISR = UART_RxISR_16BIT;
  3489. /* Enable the UART Data Register Not Empty interrupt */
  3490. SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  3491. }
  3492. }
  3493. else
  3494. {
  3495. /* Clear RXNE interrupt flag */
  3496. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3497. }
  3498. }
  3499. /**
  3500. * @}
  3501. */
  3502. #endif /* HAL_UART_MODULE_ENABLED */
  3503. /**
  3504. * @}
  3505. */
  3506. /**
  3507. * @}
  3508. */
  3509. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/