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.
 
 
 

2175 lines
69 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_uart.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 14-April-2017
  7. * @brief UART HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State and Errors functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..]
  20. The UART HAL driver can be used as follows:
  21. (#) Declare a UART_HandleTypeDef handle structure.
  22. (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
  23. (##) Enable the USARTx interface clock.
  24. (##) UART pins configuration:
  25. (+++) Enable the clock for the UART GPIOs.
  26. (+++) Configure these UART pins as alternate function pull-up.
  27. (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
  28. and HAL_UART_Receive_IT() APIs):
  29. (+++) Configure the USARTx interrupt priority.
  30. (+++) Enable the NVIC USART IRQ handle.
  31. (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
  32. and HAL_UART_Receive_DMA() APIs):
  33. (+++) Declare a DMA handle structure for the Tx/Rx stream.
  34. (+++) Enable the DMAx interface clock.
  35. (+++) Configure the declared DMA handle structure with the required
  36. Tx/Rx parameters.
  37. (+++) Configure the DMA Tx/Rx Stream.
  38. (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
  39. (+++) Configure the priority and enable the NVIC for the transfer complete
  40. interrupt on the DMA Tx/Rx Stream.
  41. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  42. flow control and Mode(Receiver/Transmitter) in the Init structure.
  43. (#) For the UART asynchronous mode, initialize the UART registers by calling
  44. the HAL_UART_Init() API.
  45. (#) For the UART Half duplex mode, initialize the UART registers by calling
  46. the HAL_HalfDuplex_Init() API.
  47. (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
  48. (#) For the Multi-Processor mode, initialize the UART registers by calling
  49. the HAL_MultiProcessor_Init() API.
  50. [..]
  51. (@) The specific UART interrupts (Transmission complete interrupt,
  52. RXNE interrupt and Error Interrupts) will be managed using the macros
  53. __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit
  54. and receive process.
  55. [..]
  56. (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the
  57. low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customized
  58. HAL_UART_MspInit() API.
  59. [..]
  60. Three operation modes are available within this driver :
  61. *** Polling mode IO operation ***
  62. =================================
  63. [..]
  64. (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
  65. (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
  66. *** Interrupt mode IO operation ***
  67. ===================================
  68. [..]
  69. (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
  70. (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
  71. add his own code by customization of function pointer HAL_UART_TxCpltCallback
  72. (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
  73. (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
  74. add his own code by customization of function pointer HAL_UART_RxCpltCallback
  75. (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
  76. add his own code by customization of function pointer HAL_UART_ErrorCallback
  77. *** DMA mode IO operation ***
  78. ==============================
  79. [..]
  80. (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
  81. (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
  82. add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
  83. (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
  84. add his own code by customization of function pointer HAL_UART_TxCpltCallback
  85. (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
  86. (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
  87. add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
  88. (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
  89. add his own code by customization of function pointer HAL_UART_RxCpltCallback
  90. (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
  91. add his own code by customization of function pointer HAL_UART_ErrorCallback
  92. (+) Pause the DMA Transfer using HAL_UART_DMAPause()
  93. (+) Resume the DMA Transfer using HAL_UART_DMAResume()
  94. (+) Stop the DMA Transfer using HAL_UART_DMAStop()
  95. *** UART HAL driver macros list ***
  96. =============================================
  97. [..]
  98. Below the list of most used macros in UART HAL driver.
  99. (+) __HAL_UART_ENABLE: Enable the UART peripheral
  100. (+) __HAL_UART_DISABLE: Disable the UART peripheral
  101. (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
  102. (+) __HAL_UART_CLEAR_IT : Clears the specified UART ISR flag
  103. (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
  104. (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
  105. (+) __HAL_UART_GET_IT_SOURCE: Check whether the specified UART interrupt has occurred or not
  106. [..]
  107. (@) You can refer to the UART HAL driver header file for more useful macros
  108. @endverbatim
  109. ******************************************************************************
  110. * @attention
  111. *
  112. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  113. *
  114. * Redistribution and use in source and binary forms, with or without modification,
  115. * are permitted provided that the following conditions are met:
  116. * 1. Redistributions of source code must retain the above copyright notice,
  117. * this list of conditions and the following disclaimer.
  118. * 2. Redistributions in binary form must reproduce the above copyright notice,
  119. * this list of conditions and the following disclaimer in the documentation
  120. * and/or other materials provided with the distribution.
  121. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  122. * may be used to endorse or promote products derived from this software
  123. * without specific prior written permission.
  124. *
  125. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  126. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  127. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  128. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  129. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  130. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  131. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  132. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  133. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  134. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  135. *
  136. ******************************************************************************
  137. */
  138. /* Includes ------------------------------------------------------------------*/
  139. #include "stm32f7xx_hal.h"
  140. /** @addtogroup STM32F7xx_HAL_Driver
  141. * @{
  142. */
  143. /** @defgroup UART UART
  144. * @brief HAL UART module driver
  145. * @{
  146. */
  147. #ifdef HAL_UART_MODULE_ENABLED
  148. /* Private typedef -----------------------------------------------------------*/
  149. /* Private define ------------------------------------------------------------*/
  150. /** @defgroup UART_Private_Constants UART Private Constants
  151. * @{
  152. */
  153. #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  154. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
  155. /**
  156. * @}
  157. */
  158. /* Private macro -------------------------------------------------------------*/
  159. /* Private variables ---------------------------------------------------------*/
  160. /* Private function prototypes -----------------------------------------------*/
  161. /** @addtogroup UART_Private_Functions
  162. * @{
  163. */
  164. static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
  165. static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
  166. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  167. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  168. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  169. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  170. static void UART_DMAError(DMA_HandleTypeDef *hdma);
  171. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  172. static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
  173. static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
  174. static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
  175. /**
  176. * @}
  177. */
  178. /* Exported functions --------------------------------------------------------*/
  179. /** @defgroup UART_Exported_Functions UART Exported Functions
  180. * @{
  181. */
  182. /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
  183. * @brief Initialization and Configuration functions
  184. *
  185. @verbatim
  186. ===============================================================================
  187. ##### Initialization and Configuration functions #####
  188. ===============================================================================
  189. [..]
  190. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  191. in asynchronous mode.
  192. (+) For the asynchronous mode only these parameters can be configured:
  193. (++) Baud Rate
  194. (++) Word Length
  195. (++) Stop Bit
  196. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  197. in the data register is transmitted but is changed by the parity bit.
  198. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  199. please refer to Reference manual for possible UART frame formats.
  200. (++) Hardware flow control
  201. (++) Receiver/transmitter modes
  202. (++) Over Sampling Method
  203. [..]
  204. The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
  205. follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor
  206. configuration procedures (details for the procedures are available in reference manual (RM0329)).
  207. @endverbatim
  208. * @{
  209. */
  210. /**
  211. * @brief Initializes the UART mode according to the specified
  212. * parameters in the UART_InitTypeDef and creates the associated handle .
  213. * @param huart: uart handle
  214. * @retval HAL status
  215. */
  216. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  217. {
  218. /* Check the UART handle allocation */
  219. if(huart == NULL)
  220. {
  221. return HAL_ERROR;
  222. }
  223. if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  224. {
  225. /* Check the parameters */
  226. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  227. }
  228. else
  229. {
  230. /* Check the parameters */
  231. assert_param(IS_UART_INSTANCE(huart->Instance));
  232. }
  233. if(huart->gState == HAL_UART_STATE_RESET)
  234. {
  235. /* Allocate lock resource and initialize it */
  236. huart->Lock = HAL_UNLOCKED;
  237. /* Init the low level hardware : GPIO, CLOCK */
  238. HAL_UART_MspInit(huart);
  239. }
  240. huart->gState = HAL_UART_STATE_BUSY;
  241. /* Disable the Peripheral */
  242. __HAL_UART_DISABLE(huart);
  243. /* Set the UART Communication parameters */
  244. if (UART_SetConfig(huart) == HAL_ERROR)
  245. {
  246. return HAL_ERROR;
  247. }
  248. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  249. {
  250. UART_AdvFeatureConfig(huart);
  251. }
  252. /* In asynchronous mode, the following bits must be kept cleared:
  253. - LINEN and CLKEN bits in the USART_CR2 register,
  254. - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
  255. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  256. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  257. /* Enable the Peripheral */
  258. __HAL_UART_ENABLE(huart);
  259. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  260. return (UART_CheckIdleState(huart));
  261. }
  262. /**
  263. * @brief Initializes the half-duplex mode according to the specified
  264. * parameters in the UART_InitTypeDef and creates the associated handle .
  265. * @param huart: UART handle
  266. * @retval HAL status
  267. */
  268. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
  269. {
  270. /* Check the UART handle allocation */
  271. if(huart == NULL)
  272. {
  273. return HAL_ERROR;
  274. }
  275. if(huart->gState == HAL_UART_STATE_RESET)
  276. {
  277. /* Allocate lock resource and initialize it */
  278. huart->Lock = HAL_UNLOCKED;
  279. /* Init the low level hardware : GPIO, CLOCK */
  280. HAL_UART_MspInit(huart);
  281. }
  282. huart->gState = HAL_UART_STATE_BUSY;
  283. /* Disable the Peripheral */
  284. __HAL_UART_DISABLE(huart);
  285. /* Set the UART Communication parameters */
  286. if (UART_SetConfig(huart) == HAL_ERROR)
  287. {
  288. return HAL_ERROR;
  289. }
  290. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  291. {
  292. UART_AdvFeatureConfig(huart);
  293. }
  294. /* In half-duplex mode, the following bits must be kept cleared:
  295. - LINEN and CLKEN bits in the USART_CR2 register,
  296. - SCEN and IREN bits in the USART_CR3 register.*/
  297. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  298. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
  299. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  300. SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  301. /* Enable the Peripheral */
  302. __HAL_UART_ENABLE(huart);
  303. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  304. return (UART_CheckIdleState(huart));
  305. }
  306. /**
  307. * @brief Initialize the LIN mode according to the specified
  308. * parameters in the UART_InitTypeDef and creates the associated handle .
  309. * @param huart: UART handle.
  310. * @param BreakDetectLength: specifies the LIN break detection length.
  311. * This parameter can be one of the following values:
  312. * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
  313. * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
  314. * @retval HAL status
  315. */
  316. HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
  317. {
  318. /* Check the UART handle allocation */
  319. if(huart == NULL)
  320. {
  321. return HAL_ERROR;
  322. }
  323. /* Check the parameters */
  324. assert_param(IS_UART_INSTANCE(huart->Instance));
  325. assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
  326. assert_param(IS_LIN_WORD_LENGTH(huart->Init.WordLength));
  327. if(huart->gState == HAL_UART_STATE_RESET)
  328. {
  329. /* Allocate lock resource and initialize it */
  330. huart->Lock = HAL_UNLOCKED;
  331. /* Init the low level hardware : GPIO, CLOCK */
  332. HAL_UART_MspInit(huart);
  333. }
  334. huart->gState = HAL_UART_STATE_BUSY;
  335. /* Disable the Peripheral */
  336. __HAL_UART_DISABLE(huart);
  337. /* Set the UART Communication parameters */
  338. if (UART_SetConfig(huart) == HAL_ERROR)
  339. {
  340. return HAL_ERROR;
  341. }
  342. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  343. {
  344. UART_AdvFeatureConfig(huart);
  345. }
  346. /* In LIN mode, the following bits must be kept cleared:
  347. - LINEN and CLKEN bits in the USART_CR2 register,
  348. - SCEN and IREN bits in the USART_CR3 register.*/
  349. CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  350. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
  351. /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  352. SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
  353. /* Set the USART LIN Break detection length. */
  354. MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
  355. /* Enable the Peripheral */
  356. __HAL_UART_ENABLE(huart);
  357. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  358. return (UART_CheckIdleState(huart));
  359. }
  360. /**
  361. * @brief Initialize the multiprocessor mode according to the specified
  362. * parameters in the UART_InitTypeDef and initialize the associated handle.
  363. * @param huart: UART handle.
  364. * @param Address: UART node address (4-, 6-, 7- or 8-bit long).
  365. * @param WakeUpMethod: specifies the UART wakeup method.
  366. * This parameter can be one of the following values:
  367. * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
  368. * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
  369. * @note If the user resorts to idle line detection wake up, the Address parameter
  370. * is useless and ignored by the initialization function.
  371. * @note If the user resorts to address mark wake up, the address length detection
  372. * is configured by default to 4 bits only. For the UART to be able to
  373. * manage 6-, 7- or 8-bit long addresses detection
  374. * @retval HAL status
  375. */
  376. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
  377. {
  378. /* Check the UART handle allocation */
  379. if(huart == NULL)
  380. {
  381. return HAL_ERROR;
  382. }
  383. /* Check the wake up method parameter */
  384. assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
  385. if(huart->gState == HAL_UART_STATE_RESET)
  386. {
  387. /* Allocate lock resource and initialize it */
  388. huart->Lock = HAL_UNLOCKED;
  389. /* Init the low level hardware : GPIO, CLOCK */
  390. HAL_UART_MspInit(huart);
  391. }
  392. huart->gState = HAL_UART_STATE_BUSY;
  393. /* Disable the Peripheral */
  394. __HAL_UART_DISABLE(huart);
  395. /* Set the UART Communication parameters */
  396. if (UART_SetConfig(huart) == HAL_ERROR)
  397. {
  398. return HAL_ERROR;
  399. }
  400. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  401. {
  402. UART_AdvFeatureConfig(huart);
  403. }
  404. /* In multiprocessor mode, the following bits must be kept cleared:
  405. - LINEN and CLKEN bits in the USART_CR2 register,
  406. - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
  407. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  408. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  409. if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
  410. {
  411. /* If address mark wake up method is chosen, set the USART address node */
  412. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
  413. }
  414. /* Set the wake up method by setting the WAKE bit in the CR1 register */
  415. MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
  416. /* Enable the Peripheral */
  417. __HAL_UART_ENABLE(huart);
  418. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  419. return (UART_CheckIdleState(huart));
  420. }
  421. /**
  422. * @brief Initialize the RS485 Driver enable feature according to the specified
  423. * parameters in the UART_InitTypeDef and creates the associated handle.
  424. * @param huart: UART handle.
  425. * @param Polarity: select the driver enable polarity.
  426. * This parameter can be one of the following values:
  427. * @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
  428. * @arg @ref UART_DE_POLARITY_LOW DE signal is active low
  429. * @param AssertionTime: Driver Enable assertion time:
  430. * 5-bit value defining the time between the activation of the DE (Driver Enable)
  431. * signal and the beginning of the start bit. It is expressed in sample time
  432. * units (1/8 or 1/16 bit time, depending on the oversampling rate)
  433. * @param DeassertionTime: Driver Enable deassertion time:
  434. * 5-bit value defining the time between the end of the last stop bit, in a
  435. * transmitted message, and the de-activation of the DE (Driver Enable) signal.
  436. * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
  437. * oversampling rate).
  438. * @retval HAL status
  439. */
  440. HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
  441. {
  442. uint32_t temp = 0x0;
  443. /* Check the UART handle allocation */
  444. if(huart == NULL)
  445. {
  446. return HAL_ERROR;
  447. }
  448. /* Check the Driver Enable UART instance */
  449. assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
  450. /* Check the Driver Enable polarity */
  451. assert_param(IS_UART_DE_POLARITY(Polarity));
  452. /* Check the Driver Enable assertion time */
  453. assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
  454. /* Check the Driver Enable deassertion time */
  455. assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
  456. if(huart->gState == HAL_UART_STATE_RESET)
  457. {
  458. /* Allocate lock resource and initialize it */
  459. huart->Lock = HAL_UNLOCKED;
  460. /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  461. HAL_UART_MspInit(huart);
  462. }
  463. huart->gState = HAL_UART_STATE_BUSY;
  464. /* Disable the Peripheral */
  465. __HAL_UART_DISABLE(huart);
  466. /* Set the UART Communication parameters */
  467. if (UART_SetConfig(huart) == HAL_ERROR)
  468. {
  469. return HAL_ERROR;
  470. }
  471. if(huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  472. {
  473. UART_AdvFeatureConfig(huart);
  474. }
  475. /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
  476. SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
  477. /* Set the Driver Enable polarity */
  478. MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
  479. /* Set the Driver Enable assertion and deassertion times */
  480. temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
  481. temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
  482. MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT|USART_CR1_DEAT), temp);
  483. /* Enable the Peripheral */
  484. __HAL_UART_ENABLE(huart);
  485. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  486. return (UART_CheckIdleState(huart));
  487. }
  488. /**
  489. * @brief DeInitializes the UART peripheral
  490. * @param huart: uart handle
  491. * @retval HAL status
  492. */
  493. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
  494. {
  495. /* Check the UART handle allocation */
  496. if(huart == NULL)
  497. {
  498. return HAL_ERROR;
  499. }
  500. /* Check the parameters */
  501. assert_param(IS_UART_INSTANCE(huart->Instance));
  502. huart->gState = HAL_UART_STATE_BUSY;
  503. /* Disable the Peripheral */
  504. __HAL_UART_DISABLE(huart);
  505. huart->Instance->CR1 = 0x0U;
  506. huart->Instance->CR2 = 0x0U;
  507. huart->Instance->CR3 = 0x0U;
  508. /* DeInit the low level hardware */
  509. HAL_UART_MspDeInit(huart);
  510. huart->ErrorCode = HAL_UART_ERROR_NONE;
  511. huart->gState = HAL_UART_STATE_RESET;
  512. huart->RxState = HAL_UART_STATE_RESET;
  513. /* Process Unlock */
  514. __HAL_UNLOCK(huart);
  515. return HAL_OK;
  516. }
  517. /**
  518. * @brief UART MSP Init
  519. * @param huart: uart handle
  520. * @retval None
  521. */
  522. __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  523. {
  524. /* Prevent unused argument(s) compilation warning */
  525. UNUSED(huart);
  526. /* NOTE : This function should not be modified, when the callback is needed,
  527. the HAL_UART_MspInit can be implemented in the user file
  528. */
  529. }
  530. /**
  531. * @brief UART MSP DeInit
  532. * @param huart: uart handle
  533. * @retval None
  534. */
  535. __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  536. {
  537. /* Prevent unused argument(s) compilation warning */
  538. UNUSED(huart);
  539. /* NOTE : This function should not be modified, when the callback is needed,
  540. the HAL_UART_MspDeInit can be implemented in the user file
  541. */
  542. }
  543. /**
  544. * @}
  545. */
  546. /** @defgroup UART_Exported_Functions_Group2 IO operation functions
  547. * @brief UART Transmit/Receive functions
  548. *
  549. @verbatim
  550. ===============================================================================
  551. ##### IO operation functions #####
  552. ===============================================================================
  553. This subsection provides a set of functions allowing to manage the UART asynchronous
  554. and Half duplex data transfers.
  555. (#) There are two mode of transfer:
  556. (+) Blocking mode: The communication is performed in polling mode.
  557. The HAL status of all data processing is returned by the same function
  558. after finishing transfer.
  559. (+) Non-Blocking mode: The communication is performed using Interrupts
  560. or DMA, These API's return the HAL status.
  561. The end of the data processing will be indicated through the
  562. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
  563. using DMA mode.
  564. The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
  565. will be executed respectively at the end of the transmit or Receive process
  566. The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
  567. (#) Blocking mode API's are :
  568. (+) HAL_UART_Transmit()
  569. (+) HAL_UART_Receive()
  570. (#) Non-Blocking mode API's with Interrupt are :
  571. (+) HAL_UART_Transmit_IT()
  572. (+) HAL_UART_Receive_IT()
  573. (+) HAL_UART_IRQHandler()
  574. (+) UART_Transmit_IT()
  575. (+) UART_Receive_IT()
  576. (#) Non-Blocking mode API's with DMA are :
  577. (+) HAL_UART_Transmit_DMA()
  578. (+) HAL_UART_Receive_DMA()
  579. (+) HAL_UART_DMAPause()
  580. (+) HAL_UART_DMAResume()
  581. (+) HAL_UART_DMAStop()
  582. (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
  583. (+) HAL_UART_TxHalfCpltCallback()
  584. (+) HAL_UART_TxCpltCallback()
  585. (+) HAL_UART_RxHalfCpltCallback()
  586. (+) HAL_UART_RxCpltCallback()
  587. (+) HAL_UART_ErrorCallback()
  588. -@- In the Half duplex communication, it is forbidden to run the transmit
  589. and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
  590. @endverbatim
  591. * @{
  592. */
  593. /**
  594. * @brief Send an amount of data in blocking mode.
  595. * @param huart: UART handle.
  596. * @param pData: Pointer to data buffer.
  597. * @param Size: Amount of data to be sent.
  598. * @param Timeout: Timeout duration.
  599. * @retval HAL status
  600. */
  601. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  602. {
  603. uint16_t* tmp;
  604. uint32_t tickstart = 0U;
  605. /* Check that a Tx process is not already ongoing */
  606. if(huart->gState == HAL_UART_STATE_READY)
  607. {
  608. if((pData == NULL ) || (Size == 0U))
  609. {
  610. return HAL_ERROR;
  611. }
  612. /* Process Locked */
  613. __HAL_LOCK(huart);
  614. huart->ErrorCode = HAL_UART_ERROR_NONE;
  615. huart->gState = HAL_UART_STATE_BUSY_TX;
  616. /* Init tickstart for timeout managment*/
  617. tickstart = HAL_GetTick();
  618. huart->TxXferSize = Size;
  619. huart->TxXferCount = Size;
  620. while(huart->TxXferCount > 0U)
  621. {
  622. huart->TxXferCount--;
  623. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  624. {
  625. return HAL_TIMEOUT;
  626. }
  627. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  628. {
  629. tmp = (uint16_t*) pData;
  630. huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
  631. pData += 2;
  632. }
  633. else
  634. {
  635. huart->Instance->TDR = (*pData++ & (uint8_t)0xFFU);
  636. }
  637. }
  638. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  639. {
  640. return HAL_TIMEOUT;
  641. }
  642. /* At end of Tx process, restore huart->gState to Ready */
  643. huart->gState = HAL_UART_STATE_READY;
  644. /* Process Unlocked */
  645. __HAL_UNLOCK(huart);
  646. return HAL_OK;
  647. }
  648. else
  649. {
  650. return HAL_BUSY;
  651. }
  652. }
  653. /**
  654. * @brief Receive an amount of data in blocking mode.
  655. * @param huart: UART handle.
  656. * @param pData: pointer to data buffer.
  657. * @param Size: amount of data to be received.
  658. * @param Timeout: Timeout duration.
  659. * @retval HAL status
  660. */
  661. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  662. {
  663. uint16_t* tmp;
  664. uint16_t uhMask;
  665. uint32_t tickstart = 0U;
  666. /* Check that a Rx process is not already ongoing */
  667. if(huart->RxState == HAL_UART_STATE_READY)
  668. {
  669. if((pData == NULL ) || (Size == 0U))
  670. {
  671. return HAL_ERROR;
  672. }
  673. /* Process Locked */
  674. __HAL_LOCK(huart);
  675. huart->ErrorCode = HAL_UART_ERROR_NONE;
  676. huart->RxState = HAL_UART_STATE_BUSY_RX;
  677. /* Init tickstart for timeout managment*/
  678. tickstart = HAL_GetTick();
  679. huart->RxXferSize = Size;
  680. huart->RxXferCount = Size;
  681. /* Computation of UART mask to apply to RDR register */
  682. UART_MASK_COMPUTATION(huart);
  683. uhMask = huart->Mask;
  684. /* as long as data have to be received */
  685. while(huart->RxXferCount > 0U)
  686. {
  687. huart->RxXferCount--;
  688. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  689. {
  690. return HAL_TIMEOUT;
  691. }
  692. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  693. {
  694. tmp = (uint16_t*) pData ;
  695. *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
  696. pData +=2U;
  697. }
  698. else
  699. {
  700. *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  701. }
  702. }
  703. /* At end of Rx process, restore huart->RxState to Ready */
  704. huart->RxState = HAL_UART_STATE_READY;
  705. /* Process Unlocked */
  706. __HAL_UNLOCK(huart);
  707. return HAL_OK;
  708. }
  709. else
  710. {
  711. return HAL_BUSY;
  712. }
  713. }
  714. /**
  715. * @brief Send an amount of data in interrupt mode.
  716. * @param huart: UART handle.
  717. * @param pData: pointer to data buffer.
  718. * @param Size: amount of data to be sent.
  719. * @retval HAL status
  720. */
  721. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  722. {
  723. /* Check that a Tx process is not already ongoing */
  724. if(huart->gState == HAL_UART_STATE_READY)
  725. {
  726. if((pData == NULL ) || (Size == 0U))
  727. {
  728. return HAL_ERROR;
  729. }
  730. /* Process Locked */
  731. __HAL_LOCK(huart);
  732. huart->pTxBuffPtr = pData;
  733. huart->TxXferSize = Size;
  734. huart->TxXferCount = Size;
  735. huart->ErrorCode = HAL_UART_ERROR_NONE;
  736. huart->gState = HAL_UART_STATE_BUSY_TX;
  737. /* Process Unlocked */
  738. __HAL_UNLOCK(huart);
  739. /* Enable the UART Transmit Data Register Empty Interrupt */
  740. SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
  741. return HAL_OK;
  742. }
  743. else
  744. {
  745. return HAL_BUSY;
  746. }
  747. }
  748. /**
  749. * @brief Receive an amount of data in interrupt mode.
  750. * @param huart: UART handle.
  751. * @param pData: pointer to data buffer.
  752. * @param Size: amount of data to be received.
  753. * @retval HAL status
  754. */
  755. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  756. {
  757. /* Check that a Rx process is not already ongoing */
  758. if(huart->RxState == HAL_UART_STATE_READY)
  759. {
  760. if((pData == NULL ) || (Size == 0U))
  761. {
  762. return HAL_ERROR;
  763. }
  764. /* Process Locked */
  765. __HAL_LOCK(huart);
  766. huart->pRxBuffPtr = pData;
  767. huart->RxXferSize = Size;
  768. huart->RxXferCount = Size;
  769. /* Computation of UART mask to apply to RDR register */
  770. UART_MASK_COMPUTATION(huart);
  771. huart->ErrorCode = HAL_UART_ERROR_NONE;
  772. huart->RxState = HAL_UART_STATE_BUSY_RX;
  773. /* Process Unlocked */
  774. __HAL_UNLOCK(huart);
  775. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  776. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  777. /* Enable the UART Parity Error and Data Register not empty Interrupts */
  778. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  779. return HAL_OK;
  780. }
  781. else
  782. {
  783. return HAL_BUSY;
  784. }
  785. }
  786. /**
  787. * @brief Send an amount of data in DMA mode.
  788. * @param huart: UART handle.
  789. * @param pData: pointer to data buffer.
  790. * @param Size: amount of data to be sent.
  791. * @retval HAL status
  792. */
  793. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  794. {
  795. uint32_t *tmp;
  796. /* Check that a Tx process is not already ongoing */
  797. if(huart->gState == HAL_UART_STATE_READY)
  798. {
  799. if((pData == NULL ) || (Size == 0U))
  800. {
  801. return HAL_ERROR;
  802. }
  803. /* Process Locked */
  804. __HAL_LOCK(huart);
  805. huart->pTxBuffPtr = pData;
  806. huart->TxXferSize = Size;
  807. huart->TxXferCount = Size;
  808. huart->ErrorCode = HAL_UART_ERROR_NONE;
  809. huart->gState = HAL_UART_STATE_BUSY_TX;
  810. /* Set the UART DMA transfer complete callback */
  811. huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
  812. /* Set the UART DMA Half transfer complete callback */
  813. huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
  814. /* Set the DMA error callback */
  815. huart->hdmatx->XferErrorCallback = UART_DMAError;
  816. /* Set the DMA abort callback */
  817. huart->hdmatx->XferAbortCallback = NULL;
  818. /* Enable the UART transmit DMA channel */
  819. tmp = (uint32_t*)&pData;
  820. HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->TDR, Size);
  821. /* Clear the TC flag in the SR register by writing 0 to it */
  822. __HAL_UART_CLEAR_IT(huart, UART_FLAG_TC);
  823. /* Process Unlocked */
  824. __HAL_UNLOCK(huart);
  825. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  826. in the UART CR3 register */
  827. SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  828. return HAL_OK;
  829. }
  830. else
  831. {
  832. return HAL_BUSY;
  833. }
  834. }
  835. /**
  836. * @brief Receive an amount of data in DMA mode.
  837. * @param huart: UART handle.
  838. * @param pData: pointer to data buffer.
  839. * @param Size: amount of data to be received.
  840. * @note When the UART parity is enabled (PCE = 1), the received data contain
  841. * the parity bit (MSB position).
  842. * @retval HAL status
  843. */
  844. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  845. {
  846. uint32_t *tmp;
  847. /* Check that a Rx process is not already ongoing */
  848. if(huart->RxState == HAL_UART_STATE_READY)
  849. {
  850. if((pData == NULL ) || (Size == 0U))
  851. {
  852. return HAL_ERROR;
  853. }
  854. /* Process Locked */
  855. __HAL_LOCK(huart);
  856. huart->pRxBuffPtr = pData;
  857. huart->RxXferSize = Size;
  858. huart->ErrorCode = HAL_UART_ERROR_NONE;
  859. huart->RxState = HAL_UART_STATE_BUSY_RX;
  860. /* Set the UART DMA transfer complete callback */
  861. huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
  862. /* Set the UART DMA Half transfer complete callback */
  863. huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
  864. /* Set the DMA error callback */
  865. huart->hdmarx->XferErrorCallback = UART_DMAError;
  866. /* Set the DMA abort callback */
  867. huart->hdmarx->XferAbortCallback = NULL;
  868. /* Enable the DMA channel */
  869. tmp = (uint32_t*)&pData;
  870. HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, *(uint32_t*)tmp, Size);
  871. /* Process Unlocked */
  872. __HAL_UNLOCK(huart);
  873. /* Enable the UART Parity Error Interrupt */
  874. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  875. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  876. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  877. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  878. in the UART CR3 register */
  879. SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  880. return HAL_OK;
  881. }
  882. else
  883. {
  884. return HAL_BUSY;
  885. }
  886. }
  887. /**
  888. * @brief Pause the DMA Transfer.
  889. * @param huart: UART handle.
  890. * @retval HAL status
  891. */
  892. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
  893. {
  894. /* Process Locked */
  895. __HAL_LOCK(huart);
  896. if ((huart->gState == HAL_UART_STATE_BUSY_TX) &&
  897. (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)))
  898. {
  899. /* Disable the UART DMA Tx request */
  900. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  901. }
  902. if ((huart->RxState == HAL_UART_STATE_BUSY_RX) &&
  903. (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)))
  904. {
  905. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  906. CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  907. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  908. /* Disable the UART DMA Rx request */
  909. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  910. }
  911. /* Process Unlocked */
  912. __HAL_UNLOCK(huart);
  913. return HAL_OK;
  914. }
  915. /**
  916. * @brief Resume the DMA Transfer.
  917. * @param huart: UART handle.
  918. * @retval HAL status
  919. */
  920. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
  921. {
  922. /* Process Locked */
  923. __HAL_LOCK(huart);
  924. if(huart->gState == HAL_UART_STATE_BUSY_TX)
  925. {
  926. /* Enable the UART DMA Tx request */
  927. SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  928. }
  929. if(huart->RxState == HAL_UART_STATE_BUSY_RX)
  930. {
  931. /* Clear the Overrun flag before resuming the Rx transfer*/
  932. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
  933. /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
  934. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  935. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  936. /* Enable the UART DMA Rx request */
  937. SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  938. }
  939. /* If the UART peripheral is still not enabled, enable it */
  940. if ((huart->Instance->CR1 & USART_CR1_UE) == 0U)
  941. {
  942. /* Enable UART peripheral */
  943. __HAL_UART_ENABLE(huart);
  944. }
  945. return HAL_OK;
  946. }
  947. /**
  948. * @brief Stop the DMA Transfer.
  949. * @param huart: UART handle.
  950. * @retval HAL status
  951. */
  952. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
  953. {
  954. /* The Lock is not implemented on this API to allow the user application
  955. to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
  956. HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
  957. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  958. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  959. the stream and the corresponding call back is executed. */
  960. /* Stop UART DMA Tx request if ongoing */
  961. if ((huart->gState == HAL_UART_STATE_BUSY_TX) &&
  962. (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)))
  963. {
  964. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  965. /* Abort the UART DMA Tx channel */
  966. if(huart->hdmatx != NULL)
  967. {
  968. HAL_DMA_Abort(huart->hdmatx);
  969. }
  970. UART_EndTxTransfer(huart);
  971. }
  972. /* Stop UART DMA Rx request if ongoing */
  973. if ((huart->RxState == HAL_UART_STATE_BUSY_RX) &&
  974. (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)))
  975. {
  976. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  977. /* Abort the UART DMA Rx channel */
  978. if(huart->hdmarx != NULL)
  979. {
  980. HAL_DMA_Abort(huart->hdmarx);
  981. }
  982. UART_EndRxTransfer(huart);
  983. }
  984. return HAL_OK;
  985. }
  986. /**
  987. * @brief This function handles UART interrupt request.
  988. * @param huart: uart handle
  989. * @retval None
  990. */
  991. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
  992. {
  993. uint32_t isrflags = READ_REG(huart->Instance->ISR);
  994. uint32_t cr1its = READ_REG(huart->Instance->CR1);
  995. uint32_t cr3its = READ_REG(huart->Instance->CR3);
  996. uint32_t errorflags;
  997. /* If no error occurs */
  998. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
  999. if (errorflags == RESET)
  1000. {
  1001. /* UART in mode Receiver ---------------------------------------------------*/
  1002. if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1003. {
  1004. UART_Receive_IT(huart);
  1005. return;
  1006. }
  1007. }
  1008. /* If some errors occur */
  1009. if( (errorflags != RESET)
  1010. && ( ((cr3its & USART_CR3_EIE) != RESET)
  1011. || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)) )
  1012. {
  1013. /* UART parity error interrupt occurred -------------------------------------*/
  1014. if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1015. {
  1016. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_PEF);
  1017. huart->ErrorCode |= HAL_UART_ERROR_PE;
  1018. }
  1019. /* UART frame error interrupt occurred --------------------------------------*/
  1020. if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1021. {
  1022. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_FEF);
  1023. huart->ErrorCode |= HAL_UART_ERROR_FE;
  1024. }
  1025. /* UART noise error interrupt occurred --------------------------------------*/
  1026. if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1027. {
  1028. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_NEF);
  1029. huart->ErrorCode |= HAL_UART_ERROR_NE;
  1030. }
  1031. /* UART Over-Run interrupt occurred -----------------------------------------*/
  1032. if(((isrflags & USART_ISR_ORE) != RESET) &&
  1033. (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
  1034. {
  1035. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
  1036. huart->ErrorCode |= HAL_UART_ERROR_ORE;
  1037. }
  1038. /* Call UART Error Call back function if need be --------------------------*/
  1039. if(huart->ErrorCode != HAL_UART_ERROR_NONE)
  1040. {
  1041. /* UART in mode Receiver ---------------------------------------------------*/
  1042. if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1043. {
  1044. UART_Receive_IT(huart);
  1045. }
  1046. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1047. consider error as blocking */
  1048. if (((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) ||
  1049. (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)))
  1050. {
  1051. /* Blocking error : transfer is aborted
  1052. Set the UART state ready to be able to start again the process,
  1053. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1054. UART_EndRxTransfer(huart);
  1055. /* Disable the UART DMA Rx request if enabled */
  1056. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1057. {
  1058. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1059. /* Abort the UART DMA Rx channel */
  1060. if(huart->hdmarx != NULL)
  1061. {
  1062. /* Set the UART DMA Abort callback :
  1063. will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
  1064. huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
  1065. /* Abort DMA RX */
  1066. if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1067. {
  1068. /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
  1069. huart->hdmarx->XferAbortCallback(huart->hdmarx);
  1070. }
  1071. }
  1072. else
  1073. {
  1074. /* Call user error callback */
  1075. HAL_UART_ErrorCallback(huart);
  1076. }
  1077. }
  1078. else
  1079. {
  1080. /* Call user error callback */
  1081. HAL_UART_ErrorCallback(huart);
  1082. }
  1083. }
  1084. else
  1085. {
  1086. /* Non Blocking error : transfer could go on.
  1087. Error is notified to user through user error callback */
  1088. HAL_UART_ErrorCallback(huart);
  1089. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1090. }
  1091. }
  1092. return;
  1093. } /* End if some error occurs */
  1094. /* UART in mode Transmitter ------------------------------------------------*/
  1095. if(((isrflags & USART_ISR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1096. {
  1097. UART_Transmit_IT(huart);
  1098. return;
  1099. }
  1100. /* UART in mode Transmitter (transmission end) -----------------------------*/
  1101. if(((isrflags & USART_ISR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1102. {
  1103. UART_EndTransmit_IT(huart);
  1104. return;
  1105. }
  1106. }
  1107. /**
  1108. * @brief This function handles UART Communication Timeout.
  1109. * @param huart UART handle
  1110. * @param Flag specifies the UART flag to check.
  1111. * @param Status The new Flag status (SET or RESET).
  1112. * @param Tickstart Tick start value
  1113. * @param Timeout Timeout duration
  1114. * @retval HAL status
  1115. */
  1116. HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1117. {
  1118. /* Wait until flag is set */
  1119. while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  1120. {
  1121. /* Check for the Timeout */
  1122. if(Timeout != HAL_MAX_DELAY)
  1123. {
  1124. if((Timeout == 0U)||((HAL_GetTick()-Tickstart) >= Timeout))
  1125. {
  1126. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1127. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  1128. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1129. huart->gState = HAL_UART_STATE_READY;
  1130. huart->RxState = HAL_UART_STATE_READY;
  1131. /* Process Unlocked */
  1132. __HAL_UNLOCK(huart);
  1133. return HAL_TIMEOUT;
  1134. }
  1135. }
  1136. }
  1137. return HAL_OK;
  1138. }
  1139. /**
  1140. * @brief DMA UART transmit process complete callback
  1141. * @param hdma: DMA handle
  1142. * @retval None
  1143. */
  1144. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1145. {
  1146. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1147. /* DMA Normal mode*/
  1148. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
  1149. {
  1150. huart->TxXferCount = 0U;
  1151. /* Disable the DMA transfer for transmit request by setting the DMAT bit
  1152. in the UART CR3 register */
  1153. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1154. /* Enable the UART Transmit Complete Interrupt */
  1155. SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  1156. }
  1157. /* DMA Circular mode */
  1158. else
  1159. {
  1160. HAL_UART_TxCpltCallback(huart);
  1161. }
  1162. }
  1163. /**
  1164. * @brief DMA UART transmit process half complete callback
  1165. * @param hdma : DMA handle
  1166. * @retval None
  1167. */
  1168. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1169. {
  1170. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1171. HAL_UART_TxHalfCpltCallback(huart);
  1172. }
  1173. /**
  1174. * @brief DMA UART receive process complete callback
  1175. * @param hdma: DMA handle
  1176. * @retval None
  1177. */
  1178. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1179. {
  1180. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1181. /* DMA Normal mode */
  1182. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
  1183. {
  1184. huart->RxXferCount = 0U;
  1185. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1186. CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1187. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1188. /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1189. in the UART CR3 register */
  1190. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1191. /* At end of Rx process, restore huart->RxState to Ready */
  1192. huart->RxState = HAL_UART_STATE_READY;
  1193. }
  1194. HAL_UART_RxCpltCallback(huart);
  1195. }
  1196. /**
  1197. * @brief DMA UART receive process half complete callback
  1198. * @param hdma : DMA handle
  1199. * @retval None
  1200. */
  1201. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1202. {
  1203. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1204. HAL_UART_RxHalfCpltCallback(huart);
  1205. }
  1206. /**
  1207. * @brief DMA UART communication error callback
  1208. * @param hdma: DMA handle
  1209. * @retval None
  1210. */
  1211. static void UART_DMAError(DMA_HandleTypeDef *hdma)
  1212. {
  1213. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1214. huart->RxXferCount = 0U;
  1215. huart->TxXferCount = 0U;
  1216. /* Stop UART DMA Tx request if ongoing */
  1217. if ( (huart->gState == HAL_UART_STATE_BUSY_TX)
  1218. &&(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) )
  1219. {
  1220. UART_EndTxTransfer(huart);
  1221. }
  1222. /* Stop UART DMA Rx request if ongoing */
  1223. if ( (huart->RxState == HAL_UART_STATE_BUSY_RX)
  1224. &&(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) )
  1225. {
  1226. UART_EndRxTransfer(huart);
  1227. }
  1228. SET_BIT(huart->ErrorCode, HAL_UART_ERROR_DMA);
  1229. HAL_UART_ErrorCallback(huart);
  1230. }
  1231. /**
  1232. * @brief DMA UART communication abort callback, when call by HAL services on Error
  1233. * (To be called at end of DMA Abort procedure following error occurrence).
  1234. * @param hdma: DMA handle.
  1235. * @retval None
  1236. */
  1237. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1238. {
  1239. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)(hdma->Parent);
  1240. huart->RxXferCount = 0U;
  1241. huart->TxXferCount = 0U;
  1242. HAL_UART_ErrorCallback(huart);
  1243. }
  1244. /**
  1245. * @brief Tx Transfer completed callbacks
  1246. * @param huart: uart handle
  1247. * @retval None
  1248. */
  1249. __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  1250. {
  1251. /* Prevent unused argument(s) compilation warning */
  1252. UNUSED(huart);
  1253. /* NOTE : This function should not be modified, when the callback is needed,
  1254. the HAL_UART_TxCpltCallback can be implemented in the user file
  1255. */
  1256. }
  1257. /**
  1258. * @brief Tx Half Transfer completed callbacks.
  1259. * @param huart: UART handle
  1260. * @retval None
  1261. */
  1262. __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
  1263. {
  1264. /* Prevent unused argument(s) compilation warning */
  1265. UNUSED(huart);
  1266. /* NOTE: This function should not be modified, when the callback is needed,
  1267. the HAL_UART_TxHalfCpltCallback can be implemented in the user file
  1268. */
  1269. }
  1270. /**
  1271. * @brief Rx Transfer completed callbacks
  1272. * @param huart: uart handle
  1273. * @retval None
  1274. */
  1275. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  1276. {
  1277. /* Prevent unused argument(s) compilation warning */
  1278. UNUSED(huart);
  1279. /* NOTE : This function should not be modified, when the callback is needed,
  1280. the HAL_UART_RxCpltCallback can be implemented in the user file
  1281. */
  1282. }
  1283. /**
  1284. * @brief Rx Half Transfer completed callbacks.
  1285. * @param huart: UART handle
  1286. * @retval None
  1287. */
  1288. __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  1289. {
  1290. /* Prevent unused argument(s) compilation warning */
  1291. UNUSED(huart);
  1292. /* NOTE: This function should not be modified, when the callback is needed,
  1293. the HAL_UART_RxHalfCpltCallback can be implemented in the user file
  1294. */
  1295. }
  1296. /**
  1297. * @brief UART error callbacks
  1298. * @param huart: uart handle
  1299. * @retval None
  1300. */
  1301. __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  1302. {
  1303. /* Prevent unused argument(s) compilation warning */
  1304. UNUSED(huart);
  1305. /* NOTE : This function should not be modified, when the callback is needed,
  1306. the HAL_UART_ErrorCallback can be implemented in the user file
  1307. */
  1308. }
  1309. /**
  1310. * @brief Send an amount of data in interrupt mode
  1311. * Function called under interruption only, once
  1312. * interruptions have been enabled by HAL_UART_Transmit_IT()
  1313. * @param huart: UART handle
  1314. * @retval HAL status
  1315. */
  1316. static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
  1317. {
  1318. uint16_t* tmp;
  1319. /* Check that a Tx process is ongoing */
  1320. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  1321. {
  1322. if(huart->TxXferCount == 0U)
  1323. {
  1324. /* Disable the UART Transmit Data Register Empty Interrupt */
  1325. CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
  1326. /* Enable the UART Transmit Complete Interrupt */
  1327. SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  1328. return HAL_OK;
  1329. }
  1330. else
  1331. {
  1332. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1333. {
  1334. tmp = (uint16_t*) huart->pTxBuffPtr;
  1335. huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
  1336. huart->pTxBuffPtr += 2U;
  1337. }
  1338. else
  1339. {
  1340. huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0xFFU);
  1341. }
  1342. huart->TxXferCount--;
  1343. return HAL_OK;
  1344. }
  1345. }
  1346. else
  1347. {
  1348. return HAL_BUSY;
  1349. }
  1350. }
  1351. /**
  1352. * @brief Wrap up transmission in non-blocking mode.
  1353. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1354. * the configuration information for the specified UART module.
  1355. * @retval HAL status
  1356. */
  1357. static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
  1358. {
  1359. /* Disable the UART Transmit Complete Interrupt */
  1360. CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  1361. /* Tx process is ended, restore huart->gState to Ready */
  1362. huart->gState = HAL_UART_STATE_READY;
  1363. HAL_UART_TxCpltCallback(huart);
  1364. return HAL_OK;
  1365. }
  1366. /**
  1367. * @brief Receive an amount of data in interrupt mode
  1368. * Function called under interruption only, once
  1369. * interruptions have been enabled by HAL_UART_Receive_IT()
  1370. * @param huart: UART handle
  1371. * @retval HAL status
  1372. */
  1373. static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
  1374. {
  1375. uint16_t* tmp;
  1376. uint16_t uhMask = huart->Mask;
  1377. /* Check that a Rx process is ongoing */
  1378. if(huart->RxState == HAL_UART_STATE_BUSY_RX)
  1379. {
  1380. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1381. {
  1382. tmp = (uint16_t*) huart->pRxBuffPtr ;
  1383. *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
  1384. huart->pRxBuffPtr +=2;
  1385. }
  1386. else
  1387. {
  1388. *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  1389. }
  1390. if(--huart->RxXferCount == 0)
  1391. {
  1392. /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
  1393. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1394. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1395. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1396. /* Rx process is completed, restore huart->RxState to Ready */
  1397. huart->RxState = HAL_UART_STATE_READY;
  1398. HAL_UART_RxCpltCallback(huart);
  1399. return HAL_OK;
  1400. }
  1401. return HAL_OK;
  1402. }
  1403. else
  1404. {
  1405. /* Clear RXNE interrupt flag */
  1406. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1407. return HAL_BUSY;
  1408. }
  1409. }
  1410. /**
  1411. * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
  1412. * @param huart: UART handle.
  1413. * @retval None
  1414. */
  1415. static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
  1416. {
  1417. /* Disable TXEIE and TCIE interrupts */
  1418. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1419. /* At end of Tx process, restore huart->gState to Ready */
  1420. huart->gState = HAL_UART_STATE_READY;
  1421. }
  1422. /**
  1423. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  1424. * @param huart: UART handle.
  1425. * @retval None
  1426. */
  1427. static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
  1428. {
  1429. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1430. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1431. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1432. /* At end of Rx process, restore huart->RxState to Ready */
  1433. huart->RxState = HAL_UART_STATE_READY;
  1434. }
  1435. /**
  1436. * @}
  1437. */
  1438. /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
  1439. * @brief UART control functions
  1440. *
  1441. @verbatim
  1442. ===============================================================================
  1443. ##### Peripheral Control functions #####
  1444. ===============================================================================
  1445. [..]
  1446. This subsection provides a set of functions allowing to control the UART.
  1447. (+) HAL_UART_GetState() API is helpful to check in run-time the state of the UART peripheral.
  1448. (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
  1449. (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
  1450. (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
  1451. (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
  1452. (+) UART_SetConfig() API configures the UART peripheral
  1453. (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
  1454. (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
  1455. (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
  1456. (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
  1457. (+) HAL_LIN_SendBreak() API transmits the break characters
  1458. (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
  1459. detection length to more than 4 bits for multiprocessor address mark wake up.
  1460. @endverbatim
  1461. * @{
  1462. */
  1463. /**
  1464. * @brief Enable UART in mute mode (doesn't mean UART enters mute mode;
  1465. * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called)
  1466. * @param huart: UART handle
  1467. * @retval HAL status
  1468. */
  1469. HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
  1470. {
  1471. /* Process Locked */
  1472. __HAL_LOCK(huart);
  1473. huart->gState = HAL_UART_STATE_BUSY;
  1474. /* Enable USART mute mode by setting the MME bit in the CR1 register */
  1475. SET_BIT(huart->Instance->CR1, USART_CR1_MME);
  1476. huart->gState = HAL_UART_STATE_READY;
  1477. return (UART_CheckIdleState(huart));
  1478. }
  1479. /**
  1480. * @brief Disable UART mute mode (doesn't mean it actually wakes up the software,
  1481. * as it may not have been in mute mode at this very moment).
  1482. * @param huart: uart handle
  1483. * @retval HAL status
  1484. */
  1485. HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
  1486. {
  1487. /* Process Locked */
  1488. __HAL_LOCK(huart);
  1489. huart->gState = HAL_UART_STATE_BUSY;
  1490. /* Disable USART mute mode by clearing the MME bit in the CR1 register */
  1491. CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
  1492. huart->gState = HAL_UART_STATE_READY;
  1493. return (UART_CheckIdleState(huart));
  1494. }
  1495. /**
  1496. * @brief Enter UART mute mode (means UART actually enters mute mode).
  1497. * To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
  1498. * @param huart: uart handle
  1499. * @retval HAL status
  1500. */
  1501. void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
  1502. {
  1503. __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
  1504. }
  1505. /**
  1506. * @brief return the UART state
  1507. * @param huart: uart handle
  1508. * @retval HAL state
  1509. */
  1510. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
  1511. {
  1512. uint32_t temp1= 0x00U, temp2 = 0x00U;
  1513. temp1 = huart->gState;
  1514. temp2 = huart->RxState;
  1515. return (HAL_UART_StateTypeDef)(temp1 | temp2);
  1516. }
  1517. /**
  1518. * @brief Return the UART error code
  1519. * @param huart : pointer to a UART_HandleTypeDef structure that contains
  1520. * the configuration information for the specified UART.
  1521. * @retval UART Error Code
  1522. */
  1523. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
  1524. {
  1525. return huart->ErrorCode;
  1526. }
  1527. /**
  1528. * @brief Configure the UART peripheral
  1529. * @param huart: uart handle
  1530. * @retval None
  1531. */
  1532. HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
  1533. {
  1534. uint32_t tmpreg = 0x00000000U;
  1535. UART_ClockSourceTypeDef clocksource = UART_CLOCKSOURCE_UNDEFINED;
  1536. uint16_t brrtemp = 0x0000U;
  1537. uint16_t usartdiv = 0x0000U;
  1538. HAL_StatusTypeDef ret = HAL_OK;
  1539. /* Check the parameters */
  1540. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  1541. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  1542. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  1543. assert_param(IS_UART_PARITY(huart->Init.Parity));
  1544. assert_param(IS_UART_MODE(huart->Init.Mode));
  1545. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  1546. assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
  1547. /*-------------------------- USART CR1 Configuration -----------------------*/
  1548. /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
  1549. * the UART Word Length, Parity, Mode and oversampling:
  1550. * set the M bits according to huart->Init.WordLength value
  1551. * set PCE and PS bits according to huart->Init.Parity value
  1552. * set TE and RE bits according to huart->Init.Mode value
  1553. * set OVER8 bit according to huart->Init.OverSampling value */
  1554. tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
  1555. MODIFY_REG(huart->Instance->CR1, UART_CR1_FIELDS, tmpreg);
  1556. /*-------------------------- USART CR2 Configuration -----------------------*/
  1557. /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  1558. * to huart->Init.StopBits value */
  1559. MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  1560. /*-------------------------- USART CR3 Configuration -----------------------*/
  1561. /* Configure
  1562. * - UART HardWare Flow Control: set CTSE and RTSE bits according
  1563. * to huart->Init.HwFlowCtl value
  1564. * - one-bit sampling method versus three samples' majority rule according
  1565. * to huart->Init.OneBitSampling */
  1566. tmpreg = (uint32_t)huart->Init.HwFlowCtl | huart->Init.OneBitSampling ;
  1567. MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT), tmpreg);
  1568. /*-------------------------- USART BRR Configuration -----------------------*/
  1569. UART_GETCLOCKSOURCE(huart, clocksource);
  1570. /* Check UART Over Sampling to set Baud Rate Register */
  1571. if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  1572. {
  1573. switch (clocksource)
  1574. {
  1575. case UART_CLOCKSOURCE_PCLK1:
  1576. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1577. break;
  1578. case UART_CLOCKSOURCE_PCLK2:
  1579. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
  1580. break;
  1581. case UART_CLOCKSOURCE_HSI:
  1582. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
  1583. break;
  1584. case UART_CLOCKSOURCE_SYSCLK:
  1585. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1586. break;
  1587. case UART_CLOCKSOURCE_LSE:
  1588. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
  1589. break;
  1590. case UART_CLOCKSOURCE_UNDEFINED:
  1591. default:
  1592. ret = HAL_ERROR;
  1593. break;
  1594. }
  1595. brrtemp = usartdiv & 0xFFF0U;
  1596. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
  1597. huart->Instance->BRR = brrtemp;
  1598. }
  1599. else
  1600. {
  1601. switch (clocksource)
  1602. {
  1603. case UART_CLOCKSOURCE_PCLK1:
  1604. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1605. break;
  1606. case UART_CLOCKSOURCE_PCLK2:
  1607. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
  1608. break;
  1609. case UART_CLOCKSOURCE_HSI:
  1610. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
  1611. break;
  1612. case UART_CLOCKSOURCE_SYSCLK:
  1613. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1614. break;
  1615. case UART_CLOCKSOURCE_LSE:
  1616. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
  1617. break;
  1618. case UART_CLOCKSOURCE_UNDEFINED:
  1619. default:
  1620. ret = HAL_ERROR;
  1621. break;
  1622. }
  1623. }
  1624. return ret;
  1625. }
  1626. /**
  1627. * @brief Configure the UART peripheral advanced features
  1628. * @param huart: uart handle
  1629. * @retval None
  1630. */
  1631. void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
  1632. {
  1633. /* Check whether the set of advanced features to configure is properly set */
  1634. assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
  1635. /* if required, configure TX pin active level inversion */
  1636. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
  1637. {
  1638. assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
  1639. MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
  1640. }
  1641. /* if required, configure RX pin active level inversion */
  1642. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
  1643. {
  1644. assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
  1645. MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
  1646. }
  1647. /* if required, configure data inversion */
  1648. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
  1649. {
  1650. assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
  1651. MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
  1652. }
  1653. /* if required, configure RX/TX pins swap */
  1654. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
  1655. {
  1656. assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
  1657. MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
  1658. }
  1659. /* if required, configure RX overrun detection disabling */
  1660. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  1661. {
  1662. assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
  1663. MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
  1664. }
  1665. /* if required, configure DMA disabling on reception error */
  1666. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
  1667. {
  1668. assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
  1669. MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
  1670. }
  1671. /* if required, configure auto Baud rate detection scheme */
  1672. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
  1673. {
  1674. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
  1675. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
  1676. /* set auto Baudrate detection parameters if detection is enabled */
  1677. if(huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
  1678. {
  1679. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
  1680. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
  1681. }
  1682. }
  1683. /* if required, configure MSB first on communication line */
  1684. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
  1685. {
  1686. assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
  1687. MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
  1688. }
  1689. }
  1690. /**
  1691. * @brief Check the UART Idle State
  1692. * @param huart: uart handle
  1693. * @retval HAL status
  1694. */
  1695. HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
  1696. {
  1697. uint32_t tickstart = 0U;
  1698. /* Initialize the UART ErrorCode */
  1699. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1700. /* Init tickstart for timeout managment*/
  1701. tickstart = HAL_GetTick();
  1702. /* Check if the Transmitter is enabled */
  1703. if((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  1704. {
  1705. /* Wait until TEACK flag is set */
  1706. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  1707. {
  1708. /* Timeout Occurred */
  1709. return HAL_TIMEOUT;
  1710. }
  1711. }
  1712. /* Initialize the UART State */
  1713. huart->gState= HAL_UART_STATE_READY;
  1714. huart->RxState= HAL_UART_STATE_READY;
  1715. /* Process Unlocked */
  1716. __HAL_UNLOCK(huart);
  1717. return HAL_OK;
  1718. }
  1719. /**
  1720. * @brief Enables the UART transmitter and disables the UART receiver.
  1721. * @param huart: UART handle
  1722. * @retval HAL status
  1723. * @retval None
  1724. */
  1725. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
  1726. {
  1727. /* Process Locked */
  1728. __HAL_LOCK(huart);
  1729. huart->gState = HAL_UART_STATE_BUSY;
  1730. /* Clear TE and RE bits */
  1731. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1732. /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
  1733. SET_BIT(huart->Instance->CR1, USART_CR1_TE);
  1734. huart->gState= HAL_UART_STATE_READY;
  1735. /* Process Unlocked */
  1736. __HAL_UNLOCK(huart);
  1737. return HAL_OK;
  1738. }
  1739. /**
  1740. * @brief Enables the UART receiver and disables the UART transmitter.
  1741. * @param huart: UART handle
  1742. * @retval HAL status
  1743. */
  1744. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
  1745. {
  1746. /* Process Locked */
  1747. __HAL_LOCK(huart);
  1748. huart->gState = HAL_UART_STATE_BUSY;
  1749. /* Clear TE and RE bits */
  1750. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1751. /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
  1752. SET_BIT(huart->Instance->CR1, USART_CR1_RE);
  1753. huart->gState = HAL_UART_STATE_READY;
  1754. /* Process Unlocked */
  1755. __HAL_UNLOCK(huart);
  1756. return HAL_OK;
  1757. }
  1758. /**
  1759. * @brief Transmits break characters.
  1760. * @param huart: UART handle
  1761. * @retval HAL status
  1762. */
  1763. HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
  1764. {
  1765. /* Check the parameters */
  1766. assert_param(IS_UART_INSTANCE(huart->Instance));
  1767. /* Process Locked */
  1768. __HAL_LOCK(huart);
  1769. huart->gState = HAL_UART_STATE_BUSY;
  1770. /* Send break characters */
  1771. SET_BIT(huart->Instance->RQR, UART_SENDBREAK_REQUEST);
  1772. huart->gState = HAL_UART_STATE_READY;
  1773. /* Process Unlocked */
  1774. __HAL_UNLOCK(huart);
  1775. return HAL_OK;
  1776. }
  1777. /**
  1778. * @brief By default in multiprocessor mode, when the wake up method is set
  1779. * to address mark, the UART handles only 4-bit long addresses detection;
  1780. * this API allows to enable longer addresses detection (6-, 7- or 8-bit
  1781. * long).
  1782. * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
  1783. * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
  1784. * @param huart: UART handle.
  1785. * @param AddressLength: this parameter can be one of the following values:
  1786. * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
  1787. * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
  1788. * @retval HAL status
  1789. */
  1790. HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
  1791. {
  1792. /* Check the UART handle allocation */
  1793. if(huart == NULL)
  1794. {
  1795. return HAL_ERROR;
  1796. }
  1797. /* Check the address length parameter */
  1798. assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
  1799. huart->gState = HAL_UART_STATE_BUSY;
  1800. /* Disable the Peripheral */
  1801. __HAL_UART_DISABLE(huart);
  1802. /* Set the address length */
  1803. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
  1804. /* Enable the Peripheral */
  1805. __HAL_UART_ENABLE(huart);
  1806. /* TEACK and/or REACK to check before moving huart->gState to Ready */
  1807. return (UART_CheckIdleState(huart));
  1808. }
  1809. /**
  1810. * @}
  1811. */
  1812. /**
  1813. * @}
  1814. */
  1815. #endif /* HAL_UART_MODULE_ENABLED */
  1816. /**
  1817. * @}
  1818. */
  1819. /**
  1820. * @}
  1821. */
  1822. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/