2 ******************************************************************************
3 * @file stm32f3xx_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
14 ===============================================================================
15 ##### How to use this driver #####
16 ===============================================================================
18 The UART HAL driver can be used as follows:
20 (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
21 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
22 (++) Enable the USARTx interface clock.
23 (++) UART pins configuration:
24 (+++) Enable the clock for the UART GPIOs.
25 (+++) Configure these UART pins as alternate function pull-up.
26 (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
27 and HAL_UART_Receive_IT() APIs):
28 (+++) Configure the USARTx interrupt priority.
29 (+++) Enable the NVIC USART IRQ handle.
30 (++) UART interrupts handling:
31 -@@- The specific UART interrupts (Transmission complete interrupt,
32 RXNE interrupt and Error Interrupts) are managed using the macros
33 __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit and receive processes.
34 (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
35 and HAL_UART_Receive_DMA() APIs):
36 (+++) Declare a DMA handle structure for the Tx/Rx channel.
37 (+++) Enable the DMAx interface clock.
38 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
39 (+++) Configure the DMA Tx/Rx channel.
40 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
41 (+++) Configure the priority and enable the NVIC for the transfer complete 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 handle Init structure.
46 (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
47 in the huart handle AdvancedInit structure.
49 (#) For the UART asynchronous mode, initialize the UART registers by calling
50 the HAL_UART_Init() API.
52 (#) For the UART Half duplex mode, initialize the UART registers by calling
53 the HAL_HalfDuplex_Init() API.
55 (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers
56 by calling the HAL_LIN_Init() API.
58 (#) For the UART Multiprocessor mode, initialize the UART registers
59 by calling the HAL_MultiProcessor_Init() API.
61 (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
62 by calling the HAL_RS485Ex_Init() API.
65 (@) These APIs (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_MultiProcessor_Init(),
66 also configure the low level Hardware (GPIO, CLOCK, CORTEX...etc) by
67 calling the customized HAL_UART_MspInit() API.
70 [..] Three operation modes are available within this driver :
72 *** Polling mode IO operation ***
73 =================================
75 (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
76 (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
78 *** Interrupt mode IO operation ***
79 ===================================
81 (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
82 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
83 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
84 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
85 add his own code by customization of function pointer HAL_UART_TxCpltCallback
86 (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
87 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
88 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
89 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
90 add his own code by customization of function pointer HAL_UART_RxCpltCallback
91 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
92 add his own code by customization of function pointer HAL_UART_ErrorCallback
94 *** DMA mode IO operation ***
95 ==============================
97 (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
98 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
99 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
100 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
101 add his own code by customization of function pointer HAL_UART_TxCpltCallback
102 (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
103 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
104 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
105 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
106 add his own code by customization of function pointer HAL_UART_RxCpltCallback
107 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
108 add his own code by customization of function pointer HAL_UART_ErrorCallback
109 (+) Pause the DMA Transfer using HAL_UART_DMAPause()
110 (+) Resume the DMA Transfer using HAL_UART_DMAResume()
111 (+) Stop the DMA Transfer using HAL_UART_DMAStop()
113 *** UART HAL driver macros list ***
114 =============================================
116 Below the list of most used macros in UART HAL driver.
118 (+) __HAL_UART_ENABLE: Enable the UART peripheral
119 (+) __HAL_UART_DISABLE: Disable the UART peripheral
120 (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
121 (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
122 (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
123 (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
126 (@) You can refer to the UART HAL driver header file for more useful macros
129 ******************************************************************************
132 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
134 * Redistribution and use in source and binary forms, with or without modification,
135 * are permitted provided that the following conditions are met:
136 * 1. Redistributions of source code must retain the above copyright notice,
137 * this list of conditions and the following disclaimer.
138 * 2. Redistributions in binary form must reproduce the above copyright notice,
139 * this list of conditions and the following disclaimer in the documentation
140 * and/or other materials provided with the distribution.
141 * 3. Neither the name of STMicroelectronics nor the names of its contributors
142 * may be used to endorse or promote products derived from this software
143 * without specific prior written permission.
145 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
146 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
147 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
148 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
149 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
150 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
151 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
152 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
153 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
154 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
156 ******************************************************************************
159 /* Includes ------------------------------------------------------------------*/
160 #include "stm32f3xx_hal.h"
162 /** @addtogroup STM32F3xx_HAL_Driver
166 /** @defgroup UART UART
167 * @brief UART HAL module driver
171 #ifdef HAL_UART_MODULE_ENABLED
173 /* Private typedef -----------------------------------------------------------*/
174 /* Private define ------------------------------------------------------------*/
175 /** @defgroup UART_Private_Constants UART Private Constants
178 #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
179 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
184 /* Private macros ------------------------------------------------------------*/
185 /* Private variables ---------------------------------------------------------*/
186 /* Private function prototypes -----------------------------------------------*/
187 /** @addtogroup UART_Private_Functions
190 static void UART_EndTxTransfer(UART_HandleTypeDef
*huart
);
191 static void UART_EndRxTransfer(UART_HandleTypeDef
*huart
);
192 static void UART_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
193 static void UART_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
);
194 static void UART_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
195 static void UART_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
);
196 static void UART_DMAError(DMA_HandleTypeDef
*hdma
);
197 static void UART_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
198 static void UART_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
);
199 static void UART_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
);
200 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
201 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
202 HAL_StatusTypeDef
UART_Transmit_IT(UART_HandleTypeDef
*huart
);
203 HAL_StatusTypeDef
UART_EndTransmit_IT(UART_HandleTypeDef
*huart
);
204 HAL_StatusTypeDef
UART_Receive_IT(UART_HandleTypeDef
*huart
);
209 /* Exported functions --------------------------------------------------------*/
211 /** @defgroup UART_Exported_Functions UART Exported Functions
215 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
216 * @brief Initialization and Configuration functions
219 ===============================================================================
220 ##### Initialization and Configuration functions #####
221 ===============================================================================
223 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
224 in asynchronous mode.
225 (+) For the asynchronous mode the parameters below can be configured:
230 (++) Hardware flow control
231 (++) Receiver/transmitter modes
232 (++) Over Sampling Method
233 (++) One-Bit Sampling Method
234 (+) For the asynchronous mode, the following advanced features can be configured as well:
235 (++) TX and/or RX pin level inversion
236 (++) data logical level inversion
237 (++) RX and TX pins swap
238 (++) RX overrun detection disabling
239 (++) DMA disabling on RX error
240 (++) MSB first on communication line
241 (++) auto Baud rate detection
243 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init()
244 API follow respectively the UART asynchronous, UART Half duplex, UART LIN mode and
245 multiprocessor configuration procedures (details for the procedures are available in reference manual).
252 Additional Table: If the parity is enabled, then the MSB bit of the data written
253 in the data register is transmitted but is changed by the parity bit.
254 According to device capability (support or not of 7-bit word length),
255 frame length is either defined by the M bit (8-bits or 9-bits)
256 or by the M1 and M0 bits (7-bit, 8-bit or 9-bit).
257 Possible UART frame formats are as listed in the following table:
259 Table 1. UART frame format.
260 +-----------------------------------------------------------------------+
261 | M bit | PCE bit | UART frame |
262 |-------------------|-----------|---------------------------------------|
263 | 0 | 0 | | SB | 8-bit data | STB | |
264 |-------------------|-----------|---------------------------------------|
265 | 0 | 1 | | SB | 7-bit data | PB | STB | |
266 |-------------------|-----------|---------------------------------------|
267 | 1 | 0 | | SB | 9-bit data | STB | |
268 |-------------------|-----------|---------------------------------------|
269 | 1 | 1 | | SB | 8-bit data | PB | STB | |
270 +-----------------------------------------------------------------------+
271 | M1 bit | M0 bit | PCE bit | UART frame |
272 |---------|---------|-----------|---------------------------------------|
273 | 0 | 0 | 0 | | SB | 8 bit data | STB | |
274 |---------|---------|-----------|---------------------------------------|
275 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
276 |---------|---------|-----------|---------------------------------------|
277 | 0 | 1 | 0 | | SB | 9 bit data | STB | |
278 |---------|---------|-----------|---------------------------------------|
279 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
280 |---------|---------|-----------|---------------------------------------|
281 | 1 | 0 | 0 | | SB | 7 bit data | STB | |
282 |---------|---------|-----------|---------------------------------------|
283 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
284 +-----------------------------------------------------------------------+
289 * @brief Initialize the UART mode according to the specified
290 * parameters in the UART_InitTypeDef and initialize the associated handle.
291 * @param huart UART handle.
294 HAL_StatusTypeDef
HAL_UART_Init(UART_HandleTypeDef
*huart
)
296 /* Check the UART handle allocation */
302 if(huart
->Init
.HwFlowCtl
!= UART_HWCONTROL_NONE
)
304 /* Check the parameters */
305 assert_param(IS_UART_HWFLOW_INSTANCE(huart
->Instance
));
309 /* Check the parameters */
310 assert_param(IS_UART_INSTANCE(huart
->Instance
));
313 if(huart
->gState
== HAL_UART_STATE_RESET
)
315 /* Allocate lock resource and initialize it */
316 huart
->Lock
= HAL_UNLOCKED
;
318 /* Init the low level hardware : GPIO, CLOCK */
319 HAL_UART_MspInit(huart
);
322 huart
->gState
= HAL_UART_STATE_BUSY
;
324 /* Disable the Peripheral */
325 __HAL_UART_DISABLE(huart
);
327 /* Set the UART Communication parameters */
328 if (UART_SetConfig(huart
) == HAL_ERROR
)
333 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
335 UART_AdvFeatureConfig(huart
);
338 /* In asynchronous mode, the following bits must be kept cleared:
339 - LINEN and CLKEN bits in the USART_CR2 register,
340 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
341 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
342 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_SCEN
| USART_CR3_HDSEL
| USART_CR3_IREN
));
344 /* Enable the Peripheral */
345 __HAL_UART_ENABLE(huart
);
347 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
348 return (UART_CheckIdleState(huart
));
352 * @brief Initialize the half-duplex mode according to the specified
353 * parameters in the UART_InitTypeDef and creates the associated handle.
354 * @param huart UART handle.
357 HAL_StatusTypeDef
HAL_HalfDuplex_Init(UART_HandleTypeDef
*huart
)
359 /* Check the UART handle allocation */
365 /* Check UART instance */
366 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart
->Instance
));
368 if(huart
->gState
== HAL_UART_STATE_RESET
)
370 /* Allocate lock resource and initialize it */
371 huart
->Lock
= HAL_UNLOCKED
;
373 /* Init the low level hardware : GPIO, CLOCK */
374 HAL_UART_MspInit(huart
);
377 huart
->gState
= HAL_UART_STATE_BUSY
;
379 /* Disable the Peripheral */
380 __HAL_UART_DISABLE(huart
);
382 /* Set the UART Communication parameters */
383 if (UART_SetConfig(huart
) == HAL_ERROR
)
388 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
390 UART_AdvFeatureConfig(huart
);
393 /* In half-duplex mode, the following bits must be kept cleared:
394 - LINEN and CLKEN bits in the USART_CR2 register,
395 - SCEN and IREN bits in the USART_CR3 register.*/
396 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
397 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_IREN
| USART_CR3_SCEN
));
399 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
400 SET_BIT(huart
->Instance
->CR3
, USART_CR3_HDSEL
);
402 /* Enable the Peripheral */
403 __HAL_UART_ENABLE(huart
);
405 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
406 return (UART_CheckIdleState(huart
));
411 * @brief Initialize the LIN mode according to the specified
412 * parameters in the UART_InitTypeDef and creates the associated handle .
413 * @param huart UART handle.
414 * @param BreakDetectLength specifies the LIN break detection length.
415 * This parameter can be one of the following values:
416 * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
417 * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
420 HAL_StatusTypeDef
HAL_LIN_Init(UART_HandleTypeDef
*huart
, uint32_t BreakDetectLength
)
422 /* Check the UART handle allocation */
428 /* Check the LIN UART instance */
429 assert_param(IS_UART_LIN_INSTANCE(huart
->Instance
));
430 /* Check the Break detection length parameter */
431 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength
));
433 /* LIN mode limited to 16-bit oversampling only */
434 if(huart
->Init
.OverSampling
== UART_OVERSAMPLING_8
)
438 /* LIN mode limited to 8-bit data length */
439 if(huart
->Init
.WordLength
!= UART_WORDLENGTH_8B
)
444 if(huart
->gState
== HAL_UART_STATE_RESET
)
446 /* Allocate lock resource and initialize it */
447 huart
->Lock
= HAL_UNLOCKED
;
449 /* Init the low level hardware : GPIO, CLOCK */
450 HAL_UART_MspInit(huart
);
453 huart
->gState
= HAL_UART_STATE_BUSY
;
455 /* Disable the Peripheral */
456 __HAL_UART_DISABLE(huart
);
458 /* Set the UART Communication parameters */
459 if (UART_SetConfig(huart
) == HAL_ERROR
)
464 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
466 UART_AdvFeatureConfig(huart
);
469 /* In LIN mode, the following bits must be kept cleared:
470 - LINEN and CLKEN bits in the USART_CR2 register,
471 - SCEN and IREN bits in the USART_CR3 register.*/
472 CLEAR_BIT(huart
->Instance
->CR2
, USART_CR2_CLKEN
);
473 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_HDSEL
| USART_CR3_IREN
| USART_CR3_SCEN
));
475 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
476 SET_BIT(huart
->Instance
->CR2
, USART_CR2_LINEN
);
478 /* Set the USART LIN Break detection length. */
479 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_LBDL
, BreakDetectLength
);
481 /* Enable the Peripheral */
482 __HAL_UART_ENABLE(huart
);
484 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
485 return (UART_CheckIdleState(huart
));
490 * @brief Initialize the multiprocessor mode according to the specified
491 * parameters in the UART_InitTypeDef and initialize the associated handle.
492 * @param huart UART handle.
493 * @param Address UART node address (4-, 6-, 7- or 8-bit long).
494 * @param WakeUpMethod specifies the UART wakeup method.
495 * This parameter can be one of the following values:
496 * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
497 * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
498 * @note If the user resorts to idle line detection wake up, the Address parameter
499 * is useless and ignored by the initialization function.
500 * @note If the user resorts to address mark wake up, the address length detection
501 * is configured by default to 4 bits only. For the UART to be able to
502 * manage 6-, 7- or 8-bit long addresses detection, the API
503 * HAL_MultiProcessorEx_AddressLength_Set() must be called after
504 * HAL_MultiProcessor_Init().
507 HAL_StatusTypeDef
HAL_MultiProcessor_Init(UART_HandleTypeDef
*huart
, uint8_t Address
, uint32_t WakeUpMethod
)
509 /* Check the UART handle allocation */
515 /* Check the wake up method parameter */
516 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod
));
518 if(huart
->gState
== HAL_UART_STATE_RESET
)
520 /* Allocate lock resource and initialize it */
521 huart
->Lock
= HAL_UNLOCKED
;
523 /* Init the low level hardware : GPIO, CLOCK */
524 HAL_UART_MspInit(huart
);
527 huart
->gState
= HAL_UART_STATE_BUSY
;
529 /* Disable the Peripheral */
530 __HAL_UART_DISABLE(huart
);
532 /* Set the UART Communication parameters */
533 if (UART_SetConfig(huart
) == HAL_ERROR
)
538 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
540 UART_AdvFeatureConfig(huart
);
543 /* In multiprocessor mode, the following bits must be kept cleared:
544 - LINEN and CLKEN bits in the USART_CR2 register,
545 - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
546 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
547 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_SCEN
| USART_CR3_HDSEL
| USART_CR3_IREN
));
549 if (WakeUpMethod
== UART_WAKEUPMETHOD_ADDRESSMARK
)
551 /* If address mark wake up method is chosen, set the USART address node */
552 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ADD
, ((uint32_t)Address
<< UART_CR2_ADDRESS_LSB_POS
));
555 /* Set the wake up method by setting the WAKE bit in the CR1 register */
556 MODIFY_REG(huart
->Instance
->CR1
, USART_CR1_WAKE
, WakeUpMethod
);
558 /* Enable the Peripheral */
559 __HAL_UART_ENABLE(huart
);
561 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
562 return (UART_CheckIdleState(huart
));
567 * @brief DeInitialize the UART peripheral.
568 * @param huart UART handle.
571 HAL_StatusTypeDef
HAL_UART_DeInit(UART_HandleTypeDef
*huart
)
573 /* Check the UART handle allocation */
579 /* Check the parameters */
580 assert_param(IS_UART_INSTANCE(huart
->Instance
));
582 huart
->gState
= HAL_UART_STATE_BUSY
;
584 /* Disable the Peripheral */
585 __HAL_UART_DISABLE(huart
);
587 huart
->Instance
->CR1
= 0x0U
;
588 huart
->Instance
->CR2
= 0x0U
;
589 huart
->Instance
->CR3
= 0x0U
;
591 /* DeInit the low level hardware */
592 HAL_UART_MspDeInit(huart
);
594 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
595 huart
->gState
= HAL_UART_STATE_RESET
;
596 huart
->RxState
= HAL_UART_STATE_RESET
;
605 * @brief Initialize the UART MSP.
606 * @param huart UART handle.
609 __weak
void HAL_UART_MspInit(UART_HandleTypeDef
*huart
)
611 /* Prevent unused argument(s) compilation warning */
614 /* NOTE : This function should not be modified, when the callback is needed,
615 the HAL_UART_MspInit can be implemented in the user file
620 * @brief DeInitialize the UART MSP.
621 * @param huart UART handle.
624 __weak
void HAL_UART_MspDeInit(UART_HandleTypeDef
*huart
)
626 /* Prevent unused argument(s) compilation warning */
629 /* NOTE : This function should not be modified, when the callback is needed,
630 the HAL_UART_MspDeInit can be implemented in the user file
638 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
639 * @brief UART Transmit/Receive functions
642 ===============================================================================
643 ##### IO operation functions #####
644 ===============================================================================
645 This subsection provides a set of functions allowing to manage the UART asynchronous
646 and Half duplex data transfers.
648 (#) There are two mode of transfer:
649 (++) Blocking mode: The communication is performed in polling mode.
650 The HAL status of all data processing is returned by the same function
651 after finishing transfer.
652 (++) Non-Blocking mode: The communication is performed using Interrupts
653 or DMA, These API's return the HAL status.
654 The end of the data processing will be indicated through the
655 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
657 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
658 will be executed respectively at the end of the transmit or Receive process
659 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
661 (#) Blocking mode API's are :
662 (++) HAL_UART_Transmit()
663 (++) HAL_UART_Receive()
665 (#) Non-Blocking mode API's with Interrupt are :
666 (++) HAL_UART_Transmit_IT()
667 (++) HAL_UART_Receive_IT()
668 (++) HAL_UART_IRQHandler()
670 (#) Non-Blocking mode API's with DMA are :
671 (++) HAL_UART_Transmit_DMA()
672 (++) HAL_UART_Receive_DMA()
673 (++) HAL_UART_DMAPause()
674 (++) HAL_UART_DMAResume()
675 (++) HAL_UART_DMAStop()
677 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
678 (++) HAL_UART_TxHalfCpltCallback()
679 (++) HAL_UART_TxCpltCallback()
680 (++) HAL_UART_RxHalfCpltCallback()
681 (++) HAL_UART_RxCpltCallback()
682 (++) HAL_UART_ErrorCallback()
684 (#) Non-Blocking mode transfers could be aborted using Abort API's :
685 (++) HAL_UART_Abort()
686 (++) HAL_UART_AbortTransmit()
687 (++) HAL_UART_AbortReceive()
688 (++) HAL_UART_Abort_IT()
689 (++) HAL_UART_AbortTransmit_IT()
690 (++) HAL_UART_AbortReceive_IT()
692 (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
693 (++) HAL_UART_AbortCpltCallback()
694 (++) HAL_UART_AbortTransmitCpltCallback()
695 (++) HAL_UART_AbortReceiveCpltCallback()
697 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
698 Errors are handled as follows :
699 (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
700 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
701 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
702 and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side.
703 If user wants to abort it, Abort services should be called by user.
704 (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
705 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
706 Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
708 -@- In the Half duplex communication, it is forbidden to run the transmit
709 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
716 * @brief Send an amount of data in blocking mode.
717 * @param huart UART handle.
718 * @param pData Pointer to data buffer.
719 * @param Size Amount of data to be sent.
720 * @param Timeout Timeout duration.
723 HAL_StatusTypeDef
HAL_UART_Transmit(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
726 uint32_t tickstart
= 0U;
728 /* Check that a Tx process is not already ongoing */
729 if(huart
->gState
== HAL_UART_STATE_READY
)
731 if((pData
== NULL
) || (Size
== 0U))
739 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
740 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
742 /* Init tickstart for timeout managment*/
743 tickstart
= HAL_GetTick();
745 huart
->TxXferSize
= Size
;
746 huart
->TxXferCount
= Size
;
747 while(huart
->TxXferCount
> 0U)
749 huart
->TxXferCount
--;
750 if(UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
754 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
756 tmp
= (uint16_t*) pData
;
757 huart
->Instance
->TDR
= (*tmp
& (uint16_t)0x01FFU
);
762 huart
->Instance
->TDR
= (*pData
++ & (uint8_t)0xFFU
);
765 if(UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_TC
, RESET
, tickstart
, Timeout
) != HAL_OK
)
770 /* At end of Tx process, restore huart->gState to Ready */
771 huart
->gState
= HAL_UART_STATE_READY
;
773 /* Process Unlocked */
785 * @brief Receive an amount of data in blocking mode.
786 * @param huart UART handle.
787 * @param pData pointer to data buffer.
788 * @param Size amount of data to be received.
789 * @param Timeout Timeout duration.
792 HAL_StatusTypeDef
HAL_UART_Receive(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
796 uint32_t tickstart
= 0U;
798 /* Check that a Rx process is not already ongoing */
799 if(huart
->RxState
== HAL_UART_STATE_READY
)
801 if((pData
== NULL
) || (Size
== 0U))
809 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
810 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
812 /* Init tickstart for timeout managment*/
813 tickstart
= HAL_GetTick();
815 huart
->RxXferSize
= Size
;
816 huart
->RxXferCount
= Size
;
818 /* Computation of UART mask to apply to RDR register */
819 UART_MASK_COMPUTATION(huart
);
820 uhMask
= huart
->Mask
;
822 /* as long as data have to be received */
823 while(huart
->RxXferCount
> 0U)
825 huart
->RxXferCount
--;
826 if(UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
830 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
832 tmp
= (uint16_t*) pData
;
833 *tmp
= (uint16_t)(huart
->Instance
->RDR
& uhMask
);
838 *pData
++ = (uint8_t)(huart
->Instance
->RDR
& (uint8_t)uhMask
);
842 /* At end of Rx process, restore huart->RxState to Ready */
843 huart
->RxState
= HAL_UART_STATE_READY
;
845 /* Process Unlocked */
857 * @brief Send an amount of data in interrupt mode.
858 * @param huart UART handle.
859 * @param pData pointer to data buffer.
860 * @param Size amount of data to be sent.
863 HAL_StatusTypeDef
HAL_UART_Transmit_IT(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
865 /* Check that a Tx process is not already ongoing */
866 if(huart
->gState
== HAL_UART_STATE_READY
)
868 if((pData
== NULL
) || (Size
== 0U))
876 huart
->pTxBuffPtr
= pData
;
877 huart
->TxXferSize
= Size
;
878 huart
->TxXferCount
= Size
;
880 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
881 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
883 /* Process Unlocked */
886 /* Enable the UART Transmit Data Register Empty Interrupt */
887 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TXEIE
);
898 * @brief Receive an amount of data in interrupt mode.
899 * @param huart UART handle.
900 * @param pData pointer to data buffer.
901 * @param Size amount of data to be received.
904 HAL_StatusTypeDef
HAL_UART_Receive_IT(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
906 /* Check that a Rx process is not already ongoing */
907 if(huart
->RxState
== HAL_UART_STATE_READY
)
909 if((pData
== NULL
) || (Size
== 0U))
917 huart
->pRxBuffPtr
= pData
;
918 huart
->RxXferSize
= Size
;
919 huart
->RxXferCount
= Size
;
921 /* Computation of UART mask to apply to RDR register */
922 UART_MASK_COMPUTATION(huart
);
924 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
925 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
927 /* Process Unlocked */
930 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
931 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
933 /* Enable the UART Parity Error and Data Register not empty Interrupts */
934 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE
);
945 * @brief Send an amount of data in DMA mode.
946 * @param huart UART handle.
947 * @param pData pointer to data buffer.
948 * @param Size amount of data to be sent.
949 * @note This function starts a DMA transfer in interrupt mode meaning that
950 * DMA half transfer complete, DMA transfer complete and DMA transfer
951 * error interrupts are enabled
954 HAL_StatusTypeDef
HAL_UART_Transmit_DMA(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
956 /* Check if UART instance supports continuous communication using DMA */
957 assert_param(IS_UART_DMA_INSTANCE(huart
->Instance
));
959 /* Check that a Tx process is not already ongoing */
960 if(huart
->gState
== HAL_UART_STATE_READY
)
962 if((pData
== NULL
) || (Size
== 0U))
970 huart
->pTxBuffPtr
= pData
;
971 huart
->TxXferSize
= Size
;
972 huart
->TxXferCount
= Size
;
974 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
975 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
977 /* Set the UART DMA transfer complete callback */
978 huart
->hdmatx
->XferCpltCallback
= UART_DMATransmitCplt
;
980 /* Set the UART DMA Half transfer complete callback */
981 huart
->hdmatx
->XferHalfCpltCallback
= UART_DMATxHalfCplt
;
983 /* Set the DMA error callback */
984 huart
->hdmatx
->XferErrorCallback
= UART_DMAError
;
986 /* Set the DMA abort callback */
987 huart
->hdmatx
->XferAbortCallback
= NULL
;
989 /* Enable the UART transmit DMA channel */
990 HAL_DMA_Start_IT(huart
->hdmatx
, (uint32_t)huart
->pTxBuffPtr
, (uint32_t)&huart
->Instance
->TDR
, Size
);
992 /* Clear the TC flag in the ICR register */
993 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_TCF
);
995 /* Process Unlocked */
998 /* Enable the DMA transfer for transmit request by setting the DMAT bit
999 in the UART CR3 register */
1000 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1011 * @brief Receive an amount of data in DMA mode.
1012 * @param huart UART handle.
1013 * @param pData pointer to data buffer.
1014 * @param Size amount of data to be received.
1015 * @note When the UART parity is enabled (PCE = 1), the received data contain
1016 * the parity bit (MSB position).
1017 * @note This function starts a DMA transfer in interrupt mode meaning that
1018 * DMA half transfer complete, DMA transfer complete and DMA transfer
1019 * error interrupts are enabled
1020 * @retval HAL status
1022 HAL_StatusTypeDef
HAL_UART_Receive_DMA(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
1024 /* Check if UART instance supports continuous communication using DMA */
1025 assert_param(IS_UART_DMA_INSTANCE(huart
->Instance
));
1027 /* Check that a Rx process is not already ongoing */
1028 if(huart
->RxState
== HAL_UART_STATE_READY
)
1030 if((pData
== NULL
) || (Size
== 0U))
1035 /* Process Locked */
1038 huart
->pRxBuffPtr
= pData
;
1039 huart
->RxXferSize
= Size
;
1041 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1042 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
1044 /* Set the UART DMA transfer complete callback */
1045 huart
->hdmarx
->XferCpltCallback
= UART_DMAReceiveCplt
;
1047 /* Set the UART DMA Half transfer complete callback */
1048 huart
->hdmarx
->XferHalfCpltCallback
= UART_DMARxHalfCplt
;
1050 /* Set the DMA error callback */
1051 huart
->hdmarx
->XferErrorCallback
= UART_DMAError
;
1053 /* Set the DMA abort callback */
1054 huart
->hdmarx
->XferAbortCallback
= NULL
;
1056 /* Enable the DMA channel */
1057 HAL_DMA_Start_IT(huart
->hdmarx
, (uint32_t)&huart
->Instance
->RDR
, (uint32_t)huart
->pRxBuffPtr
, Size
);
1059 /* Process Unlocked */
1060 __HAL_UNLOCK(huart
);
1062 /* Enable the UART Parity Error Interrupt */
1063 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1065 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1066 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1068 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1069 in the UART CR3 register */
1070 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1081 * @brief Pause the DMA Transfer.
1082 * @param huart UART handle.
1083 * @retval HAL status
1085 HAL_StatusTypeDef
HAL_UART_DMAPause(UART_HandleTypeDef
*huart
)
1087 /* Process Locked */
1090 if ((huart
->gState
== HAL_UART_STATE_BUSY_TX
) &&
1091 (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)))
1093 /* Disable the UART DMA Tx request */
1094 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1096 if ((huart
->RxState
== HAL_UART_STATE_BUSY_RX
) &&
1097 (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)))
1099 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1100 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1101 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1103 /* Disable the UART DMA Rx request */
1104 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1107 /* Process Unlocked */
1108 __HAL_UNLOCK(huart
);
1114 * @brief Resume the DMA Transfer.
1115 * @param huart UART handle.
1116 * @retval HAL status
1118 HAL_StatusTypeDef
HAL_UART_DMAResume(UART_HandleTypeDef
*huart
)
1120 /* Process Locked */
1123 if(huart
->gState
== HAL_UART_STATE_BUSY_TX
)
1125 /* Enable the UART DMA Tx request */
1126 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1128 if(huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
1130 /* Clear the Overrun flag before resuming the Rx transfer */
1131 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
);
1133 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1134 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1135 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1137 /* Enable the UART DMA Rx request */
1138 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1141 /* Process Unlocked */
1142 __HAL_UNLOCK(huart
);
1148 * @brief Stop the DMA Transfer.
1149 * @param huart UART handle.
1150 * @retval HAL status
1152 HAL_StatusTypeDef
HAL_UART_DMAStop(UART_HandleTypeDef
*huart
)
1154 /* The Lock is not implemented on this API to allow the user application
1155 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
1156 HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
1157 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1158 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1159 the stream and the corresponding call back is executed. */
1161 /* Stop UART DMA Tx request if ongoing */
1162 if ((huart
->gState
== HAL_UART_STATE_BUSY_TX
) &&
1163 (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)))
1165 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1167 /* Abort the UART DMA Tx channel */
1168 if(huart
->hdmatx
!= NULL
)
1170 HAL_DMA_Abort(huart
->hdmatx
);
1173 UART_EndTxTransfer(huart
);
1176 /* Stop UART DMA Rx request if ongoing */
1177 if ((huart
->RxState
== HAL_UART_STATE_BUSY_RX
) &&
1178 (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)))
1180 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1182 /* Abort the UART DMA Rx channel */
1183 if(huart
->hdmarx
!= NULL
)
1185 HAL_DMA_Abort(huart
->hdmarx
);
1188 UART_EndRxTransfer(huart
);
1195 * @brief Abort ongoing transfers (blocking mode).
1196 * @param huart UART handle.
1197 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1198 * This procedure performs following operations :
1199 * - Disable UART Interrupts (Tx and Rx)
1200 * - Disable the DMA transfer in the peripheral register (if enabled)
1201 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1202 * - Set handle State to READY
1203 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1204 * @retval HAL status
1206 HAL_StatusTypeDef
HAL_UART_Abort(UART_HandleTypeDef
*huart
)
1208 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1209 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
| USART_CR1_TCIE
));
1210 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1212 /* Disable the UART DMA Tx request if enabled */
1213 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1215 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1217 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1218 if(huart
->hdmatx
!= NULL
)
1220 /* Set the UART DMA Abort callback to Null.
1221 No call back execution at end of DMA abort procedure */
1222 huart
->hdmatx
->XferAbortCallback
= NULL
;
1224 HAL_DMA_Abort(huart
->hdmatx
);
1228 /* Disable the UART DMA Rx request if enabled */
1229 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1231 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1233 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1234 if(huart
->hdmarx
!= NULL
)
1236 /* Set the UART DMA Abort callback to Null.
1237 No call back execution at end of DMA abort procedure */
1238 huart
->hdmarx
->XferAbortCallback
= NULL
;
1240 HAL_DMA_Abort(huart
->hdmarx
);
1244 /* Reset Tx and Rx transfer counters */
1245 huart
->TxXferCount
= 0U;
1246 huart
->RxXferCount
= 0U;
1248 /* Clear the Error flags in the ICR register */
1249 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1251 /* Restore huart->gState and huart->RxState to Ready */
1252 huart
->gState
= HAL_UART_STATE_READY
;
1253 huart
->RxState
= HAL_UART_STATE_READY
;
1255 /* Reset Handle ErrorCode to No Error */
1256 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1262 * @brief Abort ongoing Transmit transfer (blocking mode).
1263 * @param huart UART handle.
1264 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1265 * This procedure performs following operations :
1266 * - Disable UART Interrupts (Tx)
1267 * - Disable the DMA transfer in the peripheral register (if enabled)
1268 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1269 * - Set handle State to READY
1270 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1271 * @retval HAL status
1273 HAL_StatusTypeDef
HAL_UART_AbortTransmit(UART_HandleTypeDef
*huart
)
1275 /* Disable TXEIE and TCIE interrupts */
1276 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1278 /* Disable the UART DMA Tx request if enabled */
1279 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1281 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1283 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1284 if(huart
->hdmatx
!= NULL
)
1286 /* Set the UART DMA Abort callback to Null.
1287 No call back execution at end of DMA abort procedure */
1288 huart
->hdmatx
->XferAbortCallback
= NULL
;
1290 HAL_DMA_Abort(huart
->hdmatx
);
1294 /* Reset Tx transfer counter */
1295 huart
->TxXferCount
= 0U;
1297 /* Restore huart->gState to Ready */
1298 huart
->gState
= HAL_UART_STATE_READY
;
1304 * @brief Abort ongoing Receive transfer (blocking mode).
1305 * @param huart UART handle.
1306 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1307 * This procedure performs following operations :
1308 * - Disable UART Interrupts (Rx)
1309 * - Disable the DMA transfer in the peripheral register (if enabled)
1310 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1311 * - Set handle State to READY
1312 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1313 * @retval HAL status
1315 HAL_StatusTypeDef
HAL_UART_AbortReceive(UART_HandleTypeDef
*huart
)
1317 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1318 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1319 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1321 /* Disable the UART DMA Rx request if enabled */
1322 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1324 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1326 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1327 if(huart
->hdmarx
!= NULL
)
1329 /* Set the UART DMA Abort callback to Null.
1330 No call back execution at end of DMA abort procedure */
1331 huart
->hdmarx
->XferAbortCallback
= NULL
;
1333 HAL_DMA_Abort(huart
->hdmarx
);
1337 /* Reset Rx transfer counter */
1338 huart
->RxXferCount
= 0U;
1340 /* Clear the Error flags in the ICR register */
1341 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1343 /* Restore huart->RxState to Ready */
1344 huart
->RxState
= HAL_UART_STATE_READY
;
1350 * @brief Abort ongoing transfers (Interrupt mode).
1351 * @param huart UART handle.
1352 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1353 * This procedure performs following operations :
1354 * - Disable UART Interrupts (Tx and Rx)
1355 * - Disable the DMA transfer in the peripheral register (if enabled)
1356 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1357 * - Set handle State to READY
1358 * - At abort completion, call user abort complete callback
1359 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1360 * considered as completed only when user abort complete callback is executed (not when exiting function).
1361 * @retval HAL status
1363 HAL_StatusTypeDef
HAL_UART_Abort_IT(UART_HandleTypeDef
*huart
)
1365 uint32_t abortcplt
= 1U;
1367 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1368 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
| USART_CR1_TCIE
));
1369 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1371 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
1372 before any call to DMA Abort functions */
1373 /* DMA Tx Handle is valid */
1374 if(huart
->hdmatx
!= NULL
)
1376 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
1377 Otherwise, set it to NULL */
1378 if(HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1380 huart
->hdmatx
->XferAbortCallback
= UART_DMATxAbortCallback
;
1384 huart
->hdmatx
->XferAbortCallback
= NULL
;
1387 /* DMA Rx Handle is valid */
1388 if(huart
->hdmarx
!= NULL
)
1390 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
1391 Otherwise, set it to NULL */
1392 if(HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1394 huart
->hdmarx
->XferAbortCallback
= UART_DMARxAbortCallback
;
1398 huart
->hdmarx
->XferAbortCallback
= NULL
;
1402 /* Disable the UART DMA Tx request if enabled */
1403 if(HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1405 /* Disable DMA Tx at UART level */
1406 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1408 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1409 if(huart
->hdmatx
!= NULL
)
1411 /* UART Tx DMA Abort callback has already been initialised :
1412 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1415 if(HAL_DMA_Abort_IT(huart
->hdmatx
) != HAL_OK
)
1417 huart
->hdmatx
->XferAbortCallback
= NULL
;
1426 /* Disable the UART DMA Rx request if enabled */
1427 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1429 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1431 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
1432 if(huart
->hdmarx
!= NULL
)
1434 /* UART Rx DMA Abort callback has already been initialised :
1435 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1438 if(HAL_DMA_Abort_IT(huart
->hdmarx
) != HAL_OK
)
1440 huart
->hdmarx
->XferAbortCallback
= NULL
;
1450 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1451 if (abortcplt
== 1U)
1453 /* Reset Tx and Rx transfer counters */
1454 huart
->TxXferCount
= 0U;
1455 huart
->RxXferCount
= 0U;
1457 /* Reset errorCode */
1458 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1460 /* Clear the Error flags in the ICR register */
1461 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1463 /* Restore huart->gState and huart->RxState to Ready */
1464 huart
->gState
= HAL_UART_STATE_READY
;
1465 huart
->RxState
= HAL_UART_STATE_READY
;
1467 /* As no DMA to be aborted, call directly user Abort complete callback */
1468 HAL_UART_AbortCpltCallback(huart
);
1475 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1476 * @param huart UART handle.
1477 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1478 * This procedure performs following operations :
1479 * - Disable UART Interrupts (Tx)
1480 * - Disable the DMA transfer in the peripheral register (if enabled)
1481 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1482 * - Set handle State to READY
1483 * - At abort completion, call user abort complete callback
1484 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1485 * considered as completed only when user abort complete callback is executed (not when exiting function).
1486 * @retval HAL status
1488 HAL_StatusTypeDef
HAL_UART_AbortTransmit_IT(UART_HandleTypeDef
*huart
)
1490 /* Disable TXEIE and TCIE interrupts */
1491 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1493 /* Disable the UART DMA Tx request if enabled */
1494 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1496 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1498 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1499 if(huart
->hdmatx
!= NULL
)
1501 /* Set the UART DMA Abort callback :
1502 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1503 huart
->hdmatx
->XferAbortCallback
= UART_DMATxOnlyAbortCallback
;
1506 if(HAL_DMA_Abort_IT(huart
->hdmatx
) != HAL_OK
)
1508 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
1509 huart
->hdmatx
->XferAbortCallback(huart
->hdmatx
);
1514 /* Reset Tx transfer counter */
1515 huart
->TxXferCount
= 0U;
1517 /* Restore huart->gState to Ready */
1518 huart
->gState
= HAL_UART_STATE_READY
;
1520 /* As no DMA to be aborted, call directly user Abort complete callback */
1521 HAL_UART_AbortTransmitCpltCallback(huart
);
1526 /* Reset Tx transfer counter */
1527 huart
->TxXferCount
= 0U;
1529 /* Restore huart->gState to Ready */
1530 huart
->gState
= HAL_UART_STATE_READY
;
1532 /* As no DMA to be aborted, call directly user Abort complete callback */
1533 HAL_UART_AbortTransmitCpltCallback(huart
);
1540 * @brief Abort ongoing Receive transfer (Interrupt mode).
1541 * @param huart UART handle.
1542 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1543 * This procedure performs following operations :
1544 * - Disable UART Interrupts (Rx)
1545 * - Disable the DMA transfer in the peripheral register (if enabled)
1546 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1547 * - Set handle State to READY
1548 * - At abort completion, call user abort complete callback
1549 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1550 * considered as completed only when user abort complete callback is executed (not when exiting function).
1551 * @retval HAL status
1553 HAL_StatusTypeDef
HAL_UART_AbortReceive_IT(UART_HandleTypeDef
*huart
)
1555 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1556 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1557 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1559 /* Disable the UART DMA Rx request if enabled */
1560 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1562 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1564 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
1565 if(huart
->hdmarx
!= NULL
)
1567 /* Set the UART DMA Abort callback :
1568 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1569 huart
->hdmarx
->XferAbortCallback
= UART_DMARxOnlyAbortCallback
;
1572 if(HAL_DMA_Abort_IT(huart
->hdmarx
) != HAL_OK
)
1574 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
1575 huart
->hdmarx
->XferAbortCallback(huart
->hdmarx
);
1580 /* Reset Rx transfer counter */
1581 huart
->RxXferCount
= 0U;
1583 /* Clear the Error flags in the ICR register */
1584 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1586 /* Restore huart->RxState to Ready */
1587 huart
->RxState
= HAL_UART_STATE_READY
;
1589 /* As no DMA to be aborted, call directly user Abort complete callback */
1590 HAL_UART_AbortReceiveCpltCallback(huart
);
1595 /* Reset Rx transfer counter */
1596 huart
->RxXferCount
= 0U;
1598 /* Clear the Error flags in the ICR register */
1599 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1601 /* Restore huart->RxState to Ready */
1602 huart
->RxState
= HAL_UART_STATE_READY
;
1604 /* As no DMA to be aborted, call directly user Abort complete callback */
1605 HAL_UART_AbortReceiveCpltCallback(huart
);
1612 * @brief Handle UART interrupt request.
1613 * @param huart UART handle.
1616 void HAL_UART_IRQHandler(UART_HandleTypeDef
*huart
)
1618 uint32_t isrflags
= READ_REG(huart
->Instance
->ISR
);
1619 uint32_t cr1its
= READ_REG(huart
->Instance
->CR1
);
1621 uint32_t errorflags
;
1623 /* If no error occurs */
1624 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
));
1625 if (errorflags
== RESET
)
1627 /* UART in mode Receiver ---------------------------------------------------*/
1628 if(((isrflags
& USART_ISR_RXNE
) != RESET
) && ((cr1its
& USART_CR1_RXNEIE
) != RESET
))
1630 UART_Receive_IT(huart
);
1635 /* If some errors occur */
1636 cr3its
= READ_REG(huart
->Instance
->CR3
);
1637 if( (errorflags
!= RESET
)
1638 && ( ((cr3its
& USART_CR3_EIE
) != RESET
)
1639 || ((cr1its
& (USART_CR1_RXNEIE
| USART_CR1_PEIE
)) != RESET
)) )
1641 /* UART parity error interrupt occurred -------------------------------------*/
1642 if(((isrflags
& USART_ISR_PE
) != RESET
) && ((cr1its
& USART_CR1_PEIE
) != RESET
))
1644 __HAL_UART_CLEAR_IT(huart
, UART_CLEAR_PEF
);
1646 huart
->ErrorCode
|= HAL_UART_ERROR_PE
;
1649 /* UART frame error interrupt occurred --------------------------------------*/
1650 if(((isrflags
& USART_ISR_FE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
1652 __HAL_UART_CLEAR_IT(huart
, UART_CLEAR_FEF
);
1654 huart
->ErrorCode
|= HAL_UART_ERROR_FE
;
1657 /* UART noise error interrupt occurred --------------------------------------*/
1658 if(((isrflags
& USART_ISR_NE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
1660 __HAL_UART_CLEAR_IT(huart
, UART_CLEAR_NEF
);
1662 huart
->ErrorCode
|= HAL_UART_ERROR_NE
;
1665 /* UART Over-Run interrupt occurred -----------------------------------------*/
1666 if(((isrflags
& USART_ISR_ORE
) != RESET
) &&
1667 (((cr1its
& USART_CR1_RXNEIE
) != RESET
) || ((cr3its
& USART_CR3_EIE
) != RESET
)))
1669 __HAL_UART_CLEAR_IT(huart
, UART_CLEAR_OREF
);
1671 huart
->ErrorCode
|= HAL_UART_ERROR_ORE
;
1674 /* Call UART Error Call back function if need be --------------------------*/
1675 if(huart
->ErrorCode
!= HAL_UART_ERROR_NONE
)
1677 /* UART in mode Receiver ---------------------------------------------------*/
1678 if(((isrflags
& USART_ISR_RXNE
) != RESET
) && ((cr1its
& USART_CR1_RXNEIE
) != RESET
))
1680 UART_Receive_IT(huart
);
1683 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1684 consider error as blocking */
1685 if (((huart
->ErrorCode
& HAL_UART_ERROR_ORE
) != RESET
) ||
1686 (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)))
1688 /* Blocking error : transfer is aborted
1689 Set the UART state ready to be able to start again the process,
1690 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1691 UART_EndRxTransfer(huart
);
1693 /* Disable the UART DMA Rx request if enabled */
1694 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1696 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1698 /* Abort the UART DMA Rx channel */
1699 if(huart
->hdmarx
!= NULL
)
1701 /* Set the UART DMA Abort callback :
1702 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
1703 huart
->hdmarx
->XferAbortCallback
= UART_DMAAbortOnError
;
1706 if(HAL_DMA_Abort_IT(huart
->hdmarx
) != HAL_OK
)
1708 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
1709 huart
->hdmarx
->XferAbortCallback(huart
->hdmarx
);
1714 /* Call user error callback */
1715 HAL_UART_ErrorCallback(huart
);
1720 /* Call user error callback */
1721 HAL_UART_ErrorCallback(huart
);
1726 /* Non Blocking error : transfer could go on.
1727 Error is notified to user through user error callback */
1728 HAL_UART_ErrorCallback(huart
);
1729 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1734 } /* End if some error occurs */
1736 /* UART wakeup from Stop mode interrupt occurred ---------------------------*/
1737 if(((isrflags
& USART_ISR_WUF
) != RESET
) && ((cr3its
& USART_CR3_WUFIE
) != RESET
))
1739 __HAL_UART_CLEAR_IT(huart
, UART_CLEAR_WUF
);
1740 /* Set the UART state ready to be able to start again the process */
1741 huart
->gState
= HAL_UART_STATE_READY
;
1742 huart
->RxState
= HAL_UART_STATE_READY
;
1743 HAL_UARTEx_WakeupCallback(huart
);
1747 /* UART in mode Transmitter ------------------------------------------------*/
1748 if(((isrflags
& USART_ISR_TXE
) != RESET
) && ((cr1its
& USART_CR1_TXEIE
) != RESET
))
1750 UART_Transmit_IT(huart
);
1754 /* UART in mode Transmitter (transmission end) -----------------------------*/
1755 if(((isrflags
& USART_ISR_TC
) != RESET
) && ((cr1its
& USART_CR1_TCIE
) != RESET
))
1757 UART_EndTransmit_IT(huart
);
1764 * @brief Tx Transfer completed callback.
1765 * @param huart UART handle.
1768 __weak
void HAL_UART_TxCpltCallback(UART_HandleTypeDef
*huart
)
1770 /* Prevent unused argument(s) compilation warning */
1773 /* NOTE : This function should not be modified, when the callback is needed,
1774 the HAL_UART_TxCpltCallback can be implemented in the user file.
1779 * @brief Tx Half Transfer completed callback.
1780 * @param huart UART handle.
1783 __weak
void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef
*huart
)
1785 /* Prevent unused argument(s) compilation warning */
1788 /* NOTE: This function should not be modified, when the callback is needed,
1789 the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
1794 * @brief Rx Transfer completed callback.
1795 * @param huart UART handle.
1798 __weak
void HAL_UART_RxCpltCallback(UART_HandleTypeDef
*huart
)
1800 /* Prevent unused argument(s) compilation warning */
1803 /* NOTE : This function should not be modified, when the callback is needed,
1804 the HAL_UART_RxCpltCallback can be implemented in the user file.
1809 * @brief Rx Half Transfer completed callback.
1810 * @param huart UART handle.
1813 __weak
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef
*huart
)
1815 /* Prevent unused argument(s) compilation warning */
1818 /* NOTE: This function should not be modified, when the callback is needed,
1819 the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
1824 * @brief UART error callback.
1825 * @param huart UART handle.
1828 __weak
void HAL_UART_ErrorCallback(UART_HandleTypeDef
*huart
)
1830 /* Prevent unused argument(s) compilation warning */
1833 /* NOTE : This function should not be modified, when the callback is needed,
1834 the HAL_UART_ErrorCallback can be implemented in the user file.
1839 * @brief UART Abort Complete callback.
1840 * @param huart UART handle.
1843 __weak
void HAL_UART_AbortCpltCallback (UART_HandleTypeDef
*huart
)
1845 /* Prevent unused argument(s) compilation warning */
1848 /* NOTE : This function should not be modified, when the callback is needed,
1849 the HAL_UART_AbortCpltCallback can be implemented in the user file.
1854 * @brief UART Abort Complete callback.
1855 * @param huart UART handle.
1858 __weak
void HAL_UART_AbortTransmitCpltCallback (UART_HandleTypeDef
*huart
)
1860 /* Prevent unused argument(s) compilation warning */
1863 /* NOTE : This function should not be modified, when the callback is needed,
1864 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
1869 * @brief UART Abort Receive Complete callback.
1870 * @param huart UART handle.
1873 __weak
void HAL_UART_AbortReceiveCpltCallback (UART_HandleTypeDef
*huart
)
1875 /* Prevent unused argument(s) compilation warning */
1878 /* NOTE : This function should not be modified, when the callback is needed,
1879 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
1887 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
1888 * @brief UART control functions
1891 ===============================================================================
1892 ##### Peripheral Control functions #####
1893 ===============================================================================
1895 This subsection provides a set of functions allowing to control the UART.
1896 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
1897 (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
1898 (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
1899 (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
1900 (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
1901 (+) HAL_LIN_SendBreak() API transmits the break characters
1907 * @brief Enable UART in mute mode (does not mean UART enters mute mode;
1908 * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
1909 * @param huart UART handle.
1910 * @retval HAL status
1912 HAL_StatusTypeDef
HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef
*huart
)
1914 /* Process Locked */
1917 huart
->gState
= HAL_UART_STATE_BUSY
;
1919 /* Enable USART mute mode by setting the MME bit in the CR1 register */
1920 SET_BIT(huart
->Instance
->CR1
, USART_CR1_MME
);
1922 huart
->gState
= HAL_UART_STATE_READY
;
1924 return (UART_CheckIdleState(huart
));
1928 * @brief Disable UART mute mode (does not mean the UART actually exits mute mode
1929 * as it may not have been in mute mode at this very moment).
1930 * @param huart UART handle.
1931 * @retval HAL status
1933 HAL_StatusTypeDef
HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef
*huart
)
1935 /* Process Locked */
1938 huart
->gState
= HAL_UART_STATE_BUSY
;
1940 /* Disable USART mute mode by clearing the MME bit in the CR1 register */
1941 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_MME
);
1943 huart
->gState
= HAL_UART_STATE_READY
;
1945 return (UART_CheckIdleState(huart
));
1949 * @brief Enter UART mute mode (means UART actually enters mute mode).
1950 * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
1951 * @param huart UART handle.
1954 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef
*huart
)
1956 __HAL_UART_SEND_REQ(huart
, UART_MUTE_MODE_REQUEST
);
1960 * @brief Enable the UART transmitter and disable the UART receiver.
1961 * @param huart UART handle.
1962 * @retval HAL status
1964 HAL_StatusTypeDef
HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef
*huart
)
1966 /* Process Locked */
1968 huart
->gState
= HAL_UART_STATE_BUSY
;
1970 /* Clear TE and RE bits */
1971 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TE
| USART_CR1_RE
));
1972 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
1973 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TE
);
1975 huart
->gState
= HAL_UART_STATE_READY
;
1977 /* Process Unlocked */
1978 __HAL_UNLOCK(huart
);
1984 * @brief Enable the UART receiver and disable the UART transmitter.
1985 * @param huart UART handle.
1986 * @retval HAL status.
1988 HAL_StatusTypeDef
HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef
*huart
)
1990 /* Process Locked */
1992 huart
->gState
= HAL_UART_STATE_BUSY
;
1994 /* Clear TE and RE bits */
1995 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TE
| USART_CR1_RE
));
1996 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
1997 SET_BIT(huart
->Instance
->CR1
, USART_CR1_RE
);
1999 huart
->gState
= HAL_UART_STATE_READY
;
2000 /* Process Unlocked */
2001 __HAL_UNLOCK(huart
);
2008 * @brief Transmit break characters.
2009 * @param huart UART handle.
2010 * @retval HAL status
2012 HAL_StatusTypeDef
HAL_LIN_SendBreak(UART_HandleTypeDef
*huart
)
2014 /* Check the parameters */
2015 assert_param(IS_UART_LIN_INSTANCE(huart
->Instance
));
2017 /* Process Locked */
2020 huart
->gState
= HAL_UART_STATE_BUSY
;
2022 /* Send break characters */
2023 huart
->Instance
->RQR
|= UART_SENDBREAK_REQUEST
;
2025 huart
->gState
= HAL_UART_STATE_READY
;
2027 /* Process Unlocked */
2028 __HAL_UNLOCK(huart
);
2038 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
2039 * @brief UART Peripheral State functions
2042 ==============================================================================
2043 ##### Peripheral State and Error functions #####
2044 ==============================================================================
2046 This subsection provides functions allowing to :
2047 (+) Return the UART handle state.
2048 (+) Return the UART handle error code
2055 * @brief Return the UART handle state.
2056 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2057 * the configuration information for the specified UART.
2060 HAL_UART_StateTypeDef
HAL_UART_GetState(UART_HandleTypeDef
*huart
)
2062 uint32_t temp1
= 0x00U
, temp2
= 0x00U
;
2063 temp1
= huart
->gState
;
2064 temp2
= huart
->RxState
;
2066 return (HAL_UART_StateTypeDef
)(temp1
| temp2
);
2070 * @brief Return the UART handle error code.
2071 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2072 * the configuration information for the specified UART.
2073 * @retval UART Error Code
2075 uint32_t HAL_UART_GetError(UART_HandleTypeDef
*huart
)
2077 return huart
->ErrorCode
;
2087 /** @defgroup UART_Private_Functions UART Private Functions
2092 * @brief Configure the UART peripheral.
2093 * @param huart UART handle.
2094 * @retval HAL status
2096 HAL_StatusTypeDef
UART_SetConfig(UART_HandleTypeDef
*huart
)
2098 uint32_t tmpreg
= 0x00000000U
;
2099 UART_ClockSourceTypeDef clocksource
= UART_CLOCKSOURCE_UNDEFINED
;
2100 uint16_t brrtemp
= 0x0000U
;
2101 uint16_t usartdiv
= 0x0000U
;
2102 HAL_StatusTypeDef ret
= HAL_OK
;
2104 /* Check the parameters */
2105 assert_param(IS_UART_BAUDRATE(huart
->Init
.BaudRate
));
2106 assert_param(IS_UART_WORD_LENGTH(huart
->Init
.WordLength
));
2107 assert_param(IS_UART_STOPBITS(huart
->Init
.StopBits
));
2108 assert_param(IS_UART_PARITY(huart
->Init
.Parity
));
2109 assert_param(IS_UART_MODE(huart
->Init
.Mode
));
2110 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart
->Init
.HwFlowCtl
));
2111 assert_param(IS_UART_ONE_BIT_SAMPLE(huart
->Init
.OneBitSampling
));
2112 assert_param(IS_UART_OVERSAMPLING(huart
->Init
.OverSampling
));
2115 /*-------------------------- USART CR1 Configuration -----------------------*/
2116 /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
2117 * the UART Word Length, Parity, Mode and oversampling:
2118 * set the M bits according to huart->Init.WordLength value
2119 * set PCE and PS bits according to huart->Init.Parity value
2120 * set TE and RE bits according to huart->Init.Mode value
2121 * set OVER8 bit according to huart->Init.OverSampling value */
2122 tmpreg
= (uint32_t)huart
->Init
.WordLength
| huart
->Init
.Parity
| huart
->Init
.Mode
| huart
->Init
.OverSampling
;
2123 MODIFY_REG(huart
->Instance
->CR1
, UART_CR1_FIELDS
, tmpreg
);
2125 /*-------------------------- USART CR2 Configuration -----------------------*/
2126 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
2127 * to huart->Init.StopBits value */
2128 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_STOP
, huart
->Init
.StopBits
);
2130 /*-------------------------- USART CR3 Configuration -----------------------*/
2132 * - UART HardWare Flow Control: set CTSE and RTSE bits according
2133 * to huart->Init.HwFlowCtl value
2134 * - one-bit sampling method versus three samples' majority rule according
2135 * to huart->Init.OneBitSampling */
2136 tmpreg
= (uint32_t)huart
->Init
.HwFlowCtl
| huart
->Init
.OneBitSampling
;
2137 MODIFY_REG(huart
->Instance
->CR3
, (USART_CR3_RTSE
| USART_CR3_CTSE
| USART_CR3_ONEBIT
), tmpreg
);
2139 /*-------------------------- USART BRR Configuration -----------------------*/
2140 UART_GETCLOCKSOURCE(huart
, clocksource
);
2142 /* Check UART Over Sampling to set Baud Rate Register */
2143 if (huart
->Init
.OverSampling
== UART_OVERSAMPLING_8
)
2145 switch (clocksource
)
2147 case UART_CLOCKSOURCE_PCLK1
:
2148 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart
->Init
.BaudRate
));
2150 case UART_CLOCKSOURCE_PCLK2
:
2151 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart
->Init
.BaudRate
));
2153 case UART_CLOCKSOURCE_HSI
:
2154 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE
, huart
->Init
.BaudRate
));
2156 case UART_CLOCKSOURCE_SYSCLK
:
2157 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart
->Init
.BaudRate
));
2159 case UART_CLOCKSOURCE_LSE
:
2160 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE
, huart
->Init
.BaudRate
));
2162 case UART_CLOCKSOURCE_UNDEFINED
:
2168 brrtemp
= usartdiv
& 0xFFF0U
;
2169 brrtemp
|= (uint16_t)((usartdiv
& (uint16_t)0x000FU
) >> 1U);
2170 huart
->Instance
->BRR
= brrtemp
;
2174 switch (clocksource
)
2176 case UART_CLOCKSOURCE_PCLK1
:
2177 huart
->Instance
->BRR
= (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart
->Init
.BaudRate
));
2179 case UART_CLOCKSOURCE_PCLK2
:
2180 huart
->Instance
->BRR
= (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart
->Init
.BaudRate
));
2182 case UART_CLOCKSOURCE_HSI
:
2183 huart
->Instance
->BRR
= (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE
, huart
->Init
.BaudRate
));
2185 case UART_CLOCKSOURCE_SYSCLK
:
2186 huart
->Instance
->BRR
= (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart
->Init
.BaudRate
));
2188 case UART_CLOCKSOURCE_LSE
:
2189 huart
->Instance
->BRR
= (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE
, huart
->Init
.BaudRate
));
2191 case UART_CLOCKSOURCE_UNDEFINED
:
2203 * @brief Configure the UART peripheral advanced features.
2204 * @param huart UART handle.
2207 void UART_AdvFeatureConfig(UART_HandleTypeDef
*huart
)
2209 /* Check whether the set of advanced features to configure is properly set */
2210 assert_param(IS_UART_ADVFEATURE_INIT(huart
->AdvancedInit
.AdvFeatureInit
));
2212 /* if required, configure TX pin active level inversion */
2213 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_TXINVERT_INIT
))
2215 assert_param(IS_UART_ADVFEATURE_TXINV(huart
->AdvancedInit
.TxPinLevelInvert
));
2216 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_TXINV
, huart
->AdvancedInit
.TxPinLevelInvert
);
2219 /* if required, configure RX pin active level inversion */
2220 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_RXINVERT_INIT
))
2222 assert_param(IS_UART_ADVFEATURE_RXINV(huart
->AdvancedInit
.RxPinLevelInvert
));
2223 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_RXINV
, huart
->AdvancedInit
.RxPinLevelInvert
);
2226 /* if required, configure data inversion */
2227 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_DATAINVERT_INIT
))
2229 assert_param(IS_UART_ADVFEATURE_DATAINV(huart
->AdvancedInit
.DataInvert
));
2230 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_DATAINV
, huart
->AdvancedInit
.DataInvert
);
2233 /* if required, configure RX/TX pins swap */
2234 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_SWAP_INIT
))
2236 assert_param(IS_UART_ADVFEATURE_SWAP(huart
->AdvancedInit
.Swap
));
2237 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_SWAP
, huart
->AdvancedInit
.Swap
);
2240 /* if required, configure RX overrun detection disabling */
2241 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT
))
2243 assert_param(IS_UART_OVERRUN(huart
->AdvancedInit
.OverrunDisable
));
2244 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_OVRDIS
, huart
->AdvancedInit
.OverrunDisable
);
2247 /* if required, configure DMA disabling on reception error */
2248 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_DMADISABLEONERROR_INIT
))
2250 assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart
->AdvancedInit
.DMADisableonRxError
));
2251 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_DDRE
, huart
->AdvancedInit
.DMADisableonRxError
);
2254 /* if required, configure auto Baud rate detection scheme */
2255 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_AUTOBAUDRATE_INIT
))
2257 assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart
->Instance
));
2258 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart
->AdvancedInit
.AutoBaudRateEnable
));
2259 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ABREN
, huart
->AdvancedInit
.AutoBaudRateEnable
);
2260 /* set auto Baudrate detection parameters if detection is enabled */
2261 if(huart
->AdvancedInit
.AutoBaudRateEnable
== UART_ADVFEATURE_AUTOBAUDRATE_ENABLE
)
2263 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart
->AdvancedInit
.AutoBaudRateMode
));
2264 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ABRMODE
, huart
->AdvancedInit
.AutoBaudRateMode
);
2268 /* if required, configure MSB first on communication line */
2269 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_MSBFIRST_INIT
))
2271 assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart
->AdvancedInit
.MSBFirst
));
2272 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_MSBFIRST
, huart
->AdvancedInit
.MSBFirst
);
2277 * @brief Check the UART Idle State.
2278 * @param huart UART handle.
2279 * @retval HAL status
2281 HAL_StatusTypeDef
UART_CheckIdleState(UART_HandleTypeDef
*huart
)
2283 uint32_t tickstart
= 0U;
2285 /* Initialize the UART ErrorCode */
2286 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
2288 /* Init tickstart for timeout managment*/
2289 tickstart
= HAL_GetTick();
2291 /* Check if the Transmitter is enabled */
2292 if((huart
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
2294 /* Wait until TEACK flag is set */
2295 if(UART_WaitOnFlagUntilTimeout(huart
, USART_ISR_TEACK
, RESET
, tickstart
, HAL_UART_TIMEOUT_VALUE
) != HAL_OK
)
2297 /* Timeout Occured */
2301 /* Check if the Receiver is enabled */
2302 if((huart
->Instance
->CR1
& USART_CR1_RE
) == USART_CR1_RE
)
2304 /* Wait until REACK flag is set */
2305 if(UART_WaitOnFlagUntilTimeout(huart
, USART_ISR_REACK
, RESET
, tickstart
, HAL_UART_TIMEOUT_VALUE
) != HAL_OK
)
2307 /* Timeout Occured */
2312 /* Initialize the UART State */
2313 huart
->gState
= HAL_UART_STATE_READY
;
2314 huart
->RxState
= HAL_UART_STATE_READY
;
2316 /* Process Unlocked */
2317 __HAL_UNLOCK(huart
);
2323 * @brief Handle UART Communication Timeout.
2324 * @param huart UART handle.
2325 * @param Flag Specifies the UART flag to check
2326 * @param Status Flag status (SET or RESET)
2327 * @param Tickstart Tick start value
2328 * @param Timeout Timeout duration
2329 * @retval HAL status
2331 HAL_StatusTypeDef
UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef
*huart
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
)
2333 /* Wait until flag is set */
2334 while((__HAL_UART_GET_FLAG(huart
, Flag
) ? SET
: RESET
) == Status
)
2336 /* Check for the Timeout */
2337 if(Timeout
!= HAL_MAX_DELAY
)
2339 if((Timeout
== 0U) || ((HAL_GetTick()-Tickstart
) > Timeout
))
2341 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
2342 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
));
2343 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
2345 huart
->gState
= HAL_UART_STATE_READY
;
2346 huart
->RxState
= HAL_UART_STATE_READY
;
2348 /* Process Unlocked */
2349 __HAL_UNLOCK(huart
);
2359 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
2360 * @param huart UART handle.
2363 static void UART_EndTxTransfer(UART_HandleTypeDef
*huart
)
2365 /* Disable TXEIE and TCIE interrupts */
2366 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
2368 /* At end of Tx process, restore huart->gState to Ready */
2369 huart
->gState
= HAL_UART_STATE_READY
;
2374 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2375 * @param huart UART handle.
2378 static void UART_EndRxTransfer(UART_HandleTypeDef
*huart
)
2380 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2381 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
2382 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
2384 /* At end of Rx process, restore huart->RxState to Ready */
2385 huart
->RxState
= HAL_UART_STATE_READY
;
2390 * @brief DMA UART transmit process complete callback.
2391 * @param hdma DMA handle.
2394 static void UART_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2396 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
2398 /* DMA Normal mode */
2399 if ( HAL_IS_BIT_CLR(hdma
->Instance
->CCR
, DMA_CCR_CIRC
) )
2401 huart
->TxXferCount
= 0U;
2403 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2404 in the UART CR3 register */
2405 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
2407 /* Enable the UART Transmit Complete Interrupt */
2408 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
2410 /* DMA Circular mode */
2413 HAL_UART_TxCpltCallback(huart
);
2419 * @brief DMA UART transmit process half complete callback.
2420 * @param hdma DMA handle.
2423 static void UART_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
)
2425 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
2427 HAL_UART_TxHalfCpltCallback(huart
);
2431 * @brief DMA UART receive process complete callback.
2432 * @param hdma DMA handle.
2435 static void UART_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
2437 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
2439 /* DMA Normal mode */
2440 if ( HAL_IS_BIT_CLR(hdma
->Instance
->CCR
, DMA_CCR_CIRC
) )
2442 huart
->RxXferCount
= 0U;
2444 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2445 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
2446 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
2448 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2449 in the UART CR3 register */
2450 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
2452 /* At end of Rx process, restore huart->RxState to Ready */
2453 huart
->RxState
= HAL_UART_STATE_READY
;
2456 HAL_UART_RxCpltCallback(huart
);
2460 * @brief DMA UART receive process half complete callback.
2461 * @param hdma DMA handle.
2464 static void UART_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
)
2466 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
2468 HAL_UART_RxHalfCpltCallback(huart
);
2472 * @brief DMA UART communication error callback.
2473 * @param hdma DMA handle.
2476 static void UART_DMAError(DMA_HandleTypeDef
*hdma
)
2478 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
2480 /* Stop UART DMA Tx request if ongoing */
2481 if ( (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
2482 &&(HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)) )
2484 huart
->TxXferCount
= 0U;
2485 UART_EndTxTransfer(huart
);
2488 /* Stop UART DMA Rx request if ongoing */
2489 if ( (huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
2490 &&(HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)) )
2492 huart
->RxXferCount
= 0U;
2493 UART_EndRxTransfer(huart
);
2496 huart
->ErrorCode
|= HAL_UART_ERROR_DMA
;
2497 HAL_UART_ErrorCallback(huart
);
2501 * @brief DMA UART communication abort callback, when initiated by HAL services on Error
2502 * (To be called at end of DMA Abort procedure following error occurrence).
2503 * @param hdma DMA handle.
2506 static void UART_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
2508 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
2509 huart
->RxXferCount
= 0U;
2510 huart
->TxXferCount
= 0U;
2512 HAL_UART_ErrorCallback(huart
);
2516 * @brief DMA UART Tx communication abort callback, when initiated by user
2517 * (To be called at end of DMA Tx Abort procedure following user abort request).
2518 * @note When this callback is executed, User Abort complete call back is called only if no
2519 * Abort still ongoing for Rx DMA Handle.
2520 * @param hdma DMA handle.
2523 static void UART_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
2525 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
* )(hdma
->Parent
);
2527 huart
->hdmatx
->XferAbortCallback
= NULL
;
2529 /* Check if an Abort process is still ongoing */
2530 if(huart
->hdmarx
!= NULL
)
2532 if(huart
->hdmarx
->XferAbortCallback
!= NULL
)
2538 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2539 huart
->TxXferCount
= 0U;
2540 huart
->RxXferCount
= 0U;
2542 /* Reset errorCode */
2543 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
2545 /* Clear the Error flags in the ICR register */
2546 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
2548 /* Restore huart->gState and huart->RxState to Ready */
2549 huart
->gState
= HAL_UART_STATE_READY
;
2550 huart
->RxState
= HAL_UART_STATE_READY
;
2552 /* Call user Abort complete callback */
2553 HAL_UART_AbortCpltCallback(huart
);
2558 * @brief DMA UART Rx communication abort callback, when initiated by user
2559 * (To be called at end of DMA Rx Abort procedure following user abort request).
2560 * @note When this callback is executed, User Abort complete call back is called only if no
2561 * Abort still ongoing for Tx DMA Handle.
2562 * @param hdma DMA handle.
2565 static void UART_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
2567 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
* )(hdma
->Parent
);
2569 huart
->hdmarx
->XferAbortCallback
= NULL
;
2571 /* Check if an Abort process is still ongoing */
2572 if(huart
->hdmatx
!= NULL
)
2574 if(huart
->hdmatx
->XferAbortCallback
!= NULL
)
2580 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2581 huart
->TxXferCount
= 0U;
2582 huart
->RxXferCount
= 0U;
2584 /* Reset errorCode */
2585 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
2587 /* Clear the Error flags in the ICR register */
2588 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
2590 /* Restore huart->gState and huart->RxState to Ready */
2591 huart
->gState
= HAL_UART_STATE_READY
;
2592 huart
->RxState
= HAL_UART_STATE_READY
;
2594 /* Call user Abort complete callback */
2595 HAL_UART_AbortCpltCallback(huart
);
2600 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
2601 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
2602 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2603 * and leads to user Tx Abort Complete callback execution).
2604 * @param hdma DMA handle.
2607 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2609 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
2611 huart
->TxXferCount
= 0U;
2613 /* Restore huart->gState to Ready */
2614 huart
->gState
= HAL_UART_STATE_READY
;
2616 /* Call user Abort complete callback */
2617 HAL_UART_AbortTransmitCpltCallback(huart
);
2621 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
2622 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
2623 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2624 * and leads to user Rx Abort Complete callback execution).
2625 * @param hdma DMA handle.
2628 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2630 UART_HandleTypeDef
* huart
= ( UART_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
2632 huart
->RxXferCount
= 0U;
2634 /* Clear the Error flags in the ICR register */
2635 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
2637 /* Restore huart->RxState to Ready */
2638 huart
->RxState
= HAL_UART_STATE_READY
;
2640 /* Call user Abort complete callback */
2641 HAL_UART_AbortReceiveCpltCallback(huart
);
2645 * @brief Send an amount of data in interrupt mode.
2646 * @note Function is called under interruption only, once
2647 * interruptions have been enabled by HAL_UART_Transmit_IT().
2648 * @param huart UART handle.
2649 * @retval HAL status
2651 HAL_StatusTypeDef
UART_Transmit_IT(UART_HandleTypeDef
*huart
)
2655 /* Check that a Tx process is ongoing */
2656 if (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
2658 if(huart
->TxXferCount
== 0U)
2660 /* Disable the UART Transmit Data Register Empty Interrupt */
2661 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_TXEIE
);
2663 /* Enable the UART Transmit Complete Interrupt */
2664 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
2670 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
2672 tmp
= (uint16_t*) huart
->pTxBuffPtr
;
2673 huart
->Instance
->TDR
= (*tmp
& (uint16_t)0x01FFU
);
2674 huart
->pTxBuffPtr
+= 2U;
2678 huart
->Instance
->TDR
= (uint8_t)(*huart
->pTxBuffPtr
++ & (uint8_t)0xFFU
);
2680 huart
->TxXferCount
--;
2692 * @brief Wrap up transmission in non-blocking mode.
2693 * @param huart pointer to a UART_HandleTypeDef structure that contains
2694 * the configuration information for the specified UART module.
2695 * @retval HAL status
2697 HAL_StatusTypeDef
UART_EndTransmit_IT(UART_HandleTypeDef
*huart
)
2699 /* Disable the UART Transmit Complete Interrupt */
2700 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
2702 /* Tx process is ended, restore huart->gState to Ready */
2703 huart
->gState
= HAL_UART_STATE_READY
;
2705 HAL_UART_TxCpltCallback(huart
);
2711 * @brief Receive an amount of data in interrupt mode.
2712 * @note Function is called under interruption only, once
2713 * interruptions have been enabled by HAL_UART_Receive_IT()
2714 * @param huart UART handle.
2715 * @retval HAL status
2717 HAL_StatusTypeDef
UART_Receive_IT(UART_HandleTypeDef
*huart
)
2720 uint16_t uhMask
= huart
->Mask
;
2723 /* Check that a Rx process is ongoing */
2724 if(huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
2726 uhdata
= (uint16_t) READ_REG(huart
->Instance
->RDR
);
2727 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
2729 tmp
= (uint16_t*) huart
->pRxBuffPtr
;
2730 *tmp
= (uint16_t)(uhdata
& uhMask
);
2731 huart
->pRxBuffPtr
+=2U;
2735 *huart
->pRxBuffPtr
++ = (uint8_t)(uhdata
& (uint8_t)uhMask
);
2738 if(--huart
->RxXferCount
== 0U)
2740 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
2741 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
2743 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
2744 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
2746 /* Rx process is completed, restore huart->RxState to Ready */
2747 huart
->RxState
= HAL_UART_STATE_READY
;
2749 HAL_UART_RxCpltCallback(huart
);
2758 /* Clear RXNE interrupt flag */
2759 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
2766 * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
2767 * @param huart UART handle.
2768 * @param WakeUpSelection UART wake up from stop mode parameters.
2771 void UART_Wakeup_AddressConfig(UART_HandleTypeDef
*huart
, UART_WakeUpTypeDef WakeUpSelection
)
2773 assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection
.AddressLength
));
2775 /* Set the USART address length */
2776 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ADDM7
, WakeUpSelection
.AddressLength
);
2778 /* Set the USART address node */
2779 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ADD
, ((uint32_t)WakeUpSelection
.Address
<< UART_CR2_ADDRESS_LSB_POS
));
2786 #endif /* HAL_UART_MODULE_ENABLED */
2795 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/