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