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