sidestep
[mTask.git] / int / com / lib / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_uart.c
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_uart.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 22-April-2016
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_IT : Clears the specified UART ISR 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) 2016 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 "stm32f7xx_hal.h"
158
159 /** @addtogroup STM32F7xx_HAL_Driver
160 * @{
161 */
162
163 /** @defgroup UART UART
164 * @brief HAL UART module driver
165 * @{
166 */
167
168 #ifdef HAL_UART_MODULE_ENABLED
169
170 /* Private typedef -----------------------------------------------------------*/
171 /* Private define ------------------------------------------------------------*/
172 /** @defgroup UART_Private_Constants UART Private Constants
173 * @{
174 */
175 #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
176 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
177 /**
178 * @}
179 */
180 /* Private macro -------------------------------------------------------------*/
181 /* Private variables ---------------------------------------------------------*/
182 /* Private function prototypes -----------------------------------------------*/
183 /** @addtogroup UART_Private_Functions
184 * @{
185 */
186 static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
187 static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
188 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
189 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
190 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
191 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
192 static void UART_DMAError(DMA_HandleTypeDef *hdma);
193 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
194 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
195 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
196 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
197 /**
198 * @}
199 */
200
201 /* Exported functions --------------------------------------------------------*/
202
203 /** @defgroup UART_Exported_Functions UART Exported Functions
204 * @{
205 */
206
207 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
208 * @brief Initialization and Configuration functions
209 *
210 @verbatim
211 ===============================================================================
212 ##### Initialization and Configuration functions #####
213 ===============================================================================
214 [..]
215 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
216 in asynchronous mode.
217 (+) For the asynchronous mode only these parameters can be configured:
218 (++) Baud Rate
219 (++) Word Length
220 (++) Stop Bit
221 (++) Parity: If the parity is enabled, then the MSB bit of the data written
222 in the data register is transmitted but is changed by the parity bit.
223 Depending on the frame length defined by the M bit (8-bits or 9-bits),
224 please refer to Reference manual for possible UART frame formats.
225 (++) Hardware flow control
226 (++) Receiver/transmitter modes
227 (++) Over Sampling Method
228 [..]
229 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
230 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor
231 configuration procedures (details for the procedures are available in reference manual (RM0329)).
232
233 @endverbatim
234 * @{
235 */
236
237 /**
238 * @brief Initializes the UART mode according to the specified
239 * parameters in the UART_InitTypeDef and creates the associated handle .
240 * @param huart: uart handle
241 * @retval HAL status
242 */
243 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
244 {
245 /* Check the UART handle allocation */
246 if(huart == NULL)
247 {
248 return HAL_ERROR;
249 }
250
251 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
252 {
253 /* Check the parameters */
254 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
255 }
256 else
257 {
258 /* Check the parameters */
259 assert_param(IS_UART_INSTANCE(huart->Instance));
260 }
261
262 if(huart->gState == HAL_UART_STATE_RESET)
263 {
264 /* Allocate lock resource and initialize it */
265 huart->Lock = HAL_UNLOCKED;
266
267 /* Init the low level hardware : GPIO, CLOCK */
268 HAL_UART_MspInit(huart);
269 }
270
271 huart->gState = HAL_UART_STATE_BUSY;
272
273 /* Disable the Peripheral */
274 __HAL_UART_DISABLE(huart);
275
276 /* Set the UART Communication parameters */
277 if (UART_SetConfig(huart) == HAL_ERROR)
278 {
279 return HAL_ERROR;
280 }
281
282 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
283 {
284 UART_AdvFeatureConfig(huart);
285 }
286
287 /* In asynchronous mode, the following bits must be kept cleared:
288 - LINEN and CLKEN bits in the USART_CR2 register,
289 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
290 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
291 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
292
293 /* Enable the Peripheral */
294 __HAL_UART_ENABLE(huart);
295
296 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
297 return (UART_CheckIdleState(huart));
298 }
299
300 /**
301 * @brief Initializes the half-duplex mode according to the specified
302 * parameters in the UART_InitTypeDef and creates the associated handle .
303 * @param huart: UART handle
304 * @retval HAL status
305 */
306 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
307 {
308 /* Check the UART handle allocation */
309 if(huart == NULL)
310 {
311 return HAL_ERROR;
312 }
313
314 if(huart->gState == HAL_UART_STATE_RESET)
315 {
316 /* Allocate lock resource and initialize it */
317 huart->Lock = HAL_UNLOCKED;
318
319 /* Init the low level hardware : GPIO, CLOCK */
320 HAL_UART_MspInit(huart);
321 }
322
323 huart->gState = HAL_UART_STATE_BUSY;
324
325 /* Disable the Peripheral */
326 __HAL_UART_DISABLE(huart);
327
328 /* Set the UART Communication parameters */
329 if (UART_SetConfig(huart) == HAL_ERROR)
330 {
331 return HAL_ERROR;
332 }
333
334 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
335 {
336 UART_AdvFeatureConfig(huart);
337 }
338
339 /* In half-duplex mode, the following bits must be kept cleared:
340 - LINEN and CLKEN bits in the USART_CR2 register,
341 - SCEN and IREN bits in the USART_CR3 register.*/
342 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
343 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
344
345 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
346 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
347
348 /* Enable the Peripheral */
349 __HAL_UART_ENABLE(huart);
350
351 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
352 return (UART_CheckIdleState(huart));
353 }
354
355
356 /**
357 * @brief Initialize the LIN mode according to the specified
358 * parameters in the UART_InitTypeDef and creates the associated handle .
359 * @param huart: UART handle.
360 * @param BreakDetectLength: specifies the LIN break detection length.
361 * This parameter can be one of the following values:
362 * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
363 * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
364 * @retval HAL status
365 */
366 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
367 {
368 /* Check the UART handle allocation */
369 if(huart == NULL)
370 {
371 return HAL_ERROR;
372 }
373
374 /* Check the parameters */
375 assert_param(IS_UART_INSTANCE(huart->Instance));
376 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
377 assert_param(IS_LIN_WORD_LENGTH(huart->Init.WordLength));
378
379 if(huart->gState == HAL_UART_STATE_RESET)
380 {
381 /* Allocate lock resource and initialize it */
382 huart->Lock = HAL_UNLOCKED;
383
384 /* Init the low level hardware : GPIO, CLOCK */
385 HAL_UART_MspInit(huart);
386 }
387
388 huart->gState = HAL_UART_STATE_BUSY;
389
390 /* Disable the Peripheral */
391 __HAL_UART_DISABLE(huart);
392
393 /* Set the UART Communication parameters */
394 if (UART_SetConfig(huart) == HAL_ERROR)
395 {
396 return HAL_ERROR;
397 }
398
399 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
400 {
401 UART_AdvFeatureConfig(huart);
402 }
403
404 /* In LIN mode, the following bits must be kept cleared:
405 - LINEN and CLKEN bits in the USART_CR2 register,
406 - SCEN and IREN bits in the USART_CR3 register.*/
407 CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
408 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
409
410 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
411 SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
412
413 /* Set the USART LIN Break detection length. */
414 MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
415
416 /* Enable the Peripheral */
417 __HAL_UART_ENABLE(huart);
418
419 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
420 return (UART_CheckIdleState(huart));
421 }
422
423
424 /**
425 * @brief Initialize the multiprocessor mode according to the specified
426 * parameters in the UART_InitTypeDef and initialize the associated handle.
427 * @param huart: UART handle.
428 * @param Address: UART node address (4-, 6-, 7- or 8-bit long).
429 * @param WakeUpMethod: specifies the UART wakeup method.
430 * This parameter can be one of the following values:
431 * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
432 * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
433 * @note If the user resorts to idle line detection wake up, the Address parameter
434 * is useless and ignored by the initialization function.
435 * @note If the user resorts to address mark wake up, the address length detection
436 * is configured by default to 4 bits only. For the UART to be able to
437 * manage 6-, 7- or 8-bit long addresses detection
438 * @retval HAL status
439 */
440 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
441 {
442 /* Check the UART handle allocation */
443 if(huart == NULL)
444 {
445 return HAL_ERROR;
446 }
447
448 /* Check the wake up method parameter */
449 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
450
451 if(huart->gState == HAL_UART_STATE_RESET)
452 {
453 /* Allocate lock resource and initialize it */
454 huart->Lock = HAL_UNLOCKED;
455
456 /* Init the low level hardware : GPIO, CLOCK */
457 HAL_UART_MspInit(huart);
458 }
459
460 huart->gState = HAL_UART_STATE_BUSY;
461
462 /* Disable the Peripheral */
463 __HAL_UART_DISABLE(huart);
464
465 /* Set the UART Communication parameters */
466 if (UART_SetConfig(huart) == HAL_ERROR)
467 {
468 return HAL_ERROR;
469 }
470
471 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
472 {
473 UART_AdvFeatureConfig(huart);
474 }
475
476 /* In multiprocessor mode, the following bits must be kept cleared:
477 - LINEN and CLKEN bits in the USART_CR2 register,
478 - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
479 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
480 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
481
482 if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
483 {
484 /* If address mark wake up method is chosen, set the USART address node */
485 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
486 }
487
488 /* Set the wake up method by setting the WAKE bit in the CR1 register */
489 MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
490
491 /* Enable the Peripheral */
492 __HAL_UART_ENABLE(huart);
493
494 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
495 return (UART_CheckIdleState(huart));
496 }
497
498
499 /**
500 * @brief Initialize the RS485 Driver enable feature according to the specified
501 * parameters in the UART_InitTypeDef and creates the associated handle.
502 * @param huart: UART handle.
503 * @param Polarity: select the driver enable polarity.
504 * This parameter can be one of the following values:
505 * @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
506 * @arg @ref UART_DE_POLARITY_LOW DE signal is active low
507 * @param AssertionTime: Driver Enable assertion time:
508 * 5-bit value defining the time between the activation of the DE (Driver Enable)
509 * signal and the beginning of the start bit. It is expressed in sample time
510 * units (1/8 or 1/16 bit time, depending on the oversampling rate)
511 * @param DeassertionTime: Driver Enable deassertion time:
512 * 5-bit value defining the time between the end of the last stop bit, in a
513 * transmitted message, and the de-activation of the DE (Driver Enable) signal.
514 * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
515 * oversampling rate).
516 * @retval HAL status
517 */
518 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
519 {
520 uint32_t temp = 0x0;
521
522 /* Check the UART handle allocation */
523 if(huart == NULL)
524 {
525 return HAL_ERROR;
526 }
527 /* Check the Driver Enable UART instance */
528 assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
529
530 /* Check the Driver Enable polarity */
531 assert_param(IS_UART_DE_POLARITY(Polarity));
532
533 /* Check the Driver Enable assertion time */
534 assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
535
536 /* Check the Driver Enable deassertion time */
537 assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
538
539 if(huart->gState == HAL_UART_STATE_RESET)
540 {
541 /* Allocate lock resource and initialize it */
542 huart->Lock = HAL_UNLOCKED;
543
544 /* Init the low level hardware : GPIO, CLOCK, CORTEX */
545 HAL_UART_MspInit(huart);
546 }
547
548 huart->gState = HAL_UART_STATE_BUSY;
549
550 /* Disable the Peripheral */
551 __HAL_UART_DISABLE(huart);
552
553 /* Set the UART Communication parameters */
554 if (UART_SetConfig(huart) == HAL_ERROR)
555 {
556 return HAL_ERROR;
557 }
558
559 if(huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
560 {
561 UART_AdvFeatureConfig(huart);
562 }
563
564 /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
565 SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
566
567 /* Set the Driver Enable polarity */
568 MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
569
570 /* Set the Driver Enable assertion and deassertion times */
571 temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
572 temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
573 MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT|USART_CR1_DEAT), temp);
574
575 /* Enable the Peripheral */
576 __HAL_UART_ENABLE(huart);
577
578 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
579 return (UART_CheckIdleState(huart));
580 }
581
582 /**
583 * @brief DeInitializes the UART peripheral
584 * @param huart: uart handle
585 * @retval HAL status
586 */
587 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
588 {
589 /* Check the UART handle allocation */
590 if(huart == NULL)
591 {
592 return HAL_ERROR;
593 }
594
595 /* Check the parameters */
596 assert_param(IS_UART_INSTANCE(huart->Instance));
597
598 huart->gState = HAL_UART_STATE_BUSY;
599
600 /* Disable the Peripheral */
601 __HAL_UART_DISABLE(huart);
602
603 huart->Instance->CR1 = 0x0U;
604 huart->Instance->CR2 = 0x0U;
605 huart->Instance->CR3 = 0x0U;
606
607 /* DeInit the low level hardware */
608 HAL_UART_MspDeInit(huart);
609
610 huart->ErrorCode = HAL_UART_ERROR_NONE;
611 huart->gState = HAL_UART_STATE_RESET;
612 huart->RxState = HAL_UART_STATE_RESET;
613
614 /* Process Unlock */
615 __HAL_UNLOCK(huart);
616
617 return HAL_OK;
618 }
619
620 /**
621 * @brief UART MSP Init
622 * @param huart: uart handle
623 * @retval None
624 */
625 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
626 {
627 /* Prevent unused argument(s) compilation warning */
628 UNUSED(huart);
629
630 /* NOTE : This function should not be modified, when the callback is needed,
631 the HAL_UART_MspInit can be implemented in the user file
632 */
633 }
634
635 /**
636 * @brief UART MSP DeInit
637 * @param huart: uart handle
638 * @retval None
639 */
640 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
641 {
642 /* Prevent unused argument(s) compilation warning */
643 UNUSED(huart);
644
645 /* NOTE : This function should not be modified, when the callback is needed,
646 the HAL_UART_MspDeInit can be implemented in the user file
647 */
648 }
649
650 /**
651 * @}
652 */
653
654 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
655 * @brief UART Transmit/Receive functions
656 *
657 @verbatim
658 ===============================================================================
659 ##### IO operation functions #####
660 ===============================================================================
661 This subsection provides a set of functions allowing to manage the UART asynchronous
662 and Half duplex data transfers.
663
664 (#) There are two mode of transfer:
665 (+) Blocking mode: The communication is performed in polling mode.
666 The HAL status of all data processing is returned by the same function
667 after finishing transfer.
668 (+) Non-Blocking mode: The communication is performed using Interrupts
669 or DMA, These API's return the HAL status.
670 The end of the data processing will be indicated through the
671 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
672 using DMA mode.
673 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
674 will be executed respectively at the end of the transmit or Receive process
675 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
676
677 (#) Blocking mode API's are :
678 (+) HAL_UART_Transmit()
679 (+) HAL_UART_Receive()
680
681 (#) Non-Blocking mode API's with Interrupt are :
682 (+) HAL_UART_Transmit_IT()
683 (+) HAL_UART_Receive_IT()
684 (+) HAL_UART_IRQHandler()
685 (+) UART_Transmit_IT()
686 (+) UART_Receive_IT()
687
688 (#) Non-Blocking mode API's with DMA are :
689 (+) HAL_UART_Transmit_DMA()
690 (+) HAL_UART_Receive_DMA()
691 (+) HAL_UART_DMAPause()
692 (+) HAL_UART_DMAResume()
693 (+) HAL_UART_DMAStop()
694
695 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
696 (+) HAL_UART_TxHalfCpltCallback()
697 (+) HAL_UART_TxCpltCallback()
698 (+) HAL_UART_RxHalfCpltCallback()
699 (+) HAL_UART_RxCpltCallback()
700 (+) HAL_UART_ErrorCallback()
701
702
703 -@- In the Half duplex communication, it is forbidden to run the transmit
704 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
705
706 @endverbatim
707 * @{
708 */
709
710 /**
711 * @brief Send an amount of data in blocking mode.
712 * @param huart: UART handle.
713 * @param pData: Pointer to data buffer.
714 * @param Size: Amount of data to be sent.
715 * @param Timeout: Timeout duration.
716 * @retval HAL status
717 */
718 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
719 {
720 uint16_t* tmp;
721 uint32_t tickstart = 0U;
722
723 /* Check that a Tx process is not already ongoing */
724 if(huart->gState == HAL_UART_STATE_READY)
725 {
726 if((pData == NULL ) || (Size == 0U))
727 {
728 return HAL_ERROR;
729 }
730
731 /* Process Locked */
732 __HAL_LOCK(huart);
733
734 huart->ErrorCode = HAL_UART_ERROR_NONE;
735 huart->gState = HAL_UART_STATE_BUSY_TX;
736
737 /* Init tickstart for timeout managment*/
738 tickstart = HAL_GetTick();
739
740 huart->TxXferSize = Size;
741 huart->TxXferCount = Size;
742 while(huart->TxXferCount > 0U)
743 {
744 huart->TxXferCount--;
745 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
746 {
747 return HAL_TIMEOUT;
748 }
749 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
750 {
751 tmp = (uint16_t*) pData;
752 huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
753 pData += 2;
754 }
755 else
756 {
757 huart->Instance->TDR = (*pData++ & (uint8_t)0xFFU);
758 }
759 }
760 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
761 {
762 return HAL_TIMEOUT;
763 }
764
765 /* At end of Tx process, restore huart->gState to Ready */
766 huart->gState = HAL_UART_STATE_READY;
767
768 /* Process Unlocked */
769 __HAL_UNLOCK(huart);
770
771 return HAL_OK;
772 }
773 else
774 {
775 return HAL_BUSY;
776 }
777 }
778
779 /**
780 * @brief Receive an amount of data in blocking mode.
781 * @param huart: UART handle.
782 * @param pData: pointer to data buffer.
783 * @param Size: amount of data to be received.
784 * @param Timeout: Timeout duration.
785 * @retval HAL status
786 */
787 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
788 {
789 uint16_t* tmp;
790 uint16_t uhMask;
791 uint32_t tickstart = 0U;
792
793 /* Check that a Rx process is not already ongoing */
794 if(huart->RxState == HAL_UART_STATE_READY)
795 {
796 if((pData == NULL ) || (Size == 0U))
797 {
798 return HAL_ERROR;
799 }
800
801 /* Process Locked */
802 __HAL_LOCK(huart);
803
804 huart->ErrorCode = HAL_UART_ERROR_NONE;
805 huart->RxState = HAL_UART_STATE_BUSY_RX;
806
807 /* Init tickstart for timeout managment*/
808 tickstart = HAL_GetTick();
809
810 huart->RxXferSize = Size;
811 huart->RxXferCount = Size;
812
813 /* Computation of UART mask to apply to RDR register */
814 UART_MASK_COMPUTATION(huart);
815 uhMask = huart->Mask;
816
817 /* as long as data have to be received */
818 while(huart->RxXferCount > 0U)
819 {
820 huart->RxXferCount--;
821 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
822 {
823 return HAL_TIMEOUT;
824 }
825 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
826 {
827 tmp = (uint16_t*) pData ;
828 *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
829 pData +=2U;
830 }
831 else
832 {
833 *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
834 }
835 }
836
837 /* At end of Rx process, restore huart->RxState to Ready */
838 huart->RxState = HAL_UART_STATE_READY;
839
840 /* Process Unlocked */
841 __HAL_UNLOCK(huart);
842
843 return HAL_OK;
844 }
845 else
846 {
847 return HAL_BUSY;
848 }
849 }
850
851 /**
852 * @brief Send an amount of data in interrupt mode.
853 * @param huart: UART handle.
854 * @param pData: pointer to data buffer.
855 * @param Size: amount of data to be sent.
856 * @retval HAL status
857 */
858 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
859 {
860 /* Check that a Tx process is not already ongoing */
861 if(huart->gState == HAL_UART_STATE_READY)
862 {
863 if((pData == NULL ) || (Size == 0U))
864 {
865 return HAL_ERROR;
866 }
867
868 /* Process Locked */
869 __HAL_LOCK(huart);
870
871 huart->pTxBuffPtr = pData;
872 huart->TxXferSize = Size;
873 huart->TxXferCount = Size;
874
875 huart->ErrorCode = HAL_UART_ERROR_NONE;
876 huart->gState = HAL_UART_STATE_BUSY_TX;
877
878 /* Process Unlocked */
879 __HAL_UNLOCK(huart);
880
881 /* Enable the UART Transmit Data Register Empty Interrupt */
882 SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
883
884 return HAL_OK;
885 }
886 else
887 {
888 return HAL_BUSY;
889 }
890 }
891
892 /**
893 * @brief Receive an amount of data in interrupt mode.
894 * @param huart: UART handle.
895 * @param pData: pointer to data buffer.
896 * @param Size: amount of data to be received.
897 * @retval HAL status
898 */
899 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
900 {
901 /* Check that a Rx process is not already ongoing */
902 if(huart->RxState == HAL_UART_STATE_READY)
903 {
904 if((pData == NULL ) || (Size == 0U))
905 {
906 return HAL_ERROR;
907 }
908
909 /* Process Locked */
910 __HAL_LOCK(huart);
911
912 huart->pRxBuffPtr = pData;
913 huart->RxXferSize = Size;
914 huart->RxXferCount = Size;
915
916 /* Computation of UART mask to apply to RDR register */
917 UART_MASK_COMPUTATION(huart);
918
919 huart->ErrorCode = HAL_UART_ERROR_NONE;
920 huart->RxState = HAL_UART_STATE_BUSY_RX;
921
922 /* Process Unlocked */
923 __HAL_UNLOCK(huart);
924
925 /* Enable the UART Parity Error Interrupt */
926 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
927
928 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
929 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
930
931 /* Enable the UART Data Register not empty Interrupt */
932 SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE);
933
934 return HAL_OK;
935 }
936 else
937 {
938 return HAL_BUSY;
939 }
940 }
941
942 /**
943 * @brief Send an amount of data in DMA mode.
944 * @param huart: UART handle.
945 * @param pData: pointer to data buffer.
946 * @param Size: amount of data to be sent.
947 * @retval HAL status
948 */
949 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
950 {
951 uint32_t *tmp;
952
953 /* Check that a Tx process is not already ongoing */
954 if(huart->gState == HAL_UART_STATE_READY)
955 {
956 if((pData == NULL ) || (Size == 0U))
957 {
958 return HAL_ERROR;
959 }
960
961 /* Process Locked */
962 __HAL_LOCK(huart);
963
964 huart->pTxBuffPtr = pData;
965 huart->TxXferSize = Size;
966 huart->TxXferCount = Size;
967
968 huart->ErrorCode = HAL_UART_ERROR_NONE;
969 huart->gState = HAL_UART_STATE_BUSY_TX;
970
971 /* Set the UART DMA transfer complete callback */
972 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
973
974 /* Set the UART DMA Half transfer complete callback */
975 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
976
977 /* Set the DMA error callback */
978 huart->hdmatx->XferErrorCallback = UART_DMAError;
979
980 /* Set the DMA abort callback */
981 huart->hdmatx->XferAbortCallback = NULL;
982
983 /* Enable the UART transmit DMA channel */
984 tmp = (uint32_t*)&pData;
985 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->TDR, Size);
986
987 /* Clear the TC flag in the SR register by writing 0 to it */
988 __HAL_UART_CLEAR_IT(huart, UART_FLAG_TC);
989
990 /* Process Unlocked */
991 __HAL_UNLOCK(huart);
992
993 /* Enable the DMA transfer for transmit request by setting the DMAT bit
994 in the UART CR3 register */
995 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
996
997 return HAL_OK;
998 }
999 else
1000 {
1001 return HAL_BUSY;
1002 }
1003 }
1004
1005 /**
1006 * @brief Receive an amount of data in DMA mode.
1007 * @param huart: UART handle.
1008 * @param pData: pointer to data buffer.
1009 * @param Size: amount of data to be received.
1010 * @note When the UART parity is enabled (PCE = 1), the received data contain
1011 * the parity bit (MSB position).
1012 * @retval HAL status
1013 */
1014 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1015 {
1016 uint32_t *tmp;
1017
1018 /* Check that a Rx process is not already ongoing */
1019 if(huart->RxState == HAL_UART_STATE_READY)
1020 {
1021 if((pData == NULL ) || (Size == 0U))
1022 {
1023 return HAL_ERROR;
1024 }
1025
1026 /* Process Locked */
1027 __HAL_LOCK(huart);
1028
1029 huart->pRxBuffPtr = pData;
1030 huart->RxXferSize = Size;
1031
1032 huart->ErrorCode = HAL_UART_ERROR_NONE;
1033 huart->RxState = HAL_UART_STATE_BUSY_RX;
1034
1035 /* Set the UART DMA transfer complete callback */
1036 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
1037
1038 /* Set the UART DMA Half transfer complete callback */
1039 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
1040
1041 /* Set the DMA error callback */
1042 huart->hdmarx->XferErrorCallback = UART_DMAError;
1043
1044 /* Set the DMA abort callback */
1045 huart->hdmarx->XferAbortCallback = NULL;
1046
1047 /* Enable the DMA channel */
1048 tmp = (uint32_t*)&pData;
1049 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, *(uint32_t*)tmp, Size);
1050
1051 /* Process Unlocked */
1052 __HAL_UNLOCK(huart);
1053
1054 /* Enable the UART Parity Error Interrupt */
1055 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1056
1057 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1058 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1059
1060 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1061 in the UART CR3 register */
1062 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1063
1064 return HAL_OK;
1065 }
1066 else
1067 {
1068 return HAL_BUSY;
1069 }
1070 }
1071
1072 /**
1073 * @brief Pause the DMA Transfer.
1074 * @param huart: UART handle.
1075 * @retval HAL status
1076 */
1077 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
1078 {
1079 /* Process Locked */
1080 __HAL_LOCK(huart);
1081
1082 if ((huart->gState == HAL_UART_STATE_BUSY_TX) &&
1083 (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)))
1084 {
1085 /* Disable the UART DMA Tx request */
1086 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1087 }
1088 if ((huart->RxState == HAL_UART_STATE_BUSY_RX) &&
1089 (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)))
1090 {
1091 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1092 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1093 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1094
1095 /* Disable the UART DMA Rx request */
1096 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1097 }
1098
1099 /* Process Unlocked */
1100 __HAL_UNLOCK(huart);
1101
1102 return HAL_OK;
1103 }
1104
1105 /**
1106 * @brief Resume the DMA Transfer.
1107 * @param huart: UART handle.
1108 * @retval HAL status
1109 */
1110 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
1111 {
1112 /* Process Locked */
1113 __HAL_LOCK(huart);
1114
1115 if(huart->gState == HAL_UART_STATE_BUSY_TX)
1116 {
1117 /* Enable the UART DMA Tx request */
1118 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1119 }
1120 if(huart->RxState == HAL_UART_STATE_BUSY_RX)
1121 {
1122 /* Clear the Overrun flag before resuming the Rx transfer*/
1123 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
1124
1125 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1126 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1127 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1128
1129 /* Enable the UART DMA Rx request */
1130 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1131 }
1132
1133 /* If the UART peripheral is still not enabled, enable it */
1134 if ((huart->Instance->CR1 & USART_CR1_UE) == 0U)
1135 {
1136 /* Enable UART peripheral */
1137 __HAL_UART_ENABLE(huart);
1138 }
1139
1140 return HAL_OK;
1141 }
1142
1143 /**
1144 * @brief Stop the DMA Transfer.
1145 * @param huart: UART handle.
1146 * @retval HAL status
1147 */
1148 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
1149 {
1150 /* The Lock is not implemented on this API to allow the user application
1151 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
1152 HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
1153 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1154 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1155 the stream and the corresponding call back is executed. */
1156
1157 /* Stop UART DMA Tx request if ongoing */
1158 if ((huart->gState == HAL_UART_STATE_BUSY_TX) &&
1159 (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)))
1160 {
1161 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1162
1163 /* Abort the UART DMA Tx channel */
1164 if(huart->hdmatx != NULL)
1165 {
1166 HAL_DMA_Abort(huart->hdmatx);
1167 }
1168
1169 UART_EndTxTransfer(huart);
1170 }
1171
1172 /* Stop UART DMA Rx request if ongoing */
1173 if ((huart->RxState == HAL_UART_STATE_BUSY_RX) &&
1174 (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)))
1175 {
1176 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1177
1178 /* Abort the UART DMA Rx channel */
1179 if(huart->hdmarx != NULL)
1180 {
1181 HAL_DMA_Abort(huart->hdmarx);
1182 }
1183
1184 UART_EndRxTransfer(huart);
1185 }
1186
1187 return HAL_OK;
1188 }
1189
1190 /**
1191 * @brief This function handles UART interrupt request.
1192 * @param huart: uart handle
1193 * @retval None
1194 */
1195 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
1196 {
1197 uint32_t isrflags = READ_REG(huart->Instance->ISR);
1198 uint32_t cr1its = READ_REG(huart->Instance->CR1);
1199 uint32_t cr3its = READ_REG(huart->Instance->CR3);
1200 uint32_t errorflags;
1201
1202 /* If no error occurs */
1203 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
1204 if (errorflags == RESET)
1205 {
1206 /* UART in mode Receiver ---------------------------------------------------*/
1207 if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1208 {
1209 UART_Receive_IT(huart);
1210 return;
1211 }
1212 }
1213
1214 /* If some errors occur */
1215 if((errorflags != RESET) && ((cr3its & (USART_CR3_EIE | USART_CR1_PEIE)) != RESET))
1216 {
1217
1218 /* UART parity error interrupt occurred -------------------------------------*/
1219 if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1220 {
1221 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_PEF);
1222
1223 huart->ErrorCode |= HAL_UART_ERROR_PE;
1224 }
1225
1226 /* UART frame error interrupt occurred --------------------------------------*/
1227 if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1228 {
1229 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_FEF);
1230
1231 huart->ErrorCode |= HAL_UART_ERROR_FE;
1232 }
1233
1234 /* UART noise error interrupt occurred --------------------------------------*/
1235 if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1236 {
1237 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_NEF);
1238
1239 huart->ErrorCode |= HAL_UART_ERROR_NE;
1240 }
1241
1242 /* UART Over-Run interrupt occurred -----------------------------------------*/
1243 if(((isrflags & USART_ISR_ORE) != RESET) &&
1244 (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
1245 {
1246 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
1247
1248 huart->ErrorCode |= HAL_UART_ERROR_ORE;
1249 }
1250
1251 /* Call UART Error Call back function if need be --------------------------*/
1252 if(huart->ErrorCode != HAL_UART_ERROR_NONE)
1253 {
1254 /* UART in mode Receiver ---------------------------------------------------*/
1255 if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1256 {
1257 UART_Receive_IT(huart);
1258 }
1259
1260 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1261 consider error as blocking */
1262 if (((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) ||
1263 (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)))
1264 {
1265 /* Blocking error : transfer is aborted
1266 Set the UART state ready to be able to start again the process,
1267 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1268 UART_EndRxTransfer(huart);
1269
1270 /* Disable the UART DMA Rx request if enabled */
1271 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1272 {
1273 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1274
1275 /* Abort the UART DMA Rx channel */
1276 if(huart->hdmarx != NULL)
1277 {
1278 /* Set the UART DMA Abort callback :
1279 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
1280 huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
1281
1282 /* Abort DMA RX */
1283 if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1284 {
1285 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
1286 huart->hdmarx->XferAbortCallback(huart->hdmarx);
1287 }
1288 }
1289 else
1290 {
1291 /* Call user error callback */
1292 HAL_UART_ErrorCallback(huart);
1293 }
1294 }
1295 else
1296 {
1297 /* Call user error callback */
1298 HAL_UART_ErrorCallback(huart);
1299 }
1300 }
1301 else
1302 {
1303 /* Non Blocking error : transfer could go on.
1304 Error is notified to user through user error callback */
1305 HAL_UART_ErrorCallback(huart);
1306 huart->ErrorCode = HAL_UART_ERROR_NONE;
1307 }
1308 }
1309 return;
1310
1311 } /* End if some error occurs */
1312
1313 /* UART in mode Transmitter ------------------------------------------------*/
1314 if(((isrflags & USART_ISR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1315 {
1316 UART_Transmit_IT(huart);
1317 return;
1318 }
1319
1320 /* UART in mode Transmitter (transmission end) -----------------------------*/
1321 if(((isrflags & USART_ISR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
1322 {
1323 UART_EndTransmit_IT(huart);
1324 return;
1325 }
1326
1327 }
1328
1329 /**
1330 * @brief This function handles UART Communication Timeout.
1331 * @param huart UART handle
1332 * @param Flag specifies the UART flag to check.
1333 * @param Status The new Flag status (SET or RESET).
1334 * @param Tickstart Tick start value
1335 * @param Timeout Timeout duration
1336 * @retval HAL status
1337 */
1338 HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
1339 {
1340 /* Wait until flag is set */
1341 while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
1342 {
1343 /* Check for the Timeout */
1344 if(Timeout != HAL_MAX_DELAY)
1345 {
1346 if((Timeout == 0U)||((HAL_GetTick()-Tickstart) >= Timeout))
1347 {
1348 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1349 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
1350 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1351
1352 huart->gState = HAL_UART_STATE_READY;
1353 huart->RxState = HAL_UART_STATE_READY;
1354
1355 /* Process Unlocked */
1356 __HAL_UNLOCK(huart);
1357 return HAL_TIMEOUT;
1358 }
1359 }
1360 }
1361 return HAL_OK;
1362 }
1363
1364 /**
1365 * @brief DMA UART transmit process complete callback
1366 * @param hdma: DMA handle
1367 * @retval None
1368 */
1369 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1370 {
1371 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1372
1373 /* DMA Normal mode*/
1374 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
1375 {
1376 huart->TxXferCount = 0U;
1377
1378 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1379 in the UART CR3 register */
1380 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1381
1382 /* Enable the UART Transmit Complete Interrupt */
1383 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
1384 }
1385 /* DMA Circular mode */
1386 else
1387 {
1388 HAL_UART_TxCpltCallback(huart);
1389 }
1390 }
1391
1392 /**
1393 * @brief DMA UART transmit process half complete callback
1394 * @param hdma : DMA handle
1395 * @retval None
1396 */
1397 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
1398 {
1399 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1400
1401 HAL_UART_TxHalfCpltCallback(huart);
1402 }
1403
1404 /**
1405 * @brief DMA UART receive process complete callback
1406 * @param hdma: DMA handle
1407 * @retval None
1408 */
1409 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1410 {
1411 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1412
1413 /* DMA Normal mode */
1414 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
1415 {
1416 huart->RxXferCount = 0U;
1417
1418 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1419 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1420 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1421
1422 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1423 in the UART CR3 register */
1424 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1425
1426 /* At end of Rx process, restore huart->RxState to Ready */
1427 huart->RxState = HAL_UART_STATE_READY;
1428 }
1429 HAL_UART_RxCpltCallback(huart);
1430 }
1431
1432 /**
1433 * @brief DMA UART receive process half complete callback
1434 * @param hdma : DMA handle
1435 * @retval None
1436 */
1437 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
1438 {
1439 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1440
1441 HAL_UART_RxHalfCpltCallback(huart);
1442 }
1443
1444 /**
1445 * @brief DMA UART communication error callback
1446 * @param hdma: DMA handle
1447 * @retval None
1448 */
1449 static void UART_DMAError(DMA_HandleTypeDef *hdma)
1450 {
1451 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1452 huart->RxXferCount = 0U;
1453 huart->TxXferCount = 0U;
1454 /* Stop UART DMA Tx request if ongoing */
1455 if ( (huart->gState == HAL_UART_STATE_BUSY_TX)
1456 &&(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) )
1457 {
1458 UART_EndTxTransfer(huart);
1459 }
1460
1461 /* Stop UART DMA Rx request if ongoing */
1462 if ( (huart->RxState == HAL_UART_STATE_BUSY_RX)
1463 &&(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) )
1464 {
1465 UART_EndRxTransfer(huart);
1466 }
1467 SET_BIT(huart->ErrorCode, HAL_UART_ERROR_DMA);
1468 HAL_UART_ErrorCallback(huart);
1469 }
1470
1471 /**
1472 * @brief DMA UART communication abort callback, when call by HAL services on Error
1473 * (To be called at end of DMA Abort procedure following error occurrence).
1474 * @param hdma: DMA handle.
1475 * @retval None
1476 */
1477 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
1478 {
1479 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)(hdma->Parent);
1480 huart->RxXferCount = 0U;
1481 huart->TxXferCount = 0U;
1482
1483 HAL_UART_ErrorCallback(huart);
1484 }
1485
1486 /**
1487 * @brief Tx Transfer completed callbacks
1488 * @param huart: uart handle
1489 * @retval None
1490 */
1491 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
1492 {
1493 /* Prevent unused argument(s) compilation warning */
1494 UNUSED(huart);
1495
1496 /* NOTE : This function should not be modified, when the callback is needed,
1497 the HAL_UART_TxCpltCallback can be implemented in the user file
1498 */
1499 }
1500
1501 /**
1502 * @brief Tx Half Transfer completed callbacks.
1503 * @param huart: UART handle
1504 * @retval None
1505 */
1506 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
1507 {
1508 /* Prevent unused argument(s) compilation warning */
1509 UNUSED(huart);
1510
1511 /* NOTE: This function should not be modified, when the callback is needed,
1512 the HAL_UART_TxHalfCpltCallback can be implemented in the user file
1513 */
1514 }
1515
1516 /**
1517 * @brief Rx Transfer completed callbacks
1518 * @param huart: uart handle
1519 * @retval None
1520 */
1521 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
1522 {
1523 /* Prevent unused argument(s) compilation warning */
1524 UNUSED(huart);
1525
1526 /* NOTE : This function should not be modified, when the callback is needed,
1527 the HAL_UART_RxCpltCallback can be implemented in the user file
1528 */
1529 }
1530
1531 /**
1532 * @brief Rx Half Transfer completed callbacks.
1533 * @param huart: UART handle
1534 * @retval None
1535 */
1536 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
1537 {
1538 /* Prevent unused argument(s) compilation warning */
1539 UNUSED(huart);
1540
1541 /* NOTE: This function should not be modified, when the callback is needed,
1542 the HAL_UART_RxHalfCpltCallback can be implemented in the user file
1543 */
1544 }
1545
1546 /**
1547 * @brief UART error callbacks
1548 * @param huart: uart handle
1549 * @retval None
1550 */
1551 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
1552 {
1553 /* Prevent unused argument(s) compilation warning */
1554 UNUSED(huart);
1555
1556 /* NOTE : This function should not be modified, when the callback is needed,
1557 the HAL_UART_ErrorCallback can be implemented in the user file
1558 */
1559 }
1560
1561 /**
1562 * @brief Send an amount of data in interrupt mode
1563 * Function called under interruption only, once
1564 * interruptions have been enabled by HAL_UART_Transmit_IT()
1565 * @param huart: UART handle
1566 * @retval HAL status
1567 */
1568 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
1569 {
1570 uint16_t* tmp;
1571
1572 /* Check that a Tx process is ongoing */
1573 if (huart->gState == HAL_UART_STATE_BUSY_TX)
1574 {
1575
1576 if(huart->TxXferCount == 0U)
1577 {
1578 /* Disable the UART Transmit Data Register Empty Interrupt */
1579 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
1580
1581 /* Enable the UART Transmit Complete Interrupt */
1582 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
1583
1584 return HAL_OK;
1585 }
1586 else
1587 {
1588 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1589 {
1590 tmp = (uint16_t*) huart->pTxBuffPtr;
1591 huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
1592 huart->pTxBuffPtr += 2U;
1593 }
1594 else
1595 {
1596 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0xFFU);
1597 }
1598
1599 huart->TxXferCount--;
1600
1601 return HAL_OK;
1602 }
1603 }
1604 else
1605 {
1606 return HAL_BUSY;
1607 }
1608 }
1609
1610 /**
1611 * @brief Wrap up transmission in non-blocking mode.
1612 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1613 * the configuration information for the specified UART module.
1614 * @retval HAL status
1615 */
1616 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
1617 {
1618 /* Disable the UART Transmit Complete Interrupt */
1619 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
1620
1621 /* Tx process is ended, restore huart->gState to Ready */
1622 huart->gState = HAL_UART_STATE_READY;
1623
1624 HAL_UART_TxCpltCallback(huart);
1625
1626 return HAL_OK;
1627 }
1628
1629 /**
1630 * @brief Receive an amount of data in interrupt mode
1631 * Function called under interruption only, once
1632 * interruptions have been enabled by HAL_UART_Receive_IT()
1633 * @param huart: UART handle
1634 * @retval HAL status
1635 */
1636 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
1637 {
1638 uint16_t* tmp;
1639 uint16_t uhMask = huart->Mask;
1640
1641 /* Check that a Rx process is ongoing */
1642 if(huart->RxState == HAL_UART_STATE_BUSY_RX)
1643 {
1644
1645 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1646 {
1647 tmp = (uint16_t*) huart->pRxBuffPtr ;
1648 *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
1649 huart->pRxBuffPtr +=2;
1650 }
1651 else
1652 {
1653 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
1654 }
1655
1656 if(--huart->RxXferCount == 0)
1657 {
1658 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
1659 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1660
1661 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1662 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1663
1664 /* Rx process is completed, restore huart->RxState to Ready */
1665 huart->RxState = HAL_UART_STATE_READY;
1666
1667 HAL_UART_RxCpltCallback(huart);
1668
1669 return HAL_OK;
1670 }
1671
1672 return HAL_OK;
1673 }
1674 else
1675 {
1676 /* Clear RXNE interrupt flag */
1677 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1678
1679 return HAL_BUSY;
1680 }
1681 }
1682
1683 /**
1684 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
1685 * @param huart: UART handle.
1686 * @retval None
1687 */
1688 static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
1689 {
1690 /* Disable TXEIE and TCIE interrupts */
1691 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1692
1693 /* At end of Tx process, restore huart->gState to Ready */
1694 huart->gState = HAL_UART_STATE_READY;
1695 }
1696
1697
1698 /**
1699 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
1700 * @param huart: UART handle.
1701 * @retval None
1702 */
1703 static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
1704 {
1705 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1706 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1707 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1708
1709 /* At end of Rx process, restore huart->RxState to Ready */
1710 huart->RxState = HAL_UART_STATE_READY;
1711 }
1712
1713 /**
1714 * @}
1715 */
1716
1717 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
1718 * @brief UART control functions
1719 *
1720 @verbatim
1721 ===============================================================================
1722 ##### Peripheral Control functions #####
1723 ===============================================================================
1724 [..]
1725 This subsection provides a set of functions allowing to control the UART.
1726 (+) HAL_UART_GetState() API is helpful to check in run-time the state of the UART peripheral.
1727 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
1728 (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
1729 (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
1730 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
1731 (+) UART_SetConfig() API configures the UART peripheral
1732 (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
1733 (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
1734 (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
1735 (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
1736 (+) HAL_LIN_SendBreak() API transmits the break characters
1737 (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
1738 detection length to more than 4 bits for multiprocessor address mark wake up.
1739 @endverbatim
1740 * @{
1741 */
1742
1743 /**
1744 * @brief Enable UART in mute mode (doesn't mean UART enters mute mode;
1745 * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called)
1746 * @param huart: UART handle
1747 * @retval HAL status
1748 */
1749 HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
1750 {
1751 /* Process Locked */
1752 __HAL_LOCK(huart);
1753
1754 huart->gState = HAL_UART_STATE_BUSY;
1755
1756 /* Enable USART mute mode by setting the MME bit in the CR1 register */
1757 SET_BIT(huart->Instance->CR1, USART_CR1_MME);
1758
1759 huart->gState = HAL_UART_STATE_READY;
1760
1761 return (UART_CheckIdleState(huart));
1762 }
1763
1764 /**
1765 * @brief Disable UART mute mode (doesn't mean it actually wakes up the software,
1766 * as it may not have been in mute mode at this very moment).
1767 * @param huart: uart handle
1768 * @retval HAL status
1769 */
1770 HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
1771 {
1772 /* Process Locked */
1773 __HAL_LOCK(huart);
1774
1775 huart->gState = HAL_UART_STATE_BUSY;
1776
1777 /* Disable USART mute mode by clearing the MME bit in the CR1 register */
1778 CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
1779
1780 huart->gState = HAL_UART_STATE_READY;
1781
1782 return (UART_CheckIdleState(huart));
1783 }
1784
1785 /**
1786 * @brief Enter UART mute mode (means UART actually enters mute mode).
1787 * To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
1788 * @param huart: uart handle
1789 * @retval HAL status
1790 */
1791 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
1792 {
1793 __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
1794 }
1795
1796
1797
1798 /**
1799 * @brief return the UART state
1800 * @param huart: uart handle
1801 * @retval HAL state
1802 */
1803 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
1804 {
1805 uint32_t temp1= 0x00U, temp2 = 0x00U;
1806 temp1 = huart->gState;
1807 temp2 = huart->RxState;
1808
1809 return (HAL_UART_StateTypeDef)(temp1 | temp2);
1810 }
1811
1812 /**
1813 * @brief Return the UART error code
1814 * @param huart : pointer to a UART_HandleTypeDef structure that contains
1815 * the configuration information for the specified UART.
1816 * @retval UART Error Code
1817 */
1818 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
1819 {
1820 return huart->ErrorCode;
1821 }
1822
1823 /**
1824 * @brief Configure the UART peripheral
1825 * @param huart: uart handle
1826 * @retval None
1827 */
1828 HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
1829 {
1830 uint32_t tmpreg = 0x00000000U;
1831 UART_ClockSourceTypeDef clocksource = UART_CLOCKSOURCE_UNDEFINED;
1832 uint16_t brrtemp = 0x0000U;
1833 uint16_t usartdiv = 0x0000U;
1834 HAL_StatusTypeDef ret = HAL_OK;
1835
1836 /* Check the parameters */
1837 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
1838 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
1839 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
1840 assert_param(IS_UART_PARITY(huart->Init.Parity));
1841 assert_param(IS_UART_MODE(huart->Init.Mode));
1842 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
1843 assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
1844
1845
1846 /*-------------------------- USART CR1 Configuration -----------------------*/
1847 /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
1848 * the UART Word Length, Parity, Mode and oversampling:
1849 * set the M bits according to huart->Init.WordLength value
1850 * set PCE and PS bits according to huart->Init.Parity value
1851 * set TE and RE bits according to huart->Init.Mode value
1852 * set OVER8 bit according to huart->Init.OverSampling value */
1853 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
1854 MODIFY_REG(huart->Instance->CR1, UART_CR1_FIELDS, tmpreg);
1855
1856 /*-------------------------- USART CR2 Configuration -----------------------*/
1857 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
1858 * to huart->Init.StopBits value */
1859 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
1860
1861 /*-------------------------- USART CR3 Configuration -----------------------*/
1862 /* Configure
1863 * - UART HardWare Flow Control: set CTSE and RTSE bits according
1864 * to huart->Init.HwFlowCtl value
1865 * - one-bit sampling method versus three samples' majority rule according
1866 * to huart->Init.OneBitSampling */
1867 tmpreg = (uint32_t)huart->Init.HwFlowCtl | huart->Init.OneBitSampling ;
1868 MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT), tmpreg);
1869
1870 /*-------------------------- USART BRR Configuration -----------------------*/
1871 UART_GETCLOCKSOURCE(huart, clocksource);
1872
1873 /* Check UART Over Sampling to set Baud Rate Register */
1874 if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
1875 {
1876 switch (clocksource)
1877 {
1878 case UART_CLOCKSOURCE_PCLK1:
1879 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
1880 break;
1881 case UART_CLOCKSOURCE_PCLK2:
1882 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
1883 break;
1884 case UART_CLOCKSOURCE_HSI:
1885 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
1886 break;
1887 case UART_CLOCKSOURCE_SYSCLK:
1888 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
1889 break;
1890 case UART_CLOCKSOURCE_LSE:
1891 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
1892 break;
1893 case UART_CLOCKSOURCE_UNDEFINED:
1894 default:
1895 ret = HAL_ERROR;
1896 break;
1897 }
1898
1899 brrtemp = usartdiv & 0xFFF0U;
1900 brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
1901 huart->Instance->BRR = brrtemp;
1902 }
1903 else
1904 {
1905 switch (clocksource)
1906 {
1907 case UART_CLOCKSOURCE_PCLK1:
1908 huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
1909 break;
1910 case UART_CLOCKSOURCE_PCLK2:
1911 huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
1912 break;
1913 case UART_CLOCKSOURCE_HSI:
1914 huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
1915 break;
1916 case UART_CLOCKSOURCE_SYSCLK:
1917 huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
1918 break;
1919 case UART_CLOCKSOURCE_LSE:
1920 huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
1921 break;
1922 case UART_CLOCKSOURCE_UNDEFINED:
1923 default:
1924 ret = HAL_ERROR;
1925 break;
1926 }
1927 }
1928
1929 return ret;
1930
1931 }
1932
1933
1934 /**
1935 * @brief Configure the UART peripheral advanced features
1936 * @param huart: uart handle
1937 * @retval None
1938 */
1939 void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
1940 {
1941 /* Check whether the set of advanced features to configure is properly set */
1942 assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
1943
1944 /* if required, configure TX pin active level inversion */
1945 if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
1946 {
1947 assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
1948 MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
1949 }
1950
1951 /* if required, configure RX pin active level inversion */
1952 if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
1953 {
1954 assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
1955 MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
1956 }
1957
1958 /* if required, configure data inversion */
1959 if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
1960 {
1961 assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
1962 MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
1963 }
1964
1965 /* if required, configure RX/TX pins swap */
1966 if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
1967 {
1968 assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
1969 MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
1970 }
1971
1972 /* if required, configure RX overrun detection disabling */
1973 if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
1974 {
1975 assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
1976 MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
1977 }
1978
1979 /* if required, configure DMA disabling on reception error */
1980 if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
1981 {
1982 assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
1983 MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
1984 }
1985
1986 /* if required, configure auto Baud rate detection scheme */
1987 if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
1988 {
1989 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
1990 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
1991 /* set auto Baudrate detection parameters if detection is enabled */
1992 if(huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
1993 {
1994 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
1995 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
1996 }
1997 }
1998
1999 /* if required, configure MSB first on communication line */
2000 if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
2001 {
2002 assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
2003 MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
2004 }
2005 }
2006
2007
2008
2009 /**
2010 * @brief Check the UART Idle State
2011 * @param huart: uart handle
2012 * @retval HAL status
2013 */
2014 HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
2015 {
2016 uint32_t tickstart = 0U;
2017
2018 /* Initialize the UART ErrorCode */
2019 huart->ErrorCode = HAL_UART_ERROR_NONE;
2020
2021 /* Init tickstart for timeout managment*/
2022 tickstart = HAL_GetTick();
2023
2024 /* Check if the Transmitter is enabled */
2025 if((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
2026 {
2027 /* Wait until TEACK flag is set */
2028 if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
2029 {
2030 /* Timeout Occurred */
2031 return HAL_TIMEOUT;
2032 }
2033 }
2034 /* Check if the Receiver is enabled */
2035 if((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
2036 {
2037 /* Wait until REACK flag is set */
2038 if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
2039 {
2040 /* Timeout Occurred */
2041 return HAL_TIMEOUT;
2042 }
2043 }
2044
2045 /* Initialize the UART State */
2046 huart->gState= HAL_UART_STATE_READY;
2047 huart->RxState= HAL_UART_STATE_READY;
2048
2049 /* Process Unlocked */
2050 __HAL_UNLOCK(huart);
2051
2052 return HAL_OK;
2053 }
2054
2055 /**
2056 * @brief Enables the UART transmitter and disables the UART receiver.
2057 * @param huart: UART handle
2058 * @retval HAL status
2059 * @retval None
2060 */
2061 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
2062 {
2063 /* Process Locked */
2064 __HAL_LOCK(huart);
2065 huart->gState = HAL_UART_STATE_BUSY;
2066
2067 /* Clear TE and RE bits */
2068 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2069 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
2070 SET_BIT(huart->Instance->CR1, USART_CR1_TE);
2071
2072 huart->gState= HAL_UART_STATE_READY;
2073 /* Process Unlocked */
2074 __HAL_UNLOCK(huart);
2075
2076 return HAL_OK;
2077 }
2078
2079 /**
2080 * @brief Enables the UART receiver and disables the UART transmitter.
2081 * @param huart: UART handle
2082 * @retval HAL status
2083 */
2084 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
2085 {
2086 /* Process Locked */
2087 __HAL_LOCK(huart);
2088 huart->gState = HAL_UART_STATE_BUSY;
2089
2090 /* Clear TE and RE bits */
2091 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2092 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
2093 SET_BIT(huart->Instance->CR1, USART_CR1_RE);
2094
2095 huart->gState = HAL_UART_STATE_READY;
2096 /* Process Unlocked */
2097 __HAL_UNLOCK(huart);
2098
2099 return HAL_OK;
2100 }
2101
2102
2103 /**
2104 * @brief Transmits break characters.
2105 * @param huart: UART handle
2106 * @retval HAL status
2107 */
2108 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
2109 {
2110 /* Check the parameters */
2111 assert_param(IS_UART_INSTANCE(huart->Instance));
2112
2113 /* Process Locked */
2114 __HAL_LOCK(huart);
2115
2116 huart->gState = HAL_UART_STATE_BUSY;
2117
2118 /* Send break characters */
2119 SET_BIT(huart->Instance->RQR, UART_SENDBREAK_REQUEST);
2120
2121 huart->gState = HAL_UART_STATE_READY;
2122
2123 /* Process Unlocked */
2124 __HAL_UNLOCK(huart);
2125
2126 return HAL_OK;
2127 }
2128
2129 /**
2130 * @brief By default in multiprocessor mode, when the wake up method is set
2131 * to address mark, the UART handles only 4-bit long addresses detection;
2132 * this API allows to enable longer addresses detection (6-, 7- or 8-bit
2133 * long).
2134 * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
2135 * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
2136 * @param huart: UART handle.
2137 * @param AddressLength: this parameter can be one of the following values:
2138 * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
2139 * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
2140 * @retval HAL status
2141 */
2142 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
2143 {
2144 /* Check the UART handle allocation */
2145 if(huart == NULL)
2146 {
2147 return HAL_ERROR;
2148 }
2149
2150 /* Check the address length parameter */
2151 assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
2152
2153 huart->gState = HAL_UART_STATE_BUSY;
2154
2155 /* Disable the Peripheral */
2156 __HAL_UART_DISABLE(huart);
2157
2158 /* Set the address length */
2159 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
2160
2161 /* Enable the Peripheral */
2162 __HAL_UART_ENABLE(huart);
2163
2164 /* TEACK and/or REACK to check before moving huart->gState to Ready */
2165 return (UART_CheckIdleState(huart));
2166 }
2167
2168 /**
2169 * @}
2170 */
2171
2172 /**
2173 * @}
2174 */
2175
2176 #endif /* HAL_UART_MODULE_ENABLED */
2177 /**
2178 * @}
2179 */
2180
2181 /**
2182 * @}
2183 */
2184
2185 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/