2 ******************************************************************************
3 * @file stm32l1xx_hal_irda.c
4 * @author MCD Application Team
6 * @date 5-September-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 these IRDA pins as alternate function pull-up.
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.
41 (#) Program the Baud Rate, Word Length, Parity, IrDA Mode, Prescaler
42 and Mode(Receiver/Transmitter) in the hirda Init structure.
44 (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
45 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
46 by calling the customed HAL_IRDA_MspInit() API.
48 -@@- The specific IRDA interrupts (Transmission complete interrupt,
49 RXNE interrupt and Error Interrupts) will be managed using the macros
50 __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
52 (#) Three operation modes are available within this driver :
54 *** Polling mode IO operation ***
55 =================================
57 (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
58 (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
60 *** Interrupt mode IO operation ***
61 ===================================
63 (+) Send an amount of data in non blocking mode using HAL_IRDA_Transmit_IT()
64 (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
65 add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
66 (+) Receive an amount of data in non blocking mode using HAL_IRDA_Receive_IT()
67 (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
68 add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
69 (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
70 add his own code by customization of function pointer HAL_IRDA_ErrorCallback
72 *** DMA mode IO operation ***
73 ==============================
75 (+) Send an amount of data in non blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
76 (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
77 add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
78 (+) Receive an amount of data in non blocking mode (DMA) using HAL_IRDA_Receive_DMA()
79 (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
80 add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
81 (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
82 add his own code by customization of function pointer HAL_IRDA_ErrorCallback
84 *** IRDA HAL driver macros list ***
85 ====================================
87 Below the list of most used macros in IRDA HAL driver.
89 (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
90 (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
91 (+) __HAL_IRDA_GET_FLAG : Check whether the specified IRDA flag is set or not
92 (+) __HAL_IRDA_CLEAR_FLAG : Clear the specified IRDA pending flag
93 (+) __HAL_IRDA_ENABLE_IT: Enable the specified IRDA interrupt
94 (+) __HAL_IRDA_DISABLE_IT: Disable the specified IRDA interrupt
97 (@) You can refer to the IRDA HAL driver header file for more useful macros
100 ******************************************************************************
103 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
105 * Redistribution and use in source and binary forms, with or without modification,
106 * are permitted provided that the following conditions are met:
107 * 1. Redistributions of source code must retain the above copyright notice,
108 * this list of conditions and the following disclaimer.
109 * 2. Redistributions in binary form must reproduce the above copyright notice,
110 * this list of conditions and the following disclaimer in the documentation
111 * and/or other materials provided with the distribution.
112 * 3. Neither the name of STMicroelectronics nor the names of its contributors
113 * may be used to endorse or promote products derived from this software
114 * without specific prior written permission.
116 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
117 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
118 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
119 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
120 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
121 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
122 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
123 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
124 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
125 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
127 ******************************************************************************
130 /* Includes ------------------------------------------------------------------*/
131 #include "stm32l1xx_hal.h"
133 /** @addtogroup STM32L1xx_HAL_Driver
137 /** @defgroup IRDA IRDA
138 * @brief HAL IRDA module driver
142 #ifdef HAL_IRDA_MODULE_ENABLED
144 /* Private typedef -----------------------------------------------------------*/
145 /* Private define ------------------------------------------------------------*/
146 /** @defgroup IRDA_Private_Constants IRDA Private Constants
149 #define IRDA_TIMEOUT_VALUE 22000
150 #define IRDA_DR_MASK_U16_7DATABITS (uint16_t)0x007F
151 #define IRDA_DR_MASK_U16_8DATABITS (uint16_t)0x00FF
152 #define IRDA_DR_MASK_U16_9DATABITS (uint16_t)0x01FF
154 #define IRDA_DR_MASK_U8_7DATABITS (uint8_t)0x7F
155 #define IRDA_DR_MASK_U8_8DATABITS (uint8_t)0xFF
162 /* Private macro -------------------------------------------------------------*/
163 /* Private variables ---------------------------------------------------------*/
164 /* Private function prototypes -----------------------------------------------*/
165 /** @addtogroup IRDA_Private_Functions IRDA Private Functions
168 static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
169 static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda);
170 static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
171 static void IRDA_SetConfig (IRDA_HandleTypeDef *hirda);
172 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
173 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma);
174 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
175 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma);
176 static void IRDA_DMAError(DMA_HandleTypeDef *hdma);
177 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
182 /* Exported functions ---------------------------------------------------------*/
184 /** @defgroup IRDA_Exported_Functions IRDA Exported Functions
188 /** @defgroup IRDA_Exported_Functions_Group1 IrDA Initialization and de-initialization functions
189 * @brief Initialization and Configuration functions
192 ==============================================================================
193 ##### Initialization and Configuration functions #####
194 ==============================================================================
196 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
198 (+) For the asynchronous mode only these parameters can be configured:
201 (++) Parity: If the parity is enabled, then the MSB bit of the data written
202 in the data register is transmitted but is changed by the parity bit.
203 Depending on the frame length defined by the M bit (8-bits or 9-bits),
204 the possible IRDA frame formats are as listed in the following table:
205 +-------------------------------------------------------------+
206 | M bit | PCE bit | IRDA frame |
207 |---------------------|---------------------------------------|
208 | 0 | 0 | | SB | 8 bit data | STB | |
209 |---------|-----------|---------------------------------------|
210 | 0 | 1 | | SB | 7 bit data | PB | STB | |
211 |---------|-----------|---------------------------------------|
212 | 1 | 0 | | SB | 9 bit data | STB | |
213 |---------|-----------|---------------------------------------|
214 | 1 | 1 | | SB | 8 bit data | PB | STB | |
215 +-------------------------------------------------------------+
216 (++) Prescaler: A pulse of width less than two and greater than one PSC period(s) may or may
217 not be rejected. The receiver set up time should be managed by software. The IrDA physical layer
218 specification specifies a minimum of 10 ms delay between transmission and
219 reception (IrDA is a half duplex protocol).
220 (++) Mode: Receiver/transmitter modes
221 (++) IrDAMode: the IrDA can operate in the Normal mode or in the Low power mode.
224 The HAL_IRDA_Init() function follows IRDA configuration procedures (details for the procedures
225 are available in reference manual (RM0038)).
232 * @brief Initializes the IRDA mode according to the specified
233 * parameters in the IRDA_InitTypeDef and create the associated handle.
234 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
235 * the configuration information for the specified IRDA module.
238 HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
240 /* Check the IRDA handle allocation */
241 if(hirda == HAL_NULL)
246 /* Check the IRDA instance parameters */
247 assert_param(IS_IRDA_INSTANCE(hirda->Instance));
248 /* Check the IRDA mode parameter in the IRDA handle */
249 assert_param(IS_IRDA_POWERMODE(hirda->Init.IrDAMode));
251 if(hirda->State == HAL_IRDA_STATE_RESET)
253 /* Init the low level hardware */
254 HAL_IRDA_MspInit(hirda);
257 hirda->State = HAL_IRDA_STATE_BUSY;
259 /* Disable the IRDA peripheral */
260 __HAL_IRDA_DISABLE(hirda);
262 /* Set the IRDA communication parameters */
263 IRDA_SetConfig(hirda);
265 /* In IrDA mode, the following bits must be kept cleared:
266 - LINEN, STOP and CLKEN bits in the USART_CR2 register,
267 - SCEN and HDSEL bits in the USART_CR3 register.*/
268 CLEAR_BIT(hirda->Instance->CR2, (USART_CR2_LINEN | USART_CR2_STOP | USART_CR2_CLKEN));
269 CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
271 /* Enable the IRDA peripheral */
272 __HAL_IRDA_ENABLE(hirda);
274 /* Set the prescaler */
275 MODIFY_REG(hirda->Instance->GTPR, USART_GTPR_PSC, hirda->Init.Prescaler);
277 /* Configure the IrDA mode */
278 MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.IrDAMode);
280 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
281 SET_BIT(hirda->Instance->CR3, USART_CR3_IREN);
283 /* Initialize the IRDA state*/
284 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
285 hirda->State= HAL_IRDA_STATE_READY;
291 * @brief DeInitializes the IRDA peripheral
292 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
293 * the configuration information for the specified IRDA module.
296 HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
298 /* Check the IRDA handle allocation */
299 if(hirda == HAL_NULL)
304 /* Check the parameters */
305 assert_param(IS_IRDA_INSTANCE(hirda->Instance));
307 hirda->State = HAL_IRDA_STATE_BUSY;
309 /* Disable the Peripheral */
310 __HAL_IRDA_DISABLE(hirda);
312 /* DeInit the low level hardware */
313 HAL_IRDA_MspDeInit(hirda);
315 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
316 hirda->State = HAL_IRDA_STATE_RESET;
325 * @brief IRDA MSP Init.
326 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
327 * the configuration information for the specified IRDA module.
330 __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
332 /* NOTE: This function should not be modified, when the callback is needed,
333 the HAL_IRDA_MspInit can be implemented in the user file
338 * @brief IRDA MSP DeInit.
339 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
340 * the configuration information for the specified IRDA module.
343 __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
345 /* NOTE: This function should not be modified, when the callback is needed,
346 the HAL_IRDA_MspDeInit can be implemented in the user file
354 /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
355 * @brief IRDA Transmit and Receive functions
358 ==============================================================================
359 ##### IO operation functions #####
360 ==============================================================================
362 This subsection provides a set of functions allowing to manage the IRDA data transfers.
365 IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
366 on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
367 is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
368 While receiving data, transmission should be avoided as the data to be transmitted
371 (#) There are two modes of transfer:
372 (++) Blocking mode: The communication is performed in polling mode.
373 The HAL status of all data processing is returned by the same function
374 after finishing transfer.
375 (++) No-Blocking mode: The communication is performed using Interrupts
376 or DMA, These API's return the HAL status.
377 The end of the data processing will be indicated through the
378 dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
380 The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
381 will be executed respectivelly at the end of the transmit or Receive process
382 The HAL_IRDA_ErrorCallback() user callback will be executed when a communication
385 (#) Blocking mode APIs are :
386 (++) HAL_IRDA_Transmit()
387 (++) HAL_IRDA_Receive()
389 (#) Non Blocking mode APIs with Interrupt are :
390 (++) HAL_IRDA_Transmit_IT()
391 (++) HAL_IRDA_Receive_IT()
392 (++) HAL_IRDA_IRQHandler()
394 (#) Non Blocking mode functions with DMA are :
395 (++) HAL_IRDA_Transmit_DMA()
396 (++) HAL_IRDA_Receive_DMA()
397 (++) HAL_IRDA_DMAPause()
398 (++) HAL_IRDA_DMAResume()
399 (++) HAL_IRDA_DMAStop()
401 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
402 (++) HAL_IRDA_TxHalfCpltCallback()
403 (++) HAL_IRDA_TxCpltCallback()
404 (++) HAL_IRDA_RxHalfCpltCallback()
405 (++) HAL_IRDA_RxCpltCallback()
406 (++) HAL_IRDA_ErrorCallback()
413 * @brief Sends an amount of data in blocking mode.
414 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
415 * the configuration information for the specified IRDA module.
416 * @param pData: Pointer to data buffer
417 * @param Size: Amount of data to be sent
418 * @param Timeout: Specify timeout value
421 HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
427 if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_RX))
429 if((pData == HAL_NULL) || (Size == 0))
437 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
438 if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
440 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
444 hirda->State = HAL_IRDA_STATE_BUSY_TX;
447 hirda->TxXferSize = Size;
448 hirda->TxXferCount = Size;
449 while(hirda->TxXferCount > 0)
451 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
453 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
457 tmp = (uint16_t*) pData;
458 WRITE_REG(hirda->Instance->DR,(*tmp & IRDA_DR_MASK_U16_9DATABITS));
459 if(hirda->Init.Parity == IRDA_PARITY_NONE)
470 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
474 WRITE_REG(hirda->Instance->DR, (*pData++ & IRDA_DR_MASK_U8_8DATABITS));
476 hirda->TxXferCount--;
479 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, Timeout) != HAL_OK)
484 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
486 hirda->State = HAL_IRDA_STATE_BUSY_RX;
490 hirda->State = HAL_IRDA_STATE_READY;
493 /* Process Unlocked */
505 * @brief Receive an amount of data in blocking mode.
506 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
507 * the configuration information for the specified IRDA module.
508 * @param pData: Pointer to data buffer
509 * @param Size: Amount of data to be received
510 * @param Timeout: Specify timeout value
513 HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
519 if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_TX))
521 if((pData == HAL_NULL) || (Size == 0))
529 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
530 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
532 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
536 hirda->State = HAL_IRDA_STATE_BUSY_RX;
538 hirda->RxXferSize = Size;
539 hirda->RxXferCount = Size;
540 /* Check the remain data to be received */
541 while(hirda->RxXferCount > 0)
543 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
545 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
549 tmp = (uint16_t*) pData ;
550 if(hirda->Init.Parity == IRDA_PARITY_NONE)
552 *tmp = (uint16_t)(hirda->Instance->DR & IRDA_DR_MASK_U16_9DATABITS);
557 *tmp = (uint16_t)(hirda->Instance->DR & IRDA_DR_MASK_U16_8DATABITS);
563 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
567 if(hirda->Init.Parity == IRDA_PARITY_NONE)
569 *pData++ = (uint8_t)(hirda->Instance->DR & IRDA_DR_MASK_U8_8DATABITS);
573 *pData++ = (uint8_t)(hirda->Instance->DR & IRDA_DR_MASK_U8_7DATABITS);
576 hirda->RxXferCount--;
578 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
580 hirda->State = HAL_IRDA_STATE_BUSY_TX;
584 hirda->State = HAL_IRDA_STATE_READY;
587 /* Process Unlocked */
599 * @brief Sends an amount of data in non-blocking mode.
600 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
601 * the configuration information for the specified IRDA module.
602 * @param pData: Pointer to data buffer
603 * @param Size: Amount of data to be sent
606 HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
611 if((tmp == HAL_IRDA_STATE_READY) || (tmp == HAL_IRDA_STATE_BUSY_RX))
613 if((pData == HAL_NULL) || (Size == 0))
620 hirda->pTxBuffPtr = pData;
621 hirda->TxXferSize = Size;
622 hirda->TxXferCount = Size;
624 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
625 if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
627 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
631 hirda->State = HAL_IRDA_STATE_BUSY_TX;
634 /* Enable the IRDA Parity Error Interrupt */
635 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_PE);
637 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
638 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
640 /* Process Unlocked */
643 /* Enable the IRDA Transmit Data Register Empty Interrupt */
644 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TXE);
655 * @brief Receives an amount of data in non-blocking mode.
656 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
657 * the configuration information for the specified IRDA module.
658 * @param pData: Pointer to data buffer
659 * @param Size: Amount of data to be received
662 HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
667 if((tmp == HAL_IRDA_STATE_READY) || (tmp == HAL_IRDA_STATE_BUSY_TX))
669 if((pData == HAL_NULL) || (Size == 0))
677 hirda->pRxBuffPtr = pData;
678 hirda->RxXferSize = Size;
679 hirda->RxXferCount = Size;
681 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
682 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
684 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
688 hirda->State = HAL_IRDA_STATE_BUSY_RX;
691 /* Enable the IRDA Data Register not empty Interrupt */
692 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_RXNE);
694 /* Enable the IRDA Parity Error Interrupt */
695 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_PE);
697 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
698 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
700 /* Process Unlocked */
712 * @brief Sends an amount of data in non-blocking mode.
713 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
714 * the configuration information for the specified IRDA module.
715 * @param pData: Pointer to data buffer
716 * @param Size: Amount of data to be sent
719 HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
725 if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_RX))
727 if((pData == HAL_NULL) || (Size == 0))
735 hirda->pTxBuffPtr = pData;
736 hirda->TxXferSize = Size;
737 hirda->TxXferCount = Size;
738 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
740 if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
742 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
746 hirda->State = HAL_IRDA_STATE_BUSY_TX;
749 /* Set the IRDA DMA transfer complete callback */
750 hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
752 /* Set the IRDA DMA half transfert complete callback */
753 hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;
755 /* Set the DMA error callback */
756 hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
758 /* Enable the IRDA transmit DMA channel */
759 tmp = (uint32_t*)&pData;
760 HAL_DMA_Start_IT(hirda->hdmatx, *(uint32_t*)tmp, (uint32_t)&hirda->Instance->DR, Size);
762 /* Enable the DMA transfer for transmit request by setting the DMAT bit
763 in the USART CR3 register */
764 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
766 /* Process Unlocked */
778 * @brief Receive an amount of data in non-blocking mode.
779 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
780 * the configuration information for the specified IRDA module.
781 * @param pData: Pointer to data buffer
782 * @param Size: Amount of data to be received
783 * @note When the IRDA parity is enabled (PCE = 1) the data received contain the parity bit.
786 HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
792 if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_TX))
794 if((pData == HAL_NULL) || (Size == 0))
802 hirda->pRxBuffPtr = pData;
803 hirda->RxXferSize = Size;
804 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
805 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
807 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
811 hirda->State = HAL_IRDA_STATE_BUSY_RX;
814 /* Set the IRDA DMA transfer complete callback */
815 hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
817 /* Set the IRDA DMA half transfert complete callback */
818 hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;
820 /* Set the DMA error callback */
821 hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
823 /* Enable the DMA channel */
824 tmp = (uint32_t*)&pData;
825 HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->DR, *(uint32_t*)tmp, Size);
827 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
828 in the USART CR3 register */
829 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
831 /* Process Unlocked */
843 * @brief Pauses the DMA Transfer.
844 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
845 * the configuration information for the specified IRDA module.
848 HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda)
853 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
855 /* Disable the UART DMA Tx request */
856 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
858 else if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
860 /* Disable the UART DMA Rx request */
861 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
863 else if (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
865 /* Disable the UART DMA Tx & Rx requests */
866 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
867 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
871 /* Process Unlocked */
877 /* Process Unlocked */
884 * @brief Resumes the DMA Transfer.
885 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
886 * the configuration information for the specified UART module.
889 HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda)
894 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
896 /* Enable the UART DMA Tx request */
897 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
899 else if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
901 /* Clear the Overrun flag before resumming the Rx transfer*/
902 __HAL_IRDA_CLEAR_OREFLAG(hirda);
903 /* Enable the UART DMA Rx request */
904 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
906 else if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
908 /* Clear the Overrun flag before resumming the Rx transfer*/
909 __HAL_IRDA_CLEAR_OREFLAG(hirda);
910 /* Enable the UART DMA Tx & Rx request */
911 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
912 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
916 /* Process Unlocked */
922 /* Process Unlocked */
929 * @brief Stops the DMA Transfer.
930 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
931 * the configuration information for the specified UART module.
934 HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda)
936 /* The Lock is not implemented on this API to allow the user application
937 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
938 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
939 and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
942 /* Disable the UART Tx/Rx DMA requests */
943 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
944 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
946 /* Abort the UART DMA tx channel */
947 if(hirda->hdmatx != HAL_NULL)
949 HAL_DMA_Abort(hirda->hdmatx);
951 /* Abort the UART DMA rx channel */
952 if(hirda->hdmarx != HAL_NULL)
954 HAL_DMA_Abort(hirda->hdmarx);
957 hirda->State = HAL_IRDA_STATE_READY;
963 * @brief This function handles IRDA interrupt request.
964 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
965 * the configuration information for the specified IRDA module.
968 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
970 uint32_t tmp1 = 0, tmp2 = 0;
972 tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_PE);
973 tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_PE);
974 /* IRDA parity error interrupt occurred -----------------------------------*/
975 if((tmp1 != RESET) && (tmp2 != RESET))
977 __HAL_IRDA_CLEAR_PEFLAG(hirda);
978 hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
981 tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_FE);
982 tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR);
983 /* IRDA frame error interrupt occurred ------------------------------------*/
984 if((tmp1 != RESET) && (tmp2 != RESET))
986 __HAL_IRDA_CLEAR_FEFLAG(hirda);
987 hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
990 tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_NE);
991 tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR);
992 /* IRDA noise error interrupt occurred ------------------------------------*/
993 if((tmp1 != RESET) && (tmp2 != RESET))
995 __HAL_IRDA_CLEAR_NEFLAG(hirda);
996 hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
999 tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_ORE);
1000 tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR);
1001 /* IRDA Over-Run interrupt occurred ---------------------------------------*/
1002 if((tmp1 != RESET) && (tmp2 != 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 tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_RXNE);
1022 tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_RXNE);
1023 /* IRDA in mode Receiver --------------------------------------------------*/
1024 if((tmp1 != RESET) && (tmp2 != RESET))
1026 IRDA_Receive_IT(hirda);
1029 tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_TXE);
1030 tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TXE);
1031 /* IRDA in mode Transmitter -----------------------------------------------*/
1032 if((tmp1 != RESET) && (tmp2 != RESET))
1034 IRDA_Transmit_IT(hirda);
1037 tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_TC);
1038 tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TC);
1039 /* IRDA in mode Transmitter (transmission end) -----------------------------*/
1040 if((tmp1 != RESET) && (tmp2 != 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((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
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 /* Wait for IRDA TC Flag */
1188 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, IRDA_TIMEOUT_VALUE) != HAL_OK)
1190 /* Timeout occurred */
1191 hirda->State = HAL_IRDA_STATE_TIMEOUT;
1192 HAL_IRDA_ErrorCallback(hirda);
1197 /* Check if a receive process is ongoing or not */
1198 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
1200 hirda->State = HAL_IRDA_STATE_BUSY_RX;
1204 hirda->State = HAL_IRDA_STATE_READY;
1206 HAL_IRDA_TxCpltCallback(hirda);
1209 /* DMA Circular mode */
1212 HAL_IRDA_TxCpltCallback(hirda);
1217 * @brief DMA IRDA receive process half complete callback
1218 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1219 * the configuration information for the specified DMA module.
1222 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma)
1224 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1226 HAL_IRDA_TxHalfCpltCallback(hirda);
1230 * @brief DMA IRDA receive process complete callback.
1231 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1232 * the configuration information for the specified DMA module.
1235 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1237 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1238 /* DMA Normal mode */
1239 if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
1241 hirda->RxXferCount = 0;
1243 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1244 in the IRDA CR3 register */
1245 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1247 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
1249 hirda->State = HAL_IRDA_STATE_BUSY_TX;
1253 hirda->State = HAL_IRDA_STATE_READY;
1257 HAL_IRDA_RxCpltCallback(hirda);
1261 * @brief DMA IRDA receive process half complete callback
1262 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1263 * the configuration information for the specified DMA module.
1266 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma)
1268 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1270 HAL_IRDA_RxHalfCpltCallback(hirda);
1274 * @brief DMA IRDA communication error callback.
1275 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1276 * the configuration information for the specified DMA module.
1279 static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
1281 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1283 hirda->RxXferCount = 0;
1284 hirda->TxXferCount = 0;
1285 hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
1286 hirda->State= HAL_IRDA_STATE_READY;
1288 HAL_IRDA_ErrorCallback(hirda);
1292 * @brief This function handles IRDA Communication Timeout.
1293 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1294 * the configuration information for the specified IRDA module.
1295 * @param Flag: specifies the IRDA flag to check.
1296 * @param Status: The new Flag status (SET or RESET).
1297 * @param Timeout: Timeout duration
1298 * @retval HAL status
1300 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
1302 uint32_t tickstart = 0;
1305 tickstart = HAL_GetTick();
1307 /* Wait until flag is set */
1310 while(__HAL_IRDA_GET_FLAG(hirda, Flag) == RESET)
1312 /* Check for the Timeout */
1313 if(Timeout != HAL_MAX_DELAY)
1315 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1317 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1318 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
1319 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
1320 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1321 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1323 hirda->State= HAL_IRDA_STATE_READY;
1325 /* Process Unlocked */
1326 __HAL_UNLOCK(hirda);
1335 while(__HAL_IRDA_GET_FLAG(hirda, Flag) != RESET)
1337 /* Check for the Timeout */
1338 if(Timeout != HAL_MAX_DELAY)
1340 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1342 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1343 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
1344 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
1345 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1346 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1348 hirda->State= HAL_IRDA_STATE_READY;
1350 /* Process Unlocked */
1351 __HAL_UNLOCK(hirda);
1362 * @brief Send an amount of data in non-blocking mode.
1363 * Function called under interruption only, once
1364 * interruptions have been enabled by HAL_IRDA_Transmit_IT()
1365 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1366 * the configuration information for the specified IRDA module.
1367 * @retval HAL status
1369 static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
1374 tmp1 = hirda->State;
1375 if((tmp1 == HAL_IRDA_STATE_BUSY_TX) || (tmp1 == HAL_IRDA_STATE_BUSY_TX_RX))
1377 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
1379 tmp = (uint16_t*) hirda->pTxBuffPtr;
1380 WRITE_REG(hirda->Instance->DR, (uint16_t)(*tmp & IRDA_DR_MASK_U16_9DATABITS));
1381 if(hirda->Init.Parity == IRDA_PARITY_NONE)
1383 hirda->pTxBuffPtr += 2;
1387 hirda->pTxBuffPtr += 1;
1392 WRITE_REG(hirda->Instance->DR, (uint8_t)(*hirda->pTxBuffPtr++ & IRDA_DR_MASK_U8_8DATABITS));
1395 if(--hirda->TxXferCount == 0)
1397 /* Disable the IRDA Transmit Data Register Empty Interrupt */
1398 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
1400 /* Enable the IRDA Transmit Complete Interrupt */
1401 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TC);
1413 * @brief Wraps up transmission in non blocking mode.
1414 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1415 * the configuration information for the specified IRDA module.
1416 * @retval HAL status
1418 static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda)
1420 /* Disable the IRDA Transmit Complete Interrupt */
1421 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TC);
1423 /* Check if a receive process is ongoing or not */
1424 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
1426 hirda->State = HAL_IRDA_STATE_BUSY_RX;
1430 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1431 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1433 /* Disable the IRDA Parity Error Interrupt */
1434 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1436 hirda->State = HAL_IRDA_STATE_READY;
1439 HAL_IRDA_TxCpltCallback(hirda);
1446 * @brief Receive an amount of data in non-blocking mode.
1447 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1448 * the configuration information for the specified IRDA module.
1449 * @retval HAL status
1451 static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
1456 tmp1 = hirda->State;
1457 if((tmp1 == HAL_IRDA_STATE_BUSY_RX) || (tmp1 == HAL_IRDA_STATE_BUSY_TX_RX))
1459 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
1461 tmp = (uint16_t*) hirda->pRxBuffPtr;
1462 if(hirda->Init.Parity == IRDA_PARITY_NONE)
1464 *tmp = (uint16_t)(hirda->Instance->DR & IRDA_DR_MASK_U16_9DATABITS);
1465 hirda->pRxBuffPtr += 2;
1469 *tmp = (uint16_t)(hirda->Instance->DR & IRDA_DR_MASK_U16_8DATABITS);
1470 hirda->pRxBuffPtr += 1;
1475 if(hirda->Init.Parity == IRDA_PARITY_NONE)
1477 *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->DR & IRDA_DR_MASK_U8_8DATABITS);
1481 *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->DR & IRDA_DR_MASK_U8_7DATABITS);
1485 if(--hirda->RxXferCount == 0)
1488 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
1490 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
1492 hirda->State = HAL_IRDA_STATE_BUSY_TX;
1496 /* Disable the IRDA Parity Error Interrupt */
1497 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
1499 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1500 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
1502 hirda->State = HAL_IRDA_STATE_READY;
1504 HAL_IRDA_RxCpltCallback(hirda);
1517 * @brief Configures the IRDA peripheral.
1518 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1519 * the configuration information for the specified IRDA module.
1522 static void IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
1524 /* Check the parameters */
1525 assert_param(IS_IRDA_INSTANCE(hirda->Instance));
1526 assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));
1527 assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
1528 assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
1529 assert_param(IS_IRDA_MODE(hirda->Init.Mode));
1531 /*-------------------------- IRDA CR2 Configuration ------------------------*/
1532 /* Clear STOP[13:12] bits */
1533 CLEAR_BIT(hirda->Instance->CR2, USART_CR2_STOP);
1535 /*-------------------------- USART CR1 Configuration -----------------------*/
1536 /* Configure the USART Word Length, Parity and mode:
1537 Set the M bits according to hirda->Init.WordLength value
1538 Set PCE and PS bits according to hirda->Init.Parity value
1539 Set TE and RE bits according to hirda->Init.Mode value */
1540 MODIFY_REG(hirda->Instance->CR1,
1541 ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)),
1542 (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode);
1544 /*-------------------------- USART CR3 Configuration -----------------------*/
1545 /* Clear CTSE and RTSE bits */
1546 CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
1548 /*-------------------------- USART BRR Configuration -----------------------*/
1549 if(hirda->Instance == USART1)
1551 hirda->Instance->BRR = IRDA_BRR(HAL_RCC_GetPCLK2Freq(), hirda->Init.BaudRate);
1555 hirda->Instance->BRR = IRDA_BRR(HAL_RCC_GetPCLK1Freq(), hirda->Init.BaudRate);
1562 #endif /* HAL_IRDA_MODULE_ENABLED */
1571 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/