source: asp3_wo_tecs/trunk/arch/arm_m_gcc/stm32f4xx_stm32cube/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c@ 303

Last change on this file since 303 was 303, checked in by ertl-honda, 7 years ago

nucleo_f401re依存部の追加

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