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.
 
 
 

1987 lines
62 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_usart.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 14-April-2017
  7. * @brief USART HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
  10. * Peripheral (USART).
  11. * + Initialization and de-initialization functions
  12. * + IO operation functions
  13. * + Peripheral Control functions
  14. *
  15. @verbatim
  16. ===============================================================================
  17. ##### How to use this driver #####
  18. ===============================================================================
  19. [..]
  20. The USART HAL driver can be used as follows:
  21. (#) Declare a USART_HandleTypeDef handle structure.
  22. (#) Initialize the USART low level resources by implement the HAL_USART_MspInit ()API:
  23. (##) Enable the USARTx interface clock.
  24. (##) USART pins configuration:
  25. (+++) Enable the clock for the USART GPIOs.
  26. (+++) Configure these USART pins as alternate function pull-up.
  27. (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
  28. HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
  29. (+++) Configure the USARTx interrupt priority.
  30. (+++) Enable the NVIC USART IRQ handle.
  31. (+++) The specific USART interrupts (Transmission complete interrupt,
  32. RXNE interrupt and Error Interrupts) will be managed using the macros
  33. __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
  34. (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
  35. HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
  36. (+++) Declare a DMA handle structure for the Tx/Rx stream.
  37. (+++) Enable the DMAx interface clock.
  38. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  39. (+++) Configure the DMA Tx/Rx Stream.
  40. (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
  41. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
  42. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  43. flow control and Mode(Receiver/Transmitter) in the husart Init structure.
  44. (#) Initialize the USART registers by calling the HAL_USART_Init() API:
  45. (++) These API's configures also the low level Hardware (GPIO, CLOCK, CORTEX...etc)
  46. by calling the customed HAL_USART_MspInit(&husart) API.
  47. @endverbatim
  48. ******************************************************************************
  49. * @attention
  50. *
  51. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  52. *
  53. * Redistribution and use in source and binary forms, with or without modification,
  54. * are permitted provided that the following conditions are met:
  55. * 1. Redistributions of source code must retain the above copyright notice,
  56. * this list of conditions and the following disclaimer.
  57. * 2. Redistributions in binary form must reproduce the above copyright notice,
  58. * this list of conditions and the following disclaimer in the documentation
  59. * and/or other materials provided with the distribution.
  60. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  61. * may be used to endorse or promote products derived from this software
  62. * without specific prior written permission.
  63. *
  64. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  65. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  66. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  67. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  68. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  69. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  70. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  71. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  72. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  73. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  74. *
  75. ******************************************************************************
  76. */
  77. /* Includes ------------------------------------------------------------------*/
  78. #include "stm32f7xx_hal.h"
  79. /** @addtogroup STM32F7xx_HAL_Driver
  80. * @{
  81. */
  82. /** @defgroup USART USART
  83. * @brief HAL USART Synchronous module driver
  84. * @{
  85. */
  86. #ifdef HAL_USART_MODULE_ENABLED
  87. /* Private typedef -----------------------------------------------------------*/
  88. /* Private define ------------------------------------------------------------*/
  89. /** @addtogroup USART_Private_Constants
  90. * @{
  91. */
  92. #define DUMMY_DATA ((uint16_t) 0xFFFFU)
  93. #define TEACK_REACK_TIMEOUT ((uint32_t) 1000U)
  94. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  95. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
  96. #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | \
  97. USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP))
  98. /**
  99. * @}
  100. */
  101. /* Private macro -------------------------------------------------------------*/
  102. /* Private variables ---------------------------------------------------------*/
  103. /* Private function prototypes -----------------------------------------------*/
  104. /* Private functions ---------------------------------------------------------*/
  105. /** @addtogroup USART_Private_Functions
  106. * @{
  107. */
  108. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  109. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  110. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  111. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  112. static void USART_DMAError(DMA_HandleTypeDef *hdma);
  113. static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  114. static void USART_EndTxTransfer(USART_HandleTypeDef *husart);
  115. static void USART_EndRxTransfer(USART_HandleTypeDef *husart);
  116. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  117. static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart);
  118. static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart);
  119. static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
  120. static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
  121. static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
  122. static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
  123. /**
  124. * @}
  125. */
  126. /* Exported functions --------------------------------------------------------*/
  127. /** @defgroup USART_Exported_Functions USART Exported Functions
  128. * @{
  129. */
  130. /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions
  131. * @brief Initialization and Configuration functions
  132. *
  133. @verbatim
  134. ===============================================================================
  135. ##### Initialization and Configuration functions #####
  136. ===============================================================================
  137. [..]
  138. This subsection provides a set of functions allowing to initialize the USART
  139. in asynchronous and in synchronous modes.
  140. (+) For the asynchronous mode only these parameters can be configured:
  141. (++) Baud Rate
  142. (++) Word Length
  143. (++) Stop Bit
  144. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  145. in the data register is transmitted but is changed by the parity bit.
  146. (++) USART polarity
  147. (++) USART phase
  148. (++) USART LastBit
  149. (++) Receiver/transmitter modes
  150. [..]
  151. The HAL_USART_Init() function follows the USART synchronous configuration
  152. procedure (details for the procedure are available in reference manual).
  153. @endverbatim
  154. Depending on the frame length defined by the M1 and M0 bits (7-bit,
  155. 8-bit or 9-bit), the possible USART frame formats are as listed in the
  156. following table:
  157. +---------------------------------------------------------------+
  158. | M1M0 bits | PCE bit | USART frame |
  159. |-----------------------|---------------------------------------|
  160. | 10 | 0 | | SB | 7-bit data | STB | |
  161. |-----------|-----------|---------------------------------------|
  162. | 10 | 1 | | SB | 6-bit data | PB | STB | |
  163. +---------------------------------------------------------------+
  164. * @{
  165. */
  166. /**
  167. * @brief Initializes the USART mode according to the specified
  168. * parameters in the USART_InitTypeDef and create the associated handle.
  169. * @param husart: USART handle
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
  173. {
  174. /* Check the USART handle allocation */
  175. if(husart == NULL)
  176. {
  177. return HAL_ERROR;
  178. }
  179. /* Check the parameters */
  180. assert_param(IS_USART_INSTANCE(husart->Instance));
  181. if(husart->State == HAL_USART_STATE_RESET)
  182. {
  183. /* Allocate lock resource and initialize it */
  184. husart->Lock = HAL_UNLOCKED;
  185. /* Init the low level hardware : GPIO, CLOCK */
  186. HAL_USART_MspInit(husart);
  187. }
  188. husart->State = HAL_USART_STATE_BUSY;
  189. /* Disable the Peripheral */
  190. __HAL_USART_DISABLE(husart);
  191. /* Set the Usart Communication parameters */
  192. if (USART_SetConfig(husart) == HAL_ERROR)
  193. {
  194. return HAL_ERROR;
  195. }
  196. /* In Synchronous mode, the following bits must be kept cleared:
  197. - LINEN bit in the USART_CR2 register
  198. - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/
  199. CLEAR_BIT(husart->Instance->CR2, USART_CR2_LINEN);
  200. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  201. /* Enable the Peripheral */
  202. __HAL_USART_ENABLE(husart);
  203. /* TEACK and/or REACK to check before moving husart->State to Ready */
  204. return (USART_CheckIdleState(husart));
  205. }
  206. /**
  207. * @brief DeInitializes the USART peripheral
  208. * @param husart: USART handle
  209. * @retval HAL status
  210. */
  211. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
  212. {
  213. /* Check the USART handle allocation */
  214. if(husart == NULL)
  215. {
  216. return HAL_ERROR;
  217. }
  218. /* Check the parameters */
  219. assert_param(IS_USART_INSTANCE(husart->Instance));
  220. husart->State = HAL_USART_STATE_BUSY;
  221. husart->Instance->CR1 = 0x0U;
  222. husart->Instance->CR2 = 0x0U;
  223. husart->Instance->CR3 = 0x0U;
  224. /* DeInit the low level hardware */
  225. HAL_USART_MspDeInit(husart);
  226. husart->ErrorCode = HAL_USART_ERROR_NONE;
  227. husart->State = HAL_USART_STATE_RESET;
  228. /* Process Unlock */
  229. __HAL_UNLOCK(husart);
  230. return HAL_OK;
  231. }
  232. /**
  233. * @brief USART MSP Init
  234. * @param husart: USART handle
  235. * @retval None
  236. */
  237. __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
  238. {
  239. /* Prevent unused argument(s) compilation warning */
  240. UNUSED(husart);
  241. /* NOTE : This function should not be modified, when the callback is needed,
  242. the HAL_USART_MspInit can be implemented in the user file
  243. */
  244. }
  245. /**
  246. * @brief USART MSP DeInit
  247. * @param husart: USART handle
  248. * @retval None
  249. */
  250. __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
  251. {
  252. /* Prevent unused argument(s) compilation warning */
  253. UNUSED(husart);
  254. /* NOTE : This function should not be modified, when the callback is needed,
  255. the HAL_USART_MspDeInit can be implemented in the user file
  256. */
  257. }
  258. /**
  259. * @}
  260. */
  261. /** @defgroup USART_Exported_Functions_Group2 IO operation functions
  262. * @brief USART Transmit and Receive functions
  263. *
  264. @verbatim
  265. ===============================================================================
  266. ##### IO operation functions #####
  267. ===============================================================================
  268. This subsection provides a set of functions allowing to manage the USART synchronous
  269. data transfers.
  270. [..] The USART supports master mode only: it cannot receive or send data related to an input
  271. clock (SCLK is always an output).
  272. (#) There are two mode of transfer:
  273. (++) Blocking mode: The communication is performed in polling mode.
  274. The HAL status of all data processing is returned by the same function
  275. after finishing transfer.
  276. (++) No-Blocking mode: The communication is performed using Interrupts
  277. or DMA, These API's return the HAL status.
  278. The end of the data processing will be indicated through the
  279. dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
  280. using DMA mode.
  281. The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks
  282. will be executed respectively at the end of the transmit or Receive process
  283. The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected
  284. (#) Blocking mode API's are :
  285. (++) HAL_USART_Transmit()in simplex mode
  286. (++) HAL_USART_Receive() in full duplex receive only
  287. (++) HAL_USART_TransmitReceive() in full duplex mode
  288. (#) Non-Blocking mode API's with Interrupt are :
  289. (++) HAL_USART_Transmit_IT()in simplex mode
  290. (++) HAL_USART_Receive_IT() in full duplex receive only
  291. (++) HAL_USART_TransmitReceive_IT()in full duplex mode
  292. (++) HAL_USART_IRQHandler()
  293. (#) No-Blocking mode functions with DMA are :
  294. (++) HAL_USART_Transmit_DMA()in simplex mode
  295. (++) HAL_USART_Receive_DMA() in full duplex receive only
  296. (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
  297. (++) HAL_USART_DMAPause()
  298. (++) HAL_USART_DMAResume()
  299. (++) HAL_USART_DMAStop()
  300. (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
  301. (++) HAL_USART_TxCpltCallback()
  302. (++) HAL_USART_RxCpltCallback()
  303. (++) HAL_USART_TxHalfCpltCallback()
  304. (++) HAL_USART_RxHalfCpltCallback()
  305. (++) HAL_USART_ErrorCallback()
  306. (++) HAL_USART_TxRxCpltCallback()
  307. @endverbatim
  308. * @{
  309. */
  310. /**
  311. * @brief Simplex Send an amount of data in blocking mode
  312. * @param husart: USART handle
  313. * @param pTxData: pointer to data buffer
  314. * @param Size: amount of data to be sent
  315. * @param Timeout : Timeout duration
  316. * @retval HAL status
  317. */
  318. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
  319. {
  320. uint16_t* tmp;
  321. uint32_t tickstart = 0U;
  322. if(husart->State == HAL_USART_STATE_READY)
  323. {
  324. if((pTxData == NULL) || (Size == 0U))
  325. {
  326. return HAL_ERROR;
  327. }
  328. /* Process Locked */
  329. __HAL_LOCK(husart);
  330. husart->ErrorCode = HAL_USART_ERROR_NONE;
  331. husart->State = HAL_USART_STATE_BUSY_TX;
  332. /* Init tickstart for timeout managment*/
  333. tickstart = HAL_GetTick();
  334. husart->TxXferSize = Size;
  335. husart->TxXferCount = Size;
  336. /* Check the remaining data to be sent */
  337. while(husart->TxXferCount > 0U)
  338. {
  339. husart->TxXferCount--;
  340. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  341. {
  342. return HAL_TIMEOUT;
  343. }
  344. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  345. {
  346. tmp = (uint16_t*) pTxData;
  347. husart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
  348. pTxData += 2;
  349. }
  350. else
  351. {
  352. husart->Instance->TDR = (*pTxData++ & (uint8_t)0xFFU);
  353. }
  354. }
  355. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  356. {
  357. return HAL_TIMEOUT;
  358. }
  359. husart->State = HAL_USART_STATE_READY;
  360. /* Process Unlocked */
  361. __HAL_UNLOCK(husart);
  362. return HAL_OK;
  363. }
  364. else
  365. {
  366. return HAL_BUSY;
  367. }
  368. }
  369. /**
  370. * @brief Receive an amount of data in blocking mode
  371. * @note To receive synchronous data, dummy data are simultaneously transmitted
  372. * @param husart: USART handle
  373. * @param pRxData: pointer to data buffer
  374. * @param Size: amount of data to be received
  375. * @param Timeout : Timeout duration
  376. * @retval HAL status
  377. */
  378. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  379. {
  380. uint16_t* tmp;
  381. uint16_t uhMask;
  382. uint32_t tickstart = 0U;
  383. if(husart->State == HAL_USART_STATE_READY)
  384. {
  385. if((pRxData == NULL) || (Size == 0U))
  386. {
  387. return HAL_ERROR;
  388. }
  389. /* Process Locked */
  390. __HAL_LOCK(husart);
  391. husart->ErrorCode = HAL_USART_ERROR_NONE;
  392. husart->State = HAL_USART_STATE_BUSY_RX;
  393. /* Init tickstart for timeout managment*/
  394. tickstart = HAL_GetTick();
  395. husart->RxXferSize = Size;
  396. husart->RxXferCount = Size;
  397. /* Computation of USART mask to apply to RDR register */
  398. __HAL_USART_MASK_COMPUTATION(husart);
  399. uhMask = husart->Mask;
  400. /* as long as data have to be received */
  401. while(husart->RxXferCount > 0U)
  402. {
  403. husart->RxXferCount--;
  404. /* Wait until TC flag is set to send dummy byte in order to generate the
  405. * clock for the slave to send data.
  406. * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
  407. * can be written for all the cases. */
  408. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  409. {
  410. return HAL_TIMEOUT;
  411. }
  412. husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x0FFU);
  413. /* Wait for RXNE Flag */
  414. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  415. {
  416. return HAL_TIMEOUT;
  417. }
  418. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  419. {
  420. tmp = (uint16_t*) pRxData ;
  421. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  422. pRxData +=2;
  423. }
  424. else
  425. {
  426. *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  427. }
  428. }
  429. husart->State = HAL_USART_STATE_READY;
  430. /* Process Unlocked */
  431. __HAL_UNLOCK(husart);
  432. return HAL_OK;
  433. }
  434. else
  435. {
  436. return HAL_BUSY;
  437. }
  438. }
  439. /**
  440. * @brief Full-Duplex Send and Receive an amount of data in blocking mode
  441. * @param husart: USART handle
  442. * @param pTxData: pointer to TX data buffer
  443. * @param pRxData: pointer to RX data buffer
  444. * @param Size: amount of data to be sent (same amount to be received)
  445. * @param Timeout : Timeout duration
  446. * @retval HAL status
  447. */
  448. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  449. {
  450. uint16_t* tmp;
  451. uint16_t uhMask;
  452. uint32_t tickstart = 0U;
  453. if(husart->State == HAL_USART_STATE_READY)
  454. {
  455. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  456. {
  457. return HAL_ERROR;
  458. }
  459. /* Process Locked */
  460. __HAL_LOCK(husart);
  461. husart->ErrorCode = HAL_USART_ERROR_NONE;
  462. husart->State = HAL_USART_STATE_BUSY_RX;
  463. /* Init tickstart for timeout managment*/
  464. tickstart = HAL_GetTick();
  465. husart->RxXferSize = Size;
  466. husart->TxXferSize = Size;
  467. husart->TxXferCount = Size;
  468. husart->RxXferCount = Size;
  469. /* Computation of USART mask to apply to RDR register */
  470. __HAL_USART_MASK_COMPUTATION(husart);
  471. uhMask = husart->Mask;
  472. /* Check the remain data to be sent */
  473. while(husart->TxXferCount > 0)
  474. {
  475. husart->TxXferCount--;
  476. husart->RxXferCount--;
  477. /* Wait until TC flag is set to send data */
  478. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  479. {
  480. return HAL_TIMEOUT;
  481. }
  482. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  483. {
  484. tmp = (uint16_t*) pTxData;
  485. husart->Instance->TDR = (*tmp & uhMask);
  486. pTxData += 2;
  487. }
  488. else
  489. {
  490. husart->Instance->TDR = (*pTxData++ & (uint8_t)uhMask);
  491. }
  492. /* Wait for RXNE Flag */
  493. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  494. {
  495. return HAL_TIMEOUT;
  496. }
  497. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  498. {
  499. tmp = (uint16_t*) pRxData ;
  500. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  501. pRxData +=2U;
  502. }
  503. else
  504. {
  505. *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  506. }
  507. }
  508. husart->State = HAL_USART_STATE_READY;
  509. /* Process Unlocked */
  510. __HAL_UNLOCK(husart);
  511. return HAL_OK;
  512. }
  513. else
  514. {
  515. return HAL_BUSY;
  516. }
  517. }
  518. /**
  519. * @brief Send an amount of data in interrupt mode
  520. * @param husart: USART handle
  521. * @param pTxData: pointer to data buffer
  522. * @param Size: amount of data to be sent
  523. * @retval HAL status
  524. */
  525. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
  526. {
  527. if(husart->State == HAL_USART_STATE_READY)
  528. {
  529. if((pTxData == NULL ) || (Size == 0U))
  530. {
  531. return HAL_ERROR;
  532. }
  533. /* Process Locked */
  534. __HAL_LOCK(husart);
  535. husart->pTxBuffPtr = pTxData;
  536. husart->TxXferSize = Size;
  537. husart->TxXferCount = Size;
  538. husart->ErrorCode = HAL_USART_ERROR_NONE;
  539. husart->State = HAL_USART_STATE_BUSY_TX;
  540. /* The USART Error Interrupts: (Frame error, noise error, overrun error)
  541. are not managed by the USART Transmit Process to avoid the overrun interrupt
  542. when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
  543. to benefit for the frame error and noise interrupts the usart mode should be
  544. configured only for transmit "USART_MODE_TX" */
  545. /* Process Unlocked */
  546. __HAL_UNLOCK(husart);
  547. /* Enable the USART Transmit Data Register Empty Interrupt */
  548. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  549. return HAL_OK;
  550. }
  551. else
  552. {
  553. return HAL_BUSY;
  554. }
  555. }
  556. /**
  557. * @brief Receive an amount of data in blocking mode
  558. * To receive synchronous data, dummy data are simultaneously transmitted
  559. * @param husart: USART handle
  560. * @param pRxData: pointer to data buffer
  561. * @param Size: amount of data to be received
  562. * @retval HAL status
  563. */
  564. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  565. {
  566. if(husart->State == HAL_USART_STATE_READY)
  567. {
  568. if((pRxData == NULL ) || (Size == 0U))
  569. {
  570. return HAL_ERROR;
  571. }
  572. /* Process Locked */
  573. __HAL_LOCK(husart);
  574. husart->pRxBuffPtr = pRxData;
  575. husart->RxXferSize = Size;
  576. husart->RxXferCount = Size;
  577. __HAL_USART_MASK_COMPUTATION(husart);
  578. husart->ErrorCode = HAL_USART_ERROR_NONE;
  579. husart->State = HAL_USART_STATE_BUSY_RX;
  580. /* Enable the USART Parity Error and Data Register not empty Interrupts */
  581. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  582. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  583. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  584. /* Process Unlocked */
  585. __HAL_UNLOCK(husart);
  586. /* Send dummy byte in order to generate the clock for the Slave to send the next data */
  587. if(husart->Init.WordLength == USART_WORDLENGTH_9B)
  588. {
  589. husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x01FFU);
  590. }
  591. else
  592. {
  593. husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x00FFU);
  594. }
  595. return HAL_OK;
  596. }
  597. else
  598. {
  599. return HAL_BUSY;
  600. }
  601. }
  602. /**
  603. * @brief Full-Duplex Send and Receive an amount of data in interrupt mode
  604. * @param husart: USART handle
  605. * @param pTxData: pointer to TX data buffer
  606. * @param pRxData: pointer to RX data buffer
  607. * @param Size: amount of data to be sent (same amount to be received)
  608. * @retval HAL status
  609. */
  610. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  611. {
  612. if(husart->State == HAL_USART_STATE_READY)
  613. {
  614. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  615. {
  616. return HAL_ERROR;
  617. }
  618. /* Process Locked */
  619. __HAL_LOCK(husart);
  620. husart->pRxBuffPtr = pRxData;
  621. husart->RxXferSize = Size;
  622. husart->RxXferCount = Size;
  623. husart->pTxBuffPtr = pTxData;
  624. husart->TxXferSize = Size;
  625. husart->TxXferCount = Size;
  626. /* Computation of USART mask to apply to RDR register */
  627. __HAL_USART_MASK_COMPUTATION(husart);
  628. husart->ErrorCode = HAL_USART_ERROR_NONE;
  629. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  630. /* Enable the USART Data Register not empty Interrupt */
  631. SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  632. /* Enable the USART Parity Error Interrupt */
  633. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  634. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  635. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  636. /* Process Unlocked */
  637. __HAL_UNLOCK(husart);
  638. /* Enable the USART Transmit Data Register Empty Interrupt */
  639. __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
  640. return HAL_OK;
  641. }
  642. else
  643. {
  644. return HAL_BUSY;
  645. }
  646. }
  647. /**
  648. * @brief Send an amount of data in DMA mode
  649. * @param husart: USART handle
  650. * @param pTxData: pointer to data buffer
  651. * @param Size: amount of data to be sent
  652. * @retval HAL status
  653. */
  654. HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
  655. {
  656. uint32_t *tmp;
  657. if(husart->State == HAL_USART_STATE_READY)
  658. {
  659. if((pTxData == NULL ) || (Size == 0U))
  660. {
  661. return HAL_ERROR;
  662. }
  663. /* Process Locked */
  664. __HAL_LOCK(husart);
  665. husart->pTxBuffPtr = pTxData;
  666. husart->TxXferSize = Size;
  667. husart->TxXferCount = Size;
  668. husart->ErrorCode = HAL_USART_ERROR_NONE;
  669. husart->State = HAL_USART_STATE_BUSY_TX;
  670. /* Set the USART DMA transfer complete callback */
  671. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  672. /* Set the USART DMA Half transfer complete callback */
  673. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  674. /* Set the DMA error callback */
  675. husart->hdmatx->XferErrorCallback = USART_DMAError;
  676. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  677. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  678. SET_BIT(husart->Instance->ISR, (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE | USART_ISR_ORE));
  679. /* Enable the USART transmit DMA channel */
  680. tmp = (uint32_t*)&pTxData;
  681. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
  682. /* Clear the TC flag in the SR register by writing 0 to it */
  683. __HAL_USART_CLEAR_IT(husart, USART_FLAG_TC);
  684. /* Process Unlocked */
  685. __HAL_UNLOCK(husart);
  686. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  687. in the USART CR3 register */
  688. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  689. return HAL_OK;
  690. }
  691. else
  692. {
  693. return HAL_BUSY;
  694. }
  695. }
  696. /**
  697. * @brief Receive an amount of data in DMA mode
  698. * @param husart: USART handle
  699. * @param pRxData: pointer to data buffer
  700. * @param Size: amount of data to be received
  701. * @note When the USART parity is enabled (PCE = 1), the received data contain
  702. * the parity bit (MSB position)
  703. * @retval HAL status
  704. * @note The USART DMA transmit stream must be configured in order to generate the clock for the slave.
  705. */
  706. HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  707. {
  708. uint32_t *tmp;
  709. if(husart->State == HAL_USART_STATE_READY)
  710. {
  711. if((pRxData == NULL ) || (Size == 0U))
  712. {
  713. return HAL_ERROR;
  714. }
  715. /* Process Locked */
  716. __HAL_LOCK(husart);
  717. husart->pRxBuffPtr = pRxData;
  718. husart->RxXferSize = Size;
  719. husart->pTxBuffPtr = pRxData;
  720. husart->TxXferSize = Size;
  721. husart->ErrorCode = HAL_USART_ERROR_NONE;
  722. husart->State = HAL_USART_STATE_BUSY_RX;
  723. /* Set the USART DMA Rx transfer complete callback */
  724. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  725. /* Set the USART DMA Half transfer complete callback */
  726. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  727. /* Set the USART DMA Rx transfer error callback */
  728. husart->hdmarx->XferErrorCallback = USART_DMAError;
  729. /* Set the DMA abort callback */
  730. husart->hdmatx->XferAbortCallback = NULL;
  731. /* Set the USART Tx DMA transfer complete callback as NULL because the communication closing
  732. is performed in DMA reception complete callback */
  733. husart->hdmatx->XferHalfCpltCallback = NULL;
  734. husart->hdmatx->XferCpltCallback = NULL;
  735. /* Set the DMA error callback */
  736. husart->hdmatx->XferErrorCallback = USART_DMAError;
  737. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  738. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  739. SET_BIT(husart->Instance->ISR, (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE | USART_ISR_ORE));
  740. /* Enable the USART receive DMA channel */
  741. tmp = (uint32_t*)&pRxData;
  742. HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
  743. /* Enable the USART transmit DMA channel: the transmit stream is used in order
  744. to generate in the non-blocking mode the clock to the slave device,
  745. this mode isn't a simplex receive mode but a full-duplex receive mode */
  746. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
  747. /* Process Unlocked */
  748. __HAL_UNLOCK(husart);
  749. /* Enable the USART Parity Error Interrupt */
  750. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  751. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  752. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  753. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  754. in the USART CR3 register */
  755. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  756. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  757. in the USART CR3 register */
  758. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  759. return HAL_OK;
  760. }
  761. else
  762. {
  763. return HAL_BUSY;
  764. }
  765. }
  766. /**
  767. * @brief Full-Duplex Transmit Receive an amount of data in non blocking mode
  768. * @param husart: USART handle
  769. * @param pTxData: pointer to TX data buffer
  770. * @param pRxData: pointer to RX data buffer
  771. * @param Size: amount of data to be received/sent
  772. * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
  773. * @retval HAL status
  774. */
  775. HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  776. {
  777. uint32_t *tmp;
  778. if(husart->State == HAL_USART_STATE_READY)
  779. {
  780. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  781. {
  782. return HAL_ERROR;
  783. }
  784. /* Process Locked */
  785. __HAL_LOCK(husart);
  786. husart->pRxBuffPtr = pRxData;
  787. husart->RxXferSize = Size;
  788. husart->pTxBuffPtr = pTxData;
  789. husart->TxXferSize = Size;
  790. husart->ErrorCode = HAL_USART_ERROR_NONE;
  791. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  792. /* Set the USART DMA Rx transfer complete callback */
  793. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  794. /* Set the USART DMA Half transfer complete callback */
  795. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  796. /* Set the USART DMA Tx transfer complete callback */
  797. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  798. /* Set the USART DMA Half transfer complete callback */
  799. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  800. /* Set the USART DMA Tx transfer error callback */
  801. husart->hdmatx->XferErrorCallback = USART_DMAError;
  802. /* Set the USART DMA Rx transfer error callback */
  803. husart->hdmarx->XferErrorCallback = USART_DMAError;
  804. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  805. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  806. SET_BIT(husart->Instance->ISR, (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE | USART_ISR_ORE));
  807. /* Enable the USART receive DMA channel */
  808. tmp = (uint32_t*)&pRxData;
  809. HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
  810. /* Enable the USART transmit DMA channel */
  811. tmp = (uint32_t*)&pTxData;
  812. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
  813. /* Clear the TC flag in the SR register by writing 0 to it */
  814. __HAL_USART_CLEAR_IT(husart, USART_FLAG_TC);
  815. /* Process Unlocked */
  816. __HAL_UNLOCK(husart);
  817. /* Enable the USART Parity Error Interrupt */
  818. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  819. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  820. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  821. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  822. in the USART CR3 register */
  823. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  824. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  825. in the USART CR3 register */
  826. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  827. return HAL_OK;
  828. }
  829. else
  830. {
  831. return HAL_BUSY;
  832. }
  833. }
  834. /**
  835. * @brief Pauses the DMA Transfer.
  836. * @param husart: USART handle
  837. * @retval None
  838. */
  839. HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
  840. {
  841. /* Process Locked */
  842. __HAL_LOCK(husart);
  843. if(husart->State == HAL_USART_STATE_BUSY_TX)
  844. {
  845. /* Disable the USART DMA Tx request */
  846. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  847. }
  848. else if(husart->State == HAL_USART_STATE_BUSY_RX)
  849. {
  850. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  851. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  852. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  853. /* Disable the USART DMA Rx request */
  854. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  855. }
  856. else if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
  857. {
  858. /* Disable the USART DMA Tx request */
  859. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  860. /* Disable the USART DMA Rx request */
  861. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  862. }
  863. /* Process Unlocked */
  864. __HAL_UNLOCK(husart);
  865. return HAL_OK;
  866. }
  867. /**
  868. * @brief Resumes the DMA Transfer.
  869. * @param husart: USART handle
  870. * @retval None
  871. */
  872. HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
  873. {
  874. /* Process Locked */
  875. __HAL_LOCK(husart);
  876. if(husart->State == HAL_USART_STATE_BUSY_TX)
  877. {
  878. /* Enable the USART DMA Tx request */
  879. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  880. }
  881. else if(husart->State == HAL_USART_STATE_BUSY_RX)
  882. {
  883. /* Clear the Overrun flag before resuming the Rx transfer*/
  884. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
  885. /* Reenable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  886. SET_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  887. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  888. /* Enable the USART DMA Rx request */
  889. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  890. }
  891. else if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
  892. {
  893. /* Clear the Overrun flag before resuming the Rx transfer*/
  894. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
  895. /* Enable the USART DMA Rx request before the DMA Tx request */
  896. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  897. /* Enable the USART DMA Tx request */
  898. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  899. }
  900. /* Process Unlocked */
  901. __HAL_UNLOCK(husart);
  902. return HAL_OK;
  903. }
  904. /**
  905. * @brief Stops the DMA Transfer.
  906. * @param husart: USART handle
  907. * @retval None
  908. */
  909. HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
  910. {
  911. /* The Lock is not implemented on this API to allow the user application
  912. to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() /
  913. HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback:
  914. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  915. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  916. the stream and the corresponding call back is executed. */
  917. /* Stop USART DMA Tx request if ongoing */
  918. if ((husart->State == HAL_USART_STATE_BUSY_TX) &&
  919. (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)))
  920. {
  921. USART_EndTxTransfer(husart);
  922. /* Abort the USART DMA Tx channel */
  923. if(husart->hdmatx != NULL)
  924. {
  925. HAL_DMA_Abort(husart->hdmatx);
  926. }
  927. /* Disable the USART Tx DMA request */
  928. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  929. }
  930. /* Stop USART DMA Rx request if ongoing */
  931. if ((husart->State == HAL_USART_STATE_BUSY_RX) &&
  932. (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)))
  933. {
  934. USART_EndRxTransfer(husart);
  935. /* Abort the USART DMA Rx channel */
  936. if(husart->hdmarx != NULL)
  937. {
  938. HAL_DMA_Abort(husart->hdmarx);
  939. }
  940. /* Disable the USART Rx DMA request */
  941. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  942. }
  943. return HAL_OK;
  944. }
  945. /**
  946. * @brief This function handles USART interrupt request.
  947. * @param husart: USART handle
  948. * @retval None
  949. */
  950. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
  951. {
  952. uint32_t isrflags = READ_REG(husart->Instance->ISR);
  953. uint32_t cr1its = READ_REG(husart->Instance->CR1);
  954. uint32_t cr3its = READ_REG(husart->Instance->CR3);
  955. uint32_t errorflags;
  956. /* If no error occurs */
  957. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
  958. if (errorflags == RESET)
  959. {
  960. /* USART in mode Receiver --------------------------------------------------*/
  961. if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  962. {
  963. if(husart->State == HAL_USART_STATE_BUSY_RX)
  964. {
  965. USART_Receive_IT(husart);
  966. }
  967. else
  968. {
  969. USART_TransmitReceive_IT(husart);
  970. }
  971. }
  972. }
  973. /* If some errors occur */
  974. if( (errorflags != RESET)
  975. && ( ((cr3its & USART_CR3_EIE) != RESET)
  976. || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)) )
  977. {
  978. /* USART parity error interrupt occurred ------------------------------------*/
  979. if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  980. {
  981. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_PEF);
  982. husart->ErrorCode |= HAL_USART_ERROR_PE;
  983. }
  984. /* USART frame error interrupt occurred -------------------------------------*/
  985. if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  986. {
  987. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_FEF);
  988. husart->ErrorCode |= HAL_USART_ERROR_FE;
  989. }
  990. /* USART noise error interrupt occurred -------------------------------------*/
  991. if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  992. {
  993. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_NEF);
  994. husart->ErrorCode |= HAL_USART_ERROR_NE;
  995. }
  996. /* USART Over-Run interrupt occurred ----------------------------------------*/
  997. if(((isrflags & USART_ISR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  998. {
  999. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
  1000. husart->ErrorCode |= HAL_USART_ERROR_ORE;
  1001. }
  1002. /* Call USART Error Call back function if need be --------------------------*/
  1003. if(husart->ErrorCode != HAL_USART_ERROR_NONE)
  1004. {
  1005. /* USART in mode Receiver ---------------------------------------------------*/
  1006. if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1007. {
  1008. USART_Receive_IT(husart);
  1009. }
  1010. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1011. consider error as blocking */
  1012. if (((husart->ErrorCode & HAL_USART_ERROR_ORE) != RESET) ||
  1013. (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)))
  1014. {
  1015. /* Blocking error : transfer is aborted
  1016. Set the USART state ready to be able to start again the process,
  1017. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1018. USART_EndRxTransfer(husart);
  1019. /* Disable the USART DMA Rx request if enabled */
  1020. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1021. {
  1022. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1023. /* Abort the USART DMA Rx channel */
  1024. if(husart->hdmarx != NULL)
  1025. {
  1026. /* Set the USART DMA Abort callback :
  1027. will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
  1028. husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
  1029. /* Abort DMA RX */
  1030. if(HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
  1031. {
  1032. /* Call Directly husart->hdmarx->XferAbortCallback function in case of error */
  1033. husart->hdmarx->XferAbortCallback(husart->hdmarx);
  1034. }
  1035. }
  1036. else
  1037. {
  1038. /* Call user error callback */
  1039. HAL_USART_ErrorCallback(husart);
  1040. }
  1041. }
  1042. else
  1043. {
  1044. /* Call user error callback */
  1045. HAL_USART_ErrorCallback(husart);
  1046. }
  1047. }
  1048. else
  1049. {
  1050. /* Non Blocking error : transfer could go on.
  1051. Error is notified to user through user error callback */
  1052. HAL_USART_ErrorCallback(husart);
  1053. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1054. }
  1055. }
  1056. return;
  1057. } /* End if some error occurs */
  1058. /* USART in mode Transmitter -----------------------------------------------*/
  1059. if(((isrflags & USART_ISR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1060. {
  1061. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1062. {
  1063. USART_Transmit_IT(husart);
  1064. }
  1065. else
  1066. {
  1067. USART_TransmitReceive_IT(husart);
  1068. }
  1069. return;
  1070. }
  1071. /* USART in mode Transmitter (transmission end) -----------------------------*/
  1072. if(((isrflags & USART_ISR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1073. {
  1074. USART_EndTransmit_IT(husart);
  1075. return;
  1076. }
  1077. }
  1078. /**
  1079. * @brief Tx Transfer completed callbacks
  1080. * @param husart: USART handle
  1081. * @retval None
  1082. */
  1083. __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
  1084. {
  1085. /* Prevent unused argument(s) compilation warning */
  1086. UNUSED(husart);
  1087. /* NOTE : This function should not be modified, when the callback is needed,
  1088. the HAL_USART_TxCpltCallback can be implemented in the user file
  1089. */
  1090. }
  1091. /**
  1092. * @brief Tx Half Transfer completed callbacks.
  1093. * @param husart: USART handle
  1094. * @retval None
  1095. */
  1096. __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
  1097. {
  1098. /* Prevent unused argument(s) compilation warning */
  1099. UNUSED(husart);
  1100. /* NOTE: This function should not be modified, when the callback is needed,
  1101. the HAL_USART_TxHalfCpltCallback can be implemented in the user file
  1102. */
  1103. }
  1104. /**
  1105. * @brief Rx Transfer completed callbacks.
  1106. * @param husart: USART handle
  1107. * @retval None
  1108. */
  1109. __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
  1110. {
  1111. /* Prevent unused argument(s) compilation warning */
  1112. UNUSED(husart);
  1113. /* NOTE: This function should not be modified, when the callback is needed,
  1114. the HAL_USART_RxCpltCallback can be implemented in the user file
  1115. */
  1116. }
  1117. /**
  1118. * @brief Rx Half Transfer completed callbacks
  1119. * @param husart: usart handle
  1120. * @retval None
  1121. */
  1122. __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
  1123. {
  1124. /* Prevent unused argument(s) compilation warning */
  1125. UNUSED(husart);
  1126. /* NOTE : This function should not be modified, when the callback is needed,
  1127. the HAL_USART_RxHalfCpltCallback can be implemented in the user file
  1128. */
  1129. }
  1130. /**
  1131. * @brief Tx/Rx Transfers completed callback for the non-blocking process
  1132. * @param husart: USART handle
  1133. * @retval None
  1134. */
  1135. __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
  1136. {
  1137. /* Prevent unused argument(s) compilation warning */
  1138. UNUSED(husart);
  1139. /* NOTE : This function should not be modified, when the callback is needed,
  1140. the HAL_USART_TxRxCpltCallback can be implemented in the user file
  1141. */
  1142. }
  1143. /**
  1144. * @brief USART error callbacks
  1145. * @param husart: USART handle
  1146. * @retval None
  1147. */
  1148. __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
  1149. {
  1150. /* Prevent unused argument(s) compilation warning */
  1151. UNUSED(husart);
  1152. /* NOTE : This function should not be modified, when the callback is needed,
  1153. the HAL_USART_ErrorCallback can be implemented in the user file
  1154. */
  1155. }
  1156. /**
  1157. * @}
  1158. */
  1159. /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions
  1160. * @brief USART State and Errors functions
  1161. *
  1162. @verbatim
  1163. ==============================================================================
  1164. ##### Peripheral State and Errors functions #####
  1165. ==============================================================================
  1166. [..]
  1167. This subsection provides a set of functions allowing to return the State of
  1168. USART communication
  1169. process, return Peripheral Errors occurred during communication process
  1170. (+) HAL_USART_GetState() API can be helpful to check in run-time the state
  1171. of the USART peripheral.
  1172. (+) HAL_USART_GetError() check in run-time errors that could be occurred during
  1173. communication.
  1174. @endverbatim
  1175. * @{
  1176. */
  1177. /**
  1178. * @brief return the USART state
  1179. * @param husart: USART handle
  1180. * @retval HAL state
  1181. */
  1182. HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
  1183. {
  1184. return husart->State;
  1185. }
  1186. /**
  1187. * @brief Return the USART error code
  1188. * @param husart : pointer to a USART_HandleTypeDef structure that contains
  1189. * the configuration information for the specified USART.
  1190. * @retval USART Error Code
  1191. */
  1192. uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
  1193. {
  1194. return husart->ErrorCode;
  1195. }
  1196. /**
  1197. * @}
  1198. */
  1199. /**
  1200. * @brief Simplex Send an amount of data in non-blocking mode.
  1201. * @note Function called under interruption only, once
  1202. * interruptions have been enabled by HAL_USART_Transmit_IT().
  1203. * @param husart: USART handle
  1204. * @retval HAL status
  1205. * @note The USART errors are not managed to avoid the overrun error.
  1206. */
  1207. static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
  1208. {
  1209. uint16_t* tmp;
  1210. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1211. {
  1212. if(husart->TxXferCount == 0U)
  1213. {
  1214. /* Disable the USART Transmit data register empty interrupt */
  1215. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  1216. /* Enable the USART Transmit Complete Interrupt */
  1217. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  1218. return HAL_OK;
  1219. }
  1220. else
  1221. {
  1222. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1223. {
  1224. tmp = (uint16_t*) husart->pTxBuffPtr;
  1225. husart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
  1226. husart->pTxBuffPtr += 2U;
  1227. }
  1228. else
  1229. {
  1230. husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0xFF);
  1231. }
  1232. husart->TxXferCount--;
  1233. return HAL_OK;
  1234. }
  1235. }
  1236. else
  1237. {
  1238. return HAL_BUSY;
  1239. }
  1240. }
  1241. /**
  1242. * @brief Wraps up transmission in non-blocking mode.
  1243. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1244. * the configuration information for the specified USART module.
  1245. * @retval HAL status
  1246. */
  1247. static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
  1248. {
  1249. /* Disable the USART Transmit Complete Interrupt */
  1250. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TCIE);
  1251. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1252. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1253. husart->State = HAL_USART_STATE_READY;
  1254. HAL_USART_TxCpltCallback(husart);
  1255. return HAL_OK;
  1256. }
  1257. /**
  1258. * @brief Simplex Receive an amount of data in non-blocking mode.
  1259. * Function called under interruption only, once
  1260. * interruptions have been enabled by HAL_USART_Receive_IT()
  1261. * @param husart: USART handle
  1262. * @retval HAL status
  1263. */
  1264. static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
  1265. {
  1266. uint16_t* tmp;
  1267. uint16_t uhMask = husart->Mask;
  1268. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1269. {
  1270. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1271. {
  1272. tmp = (uint16_t*) husart->pRxBuffPtr;
  1273. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  1274. husart->pRxBuffPtr += 2U;
  1275. }
  1276. else
  1277. {
  1278. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  1279. }
  1280. /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
  1281. husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x00FFU);
  1282. if(--husart->RxXferCount == 0U)
  1283. {
  1284. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1285. /* Disable the USART Parity Error Interrupt */
  1286. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1287. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1288. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1289. husart->State = HAL_USART_STATE_READY;
  1290. HAL_USART_RxCpltCallback(husart);
  1291. return HAL_OK;
  1292. }
  1293. return HAL_OK;
  1294. }
  1295. else
  1296. {
  1297. return HAL_BUSY;
  1298. }
  1299. }
  1300. /**
  1301. * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
  1302. * Function called under interruption only, once
  1303. * interruptions have been enabled by HAL_USART_TransmitReceive_IT()
  1304. * @param husart: USART handle
  1305. * @retval HAL status
  1306. */
  1307. static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
  1308. {
  1309. uint16_t* tmp;
  1310. uint16_t uhMask = husart->Mask;
  1311. if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
  1312. {
  1313. if(husart->TxXferCount != 0x00U)
  1314. {
  1315. if(__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
  1316. {
  1317. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1318. {
  1319. tmp = (uint16_t*) husart->pTxBuffPtr;
  1320. husart->Instance->TDR = (uint16_t)(*tmp & uhMask);
  1321. husart->pTxBuffPtr += 2U;
  1322. }
  1323. else
  1324. {
  1325. husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)uhMask);
  1326. }
  1327. husart->TxXferCount--;
  1328. /* Check the latest data transmitted */
  1329. if(husart->TxXferCount == 0U)
  1330. {
  1331. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  1332. }
  1333. }
  1334. }
  1335. if(husart->RxXferCount != 0x00U)
  1336. {
  1337. if(__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
  1338. {
  1339. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1340. {
  1341. tmp = (uint16_t*) husart->pRxBuffPtr;
  1342. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  1343. husart->pRxBuffPtr += 2U;
  1344. }
  1345. else
  1346. {
  1347. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  1348. }
  1349. husart->RxXferCount--;
  1350. }
  1351. }
  1352. /* Check the latest data received */
  1353. if(husart->RxXferCount == 0U)
  1354. {
  1355. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1356. /* Disable the USART Parity Error Interrupt */
  1357. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1358. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1359. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1360. husart->State = HAL_USART_STATE_READY;
  1361. HAL_USART_TxRxCpltCallback(husart);
  1362. return HAL_OK;
  1363. }
  1364. return HAL_OK;
  1365. }
  1366. else
  1367. {
  1368. return HAL_BUSY;
  1369. }
  1370. }
  1371. /**
  1372. * @brief This function handles USART Communication Timeout.
  1373. * @param husart USART handle
  1374. * @param Flag specifies the USART flag to check.
  1375. * @param Status The new Flag status (SET or RESET).
  1376. * @param Tickstart Tick start value
  1377. * @param Timeout Timeout duration
  1378. * @retval HAL status
  1379. */
  1380. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1381. {
  1382. /* Wait until flag is set */
  1383. while((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
  1384. {
  1385. /* Check for the Timeout */
  1386. if(Timeout != HAL_MAX_DELAY)
  1387. {
  1388. if((Timeout == 0U)||((HAL_GetTick()-Tickstart) >= Timeout))
  1389. {
  1390. /* Disable the USART Transmit Complete Interrupt */
  1391. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  1392. /* Disable the USART RXNE Interrupt */
  1393. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1394. /* Disable the USART Parity Error Interrupt */
  1395. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1396. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1397. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1398. husart->State= HAL_USART_STATE_READY;
  1399. /* Process Unlocked */
  1400. __HAL_UNLOCK(husart);
  1401. return HAL_TIMEOUT;
  1402. }
  1403. }
  1404. }
  1405. return HAL_OK;
  1406. }
  1407. /**
  1408. * @brief DMA USART transmit process complete callback
  1409. * @param hdma: DMA handle
  1410. * @retval None
  1411. */
  1412. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1413. {
  1414. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1415. /* DMA Normal mode */
  1416. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
  1417. {
  1418. husart->TxXferCount = 0U;
  1419. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1420. {
  1421. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  1422. in the USART CR3 register */
  1423. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1424. /* Enable the USART Transmit Complete Interrupt */
  1425. SET_BIT(husart->Instance->CR1, USART_CR1_TCIE);
  1426. }
  1427. }
  1428. /* DMA Circular mode */
  1429. else
  1430. {
  1431. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1432. {
  1433. HAL_USART_TxCpltCallback(husart);
  1434. }
  1435. }
  1436. }
  1437. /**
  1438. * @brief DMA USART transmit process half complete callback
  1439. * @param hdma : DMA handle
  1440. * @retval None
  1441. */
  1442. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1443. {
  1444. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1445. HAL_USART_TxHalfCpltCallback(husart);
  1446. }
  1447. /**
  1448. * @brief DMA USART receive process complete callback
  1449. * @param hdma: DMA handle
  1450. * @retval None
  1451. */
  1452. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1453. {
  1454. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1455. /* DMA Normal mode */
  1456. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
  1457. {
  1458. husart->RxXferCount = 0U;
  1459. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1460. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1461. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1462. /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
  1463. in USART CR3 register */
  1464. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1465. /* similarly, disable the DMA TX transfer that was started to provide the
  1466. clock to the slave device */
  1467. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1468. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1469. {
  1470. HAL_USART_RxCpltCallback(husart);
  1471. }
  1472. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  1473. else
  1474. {
  1475. HAL_USART_TxRxCpltCallback(husart);
  1476. }
  1477. husart->State= HAL_USART_STATE_READY;
  1478. }
  1479. /* DMA circular mode */
  1480. else
  1481. {
  1482. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1483. {
  1484. HAL_USART_RxCpltCallback(husart);
  1485. }
  1486. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  1487. else
  1488. {
  1489. HAL_USART_TxRxCpltCallback(husart);
  1490. }
  1491. }
  1492. }
  1493. /**
  1494. * @brief DMA USART receive process half complete callback
  1495. * @param hdma : DMA handle
  1496. * @retval None
  1497. */
  1498. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1499. {
  1500. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1501. HAL_USART_RxHalfCpltCallback(husart);
  1502. }
  1503. /**
  1504. * @brief DMA USART communication error callback
  1505. * @param hdma: DMA handle
  1506. * @retval None
  1507. */
  1508. static void USART_DMAError(DMA_HandleTypeDef *hdma)
  1509. {
  1510. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1511. husart->RxXferCount = 0U;
  1512. husart->TxXferCount = 0U;
  1513. /* Stop USART DMA Tx request if ongoing */
  1514. if((husart->State == HAL_USART_STATE_BUSY_TX)
  1515. &&(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)))
  1516. {
  1517. USART_EndTxTransfer(husart);
  1518. }
  1519. /* Stop USART DMA Rx request if ongoing */
  1520. if((husart->State == HAL_USART_STATE_BUSY_RX)
  1521. &&(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)))
  1522. {
  1523. USART_EndRxTransfer(husart);
  1524. }
  1525. husart->ErrorCode |= HAL_USART_ERROR_DMA;
  1526. husart->State= HAL_USART_STATE_READY;
  1527. HAL_USART_ErrorCallback(husart);
  1528. }
  1529. /**
  1530. * @brief DMA USART communication abort callback
  1531. * (To be called at end of DMA Abort procedure).
  1532. * @param hdma: DMA handle.
  1533. * @retval None
  1534. */
  1535. static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1536. {
  1537. USART_HandleTypeDef* husart = (USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1538. husart->RxXferCount = 0U;
  1539. husart->TxXferCount = 0U;
  1540. HAL_USART_ErrorCallback(husart);
  1541. }
  1542. /**
  1543. * @brief End ongoing Tx transfer on USART peripheral (following error detection or Transmit completion).
  1544. * @param husart: USART handle.
  1545. * @retval None
  1546. */
  1547. static void USART_EndTxTransfer(USART_HandleTypeDef *husart)
  1548. {
  1549. /* At end of Tx process, restore husart->State to Ready */
  1550. husart->State = HAL_USART_STATE_READY;
  1551. /* Disable TXEIE and TCIE interrupts */
  1552. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1553. }
  1554. /**
  1555. * @brief End ongoing Rx transfer on USART peripheral (following error detection or Reception completion).
  1556. * @param husart: USART handle.
  1557. * @retval None
  1558. */
  1559. static void USART_EndRxTransfer(USART_HandleTypeDef *husart)
  1560. {
  1561. /* At end of Rx process, restore husart->RxState to Ready */
  1562. husart->State = HAL_USART_STATE_READY;
  1563. /* Disable RXNE, PE and ERR interrupts */
  1564. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1565. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1566. }
  1567. /**
  1568. * @brief Configure the USART peripheral
  1569. * @param husart: USART handle
  1570. * @retval None
  1571. */
  1572. static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart)
  1573. {
  1574. uint32_t tmpreg = 0x0U;
  1575. USART_ClockSourceTypeDef clocksource = USART_CLOCKSOURCE_UNDEFINED;
  1576. HAL_StatusTypeDef ret = HAL_OK;
  1577. uint16_t brrtemp = 0x0000U;
  1578. uint16_t usartdiv = 0x0000U;
  1579. /* Check the parameters */
  1580. assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
  1581. assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
  1582. assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
  1583. assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
  1584. assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
  1585. assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
  1586. assert_param(IS_USART_PARITY(husart->Init.Parity));
  1587. assert_param(IS_USART_MODE(husart->Init.Mode));
  1588. assert_param(IS_USART_OVERSAMPLING(husart->Init.OverSampling));
  1589. /*-------------------------- USART CR1 Configuration -----------------------*/
  1590. /* Clear M, PCE, PS, TE and RE bits and configure
  1591. * the USART Word Length, Parity, Mode and OverSampling:
  1592. * set the M bits according to husart->Init.WordLength value
  1593. * set PCE and PS bits according to husart->Init.Parity value
  1594. * set TE and RE bits according to husart->Init.Mode value
  1595. * force OVER8 to 1 to allow to reach the maximum speed (Fclock/8) */
  1596. tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
  1597. MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  1598. /*---------------------------- USART CR2 Configuration ---------------------*/
  1599. /* Clear and configure the USART Clock, CPOL, CPHA, LBCL and STOP bits:
  1600. * set CPOL bit according to husart->Init.CLKPolarity value
  1601. * set CPHA bit according to husart->Init.CLKPhase value
  1602. * set LBCL bit according to husart->Init.CLKLastBit value
  1603. * set STOP[13:12] bits according to husart->Init.StopBits value */
  1604. tmpreg = (uint32_t)(USART_CLOCK_ENABLE);
  1605. tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase);
  1606. tmpreg |= ((uint32_t)husart->Init.CLKLastBit | (uint32_t)husart->Init.StopBits);
  1607. MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg);
  1608. /*-------------------------- USART CR3 Configuration -----------------------*/
  1609. /* no CR3 register configuration */
  1610. /*-------------------------- USART BRR Configuration -----------------------*/
  1611. /* BRR is filled-up according to OVER8 bit setting which is forced to 1 */
  1612. USART_GETCLOCKSOURCE(husart, clocksource);
  1613. switch (clocksource)
  1614. {
  1615. case USART_CLOCKSOURCE_PCLK1:
  1616. usartdiv = (uint16_t)(((2*HAL_RCC_GetPCLK1Freq()) + (husart->Init.BaudRate/2))/ husart->Init.BaudRate);
  1617. break;
  1618. case USART_CLOCKSOURCE_PCLK2:
  1619. usartdiv = (uint16_t)(((2*HAL_RCC_GetPCLK2Freq()) + (husart->Init.BaudRate/2))/ husart->Init.BaudRate);
  1620. break;
  1621. case USART_CLOCKSOURCE_HSI:
  1622. usartdiv = (uint16_t)(((2*HSI_VALUE) + (husart->Init.BaudRate/2))/ husart->Init.BaudRate);
  1623. break;
  1624. case USART_CLOCKSOURCE_SYSCLK:
  1625. usartdiv = (uint16_t)(((2*HAL_RCC_GetSysClockFreq()) + (husart->Init.BaudRate/2))/ husart->Init.BaudRate);
  1626. break;
  1627. case USART_CLOCKSOURCE_LSE:
  1628. usartdiv = (uint16_t)(((2*LSE_VALUE) + (husart->Init.BaudRate/2))/ husart->Init.BaudRate);
  1629. break;
  1630. case USART_CLOCKSOURCE_UNDEFINED:
  1631. default:
  1632. ret = HAL_ERROR;
  1633. break;
  1634. }
  1635. brrtemp = usartdiv & 0xFFF0U;
  1636. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
  1637. husart->Instance->BRR = brrtemp;
  1638. return ret;
  1639. }
  1640. /**
  1641. * @brief Check the USART Idle State
  1642. * @param husart: USART handle
  1643. * @retval HAL status
  1644. */
  1645. static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart)
  1646. {
  1647. uint32_t tickstart = 0U;
  1648. /* Initialize the USART ErrorCode */
  1649. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1650. /* Init tickstart for timeout managment*/
  1651. tickstart = HAL_GetTick();
  1652. /* Check if the Transmitter is enabled */
  1653. if((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  1654. {
  1655. /* Wait until TEACK flag is set */
  1656. if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, tickstart, TEACK_REACK_TIMEOUT) != HAL_OK)
  1657. {
  1658. husart->State= HAL_USART_STATE_TIMEOUT;
  1659. return HAL_TIMEOUT;
  1660. }
  1661. }
  1662. /* Initialize the USART state*/
  1663. husart->State= HAL_USART_STATE_READY;
  1664. /* Process Unlocked */
  1665. __HAL_UNLOCK(husart);
  1666. return HAL_OK;
  1667. }
  1668. /**
  1669. * @}
  1670. */
  1671. #endif /* HAL_USART_MODULE_ENABLED */
  1672. /**
  1673. * @}
  1674. */
  1675. /**
  1676. * @}
  1677. */
  1678. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/