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