训练营PLSR题目
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.
 
 
 
 
 
 

2839 line
99 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_usart.c
  4. * @author MCD Application Team
  5. * @brief USART HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
  8. * Peripheral (USART).
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral Control functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2016 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The USART HAL driver can be used as follows:
  30. (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
  31. (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
  32. (##) Enable the USARTx interface clock.
  33. (##) USART pins configuration:
  34. (+++) Enable the clock for the USART GPIOs.
  35. (+++) Configure the USART pins as alternate function pull-up.
  36. (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
  37. HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
  38. (+++) Configure the USARTx interrupt priority.
  39. (+++) Enable the NVIC USART IRQ handle.
  40. (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
  41. HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
  42. (+++) Declare a DMA handle structure for the Tx/Rx stream.
  43. (+++) Enable the DMAx interface clock.
  44. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  45. (+++) Configure the DMA Tx/Rx stream.
  46. (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
  47. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx stream.
  48. (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
  49. (used for last byte sending completion detection in DMA non circular mode)
  50. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  51. flow control and Mode(Receiver/Transmitter) in the husart Init structure.
  52. (#) Initialize the USART registers by calling the HAL_USART_Init() API:
  53. (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  54. by calling the customized HAL_USART_MspInit(&husart) API.
  55. -@@- The specific USART interrupts (Transmission complete interrupt,
  56. RXNE interrupt and Error Interrupts) will be managed using the macros
  57. __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
  58. (#) Three operation modes are available within this driver :
  59. *** Polling mode IO operation ***
  60. =================================
  61. [..]
  62. (+) Send an amount of data in blocking mode using HAL_USART_Transmit()
  63. (+) Receive an amount of data in blocking mode using HAL_USART_Receive()
  64. *** Interrupt mode IO operation ***
  65. ===================================
  66. [..]
  67. (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT()
  68. (+) At transmission end of transfer HAL_USART_TxHalfCpltCallback is executed and user can
  69. add his own code by customization of function pointer HAL_USART_TxCpltCallback
  70. (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT()
  71. (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
  72. add his own code by customization of function pointer HAL_USART_RxCpltCallback
  73. (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
  74. add his own code by customization of function pointer HAL_USART_ErrorCallback
  75. *** DMA mode IO operation ***
  76. ==============================
  77. [..]
  78. (+) Send an amount of data in non blocking mode (DMA) using HAL_USART_Transmit_DMA()
  79. (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can
  80. add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback
  81. (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can
  82. add his own code by customization of function pointer HAL_USART_TxCpltCallback
  83. (+) Receive an amount of data in non blocking mode (DMA) using HAL_USART_Receive_DMA()
  84. (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can
  85. add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback
  86. (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
  87. add his own code by customization of function pointer HAL_USART_RxCpltCallback
  88. (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
  89. add his own code by customization of function pointer HAL_USART_ErrorCallback
  90. (+) Pause the DMA Transfer using HAL_USART_DMAPause()
  91. (+) Resume the DMA Transfer using HAL_USART_DMAResume()
  92. (+) Stop the DMA Transfer using HAL_USART_DMAStop()
  93. *** USART HAL driver macros list ***
  94. =============================================
  95. [..]
  96. Below the list of most used macros in USART HAL driver.
  97. (+) __HAL_USART_ENABLE: Enable the USART peripheral
  98. (+) __HAL_USART_DISABLE: Disable the USART peripheral
  99. (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not
  100. (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag
  101. (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt
  102. (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt
  103. [..]
  104. (@) You can refer to the USART HAL driver header file for more useful macros
  105. ##### Callback registration #####
  106. ==================================
  107. [..]
  108. The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1
  109. allows the user to configure dynamically the driver callbacks.
  110. [..]
  111. Use Function @ref HAL_USART_RegisterCallback() to register a user callback.
  112. Function @ref HAL_USART_RegisterCallback() allows to register following callbacks:
  113. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  114. (+) TxCpltCallback : Tx Complete Callback.
  115. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  116. (+) RxCpltCallback : Rx Complete Callback.
  117. (+) TxRxCpltCallback : Tx Rx Complete Callback.
  118. (+) ErrorCallback : Error Callback.
  119. (+) AbortCpltCallback : Abort Complete Callback.
  120. (+) MspInitCallback : USART MspInit.
  121. (+) MspDeInitCallback : USART MspDeInit.
  122. This function takes as parameters the HAL peripheral handle, the Callback ID
  123. and a pointer to the user callback function.
  124. [..]
  125. Use function @ref HAL_USART_UnRegisterCallback() to reset a callback to the default
  126. weak (surcharged) function.
  127. @ref HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  128. and the Callback ID.
  129. This function allows to reset following callbacks:
  130. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  131. (+) TxCpltCallback : Tx Complete Callback.
  132. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  133. (+) RxCpltCallback : Rx Complete Callback.
  134. (+) TxRxCpltCallback : Tx Rx Complete Callback.
  135. (+) ErrorCallback : Error Callback.
  136. (+) AbortCpltCallback : Abort Complete Callback.
  137. (+) MspInitCallback : USART MspInit.
  138. (+) MspDeInitCallback : USART MspDeInit.
  139. [..]
  140. By default, after the @ref HAL_USART_Init() and when the state is HAL_USART_STATE_RESET
  141. all callbacks are set to the corresponding weak (surcharged) functions:
  142. examples @ref HAL_USART_TxCpltCallback(), @ref HAL_USART_RxHalfCpltCallback().
  143. Exception done for MspInit and MspDeInit functions that are respectively
  144. reset to the legacy weak (surcharged) functions in the @ref HAL_USART_Init()
  145. and @ref HAL_USART_DeInit() only when these callbacks are null (not registered beforehand).
  146. If not, MspInit or MspDeInit are not null, the @ref HAL_USART_Init() and @ref HAL_USART_DeInit()
  147. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  148. [..]
  149. Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only.
  150. Exception done MspInit/MspDeInit that can be registered/unregistered
  151. in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user)
  152. MspInit/DeInit callbacks can be used during the Init/DeInit.
  153. In that case first register the MspInit/MspDeInit user callbacks
  154. using @ref HAL_USART_RegisterCallback() before calling @ref HAL_USART_DeInit()
  155. or @ref HAL_USART_Init() function.
  156. [..]
  157. When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or
  158. not defined, the callback registration feature is not available
  159. and weak (surcharged) callbacks are used.
  160. @endverbatim
  161. [..]
  162. (@) Additional remark: If the parity is enabled, then the MSB bit of the data written
  163. in the data register is transmitted but is changed by the parity bit.
  164. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  165. the possible USART frame formats are as listed in the following table:
  166. +-------------------------------------------------------------+
  167. | M bit | PCE bit | USART frame |
  168. |---------------------|---------------------------------------|
  169. | 0 | 0 | | SB | 8 bit data | STB | |
  170. |---------|-----------|---------------------------------------|
  171. | 0 | 1 | | SB | 7 bit data | PB | STB | |
  172. |---------|-----------|---------------------------------------|
  173. | 1 | 0 | | SB | 9 bit data | STB | |
  174. |---------|-----------|---------------------------------------|
  175. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  176. +-------------------------------------------------------------+
  177. ******************************************************************************
  178. */
  179. /* Includes ------------------------------------------------------------------*/
  180. #include "stm32f4xx_hal.h"
  181. /** @addtogroup STM32F4xx_HAL_Driver
  182. * @{
  183. */
  184. /** @defgroup USART USART
  185. * @brief HAL USART Synchronous module driver
  186. * @{
  187. */
  188. #ifdef HAL_USART_MODULE_ENABLED
  189. /* Private typedef -----------------------------------------------------------*/
  190. /* Private define ------------------------------------------------------------*/
  191. /** @addtogroup USART_Private_Constants
  192. * @{
  193. */
  194. #define DUMMY_DATA 0xFFFFU
  195. #define USART_TIMEOUT_VALUE 22000U
  196. /**
  197. * @}
  198. */
  199. /* Private macro -------------------------------------------------------------*/
  200. /* Private variables ---------------------------------------------------------*/
  201. /* Private function prototypes -----------------------------------------------*/
  202. /* Private functions ---------------------------------------------------------*/
  203. /** @addtogroup USART_Private_Functions
  204. * @{
  205. */
  206. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  207. void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart);
  208. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  209. static void USART_EndTxTransfer(USART_HandleTypeDef *husart);
  210. static void USART_EndRxTransfer(USART_HandleTypeDef *husart);
  211. static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
  212. static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
  213. static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
  214. static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
  215. static void USART_SetConfig(USART_HandleTypeDef *husart);
  216. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  217. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  218. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  219. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  220. static void USART_DMAError(DMA_HandleTypeDef *hdma);
  221. static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  222. static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  223. static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  224. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status,
  225. uint32_t Tickstart, uint32_t Timeout);
  226. /**
  227. * @}
  228. */
  229. /* Exported functions --------------------------------------------------------*/
  230. /** @defgroup USART_Exported_Functions USART Exported Functions
  231. * @{
  232. */
  233. /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions
  234. * @brief Initialization and Configuration functions
  235. *
  236. @verbatim
  237. ==============================================================================
  238. ##### Initialization and Configuration functions #####
  239. ==============================================================================
  240. [..]
  241. This subsection provides a set of functions allowing to initialize the USART
  242. in asynchronous and in synchronous modes.
  243. (+) For the asynchronous mode only these parameters can be configured:
  244. (++) Baud Rate
  245. (++) Word Length
  246. (++) Stop Bit
  247. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  248. in the data register is transmitted but is changed by the parity bit.
  249. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  250. please refer to Reference manual for possible USART frame formats.
  251. (++) USART polarity
  252. (++) USART phase
  253. (++) USART LastBit
  254. (++) Receiver/transmitter modes
  255. [..]
  256. The HAL_USART_Init() function follows the USART synchronous configuration
  257. procedures (details for the procedures are available in reference manual
  258. (RM0430 for STM32F4X3xx MCUs and RM0402 for STM32F412xx MCUs
  259. RM0383 for STM32F411xC/E MCUs and RM0401 for STM32F410xx MCUs
  260. RM0090 for STM32F4X5xx/STM32F4X7xx/STM32F429xx/STM32F439xx MCUs
  261. RM0390 for STM32F446xx MCUs and RM0386 for STM32F469xx/STM32F479xx MCUs)).
  262. @endverbatim
  263. * @{
  264. */
  265. /**
  266. * @brief Initialize the USART mode according to the specified
  267. * parameters in the USART_InitTypeDef and initialize the associated handle.
  268. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  269. * the configuration information for the specified USART module.
  270. * @retval HAL status
  271. */
  272. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
  273. {
  274. /* Check the USART handle allocation */
  275. if (husart == NULL)
  276. {
  277. return HAL_ERROR;
  278. }
  279. /* Check the parameters */
  280. assert_param(IS_USART_INSTANCE(husart->Instance));
  281. if (husart->State == HAL_USART_STATE_RESET)
  282. {
  283. /* Allocate lock resource and initialize it */
  284. husart->Lock = HAL_UNLOCKED;
  285. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  286. USART_InitCallbacksToDefault(husart);
  287. if (husart->MspInitCallback == NULL)
  288. {
  289. husart->MspInitCallback = HAL_USART_MspInit;
  290. }
  291. /* Init the low level hardware */
  292. husart->MspInitCallback(husart);
  293. #else
  294. /* Init the low level hardware : GPIO, CLOCK */
  295. HAL_USART_MspInit(husart);
  296. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  297. }
  298. husart->State = HAL_USART_STATE_BUSY;
  299. /* Set the USART Communication parameters */
  300. USART_SetConfig(husart);
  301. /* In USART mode, the following bits must be kept cleared:
  302. - LINEN bit in the USART_CR2 register
  303. - HDSEL, SCEN and IREN bits in the USART_CR3 register */
  304. CLEAR_BIT(husart->Instance->CR2, USART_CR2_LINEN);
  305. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  306. /* Enable the Peripheral */
  307. __HAL_USART_ENABLE(husart);
  308. /* Initialize the USART state */
  309. husart->ErrorCode = HAL_USART_ERROR_NONE;
  310. husart->State = HAL_USART_STATE_READY;
  311. return HAL_OK;
  312. }
  313. /**
  314. * @brief DeInitializes the USART peripheral.
  315. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  316. * the configuration information for the specified USART module.
  317. * @retval HAL status
  318. */
  319. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
  320. {
  321. /* Check the USART handle allocation */
  322. if (husart == NULL)
  323. {
  324. return HAL_ERROR;
  325. }
  326. /* Check the parameters */
  327. assert_param(IS_USART_INSTANCE(husart->Instance));
  328. husart->State = HAL_USART_STATE_BUSY;
  329. /* Disable the Peripheral */
  330. __HAL_USART_DISABLE(husart);
  331. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  332. if (husart->MspDeInitCallback == NULL)
  333. {
  334. husart->MspDeInitCallback = HAL_USART_MspDeInit;
  335. }
  336. /* DeInit the low level hardware */
  337. husart->MspDeInitCallback(husart);
  338. #else
  339. /* DeInit the low level hardware */
  340. HAL_USART_MspDeInit(husart);
  341. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  342. husart->ErrorCode = HAL_USART_ERROR_NONE;
  343. husart->State = HAL_USART_STATE_RESET;
  344. /* Release Lock */
  345. __HAL_UNLOCK(husart);
  346. return HAL_OK;
  347. }
  348. /**
  349. * @brief USART MSP Init.
  350. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  351. * the configuration information for the specified USART module.
  352. * @retval None
  353. */
  354. __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
  355. {
  356. /* Prevent unused argument(s) compilation warning */
  357. UNUSED(husart);
  358. /* NOTE: This function should not be modified, when the callback is needed,
  359. the HAL_USART_MspInit could be implemented in the user file
  360. */
  361. }
  362. /**
  363. * @brief USART MSP DeInit.
  364. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  365. * the configuration information for the specified USART module.
  366. * @retval None
  367. */
  368. __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
  369. {
  370. /* Prevent unused argument(s) compilation warning */
  371. UNUSED(husart);
  372. /* NOTE: This function should not be modified, when the callback is needed,
  373. the HAL_USART_MspDeInit could be implemented in the user file
  374. */
  375. }
  376. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  377. /**
  378. * @brief Register a User USART Callback
  379. * To be used instead of the weak predefined callback
  380. * @param husart usart handle
  381. * @param CallbackID ID of the callback to be registered
  382. * This parameter can be one of the following values:
  383. * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  384. * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  385. * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  386. * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  387. * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
  388. * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
  389. * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  390. * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
  391. * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
  392. * @param pCallback pointer to the Callback function
  393. * @retval HAL status
  394. + */
  395. HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID,
  396. pUSART_CallbackTypeDef pCallback)
  397. {
  398. HAL_StatusTypeDef status = HAL_OK;
  399. if (pCallback == NULL)
  400. {
  401. /* Update the error code */
  402. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  403. return HAL_ERROR;
  404. }
  405. /* Process locked */
  406. __HAL_LOCK(husart);
  407. if (husart->State == HAL_USART_STATE_READY)
  408. {
  409. switch (CallbackID)
  410. {
  411. case HAL_USART_TX_HALFCOMPLETE_CB_ID :
  412. husart->TxHalfCpltCallback = pCallback;
  413. break;
  414. case HAL_USART_TX_COMPLETE_CB_ID :
  415. husart->TxCpltCallback = pCallback;
  416. break;
  417. case HAL_USART_RX_HALFCOMPLETE_CB_ID :
  418. husart->RxHalfCpltCallback = pCallback;
  419. break;
  420. case HAL_USART_RX_COMPLETE_CB_ID :
  421. husart->RxCpltCallback = pCallback;
  422. break;
  423. case HAL_USART_TX_RX_COMPLETE_CB_ID :
  424. husart->TxRxCpltCallback = pCallback;
  425. break;
  426. case HAL_USART_ERROR_CB_ID :
  427. husart->ErrorCallback = pCallback;
  428. break;
  429. case HAL_USART_ABORT_COMPLETE_CB_ID :
  430. husart->AbortCpltCallback = pCallback;
  431. break;
  432. case HAL_USART_MSPINIT_CB_ID :
  433. husart->MspInitCallback = pCallback;
  434. break;
  435. case HAL_USART_MSPDEINIT_CB_ID :
  436. husart->MspDeInitCallback = pCallback;
  437. break;
  438. default :
  439. /* Update the error code */
  440. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  441. /* Return error status */
  442. status = HAL_ERROR;
  443. break;
  444. }
  445. }
  446. else if (husart->State == HAL_USART_STATE_RESET)
  447. {
  448. switch (CallbackID)
  449. {
  450. case HAL_USART_MSPINIT_CB_ID :
  451. husart->MspInitCallback = pCallback;
  452. break;
  453. case HAL_USART_MSPDEINIT_CB_ID :
  454. husart->MspDeInitCallback = pCallback;
  455. break;
  456. default :
  457. /* Update the error code */
  458. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  459. /* Return error status */
  460. status = HAL_ERROR;
  461. break;
  462. }
  463. }
  464. else
  465. {
  466. /* Update the error code */
  467. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  468. /* Return error status */
  469. status = HAL_ERROR;
  470. }
  471. /* Release Lock */
  472. __HAL_UNLOCK(husart);
  473. return status;
  474. }
  475. /**
  476. * @brief Unregister an USART Callback
  477. * USART callaback is redirected to the weak predefined callback
  478. * @param husart usart handle
  479. * @param CallbackID ID of the callback to be unregistered
  480. * This parameter can be one of the following values:
  481. * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  482. * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  483. * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  484. * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  485. * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
  486. * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
  487. * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  488. * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
  489. * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
  490. * @retval HAL status
  491. */
  492. HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID)
  493. {
  494. HAL_StatusTypeDef status = HAL_OK;
  495. /* Process locked */
  496. __HAL_LOCK(husart);
  497. if (husart->State == HAL_USART_STATE_READY)
  498. {
  499. switch (CallbackID)
  500. {
  501. case HAL_USART_TX_HALFCOMPLETE_CB_ID :
  502. husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  503. break;
  504. case HAL_USART_TX_COMPLETE_CB_ID :
  505. husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */
  506. break;
  507. case HAL_USART_RX_HALFCOMPLETE_CB_ID :
  508. husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  509. break;
  510. case HAL_USART_RX_COMPLETE_CB_ID :
  511. husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */
  512. break;
  513. case HAL_USART_TX_RX_COMPLETE_CB_ID :
  514. husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
  515. break;
  516. case HAL_USART_ERROR_CB_ID :
  517. husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */
  518. break;
  519. case HAL_USART_ABORT_COMPLETE_CB_ID :
  520. husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  521. break;
  522. case HAL_USART_MSPINIT_CB_ID :
  523. husart->MspInitCallback = HAL_USART_MspInit; /* Legacy weak MspInitCallback */
  524. break;
  525. case HAL_USART_MSPDEINIT_CB_ID :
  526. husart->MspDeInitCallback = HAL_USART_MspDeInit; /* Legacy weak MspDeInitCallback */
  527. break;
  528. default :
  529. /* Update the error code */
  530. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  531. /* Return error status */
  532. status = HAL_ERROR;
  533. break;
  534. }
  535. }
  536. else if (husart->State == HAL_USART_STATE_RESET)
  537. {
  538. switch (CallbackID)
  539. {
  540. case HAL_USART_MSPINIT_CB_ID :
  541. husart->MspInitCallback = HAL_USART_MspInit;
  542. break;
  543. case HAL_USART_MSPDEINIT_CB_ID :
  544. husart->MspDeInitCallback = HAL_USART_MspDeInit;
  545. break;
  546. default :
  547. /* Update the error code */
  548. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  549. /* Return error status */
  550. status = HAL_ERROR;
  551. break;
  552. }
  553. }
  554. else
  555. {
  556. /* Update the error code */
  557. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  558. /* Return error status */
  559. status = HAL_ERROR;
  560. }
  561. /* Release Lock */
  562. __HAL_UNLOCK(husart);
  563. return status;
  564. }
  565. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  566. /**
  567. * @}
  568. */
  569. /** @defgroup USART_Exported_Functions_Group2 IO operation functions
  570. * @brief USART Transmit and Receive functions
  571. *
  572. @verbatim
  573. ==============================================================================
  574. ##### IO operation functions #####
  575. ==============================================================================
  576. [..]
  577. This subsection provides a set of functions allowing to manage the USART synchronous
  578. data transfers.
  579. [..]
  580. The USART supports master mode only: it cannot receive or send data related to an input
  581. clock (SCLK is always an output).
  582. (#) There are two modes of transfer:
  583. (++) Blocking mode: The communication is performed in polling mode.
  584. The HAL status of all data processing is returned by the same function
  585. after finishing transfer.
  586. (++) No-Blocking mode: The communication is performed using Interrupts
  587. or DMA, These API's return the HAL status.
  588. The end of the data processing will be indicated through the
  589. dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
  590. using DMA mode.
  591. The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback()
  592. user callbacks
  593. will be executed respectively at the end of the transmit or Receive process
  594. The HAL_USART_ErrorCallback() user callback will be executed when a communication
  595. error is detected
  596. (#) Blocking mode APIs are :
  597. (++) HAL_USART_Transmit() in simplex mode
  598. (++) HAL_USART_Receive() in full duplex receive only
  599. (++) HAL_USART_TransmitReceive() in full duplex mode
  600. (#) Non Blocking mode APIs with Interrupt are :
  601. (++) HAL_USART_Transmit_IT()in simplex mode
  602. (++) HAL_USART_Receive_IT() in full duplex receive only
  603. (++) HAL_USART_TransmitReceive_IT() in full duplex mode
  604. (++) HAL_USART_IRQHandler()
  605. (#) Non Blocking mode functions with DMA are :
  606. (++) HAL_USART_Transmit_DMA()in simplex mode
  607. (++) HAL_USART_Receive_DMA() in full duplex receive only
  608. (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
  609. (++) HAL_USART_DMAPause()
  610. (++) HAL_USART_DMAResume()
  611. (++) HAL_USART_DMAStop()
  612. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  613. (++) HAL_USART_TxHalfCpltCallback()
  614. (++) HAL_USART_TxCpltCallback()
  615. (++) HAL_USART_RxHalfCpltCallback()
  616. (++) HAL_USART_RxCpltCallback()
  617. (++) HAL_USART_ErrorCallback()
  618. (++) HAL_USART_TxRxCpltCallback()
  619. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  620. (++) HAL_USART_Abort()
  621. (++) HAL_USART_Abort_IT()
  622. (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided:
  623. (++) HAL_USART_AbortCpltCallback()
  624. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  625. Errors are handled as follows :
  626. (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  627. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  628. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  629. and HAL_USART_ErrorCallback() user callback is executed. Transfer is kept ongoing on USART side.
  630. If user wants to abort it, Abort services should be called by user.
  631. (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  632. This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
  633. Error code is set to allow user to identify error type, and HAL_USART_ErrorCallback() user callback is executed.
  634. @endverbatim
  635. * @{
  636. */
  637. /**
  638. * @brief Simplex Send an amount of data in blocking mode.
  639. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  640. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  641. * of u16 provided through pTxData.
  642. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  643. * the configuration information for the specified USART module.
  644. * @param pTxData Pointer to data buffer (u8 or u16 data elements).
  645. * @param Size Amount of data elements (u8 or u16) to be sent.
  646. * @param Timeout Timeout duration.
  647. * @retval HAL status
  648. */
  649. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
  650. {
  651. const uint8_t *ptxdata8bits;
  652. const uint16_t *ptxdata16bits;
  653. uint32_t tickstart;
  654. if (husart->State == HAL_USART_STATE_READY)
  655. {
  656. if ((pTxData == NULL) || (Size == 0))
  657. {
  658. return HAL_ERROR;
  659. }
  660. /* Process Locked */
  661. __HAL_LOCK(husart);
  662. husart->ErrorCode = HAL_USART_ERROR_NONE;
  663. husart->State = HAL_USART_STATE_BUSY_TX;
  664. /* Init tickstart for timeout management */
  665. tickstart = HAL_GetTick();
  666. husart->TxXferSize = Size;
  667. husart->TxXferCount = Size;
  668. /* In case of 9bits/No Parity transfer, pTxData needs to be handled as a uint16_t pointer */
  669. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  670. {
  671. ptxdata8bits = NULL;
  672. ptxdata16bits = (const uint16_t *) pTxData;
  673. }
  674. else
  675. {
  676. ptxdata8bits = pTxData;
  677. ptxdata16bits = NULL;
  678. }
  679. while (husart->TxXferCount > 0U)
  680. {
  681. /* Wait for TXE flag in order to write data in DR */
  682. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  683. {
  684. return HAL_TIMEOUT;
  685. }
  686. if (ptxdata8bits == NULL)
  687. {
  688. husart->Instance->DR = (uint16_t)(*ptxdata16bits & (uint16_t)0x01FF);
  689. ptxdata16bits++;
  690. }
  691. else
  692. {
  693. husart->Instance->DR = (uint8_t)(*ptxdata8bits & (uint8_t)0xFF);
  694. ptxdata8bits++;
  695. }
  696. husart->TxXferCount--;
  697. }
  698. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  699. {
  700. return HAL_TIMEOUT;
  701. }
  702. husart->State = HAL_USART_STATE_READY;
  703. /* Process Unlocked */
  704. __HAL_UNLOCK(husart);
  705. return HAL_OK;
  706. }
  707. else
  708. {
  709. return HAL_BUSY;
  710. }
  711. }
  712. /**
  713. * @brief Full-Duplex Receive an amount of data in blocking mode.
  714. * @note To receive synchronous data, dummy data are simultaneously transmitted.
  715. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  716. * the received data is handled as a set of u16. In this case, Size must indicate the number
  717. * of u16 available through pRxData.
  718. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  719. * the configuration information for the specified USART module.
  720. * @param pRxData Pointer to data buffer (u8 or u16 data elements).
  721. * @param Size Amount of data elements (u8 or u16) to be received.
  722. * @param Timeout Timeout duration.
  723. * @retval HAL status
  724. */
  725. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  726. {
  727. uint8_t *prxdata8bits;
  728. uint16_t *prxdata16bits;
  729. uint32_t tickstart;
  730. if (husart->State == HAL_USART_STATE_READY)
  731. {
  732. if ((pRxData == NULL) || (Size == 0))
  733. {
  734. return HAL_ERROR;
  735. }
  736. /* Process Locked */
  737. __HAL_LOCK(husart);
  738. husart->ErrorCode = HAL_USART_ERROR_NONE;
  739. husart->State = HAL_USART_STATE_BUSY_RX;
  740. /* Init tickstart for timeout management */
  741. tickstart = HAL_GetTick();
  742. husart->RxXferSize = Size;
  743. husart->RxXferCount = Size;
  744. /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  745. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  746. {
  747. prxdata8bits = NULL;
  748. prxdata16bits = (uint16_t *) pRxData;
  749. }
  750. else
  751. {
  752. prxdata8bits = pRxData;
  753. prxdata16bits = NULL;
  754. }
  755. /* Check the remain data to be received */
  756. while (husart->RxXferCount > 0U)
  757. {
  758. /* Wait until TXE flag is set to send dummy byte in order to generate the
  759. * clock for the slave to send data.
  760. * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
  761. * can be written for all the cases. */
  762. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  763. {
  764. return HAL_TIMEOUT;
  765. }
  766. husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x0FF);
  767. /* Wait until RXNE flag is set to receive the byte */
  768. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  769. {
  770. return HAL_TIMEOUT;
  771. }
  772. if (prxdata8bits == NULL)
  773. {
  774. *prxdata16bits = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  775. prxdata16bits++;
  776. }
  777. else
  778. {
  779. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) || ((husart->Init.WordLength == USART_WORDLENGTH_8B) && (husart->Init.Parity == USART_PARITY_NONE)))
  780. {
  781. *prxdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x0FF);
  782. }
  783. else
  784. {
  785. *prxdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x07F);
  786. }
  787. prxdata8bits++;
  788. }
  789. husart->RxXferCount--;
  790. }
  791. husart->State = HAL_USART_STATE_READY;
  792. /* Process Unlocked */
  793. __HAL_UNLOCK(husart);
  794. return HAL_OK;
  795. }
  796. else
  797. {
  798. return HAL_BUSY;
  799. }
  800. }
  801. /**
  802. * @brief Full-Duplex Send and Receive an amount of data in full-duplex mode (blocking mode).
  803. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  804. * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
  805. * of u16 available through pTxData and through pRxData.
  806. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  807. * the configuration information for the specified USART module.
  808. * @param pTxData Pointer to TX data buffer (u8 or u16 data elements).
  809. * @param pRxData Pointer to RX data buffer (u8 or u16 data elements).
  810. * @param Size Amount of data elements (u8 or u16) to be sent (same amount to be received).
  811. * @param Timeout Timeout duration
  812. * @retval HAL status
  813. */
  814. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  815. uint16_t Size, uint32_t Timeout)
  816. {
  817. uint8_t *prxdata8bits;
  818. uint16_t *prxdata16bits;
  819. const uint8_t *ptxdata8bits;
  820. const uint16_t *ptxdata16bits;
  821. uint16_t rxdatacount;
  822. uint32_t tickstart;
  823. if (husart->State == HAL_USART_STATE_READY)
  824. {
  825. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  826. {
  827. return HAL_ERROR;
  828. }
  829. /* In case of 9bits/No Parity transfer, pTxData and pRxData buffers provided as input parameter
  830. should be aligned on a u16 frontier, as data to be filled into TDR/retrieved from RDR will be
  831. handled through a u16 cast. */
  832. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  833. {
  834. if (((((uint32_t)pTxData) & 1U) != 0U) || ((((uint32_t)pRxData) & 1U) != 0U))
  835. {
  836. return HAL_ERROR;
  837. }
  838. }
  839. /* Process Locked */
  840. __HAL_LOCK(husart);
  841. husart->ErrorCode = HAL_USART_ERROR_NONE;
  842. husart->State = HAL_USART_STATE_BUSY_RX;
  843. /* Init tickstart for timeout management */
  844. tickstart = HAL_GetTick();
  845. husart->RxXferSize = Size;
  846. husart->TxXferSize = Size;
  847. husart->TxXferCount = Size;
  848. husart->RxXferCount = Size;
  849. /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  850. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  851. {
  852. prxdata8bits = NULL;
  853. ptxdata8bits = NULL;
  854. ptxdata16bits = (const uint16_t *) pTxData;
  855. prxdata16bits = (uint16_t *) pRxData;
  856. }
  857. else
  858. {
  859. prxdata8bits = pRxData;
  860. ptxdata8bits = pTxData;
  861. ptxdata16bits = NULL;
  862. prxdata16bits = NULL;
  863. }
  864. /* Check the remain data to be received */
  865. /* rxdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
  866. rxdatacount = husart->RxXferCount;
  867. while ((husart->TxXferCount > 0U) || (rxdatacount > 0U))
  868. {
  869. if (husart->TxXferCount > 0U)
  870. {
  871. /* Wait for TXE flag in order to write data in DR */
  872. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  873. {
  874. return HAL_TIMEOUT;
  875. }
  876. if (ptxdata8bits == NULL)
  877. {
  878. husart->Instance->DR = (uint16_t)(*ptxdata16bits & (uint16_t)0x01FF);
  879. ptxdata16bits++;
  880. }
  881. else
  882. {
  883. husart->Instance->DR = (uint8_t)(*ptxdata8bits & (uint8_t)0xFF);
  884. ptxdata8bits++;
  885. }
  886. husart->TxXferCount--;
  887. }
  888. if (husart->RxXferCount > 0U)
  889. {
  890. /* Wait for RXNE Flag */
  891. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  892. {
  893. return HAL_TIMEOUT;
  894. }
  895. if (prxdata8bits == NULL)
  896. {
  897. *prxdata16bits = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  898. prxdata16bits++;
  899. }
  900. else
  901. {
  902. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) || ((husart->Init.WordLength == USART_WORDLENGTH_8B) && (husart->Init.Parity == USART_PARITY_NONE)))
  903. {
  904. *prxdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x0FF);
  905. }
  906. else
  907. {
  908. *prxdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x07F);
  909. }
  910. prxdata8bits++;
  911. }
  912. husart->RxXferCount--;
  913. }
  914. rxdatacount = husart->RxXferCount;
  915. }
  916. husart->State = HAL_USART_STATE_READY;
  917. /* Process Unlocked */
  918. __HAL_UNLOCK(husart);
  919. return HAL_OK;
  920. }
  921. else
  922. {
  923. return HAL_BUSY;
  924. }
  925. }
  926. /**
  927. * @brief Simplex Send an amount of data in non-blocking mode.
  928. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  929. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  930. * of u16 provided through pTxData.
  931. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  932. * the configuration information for the specified USART module.
  933. * @param pTxData Pointer to data buffer (u8 or u16 data elements).
  934. * @param Size Amount of data elements (u8 or u16) to be sent.
  935. * @retval HAL status
  936. * @note The USART errors are not managed to avoid the overrun error.
  937. */
  938. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size)
  939. {
  940. if (husart->State == HAL_USART_STATE_READY)
  941. {
  942. if ((pTxData == NULL) || (Size == 0))
  943. {
  944. return HAL_ERROR;
  945. }
  946. /* Process Locked */
  947. __HAL_LOCK(husart);
  948. husart->pTxBuffPtr = pTxData;
  949. husart->TxXferSize = Size;
  950. husart->TxXferCount = Size;
  951. husart->ErrorCode = HAL_USART_ERROR_NONE;
  952. husart->State = HAL_USART_STATE_BUSY_TX;
  953. /* The USART Error Interrupts: (Frame error, Noise error, Overrun error)
  954. are not managed by the USART transmit process to avoid the overrun interrupt
  955. when the USART mode is configured for transmit and receive "USART_MODE_TX_RX"
  956. to benefit for the frame error and noise interrupts the USART mode should be
  957. configured only for transmit "USART_MODE_TX"
  958. The __HAL_USART_ENABLE_IT(husart, USART_IT_ERR) can be used to enable the Frame error,
  959. Noise error interrupt */
  960. /* Process Unlocked */
  961. __HAL_UNLOCK(husart);
  962. /* Enable the USART Transmit Data Register Empty Interrupt */
  963. SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  964. return HAL_OK;
  965. }
  966. else
  967. {
  968. return HAL_BUSY;
  969. }
  970. }
  971. /**
  972. * @brief Simplex Receive an amount of data in non-blocking mode.
  973. * @note To receive synchronous data, dummy data are simultaneously transmitted.
  974. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  975. * the received data is handled as a set of u16. In this case, Size must indicate the number
  976. * of u16 available through pRxData.
  977. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  978. * the configuration information for the specified USART module.
  979. * @param pRxData Pointer to data buffer (u8 or u16 data elements).
  980. * @param Size Amount of data elements (u8 or u16) to be received.
  981. * @retval HAL status
  982. */
  983. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  984. {
  985. if (husart->State == HAL_USART_STATE_READY)
  986. {
  987. if ((pRxData == NULL) || (Size == 0))
  988. {
  989. return HAL_ERROR;
  990. }
  991. /* Process Locked */
  992. __HAL_LOCK(husart);
  993. husart->pRxBuffPtr = pRxData;
  994. husart->RxXferSize = Size;
  995. husart->RxXferCount = Size;
  996. husart->ErrorCode = HAL_USART_ERROR_NONE;
  997. husart->State = HAL_USART_STATE_BUSY_RX;
  998. /* Process Unlocked */
  999. __HAL_UNLOCK(husart);
  1000. if (husart->Init.Parity != USART_PARITY_NONE)
  1001. {
  1002. /* Enable the USART Parity Error and Data Register not empty Interrupts */
  1003. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  1004. }
  1005. else
  1006. {
  1007. /* Enable the USART Data Register not empty Interrupts */
  1008. SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1009. }
  1010. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1011. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1012. /* Send dummy byte in order to generate the clock for the slave to send data */
  1013. husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
  1014. return HAL_OK;
  1015. }
  1016. else
  1017. {
  1018. return HAL_BUSY;
  1019. }
  1020. }
  1021. /**
  1022. * @brief Full-Duplex Send and Receive an amount of data in full-duplex mode (non-blocking).
  1023. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1024. * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
  1025. * of u16 available through pTxData and through pRxData.
  1026. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1027. * the configuration information for the specified USART module.
  1028. * @param pTxData Pointer to TX data buffer (u8 or u16 data elements).
  1029. * @param pRxData Pointer to RX data buffer (u8 or u16 data elements).
  1030. * @param Size Amount of data elements (u8 or u16) to be sent (same amount to be received).
  1031. * @retval HAL status
  1032. */
  1033. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  1034. uint16_t Size)
  1035. {
  1036. if (husart->State == HAL_USART_STATE_READY)
  1037. {
  1038. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  1039. {
  1040. return HAL_ERROR;
  1041. }
  1042. /* Process Locked */
  1043. __HAL_LOCK(husart);
  1044. husart->pRxBuffPtr = pRxData;
  1045. husart->RxXferSize = Size;
  1046. husart->RxXferCount = Size;
  1047. husart->pTxBuffPtr = pTxData;
  1048. husart->TxXferSize = Size;
  1049. husart->TxXferCount = Size;
  1050. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1051. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  1052. /* Process Unlocked */
  1053. __HAL_UNLOCK(husart);
  1054. /* Enable the USART Data Register not empty Interrupt */
  1055. SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1056. if (husart->Init.Parity != USART_PARITY_NONE)
  1057. {
  1058. /* Enable the USART Parity Error Interrupt */
  1059. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1060. }
  1061. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1062. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1063. /* Enable the USART Transmit Data Register Empty Interrupt */
  1064. SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  1065. return HAL_OK;
  1066. }
  1067. else
  1068. {
  1069. return HAL_BUSY;
  1070. }
  1071. }
  1072. /**
  1073. * @brief Simplex Send an amount of data in DMA mode.
  1074. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1075. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  1076. * of u16 provided through pTxData.
  1077. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1078. * the configuration information for the specified USART module.
  1079. * @param pTxData Pointer to data buffer (u8 or u16 data elements).
  1080. * @param Size Amount of data elements (u8 or u16) to be sent.
  1081. * @retval HAL status
  1082. */
  1083. HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size)
  1084. {
  1085. const uint32_t *tmp;
  1086. if (husart->State == HAL_USART_STATE_READY)
  1087. {
  1088. if ((pTxData == NULL) || (Size == 0))
  1089. {
  1090. return HAL_ERROR;
  1091. }
  1092. /* Process Locked */
  1093. __HAL_LOCK(husart);
  1094. husart->pTxBuffPtr = pTxData;
  1095. husart->TxXferSize = Size;
  1096. husart->TxXferCount = Size;
  1097. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1098. husart->State = HAL_USART_STATE_BUSY_TX;
  1099. /* Set the USART DMA transfer complete callback */
  1100. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  1101. /* Set the USART DMA Half transfer complete callback */
  1102. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  1103. /* Set the DMA error callback */
  1104. husart->hdmatx->XferErrorCallback = USART_DMAError;
  1105. /* Set the DMA abort callback */
  1106. husart->hdmatx->XferAbortCallback = NULL;
  1107. /* Enable the USART transmit DMA stream */
  1108. tmp = (const uint32_t *)&pTxData;
  1109. HAL_DMA_Start_IT(husart->hdmatx, *(const uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size);
  1110. /* Clear the TC flag in the SR register by writing 0 to it */
  1111. __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
  1112. /* Process Unlocked */
  1113. __HAL_UNLOCK(husart);
  1114. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1115. in the USART CR3 register */
  1116. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1117. return HAL_OK;
  1118. }
  1119. else
  1120. {
  1121. return HAL_BUSY;
  1122. }
  1123. }
  1124. /**
  1125. * @brief Full-Duplex Receive an amount of data in DMA mode.
  1126. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1127. * the received data is handled as a set of u16. In this case, Size must indicate the number
  1128. * of u16 available through pRxData.
  1129. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1130. * the configuration information for the specified USART module.
  1131. * @param pRxData Pointer to data buffer (u8 or u16 data elements).
  1132. * @param Size Amount of data elements (u8 or u16) to be received.
  1133. * @retval HAL status
  1134. * @note The USART DMA transmit stream must be configured in order to generate the clock for the slave.
  1135. * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
  1136. */
  1137. HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  1138. {
  1139. uint32_t *tmp;
  1140. if (husart->State == HAL_USART_STATE_READY)
  1141. {
  1142. if ((pRxData == NULL) || (Size == 0))
  1143. {
  1144. return HAL_ERROR;
  1145. }
  1146. /* Process Locked */
  1147. __HAL_LOCK(husart);
  1148. husart->pRxBuffPtr = pRxData;
  1149. husart->RxXferSize = Size;
  1150. husart->pTxBuffPtr = pRxData;
  1151. husart->TxXferSize = Size;
  1152. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1153. husart->State = HAL_USART_STATE_BUSY_RX;
  1154. /* Set the USART DMA Rx transfer complete callback */
  1155. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  1156. /* Set the USART DMA Half transfer complete callback */
  1157. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  1158. /* Set the USART DMA Rx transfer error callback */
  1159. husart->hdmarx->XferErrorCallback = USART_DMAError;
  1160. /* Set the DMA abort callback */
  1161. husart->hdmarx->XferAbortCallback = NULL;
  1162. /* Set the USART Tx DMA transfer complete callback as NULL because the communication closing
  1163. is performed in DMA reception complete callback */
  1164. husart->hdmatx->XferHalfCpltCallback = NULL;
  1165. husart->hdmatx->XferCpltCallback = NULL;
  1166. /* Set the DMA error callback */
  1167. husart->hdmatx->XferErrorCallback = USART_DMAError;
  1168. /* Set the DMA AbortCpltCallback */
  1169. husart->hdmatx->XferAbortCallback = NULL;
  1170. /* Enable the USART receive DMA stream */
  1171. tmp = (uint32_t *)&pRxData;
  1172. HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t *)tmp, Size);
  1173. /* Enable the USART transmit DMA stream: the transmit stream is used in order
  1174. to generate in the non-blocking mode the clock to the slave device,
  1175. this mode isn't a simplex receive mode but a full-duplex receive one */
  1176. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size);
  1177. /* Clear the Overrun flag just before enabling the DMA Rx request: mandatory for the second transfer */
  1178. __HAL_USART_CLEAR_OREFLAG(husart);
  1179. /* Process Unlocked */
  1180. __HAL_UNLOCK(husart);
  1181. if (husart->Init.Parity != USART_PARITY_NONE)
  1182. {
  1183. /* Enable the USART Parity Error Interrupt */
  1184. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1185. }
  1186. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1187. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1188. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1189. in the USART CR3 register */
  1190. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1191. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1192. in the USART CR3 register */
  1193. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1194. return HAL_OK;
  1195. }
  1196. else
  1197. {
  1198. return HAL_BUSY;
  1199. }
  1200. }
  1201. /**
  1202. * @brief Full-Duplex Transmit Receive an amount of data in DMA mode.
  1203. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1204. * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
  1205. * of u16 available through pTxData and through pRxData.
  1206. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1207. * the configuration information for the specified USART module.
  1208. * @param pTxData Pointer to TX data buffer (u8 or u16 data elements).
  1209. * @param pRxData Pointer to RX data buffer (u8 or u16 data elements).
  1210. * @param Size Amount of data elements (u8 or u16) to be received/sent.
  1211. * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
  1212. * @retval HAL status
  1213. */
  1214. HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  1215. uint16_t Size)
  1216. {
  1217. const uint32_t *tmp;
  1218. if (husart->State == HAL_USART_STATE_READY)
  1219. {
  1220. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  1221. {
  1222. return HAL_ERROR;
  1223. }
  1224. /* Process Locked */
  1225. __HAL_LOCK(husart);
  1226. husart->pRxBuffPtr = pRxData;
  1227. husart->RxXferSize = Size;
  1228. husart->pTxBuffPtr = pTxData;
  1229. husart->TxXferSize = Size;
  1230. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1231. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  1232. /* Set the USART DMA Rx transfer complete callback */
  1233. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  1234. /* Set the USART DMA Half transfer complete callback */
  1235. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  1236. /* Set the USART DMA Tx transfer complete callback */
  1237. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  1238. /* Set the USART DMA Half transfer complete callback */
  1239. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  1240. /* Set the USART DMA Tx transfer error callback */
  1241. husart->hdmatx->XferErrorCallback = USART_DMAError;
  1242. /* Set the USART DMA Rx transfer error callback */
  1243. husart->hdmarx->XferErrorCallback = USART_DMAError;
  1244. /* Set the DMA abort callback */
  1245. husart->hdmarx->XferAbortCallback = NULL;
  1246. /* Enable the USART receive DMA stream */
  1247. tmp = (uint32_t *)&pRxData;
  1248. HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(const uint32_t *)tmp, Size);
  1249. /* Enable the USART transmit DMA stream */
  1250. tmp = (const uint32_t *)&pTxData;
  1251. HAL_DMA_Start_IT(husart->hdmatx, *(const uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size);
  1252. /* Clear the TC flag in the SR register by writing 0 to it */
  1253. __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
  1254. /* Clear the Overrun flag: mandatory for the second transfer in circular mode */
  1255. __HAL_USART_CLEAR_OREFLAG(husart);
  1256. /* Process Unlocked */
  1257. __HAL_UNLOCK(husart);
  1258. if (husart->Init.Parity != USART_PARITY_NONE)
  1259. {
  1260. /* Enable the USART Parity Error Interrupt */
  1261. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1262. }
  1263. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1264. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1265. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1266. in the USART CR3 register */
  1267. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1268. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1269. in the USART CR3 register */
  1270. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1271. return HAL_OK;
  1272. }
  1273. else
  1274. {
  1275. return HAL_BUSY;
  1276. }
  1277. }
  1278. /**
  1279. * @brief Pauses the DMA Transfer.
  1280. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1281. * the configuration information for the specified USART module.
  1282. * @retval HAL status
  1283. */
  1284. HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
  1285. {
  1286. /* Process Locked */
  1287. __HAL_LOCK(husart);
  1288. /* Disable the USART DMA Tx request */
  1289. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1290. /* Process Unlocked */
  1291. __HAL_UNLOCK(husart);
  1292. return HAL_OK;
  1293. }
  1294. /**
  1295. * @brief Resumes the DMA Transfer.
  1296. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1297. * the configuration information for the specified USART module.
  1298. * @retval HAL status
  1299. */
  1300. HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
  1301. {
  1302. /* Process Locked */
  1303. __HAL_LOCK(husart);
  1304. /* Enable the USART DMA Tx request */
  1305. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1306. /* Process Unlocked */
  1307. __HAL_UNLOCK(husart);
  1308. return HAL_OK;
  1309. }
  1310. /**
  1311. * @brief Stops the DMA Transfer.
  1312. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1313. * the configuration information for the specified USART module.
  1314. * @retval HAL status
  1315. */
  1316. HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
  1317. {
  1318. uint32_t dmarequest = 0x00U;
  1319. /* The Lock is not implemented on this API to allow the user application
  1320. to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback():
  1321. when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  1322. and the correspond call back is executed HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback()
  1323. */
  1324. /* Stop USART DMA Tx request if ongoing */
  1325. dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT);
  1326. if ((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest)
  1327. {
  1328. USART_EndTxTransfer(husart);
  1329. /* Abort the USART DMA Tx channel */
  1330. if (husart->hdmatx != NULL)
  1331. {
  1332. HAL_DMA_Abort(husart->hdmatx);
  1333. }
  1334. /* Disable the USART Tx DMA request */
  1335. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1336. }
  1337. /* Stop USART DMA Rx request if ongoing */
  1338. dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
  1339. if ((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest)
  1340. {
  1341. USART_EndRxTransfer(husart);
  1342. /* Abort the USART DMA Rx channel */
  1343. if (husart->hdmarx != NULL)
  1344. {
  1345. HAL_DMA_Abort(husart->hdmarx);
  1346. }
  1347. /* Disable the USART Rx DMA request */
  1348. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1349. }
  1350. return HAL_OK;
  1351. }
  1352. /**
  1353. * @brief Abort ongoing transfer (blocking mode).
  1354. * @param husart USART handle.
  1355. * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
  1356. * as described by TransferType parameter) started in Interrupt or DMA mode.
  1357. * This procedure performs following operations :
  1358. * - Disable PPP Interrupts (depending of transfer direction)
  1359. * - Disable the DMA transfer in the peripheral register (if enabled)
  1360. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1361. * - Set handle State to READY
  1362. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1363. * @retval HAL status
  1364. */
  1365. HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
  1366. {
  1367. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1368. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1369. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1370. /* Disable the USART DMA Tx request if enabled */
  1371. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1372. {
  1373. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1374. /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1375. if (husart->hdmatx != NULL)
  1376. {
  1377. /* Set the USART DMA Abort callback to Null.
  1378. No call back execution at end of DMA abort procedure */
  1379. husart->hdmatx->XferAbortCallback = NULL;
  1380. HAL_DMA_Abort(husart->hdmatx);
  1381. }
  1382. }
  1383. /* Disable the USART DMA Rx request if enabled */
  1384. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1385. {
  1386. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1387. /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1388. if (husart->hdmarx != NULL)
  1389. {
  1390. /* Set the USART DMA Abort callback to Null.
  1391. No call back execution at end of DMA abort procedure */
  1392. husart->hdmarx->XferAbortCallback = NULL;
  1393. HAL_DMA_Abort(husart->hdmarx);
  1394. }
  1395. }
  1396. /* Reset Tx and Rx transfer counters */
  1397. husart->TxXferCount = 0x00U;
  1398. husart->RxXferCount = 0x00U;
  1399. /* Restore husart->State to Ready */
  1400. husart->State = HAL_USART_STATE_READY;
  1401. /* Reset Handle ErrorCode to No Error */
  1402. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1403. return HAL_OK;
  1404. }
  1405. /**
  1406. * @brief Abort ongoing transfer (Interrupt mode).
  1407. * @param husart USART handle.
  1408. * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
  1409. * as described by TransferType parameter) started in Interrupt or DMA mode.
  1410. * This procedure performs following operations :
  1411. * - Disable PPP Interrupts (depending of transfer direction)
  1412. * - Disable the DMA transfer in the peripheral register (if enabled)
  1413. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1414. * - Set handle State to READY
  1415. * - At abort completion, call user abort complete callback
  1416. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1417. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1418. * @retval HAL status
  1419. */
  1420. HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
  1421. {
  1422. uint32_t AbortCplt = 0x01U;
  1423. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1424. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1425. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1426. /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
  1427. before any call to DMA Abort functions */
  1428. /* DMA Tx Handle is valid */
  1429. if (husart->hdmatx != NULL)
  1430. {
  1431. /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
  1432. Otherwise, set it to NULL */
  1433. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1434. {
  1435. husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback;
  1436. }
  1437. else
  1438. {
  1439. husart->hdmatx->XferAbortCallback = NULL;
  1440. }
  1441. }
  1442. /* DMA Rx Handle is valid */
  1443. if (husart->hdmarx != NULL)
  1444. {
  1445. /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
  1446. Otherwise, set it to NULL */
  1447. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1448. {
  1449. husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback;
  1450. }
  1451. else
  1452. {
  1453. husart->hdmarx->XferAbortCallback = NULL;
  1454. }
  1455. }
  1456. /* Disable the USART DMA Tx request if enabled */
  1457. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1458. {
  1459. /* Disable DMA Tx at USART level */
  1460. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1461. /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1462. if (husart->hdmatx != NULL)
  1463. {
  1464. /* USART Tx DMA Abort callback has already been initialised :
  1465. will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
  1466. /* Abort DMA TX */
  1467. if (HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK)
  1468. {
  1469. husart->hdmatx->XferAbortCallback = NULL;
  1470. }
  1471. else
  1472. {
  1473. AbortCplt = 0x00U;
  1474. }
  1475. }
  1476. }
  1477. /* Disable the USART DMA Rx request if enabled */
  1478. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1479. {
  1480. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1481. /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1482. if (husart->hdmarx != NULL)
  1483. {
  1484. /* USART Rx DMA Abort callback has already been initialised :
  1485. will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
  1486. /* Abort DMA RX */
  1487. if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
  1488. {
  1489. husart->hdmarx->XferAbortCallback = NULL;
  1490. AbortCplt = 0x01U;
  1491. }
  1492. else
  1493. {
  1494. AbortCplt = 0x00U;
  1495. }
  1496. }
  1497. }
  1498. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1499. if (AbortCplt == 0x01U)
  1500. {
  1501. /* Reset Tx and Rx transfer counters */
  1502. husart->TxXferCount = 0x00U;
  1503. husart->RxXferCount = 0x00U;
  1504. /* Reset errorCode */
  1505. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1506. /* Restore husart->State to Ready */
  1507. husart->State = HAL_USART_STATE_READY;
  1508. /* As no DMA to be aborted, call directly user Abort complete callback */
  1509. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1510. /* Call registered Abort Complete Callback */
  1511. husart->AbortCpltCallback(husart);
  1512. #else
  1513. /* Call legacy weak Abort Complete Callback */
  1514. HAL_USART_AbortCpltCallback(husart);
  1515. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1516. }
  1517. return HAL_OK;
  1518. }
  1519. /**
  1520. * @brief This function handles USART interrupt request.
  1521. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1522. * the configuration information for the specified USART module.
  1523. * @retval None
  1524. */
  1525. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
  1526. {
  1527. uint32_t isrflags = READ_REG(husart->Instance->SR);
  1528. uint32_t cr1its = READ_REG(husart->Instance->CR1);
  1529. uint32_t cr3its = READ_REG(husart->Instance->CR3);
  1530. uint32_t errorflags = 0x00U;
  1531. uint32_t dmarequest = 0x00U;
  1532. /* If no error occurs */
  1533. errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
  1534. if (errorflags == RESET)
  1535. {
  1536. /* USART in mode Receiver -------------------------------------------------*/
  1537. if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1538. {
  1539. if (husart->State == HAL_USART_STATE_BUSY_RX)
  1540. {
  1541. USART_Receive_IT(husart);
  1542. }
  1543. else
  1544. {
  1545. USART_TransmitReceive_IT(husart);
  1546. }
  1547. return;
  1548. }
  1549. }
  1550. /* If some errors occur */
  1551. if ((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
  1552. {
  1553. /* USART parity error interrupt occurred ----------------------------------*/
  1554. if (((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1555. {
  1556. husart->ErrorCode |= HAL_USART_ERROR_PE;
  1557. }
  1558. /* USART noise error interrupt occurred --------------------------------*/
  1559. if (((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1560. {
  1561. husart->ErrorCode |= HAL_USART_ERROR_NE;
  1562. }
  1563. /* USART frame error interrupt occurred --------------------------------*/
  1564. if (((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1565. {
  1566. husart->ErrorCode |= HAL_USART_ERROR_FE;
  1567. }
  1568. /* USART Over-Run interrupt occurred -----------------------------------*/
  1569. if (((isrflags & USART_SR_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
  1570. {
  1571. husart->ErrorCode |= HAL_USART_ERROR_ORE;
  1572. }
  1573. if (husart->ErrorCode != HAL_USART_ERROR_NONE)
  1574. {
  1575. /* USART in mode Receiver -----------------------------------------------*/
  1576. if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1577. {
  1578. if (husart->State == HAL_USART_STATE_BUSY_RX)
  1579. {
  1580. USART_Receive_IT(husart);
  1581. }
  1582. else
  1583. {
  1584. USART_TransmitReceive_IT(husart);
  1585. }
  1586. }
  1587. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1588. consider error as blocking */
  1589. dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
  1590. if (((husart->ErrorCode & HAL_USART_ERROR_ORE) != RESET) || dmarequest)
  1591. {
  1592. /* Set the USART state ready to be able to start again the process,
  1593. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1594. USART_EndRxTransfer(husart);
  1595. /* Disable the USART DMA Rx request if enabled */
  1596. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1597. {
  1598. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1599. /* Abort the USART DMA Rx channel */
  1600. if (husart->hdmarx != NULL)
  1601. {
  1602. /* Set the USART DMA Abort callback :
  1603. will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
  1604. husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
  1605. if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
  1606. {
  1607. /* Call Directly XferAbortCallback function in case of error */
  1608. husart->hdmarx->XferAbortCallback(husart->hdmarx);
  1609. }
  1610. }
  1611. else
  1612. {
  1613. /* Call user error callback */
  1614. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1615. /* Call registered Error Callback */
  1616. husart->ErrorCallback(husart);
  1617. #else
  1618. /* Call legacy weak Error Callback */
  1619. HAL_USART_ErrorCallback(husart);
  1620. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1621. }
  1622. }
  1623. else
  1624. {
  1625. /* Call user error callback */
  1626. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1627. /* Call registered Error Callback */
  1628. husart->ErrorCallback(husart);
  1629. #else
  1630. /* Call legacy weak Error Callback */
  1631. HAL_USART_ErrorCallback(husart);
  1632. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1633. }
  1634. }
  1635. else
  1636. {
  1637. /* Call user error callback */
  1638. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1639. /* Call registered Error Callback */
  1640. husart->ErrorCallback(husart);
  1641. #else
  1642. /* Call legacy weak Error Callback */
  1643. HAL_USART_ErrorCallback(husart);
  1644. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1645. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1646. }
  1647. }
  1648. return;
  1649. }
  1650. /* USART in mode Transmitter -----------------------------------------------*/
  1651. if (((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1652. {
  1653. if (husart->State == HAL_USART_STATE_BUSY_TX)
  1654. {
  1655. USART_Transmit_IT(husart);
  1656. }
  1657. else
  1658. {
  1659. USART_TransmitReceive_IT(husart);
  1660. }
  1661. return;
  1662. }
  1663. /* USART in mode Transmitter (transmission end) ----------------------------*/
  1664. if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1665. {
  1666. USART_EndTransmit_IT(husart);
  1667. return;
  1668. }
  1669. }
  1670. /**
  1671. * @brief Tx Transfer completed callbacks.
  1672. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1673. * the configuration information for the specified USART module.
  1674. * @retval None
  1675. */
  1676. __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
  1677. {
  1678. /* Prevent unused argument(s) compilation warning */
  1679. UNUSED(husart);
  1680. /* NOTE: This function should not be modified, when the callback is needed,
  1681. the HAL_USART_TxCpltCallback could be implemented in the user file
  1682. */
  1683. }
  1684. /**
  1685. * @brief Tx Half Transfer completed callbacks.
  1686. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1687. * the configuration information for the specified USART module.
  1688. * @retval None
  1689. */
  1690. __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
  1691. {
  1692. /* Prevent unused argument(s) compilation warning */
  1693. UNUSED(husart);
  1694. /* NOTE: This function should not be modified, when the callback is needed,
  1695. the HAL_USART_TxHalfCpltCallback could be implemented in the user file
  1696. */
  1697. }
  1698. /**
  1699. * @brief Rx Transfer completed callbacks.
  1700. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1701. * the configuration information for the specified USART module.
  1702. * @retval None
  1703. */
  1704. __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
  1705. {
  1706. /* Prevent unused argument(s) compilation warning */
  1707. UNUSED(husart);
  1708. /* NOTE: This function should not be modified, when the callback is needed,
  1709. the HAL_USART_RxCpltCallback could be implemented in the user file
  1710. */
  1711. }
  1712. /**
  1713. * @brief Rx Half Transfer completed callbacks.
  1714. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1715. * the configuration information for the specified USART module.
  1716. * @retval None
  1717. */
  1718. __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
  1719. {
  1720. /* Prevent unused argument(s) compilation warning */
  1721. UNUSED(husart);
  1722. /* NOTE: This function should not be modified, when the callback is needed,
  1723. the HAL_USART_RxHalfCpltCallback could be implemented in the user file
  1724. */
  1725. }
  1726. /**
  1727. * @brief Tx/Rx Transfers completed callback for the non-blocking process.
  1728. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1729. * the configuration information for the specified USART module.
  1730. * @retval None
  1731. */
  1732. __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
  1733. {
  1734. /* Prevent unused argument(s) compilation warning */
  1735. UNUSED(husart);
  1736. /* NOTE: This function should not be modified, when the callback is needed,
  1737. the HAL_USART_TxRxCpltCallback could be implemented in the user file
  1738. */
  1739. }
  1740. /**
  1741. * @brief USART error callbacks.
  1742. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1743. * the configuration information for the specified USART module.
  1744. * @retval None
  1745. */
  1746. __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
  1747. {
  1748. /* Prevent unused argument(s) compilation warning */
  1749. UNUSED(husart);
  1750. /* NOTE: This function should not be modified, when the callback is needed,
  1751. the HAL_USART_ErrorCallback could be implemented in the user file
  1752. */
  1753. }
  1754. /**
  1755. * @brief USART Abort Complete callback.
  1756. * @param husart USART handle.
  1757. * @retval None
  1758. */
  1759. __weak void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart)
  1760. {
  1761. /* Prevent unused argument(s) compilation warning */
  1762. UNUSED(husart);
  1763. /* NOTE : This function should not be modified, when the callback is needed,
  1764. the HAL_USART_AbortCpltCallback can be implemented in the user file.
  1765. */
  1766. }
  1767. /**
  1768. * @}
  1769. */
  1770. /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions
  1771. * @brief USART State and Errors functions
  1772. *
  1773. @verbatim
  1774. ==============================================================================
  1775. ##### Peripheral State and Errors functions #####
  1776. ==============================================================================
  1777. [..]
  1778. This subsection provides a set of functions allowing to return the State of
  1779. USART communication
  1780. process, return Peripheral Errors occurred during communication process
  1781. (+) HAL_USART_GetState() API can be helpful to check in run-time the state
  1782. of the USART peripheral.
  1783. (+) HAL_USART_GetError() check in run-time errors that could be occurred during
  1784. communication.
  1785. @endverbatim
  1786. * @{
  1787. */
  1788. /**
  1789. * @brief Returns the USART state.
  1790. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1791. * the configuration information for the specified USART module.
  1792. * @retval HAL state
  1793. */
  1794. HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
  1795. {
  1796. return husart->State;
  1797. }
  1798. /**
  1799. * @brief Return the USART error code
  1800. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1801. * the configuration information for the specified USART.
  1802. * @retval USART Error Code
  1803. */
  1804. uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
  1805. {
  1806. return husart->ErrorCode;
  1807. }
  1808. /**
  1809. * @}
  1810. */
  1811. /** @defgroup USART_Private_Functions USART Private Functions
  1812. * @{
  1813. */
  1814. /**
  1815. * @brief Initialize the callbacks to their default values.
  1816. * @param husart USART handle.
  1817. * @retval none
  1818. */
  1819. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1820. void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart)
  1821. {
  1822. /* Init the USART Callback settings */
  1823. husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  1824. husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */
  1825. husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  1826. husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */
  1827. husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
  1828. husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */
  1829. husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  1830. }
  1831. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1832. /**
  1833. * @brief DMA USART transmit process complete callback.
  1834. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1835. * the configuration information for the specified DMA module.
  1836. * @retval None
  1837. */
  1838. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1839. {
  1840. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1841. /* DMA Normal mode */
  1842. if ((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
  1843. {
  1844. husart->TxXferCount = 0U;
  1845. if (husart->State == HAL_USART_STATE_BUSY_TX)
  1846. {
  1847. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  1848. in the USART CR3 register */
  1849. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1850. /* Enable the USART Transmit Complete Interrupt */
  1851. SET_BIT(husart->Instance->CR1, USART_CR1_TCIE);
  1852. }
  1853. }
  1854. /* DMA Circular mode */
  1855. else
  1856. {
  1857. if (husart->State == HAL_USART_STATE_BUSY_TX)
  1858. {
  1859. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1860. /* Call registered Tx Complete Callback */
  1861. husart->TxCpltCallback(husart);
  1862. #else
  1863. /* Call legacy weak Tx Complete Callback */
  1864. HAL_USART_TxCpltCallback(husart);
  1865. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1866. }
  1867. }
  1868. }
  1869. /**
  1870. * @brief DMA USART transmit process half complete callback
  1871. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1872. * the configuration information for the specified DMA module.
  1873. * @retval None
  1874. */
  1875. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1876. {
  1877. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1878. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1879. /* Call registered Tx Half Complete Callback */
  1880. husart->TxHalfCpltCallback(husart);
  1881. #else
  1882. /* Call legacy weak Tx Half Complete Callback */
  1883. HAL_USART_TxHalfCpltCallback(husart);
  1884. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1885. }
  1886. /**
  1887. * @brief DMA USART receive process complete callback.
  1888. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1889. * the configuration information for the specified DMA module.
  1890. * @retval None
  1891. */
  1892. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1893. {
  1894. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1895. /* DMA Normal mode */
  1896. if ((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
  1897. {
  1898. husart->RxXferCount = 0x00U;
  1899. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1900. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1901. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1902. /* Disable the DMA transfer for the Transmit/receiver request by clearing the DMAT/DMAR bit
  1903. in the USART CR3 register */
  1904. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1905. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1906. /* The USART state is HAL_USART_STATE_BUSY_RX */
  1907. if (husart->State == HAL_USART_STATE_BUSY_RX)
  1908. {
  1909. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1910. /* Call registered Rx Complete Callback */
  1911. husart->RxCpltCallback(husart);
  1912. #else
  1913. /* Call legacy weak Rx Complete Callback */
  1914. HAL_USART_RxCpltCallback(husart);
  1915. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1916. }
  1917. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  1918. else
  1919. {
  1920. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1921. /* Call registered Tx Rx Complete Callback */
  1922. husart->TxRxCpltCallback(husart);
  1923. #else
  1924. /* Call legacy weak Tx Rx Complete Callback */
  1925. HAL_USART_TxRxCpltCallback(husart);
  1926. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1927. }
  1928. husart->State = HAL_USART_STATE_READY;
  1929. }
  1930. /* DMA circular mode */
  1931. else
  1932. {
  1933. if (husart->State == HAL_USART_STATE_BUSY_RX)
  1934. {
  1935. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1936. /* Call registered Rx Complete Callback */
  1937. husart->RxCpltCallback(husart);
  1938. #else
  1939. /* Call legacy weak Rx Complete Callback */
  1940. HAL_USART_RxCpltCallback(husart);
  1941. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1942. }
  1943. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  1944. else
  1945. {
  1946. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1947. /* Call registered Tx Rx Complete Callback */
  1948. husart->TxRxCpltCallback(husart);
  1949. #else
  1950. /* Call legacy weak Tx Rx Complete Callback */
  1951. HAL_USART_TxRxCpltCallback(husart);
  1952. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1953. }
  1954. }
  1955. }
  1956. /**
  1957. * @brief DMA USART receive process half complete callback
  1958. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1959. * the configuration information for the specified DMA module.
  1960. * @retval None
  1961. */
  1962. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1963. {
  1964. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1965. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1966. /* Call registered Rx Half Complete Callback */
  1967. husart->RxHalfCpltCallback(husart);
  1968. #else
  1969. /* Call legacy weak Rx Half Complete Callback */
  1970. HAL_USART_RxHalfCpltCallback(husart);
  1971. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1972. }
  1973. /**
  1974. * @brief DMA USART communication error callback.
  1975. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1976. * the configuration information for the specified DMA module.
  1977. * @retval None
  1978. */
  1979. static void USART_DMAError(DMA_HandleTypeDef *hdma)
  1980. {
  1981. uint32_t dmarequest = 0x00U;
  1982. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  1983. husart->RxXferCount = 0x00U;
  1984. husart->TxXferCount = 0x00U;
  1985. /* Stop USART DMA Tx request if ongoing */
  1986. dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT);
  1987. if ((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest)
  1988. {
  1989. USART_EndTxTransfer(husart);
  1990. }
  1991. /* Stop USART DMA Rx request if ongoing */
  1992. dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
  1993. if ((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest)
  1994. {
  1995. USART_EndRxTransfer(husart);
  1996. }
  1997. husart->ErrorCode |= HAL_USART_ERROR_DMA;
  1998. husart->State = HAL_USART_STATE_READY;
  1999. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2000. /* Call registered Error Callback */
  2001. husart->ErrorCallback(husart);
  2002. #else
  2003. /* Call legacy weak Error Callback */
  2004. HAL_USART_ErrorCallback(husart);
  2005. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2006. }
  2007. /**
  2008. * @brief This function handles USART Communication Timeout. It waits
  2009. * until a flag is no longer in the specified status.
  2010. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  2011. * the configuration information for the specified USART module.
  2012. * @param Flag specifies the USART flag to check.
  2013. * @param Status The actual Flag status (SET or RESET).
  2014. * @param Tickstart Tick start value.
  2015. * @param Timeout Timeout duration.
  2016. * @retval HAL status
  2017. */
  2018. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status,
  2019. uint32_t Tickstart, uint32_t Timeout)
  2020. {
  2021. /* Wait until flag is set */
  2022. while ((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
  2023. {
  2024. /* Check for the Timeout */
  2025. if (Timeout != HAL_MAX_DELAY)
  2026. {
  2027. if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
  2028. {
  2029. /* Disable the USART Transmit Complete Interrupt */
  2030. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  2031. /* Disable the USART RXNE Interrupt */
  2032. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  2033. /* Disable the USART Parity Error Interrupt */
  2034. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  2035. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  2036. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2037. husart->State = HAL_USART_STATE_READY;
  2038. /* Process Unlocked */
  2039. __HAL_UNLOCK(husart);
  2040. return HAL_TIMEOUT;
  2041. }
  2042. }
  2043. }
  2044. return HAL_OK;
  2045. }
  2046. /**
  2047. * @brief End ongoing Tx transfer on USART peripheral (following error detection or Transmit completion).
  2048. * @param husart USART handle.
  2049. * @retval None
  2050. */
  2051. static void USART_EndTxTransfer(USART_HandleTypeDef *husart)
  2052. {
  2053. /* Disable TXEIE and TCIE interrupts */
  2054. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  2055. /* At end of Tx process, restore husart->State to Ready */
  2056. husart->State = HAL_USART_STATE_READY;
  2057. }
  2058. /**
  2059. * @brief End ongoing Rx transfer on USART peripheral (following error detection or Reception completion).
  2060. * @param husart USART handle.
  2061. * @retval None
  2062. */
  2063. static void USART_EndRxTransfer(USART_HandleTypeDef *husart)
  2064. {
  2065. /* Disable RXNE, PE and ERR interrupts */
  2066. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  2067. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2068. /* At end of Rx process, restore husart->State to Ready */
  2069. husart->State = HAL_USART_STATE_READY;
  2070. }
  2071. /**
  2072. * @brief DMA USART communication abort callback, when initiated by HAL services on Error
  2073. * (To be called at end of DMA Abort procedure following error occurrence).
  2074. * @param hdma DMA handle.
  2075. * @retval None
  2076. */
  2077. static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2078. {
  2079. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2080. husart->RxXferCount = 0x00U;
  2081. husart->TxXferCount = 0x00U;
  2082. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2083. /* Call registered Error Callback */
  2084. husart->ErrorCallback(husart);
  2085. #else
  2086. /* Call legacy weak Error Callback */
  2087. HAL_USART_ErrorCallback(husart);
  2088. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2089. }
  2090. /**
  2091. * @brief DMA USART Tx communication abort callback, when initiated by user
  2092. * (To be called at end of DMA Tx Abort procedure following user abort request).
  2093. * @note When this callback is executed, User Abort complete call back is called only if no
  2094. * Abort still ongoing for Rx DMA Handle.
  2095. * @param hdma DMA handle.
  2096. * @retval None
  2097. */
  2098. static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  2099. {
  2100. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2101. husart->hdmatx->XferAbortCallback = NULL;
  2102. /* Check if an Abort process is still ongoing */
  2103. if (husart->hdmarx != NULL)
  2104. {
  2105. if (husart->hdmarx->XferAbortCallback != NULL)
  2106. {
  2107. return;
  2108. }
  2109. }
  2110. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2111. husart->TxXferCount = 0x00U;
  2112. husart->RxXferCount = 0x00U;
  2113. /* Reset errorCode */
  2114. husart->ErrorCode = HAL_USART_ERROR_NONE;
  2115. /* Restore husart->State to Ready */
  2116. husart->State = HAL_USART_STATE_READY;
  2117. /* Call user Abort complete callback */
  2118. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2119. /* Call registered Abort Complete Callback */
  2120. husart->AbortCpltCallback(husart);
  2121. #else
  2122. /* Call legacy weak Abort Complete Callback */
  2123. HAL_USART_AbortCpltCallback(husart);
  2124. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2125. }
  2126. /**
  2127. * @brief DMA USART Rx communication abort callback, when initiated by user
  2128. * (To be called at end of DMA Rx Abort procedure following user abort request).
  2129. * @note When this callback is executed, User Abort complete call back is called only if no
  2130. * Abort still ongoing for Tx DMA Handle.
  2131. * @param hdma DMA handle.
  2132. * @retval None
  2133. */
  2134. static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  2135. {
  2136. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2137. husart->hdmarx->XferAbortCallback = NULL;
  2138. /* Check if an Abort process is still ongoing */
  2139. if (husart->hdmatx != NULL)
  2140. {
  2141. if (husart->hdmatx->XferAbortCallback != NULL)
  2142. {
  2143. return;
  2144. }
  2145. }
  2146. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2147. husart->TxXferCount = 0x00U;
  2148. husart->RxXferCount = 0x00U;
  2149. /* Reset errorCode */
  2150. husart->ErrorCode = HAL_USART_ERROR_NONE;
  2151. /* Restore husart->State to Ready */
  2152. husart->State = HAL_USART_STATE_READY;
  2153. /* Call user Abort complete callback */
  2154. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2155. /* Call registered Abort Complete Callback */
  2156. husart->AbortCpltCallback(husart);
  2157. #else
  2158. /* Call legacy weak Abort Complete Callback */
  2159. HAL_USART_AbortCpltCallback(husart);
  2160. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2161. }
  2162. /**
  2163. * @brief Simplex Send an amount of data in non-blocking mode.
  2164. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  2165. * the configuration information for the specified USART module.
  2166. * @retval HAL status
  2167. * @note The USART errors are not managed to avoid the overrun error.
  2168. */
  2169. static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
  2170. {
  2171. const uint16_t *tmp;
  2172. if (husart->State == HAL_USART_STATE_BUSY_TX)
  2173. {
  2174. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  2175. {
  2176. tmp = (const uint16_t *) husart->pTxBuffPtr;
  2177. husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
  2178. husart->pTxBuffPtr += 2U;
  2179. }
  2180. else
  2181. {
  2182. husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF);
  2183. }
  2184. if (--husart->TxXferCount == 0U)
  2185. {
  2186. /* Disable the USART Transmit data register empty Interrupt */
  2187. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  2188. /* Enable the USART Transmit Complete Interrupt */
  2189. SET_BIT(husart->Instance->CR1, USART_CR1_TCIE);
  2190. }
  2191. return HAL_OK;
  2192. }
  2193. else
  2194. {
  2195. return HAL_BUSY;
  2196. }
  2197. }
  2198. /**
  2199. * @brief Wraps up transmission in non blocking mode.
  2200. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  2201. * the configuration information for the specified USART module.
  2202. * @retval HAL status
  2203. */
  2204. static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
  2205. {
  2206. /* Disable the USART Transmit Complete Interrupt */
  2207. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TCIE);
  2208. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  2209. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2210. husart->State = HAL_USART_STATE_READY;
  2211. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2212. /* Call registered Tx Complete Callback */
  2213. husart->TxCpltCallback(husart);
  2214. #else
  2215. /* Call legacy weak Tx Complete Callback */
  2216. HAL_USART_TxCpltCallback(husart);
  2217. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2218. return HAL_OK;
  2219. }
  2220. /**
  2221. * @brief Simplex Receive an amount of data in non-blocking mode.
  2222. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  2223. * the configuration information for the specified USART module.
  2224. * @retval HAL status
  2225. */
  2226. static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
  2227. {
  2228. uint8_t *pdata8bits;
  2229. uint16_t *pdata16bits;
  2230. if (husart->State == HAL_USART_STATE_BUSY_RX)
  2231. {
  2232. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  2233. {
  2234. pdata8bits = NULL;
  2235. pdata16bits = (uint16_t *) husart->pRxBuffPtr;
  2236. *pdata16bits = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  2237. husart->pRxBuffPtr += 2U;
  2238. }
  2239. else
  2240. {
  2241. pdata8bits = (uint8_t *) husart->pRxBuffPtr;
  2242. pdata16bits = NULL;
  2243. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) || ((husart->Init.WordLength == USART_WORDLENGTH_8B) && (husart->Init.Parity == USART_PARITY_NONE)))
  2244. {
  2245. *pdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
  2246. }
  2247. else
  2248. {
  2249. *pdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
  2250. }
  2251. husart->pRxBuffPtr += 1U;
  2252. }
  2253. husart->RxXferCount--;
  2254. if (husart->RxXferCount == 0U)
  2255. {
  2256. /* Disable the USART RXNE Interrupt */
  2257. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  2258. /* Disable the USART Parity Error Interrupt */
  2259. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  2260. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  2261. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2262. husart->State = HAL_USART_STATE_READY;
  2263. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2264. /* Call registered Rx Complete Callback */
  2265. husart->RxCpltCallback(husart);
  2266. #else
  2267. /* Call legacy weak Rx Complete Callback */
  2268. HAL_USART_RxCpltCallback(husart);
  2269. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2270. return HAL_OK;
  2271. }
  2272. else
  2273. {
  2274. /* Send dummy byte in order to generate the clock for the slave to send the next data.
  2275. * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
  2276. * can be written for all the cases. */
  2277. husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x0FF);
  2278. }
  2279. return HAL_OK;
  2280. }
  2281. else
  2282. {
  2283. return HAL_BUSY;
  2284. }
  2285. }
  2286. /**
  2287. * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
  2288. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  2289. * the configuration information for the specified USART module.
  2290. * @retval HAL status
  2291. */
  2292. static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
  2293. {
  2294. const uint16_t *pdatatx16bits;
  2295. uint16_t *pdatarx16bits;
  2296. if (husart->State == HAL_USART_STATE_BUSY_TX_RX)
  2297. {
  2298. if (husart->TxXferCount != 0x00U)
  2299. {
  2300. if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
  2301. {
  2302. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  2303. {
  2304. pdatatx16bits = (const uint16_t *) husart->pTxBuffPtr;
  2305. husart->Instance->DR = (uint16_t)(*pdatatx16bits & (uint16_t)0x01FF);
  2306. husart->pTxBuffPtr += 2U;
  2307. }
  2308. else
  2309. {
  2310. husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF);
  2311. }
  2312. husart->TxXferCount--;
  2313. /* Check the latest data transmitted */
  2314. if (husart->TxXferCount == 0U)
  2315. {
  2316. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  2317. }
  2318. }
  2319. }
  2320. if (husart->RxXferCount != 0x00U)
  2321. {
  2322. if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
  2323. {
  2324. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  2325. {
  2326. pdatarx16bits = (uint16_t *) husart->pRxBuffPtr;
  2327. *pdatarx16bits = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  2328. husart->pRxBuffPtr += 2U;
  2329. }
  2330. else
  2331. {
  2332. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) || ((husart->Init.WordLength == USART_WORDLENGTH_8B) && (husart->Init.Parity == USART_PARITY_NONE)))
  2333. {
  2334. *husart->pRxBuffPtr = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
  2335. }
  2336. else
  2337. {
  2338. *husart->pRxBuffPtr = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
  2339. }
  2340. husart->pRxBuffPtr += 1U;
  2341. }
  2342. husart->RxXferCount--;
  2343. }
  2344. }
  2345. /* Check the latest data received */
  2346. if (husart->RxXferCount == 0U)
  2347. {
  2348. /* Disable the USART RXNE Interrupt */
  2349. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  2350. /* Disable the USART Parity Error Interrupt */
  2351. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  2352. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  2353. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2354. husart->State = HAL_USART_STATE_READY;
  2355. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2356. /* Call registered Tx Rx Complete Callback */
  2357. husart->TxRxCpltCallback(husart);
  2358. #else
  2359. /* Call legacy weak Tx Rx Complete Callback */
  2360. HAL_USART_TxRxCpltCallback(husart);
  2361. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2362. return HAL_OK;
  2363. }
  2364. return HAL_OK;
  2365. }
  2366. else
  2367. {
  2368. return HAL_BUSY;
  2369. }
  2370. }
  2371. /**
  2372. * @brief Configures the USART peripheral.
  2373. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  2374. * the configuration information for the specified USART module.
  2375. * @retval None
  2376. */
  2377. static void USART_SetConfig(USART_HandleTypeDef *husart)
  2378. {
  2379. uint32_t tmpreg = 0x00U;
  2380. uint32_t pclk;
  2381. /* Check the parameters */
  2382. assert_param(IS_USART_INSTANCE(husart->Instance));
  2383. assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
  2384. assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
  2385. assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
  2386. assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
  2387. assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
  2388. assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
  2389. assert_param(IS_USART_PARITY(husart->Init.Parity));
  2390. assert_param(IS_USART_MODE(husart->Init.Mode));
  2391. /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
  2392. receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
  2393. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  2394. /*---------------------------- USART CR2 Configuration ---------------------*/
  2395. tmpreg = husart->Instance->CR2;
  2396. /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  2397. tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP));
  2398. /* Configure the USART Clock, CPOL, CPHA and LastBit -----------------------*/
  2399. /* Set CPOL bit according to husart->Init.CLKPolarity value */
  2400. /* Set CPHA bit according to husart->Init.CLKPhase value */
  2401. /* Set LBCL bit according to husart->Init.CLKLastBit value */
  2402. /* Set Stop Bits: Set STOP[13:12] bits according to husart->Init.StopBits value */
  2403. tmpreg |= (uint32_t)(USART_CLOCK_ENABLE | husart->Init.CLKPolarity |
  2404. husart->Init.CLKPhase | husart->Init.CLKLastBit | husart->Init.StopBits);
  2405. /* Write to USART CR2 */
  2406. WRITE_REG(husart->Instance->CR2, (uint32_t)tmpreg);
  2407. /*-------------------------- USART CR1 Configuration -----------------------*/
  2408. tmpreg = husart->Instance->CR1;
  2409. /* Clear M, PCE, PS, TE, RE and OVER8 bits */
  2410. tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
  2411. USART_CR1_RE | USART_CR1_OVER8));
  2412. /* Configure the USART Word Length, Parity and mode:
  2413. Set the M bits according to husart->Init.WordLength value
  2414. Set PCE and PS bits according to husart->Init.Parity value
  2415. Set TE and RE bits according to husart->Init.Mode value
  2416. Force OVER8 bit to 1 in order to reach the max USART frequencies */
  2417. tmpreg |= (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
  2418. /* Write to USART CR1 */
  2419. WRITE_REG(husart->Instance->CR1, (uint32_t)tmpreg);
  2420. /*-------------------------- USART CR3 Configuration -----------------------*/
  2421. /* Clear CTSE and RTSE bits */
  2422. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
  2423. /*-------------------------- USART BRR Configuration -----------------------*/
  2424. #if defined(USART6) && defined(UART9) && defined(UART10)
  2425. if ((husart->Instance == USART1) || (husart->Instance == USART6) || (husart->Instance == UART9) || (husart->Instance == UART10))
  2426. {
  2427. pclk = HAL_RCC_GetPCLK2Freq();
  2428. husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
  2429. }
  2430. #elif defined(USART6)
  2431. if((husart->Instance == USART1) || (husart->Instance == USART6))
  2432. {
  2433. pclk = HAL_RCC_GetPCLK2Freq();
  2434. husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
  2435. }
  2436. #else
  2437. if(husart->Instance == USART1)
  2438. {
  2439. pclk = HAL_RCC_GetPCLK2Freq();
  2440. husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
  2441. }
  2442. #endif /* USART6 || UART9 || UART10 */
  2443. else
  2444. {
  2445. pclk = HAL_RCC_GetPCLK1Freq();
  2446. husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
  2447. }
  2448. }
  2449. /**
  2450. * @}
  2451. */
  2452. /**
  2453. * @}
  2454. */
  2455. #endif /* HAL_USART_MODULE_ENABLED */
  2456. /**
  2457. * @}
  2458. */
  2459. /**
  2460. * @}
  2461. */