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