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