2 ******************************************************************************
3 * @file stm32f7xx_hal_irda.c
4 * @author MCD Application Team
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
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
19 The IRDA HAL driver can be used as follows:
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 initialized 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.
40 (#) Program the Baud Rate, Word Length, Parity, IrDA Mode, Prescaler
41 and Mode(Receiver/Transmitter) in the hirda Init structure.
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 customized 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.
50 (#) Three operation modes are available within this driver :
52 *** Polling mode IO operation ***
53 =================================
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()
58 *** Interrupt mode IO operation ***
59 ===================================
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
70 *** DMA mode IO operation ***
71 =============================
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
82 *** IRDA HAL driver macros list ***
83 ===================================
85 Below the list of most used macros in IRDA HAL driver.
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
94 (@) You can refer to the IRDA HAL driver header file for more useful macros
97 ******************************************************************************
100 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
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.
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.
124 ******************************************************************************
127 /* Includes ------------------------------------------------------------------*/
128 #include "stm32f7xx_hal.h"
130 /** @addtogroup STM32F7xx_HAL_Driver
134 /** @defgroup IRDA IRDA
135 * @brief HAL IRDA module driver
138 #ifdef HAL_IRDA_MODULE_ENABLED
140 /* Private typedef -----------------------------------------------------------*/
141 /* Private define ------------------------------------------------------------*/
142 /** @addtogroup IRDA_Private_Constants
145 #define TEACK_REACK_TIMEOUT 1000U
146 #define HAL_IRDA_TXDMA_TIMEOUTVALUE 22000U
147 #define IRDA_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE \
148 | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE))
152 /* Private macro -------------------------------------------------------------*/
153 /* Private variables ---------------------------------------------------------*/
154 /* Private function prototypes -----------------------------------------------*/
155 /** @addtogroup IRDA_Private_Functions
158 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
159 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef
*hdma
);
160 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
161 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef
*hdma
);
162 static void IRDA_DMAError(DMA_HandleTypeDef
*hdma
);
163 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
164 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef
*hirda
);
165 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef
*hirda
);
166 static void IRDA_SetConfig (IRDA_HandleTypeDef
*hirda
);
167 static HAL_StatusTypeDef
IRDA_CheckIdleState(IRDA_HandleTypeDef
*hirda
);
168 static HAL_StatusTypeDef
IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef
*hirda
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
);
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
);
175 /* Exported functions --------------------------------------------------------*/
176 /** @defgroup IRDA_Exported_Functions IrDA Exported Functions
180 /** @defgroup IRDA_Exported_Functions_Group1 IrDA Initialization and de-initialization functions
181 * @brief Initialization and Configuration functions
185 ===============================================================================
186 ##### Initialization and Configuration functions #####
187 ===============================================================================
189 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
191 (+) For the asynchronous mode only these parameters can be configured:
194 (++) Parity: If the parity is enabled, then the MSB bit of the data written
195 in the data register is transmitted but is changed by the parity bit.
196 Depending on the frame length defined by the M bit (8-bits or 9-bits),
197 please refer to Reference manual for possible IRDA frame formats.
198 (++) Prescaler: A pulse of width less than two and greater than one PSC period(s) may or may
199 not be rejected. The receiver set up time should be managed by software. The IrDA physical layer
200 specification specifies a minimum of 10 ms delay between transmission and
201 reception (IrDA is a half duplex protocol).
202 (++) Mode: Receiver/transmitter modes
203 (++) IrDAMode: the IrDA can operate in the Normal mode or in the Low power mode.
205 The HAL_IRDA_Init() API follows IRDA configuration procedures (details for the procedures
206 are available in reference manual).
213 * @brief Initializes the IRDA mode according to the specified
214 * parameters in the IRDA_InitTypeDef and create the associated handle.
215 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
216 * the configuration information for the specified IRDA module.
219 HAL_StatusTypeDef
HAL_IRDA_Init(IRDA_HandleTypeDef
*hirda
)
221 /* Check the IRDA handle allocation */
227 /* Check the USART/UART associated to the IRDA handle */
228 assert_param(IS_IRDA_INSTANCE(hirda
->Instance
));
230 if(hirda
->gState
== HAL_IRDA_STATE_RESET
)
232 /* Allocate lock resource and initialize it */
233 hirda
->Lock
= HAL_UNLOCKED
;
234 /* Init the low level hardware : GPIO, CLOCK, CORTEX */
235 HAL_IRDA_MspInit(hirda
);
238 hirda
->gState
= HAL_IRDA_STATE_BUSY
;
240 /* Disable the Peripheral to update the configuration registers */
241 __HAL_IRDA_DISABLE(hirda
);
243 /* Set the IRDA Communication parameters */
244 IRDA_SetConfig(hirda
);
246 /* In IRDA mode, the following bits must be kept cleared:
247 - LINEN, STOP and CLKEN bits in the USART_CR2 register,
248 - SCEN and HDSEL bits in the USART_CR3 register.*/
249 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR2_LINEN
| USART_CR2_STOP
| USART_CR2_CLKEN
);
250 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_SCEN
| USART_CR3_HDSEL
);
252 /* set the UART/USART in IRDA mode */
253 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_IREN
);
255 /* Enable the Peripheral */
256 __HAL_IRDA_ENABLE(hirda
);
258 /* TEACK and/or REACK to check before moving hirda->State to Ready */
259 return (IRDA_CheckIdleState(hirda
));
263 * @brief DeInitializes the IRDA peripheral
264 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
265 * the configuration information for the specified IRDA module.
268 HAL_StatusTypeDef
HAL_IRDA_DeInit(IRDA_HandleTypeDef
*hirda
)
270 /* Check the IRDA handle allocation */
276 /* Check the parameters */
277 assert_param(IS_IRDA_INSTANCE(hirda
->Instance
));
279 hirda
->gState
= HAL_IRDA_STATE_BUSY
;
281 /* DeInit the low level hardware */
282 HAL_IRDA_MspDeInit(hirda
);
283 /* Disable the Peripheral */
284 __HAL_IRDA_DISABLE(hirda
);
286 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
287 hirda
->gState
= HAL_IRDA_STATE_RESET
;
288 hirda
->RxState
= HAL_IRDA_STATE_RESET
;
297 * @brief IRDA MSP Init.
298 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
299 * the configuration information for the specified IRDA module.
302 __weak
void HAL_IRDA_MspInit(IRDA_HandleTypeDef
*hirda
)
304 /* Prevent unused argument(s) compilation warning */
307 /* NOTE : This function Should not be modified, when the callback is needed,
308 the HAL_IRDA_MspInit could be implemented in the user file
313 * @brief IRDA MSP DeInit.
314 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
315 * the configuration information for the specified IRDA module.
318 __weak
void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef
*hirda
)
320 /* Prevent unused argument(s) compilation warning */
323 /* NOTE : This function Should not be modified, when the callback is needed,
324 the HAL_IRDA_MspDeInit could be implemented in the user file
332 /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
333 * @brief IRDA Transmit/Receive functions
336 ===============================================================================
337 ##### IO operation functions #####
338 ===============================================================================
339 This subsection provides a set of functions allowing to manage the IRDA data transfers.
341 IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
342 on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
343 is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
344 While receiving data, transmission should be avoided as the data to be transmitted
347 (#) There are two modes of transfer:
348 (++) Blocking mode: the communication is performed in polling mode.
349 The HAL status of all data processing is returned by the same function
350 after finishing transfer.
351 (++) No-Blocking mode: the communication is performed using Interrupts
352 or DMA, these API's return the HAL status.
353 The end of the data processing will be indicated through the
354 dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
356 The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
357 will be executed respectively at the end of the Transmit or Receive process
358 The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
360 (#) Blocking mode API's are :
361 (++) HAL_IRDA_Transmit()
362 (++) HAL_IRDA_Receive()
364 (#) Non-Blocking mode API's with Interrupt are :
365 (++) HAL_IRDA_Transmit_IT()
366 (++) HAL_IRDA_Receive_IT()
367 (++) HAL_IRDA_IRQHandler()
368 (++) IRDA_Transmit_IT()
369 (++) IRDA_Receive_IT()
371 (#) Non-Blocking mode functions with DMA are :
372 (++) HAL_IRDA_Transmit_DMA()
373 (++) HAL_IRDA_Receive_DMA()
375 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
376 (++) HAL_IRDA_TxCpltCallback()
377 (++) HAL_IRDA_RxCpltCallback()
378 (++) HAL_IRDA_ErrorCallback()
385 * @brief Sends an amount of data in blocking mode.
386 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
387 * the configuration information for the specified IRDA module.
388 * @param pData: Pointer to data buffer
389 * @param Size: Amount of data to be sent
390 * @param Timeout: Specify timeout value
393 HAL_StatusTypeDef
HAL_IRDA_Transmit(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
396 uint32_t tickstart
= 0U;
398 /* Check that a Tx process is not already ongoing */
399 if(hirda
->gState
== HAL_IRDA_STATE_READY
)
401 if((pData
== NULL
) || (Size
== 0U))
408 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
409 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
411 /* Init tickstart for timeout managment*/
412 tickstart
= HAL_GetTick();
414 hirda
->TxXferSize
= Size
;
415 hirda
->TxXferCount
= Size
;
416 while(hirda
->TxXferCount
> 0U)
418 hirda
->TxXferCount
--;
420 if(IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
424 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
426 tmp
= (uint16_t*) pData
;
427 hirda
->Instance
->TDR
= (*tmp
& (uint16_t)0x01FFU
);
432 hirda
->Instance
->TDR
= (*pData
++ & (uint8_t)0xFFU
);
436 if(IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_TC
, RESET
, tickstart
, Timeout
) != HAL_OK
)
441 /* At end of Tx process, restore hirda->gState to Ready */
442 hirda
->gState
= HAL_IRDA_STATE_READY
;
444 /* Process Unlocked */
456 * @brief Receive an amount of data in blocking mode.
457 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
458 * the configuration information for the specified IRDA module.
459 * @param pData: Pointer to data buffer
460 * @param Size: Amount of data to be received
461 * @param Timeout: Specify timeout value
464 HAL_StatusTypeDef
HAL_IRDA_Receive(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
468 uint32_t tickstart
= 0U;
470 /* Check that a Rx process is not already ongoing */
471 if(hirda
->RxState
== HAL_IRDA_STATE_READY
)
473 if((pData
== NULL
) || (Size
== 0U))
480 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
481 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
483 /* Init tickstart for timeout managment*/
484 tickstart
= HAL_GetTick();
486 hirda
->RxXferSize
= Size
;
487 hirda
->RxXferCount
= Size
;
489 /* Computation of the mask to apply to the RDR register
490 of the UART associated to the IRDA */
491 IRDA_MASK_COMPUTATION(hirda
);
492 uhMask
= hirda
->Mask
;
494 /* Check data remaining to be received */
495 while(hirda
->RxXferCount
> 0U)
497 hirda
->RxXferCount
--;
499 if(IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
503 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
505 tmp
= (uint16_t*) pData
;
506 *tmp
= (uint16_t)(hirda
->Instance
->RDR
& uhMask
);
511 *pData
++ = (uint8_t)(hirda
->Instance
->RDR
& (uint8_t)uhMask
);
515 /* At end of Rx process, restore hirda->RxState to Ready */
516 hirda
->RxState
= HAL_IRDA_STATE_READY
;
518 /* Process Unlocked */
530 * @brief Send an amount of data in non blocking mode.
531 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
532 * the configuration information for the specified IRDA module.
533 * @param pData: Pointer to data buffer
534 * @param Size: Amount of data to be sent
537 HAL_StatusTypeDef
HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
539 /* Check that a Tx process is not already ongoing */
540 if(hirda
->gState
== HAL_IRDA_STATE_READY
)
542 if((pData
== NULL
) || (Size
== 0U))
550 hirda
->pTxBuffPtr
= pData
;
551 hirda
->TxXferSize
= Size
;
552 hirda
->TxXferCount
= Size
;
554 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
555 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
557 /* Process Unlocked */
560 /* Enable the IRDA Transmit Complete Interrupt */
561 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
572 * @brief Receives an amount of data in non blocking mode.
573 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
574 * the configuration information for the specified IRDA module.
575 * @param pData: Pointer to data buffer
576 * @param Size: Amount of data to be received
579 HAL_StatusTypeDef
HAL_IRDA_Receive_IT(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
581 /* Check that a Rx process is not already ongoing */
582 if(hirda
->RxState
== HAL_IRDA_STATE_READY
)
584 if((pData
== NULL
) || (Size
== 0U))
592 hirda
->pRxBuffPtr
= pData
;
593 hirda
->RxXferSize
= Size
;
594 hirda
->RxXferCount
= Size
;
596 /* Computation of the mask to apply to the RDR register
597 of the UART associated to the IRDA */
598 IRDA_MASK_COMPUTATION(hirda
);
600 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
601 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
603 /* Process Unlocked */
606 /* Enable the IRDA Parity Error Interrupt */
607 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
609 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
610 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
612 /* Enable the IRDA Data Register not empty Interrupt */
613 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_RXNEIE
);
624 * @brief Sends an amount of data in non blocking mode.
625 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
626 * the configuration information for the specified IRDA module.
627 * @param pData: Pointer to data buffer
628 * @param Size: Amount of data to be sent
631 HAL_StatusTypeDef
HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
635 /* Check that a Tx process is not already ongoing */
636 if(hirda
->gState
== HAL_IRDA_STATE_READY
)
638 if((pData
== NULL
) || (Size
== 0U))
646 hirda
->pTxBuffPtr
= pData
;
647 hirda
->TxXferSize
= Size
;
648 hirda
->TxXferCount
= Size
;
650 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
651 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
653 /* Set the IRDA DMA transfer complete callback */
654 hirda
->hdmatx
->XferCpltCallback
= IRDA_DMATransmitCplt
;
656 /* Set the IRDA DMA half transfer complete callback */
657 hirda
->hdmatx
->XferHalfCpltCallback
= IRDA_DMATransmitHalfCplt
;
659 /* Set the DMA error callback */
660 hirda
->hdmatx
->XferErrorCallback
= IRDA_DMAError
;
662 /* Set the DMA abort callback */
663 hirda
->hdmatx
->XferAbortCallback
= NULL
;
665 /* Enable the IRDA transmit DMA channel */
666 tmp
= (uint32_t*)&pData
;
667 HAL_DMA_Start_IT(hirda
->hdmatx
, *(uint32_t*)tmp
, (uint32_t)&hirda
->Instance
->TDR
, Size
);
669 /* Clear the TC flag in the SR register by writing 0 to it */
670 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_FLAG_TC
);
672 /* Process Unlocked */
675 /* Enable the DMA transfer for transmit request by setting the DMAT bit
676 in the IRDA CR3 register */
677 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
688 * @brief Receives an amount of data in non blocking mode.
689 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
690 * the configuration information for the specified IRDA module.
691 * @param pData: Pointer to data buffer
692 * @param Size: Amount of data to be received
693 * @note When the IRDA parity is enabled (PCE = 1) the data received contain the parity bit.
696 HAL_StatusTypeDef
HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
700 /* Check that a Rx process is not already ongoing */
701 if(hirda
->RxState
== HAL_IRDA_STATE_READY
)
703 if((pData
== NULL
) || (Size
== 0U))
711 hirda
->pRxBuffPtr
= pData
;
712 hirda
->RxXferSize
= Size
;
714 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
715 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
717 /* Set the IRDA DMA transfer complete callback */
718 hirda
->hdmarx
->XferCpltCallback
= IRDA_DMAReceiveCplt
;
720 /* Set the IRDA DMA half transfer complete callback */
721 hirda
->hdmarx
->XferHalfCpltCallback
= IRDA_DMAReceiveHalfCplt
;
723 /* Set the DMA error callback */
724 hirda
->hdmarx
->XferErrorCallback
= IRDA_DMAError
;
726 /* Set the DMA abort callback */
727 hirda
->hdmarx
->XferAbortCallback
= NULL
;
729 /* Enable the DMA channel */
730 tmp
= (uint32_t*)&pData
;
731 HAL_DMA_Start_IT(hirda
->hdmarx
, (uint32_t)&hirda
->Instance
->RDR
, *(uint32_t*)tmp
, Size
);
733 /* Process Unlocked */
736 /* Enable the IRDA Parity Error Interrupt */
737 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
739 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
740 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
742 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
743 in the USART CR3 register */
744 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
755 * @brief Pauses the DMA Transfer.
756 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
757 * the configuration information for the specified IRDA module.
760 HAL_StatusTypeDef
HAL_IRDA_DMAPause(IRDA_HandleTypeDef
*hirda
)
765 if((hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)&&
766 (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
)))
768 /* Disable the UART DMA Tx request */
769 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
771 if((hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)&&
772 (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
)))
774 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
775 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
776 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
778 /* Disable the UART DMA Rx request */
779 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
782 /* Process Unlocked */
789 * @brief Resumes the DMA Transfer.
790 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
791 * the configuration information for the specified UART module.
794 HAL_StatusTypeDef
HAL_IRDA_DMAResume(IRDA_HandleTypeDef
*hirda
)
799 if(hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
801 /* Enable the UART DMA Tx request */
802 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
804 if(hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
806 /* Clear the Overrun flag before resuming the Rx transfer*/
807 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_OREF
);
809 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
810 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
811 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
813 /* Enable the UART DMA Rx request */
814 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
817 /* Process Unlocked */
824 * @brief Stops the DMA Transfer.
825 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
826 * the configuration information for the specified UART module.
829 HAL_StatusTypeDef
HAL_IRDA_DMAStop(IRDA_HandleTypeDef
*hirda
)
831 /* The Lock is not implemented on this API to allow the user application
832 to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback() /
833 HAL_IRDA_TxHalfCpltCallback / HAL_IRDA_RxHalfCpltCallback:
834 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
835 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
836 the stream and the corresponding call back is executed. */
838 /* Stop IRDA DMA Tx request if ongoing */
839 if ((hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
) &&
840 (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
)))
842 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
844 /* Abort the IRDA DMA Tx channel */
845 if(hirda
->hdmatx
!= NULL
)
847 HAL_DMA_Abort(hirda
->hdmatx
);
849 IRDA_EndTxTransfer(hirda
);
852 /* Stop IRDA DMA Rx request if ongoing */
853 if ((hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
) &&
854 (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
)))
856 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
858 /* Abort the IRDA DMA Rx channel */
859 if(hirda
->hdmarx
!= NULL
)
861 HAL_DMA_Abort(hirda
->hdmarx
);
863 IRDA_EndRxTransfer(hirda
);
869 * @brief DMA IRDA communication abort callback, when call by HAL services on Error
870 * (To be called at end of DMA Abort procedure following error occurrence).
871 * @param hdma: DMA handle.
874 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
876 IRDA_HandleTypeDef
* hirda
= ( IRDA_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
877 hirda
->RxXferCount
= 0U;
878 hirda
->TxXferCount
= 0U;
880 HAL_IRDA_ErrorCallback(hirda
);
884 * @brief This function handles IRDA interrupt request.
885 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
886 * the configuration information for the specified IRDA module.
889 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef
*hirda
)
891 uint32_t isrflags
, cr1its
, cr3its
, errorflags
;
893 isrflags
= READ_REG(hirda
->Instance
->ISR
);
894 cr1its
= READ_REG(hirda
->Instance
->CR1
);
895 cr3its
= READ_REG(hirda
->Instance
->CR3
);
897 /* If no error occurs */
898 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
));
899 if (errorflags
== RESET
)
901 /* IRDA in mode Receiver ---------------------------------------------------*/
902 if(((isrflags
& USART_ISR_RXNE
) != RESET
) && ((cr1its
& USART_CR1_RXNEIE
) != RESET
))
904 IRDA_Receive_IT(hirda
);
905 /* Clear RXNE interrupt flag */
906 __HAL_IRDA_SEND_REQ(hirda
, IRDA_RXDATA_FLUSH_REQUEST
);
910 /* If some errors occur */
911 if((errorflags
!= RESET
) && ((cr3its
& (USART_CR3_EIE
| USART_CR1_PEIE
)) != RESET
))
913 /* IRDA parity error interrupt occurred -------------------------------------*/
914 if(((isrflags
& USART_ISR_PE
) != RESET
) && ((cr1its
& USART_CR1_PEIE
) != RESET
))
916 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_PEF
);
917 hirda
->ErrorCode
|= HAL_IRDA_ERROR_PE
;
920 /* IRDA frame error interrupt occurred --------------------------------------*/
921 if(((isrflags
& USART_ISR_FE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
923 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_FEF
);
924 hirda
->ErrorCode
|= HAL_IRDA_ERROR_FE
;
927 /* IRDA noise error interrupt occurred --------------------------------------*/
928 if(((isrflags
& USART_ISR_NE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
930 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_NEF
);
931 hirda
->ErrorCode
|= HAL_IRDA_ERROR_NE
;
934 /* IRDA Over-Run interrupt occurred -----------------------------------------*/
935 if(((isrflags
& USART_ISR_ORE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
937 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_OREF
);
938 hirda
->ErrorCode
|= HAL_IRDA_ERROR_ORE
;
941 /* Call IRDA Error Call back function if need be --------------------------*/
942 if(hirda
->ErrorCode
!= HAL_IRDA_ERROR_NONE
)
944 /* IRDA in mode Receiver ---------------------------------------------------*/
945 if(((isrflags
& USART_ISR_RXNE
) != RESET
) && ((cr1its
& USART_CR1_RXNEIE
) != RESET
))
947 IRDA_Receive_IT(hirda
);
950 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
951 consider error as blocking */
952 if (((hirda
->ErrorCode
& HAL_UART_ERROR_ORE
) != RESET
) ||
953 (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
)))
955 /* Blocking error : transfer is aborted
956 Set the IRDA state ready to be able to start again the process,
957 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
958 IRDA_EndRxTransfer(hirda
);
960 /* Disable the IRDA DMA Rx request if enabled */
961 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
963 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
965 /* Abort the IRDA DMA Rx channel */
966 if(hirda
->hdmarx
!= NULL
)
968 /* Set the IRDA DMA Abort callback :
969 will lead to call HAL_IRDA_ErrorCallback() at end of DMA abort procedure */
970 hirda
->hdmarx
->XferAbortCallback
= IRDA_DMAAbortOnError
;
973 if(HAL_DMA_Abort_IT(hirda
->hdmarx
) != HAL_OK
)
975 /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
976 hirda
->hdmarx
->XferAbortCallback(hirda
->hdmarx
);
981 /* Call user error callback */
982 HAL_IRDA_ErrorCallback(hirda
);
987 /* Call user error callback */
988 HAL_IRDA_ErrorCallback(hirda
);
993 /* Non Blocking error : transfer could go on.
994 Error is notified to user through user error callback */
995 HAL_IRDA_ErrorCallback(hirda
);
996 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1001 } /* End if some error occurs */
1003 /* IRDA in mode Transmitter ------------------------------------------------*/
1004 if(((isrflags
& USART_ISR_TXE
) != RESET
) && ((cr1its
& USART_CR1_TXEIE
) != RESET
))
1006 IRDA_Transmit_IT(hirda
);
1010 /* IRDA in mode Transmitter (transmission end) -----------------------------*/
1011 if(((isrflags
& USART_ISR_TC
) != RESET
) && ((cr1its
& USART_CR1_TCIE
) != RESET
))
1013 IRDA_EndTransmit_IT(hirda
);
1019 * @brief End ongoing Tx transfer on IRDA peripheral (following error detection or Transmit completion).
1020 * @param hirda: IRDA handle.
1023 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef
*hirda
)
1025 /* Disable TXEIE and TCIE interrupts */
1026 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1028 /* At end of Tx process, restore hirda->gState to Ready */
1029 hirda
->gState
= HAL_IRDA_STATE_READY
;
1033 * @brief End ongoing Rx transfer on IRDA peripheral (following error detection or Reception completion).
1034 * @param hirda: IRDA handle.
1037 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef
*hirda
)
1039 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1040 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1041 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1043 /* At end of Rx process, restore huart->RxState to Ready */
1044 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1048 * @brief Tx Transfer complete 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_TxHalfCpltCallback(IRDA_HandleTypeDef
*hirda
)
1055 /* Prevent unused argument(s) compilation warning */
1058 /* NOTE : This function should not be modified, when the callback is needed,
1059 the HAL_IRDA_TxHalfCpltCallback can be implemented in the user file
1064 * @brief Tx Half Transfer completed callbacks.
1065 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1066 * the configuration information for the specified USART module.
1069 __weak
void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef
*hirda
)
1071 /* Prevent unused argument(s) compilation warning */
1074 /* NOTE : This function should not be modified, when the callback is needed,
1075 the HAL_IRDA_TxCpltCallback can be implemented in the user file
1080 * @brief Rx Transfer complete callbacks.
1081 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1082 * the configuration information for the specified IRDA module.
1085 __weak
void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef
*hirda
)
1087 /* Prevent unused argument(s) compilation warning */
1090 /* NOTE : This function should not be modified, when the callback is needed,
1091 the HAL_IRDA_RxHalfCpltCallback can be implemented in the user file
1096 * @brief Rx Half Transfer complete callbacks.
1097 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1098 * the configuration information for the specified IRDA module.
1101 __weak
void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef
*hirda
)
1103 /* Prevent unused argument(s) compilation warning */
1106 /* NOTE : This function should not be modified, when the callback is needed,
1107 the HAL_IRDA_RxCpltCallback can be implemented in the user file
1112 * @brief IRDA error callbacks.
1113 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1114 * the configuration information for the specified IRDA module.
1117 __weak
void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef
*hirda
)
1119 /* Prevent unused argument(s) compilation warning */
1122 /* NOTE : This function should not be modified, when the callback is needed,
1123 the HAL_IRDA_ErrorCallback can be implemented in the user file
1131 /** @defgroup IRDA_Exported_Functions_Group3 Peripheral Control functions
1132 * @brief IRDA control functions
1135 ===============================================================================
1136 ##### Peripheral Control functions #####
1137 ===============================================================================
1139 This subsection provides a set of functions allowing to control the IRDA.
1140 (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state of the IRDA peripheral.
1141 (+) IRDA_SetConfig() API is used to configure the IRDA communications parameters.
1147 * @brief Returns the IRDA state.
1148 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1149 * the configuration information for the specified IRDA module.
1152 HAL_IRDA_StateTypeDef
HAL_IRDA_GetState(IRDA_HandleTypeDef
*hirda
)
1154 uint32_t temp1
= 0x00U
, temp2
= 0x00U
;
1155 temp1
= hirda
->gState
;
1156 temp2
= hirda
->RxState
;
1158 return (HAL_IRDA_StateTypeDef
)(temp1
| temp2
);
1162 * @brief Return the IRDA error code
1163 * @param hirda : pointer to a IRDA_HandleTypeDef structure that contains
1164 * the configuration information for the specified IRDA.
1165 * @retval IRDA Error Code
1167 uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef
*hirda
)
1169 uint32_t temp1
= 0x00U
, temp2
= 0x00U
;
1170 temp1
= hirda
->gState
;
1171 temp2
= hirda
->RxState
;
1173 return (HAL_IRDA_StateTypeDef
)(temp1
| temp2
);
1181 * @brief Configure the IRDA peripheral
1182 * @param hirda: irda handle
1185 static void IRDA_SetConfig(IRDA_HandleTypeDef
*hirda
)
1187 uint32_t tmpreg
= 0x00000000U
;
1188 uint32_t clocksource
= 0x00000000U
;
1190 /* Check the communication parameters */
1191 assert_param(IS_IRDA_BAUDRATE(hirda
->Init
.BaudRate
));
1192 assert_param(IS_IRDA_WORD_LENGTH(hirda
->Init
.WordLength
));
1193 assert_param(IS_IRDA_PARITY(hirda
->Init
.Parity
));
1194 assert_param(IS_IRDA_TX_RX_MODE(hirda
->Init
.Mode
));
1195 assert_param(IS_IRDA_PRESCALER(hirda
->Init
.Prescaler
));
1196 assert_param(IS_IRDA_POWERMODE(hirda
->Init
.PowerMode
));
1197 /*-------------------------- USART CR1 Configuration -----------------------*/
1198 /* Configure the IRDA Word Length, Parity and transfer Mode:
1199 Set the M bits according to hirda->Init.WordLength value
1200 Set PCE and PS bits according to hirda->Init.Parity value
1201 Set TE and RE bits according to hirda->Init.Mode value */
1202 tmpreg
= (uint32_t)hirda
->Init
.WordLength
| hirda
->Init
.Parity
| hirda
->Init
.Mode
;
1204 MODIFY_REG(hirda
->Instance
->CR1
, IRDA_CR1_FIELDS
, tmpreg
);
1206 /*-------------------------- USART CR3 Configuration -----------------------*/
1207 MODIFY_REG(hirda
->Instance
->CR3
, USART_CR3_IRLP
, hirda
->Init
.PowerMode
);
1209 /*-------------------------- USART GTPR Configuration ----------------------*/
1210 MODIFY_REG(hirda
->Instance
->GTPR
, (uint32_t)USART_GTPR_PSC
, hirda
->Init
.Prescaler
);
1212 /*-------------------------- USART BRR Configuration -----------------------*/
1213 IRDA_GETCLOCKSOURCE(hirda
, clocksource
);
1214 switch (clocksource
)
1216 case IRDA_CLOCKSOURCE_PCLK1
:
1217 hirda
->Instance
->BRR
= (uint16_t)((HAL_RCC_GetPCLK1Freq() + (hirda
->Init
.BaudRate
/2))/ hirda
->Init
.BaudRate
);
1219 case IRDA_CLOCKSOURCE_PCLK2
:
1220 hirda
->Instance
->BRR
= (uint16_t)((HAL_RCC_GetPCLK2Freq() + (hirda
->Init
.BaudRate
/2))/ hirda
->Init
.BaudRate
);
1222 case IRDA_CLOCKSOURCE_HSI
:
1223 hirda
->Instance
->BRR
= (uint16_t)((HSI_VALUE
+ (hirda
->Init
.BaudRate
/2))/ hirda
->Init
.BaudRate
);
1225 case IRDA_CLOCKSOURCE_SYSCLK
:
1226 hirda
->Instance
->BRR
= (uint16_t)((HAL_RCC_GetSysClockFreq() + (hirda
->Init
.BaudRate
/2))/ hirda
->Init
.BaudRate
);
1228 case IRDA_CLOCKSOURCE_LSE
:
1229 hirda
->Instance
->BRR
= (uint16_t)((LSE_VALUE
+ (hirda
->Init
.BaudRate
/2))/ hirda
->Init
.BaudRate
);
1237 * @brief Check the IRDA Idle State
1238 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1239 * the configuration information for the specified IRDA module.
1240 * @retval HAL status
1242 static HAL_StatusTypeDef
IRDA_CheckIdleState(IRDA_HandleTypeDef
*hirda
)
1244 uint32_t tickstart
= 0U;
1246 /* Initialize the IRDA ErrorCode */
1247 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1249 /* Init tickstart for timeout managment*/
1250 tickstart
= HAL_GetTick();
1252 /* Check if the Transmitter is enabled */
1253 if((hirda
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
1255 /* Wait until TEACK flag is set */
1256 if(IRDA_WaitOnFlagUntilTimeout(hirda
, USART_ISR_TEACK
, RESET
, tickstart
, TEACK_REACK_TIMEOUT
) != HAL_OK
)
1261 /* Check if the Receiver is enabled */
1262 if((hirda
->Instance
->CR1
& USART_CR1_RE
) == USART_CR1_RE
)
1264 if(IRDA_WaitOnFlagUntilTimeout(hirda
, USART_ISR_REACK
, RESET
, tickstart
, TEACK_REACK_TIMEOUT
) != HAL_OK
)
1269 /* Process Unlocked */
1270 __HAL_UNLOCK(hirda
);
1272 /* Initialize the IRDA state*/
1273 hirda
->gState
= HAL_IRDA_STATE_READY
;
1274 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1280 * @brief This function handles IRDA Communication Timeout.
1281 * @param hirda pointer to a IRDA_HandleTypeDef structure that contains
1282 * the configuration information for the specified IRDA module.
1283 * @param Flag specifies the IRDA flag to check.
1284 * @param Status The new Flag status (SET or RESET).
1285 * @param Tickstart Tick start value
1286 * @param Timeout Timeout duration
1287 * @retval HAL status
1289 static HAL_StatusTypeDef
IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef
*hirda
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
)
1291 /* Wait until flag is set */
1292 while((__HAL_IRDA_GET_FLAG(hirda
, Flag
) ? SET
: RESET
) == Status
)
1294 /* Check for the Timeout */
1295 if(Timeout
!= HAL_MAX_DELAY
)
1297 if((Timeout
== 0)||((HAL_GetTick() - Tickstart
) > Timeout
))
1299 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1300 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
));
1301 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1303 hirda
->gState
= HAL_IRDA_STATE_READY
;
1304 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1306 /* Process Unlocked */
1307 __HAL_UNLOCK(hirda
);
1317 * @brief Send an amount of data in non blocking mode.
1318 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1319 * the configuration information for the specified IRDA module.
1320 * @retval HAL status
1322 static HAL_StatusTypeDef
IRDA_Transmit_IT(IRDA_HandleTypeDef
*hirda
)
1326 /* Check that a Tx process is ongoing */
1327 if(hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
1329 if(hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
)
1331 tmp
= (uint16_t*) hirda
->pTxBuffPtr
;
1332 hirda
->Instance
->RDR
= (uint16_t)(*tmp
& (uint16_t)0x01FFU
);
1333 if(hirda
->Init
.Parity
== IRDA_PARITY_NONE
)
1335 hirda
->pTxBuffPtr
+= 2U;
1339 hirda
->pTxBuffPtr
+= 1U;
1344 hirda
->Instance
->RDR
= (uint8_t)(*hirda
->pTxBuffPtr
++ & (uint8_t)0x00FFU
);
1347 if(--hirda
->TxXferCount
== 0U)
1349 /* Disable the IRDA Transmit Data Register Empty Interrupt */
1350 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_TXEIE
);
1352 /* Enable the IRDA Transmit Complete Interrupt */
1353 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
1365 * @brief Wraps up transmission in non blocking mode.
1366 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1367 * the configuration information for the specified IRDA module.
1368 * @retval HAL status
1370 static HAL_StatusTypeDef
IRDA_EndTransmit_IT(IRDA_HandleTypeDef
*hirda
)
1372 /* Disable the IRDA Transmit Complete Interrupt */
1373 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
1375 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1376 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1378 /* Tx process is ended, restore hirda->gState to Ready */
1379 hirda
->gState
= HAL_IRDA_STATE_READY
;
1381 HAL_IRDA_TxCpltCallback(hirda
);
1387 * @brief Receive an amount of data in non blocking mode.
1388 * Function called under interruption only, once
1389 * interruptions have been enabled by HAL_IRDA_Receive_IT()
1390 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1391 * the configuration information for the specified IRDA module.
1392 * @retval HAL status
1394 static HAL_StatusTypeDef
IRDA_Receive_IT(IRDA_HandleTypeDef
*hirda
)
1398 uint16_t uhMask
= hirda
->Mask
;
1400 if(hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
1402 uhdata
= (uint16_t) READ_REG(hirda
->Instance
->RDR
);
1403 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
1405 tmp
= (uint16_t*) hirda
->pRxBuffPtr
;
1406 *tmp
= (uint16_t)(uhdata
& uhMask
);
1407 hirda
->pRxBuffPtr
+=2U;
1411 *hirda
->pRxBuffPtr
++ = (uint8_t)(uhdata
& (uint8_t)uhMask
);
1414 if(--hirda
->RxXferCount
== 0U)
1416 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_RXNEIE
);
1418 /* Disable the IRDA Parity Error Interrupt */
1419 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
1421 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1422 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1424 /* Rx process is completed, restore hirda->RxState to Ready */
1425 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1427 HAL_IRDA_RxCpltCallback(hirda
);
1435 /* Clear RXNE interrupt flag */
1436 __HAL_IRDA_SEND_REQ(hirda
, IRDA_RXDATA_FLUSH_REQUEST
);
1442 * @brief DMA IRDA Tx transfer completed callback
1443 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1444 * the configuration information for the specified DMA module.
1447 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
1449 IRDA_HandleTypeDef
* hirda
= ( IRDA_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1451 /* DMA Normal mode*/
1452 if((hdma
->Instance
->CR
& DMA_SxCR_CIRC
) == 0U)
1454 hirda
->TxXferCount
= 0U;
1456 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1457 in the IRDA CR3 register */
1458 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1460 /* Enable the IRDA Transmit Complete Interrupt */
1461 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
1463 /* DMA Circular mode */
1466 HAL_IRDA_TxCpltCallback(hirda
);
1471 * @brief DMA IRDA receive process half complete callback
1472 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1473 * the configuration information for the specified DMA module.
1476 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef
*hdma
)
1478 IRDA_HandleTypeDef
* hirda
= ( IRDA_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1480 HAL_IRDA_TxHalfCpltCallback(hirda
);
1484 * @brief DMA IRDA Rx Transfer completed callback
1485 * @param hdma: DMA handle
1488 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
1490 IRDA_HandleTypeDef
* hirda
= ( IRDA_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1492 /* DMA Normal mode */
1493 if((hdma
->Instance
->CR
& DMA_SxCR_CIRC
) == 0U)
1495 hirda
->RxXferCount
= 0U;
1497 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1498 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
1499 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1501 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1502 in the IRDA CR3 register */
1503 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1505 /* At end of Rx process, restore hirda->RxState to Ready */
1506 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1509 HAL_IRDA_RxCpltCallback(hirda
);
1513 * @brief DMA IRDA receive process half complete callback
1514 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1515 * the configuration information for the specified DMA module.
1518 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef
*hdma
)
1520 IRDA_HandleTypeDef
* hirda
= ( IRDA_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1522 HAL_IRDA_RxHalfCpltCallback(hirda
);
1526 * @brief DMA IRDA communication error callback
1527 * @param hdma: DMA handle
1530 static void IRDA_DMAError(DMA_HandleTypeDef
*hdma
)
1532 IRDA_HandleTypeDef
* hirda
= ( IRDA_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1534 hirda
->RxXferCount
= 0U;
1535 hirda
->TxXferCount
= 0U;
1537 /* Stop IRDA DMA Tx request if ongoing */
1538 if ( (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
1539 &&(HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
)) )
1541 IRDA_EndTxTransfer(hirda
);
1544 /* Stop IRDA DMA Rx request if ongoing */
1545 if ( (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
1546 &&(HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
)) )
1548 IRDA_EndRxTransfer(hirda
);
1551 hirda
->ErrorCode
|= HAL_IRDA_ERROR_DMA
;
1553 HAL_IRDA_ErrorCallback(hirda
);
1564 #endif /* HAL_IRDA_MODULE_ENABLED */
1570 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/