No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

3749 líneas
130 KiB

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