Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

1943 lines
62 KiB

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