Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3798 lines
130 KiB

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