2 ******************************************************************************
3 * @file stm32l1xx_hal_uart.c
4 * @author MCD Application Team
6 * @date 5-September-2014
7 * @brief UART HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State and Errors functions
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
19 The UART HAL driver can be used as follows:
21 (#) Declare a UART_HandleTypeDef handle structure.
23 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
24 (##) Enable the USARTx interface clock.
25 (##) UART pins configuration:
26 (+++) Enable the clock for the UART GPIOs.
27 (+++) Configure these UART pins as alternate function pull-up.
28 (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
29 and HAL_UART_Receive_IT() APIs):
30 (+++) Configure the USARTx interrupt priority.
31 (+++) Enable the NVIC USART IRQ handle.
32 (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
33 and HAL_UART_Receive_DMA() APIs):
34 (+++) Declare a DMA handle structure for the Tx/Rx channel.
35 (+++) Enable the DMAx interface clock.
36 (+++) Configure the declared DMA handle structure with the required
38 (+++) Configure the DMA Tx/Rx channel.
39 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
40 (+++) Configure the priority and enable the NVIC for the transfer complete
41 interrupt on the DMA Tx/Rx channel.
43 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
44 flow control and Mode(Receiver/Transmitter) in the huart Init structure.
46 (#) For the UART asynchronous mode, initialize the UART registers by calling
47 the HAL_UART_Init() API.
49 (#) For the UART Half duplex mode, initialize the UART registers by calling
50 the HAL_HalfDuplex_Init() API.
52 (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
54 (#) For the Multi-Processor mode, initialize the UART registers by calling
55 the HAL_MultiProcessor_Init() API.
58 (@) The specific UART interrupts (Transmission complete interrupt,
59 RXNE interrupt and Error Interrupts) will be managed using the macros
60 __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit
64 (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the
65 low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customed
66 HAL_UART_MspInit() API.
69 Three operation modes are available within this driver :
71 *** Polling mode IO operation ***
72 =================================
74 (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
75 (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
77 *** Interrupt mode IO operation ***
78 ===================================
80 (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
81 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
82 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
83 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
84 add his own code by customization of function pointer HAL_UART_TxCpltCallback
85 (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
86 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
87 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
88 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
89 add his own code by customization of function pointer HAL_UART_RxCpltCallback
90 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
91 add his own code by customization of function pointer HAL_UART_ErrorCallback
93 *** DMA mode IO operation ***
94 ==============================
96 (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
97 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
98 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
99 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
100 add his own code by customization of function pointer HAL_UART_TxCpltCallback
101 (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
102 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
103 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
104 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
105 add his own code by customization of function pointer HAL_UART_RxCpltCallback
106 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
107 add his own code by customization of function pointer HAL_UART_ErrorCallback
108 (+) Pause the DMA Transfer using HAL_UART_DMAPause()
109 (+) Resume the DMA Transfer using HAL_UART_DMAResume()
110 (+) Stop the DMA Transfer using HAL_UART_DMAStop()
112 *** UART HAL driver macros list ***
113 =============================================
115 Below the list of most used macros in UART HAL driver.
117 (+) __HAL_UART_ENABLE: Enable the UART peripheral
118 (+) __HAL_UART_DISABLE: Disable the UART peripheral
119 (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
120 (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
121 (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
122 (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
125 (@) You can refer to the UART HAL driver header file for more useful macros
128 ******************************************************************************
131 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
133 * Redistribution and use in source and binary forms, with or without modification,
134 * are permitted provided that the following conditions are met:
135 * 1. Redistributions of source code must retain the above copyright notice,
136 * this list of conditions and the following disclaimer.
137 * 2. Redistributions in binary form must reproduce the above copyright notice,
138 * this list of conditions and the following disclaimer in the documentation
139 * and/or other materials provided with the distribution.
140 * 3. Neither the name of STMicroelectronics nor the names of its contributors
141 * may be used to endorse or promote products derived from this software
142 * without specific prior written permission.
144 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
145 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
146 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
147 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
148 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
149 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
150 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
151 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
152 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
153 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
155 ******************************************************************************
158 /* Includes ------------------------------------------------------------------*/
159 #include "stm32l1xx_hal.h"
161 /** @addtogroup STM32L1xx_HAL_Driver
165 /** @defgroup UART UART HAL module driver
166 * @brief HAL UART module driver
169 #ifdef HAL_UART_MODULE_ENABLED
171 /* Private typedef -----------------------------------------------------------*/
172 /* Private define ------------------------------------------------------------*/
173 /** @defgroup UART_Private_Constants UART Private Constants
176 #define UART_TIMEOUT_VALUE 22000
181 /* Private macro -------------------------------------------------------------*/
182 /* Private variables ---------------------------------------------------------*/
183 /* Private function prototypes -----------------------------------------------*/
184 /** @addtogroup UART_Private_Functions UART Private Functions
187 static void UART_SetConfig (UART_HandleTypeDef *huart);
188 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
189 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
190 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
191 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
192 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
193 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
194 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
195 static void UART_DMAError(DMA_HandleTypeDef *hdma);
196 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
201 /* Exported functions ---------------------------------------------------------*/
203 /** @defgroup UART_Exported_Functions UART Exported Functions
207 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
208 * @brief Initialization and Configuration functions
211 ===============================================================================
212 ##### Initialization and Configuration functions #####
213 ===============================================================================
215 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
216 in asynchronous mode.
217 (+) For the asynchronous mode only these parameters can be configured:
221 (++) Parity: If the parity is enabled, then the MSB bit of the data written
222 in the data register is transmitted but is changed by the parity bit.
223 Depending on the frame length defined by the M bit (8-bits or 9-bits),
224 the possible UART frame formats are as listed in the following table:
225 +-------------------------------------------------------------+
226 | M bit | PCE bit | UART frame |
227 |---------------------|---------------------------------------|
228 | 0 | 0 | | SB | 8 bit data | STB | |
229 |---------|-----------|---------------------------------------|
230 | 0 | 1 | | SB | 7 bit data | PB | STB | |
231 |---------|-----------|---------------------------------------|
232 | 1 | 0 | | SB | 9 bit data | STB | |
233 |---------|-----------|---------------------------------------|
234 | 1 | 1 | | SB | 8 bit data | PB | STB | |
235 +-------------------------------------------------------------+
236 (++) Hardware flow control
237 (++) Receiver/transmitter modes
238 (++) Over Sampling Methode
240 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
241 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor
242 configuration procedures (details for the procedures are available in reference manual (RM0038)).
249 * @brief Initializes the UART mode according to the specified parameters in
250 * the UART_InitTypeDef and create the associated handle.
251 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
252 * the configuration information for the specified UART module.
255 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
257 /* Check the UART handle allocation */
258 if(huart == HAL_NULL)
263 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
265 /* Check the parameters */
266 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
270 /* Check the parameters */
271 assert_param(IS_UART_INSTANCE(huart->Instance));
274 if(huart->State == HAL_UART_STATE_RESET)
276 /* Init the low level hardware */
277 HAL_UART_MspInit(huart);
280 huart->State = HAL_UART_STATE_BUSY;
282 /* Disable the peripheral */
283 __HAL_UART_DISABLE(huart);
285 /* Set the UART Communication parameters */
286 UART_SetConfig(huart);
288 /* In asynchronous mode, the following bits must be kept cleared:
289 - LINEN and CLKEN bits in the USART_CR2 register,
290 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
291 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
292 huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
294 /* Enable the peripheral */
295 __HAL_UART_ENABLE(huart);
297 /* Initialize the UART state */
298 huart->ErrorCode = HAL_UART_ERROR_NONE;
299 huart->State= HAL_UART_STATE_READY;
305 * @brief Initializes the half-duplex mode according to the specified
306 * parameters in the UART_InitTypeDef and create the associated handle.
307 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
308 * the configuration information for the specified UART module.
311 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
313 /* Check the UART handle allocation */
314 if(huart == HAL_NULL)
319 /* Check UART instance */
320 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
322 if(huart->State == HAL_UART_STATE_RESET)
324 /* Init the low level hardware */
325 HAL_UART_MspInit(huart);
328 huart->State = HAL_UART_STATE_BUSY;
330 /* Disable the peripheral */
331 __HAL_UART_DISABLE(huart);
333 /* Set the UART Communication parameters */
334 UART_SetConfig(huart);
336 /* In half-duplex mode, the following bits must be kept cleared:
337 - LINEN and CLKEN bits in the USART_CR2 register,
338 - SCEN and IREN bits in the USART_CR3 register.*/
339 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
340 huart->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_SCEN);
342 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
343 huart->Instance->CR3 |= USART_CR3_HDSEL;
345 /* Enable the peripheral */
346 __HAL_UART_ENABLE(huart);
348 /* Initialize the UART state*/
349 huart->ErrorCode = HAL_UART_ERROR_NONE;
350 huart->State= HAL_UART_STATE_READY;
356 * @brief Initializes the LIN mode according to the specified
357 * parameters in the UART_InitTypeDef and create the associated handle.
358 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
359 * the configuration information for the specified UART module.
360 * @param BreakDetectLength: Specifies the LIN break detection length.
361 * This parameter can be one of the following values:
362 * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
363 * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
366 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
368 /* Check the UART handle allocation */
369 if(huart == HAL_NULL)
374 /* Check the LIN UART instance */
375 assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
376 /* Check the Break detection length parameter */
377 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
379 /* LIN mode limited to 16-bit oversampling only */
380 if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
385 if(huart->State == HAL_UART_STATE_RESET)
387 /* Init the low level hardware */
388 HAL_UART_MspInit(huart);
391 huart->State = HAL_UART_STATE_BUSY;
393 /* Disable the peripheral */
394 __HAL_UART_DISABLE(huart);
396 /* Set the UART Communication parameters */
397 UART_SetConfig(huart);
399 /* In LIN mode, the following bits must be kept cleared:
400 - CLKEN bits in the USART_CR2 register,
401 - SCEN and IREN bits in the USART_CR3 register.*/
402 huart->Instance->CR2 &= ~(USART_CR2_CLKEN);
403 huart->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN);
405 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
406 huart->Instance->CR2 |= USART_CR2_LINEN;
408 /* Set the USART LIN Break detection length. */
409 MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
411 /* Enable the peripheral */
412 __HAL_UART_ENABLE(huart);
414 /* Initialize the UART state*/
415 huart->ErrorCode = HAL_UART_ERROR_NONE;
416 huart->State= HAL_UART_STATE_READY;
422 * @brief Initializes the Multi-Processor mode according to the specified
423 * parameters in the UART_InitTypeDef and create the associated handle.
424 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
425 * the configuration information for the specified UART module.
426 * @param Address: UART node address
427 * @param WakeUpMethod: specifies the UART wakeup method.
428 * This parameter can be one of the following values:
429 * @arg UART_WAKEUPMETHOD_IDLELINE: Wakeup by an idle line detection
430 * @arg UART_WAKEUPMETHOD_ADDRESSMARK: Wakeup by an address mark
433 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
435 /* Check the UART handle allocation */
436 if(huart == HAL_NULL)
441 /* Check UART instance capabilities */
442 assert_param(IS_UART_MULTIPROCESSOR_INSTANCE(huart->Instance));
444 /* Check the Address & wake up method parameters */
445 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
446 assert_param(IS_UART_ADDRESS(Address));
448 if(huart->State == HAL_UART_STATE_RESET)
450 /* Init the low level hardware */
451 HAL_UART_MspInit(huart);
454 huart->State = HAL_UART_STATE_BUSY;
456 /* Disable the peripheral */
457 __HAL_UART_DISABLE(huart);
459 /* Set the UART Communication parameters */
460 UART_SetConfig(huart);
462 /* In Multi-Processor mode, the following bits must be kept cleared:
463 - LINEN and CLKEN bits in the USART_CR2 register,
464 - SCEN, HDSEL and IREN bits in the USART_CR3 register */
465 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
466 huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
468 /* Set the USART address node */
469 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, Address);
471 /* Set the wake up method by setting the WAKE bit in the CR1 register */
472 MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
474 /* Enable the peripheral */
475 __HAL_UART_ENABLE(huart);
477 /* Initialize the UART state */
478 huart->ErrorCode = HAL_UART_ERROR_NONE;
479 huart->State= HAL_UART_STATE_READY;
485 * @brief DeInitializes the UART peripheral.
486 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
487 * the configuration information for the specified UART module.
490 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
492 /* Check the UART handle allocation */
493 if(huart == HAL_NULL)
498 /* Check the parameters */
499 assert_param(IS_UART_INSTANCE(huart->Instance));
501 huart->State = HAL_UART_STATE_BUSY;
503 /* Disable the Peripheral */
504 __HAL_UART_DISABLE(huart);
506 /* DeInit the low level hardware */
507 HAL_UART_MspDeInit(huart);
509 huart->ErrorCode = HAL_UART_ERROR_NONE;
510 huart->State = HAL_UART_STATE_RESET;
519 * @brief UART MSP Init.
520 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
521 * the configuration information for the specified UART module.
524 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
526 /* NOTE: This function Should not be modified, when the callback is needed,
527 the HAL_UART_MspInit could be implemented in the user file
532 * @brief UART MSP DeInit.
533 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
534 * the configuration information for the specified UART module.
537 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
539 /* NOTE: This function Should not be modified, when the callback is needed,
540 the HAL_UART_MspDeInit could be implemented in the user file
548 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
549 * @brief UART Transmit and Receive functions
552 ==============================================================================
553 ##### IO operation functions #####
554 ==============================================================================
556 This subsection provides a set of functions allowing to manage the UART asynchronous
557 and Half duplex data transfers.
559 (#) There are two modes of transfer:
560 (++) Blocking mode: The communication is performed in polling mode.
561 The HAL status of all data processing is returned by the same function
562 after finishing transfer.
563 (++) Non blocking mode: The communication is performed using Interrupts
564 or DMA, these APIs return the HAL status.
565 The end of the data processing will be indicated through the
566 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
568 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
569 will be executed respectivelly at the end of the transmit or receive process.
570 The HAL_UART_ErrorCallback() user callback will be executed when
571 a communication error is detected.
573 (#) Blocking mode APIs are:
574 (++) HAL_UART_Transmit()
575 (++) HAL_UART_Receive()
577 (#) Non Blocking mode APIs with Interrupt are:
578 (++) HAL_UART_Transmit_IT()
579 (++) HAL_UART_Receive_IT()
580 (++) HAL_UART_IRQHandler()
582 (#) Non Blocking mode functions with DMA are:
583 (++) HAL_UART_Transmit_DMA()
584 (++) HAL_UART_Receive_DMA()
585 (++) HAL_UART_DMAPause()
586 (++) HAL_UART_DMAResume()
587 (++) HAL_UART_DMAStop()
589 (#) A set of Transfer Complete Callbacks are provided in non blocking mode:
590 (++) HAL_UART_TxHalfCpltCallback()
591 (++) HAL_UART_TxCpltCallback()
592 (++) HAL_UART_RxHalfCpltCallback()
593 (++) HAL_UART_RxCpltCallback()
594 (++) HAL_UART_ErrorCallback()
597 (@) In the Half duplex communication, it is forbidden to run the transmit
598 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX
606 * @brief Sends an amount of data in blocking mode.
607 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
608 * the configuration information for the specified UART module.
609 * @param pData: Pointer to data buffer
610 * @param Size: Amount of data to be sent
611 * @param Timeout: Timeout duration
614 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
620 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_RX))
622 if((pData == HAL_NULL) || (Size == 0))
630 huart->ErrorCode = HAL_UART_ERROR_NONE;
631 /* Check if a non-blocking receive process is ongoing or not */
632 if(huart->State == HAL_UART_STATE_BUSY_RX)
634 huart->State = HAL_UART_STATE_BUSY_TX_RX;
638 huart->State = HAL_UART_STATE_BUSY_TX;
641 huart->TxXferSize = Size;
642 huart->TxXferCount = Size;
643 while(huart->TxXferCount > 0)
645 huart->TxXferCount--;
646 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
648 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
652 tmp = (uint16_t*) pData;
653 huart->Instance->DR = (*tmp & (uint16_t)0x01FF);
654 if(huart->Init.Parity == UART_PARITY_NONE)
665 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
669 huart->Instance->DR = (*pData++ & (uint8_t)0xFF);
673 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, Timeout) != HAL_OK)
678 /* Check if a non-blocking receive process is ongoing or not */
679 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
681 huart->State = HAL_UART_STATE_BUSY_RX;
685 huart->State = HAL_UART_STATE_READY;
688 /* Process Unlocked */
700 * @brief Receives an amount of data in 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 received
705 * @param Timeout: Timeout duration
708 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
714 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_TX))
716 if((pData == HAL_NULL ) || (Size == 0))
724 huart->ErrorCode = HAL_UART_ERROR_NONE;
725 /* Check if a non-blocking transmit process is ongoing or not */
726 if(huart->State == HAL_UART_STATE_BUSY_TX)
728 huart->State = HAL_UART_STATE_BUSY_TX_RX;
732 huart->State = HAL_UART_STATE_BUSY_RX;
735 huart->RxXferSize = Size;
736 huart->RxXferCount = Size;
738 /* Check the remain data to be received */
739 while(huart->RxXferCount > 0)
741 huart->RxXferCount--;
742 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
744 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
748 tmp = (uint16_t*) pData ;
749 if(huart->Init.Parity == UART_PARITY_NONE)
751 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
756 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
763 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
767 if(huart->Init.Parity == UART_PARITY_NONE)
769 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
773 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
779 /* Check if a non-blocking transmit process is ongoing or not */
780 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
782 huart->State = HAL_UART_STATE_BUSY_TX;
786 huart->State = HAL_UART_STATE_READY;
788 /* Process Unlocked */
800 * @brief Sends an amount of data in non blocking mode.
801 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
802 * the configuration information for the specified UART module.
803 * @param pData: Pointer to data buffer
804 * @param Size: Amount of data to be sent
807 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
812 if((tmp == HAL_UART_STATE_READY) || (tmp == HAL_UART_STATE_BUSY_RX))
814 if((pData == HAL_NULL ) || (Size == 0))
822 huart->pTxBuffPtr = pData;
823 huart->TxXferSize = Size;
824 huart->TxXferCount = Size;
826 huart->ErrorCode = HAL_UART_ERROR_NONE;
827 /* Check if a receive process is ongoing or not */
828 if(huart->State == HAL_UART_STATE_BUSY_RX)
830 huart->State = HAL_UART_STATE_BUSY_TX_RX;
834 huart->State = HAL_UART_STATE_BUSY_TX;
837 /* Enable the UART Parity Error Interrupt */
838 __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
840 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
841 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
843 /* Process Unlocked */
846 /* Enable the UART Transmit data register empty Interrupt */
847 __HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
858 * @brief Receives an amount of data in non blocking mode
859 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
860 * the configuration information for the specified UART module.
861 * @param pData: Pointer to data buffer
862 * @param Size: Amount of data to be received
865 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
870 if((tmp == HAL_UART_STATE_READY) || (tmp == HAL_UART_STATE_BUSY_TX))
872 if((pData == HAL_NULL ) || (Size == 0))
880 huart->pRxBuffPtr = pData;
881 huart->RxXferSize = Size;
882 huart->RxXferCount = Size;
884 huart->ErrorCode = HAL_UART_ERROR_NONE;
885 /* Check if a transmit process is ongoing or not */
886 if(huart->State == HAL_UART_STATE_BUSY_TX)
888 huart->State = HAL_UART_STATE_BUSY_TX_RX;
892 huart->State = HAL_UART_STATE_BUSY_RX;
895 /* Enable the UART Parity Error Interrupt */
896 __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
898 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
899 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
901 /* Process Unlocked */
904 /* Enable the UART Data Register not empty Interrupt */
905 __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
916 * @brief Sends an amount of data in non blocking mode.
917 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
918 * the configuration information for the specified UART module.
919 * @param pData: Pointer to data buffer
920 * @param Size: Amount of data to be sent
923 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
929 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_RX))
931 if((pData == HAL_NULL ) || (Size == 0))
939 huart->pTxBuffPtr = pData;
940 huart->TxXferSize = Size;
941 huart->TxXferCount = Size;
943 huart->ErrorCode = HAL_UART_ERROR_NONE;
944 /* Check if a receive process is ongoing or not */
945 if(huart->State == HAL_UART_STATE_BUSY_RX)
947 huart->State = HAL_UART_STATE_BUSY_TX_RX;
951 huart->State = HAL_UART_STATE_BUSY_TX;
954 /* Set the UART DMA transfer complete callback */
955 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
957 /* Set the UART DMA Half transfer complete callback */
958 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
960 /* Set the DMA error callback */
961 huart->hdmatx->XferErrorCallback = UART_DMAError;
963 /* Enable the UART transmit DMA channel */
964 tmp = (uint32_t*)&pData;
965 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->DR, Size);
967 /* Enable the DMA transfer for transmit request by setting the DMAT bit
968 in the UART CR3 register */
969 huart->Instance->CR3 |= USART_CR3_DMAT;
971 /* Process Unlocked */
983 * @brief Receives an amount of data in non blocking mode.
984 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
985 * the configuration information for the specified UART module.
986 * @param pData: Pointer to data buffer
987 * @param Size: Amount of data to be received
988 * @note When the UART parity is enabled (PCE = 1), the received data contain
989 * the parity bit (MSB position)
992 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
998 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_TX))
1000 if((pData == HAL_NULL ) || (Size == 0))
1005 /* Process Locked */
1008 huart->pRxBuffPtr = pData;
1009 huart->RxXferSize = Size;
1011 huart->ErrorCode = HAL_UART_ERROR_NONE;
1012 /* Check if a transmit process is ongoing or not */
1013 if(huart->State == HAL_UART_STATE_BUSY_TX)
1015 huart->State = HAL_UART_STATE_BUSY_TX_RX;
1019 huart->State = HAL_UART_STATE_BUSY_RX;
1022 /* Set the UART DMA transfer complete callback */
1023 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
1025 /* Set the UART DMA Half transfer complete callback */
1026 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
1028 /* Set the DMA error callback */
1029 huart->hdmarx->XferErrorCallback = UART_DMAError;
1031 /* Enable the DMA channel */
1032 tmp = (uint32_t*)&pData;
1033 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size);
1035 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1036 in the UART CR3 register */
1037 huart->Instance->CR3 |= USART_CR3_DMAR;
1039 /* Process Unlocked */
1040 __HAL_UNLOCK(huart);
1051 * @brief Pauses the DMA Transfer.
1052 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1053 * the configuration information for the specified UART module.
1054 * @retval HAL status
1056 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
1058 /* Process Locked */
1061 if(huart->State == HAL_UART_STATE_BUSY_TX)
1063 /* Disable the UART DMA Tx request */
1064 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
1066 else if(huart->State == HAL_UART_STATE_BUSY_RX)
1068 /* Disable the UART DMA Rx request */
1069 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
1071 else if (huart->State == HAL_UART_STATE_BUSY_TX_RX)
1073 /* Disable the UART DMA Tx & Rx requests */
1074 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
1075 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
1079 /* Process Unlocked */
1080 __HAL_UNLOCK(huart);
1085 /* Process Unlocked */
1086 __HAL_UNLOCK(huart);
1092 * @brief Resumes the DMA Transfer.
1093 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1094 * the configuration information for the specified UART module.
1095 * @retval HAL status
1097 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
1099 /* Process Locked */
1102 if(huart->State == HAL_UART_STATE_BUSY_TX)
1104 /* Enable the UART DMA Tx request */
1105 huart->Instance->CR3 |= USART_CR3_DMAT;
1107 else if(huart->State == HAL_UART_STATE_BUSY_RX)
1109 /* Clear the Overrun flag before resumming the Rx transfer*/
1110 __HAL_UART_CLEAR_OREFLAG(huart);
1111 /* Enable the UART DMA Rx request */
1112 huart->Instance->CR3 |= USART_CR3_DMAR;
1114 else if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
1116 /* Clear the Overrun flag before resumming the Rx transfer*/
1117 __HAL_UART_CLEAR_OREFLAG(huart);
1118 /* Enable the UART DMA Tx & Rx request */
1119 huart->Instance->CR3 |= USART_CR3_DMAT;
1120 huart->Instance->CR3 |= USART_CR3_DMAR;
1124 /* Process Unlocked */
1125 __HAL_UNLOCK(huart);
1130 /* Process Unlocked */
1131 __HAL_UNLOCK(huart);
1137 * @brief Stops the DMA Transfer.
1138 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1139 * the configuration information for the specified UART module.
1140 * @retval HAL status
1142 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
1144 /* The Lock is not implemented on this API to allow the user application
1145 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
1146 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
1147 and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
1150 /* Disable the UART Tx/Rx DMA requests */
1151 huart->Instance->CR3 &= ~USART_CR3_DMAT;
1152 huart->Instance->CR3 &= ~USART_CR3_DMAR;
1154 /* Abort the UART DMA tx channel */
1155 if(huart->hdmatx != HAL_NULL)
1157 HAL_DMA_Abort(huart->hdmatx);
1159 /* Abort the UART DMA rx channel */
1160 if(huart->hdmarx != HAL_NULL)
1162 HAL_DMA_Abort(huart->hdmarx);
1165 huart->State = HAL_UART_STATE_READY;
1171 * @brief This function handles UART interrupt request.
1172 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1173 * the configuration information for the specified UART module.
1176 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
1178 uint32_t tmp1 = 0, tmp2 = 0;
1180 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_PE);
1181 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE);
1182 /* UART parity error interrupt occurred ------------------------------------*/
1183 if((tmp1 != RESET) && (tmp2 != RESET))
1185 __HAL_UART_CLEAR_PEFLAG(huart);
1187 huart->ErrorCode |= HAL_UART_ERROR_PE;
1190 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_FE);
1191 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
1192 /* UART frame error interrupt occurred -------------------------------------*/
1193 if((tmp1 != RESET) && (tmp2 != RESET))
1195 __HAL_UART_CLEAR_FEFLAG(huart);
1197 huart->ErrorCode |= HAL_UART_ERROR_FE;
1200 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_NE);
1201 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
1202 /* UART noise error interrupt occurred -------------------------------------*/
1203 if((tmp1 != RESET) && (tmp2 != RESET))
1205 __HAL_UART_CLEAR_NEFLAG(huart);
1207 huart->ErrorCode |= HAL_UART_ERROR_NE;
1210 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_ORE);
1211 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
1212 /* UART Over-Run interrupt occurred ----------------------------------------*/
1213 if((tmp1 != RESET) && (tmp2 != RESET))
1215 __HAL_UART_CLEAR_OREFLAG(huart);
1217 huart->ErrorCode |= HAL_UART_ERROR_ORE;
1220 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE);
1221 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE);
1222 /* UART in mode Receiver ---------------------------------------------------*/
1223 if((tmp1 != RESET) && (tmp2 != RESET))
1225 UART_Receive_IT(huart);
1228 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_TXE);
1229 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE);
1230 /* UART in mode Transmitter ------------------------------------------------*/
1231 if((tmp1 != RESET) && (tmp2 != RESET))
1233 UART_Transmit_IT(huart);
1236 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_TC);
1237 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC);
1238 /* UART in mode Transmitter end --------------------------------------------*/
1239 if((tmp1 != RESET) && (tmp2 != RESET))
1241 UART_EndTransmit_IT(huart);
1244 if(huart->ErrorCode != HAL_UART_ERROR_NONE)
1246 /* Set the UART state ready to be able to start again the process */
1247 huart->State = HAL_UART_STATE_READY;
1249 HAL_UART_ErrorCallback(huart);
1254 * @brief Tx Transfer completed callbacks.
1255 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1256 * the configuration information for the specified UART module.
1259 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
1261 /* NOTE: This function Should not be modified, when the callback is needed,
1262 the HAL_UART_TxCpltCallback could be implemented in the user file
1267 * @brief Tx Half Transfer completed callbacks.
1268 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1269 * the configuration information for the specified UART module.
1272 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
1274 /* NOTE: This function Should not be modified, when the callback is needed,
1275 the HAL_UART_TxHalfCpltCallback could be implemented in the user file
1280 * @brief Rx Transfer completed callbacks.
1281 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1282 * the configuration information for the specified UART module.
1285 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
1287 /* NOTE: This function Should not be modified, when the callback is needed,
1288 the HAL_UART_RxCpltCallback could be implemented in the user file
1293 * @brief Rx Half Transfer completed callbacks.
1294 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1295 * the configuration information for the specified UART module.
1298 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
1300 /* NOTE: This function Should not be modified, when the callback is needed,
1301 the HAL_UART_RxHalfCpltCallback could be implemented in the user file
1306 * @brief UART error callbacks.
1307 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1308 * the configuration information for the specified UART module.
1311 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
1313 /* NOTE: This function Should not be modified, when the callback is needed,
1314 the HAL_UART_ErrorCallback could be implemented in the user file
1322 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
1323 * @brief UART control functions
1326 ==============================================================================
1327 ##### Peripheral Control functions #####
1328 ==============================================================================
1330 This subsection provides a set of functions allowing to control the UART:
1331 (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character.
1332 (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode.
1333 (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software.
1334 (+) HAL_HalfDuplex_EnableTransmitter() API to enable the UART transmitter and disables the UART receiver in Half Duplex mode
1335 (+) HAL_HalfDuplex_EnableReceiver() API to enable the UART receiver and disables the UART transmitter in Half Duplex mode
1342 * @brief Transmits break characters.
1343 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1344 * the configuration information for the specified UART module.
1345 * @retval HAL status
1347 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
1349 /* Check the parameters */
1350 assert_param(IS_UART_INSTANCE(huart->Instance));
1352 /* Process Locked */
1355 huart->State = HAL_UART_STATE_BUSY;
1357 /* Send break characters */
1358 huart->Instance->CR1 |= USART_CR1_SBK;
1360 huart->State = HAL_UART_STATE_READY;
1362 /* Process Unlocked */
1363 __HAL_UNLOCK(huart);
1369 * @brief Enters the UART in mute mode.
1370 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1371 * the configuration information for the specified UART module.
1372 * @retval HAL status
1374 HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
1376 /* Check the parameters */
1377 assert_param(IS_UART_INSTANCE(huart->Instance));
1379 /* Process Locked */
1382 huart->State = HAL_UART_STATE_BUSY;
1384 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
1385 huart->Instance->CR1 |= USART_CR1_RWU;
1387 huart->State = HAL_UART_STATE_READY;
1389 /* Process Unlocked */
1390 __HAL_UNLOCK(huart);
1396 * @brief Exits the UART mute mode: wake up software.
1397 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1398 * the configuration information for the specified UART module.
1399 * @retval HAL status
1401 HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart)
1403 /* Check the parameters */
1404 assert_param(IS_UART_INSTANCE(huart->Instance));
1406 /* Process Locked */
1409 huart->State = HAL_UART_STATE_BUSY;
1411 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
1412 huart->Instance->CR1 &= (uint32_t)~((uint32_t)USART_CR1_RWU);
1414 huart->State = HAL_UART_STATE_READY;
1416 /* Process Unlocked */
1417 __HAL_UNLOCK(huart);
1423 * @brief Enables the UART transmitter and disables the UART receiver.
1424 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1425 * the configuration information for the specified UART module.
1426 * @retval HAL status
1428 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
1430 uint32_t tmpreg = 0x00;
1432 /* Process Locked */
1435 huart->State = HAL_UART_STATE_BUSY;
1437 /*-------------------------- USART CR1 Configuration -----------------------*/
1438 tmpreg = huart->Instance->CR1;
1440 /* Clear TE and RE bits */
1441 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
1443 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
1444 tmpreg |= (uint32_t)USART_CR1_TE;
1446 /* Write to USART CR1 */
1447 huart->Instance->CR1 = (uint32_t)tmpreg;
1449 huart->State = HAL_UART_STATE_READY;
1451 /* Process Unlocked */
1452 __HAL_UNLOCK(huart);
1458 * @brief Enables the UART receiver and disables the UART transmitter.
1459 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1460 * the configuration information for the specified UART module.
1461 * @retval HAL status
1463 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
1465 uint32_t tmpreg = 0x00;
1467 /* Process Locked */
1470 huart->State = HAL_UART_STATE_BUSY;
1472 /*-------------------------- USART CR1 Configuration -----------------------*/
1473 tmpreg = huart->Instance->CR1;
1475 /* Clear TE and RE bits */
1476 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
1478 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
1479 tmpreg |= (uint32_t)USART_CR1_RE;
1481 /* Write to USART CR1 */
1482 huart->Instance->CR1 = (uint32_t)tmpreg;
1484 huart->State = HAL_UART_STATE_READY;
1486 /* Process Unlocked */
1487 __HAL_UNLOCK(huart);
1496 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Errors functions
1497 * @brief UART State and Errors functions
1500 ==============================================================================
1501 ##### Peripheral State and Errors functions #####
1502 ==============================================================================
1504 This subsection provides a set of functions allowing to return the State of
1505 UART communication process, return Peripheral Errors occurred during communication
1507 (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral.
1508 (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication.
1515 * @brief Returns the UART state.
1516 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1517 * the configuration information for the specified UART module.
1520 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
1522 return huart->State;
1526 * @brief Return the UART error code
1527 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1528 * the configuration information for the specified UART.
1529 * @retval UART Error Code
1531 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
1533 return huart->ErrorCode;
1544 /** @defgroup UART_Private_Functions UART Private Functions
1545 * @brief UART Private functions
1549 * @brief DMA UART transmit process complete callback.
1550 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1551 * the configuration information for the specified DMA module.
1554 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1556 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1557 /* DMA Normal mode*/
1558 if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
1560 huart->TxXferCount = 0;
1562 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1563 in the UART CR3 register */
1564 huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
1566 /* Wait for UART TC Flag */
1567 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, UART_TIMEOUT_VALUE) != HAL_OK)
1569 /* Timeout occurred */
1570 huart->State = HAL_UART_STATE_TIMEOUT;
1571 HAL_UART_ErrorCallback(huart);
1576 /* Check if a receive process is ongoing or not */
1577 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
1579 huart->State = HAL_UART_STATE_BUSY_RX;
1583 huart->State = HAL_UART_STATE_READY;
1585 HAL_UART_TxCpltCallback(huart);
1588 /* DMA Circular mode */
1591 HAL_UART_TxCpltCallback(huart);
1596 * @brief DMA UART transmit process half complete callback
1597 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1598 * the configuration information for the specified DMA module.
1601 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
1603 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1605 HAL_UART_TxHalfCpltCallback(huart);
1609 * @brief DMA UART receive process complete callback.
1610 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1611 * the configuration information for the specified DMA module.
1614 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1616 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1617 /* DMA Normal mode*/
1618 if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
1620 huart->RxXferCount = 0;
1622 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1623 in the UART CR3 register */
1624 huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
1626 /* Check if a transmit process is ongoing or not */
1627 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
1629 huart->State = HAL_UART_STATE_BUSY_TX;
1633 huart->State = HAL_UART_STATE_READY;
1636 HAL_UART_RxCpltCallback(huart);
1640 * @brief DMA UART receive process half complete callback
1641 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1642 * the configuration information for the specified DMA module.
1645 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
1647 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1649 HAL_UART_RxHalfCpltCallback(huart);
1653 * @brief DMA UART communication error callback.
1654 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1655 * the configuration information for the specified DMA module.
1658 static void UART_DMAError(DMA_HandleTypeDef *hdma)
1660 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1661 huart->RxXferCount = 0;
1662 huart->TxXferCount = 0;
1663 huart->State= HAL_UART_STATE_READY;
1664 huart->ErrorCode |= HAL_UART_ERROR_DMA;
1665 HAL_UART_ErrorCallback(huart);
1669 * @brief This function handles UART Communication Timeout.
1670 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1671 * the configuration information for the specified UART module.
1672 * @param Flag: specifies the UART flag to check.
1673 * @param Status: The new Flag status (SET or RESET).
1674 * @param Timeout: Timeout duration
1675 * @retval HAL status
1677 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
1679 uint32_t tickstart = 0;
1682 tickstart = HAL_GetTick();
1684 /* Wait until flag is set */
1687 while(__HAL_UART_GET_FLAG(huart, Flag) == RESET)
1689 /* Check for the Timeout */
1690 if(Timeout != HAL_MAX_DELAY)
1692 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1694 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1695 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
1696 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
1697 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
1698 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
1700 huart->State= HAL_UART_STATE_READY;
1702 /* Process Unlocked */
1703 __HAL_UNLOCK(huart);
1712 while(__HAL_UART_GET_FLAG(huart, Flag) != RESET)
1714 /* Check for the Timeout */
1715 if(Timeout != HAL_MAX_DELAY)
1717 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1719 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1720 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
1721 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
1722 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
1723 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
1725 huart->State= HAL_UART_STATE_READY;
1727 /* Process Unlocked */
1728 __HAL_UNLOCK(huart);
1739 * @brief Sends an amount of data in non blocking mode.
1740 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1741 * the configuration information for the specified UART module.
1742 * @retval HAL status
1744 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
1749 tmp1 = huart->State;
1750 if((tmp1 == HAL_UART_STATE_BUSY_TX) || (tmp1 == HAL_UART_STATE_BUSY_TX_RX))
1752 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
1754 tmp = (uint16_t*) huart->pTxBuffPtr;
1755 huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
1756 if(huart->Init.Parity == UART_PARITY_NONE)
1758 huart->pTxBuffPtr += 2;
1762 huart->pTxBuffPtr += 1;
1767 huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF);
1770 if(--huart->TxXferCount == 0)
1772 /* Disable the UART Transmit Complete Interrupt */
1773 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
1775 /* Enable the UART Transmit Complete Interrupt */
1776 __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
1788 * @brief Wraps up transmission in non blocking mode.
1789 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1790 * the configuration information for the specified UART module.
1791 * @retval HAL status
1793 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
1795 /* Disable the UART Transmit Complete Interrupt */
1796 __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
1798 /* Check if a receive process is ongoing or not */
1799 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
1801 huart->State = HAL_UART_STATE_BUSY_RX;
1805 /* Disable the UART Parity Error Interrupt */
1806 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
1808 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1809 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
1811 huart->State = HAL_UART_STATE_READY;
1814 HAL_UART_TxCpltCallback(huart);
1820 * @brief Receives an amount of data in non blocking mode
1821 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1822 * the configuration information for the specified UART module.
1823 * @retval HAL status
1825 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
1830 tmp1 = huart->State;
1831 if((tmp1 == HAL_UART_STATE_BUSY_RX) || (tmp1 == HAL_UART_STATE_BUSY_TX_RX))
1833 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
1835 tmp = (uint16_t*) huart->pRxBuffPtr;
1836 if(huart->Init.Parity == UART_PARITY_NONE)
1838 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
1839 huart->pRxBuffPtr += 2;
1843 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
1844 huart->pRxBuffPtr += 1;
1849 if(huart->Init.Parity == UART_PARITY_NONE)
1851 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
1855 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
1859 if(--huart->RxXferCount == 0)
1861 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
1863 /* Check if a transmit process is ongoing or not */
1864 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
1866 huart->State = HAL_UART_STATE_BUSY_TX;
1870 /* Disable the UART Parity Error Interrupt */
1871 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
1873 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1874 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
1876 huart->State = HAL_UART_STATE_READY;
1878 HAL_UART_RxCpltCallback(huart);
1891 * @brief Configures the UART peripheral.
1892 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
1893 * the configuration information for the specified UART module.
1896 static void UART_SetConfig(UART_HandleTypeDef *huart)
1898 uint32_t tmpreg = 0x00;
1900 /* Check the parameters */
1901 assert_param(IS_UART_INSTANCE(huart->Instance));
1902 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
1903 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
1904 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
1905 assert_param(IS_UART_PARITY(huart->Init.Parity));
1906 assert_param(IS_UART_MODE(huart->Init.Mode));
1907 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
1909 /* The hardware flow control is available only for USART1, USART2, USART3 and USART6 */
1910 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
1912 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
1915 /*-------------------------- USART CR2 Configuration -----------------------*/
1916 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
1917 * to huart->Init.StopBits value */
1918 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
1920 /*-------------------------- USART CR1 Configuration -----------------------*/
1921 /* Configure the UART Word Length, Parity and mode:
1922 Set the M bits according to huart->Init.WordLength value
1923 Set PCE and PS bits according to huart->Init.Parity value
1924 Set TE and RE bits according to huart->Init.Mode value
1925 Set OVER8 bit according to huart->Init.OverSampling value */
1926 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling;
1927 MODIFY_REG(huart->Instance->CR1,
1928 (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
1931 /*-------------------------- USART CR3 Configuration -----------------------*/
1932 /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
1933 MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE), huart->Init.HwFlowCtl);
1935 /* Check the Over Sampling */
1936 if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
1938 /*-------------------------- USART BRR Configuration ---------------------*/
1939 if((huart->Instance == USART1))
1941 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
1945 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
1950 /*-------------------------- USART BRR Configuration ---------------------*/
1951 if((huart->Instance == USART1))
1953 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
1957 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
1965 #endif /* HAL_UART_MODULE_ENABLED */
1974 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/