2 ******************************************************************************
3 * @file stm32f1xx_hal_irda.c
4 * @author MCD Application Team
6 * @date 15-December-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 functions
11 * + IO operation functions
12 * + Peripheral State and Errors functions
13 * + Peripheral Control functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 The IRDA HAL driver can be used as follows:
22 (#) Declare a IRDA_HandleTypeDef handle structure.
23 (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API:
24 (##) Enable the USARTx interface clock.
25 (##) IRDA pins configuration:
26 (+++) Enable the clock for the IRDA GPIOs.
27 (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
28 (##) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
29 and HAL_IRDA_Receive_IT() APIs):
30 (+++) Configure the USARTx interrupt priority.
31 (+++) Enable the NVIC USART IRQ handle.
32 (##) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
33 and HAL_IRDA_Receive_DMA() APIs):
34 (+++) Declare a DMA handle structure for the Tx/Rx channel.
35 (+++) Enable the DMAx interface clock.
36 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
37 (+++) Configure the DMA Tx/Rx channel.
38 (+++) Associate the initilalized DMA handle to the IRDA DMA Tx/Rx handle.
39 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
40 (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
41 (used for last byte sending completion detection in DMA non circular mode)
43 (#) Program the Baud Rate, Word Length, Parity, IrDA Mode, Prescaler
44 and Mode(Receiver/Transmitter) in the hirda Init structure.
46 (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
47 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
48 by calling the customed HAL_IRDA_MspInit() API.
50 -@@- The specific IRDA interrupts (Transmission complete interrupt,
51 RXNE interrupt and Error Interrupts) will be managed using the macros
52 __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
54 (#) Three operation modes are available within this driver :
56 *** Polling mode IO operation ***
57 =================================
59 (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
60 (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
62 *** Interrupt mode IO operation ***
63 ===================================
65 (+) Send an amount of data in non blocking mode using HAL_IRDA_Transmit_IT()
66 (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
67 add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
68 (+) Receive an amount of data in non blocking mode using HAL_IRDA_Receive_IT()
69 (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
70 add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
71 (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
72 add his own code by customization of function pointer HAL_IRDA_ErrorCallback
74 *** DMA mode IO operation ***
75 ==============================
77 (+) Send an amount of data in non blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
78 (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
79 add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
80 (+) Receive an amount of data in non blocking mode (DMA) using HAL_IRDA_Receive_DMA()
81 (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
82 add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
83 (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
84 add his own code by customization of function pointer HAL_IRDA_ErrorCallback
86 *** IRDA HAL driver macros list ***
87 ====================================
89 Below the list of most used macros in IRDA HAL driver.
91 (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
92 (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
93 (+) __HAL_IRDA_GET_FLAG : Check whether the specified IRDA flag is set or not
94 (+) __HAL_IRDA_CLEAR_FLAG : Clear the specified IRDA pending flag
95 (+) __HAL_IRDA_ENABLE_IT: Enable the specified IRDA interrupt
96 (+) __HAL_IRDA_DISABLE_IT: Disable the specified IRDA interrupt
97 (+) __HAL_IRDA_GET_IT_SOURCE: Check whether the specified IRDA interrupt has occurred or not
100 (@) You can refer to the IRDA HAL driver header file for more useful macros
103 ******************************************************************************
106 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
108 * Redistribution and use in source and binary forms, with or without modification,
109 * are permitted provided that the following conditions are met:
110 * 1. Redistributions of source code must retain the above copyright notice,
111 * this list of conditions and the following disclaimer.
112 * 2. Redistributions in binary form must reproduce the above copyright notice,
113 * this list of conditions and the following disclaimer in the documentation
114 * and/or other materials provided with the distribution.
115 * 3. Neither the name of STMicroelectronics nor the names of its contributors
116 * may be used to endorse or promote products derived from this software
117 * without specific prior written permission.
119 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
120 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
121 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
122 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
123 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
124 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
125 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
126 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
127 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
128 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
130 ******************************************************************************
133 /* Includes ------------------------------------------------------------------*/
134 #include "stm32f1xx_hal.h"
136 /** @addtogroup STM32F1xx_HAL_Driver
140 /** @defgroup IRDA IRDA
141 * @brief HAL IRDA module driver
145 #ifdef HAL_IRDA_MODULE_ENABLED
147 /* Private typedef -----------------------------------------------------------*/
148 /* Private define ------------------------------------------------------------*/
149 /** @defgroup IRDA_Private_Constants IRDA Private Constants
152 #define IRDA_DR_MASK_U16_8DATABITS (uint16_t)0x00FF
153 #define IRDA_DR_MASK_U16_9DATABITS (uint16_t)0x01FF
155 #define IRDA_DR_MASK_U8_7DATABITS (uint8_t)0x7F
156 #define IRDA_DR_MASK_U8_8DATABITS (uint8_t)0xFF
163 /* Private macros --------------------------------------------------------*/
164 /* Private variables ---------------------------------------------------------*/
165 /* Private function prototypes -----------------------------------------------*/
166 /** @addtogroup IRDA_Private_Functions IRDA Private Functions
169 static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
170 static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda);
171 static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
172 static void IRDA_SetConfig (IRDA_HandleTypeDef *hirda);
173 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
174 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma);
175 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
176 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma);
177 static void IRDA_DMAError(DMA_HandleTypeDef *hdma);
178 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
183 /* Exported functions ---------------------------------------------------------*/
185 /** @defgroup IRDA_Exported_Functions IRDA Exported Functions
189 /** @defgroup IRDA_Exported_Functions_Group1 Initialization and de-initialization functions
190 * @brief Initialization and Configuration functions
193 ==============================================================================
194 ##### Initialization and Configuration functions #####
195 ==============================================================================
197 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
199 (+) For the asynchronous mode only these parameters can be configured:
202 (++) Parity: If the parity is enabled, then the MSB bit of the data written
203 in the data register is transmitted but is changed by the parity bit.
204 Depending on the frame length defined by the M bit (8-bits or 9-bits),
205 the possible IRDA frame formats are as listed in the following table:
206 (+++) +-------------------------------------------------------------+
207 (+++) | M bit | PCE bit | IRDA frame |
208 (+++) |---------------------|---------------------------------------|
209 (+++) | 0 | 0 | | SB | 8 bit data | STB | |
210 (+++) |---------|-----------|---------------------------------------|
211 (+++) | 0 | 1 | | SB | 7 bit data | PB | STB | |
212 (+++) |---------|-----------|---------------------------------------|
213 (+++) | 1 | 0 | | SB | 9 bit data | STB | |
214 (+++) |---------|-----------|---------------------------------------|
215 (+++) | 1 | 1 | | SB | 8 bit data | PB | STB | |
216 (+++) +-------------------------------------------------------------+
217 (++) Prescaler: A pulse of width less than two and greater than one PSC period(s) may or may
218 not be rejected. The receiver set up time should be managed by software. The IrDA physical layer
219 specification specifies a minimum of 10 ms delay between transmission and
220 reception (IrDA is a half duplex protocol).
221 (++) Mode: Receiver/transmitter modes
222 (++) IrDAMode: the IrDA can operate in the Normal mode or in the Low power mode.
225 The HAL_IRDA_Init() function follows IRDA configuration procedures (details for the procedures
226 are available in reference manuals (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
233 * @brief Initializes the IRDA mode according to the specified
234 * parameters in the IRDA_InitTypeDef and create the associated handle.
235 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
236 * the configuration information for the specified IRDA module.
239 HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
241 /* Check the IRDA handle allocation */
247 /* Check the IRDA instance parameters */
248 assert_param(IS_IRDA_INSTANCE(hirda->Instance));
249 /* Check the IRDA mode parameter in the IRDA handle */
250 assert_param(IS_IRDA_POWERMODE(hirda->Init.IrDAMode));
252 if(hirda->State == HAL_IRDA_STATE_RESET)
254 /* Allocate lock resource and initialize it */
255 hirda-> Lock = HAL_UNLOCKED;
257 /* Init the low level hardware */
258 HAL_IRDA_MspInit(hirda);
261 hirda->State = HAL_IRDA_STATE_BUSY;
263 /* Disable the IRDA peripheral */
264 __HAL_IRDA_DISABLE(hirda);
266 /* Set the IRDA communication parameters */
267 IRDA_SetConfig(hirda);
269 /* In IrDA mode, the following bits must be kept cleared:
270 - LINEN, STOP and CLKEN bits in the USART_CR2 register,
271 - SCEN and HDSEL bits in the USART_CR3 register.*/
272 CLEAR_BIT(hirda->Instance->CR2, (USART_CR2_LINEN | USART_CR2_STOP | USART_CR2_CLKEN));
273 CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
275 /* Enable the IRDA peripheral */
276 __HAL_IRDA_ENABLE(hirda);
278 /* Set the prescaler */
279 MODIFY_REG(hirda->Instance->GTPR, USART_GTPR_PSC, hirda->Init.Prescaler);
281 /* Configure the IrDA mode */
282 MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.IrDAMode);
284 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
285 SET_BIT(hirda->Instance->CR3, USART_CR3_IREN);
287 /* Initialize the IRDA state*/
288 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
289 hirda->State= HAL_IRDA_STATE_READY;
295 * @brief DeInitializes the IRDA peripheral
296 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
297 * the configuration information for the specified IRDA module.
300 HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
302 /* Check the IRDA handle allocation */
308 /* Check the parameters */
309 assert_param(IS_IRDA_INSTANCE(hirda->Instance));
311 hirda->State = HAL_IRDA_STATE_BUSY;
313 /* Disable the Peripheral */
314 __HAL_IRDA_DISABLE(hirda);
316 /* DeInit the low level hardware */
317 HAL_IRDA_MspDeInit(hirda);
319 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
320 hirda->State = HAL_IRDA_STATE_RESET;
329 * @brief IRDA MSP Init.
330 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
331 * the configuration information for the specified IRDA module.
334 __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
336 /* NOTE: This function should not be modified, when the callback is needed,
337 the HAL_IRDA_MspInit can be implemented in the user file
342 * @brief IRDA MSP DeInit.
343 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
344 * the configuration information for the specified IRDA module.
347 __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
349 /* NOTE: This function should not be modified, when the callback is needed,
350 the HAL_IRDA_MspDeInit can be implemented in the user file
358 /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
359 * @brief IRDA Transmit and Receive functions
362 ==============================================================================
363 ##### IO operation functions #####
364 ==============================================================================
366 This subsection provides a set of functions allowing to manage the IRDA data transfers.
369 IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
370 on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
371 is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
372 While receiving data, transmission should be avoided as the data to be transmitted
375 (#) There are two modes of transfer:
376 (++) Blocking mode: The communication is performed in polling mode.
377 The HAL status of all data processing is returned by the same function
378 after finishing transfer.
379 (++) No-Blocking mode: The communication is performed using Interrupts
380 or DMA, These API's return the HAL status.
381 The end of the data processing will be indicated through the
382 dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
384 The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
385 will be executed respectively at the end of the transmit or Receive process
386 The HAL_IRDA_ErrorCallback() user callback will be executed when a communication
389 (#) Blocking mode APIs are :
390 (++) HAL_IRDA_Transmit()
391 (++) HAL_IRDA_Receive()
393 (#) Non Blocking mode APIs with Interrupt are :
394 (++) HAL_IRDA_Transmit_IT()
395 (++) HAL_IRDA_Receive_IT()
396 (++) HAL_IRDA_IRQHandler()
398 (#) Non Blocking mode functions with DMA are :
399 (++) HAL_IRDA_Transmit_DMA()
400 (++) HAL_IRDA_Receive_DMA()
401 (++) HAL_IRDA_DMAPause()
402 (++) HAL_IRDA_DMAResume()
403 (++) HAL_IRDA_DMAStop()
405 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
406 (++) HAL_IRDA_TxHalfCpltCallback()
407 (++) HAL_IRDA_TxCpltCallback()
408 (++) HAL_IRDA_RxHalfCpltCallback()
409 (++) HAL_IRDA_RxCpltCallback()
410 (++) HAL_IRDA_ErrorCallback()
417 * @brief Sends an amount of data in blocking mode.
418 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
419 * the configuration information for the specified IRDA module.
420 * @param pData: Pointer to data buffer
421 * @param Size: Amount of data to be sent
422 * @param Timeout: Specify timeout value
425 HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
428 uint32_t tmp_state = 0;
430 tmp_state = hirda->State;
431 if((tmp_state == HAL_IRDA_STATE_READY) || (tmp_state == HAL_IRDA_STATE_BUSY_RX))
433 if((pData == NULL) || (Size == 0))
441 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
442 if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
444 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
448 hirda->State = HAL_IRDA_STATE_BUSY_TX;
451 hirda->TxXferSize = Size;
452 hirda->TxXferCount = Size;
453 while(hirda->TxXferCount > 0)
455 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
457 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
461 tmp = (uint16_t*) pData;
462 WRITE_REG(hirda->Instance->DR,(*tmp & IRDA_DR_MASK_U16_9DATABITS));
463 if(hirda->Init.Parity == IRDA_PARITY_NONE)
474 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
478 WRITE_REG(hirda->Instance->DR, (*pData++ & IRDA_DR_MASK_U8_8DATABITS));
480 hirda->TxXferCount--;
483 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, Timeout) != HAL_OK)
488 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
490 hirda->State = HAL_IRDA_STATE_BUSY_RX;
494 hirda->State = HAL_IRDA_STATE_READY;
497 /* Process Unlocked */
509 * @brief Receive an amount of data in blocking mode.
510 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
511 * the configuration information for the specified IRDA module.
512 * @param pData: Pointer to data buffer
513 * @param Size: Amount of data to be received
514 * @param Timeout: Specify timeout value
517 HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
520 uint32_t tmp_state = 0;
522 tmp_state = hirda->State;
523 if((tmp_state == HAL_IRDA_STATE_READY) || (tmp_state == HAL_IRDA_STATE_BUSY_TX))
525 if((pData == NULL) || (Size == 0))
533 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
534 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
536 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
540 hirda->State = HAL_IRDA_STATE_BUSY_RX;
542 hirda->RxXferSize = Size;
543 hirda->RxXferCount = Size;
544 /* Check the remain data to be received */
545 while(hirda->RxXferCount > 0)
547 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
549 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
553 tmp = (uint16_t*) pData ;
554 if(hirda->Init.Parity == IRDA_PARITY_NONE)
556 *tmp = (uint16_t)(hirda->Instance->DR & IRDA_DR_MASK_U16_9DATABITS);
561 *tmp = (uint16_t)(hirda->Instance->DR & IRDA_DR_MASK_U16_8DATABITS);
567 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
571 if(hirda->Init.Parity == IRDA_PARITY_NONE)
573 *pData++ = (uint8_t)(hirda->Instance->DR & IRDA_DR_MASK_U8_8DATABITS);
577 *pData++ = (uint8_t)(hirda->Instance->DR & IRDA_DR_MASK_U8_7DATABITS);
580 hirda->RxXferCount--;
582 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
584 hirda->State = HAL_IRDA_STATE_BUSY_TX;
588 hirda->State = HAL_IRDA_STATE_READY;
591 /* Process Unlocked */
603 * @brief Sends an amount of data in non-blocking mode.
604 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
605 * the configuration information for the specified IRDA module.
606 * @param pData: Pointer to data buffer
607 * @param Size: Amount of data to be sent
610 HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
612 uint32_t tmp_state = 0;
614 tmp_state = hirda->State;
615 if((tmp_state == HAL_IRDA_STATE_READY) || (tmp_state == HAL_IRDA_STATE_BUSY_RX))
617 if((pData == NULL) || (Size == 0))
624 hirda->pTxBuffPtr = pData;
625 hirda->TxXferSize = Size;
626 hirda->TxXferCount = Size;
628 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
629 if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
631 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
635 hirda->State = HAL_IRDA_STATE_BUSY_TX;
638 /* Process Unlocked */
641 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
642 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
644 /* Enable the IRDA Transmit Data Register Empty Interrupt */
645 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TXE);
656 * @brief Receives an amount of data in non-blocking mode.
657 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
658 * the configuration information for the specified IRDA module.
659 * @param pData: Pointer to data buffer
660 * @param Size: Amount of data to be received
663 HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
665 uint32_t tmp_state = 0;
667 tmp_state = hirda->State;
668 if((tmp_state == HAL_IRDA_STATE_READY) || (tmp_state == HAL_IRDA_STATE_BUSY_TX))
670 if((pData == NULL) || (Size == 0))
678 hirda->pRxBuffPtr = pData;
679 hirda->RxXferSize = Size;
680 hirda->RxXferCount = Size;
682 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
683 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
685 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
689 hirda->State = HAL_IRDA_STATE_BUSY_RX;
692 /* Process Unlocked */
695 /* Enable the IRDA Data Register not empty Interrupt */
696 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_RXNE);
698 /* Enable the IRDA Parity Error Interrupt */
699 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_PE);
701 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
702 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
713 * @brief Sends an amount of data in non-blocking mode.
714 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
715 * the configuration information for the specified IRDA module.
716 * @param pData: Pointer to data buffer
717 * @param Size: Amount of data to be sent
720 HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
723 uint32_t tmp_state = 0;
725 tmp_state = hirda->State;
726 if((tmp_state == HAL_IRDA_STATE_READY) || (tmp_state == HAL_IRDA_STATE_BUSY_RX))
728 if((pData == NULL) || (Size == 0))
736 hirda->pTxBuffPtr = pData;
737 hirda->TxXferSize = Size;
738 hirda->TxXferCount = Size;
739 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
741 if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
743 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
747 hirda->State = HAL_IRDA_STATE_BUSY_TX;
750 /* Set the IRDA DMA transfer complete callback */
751 hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
753 /* Set the IRDA DMA half transfert complete callback */
754 hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;
756 /* Set the DMA error callback */
757 hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
759 /* Enable the IRDA transmit DMA channel */
760 tmp = (uint32_t*)&pData;
761 HAL_DMA_Start_IT(hirda->hdmatx, *(uint32_t*)tmp, (uint32_t)&hirda->Instance->DR, Size);
763 /* Clear the TC flag in the SR register by writing 0 to it */
764 __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_FLAG_TC);
766 /* Enable the DMA transfer for transmit request by setting the DMAT bit
767 in the USART CR3 register */
768 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
770 /* Process Unlocked */
782 * @brief Receive an amount of data in non-blocking mode.
783 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
784 * the configuration information for the specified IRDA module.
785 * @param pData: Pointer to data buffer
786 * @param Size: Amount of data to be received
787 * @note When the IRDA parity is enabled (PCE = 1) the data received contain the parity bit.
790 HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
793 uint32_t tmp_state = 0;
795 tmp_state = hirda->State;
796 if((tmp_state == HAL_IRDA_STATE_READY) || (tmp_state == HAL_IRDA_STATE_BUSY_TX))
798 if((pData == NULL) || (Size == 0))
806 hirda->pRxBuffPtr = pData;
807 hirda->RxXferSize = Size;
808 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
809 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
811 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
815 hirda->State = HAL_IRDA_STATE_BUSY_RX;
818 /* Set the IRDA DMA transfer complete callback */
819 hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
821 /* Set the IRDA DMA half transfert complete callback */
822 hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;
824 /* Set the DMA error callback */
825 hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
827 /* Enable the DMA channel */
828 tmp = (uint32_t*)&pData;
829 HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->DR, *(uint32_t*)tmp, Size);
831 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
832 in the USART CR3 register */
833 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
835 /* Process Unlocked */
847 * @brief Pauses the DMA Transfer.
848 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
849 * the configuration information for the specified IRDA module.
852 HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda)
857 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
859 /* Disable the IRDA DMA Tx request */
860 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
862 else if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
864 /* Disable the IRDA DMA Rx request */
865 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
867 else if (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
869 /* Disable the IRDA DMA Tx & Rx requests */
870 CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_DMAT | USART_CR3_DMAR));
874 /* Process Unlocked */
880 /* Process Unlocked */
887 * @brief Resumes the DMA Transfer.
888 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
889 * the configuration information for the specified UART module.
892 HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda)
897 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
899 /* Enable the IRDA DMA Tx request */
900 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
902 else if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
904 /* Clear the Overrun flag before resumming the Rx transfer*/
905 __HAL_IRDA_CLEAR_OREFLAG(hirda);
906 /* Enable the IRDA DMA Rx request */
907 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
909 else if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
911 /* Clear the Overrun flag before resumming the Rx transfer*/
912 __HAL_IRDA_CLEAR_OREFLAG(hirda);
913 /* Enable the IRDA DMA Tx & Rx request */
914 SET_BIT(hirda->Instance->CR3, (USART_CR3_DMAT | USART_CR3_DMAR));
918 /* Process Unlocked */
924 /* Process Unlocked */
931 * @brief Stops the DMA Transfer.
932 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
933 * the configuration information for the specified UART module.
936 HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda)
938 /* The Lock is not implemented on this API to allow the user application
939 to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback():
940 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
941 and the correspond call back is executed HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback()
944 /* Disable the IRDA Tx/Rx DMA requests */
945 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
946 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
948 /* Abort the IRDA DMA tx channel */
949 if(hirda->hdmatx != NULL)
951 HAL_DMA_Abort(hirda->hdmatx);
953 /* Abort the IRDA DMA rx channel */
954 if(hirda->hdmarx != NULL)
956 HAL_DMA_Abort(hirda->hdmarx);
959 hirda->State = HAL_IRDA_STATE_READY;
965 * @brief This function handles IRDA interrupt request.
966 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
967 * the configuration information for the specified IRDA module.
970 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
972 uint32_t tmp_flag = 0, tmp_it_source = 0;
974 tmp_flag = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_PE);
975 tmp_it_source = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_PE);
976 /* IRDA parity error interrupt occurred -----------------------------------*/
977 if((tmp_flag != RESET) && (tmp_it_source != RESET))
979 __HAL_IRDA_CLEAR_PEFLAG(hirda);
980 hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
983 tmp_flag = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_FE);
984 tmp_it_source = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR);
985 /* IRDA frame error interrupt occurred ------------------------------------*/
986 if((tmp_flag != RESET) && (tmp_it_source != RESET))
988 __HAL_IRDA_CLEAR_FEFLAG(hirda);
989 hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
992 tmp_flag = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_NE);
993 /* IRDA noise error interrupt occurred ------------------------------------*/
994 if((tmp_flag != RESET) && (tmp_it_source != RESET))
996 __HAL_IRDA_CLEAR_NEFLAG(hirda);
997 hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
1000 tmp_flag = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_ORE);
1001 /* IRDA Over-Run interrupt occurred ---------------------------------------*/
1002 if((tmp_flag != RESET) && (tmp_it_source != RESET))
1004 __HAL_IRDA_CLEAR_OREFLAG(hirda);
1005 hirda->ErrorCode |= HAL_IRDA_ERROR_ORE;
1008 /* Call the Error call Back in case of Errors */
1009 if(hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
1011 /* Disable PE and ERR interrupt */
1012 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1013 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1014 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
1016 /* Set the IRDA state ready to be able to start again the process */
1017 hirda->State = HAL_IRDA_STATE_READY;
1018 HAL_IRDA_ErrorCallback(hirda);
1021 tmp_flag = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_RXNE);
1022 tmp_it_source = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_RXNE);
1023 /* IRDA in mode Receiver --------------------------------------------------*/
1024 if((tmp_flag != RESET) && (tmp_it_source != RESET))
1026 IRDA_Receive_IT(hirda);
1029 tmp_flag = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_TXE);
1030 tmp_it_source = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TXE);
1031 /* IRDA in mode Transmitter -----------------------------------------------*/
1032 if((tmp_flag != RESET) && (tmp_it_source != RESET))
1034 IRDA_Transmit_IT(hirda);
1037 tmp_flag = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_TC);
1038 tmp_it_source = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TC);
1039 /* IRDA in mode Transmitter (transmission end) -----------------------------*/
1040 if((tmp_flag != RESET) && (tmp_it_source != RESET))
1042 IRDA_EndTransmit_IT(hirda);
1048 * @brief Tx Transfer completed callbacks.
1049 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1050 * the configuration information for the specified IRDA module.
1053 __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
1055 /* NOTE: This function should not be modified, when the callback is needed,
1056 the HAL_IRDA_TxCpltCallback can be implemented in the user file
1061 * @brief Tx Half Transfer completed callbacks.
1062 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1063 * the configuration information for the specified USART module.
1066 __weak void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
1068 /* NOTE: This function should not be modified, when the callback is needed,
1069 the HAL_IRDA_TxHalfCpltCallback can be implemented in the user file
1074 * @brief Rx Transfer completed callbacks.
1075 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1076 * the configuration information for the specified IRDA module.
1079 __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
1081 /* NOTE: This function should not be modified, when the callback is needed,
1082 the HAL_IRDA_RxCpltCallback can be implemented in the user file
1087 * @brief Rx Half Transfer complete callbacks.
1088 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1089 * the configuration information for the specified IRDA module.
1092 __weak void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
1094 /* NOTE : This function should not be modified, when the callback is needed,
1095 the HAL_IRDA_RxHalfCpltCallback can be implemented in the user file
1100 * @brief IRDA error callbacks.
1101 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1102 * the configuration information for the specified IRDA module.
1105 __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
1107 /* NOTE: This function should not be modified, when the callback is needed,
1108 the HAL_IRDA_ErrorCallback can be implemented in the user file
1116 /** @defgroup IRDA_Exported_Functions_Group3 Peripheral State and Errors functions
1117 * @brief IRDA State and Errors functions
1120 ==============================================================================
1121 ##### Peripheral State and Errors functions #####
1122 ==============================================================================
1124 This subsection provides a set of functions allowing to return the State of IrDA
1125 communication process and also return Peripheral Errors occurred during communication process
1126 (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state
1127 of the IRDA peripheral.
1128 (+) HAL_IRDA_GetError() check in run-time errors that could be occurred during
1136 * @brief Returns the IRDA state.
1137 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1138 * the configuration information for the specified IRDA module.
1141 HAL_IRDA_StateTypeDef HAL_IRDA_GetState(IRDA_HandleTypeDef *hirda)
1143 return hirda->State;
1147 * @brief Return the IRDA error code
1148 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1149 * the configuration information for the specified IRDA module.
1150 * @retval IRDA Error Code
1152 uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef *hirda)
1154 return hirda->ErrorCode;
1165 /** @defgroup IRDA_Private_Functions IRDA Private Functions
1166 * @brief IRDA Private functions
1170 * @brief DMA IRDA transmit process complete callback.
1171 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1172 * the configuration information for the specified DMA module.
1175 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1177 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1178 /* DMA Normal mode */
1179 if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
1181 hirda->TxXferCount = 0;
1183 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1184 in the IRDA CR3 register */
1185 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1187 /* Enable the IRDA Transmit Complete Interrupt */
1188 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TC);
1190 /* DMA Circular mode */
1193 HAL_IRDA_TxCpltCallback(hirda);
1198 * @brief DMA IRDA receive process half complete callback
1199 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1200 * the configuration information for the specified DMA module.
1203 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma)
1205 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1207 HAL_IRDA_TxHalfCpltCallback(hirda);
1211 * @brief DMA IRDA receive process complete callback.
1212 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1213 * the configuration information for the specified DMA module.
1216 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1218 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1219 /* DMA Normal mode */
1220 if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
1222 hirda->RxXferCount = 0;
1224 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1225 in the IRDA CR3 register */
1226 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1228 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
1230 hirda->State = HAL_IRDA_STATE_BUSY_TX;
1234 hirda->State = HAL_IRDA_STATE_READY;
1238 HAL_IRDA_RxCpltCallback(hirda);
1242 * @brief DMA IRDA receive process half complete callback
1243 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1244 * the configuration information for the specified DMA module.
1247 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma)
1249 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1251 HAL_IRDA_RxHalfCpltCallback(hirda);
1255 * @brief DMA IRDA communication error callback.
1256 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1257 * the configuration information for the specified DMA module.
1260 static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
1262 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1264 hirda->RxXferCount = 0;
1265 hirda->TxXferCount = 0;
1266 hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
1267 hirda->State= HAL_IRDA_STATE_READY;
1269 HAL_IRDA_ErrorCallback(hirda);
1273 * @brief This function handles IRDA Communication Timeout.
1274 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1275 * the configuration information for the specified IRDA module.
1276 * @param Flag: specifies the IRDA flag to check.
1277 * @param Status: The new Flag status (SET or RESET).
1278 * @param Timeout: Timeout duration
1279 * @retval HAL status
1281 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
1283 uint32_t tickstart = 0;
1286 tickstart = HAL_GetTick();
1288 /* Wait until flag is set */
1291 while(__HAL_IRDA_GET_FLAG(hirda, Flag) == RESET)
1293 /* Check for the Timeout */
1294 if(Timeout != HAL_MAX_DELAY)
1296 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1298 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1299 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
1300 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
1301 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1302 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1304 hirda->State= HAL_IRDA_STATE_READY;
1306 /* Process Unlocked */
1307 __HAL_UNLOCK(hirda);
1316 while(__HAL_IRDA_GET_FLAG(hirda, Flag) != RESET)
1318 /* Check for the Timeout */
1319 if(Timeout != HAL_MAX_DELAY)
1321 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1323 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1324 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
1325 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
1326 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1327 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1329 hirda->State= HAL_IRDA_STATE_READY;
1331 /* Process Unlocked */
1332 __HAL_UNLOCK(hirda);
1343 * @brief Send an amount of data in non-blocking mode.
1344 * Function called under interruption only, once
1345 * interruptions have been enabled by HAL_IRDA_Transmit_IT()
1346 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1347 * the configuration information for the specified IRDA module.
1348 * @retval HAL status
1350 static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
1353 uint32_t tmp_state = 0;
1355 tmp_state = hirda->State;
1356 if((tmp_state == HAL_IRDA_STATE_BUSY_TX) || (tmp_state == HAL_IRDA_STATE_BUSY_TX_RX))
1358 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
1360 tmp = (uint16_t*) hirda->pTxBuffPtr;
1361 WRITE_REG(hirda->Instance->DR, (uint16_t)(*tmp & IRDA_DR_MASK_U16_9DATABITS));
1362 if(hirda->Init.Parity == IRDA_PARITY_NONE)
1364 hirda->pTxBuffPtr += 2;
1368 hirda->pTxBuffPtr += 1;
1373 WRITE_REG(hirda->Instance->DR, (uint8_t)(*hirda->pTxBuffPtr++ & IRDA_DR_MASK_U8_8DATABITS));
1376 if(--hirda->TxXferCount == 0)
1378 /* Disable the IRDA Transmit Data Register Empty Interrupt */
1379 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
1381 /* Enable the IRDA Transmit Complete Interrupt */
1382 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TC);
1394 * @brief Wraps up transmission in non blocking mode.
1395 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1396 * the configuration information for the specified IRDA module.
1397 * @retval HAL status
1399 static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda)
1401 /* Disable the IRDA Transmit Complete Interrupt */
1402 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TC);
1404 /* Check if a receive process is ongoing or not */
1405 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
1407 hirda->State = HAL_IRDA_STATE_BUSY_RX;
1411 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1412 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1414 hirda->State = HAL_IRDA_STATE_READY;
1417 HAL_IRDA_TxCpltCallback(hirda);
1424 * @brief Receive an amount of data in non-blocking mode.
1425 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1426 * the configuration information for the specified IRDA module.
1427 * @retval HAL status
1429 static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
1432 uint32_t tmp_state = 0;
1434 tmp_state = hirda->State;
1435 if((tmp_state == HAL_IRDA_STATE_BUSY_RX) || (tmp_state == HAL_IRDA_STATE_BUSY_TX_RX))
1437 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
1439 tmp = (uint16_t*) hirda->pRxBuffPtr;
1440 if(hirda->Init.Parity == IRDA_PARITY_NONE)
1442 *tmp = (uint16_t)(hirda->Instance->DR & IRDA_DR_MASK_U16_9DATABITS);
1443 hirda->pRxBuffPtr += 2;
1447 *tmp = (uint16_t)(hirda->Instance->DR & IRDA_DR_MASK_U16_8DATABITS);
1448 hirda->pRxBuffPtr += 1;
1453 if(hirda->Init.Parity == IRDA_PARITY_NONE)
1455 *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->DR & IRDA_DR_MASK_U8_8DATABITS);
1459 *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->DR & IRDA_DR_MASK_U8_7DATABITS);
1463 if(--hirda->RxXferCount == 0)
1466 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
1468 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
1470 hirda->State = HAL_IRDA_STATE_BUSY_TX;
1474 /* Disable the IRDA Parity Error Interrupt */
1475 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1477 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1478 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1480 hirda->State = HAL_IRDA_STATE_READY;
1482 HAL_IRDA_RxCpltCallback(hirda);
1495 * @brief Configures the IRDA peripheral.
1496 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1497 * the configuration information for the specified IRDA module.
1500 static void IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
1502 /* Check the parameters */
1503 assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));
1504 assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
1505 assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
1506 assert_param(IS_IRDA_MODE(hirda->Init.Mode));
1508 /*------- IRDA-associated USART registers setting : CR2 Configuration ------*/
1509 /* Clear STOP[13:12] bits */
1510 CLEAR_BIT(hirda->Instance->CR2, USART_CR2_STOP);
1512 /*------- IRDA-associated USART registers setting : CR1 Configuration ------*/
1513 /* Configure the USART Word Length, Parity and mode:
1514 Set the M bits according to hirda->Init.WordLength value
1515 Set PCE and PS bits according to hirda->Init.Parity value
1516 Set TE and RE bits according to hirda->Init.Mode value */
1517 MODIFY_REG(hirda->Instance->CR1,
1518 ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)),
1519 (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode);
1521 /*------- IRDA-associated USART registers setting : CR3 Configuration ------*/
1522 /* Clear CTSE and RTSE bits */
1523 CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
1525 /*------- IRDA-associated USART registers setting : BRR Configuration ------*/
1526 if(hirda->Instance == USART1)
1528 hirda->Instance->BRR = IRDA_BRR(HAL_RCC_GetPCLK2Freq(), hirda->Init.BaudRate);
1532 hirda->Instance->BRR = IRDA_BRR(HAL_RCC_GetPCLK1Freq(), hirda->Init.BaudRate);
1539 #endif /* HAL_IRDA_MODULE_ENABLED */
1548 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/