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.
 
 
 

1997 lines
62 KiB

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