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.
 
 
 

2118 lines
65 KiB

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