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