]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_irda.c
Merge commit '4d116a04e94cf0d19317d5b44e4fa9f34a3e5594'
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_irda.c
1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_hal_irda.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    12-Sept-2014
7   * @brief   IRDA HAL module driver.
8   * 
9   *          This file provides firmware functions to manage the following 
10   *          functionalities of the IrDA (Infrared Data Association) Peripheral 
11   *          (IRDA)
12   *           + Initialization and de-initialization functions
13   *           + IO operation functions
14   *           + Peripheral Control functions
15   *
16   *           
17   @verbatim       
18  ===============================================================================
19                         ##### How to use this driver #####
20  ===============================================================================
21     [..]
22     The IRDA HAL driver can be used as follows:
23     
24     (#) Declare a IRDA_HandleTypeDef handle structure.
25     (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API
26         in setting the associated USART or UART in IRDA mode:
27         (##) Enable the USARTx/UARTx interface clock.
28         (##) USARTx/UARTx pins configuration:
29             (+) Enable the clock for the USARTx/UARTx GPIOs.
30             (+) Configure these USARTx/UARTx pins as alternate function pull-up.
31         (##) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
32              and HAL_IRDA_Receive_IT() APIs):
33             (+) Configure the USARTx/UARTx interrupt priority.
34             (+) Enable the NVIC IRDA IRQ handle.
35             (@) The specific IRDA interrupts (Transmission complete interrupt, 
36                 RXNE interrupt and Error Interrupts) will be managed using the macros
37                 __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
38         (##) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
39              and HAL_IRDA_Receive_DMA() APIs):
40             (+) Declare a DMA handle structure for the Tx/Rx stream.
41             (+) Enable the DMAx interface clock.
42             (+) Configure the declared DMA handle structure with the required Tx/Rx parameters.                
43             (+) Configure the DMA Tx/Rx Stream.
44             (+) Associate the initilalized DMA handle to the IRDA DMA Tx/Rx handle.
45             (+) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
46
47     (#) Program the Baud Rate, Word Length and Parity and Mode(Receiver/Transmitter),
48         the normal or low power mode and the clock prescaler in the hirda Init structure.
49
50     (#) Initialize the IRDA registers by calling
51         the HAL_IRDA_Init() API.
52         
53     (@) This API (HAL_IRDA_Init()) configures also the low level Hardware (GPIO, CLOCK, CORTEX...etc)
54         by calling the customized HAL_IRDA_MspInit() API.
55
56   @endverbatim
57   ******************************************************************************
58   * @attention
59   *
60   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
61   *
62   * Redistribution and use in source and binary forms, with or without modification,
63   * are permitted provided that the following conditions are met:
64   *   1. Redistributions of source code must retain the above copyright notice,
65   *      this list of conditions and the following disclaimer.
66   *   2. Redistributions in binary form must reproduce the above copyright notice,
67   *      this list of conditions and the following disclaimer in the documentation
68   *      and/or other materials provided with the distribution.
69   *   3. Neither the name of STMicroelectronics nor the names of its contributors
70   *      may be used to endorse or promote products derived from this software
71   *      without specific prior written permission.
72   *
73   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
76   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
77   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
79   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
80   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
81   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
82   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83   *
84   ******************************************************************************  
85   */
86
87 /* Includes ------------------------------------------------------------------*/
88 #include "stm32f3xx_hal.h"
89
90 /** @addtogroup STM32F3xx_HAL_Driver
91   * @{
92   */
93
94 /** @defgroup IRDA IRDA HAL module driver
95   * @brief HAL IRDA module driver
96   * @{
97   */
98 #ifdef HAL_IRDA_MODULE_ENABLED
99     
100 /* Private typedef -----------------------------------------------------------*/
101 /* Private define ------------------------------------------------------------*/
102 /** @defgroup IRDA_Private_Constants   IRDA Private Constants
103   * @{
104   */
105 #define TEACK_REACK_TIMEOUT            1000
106 #define IRDA_TXDMA_TIMEOUTVALUE        22000
107 #define IRDA_TIMEOUT_VALUE             22000
108 #define IRDA_CR1_FIELDS  ((uint32_t)(USART_CR1_M | USART_CR1_PCE \
109                                    | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE))
110 /**
111   * @}
112   */
113
114 /* Private macro -------------------------------------------------------------*/
115 /* Private variables ---------------------------------------------------------*/
116 /* Private function prototypes -----------------------------------------------*/
117 /** @defgroup IRDA_Private_Functions IRDA Private Functions
118   * @{
119   */
120 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
121 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
122 static void IRDA_DMAError(DMA_HandleTypeDef *hdma); 
123 static HAL_StatusTypeDef IRDA_SetConfig(IRDA_HandleTypeDef *hirda);
124 static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda);
125 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
126 static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
127 static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda);
128 static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
129 /**
130   * @}
131   */
132 /* Exported functions ---------------------------------------------------------*/
133
134 /** @defgroup IRDA_Exported_Functions IRDA Exported Functions
135   * @{
136   */
137
138 /** @defgroup IRDA_Exported_Functions_Group1 Initialization and de-initialization functions 
139   *  @brief    Initialization and Configuration functions 
140   *
141 @verbatim    
142   ==============================================================================
143             ##### Initialization and Configuration functions #####
144   ==============================================================================
145     [..]
146     This subsection provides a set of functions allowing to initialize the USARTx 
147     in asynchronous IRDA mode.
148       (+) For the asynchronous mode only these parameters can be configured: 
149         (++) Baud Rate
150         (++) Word Length 
151         (++) Parity: If the parity is enabled, then the MSB bit of the data written
152              in the data register is transmitted but is changed by the parity bit.
153              Depending on the frame length defined by the M bit (8-bits or 9-bits)
154              or by the M1 and M0 bits (7-bit, 8-bit or 9-bit),
155              the possible IRDA frame formats are as listed in the following table:
156    +---------------------------------------------------------------+     
157    |    M bit  |  PCE bit  |            IRDA frame                 |
158    |-----------|-----------|---------------------------------------|             
159    |     0     |     0     |    | SB | 8-bit data | STB |          |
160    |-----------|-----------|---------------------------------------|  
161    |     0     |     1     |    | SB | 7-bit data | PB | STB |     |
162    |-----------|-----------|---------------------------------------|  
163    |     1     |     0     |    | SB | 9-bit data | STB |          |
164    |-----------|-----------|---------------------------------------|  
165    |     1     |     1     |    | SB | 8-bit data | PB | STB |     |
166    +---------------------------------------------------------------+     
167    | M1M0 bits |  PCE bit  |            IRDA frame                 |
168    |-----------------------|---------------------------------------|             
169    |     10    |     0     |    | SB | 7-bit data | STB |          |
170    |-----------|-----------|---------------------------------------|  
171    |     10    |     1     |    | SB | 6-bit data | PB | STB |     |   
172    +---------------------------------------------------------------+                   
173           
174         (++) Power mode
175         (++) Prescaler setting       
176         (++) Receiver/transmitter modes
177
178     [..]
179     The HAL_IRDA_Init() API follows the USART asynchronous configuration procedures 
180     (details for the procedures are available in reference manual).
181
182 @endverbatim
183   * @{
184   */
185
186 /**
187   * @brief Initializes the IRDA mode according to the specified
188   *         parameters in the IRDA_InitTypeDef and creates the associated handle .
189   * @param hirda: IRDA handle
190   * @retval HAL status
191   */
192 HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
193 {
194   /* Check the IRDA handle allocation */
195   if(hirda == HAL_NULL)
196   {
197     return HAL_ERROR;
198   }
199   
200   /* Check the USART/UART associated to the IRDA handle */
201   assert_param(IS_IRDA_INSTANCE(hirda->Instance));
202   
203   if(hirda->State == HAL_IRDA_STATE_RESET)
204   {   
205     /* Init the low level hardware : GPIO, CLOCK */
206     HAL_IRDA_MspInit(hirda);
207   }
208   
209   hirda->State = HAL_IRDA_STATE_BUSY;
210   
211   /* Disable the Peripheral to update the configuration registers */
212   __HAL_IRDA_DISABLE(hirda);
213   
214   /* Set the IRDA Communication parameters */
215   if (IRDA_SetConfig(hirda) == HAL_ERROR)
216   {
217     return HAL_ERROR;
218   }  
219   
220   /* In IRDA mode, the following bits must be kept cleared: 
221   - LINEN, STOP and CLKEN bits in the USART_CR2 register,
222   - SCEN and HDSEL bits in the USART_CR3 register.*/
223   hirda->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN | USART_CR2_STOP); 
224   hirda->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL); 
225    
226   /* set the UART/USART in IRDA mode */
227   hirda->Instance->CR3 |= USART_CR3_IREN; 
228     
229   /* Enable the Peripheral */
230   __HAL_IRDA_ENABLE(hirda);
231   
232   /* TEACK and/or REACK to check before moving hirda->State to Ready */
233   return (IRDA_CheckIdleState(hirda));
234 }
235
236 /**
237   * @brief DeInitializes the IRDA peripheral 
238   * @param hirda: IRDA handle
239   * @retval HAL status
240   */
241 HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
242 {
243   /* Check the IRDA handle allocation */
244   if(hirda == HAL_NULL)
245   {
246     return HAL_ERROR;
247   }
248   
249   /* Check the USART/UART associated to the IRDA handle */
250   assert_param(IS_IRDA_INSTANCE(hirda->Instance));
251
252   hirda->State = HAL_IRDA_STATE_BUSY;
253   
254   /* DeInit the low level hardware */
255   HAL_IRDA_MspDeInit(hirda);
256   /* Disable the Peripheral */
257   __HAL_IRDA_DISABLE(hirda);
258   
259   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
260   hirda->State = HAL_IRDA_STATE_RESET;
261   
262   /* Process Unlock */
263   __HAL_UNLOCK(hirda);
264   
265   return HAL_OK;
266 }
267
268 /**
269   * @brief IRDA MSP Init
270   * @param hirda: IRDA handle
271   * @retval None
272   */
273  __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
274 {
275   /* NOTE : This function should not be modified, when the callback is needed,
276             the HAL_IRDA_MspInit can be implemented in the user file
277    */ 
278 }
279
280 /**
281   * @brief IRDA MSP DeInit
282   * @param hirda: IRDA handle
283   * @retval None
284   */
285  __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
286 {
287   /* NOTE : This function should not be modified, when the callback is needed,
288             the HAL_IRDA_MspDeInit can be implemented in the user file
289    */ 
290 }
291
292 /**
293   * @}
294   */
295
296 /** @defgroup IRDA_Exported_Functions_Group2 Input and Output operation functions 
297   *  @brief   IRDA Transmit and Receive functions 
298   *
299 @verbatim   
300  ===============================================================================
301                       ##### I/O operation functions #####
302  ===============================================================================  
303     This subsection provides a set of functions allowing to manage the IRDA asynchronous
304     data transfers.
305
306     (#) There are two modes of transfer:
307        (+) Blocking mode: the communication is performed in polling mode. 
308             The HAL status of all data processing is returned by the same function 
309             after finishing transfer.  
310        (+) No-Blocking mode: the communication is performed using Interrupts 
311            or DMA, these API's return the HAL status.
312            The end of the data processing will be indicated through the 
313            dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when 
314            using DMA mode.
315            The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks 
316            will be executed respectivelly at the end of the Transmit or Receive process
317            The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
318
319     (#) Blocking mode API's are :
320         (+) HAL_IRDA_Transmit()
321         (+) HAL_IRDA_Receive() 
322         
323     (#) Non-Blocking mode API's with Interrupt are :
324         (+) HAL_IRDA_Transmit_IT()
325         (+) HAL_IRDA_Receive_IT()
326         (+) HAL_IRDA_IRQHandler()
327         (+) IRDA_Transmit_IT()
328         (+) IRDA_Receive_IT()
329
330     (#) Non-Blocking mode functions with DMA are :
331         (+) HAL_IRDA_Transmit_DMA()
332         (+) HAL_IRDA_Receive_DMA()
333
334     (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
335         (+) HAL_IRDA_TxCpltCallback()
336         (+) HAL_IRDA_RxCpltCallback()
337         (+) HAL_IRDA_ErrorCallback()
338       
339 @endverbatim
340   * @{
341   */
342
343 /**
344   * @brief Send an amount of data in blocking mode 
345   * @param hirda: IRDA handle
346   * @param pData: pointer to data buffer
347   * @param Size: amount of data to be sent
348   * @param Timeout: Duration of the timeout
349   * @retval HAL status
350   */
351 HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
352 {
353    uint16_t* tmp;
354    
355   if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_RX)) 
356   {
357     if((pData == HAL_NULL) || (Size == 0)) 
358     {
359       return  HAL_ERROR;                                    
360     }
361     
362     /* Process Locked */
363     __HAL_LOCK(hirda);
364     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
365
366     if(hirda->State == HAL_IRDA_STATE_BUSY_RX) 
367     {
368       hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
369     }
370     else
371     {
372       hirda->State = HAL_IRDA_STATE_BUSY_TX;
373     }    
374     
375     hirda->TxXferSize = Size;
376     hirda->TxXferCount = Size;
377     while(hirda->TxXferCount > 0)
378     {
379       hirda->TxXferCount--;
380
381         if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
382         { 
383           return HAL_TIMEOUT;
384         }
385       if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
386         {
387           tmp = (uint16_t*) pData;
388           hirda->Instance->TDR = (*tmp & (uint16_t)0x01FF);   
389           pData +=2;
390         }
391         else
392         { 
393           hirda->Instance->TDR = (*pData++ & (uint8_t)0xFF); 
394         }
395       } 
396
397     if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, Timeout) != HAL_OK)
398     { 
399       return HAL_TIMEOUT;
400     } 
401
402     if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX) 
403     {
404       hirda->State = HAL_IRDA_STATE_BUSY_RX;
405     }
406     else
407     {
408       hirda->State = HAL_IRDA_STATE_READY;
409     }    
410     
411     /* Process Unlocked */
412     __HAL_UNLOCK(hirda);
413     
414     return HAL_OK;
415   }
416   else
417   {
418     return HAL_BUSY;   
419   }
420 }
421
422 /**
423   * @brief Receive an amount of data in blocking mode 
424   * @param hirda: IRDA handle
425   * @param pData: pointer to data buffer
426   * @param Size: amount of data to be received
427   * @param Timeout: Duration of the timeout
428   * @retval HAL status
429   */
430 HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
431
432   uint16_t* tmp;
433   uint16_t uhMask;
434   
435   if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_TX))
436   { 
437     if((pData == HAL_NULL) || (Size == 0)) 
438     {
439       return  HAL_ERROR;                                    
440     }
441     
442     /* Process Locked */
443     __HAL_LOCK(hirda);
444     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
445
446     if(hirda->State == HAL_IRDA_STATE_BUSY_TX) 
447     {
448       hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
449     }
450     else
451     {
452       hirda->State = HAL_IRDA_STATE_BUSY_RX;
453     }    
454     
455     hirda->RxXferSize = Size; 
456     hirda->RxXferCount = Size;
457
458     /* Computation of the mask to apply to the RDR register 
459        of the UART associated to the IRDA */
460     __HAL_IRDA_MASK_COMPUTATION(hirda);
461     uhMask = hirda->Mask;
462
463     /* Check data remaining to be received */
464     while(hirda->RxXferCount > 0)
465     {
466       hirda->RxXferCount--;
467
468       if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
469       { 
470         return HAL_TIMEOUT;
471       }         
472       if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
473       {
474         tmp = (uint16_t*) pData ;
475         *tmp = (uint16_t)(hirda->Instance->RDR & uhMask);
476         pData +=2;
477       }
478       else
479       {
480         *pData++ = (uint8_t)(hirda->Instance->RDR & (uint8_t)uhMask); 
481       }       
482     } 
483
484     if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX) 
485     {
486       hirda->State = HAL_IRDA_STATE_BUSY_TX;
487     }
488     else
489     {
490       hirda->State = HAL_IRDA_STATE_READY;
491     }
492      
493     /* Process Unlocked */
494     __HAL_UNLOCK(hirda);
495     
496     return HAL_OK;
497   }
498   else
499   {
500     return HAL_BUSY;   
501   }
502 }
503
504 /**
505   * @brief Send an amount of data in interrupt mode 
506   * @param hirda: IRDA handle
507   * @param pData: pointer to data buffer
508   * @param Size: amount of data to be sent
509   * @retval HAL status
510   */
511 HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
512 {
513   if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_RX))
514   {
515     if((pData == HAL_NULL) || (Size == 0)) 
516     {
517       return HAL_ERROR;                                    
518     }
519     
520     /* Process Locked */
521     __HAL_LOCK(hirda);
522     
523     hirda->pTxBuffPtr = pData;
524     hirda->TxXferSize = Size;
525     hirda->TxXferCount = Size;
526
527     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
528     if(hirda->State == HAL_IRDA_STATE_BUSY_RX) 
529     {
530       hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
531     }
532     else
533     {
534       hirda->State = HAL_IRDA_STATE_BUSY_TX;
535     }
536         
537     /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
538     __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
539     
540     /* Process Unlocked */
541     __HAL_UNLOCK(hirda);    
542     
543     /* Enable the IRDA Transmit Data Register Empty Interrupt */
544     __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TXE);
545     
546     return HAL_OK;
547   }
548   else
549   {
550     return HAL_BUSY;   
551   }
552 }
553
554 /**
555   * @brief Receive an amount of data in interrupt mode 
556   * @param hirda: IRDA handle
557   * @param pData: pointer to data buffer
558   * @param Size: amount of data to be received
559   * @retval HAL status
560   */
561 HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
562 {  
563   if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_TX))
564   {
565     if((pData == HAL_NULL) || (Size == 0)) 
566     {
567       return HAL_ERROR;                                    
568     }
569     
570     /* Process Locked */
571   __HAL_LOCK(hirda);
572   
573     hirda->pRxBuffPtr = pData;
574     hirda->RxXferSize = Size;
575     hirda->RxXferCount = Size;
576   
577     /* Computation of the mask to apply to the RDR register 
578        of the UART associated to the IRDA */
579     __HAL_IRDA_MASK_COMPUTATION(hirda); 
580   
581     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;  
582     if(hirda->State == HAL_IRDA_STATE_BUSY_TX) 
583     {
584       hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
585     }
586     else
587     {
588       hirda->State = HAL_IRDA_STATE_BUSY_RX;
589     }
590     
591     /* Enable the IRDA Parity Error Interrupt */
592     __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_PE);
593     
594     /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
595     __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
596     
597     /* Process Unlocked */
598     __HAL_UNLOCK(hirda);
599     
600     /* Enable the IRDA Data Register not empty Interrupt */
601     __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_RXNE);
602     
603     return HAL_OK;
604   }
605   else
606   {
607     return HAL_BUSY; 
608   }
609 }
610
611 /**
612   * @brief Send an amount of data in DMA mode 
613   * @param hirda: IRDA handle
614   * @param pData: pointer to data buffer
615   * @param Size: amount of data to be sent
616   * @retval HAL status
617   */
618 HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
619 {
620   uint32_t *tmp;
621   
622   if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_RX))
623   {
624     if((pData == HAL_NULL) || (Size == 0)) 
625     {
626       return HAL_ERROR;                                    
627     }
628     
629     /* Process Locked */
630     __HAL_LOCK(hirda);
631     
632     hirda->pTxBuffPtr = pData;
633     hirda->TxXferSize = Size;
634     hirda->TxXferCount = Size; 
635     
636     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
637     
638     if(hirda->State == HAL_IRDA_STATE_BUSY_RX) 
639     {
640       hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
641     }
642     else
643     {
644       hirda->State = HAL_IRDA_STATE_BUSY_TX;
645     }
646     
647     /* Set the IRDA DMA transfer complete callback */
648     hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
649     
650     /* Set the DMA error callback */
651     hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
652
653     /* Enable the IRDA transmit DMA channel */
654     tmp = (uint32_t*)&pData;
655     HAL_DMA_Start_IT(hirda->hdmatx, *(uint32_t*)tmp, (uint32_t)&hirda->Instance->TDR, Size);
656     
657     /* Enable the DMA transfer for transmit request by setting the DMAT bit
658        in the IRDA CR3 register */
659     hirda->Instance->CR3 |= USART_CR3_DMAT;
660     
661     /* Process Unlocked */
662     __HAL_UNLOCK(hirda);
663     
664     return HAL_OK;
665   }
666   else
667   {
668     return HAL_BUSY;   
669   }
670 }
671
672 /**
673   * @brief Receive an amount of data in DMA mode 
674   * @param hirda: IRDA handle
675   * @param pData: pointer to data buffer
676   * @param Size: amount of data to be received
677   * @note   When the IRDA parity is enabled (PCE = 1), the received data contain 
678   *         the parity bit (MSB position)  
679   * @retval HAL status
680   */
681 HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
682 {
683   uint32_t *tmp;
684   
685   if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_TX))
686   {
687     if((pData == HAL_NULL) || (Size == 0)) 
688     {
689       return HAL_ERROR;                                    
690     }
691     
692     /* Process Locked */
693     __HAL_LOCK(hirda);
694     
695     hirda->pRxBuffPtr = pData;
696     hirda->RxXferSize = Size;
697
698     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
699     if(hirda->State == HAL_IRDA_STATE_BUSY_TX) 
700     {
701       hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
702     }
703     else
704     {
705       hirda->State = HAL_IRDA_STATE_BUSY_RX;
706     }
707     
708     /* Set the IRDA DMA transfer complete callback */
709     hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
710     
711     /* Set the DMA error callback */
712     hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
713
714     /* Enable the DMA channel */
715     tmp = (uint32_t*)&pData;
716     HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->RDR, *(uint32_t*)tmp, Size);
717
718     /* Enable the DMA transfer for the receiver request by setting the DMAR bit 
719        in the IRDA CR3 register */
720      hirda->Instance->CR3 |= USART_CR3_DMAR;
721     
722      /* Process Unlocked */
723      __HAL_UNLOCK(hirda);
724      
725     return HAL_OK;
726   }
727   else
728   {
729     return HAL_BUSY; 
730   }
731 }
732     
733 /**
734   * @brief This function handles IRDA interrupt request.
735   * @param hirda: IRDA handle
736   * @retval None
737   */
738 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
739 {
740   /* IRDA parity error interrupt occurred -------------------------------------*/
741   if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_PE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_PE) != RESET))
742   { 
743     __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_PEF);
744
745     hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
746     /* Set the IRDA state ready to be able to start again the process */
747     hirda->State = HAL_IRDA_STATE_READY;
748   }
749   
750   /* IRDA frame error interrupt occured --------------------------------------*/
751   if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_FE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR) != RESET))
752   { 
753     __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_FEF);
754
755     hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
756     /* Set the IRDA state ready to be able to start again the process */
757     hirda->State = HAL_IRDA_STATE_READY;
758   }
759   
760   /* IRDA noise error interrupt occured --------------------------------------*/
761   if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_NE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR) != RESET))
762   { 
763     __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_NEF);
764
765     hirda->ErrorCode |= HAL_IRDA_ERROR_NE; 
766     /* Set the IRDA state ready to be able to start again the process */
767     hirda->State = HAL_IRDA_STATE_READY;
768   }
769   
770   /* IRDA Over-Run interrupt occured -----------------------------------------*/
771   if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_ORE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR) != RESET))
772   { 
773     __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_OREF);
774
775     hirda->ErrorCode |= HAL_IRDA_ERROR_ORE; 
776     /* Set the IRDA state ready to be able to start again the process */
777     hirda->State = HAL_IRDA_STATE_READY;
778   }
779   
780   /* Call IRDA Error Call back function if need be --------------------------*/
781   if(hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
782   {
783     HAL_IRDA_ErrorCallback(hirda);
784   } 
785
786   /* IRDA in mode Receiver ---------------------------------------------------*/
787   if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_RXNE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_RXNE) != RESET))
788   { 
789     IRDA_Receive_IT(hirda);
790     /* Clear RXNE interrupt flag */
791     __HAL_IRDA_SEND_REQ(hirda, IRDA_RXDATA_FLUSH_REQUEST);
792   }
793   
794
795   /* IRDA in mode Transmitter ------------------------------------------------*/
796  if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_TXE) != RESET) &&(__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TXE) != RESET))
797   {
798     IRDA_Transmit_IT(hirda);
799   } 
800   
801   /* IRDA in mode Transmitter (transmission end) -----------------------------*/
802  if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_TC) != RESET) &&(__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TC) != RESET))
803   {
804     IRDA_EndTransmit_IT(hirda);
805   }   
806   
807 }
808 /**
809   * @}
810   */
811
812 /**
813   * @}
814   */  
815
816 /** @addtogroup IRDA_Private_Functions IRDA Private Functions
817   * @{
818   */
819
820 /**
821   * @brief DMA IRDA Tx transfer completed callback 
822   * @param hdma: DMA handle
823   * @retval None
824   */
825 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)     
826 {
827   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
828   hirda->TxXferCount = 0;
829   
830   /* Disable the DMA transfer for transmit request by setting the DMAT bit
831   in the IRDA CR3 register */
832   hirda->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAT);
833   
834   /* Wait for IRDA TC Flag */
835   if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, IRDA_TXDMA_TIMEOUTVALUE) != HAL_OK)
836   {
837     /* Timeout Occured */ 
838     HAL_IRDA_ErrorCallback(hirda);
839   }
840   else
841   {
842     /* No Timeout */
843     if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
844     {
845       hirda->State = HAL_IRDA_STATE_BUSY_RX;
846     }
847     else
848     {
849       hirda->State = HAL_IRDA_STATE_READY;
850     }
851     HAL_IRDA_TxCpltCallback(hirda);
852   }
853 }
854
855 /**
856   * @brief DMA IRDA Rx Transfer completed callback 
857   * @param hdma: DMA handle
858   * @retval None
859   */
860 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)  
861 {
862   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
863   hirda->RxXferCount = 0;
864   
865   /* Disable the DMA transfer for the receiver request by setting the DMAR bit 
866      in the IRDA CR3 register */
867   hirda->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAR);
868   
869   if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX) 
870   {
871     hirda->State = HAL_IRDA_STATE_BUSY_TX;
872   }
873   else
874   {
875     hirda->State = HAL_IRDA_STATE_READY;
876   }
877
878   HAL_IRDA_RxCpltCallback(hirda);
879 }
880
881 /**
882   * @brief DMA IRDA communication error callback 
883   * @param hdma: DMA handle
884   * @retval None
885   */
886 static void IRDA_DMAError(DMA_HandleTypeDef *hdma)   
887 {
888   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
889   hirda->RxXferCount = 0;
890   hirda->TxXferCount = 0;
891   hirda->State= HAL_IRDA_STATE_READY;
892   hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
893   HAL_IRDA_ErrorCallback(hirda);
894 }
895 /**
896   * @}
897   */
898
899 /** @addtogroup IRDA_Exported_Functions IRDA Exported Functions
900   * @{
901   */
902   
903 /** @addtogroup IRDA_Exported_Functions_Group2 Input and Output operation functions 
904   * @{
905   */
906
907 /**
908   * @brief Tx Transfer completed callback
909   * @param hirda: irda handle
910   * @retval None
911   */
912  __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
913 {
914   /* NOTE : This function should not be modified, when the callback is needed,
915             the HAL_IRDA_TxCpltCallback can be implemented in the user file
916    */ 
917 }
918
919 /**
920   * @brief Rx Transfer completed callback
921   * @param hirda: irda handle
922   * @retval None
923   */
924 __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
925 {
926   /* NOTE : This function should not be modified, when the callback is needed,
927             the HAL_IRDA_TxCpltCallback can be implemented in the user file
928    */
929 }
930
931 /**
932   * @brief IRDA error callback
933   * @param hirda: IRDA handle
934   * @retval None
935   */
936  __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
937 {
938   /* NOTE : This function should not be modified, when the callback is needed,
939             the HAL_IRDA_ErrorCallback can be implemented in the user file
940    */ 
941 }
942
943 /**
944   * @}
945   */
946
947 /**
948   * @}
949   */
950   
951 /** @addtogroup IRDA_Private_Functions IRDA Private Functions
952   * @{
953   */
954   
955 /**
956   * @brief Receive an amount of data in non blocking mode. 
957   *         Function called under interruption only, once
958   *         interruptions have been enabled by HAL_IRDA_Transmit_IT()      
959   * @param hirda: IRDA handle
960   * @retval HAL status
961   */
962 static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
963 {
964   uint16_t* tmp;
965     
966   if((hirda->State == HAL_IRDA_STATE_BUSY_TX) || (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX))
967   {
968  
969     if(hirda->TxXferCount == 0)
970     {
971       /* Disable the IRDA Transmit Data Register Empty Interrupt */
972       __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
973       
974       /* Enable the IRDA Transmit Complete Interrupt */    
975       __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TC);
976       
977       return HAL_OK;
978     }
979     else
980     {
981       if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
982       {
983         tmp = (uint16_t*) hirda->pTxBuffPtr;
984         hirda->Instance->TDR = (*tmp & (uint16_t)0x01FF);
985         hirda->pTxBuffPtr += 2;
986       }
987       else
988       {
989         hirda->Instance->TDR = (uint8_t)(*hirda->pTxBuffPtr++ & (uint8_t)0xFF); 
990       }
991       hirda->TxXferCount--;
992   
993       return HAL_OK;
994     }
995   }
996   else
997   {
998     return HAL_BUSY;   
999   }
1000 }
1001
1002 /**
1003   * @brief  Wraps up transmission in non blocking mode.
1004   * @param  hirda: pointer to a IRDA_HandleTypeDef structure that contains
1005   *                the configuration information for the specified IRDA module.
1006   * @retval HAL status
1007   */
1008 static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda)
1009 {
1010   /* Disable the IRDA Transmit Complete Interrupt */    
1011   __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TC);
1012   
1013   /* Check if a receive process is ongoing or not */
1014   if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX) 
1015   {
1016     hirda->State = HAL_IRDA_STATE_BUSY_RX;
1017   }
1018   else
1019   {
1020     /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1021     __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1022     
1023     hirda->State = HAL_IRDA_STATE_READY;
1024   }
1025   
1026   HAL_IRDA_TxCpltCallback(hirda);
1027   
1028   return HAL_OK;
1029 }
1030
1031
1032 /**
1033   * @brief Receive an amount of data in non blocking mode. 
1034   *         Function called under interruption only, once
1035   *         interruptions have been enabled by HAL_IRDA_Receive_IT()      
1036   * @param hirda: IRDA handle
1037   * @retval HAL status
1038   */
1039 static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
1040 {
1041   uint16_t* tmp;
1042   uint16_t uhMask = hirda->Mask;
1043   
1044   if ((hirda->State == HAL_IRDA_STATE_BUSY_RX) || (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX))
1045   {
1046     
1047     if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
1048         {
1049         tmp = (uint16_t*) hirda->pRxBuffPtr ;
1050           *tmp = (uint16_t)(hirda->Instance->RDR & uhMask);
1051          hirda->pRxBuffPtr  +=2;
1052         }
1053         else
1054         {
1055           *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->RDR & (uint8_t)uhMask); 
1056         }
1057     
1058     if(--hirda->RxXferCount == 0)
1059     {
1060       __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
1061       
1062       if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX) 
1063       {
1064         hirda->State = HAL_IRDA_STATE_BUSY_TX;
1065       }
1066       else
1067       {      
1068       /* Disable the IRDA Parity Error Interrupt */
1069         __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1070       
1071         /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1072         __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1073       
1074         hirda->State = HAL_IRDA_STATE_READY;
1075       }
1076       
1077       HAL_IRDA_RxCpltCallback(hirda);
1078       
1079       return HAL_OK;
1080     }
1081     
1082     return HAL_OK;
1083   }
1084   else
1085   {
1086     return HAL_BUSY; 
1087   }
1088 }
1089
1090 /**
1091   * @}
1092   */
1093
1094 /** @addtogroup IRDA_Exported_Functions IRDA Exported Functions
1095   * @{
1096   */
1097
1098 /** @defgroup IRDA_Exported_Functions_Group3 Peripheral State and Errors functions 
1099   *  @brief   IRDA control functions 
1100   *
1101 @verbatim   
1102  ===============================================================================
1103                    ##### Peripheral State and Error functions #####
1104  ===============================================================================  
1105     [..]
1106     This subsection provides a set of functions allowing to control the IRDA.
1107      (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state of the IRDA peripheral. 
1108      (+) IRDA_SetConfig() API is used to configure the IRDA communications parameters.
1109 @endverbatim
1110   * @{
1111   */
1112
1113 /**
1114   * @brief return the IRDA state
1115   * @param hirda: irda handle
1116   * @retval HAL state
1117   */
1118 HAL_IRDA_StateTypeDef HAL_IRDA_GetState(IRDA_HandleTypeDef *hirda)
1119 {
1120   return hirda->State;
1121 }
1122
1123 /**
1124 * @brief  Return the IRDA error code
1125 * @param  hirda : pointer to a IRDA_HandleTypeDef structure that contains
1126   *              the configuration information for the specified IRDA.
1127 * @retval IRDA Error Code
1128 */
1129 uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef *hirda)
1130 {
1131   return hirda->ErrorCode;
1132 }
1133
1134 /**
1135   * @}
1136   */
1137
1138 /**
1139   * @}
1140   */  
1141
1142 /** @addtogroup IRDA_Private_Functions IRDA Private Functions
1143   * @{
1144   */
1145   
1146 /**
1147   * @brief Configure the IRDA peripheral 
1148   * @param hirda: irda handle
1149   * @retval None
1150   */
1151 static HAL_StatusTypeDef IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
1152 {
1153   uint32_t tmpreg                     = 0x00000000;
1154   IRDA_ClockSourceTypeDef clocksource = IRDA_CLOCKSOURCE_UNDEFINED;
1155   HAL_StatusTypeDef ret               = HAL_OK;  
1156   
1157   /* Check the communication parameters */ 
1158   assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));  
1159   assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
1160   assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
1161   assert_param(IS_IRDA_TX_RX_MODE(hirda->Init.Mode));
1162   assert_param(IS_IRDA_PRESCALER(hirda->Init.Prescaler)); 
1163   assert_param(IS_IRDA_POWERMODE(hirda->Init.PowerMode)); 
1164
1165   /*-------------------------- USART CR1 Configuration -----------------------*/        
1166   /* Configure the IRDA Word Length, Parity and transfer Mode: 
1167      Set the M bits according to hirda->Init.WordLength value 
1168      Set PCE and PS bits according to hirda->Init.Parity value
1169      Set TE and RE bits according to hirda->Init.Mode value */
1170   tmpreg = (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode ;
1171   
1172   MODIFY_REG(hirda->Instance->CR1, IRDA_CR1_FIELDS, tmpreg);
1173   
1174   /*-------------------------- USART CR3 Configuration -----------------------*/
1175   MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.PowerMode);
1176     
1177   /*-------------------------- USART GTPR Configuration ----------------------*/  
1178   MODIFY_REG(hirda->Instance->GTPR, USART_GTPR_PSC, hirda->Init.Prescaler);
1179   
1180   /*-------------------------- USART BRR Configuration -----------------------*/ 
1181   __HAL_IRDA_GETCLOCKSOURCE(hirda, clocksource);
1182   switch (clocksource)
1183   {
1184     case IRDA_CLOCKSOURCE_PCLK1: 
1185       hirda->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK1Freq() / hirda->Init.BaudRate);
1186       break;
1187     case IRDA_CLOCKSOURCE_PCLK2: 
1188       hirda->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK2Freq() / hirda->Init.BaudRate);
1189       break;
1190     case IRDA_CLOCKSOURCE_HSI: 
1191       hirda->Instance->BRR = (uint16_t)(HSI_VALUE / hirda->Init.BaudRate); 
1192       break; 
1193     case IRDA_CLOCKSOURCE_SYSCLK:  
1194       hirda->Instance->BRR = (uint16_t)(HAL_RCC_GetSysClockFreq() / hirda->Init.BaudRate);
1195       break;  
1196     case IRDA_CLOCKSOURCE_LSE:                
1197       hirda->Instance->BRR = (uint16_t)(LSE_VALUE / hirda->Init.BaudRate); 
1198       break;      
1199     case IRDA_CLOCKSOURCE_UNDEFINED:                
1200     default:                
1201       ret = HAL_ERROR; 
1202       break;              
1203   } 
1204   
1205   return ret;  
1206 }
1207
1208 /**
1209   * @brief Check the IRDA Idle State
1210   * @param hirda: IRDA handle
1211   * @retval HAL status
1212   */
1213 static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda)
1214 {
1215
1216   /* Initialize the IRDA ErrorCode */
1217   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1218
1219   /* Check if the Transmitter is enabled */
1220   if((hirda->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
1221   {
1222     /* Wait until TEACK flag is set */
1223     if(IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_TEACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
1224     { 
1225       /* Timeout Occured */ 
1226       return HAL_TIMEOUT;
1227     }     
1228   }
1229   /* Check if the Receiver is enabled */
1230   if((hirda->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
1231   {
1232     if(IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_REACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
1233     { 
1234       /* Timeout Occured */ 
1235       return HAL_TIMEOUT;
1236     }       
1237   }
1238         
1239   /* Initialize the IRDA state*/
1240   hirda->State= HAL_IRDA_STATE_READY;  
1241   /* Process Unlocked */
1242   __HAL_UNLOCK(hirda);
1243   
1244   return HAL_OK;
1245 }
1246
1247 /**
1248   * @brief  Handle IRDA Communication Timeout.
1249   * @param  hirda: IRDA handle
1250   * @param  Flag: specifies the IRDA flag to check.
1251   * @param  Status: the flag status (SET or RESET). The function is locked in a while loop as long as the flag remains set to Status.
1252   * @param  Timeout: Timeout duration
1253   * @retval HAL status
1254   */
1255 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout)  
1256 {
1257   uint32_t tickstart = HAL_GetTick();
1258   
1259   /* Wait until flag is set */
1260   if(Status == RESET)
1261   {    
1262     while(__HAL_IRDA_GET_FLAG(hirda, Flag) == RESET)
1263     {
1264       /* Check for the Timeout */
1265       if(Timeout != HAL_MAX_DELAY)
1266       {
1267         if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1268         {
1269           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1270           __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
1271           __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
1272           __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1273           __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1274           
1275           hirda->State= HAL_IRDA_STATE_TIMEOUT;
1276           
1277           /* Process Unlocked */
1278           __HAL_UNLOCK(hirda);
1279           
1280           return HAL_TIMEOUT;
1281         }
1282       }
1283     }
1284   }
1285   else
1286   {
1287     while(__HAL_IRDA_GET_FLAG(hirda, Flag) != RESET)
1288     {
1289       /* Check for the Timeout */
1290       if(Timeout != HAL_MAX_DELAY)
1291       {    
1292         if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1293         {
1294           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1295           __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
1296           __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
1297           __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1298           __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1299   
1300           hirda->State= HAL_IRDA_STATE_TIMEOUT;
1301           
1302           /* Process Unlocked */
1303           __HAL_UNLOCK(hirda);
1304           
1305           return HAL_TIMEOUT;
1306         }
1307       }
1308     }
1309   }
1310   return HAL_OK;      
1311 }
1312
1313 /**
1314   * @}
1315   */
1316
1317 #endif /* HAL_IRDA_MODULE_ENABLED */
1318 /**
1319   * @}
1320   */
1321
1322 /**
1323   * @}
1324   */
1325
1326 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/