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 (Infrared Data Association) Peripheral
11 * + Initialization and de-initialization functions
12 * + IO operation functions
13 * + Peripheral State and Errors functions
14 * + Peripheral Control functions
17 ==============================================================================
18 ##### How to use this driver #####
19 ==============================================================================
21 The IRDA HAL driver can be used as follows:
23 (#) Declare a IRDA_HandleTypeDef handle structure (eg. IRDA_HandleTypeDef hirda).
24 (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API
25 in setting the associated USART or UART in IRDA mode:
26 (++) Enable the USARTx/UARTx interface clock.
27 (++) USARTx/UARTx pins configuration:
28 (+++) Enable the clock for the USARTx/UARTx GPIOs.
29 (+++) Configure these USARTx/UARTx pins (TX as alternate function pull-up, RX as alternate function Input).
30 (++) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
31 and HAL_IRDA_Receive_IT() APIs):
32 (+++) Configure the USARTx/UARTx interrupt priority.
33 (+++) Enable the NVIC USARTx/UARTx IRQ handle.
34 (+++) The specific IRDA interrupts (Transmission complete interrupt,
35 RXNE interrupt and Error Interrupts) will be managed using the macros
36 __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
38 (++) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
39 and HAL_IRDA_Receive_DMA() APIs):
40 (+++) Declare a DMA handle structure for the Tx/Rx channel.
41 (+++) Enable the DMAx interface clock.
42 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
43 (+++) Configure the DMA Tx/Rx channel.
44 (+++) Associate the initialized DMA handle to the IRDA DMA Tx/Rx handle.
45 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
47 (#) Program the Baud Rate, Word Length, Parity, IrDA Mode, Prescaler
48 and Mode(Receiver/Transmitter) in the hirda Init structure.
50 (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
51 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
52 by calling the customized HAL_IRDA_MspInit() API.
54 -@@- The specific IRDA interrupts (Transmission complete interrupt,
55 RXNE interrupt and Error Interrupts) will be managed using the macros
56 __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
58 (#) Three operation modes are available within this driver :
60 *** Polling mode IO operation ***
61 =================================
63 (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
64 (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
66 *** Interrupt mode IO operation ***
67 ===================================
69 (+) Send an amount of data in non blocking mode using HAL_IRDA_Transmit_IT()
70 (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
71 add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
72 (+) Receive an amount of data in non blocking mode using HAL_IRDA_Receive_IT()
73 (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
74 add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
75 (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
76 add his own code by customization of function pointer HAL_IRDA_ErrorCallback
78 *** DMA mode IO operation ***
79 =============================
81 (+) Send an amount of data in non blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
82 (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
83 add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
84 (+) Receive an amount of data in non blocking mode (DMA) using HAL_IRDA_Receive_DMA()
85 (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
86 add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
87 (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
88 add his own code by customization of function pointer HAL_IRDA_ErrorCallback
90 *** IRDA HAL driver macros list ***
91 ===================================
93 Below the list of most used macros in IRDA HAL driver.
95 (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
96 (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
97 (+) __HAL_IRDA_GET_FLAG : Checks whether the specified IRDA flag is set or not
98 (+) __HAL_IRDA_CLEAR_FLAG : Clears the specified IRDA pending flag
99 (+) __HAL_IRDA_ENABLE_IT: Enables the specified IRDA interrupt
100 (+) __HAL_IRDA_DISABLE_IT: Disables the specified IRDA interrupt
103 (@) You can refer to the IRDA HAL driver header file for more useful macros
106 ******************************************************************************
109 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
111 * Redistribution and use in source and binary forms, with or without modification,
112 * are permitted provided that the following conditions are met:
113 * 1. Redistributions of source code must retain the above copyright notice,
114 * this list of conditions and the following disclaimer.
115 * 2. Redistributions in binary form must reproduce the above copyright notice,
116 * this list of conditions and the following disclaimer in the documentation
117 * and/or other materials provided with the distribution.
118 * 3. Neither the name of STMicroelectronics nor the names of its contributors
119 * may be used to endorse or promote products derived from this software
120 * without specific prior written permission.
122 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
123 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
124 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
125 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
126 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
127 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
128 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
129 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
130 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
131 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
133 ******************************************************************************
136 /* Includes ------------------------------------------------------------------*/
137 #include "stm32f7xx_hal.h"
139 /** @addtogroup STM32F7xx_HAL_Driver
143 /** @defgroup IRDA IRDA
144 * @brief HAL IRDA module driver
148 #ifdef HAL_IRDA_MODULE_ENABLED
150 /* Private typedef -----------------------------------------------------------*/
151 /* Private define ------------------------------------------------------------*/
152 /** @defgroup IRDA_Private_Constants IRDA Private Constants
155 #define IRDA_TEACK_REACK_TIMEOUT 1000U
156 #define HAL_IRDA_TXDMA_TIMEOUTVALUE 22000U
157 #define IRDA_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE \
158 | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)) /*!< UART or USART CR1 fields of parameters set by IRDA_SetConfig API */
163 /* Private macros ------------------------------------------------------------*/
164 /* Private variables ---------------------------------------------------------*/
165 /* Private function prototypes -----------------------------------------------*/
166 /** @addtogroup IRDA_Private_Functions
169 static HAL_StatusTypeDef
IRDA_SetConfig(IRDA_HandleTypeDef
*hirda
);
170 static HAL_StatusTypeDef
IRDA_CheckIdleState(IRDA_HandleTypeDef
*hirda
);
171 static HAL_StatusTypeDef
IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef
*hirda
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
);
172 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef
*hirda
);
173 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef
*hirda
);
174 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
175 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef
*hdma
);
176 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
177 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef
*hdma
);
178 static void IRDA_DMAError(DMA_HandleTypeDef
*hdma
);
179 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
180 static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
);
181 static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
);
182 static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
183 static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
184 static HAL_StatusTypeDef
IRDA_Transmit_IT(IRDA_HandleTypeDef
*hirda
);
185 static HAL_StatusTypeDef
IRDA_EndTransmit_IT(IRDA_HandleTypeDef
*hirda
);
186 static HAL_StatusTypeDef
IRDA_Receive_IT(IRDA_HandleTypeDef
*hirda
);
191 /* Exported functions --------------------------------------------------------*/
193 /** @defgroup IRDA_Exported_Functions IRDA Exported Functions
197 /** @defgroup IRDA_Exported_Functions_Group1 IrDA Initialization and de-initialization functions
198 * @brief Initialization and Configuration functions
202 ===============================================================================
203 ##### Initialization and Configuration functions #####
204 ===============================================================================
206 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
208 (+) For the asynchronous mode only these parameters can be configured:
211 (++) Parity: If the parity is enabled, then the MSB bit of the data written
212 in the data register is transmitted but is changed by the parity bit.
213 Depending on the frame length defined by the M bit (8-bits or 9-bits),
214 please refer to Reference manual for possible IRDA frame formats.
215 (++) Prescaler: A pulse of width less than two and greater than one PSC period(s) may or may
216 not be rejected. The receiver set up time should be managed by software. The IrDA physical layer
217 specification specifies a minimum of 10 ms delay between transmission and
218 reception (IrDA is a half duplex protocol).
219 (++) Mode: Receiver/transmitter modes
220 (++) IrDAMode: the IrDA can operate in the Normal mode or in the Low power mode.
222 The HAL_IRDA_Init() API follows IRDA configuration procedures (details for the procedures
223 are available in reference manual).
230 * @brief Initialize the IRDA mode according to the specified
231 * parameters in the IRDA_InitTypeDef and initialize the associated handle.
232 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
233 * the configuration information for the specified IRDA module.
236 HAL_StatusTypeDef
HAL_IRDA_Init(IRDA_HandleTypeDef
*hirda
)
238 /* Check the IRDA handle allocation */
244 /* Check the USART/UART associated to the IRDA handle */
245 assert_param(IS_IRDA_INSTANCE(hirda
->Instance
));
247 if(hirda
->gState
== HAL_IRDA_STATE_RESET
)
249 /* Allocate lock resource and initialize it */
250 hirda
->Lock
= HAL_UNLOCKED
;
252 /* Init the low level hardware : GPIO, CLOCK */
253 HAL_IRDA_MspInit(hirda
);
256 hirda
->gState
= HAL_IRDA_STATE_BUSY
;
258 /* Disable the Peripheral to update the configuration registers */
259 __HAL_IRDA_DISABLE(hirda
);
261 /* Set the IRDA Communication parameters */
262 if (IRDA_SetConfig(hirda
) == HAL_ERROR
)
267 /* In IRDA mode, the following bits must be kept cleared:
268 - LINEN, STOP and CLKEN bits in the USART_CR2 register,
269 - SCEN and HDSEL bits in the USART_CR3 register.*/
270 CLEAR_BIT(hirda
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
| USART_CR2_STOP
));
271 CLEAR_BIT(hirda
->Instance
->CR3
, (USART_CR3_SCEN
| USART_CR3_HDSEL
));
273 /* set the UART/USART in IRDA mode */
274 hirda
->Instance
->CR3
|= USART_CR3_IREN
;
276 /* Enable the Peripheral */
277 __HAL_IRDA_ENABLE(hirda
);
279 /* TEACK and/or REACK to check before moving hirda->gState and hirda->RxState to Ready */
280 return (IRDA_CheckIdleState(hirda
));
284 * @brief DeInitialize the IRDA peripheral.
285 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
286 * the configuration information for the specified IRDA module.
289 HAL_StatusTypeDef
HAL_IRDA_DeInit(IRDA_HandleTypeDef
*hirda
)
291 /* Check the IRDA handle allocation */
297 /* Check the USART/UART associated to the IRDA handle */
298 assert_param(IS_IRDA_INSTANCE(hirda
->Instance
));
300 hirda
->gState
= HAL_IRDA_STATE_BUSY
;
302 /* DeInit the low level hardware */
303 HAL_IRDA_MspDeInit(hirda
);
304 /* Disable the Peripheral */
305 __HAL_IRDA_DISABLE(hirda
);
307 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
308 hirda
->gState
= HAL_IRDA_STATE_RESET
;
309 hirda
->RxState
= HAL_IRDA_STATE_RESET
;
318 * @brief Initialize the IRDA MSP.
319 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
320 * the configuration information for the specified IRDA module.
323 __weak
void HAL_IRDA_MspInit(IRDA_HandleTypeDef
*hirda
)
325 /* Prevent unused argument(s) compilation warning */
328 /* NOTE: This function should not be modified, when the callback is needed,
329 the HAL_IRDA_MspInit can be implemented in the user file
334 * @brief DeInitialize the IRDA MSP.
335 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
336 * the configuration information for the specified IRDA module.
339 __weak
void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef
*hirda
)
341 /* Prevent unused argument(s) compilation warning */
344 /* NOTE: This function should not be modified, when the callback is needed,
345 the HAL_IRDA_MspDeInit can be implemented in the user file
353 /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
354 * @brief IRDA Transmit and Receive functions
357 ===============================================================================
358 ##### IO operation functions #####
359 ===============================================================================
361 This subsection provides a set of functions allowing to manage the IRDA data transfers.
364 IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
365 on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
366 is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
367 While receiving data, transmission should be avoided as the data to be transmitted
370 (#) There are two modes of transfer:
371 (++) Blocking mode: the communication is performed in polling mode.
372 The HAL status of all data processing is returned by the same function
373 after finishing transfer.
374 (++) Non-Blocking mode: the communication is performed using Interrupts
375 or DMA, these API's return the HAL status.
376 The end of the data processing will be indicated through the
377 dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
379 The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
380 will be executed respectively at the end of the Transmit or Receive process
381 The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
383 (#) Blocking mode APIs are :
384 (++) HAL_IRDA_Transmit()
385 (++) HAL_IRDA_Receive()
387 (#) Non-Blocking mode APIs with Interrupt are :
388 (++) HAL_IRDA_Transmit_IT()
389 (++) HAL_IRDA_Receive_IT()
390 (++) HAL_IRDA_IRQHandler()
391 (++) IRDA_Transmit_IT()
392 (++) IRDA_Receive_IT()
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()
408 (#) Non-Blocking mode transfers could be aborted using Abort API's :
409 (++) HAL_IRDA_Abort()
410 (++) HAL_IRDA_AbortTransmit()
411 (++) HAL_IRDA_AbortReceive()
412 (++) HAL_IRDA_Abort_IT()
413 (++) HAL_IRDA_AbortTransmit_IT()
414 (++) HAL_IRDA_AbortReceive_IT()
416 (#) For Abort services based on interrupts (HAL_IRDA_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
417 (++) HAL_IRDA_AbortCpltCallback()
418 (++) HAL_IRDA_AbortTransmitCpltCallback()
419 (++) HAL_IRDA_AbortReceiveCpltCallback()
421 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
422 Errors are handled as follows :
423 (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
424 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
425 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
426 and HAL_IRDA_ErrorCallback() user callback is executed. Transfer is kept ongoing on IRDA side.
427 If user wants to abort it, Abort services should be called by user.
428 (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
429 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
430 Error code is set to allow user to identify error type, and HAL_IRDA_ErrorCallback() user callback is executed.
437 * @brief Send an amount of data in blocking mode.
438 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
439 * the configuration information for the specified IRDA module.
440 * @param pData Pointer to data buffer.
441 * @param Size Amount of data to be sent.
442 * @param Timeout Specify timeout value.
445 HAL_StatusTypeDef
HAL_IRDA_Transmit(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
448 uint32_t tickstart
= 0U;
450 /* Check that a Tx process is not already ongoing */
451 if(hirda
->gState
== HAL_IRDA_STATE_READY
)
453 if((pData
== NULL
) || (Size
== 0))
461 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
462 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
464 /* Init tickstart for timeout managment*/
465 tickstart
= HAL_GetTick();
467 hirda
->TxXferSize
= Size
;
468 hirda
->TxXferCount
= Size
;
469 while(hirda
->TxXferCount
> 0)
471 hirda
->TxXferCount
--;
473 if(IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
477 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
479 tmp
= (uint16_t*) pData
;
480 hirda
->Instance
->TDR
= (*tmp
& (uint16_t)0x01FF);
485 hirda
->Instance
->TDR
= (*pData
++ & (uint8_t)0xFF);
489 if(IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_TC
, RESET
, tickstart
, Timeout
) != HAL_OK
)
494 /* At end of Tx process, restore hirda->gState to Ready */
495 hirda
->gState
= HAL_IRDA_STATE_READY
;
497 /* Process Unlocked */
509 * @brief Receive an amount of data in blocking mode.
510 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
511 * the configuration information for the specified IRDA module.
512 * @param pData Pointer to data buffer.
513 * @param Size Amount of data to be received.
514 * @param Timeout Specify timeout value.
517 HAL_StatusTypeDef
HAL_IRDA_Receive(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
521 uint32_t tickstart
= 0;
523 /* Check that a Rx process is not already ongoing */
524 if(hirda
->RxState
== HAL_IRDA_STATE_READY
)
526 if((pData
== NULL
) || (Size
== 0))
534 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
535 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
537 /* Init tickstart for timeout managment*/
538 tickstart
= HAL_GetTick();
540 hirda
->RxXferSize
= Size
;
541 hirda
->RxXferCount
= Size
;
543 /* Computation of the mask to apply to RDR register
544 of the UART associated to the IRDA */
545 IRDA_MASK_COMPUTATION(hirda
);
546 uhMask
= hirda
->Mask
;
548 /* Check data remaining to be received */
549 while(hirda
->RxXferCount
> 0)
551 hirda
->RxXferCount
--;
553 if(IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
557 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
559 tmp
= (uint16_t*) pData
;
560 *tmp
= (uint16_t)(hirda
->Instance
->RDR
& uhMask
);
565 *pData
++ = (uint8_t)(hirda
->Instance
->RDR
& (uint8_t)uhMask
);
569 /* At end of Rx process, restore hirda->RxState to Ready */
570 hirda
->RxState
= HAL_IRDA_STATE_READY
;
572 /* Process Unlocked */
584 * @brief Send an amount of data in interrupt mode.
585 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
586 * the configuration information for the specified IRDA module.
587 * @param pData Pointer to data buffer.
588 * @param Size Amount of data to be sent.
591 HAL_StatusTypeDef
HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
593 /* Check that a Tx process is not already ongoing */
594 if(hirda
->gState
== HAL_IRDA_STATE_READY
)
596 if((pData
== NULL
) || (Size
== 0))
604 hirda
->pTxBuffPtr
= pData
;
605 hirda
->TxXferSize
= Size
;
606 hirda
->TxXferCount
= Size
;
608 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
609 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
611 /* Process Unlocked */
614 /* Enable the IRDA Transmit Data Register Empty Interrupt */
615 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TXEIE
);
626 * @brief Receive an amount of data in interrupt mode.
627 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
628 * the configuration information for the specified IRDA module.
629 * @param pData Pointer to data buffer.
630 * @param Size Amount of data to be received.
633 HAL_StatusTypeDef
HAL_IRDA_Receive_IT(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
635 /* Check that a Rx process is not already ongoing */
636 if(hirda
->RxState
== HAL_IRDA_STATE_READY
)
638 if((pData
== NULL
) || (Size
== 0))
646 hirda
->pRxBuffPtr
= pData
;
647 hirda
->RxXferSize
= Size
;
648 hirda
->RxXferCount
= Size
;
650 /* Computation of the mask to apply to the RDR register
651 of the UART associated to the IRDA */
652 IRDA_MASK_COMPUTATION(hirda
);
654 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
655 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
657 /* Process Unlocked */
660 /* Enable the IRDA Parity Error and Data Register not empty Interrupts */
661 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE
);
663 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
664 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
675 * @brief Send an amount of data in DMA mode.
676 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
677 * the configuration information for the specified IRDA module.
678 * @param pData pointer to data buffer.
679 * @param Size amount of data to be sent.
682 HAL_StatusTypeDef
HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
684 /* Check that a Tx process is not already ongoing */
685 if(hirda
->gState
== HAL_IRDA_STATE_READY
)
687 if((pData
== NULL
) || (Size
== 0))
695 hirda
->pTxBuffPtr
= pData
;
696 hirda
->TxXferSize
= Size
;
697 hirda
->TxXferCount
= Size
;
699 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
700 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
702 /* Set the IRDA DMA transfer complete callback */
703 hirda
->hdmatx
->XferCpltCallback
= IRDA_DMATransmitCplt
;
705 /* Set the IRDA DMA half transfer complete callback */
706 hirda
->hdmatx
->XferHalfCpltCallback
= IRDA_DMATransmitHalfCplt
;
708 /* Set the DMA error callback */
709 hirda
->hdmatx
->XferErrorCallback
= IRDA_DMAError
;
711 /* Set the DMA abort callback */
712 hirda
->hdmatx
->XferAbortCallback
= NULL
;
714 /* Enable the IRDA transmit DMA channel */
715 HAL_DMA_Start_IT(hirda
->hdmatx
, (uint32_t)hirda
->pTxBuffPtr
, (uint32_t)&hirda
->Instance
->TDR
, Size
);
717 /* Clear the TC flag in the ICR register */
718 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_TCF
);
720 /* Process Unlocked */
723 /* Enable the DMA transfer for transmit request by setting the DMAT bit
724 in the USART CR3 register */
725 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
736 * @brief Receive an amount of data in DMA mode.
737 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
738 * the configuration information for the specified IRDA module.
739 * @param pData Pointer to data buffer.
740 * @param Size Amount of data to be received.
741 * @note When the IRDA parity is enabled (PCE = 1), the received data contains
742 * the parity bit (MSB position).
745 HAL_StatusTypeDef
HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
747 /* Check that a Rx process is not already ongoing */
748 if(hirda
->RxState
== HAL_IRDA_STATE_READY
)
750 if((pData
== NULL
) || (Size
== 0))
758 hirda
->pRxBuffPtr
= pData
;
759 hirda
->RxXferSize
= Size
;
761 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
762 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
764 /* Set the IRDA DMA transfer complete callback */
765 hirda
->hdmarx
->XferCpltCallback
= IRDA_DMAReceiveCplt
;
767 /* Set the IRDA DMA half transfer complete callback */
768 hirda
->hdmarx
->XferHalfCpltCallback
= IRDA_DMAReceiveHalfCplt
;
770 /* Set the DMA error callback */
771 hirda
->hdmarx
->XferErrorCallback
= IRDA_DMAError
;
773 /* Set the DMA abort callback */
774 hirda
->hdmarx
->XferAbortCallback
= NULL
;
776 /* Enable the DMA channel */
777 HAL_DMA_Start_IT(hirda
->hdmarx
, (uint32_t)&hirda
->Instance
->RDR
, (uint32_t)hirda
->pRxBuffPtr
, Size
);
779 /* Process Unlocked */
782 /* Enable the UART Parity Error Interrupt */
783 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
785 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
786 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
788 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
789 in the USART CR3 register */
790 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
802 * @brief Pause the DMA Transfer.
803 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
804 * the configuration information for the specified IRDA module.
807 HAL_StatusTypeDef
HAL_IRDA_DMAPause(IRDA_HandleTypeDef
*hirda
)
812 if ((hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
) &&
813 (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
)))
815 /* Disable the IRDA DMA Tx request */
816 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
818 if ((hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
) &&
819 (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
)))
821 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
822 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
823 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
825 /* Disable the IRDA DMA Rx request */
826 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
829 /* Process Unlocked */
836 * @brief Resume the DMA Transfer.
837 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
838 * the configuration information for the specified UART module.
841 HAL_StatusTypeDef
HAL_IRDA_DMAResume(IRDA_HandleTypeDef
*hirda
)
846 if(hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
848 /* Enable the IRDA DMA Tx request */
849 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
851 if(hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
853 /* Clear the Overrun flag before resuming the Rx transfer*/
854 __HAL_IRDA_CLEAR_OREFLAG(hirda
);
856 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
857 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
858 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
860 /* Enable the IRDA DMA Rx request */
861 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
864 /* Process Unlocked */
871 * @brief Stop the DMA Transfer.
872 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
873 * the configuration information for the specified UART module.
876 HAL_StatusTypeDef
HAL_IRDA_DMAStop(IRDA_HandleTypeDef
*hirda
)
878 /* The Lock is not implemented on this API to allow the user application
879 to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback() /
880 HAL_IRDA_TxHalfCpltCallback / HAL_IRDA_RxHalfCpltCallback:
881 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
882 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
883 the stream and the corresponding call back is executed. */
885 /* Stop IRDA DMA Tx request if ongoing */
886 if ((hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
) &&
887 (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
)))
889 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
891 /* Abort the IRDA DMA Tx channel */
892 if(hirda
->hdmatx
!= NULL
)
894 HAL_DMA_Abort(hirda
->hdmatx
);
897 IRDA_EndTxTransfer(hirda
);
900 /* Stop IRDA DMA Rx request if ongoing */
901 if ((hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
) &&
902 (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
)))
904 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
906 /* Abort the IRDA DMA Rx channel */
907 if(hirda
->hdmarx
!= NULL
)
909 HAL_DMA_Abort(hirda
->hdmarx
);
912 IRDA_EndRxTransfer(hirda
);
919 * @brief Abort ongoing transfers (blocking mode).
920 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
921 * the configuration information for the specified UART module.
922 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
923 * This procedure performs following operations :
924 * - Disable IRDA Interrupts (Tx and Rx)
925 * - Disable the DMA transfer in the peripheral register (if enabled)
926 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
927 * - Set handle State to READY
928 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
931 HAL_StatusTypeDef
HAL_IRDA_Abort(IRDA_HandleTypeDef
*hirda
)
933 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
934 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
| USART_CR1_TCIE
));
935 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
937 /* Disable the IRDA DMA Tx request if enabled */
938 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
940 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
942 /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
943 if(hirda
->hdmatx
!= NULL
)
945 /* Set the IRDA DMA Abort callback to Null.
946 No call back execution at end of DMA abort procedure */
947 hirda
->hdmatx
->XferAbortCallback
= NULL
;
949 HAL_DMA_Abort(hirda
->hdmatx
);
953 /* Disable the IRDA DMA Rx request if enabled */
954 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
956 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
958 /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
959 if(hirda
->hdmarx
!= NULL
)
961 /* Set the IRDA DMA Abort callback to Null.
962 No call back execution at end of DMA abort procedure */
963 hirda
->hdmarx
->XferAbortCallback
= NULL
;
965 HAL_DMA_Abort(hirda
->hdmarx
);
969 /* Reset Tx and Rx transfer counters */
970 hirda
->TxXferCount
= 0;
971 hirda
->RxXferCount
= 0;
973 /* Clear the Error flags in the ICR register */
974 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
976 /* Restore hirda->gState and hirda->RxState to Ready */
977 hirda
->gState
= HAL_IRDA_STATE_READY
;
978 hirda
->RxState
= HAL_IRDA_STATE_READY
;
980 /* Reset Handle ErrorCode to No Error */
981 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
987 * @brief Abort ongoing Transmit transfer (blocking mode).
988 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
989 * the configuration information for the specified UART module.
990 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
991 * This procedure performs following operations :
992 * - Disable IRDA Interrupts (Tx)
993 * - Disable the DMA transfer in the peripheral register (if enabled)
994 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
995 * - Set handle State to READY
996 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
999 HAL_StatusTypeDef
HAL_IRDA_AbortTransmit(IRDA_HandleTypeDef
*hirda
)
1001 /* Disable TXEIE and TCIE interrupts */
1002 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1004 /* Disable the IRDA DMA Tx request if enabled */
1005 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1007 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1009 /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
1010 if(hirda
->hdmatx
!= NULL
)
1012 /* Set the IRDA DMA Abort callback to Null.
1013 No call back execution at end of DMA abort procedure */
1014 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1016 HAL_DMA_Abort(hirda
->hdmatx
);
1020 /* Reset Tx transfer counter */
1021 hirda
->TxXferCount
= 0;
1023 /* Restore hirda->gState to Ready */
1024 hirda
->gState
= HAL_IRDA_STATE_READY
;
1030 * @brief Abort ongoing Receive transfer (blocking mode).
1031 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1032 * the configuration information for the specified UART module.
1033 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1034 * This procedure performs following operations :
1035 * - Disable IRDA Interrupts (Rx)
1036 * - Disable the DMA transfer in the peripheral register (if enabled)
1037 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1038 * - Set handle State to READY
1039 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1040 * @retval HAL status
1042 HAL_StatusTypeDef
HAL_IRDA_AbortReceive(IRDA_HandleTypeDef
*hirda
)
1044 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1045 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1046 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1048 /* Disable the IRDA DMA Rx request if enabled */
1049 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1051 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1053 /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
1054 if(hirda
->hdmarx
!= NULL
)
1056 /* Set the IRDA DMA Abort callback to Null.
1057 No call back execution at end of DMA abort procedure */
1058 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1060 HAL_DMA_Abort(hirda
->hdmarx
);
1064 /* Reset Rx transfer counter */
1065 hirda
->RxXferCount
= 0;
1067 /* Clear the Error flags in the ICR register */
1068 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1070 /* Restore hirda->RxState to Ready */
1071 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1077 * @brief Abort ongoing transfers (Interrupt mode).
1078 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1079 * the configuration information for the specified UART module.
1080 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1081 * This procedure performs following operations :
1082 * - Disable IRDA Interrupts (Tx and Rx)
1083 * - Disable the DMA transfer in the peripheral register (if enabled)
1084 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1085 * - Set handle State to READY
1086 * - At abort completion, call user abort complete callback
1087 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1088 * considered as completed only when user abort complete callback is executed (not when exiting function).
1089 * @retval HAL status
1091 HAL_StatusTypeDef
HAL_IRDA_Abort_IT(IRDA_HandleTypeDef
*hirda
)
1093 uint32_t abortcplt
= 1;
1095 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1096 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
| USART_CR1_TCIE
));
1097 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1099 /* If DMA Tx and/or DMA Rx Handles are associated to IRDA Handle, DMA Abort complete callbacks should be initialised
1100 before any call to DMA Abort functions */
1101 /* DMA Tx Handle is valid */
1102 if(hirda
->hdmatx
!= NULL
)
1104 /* Set DMA Abort Complete callback if IRDA DMA Tx request if enabled.
1105 Otherwise, set it to NULL */
1106 if(HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1108 hirda
->hdmatx
->XferAbortCallback
= IRDA_DMATxAbortCallback
;
1112 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1115 /* DMA Rx Handle is valid */
1116 if(hirda
->hdmarx
!= NULL
)
1118 /* Set DMA Abort Complete callback if IRDA DMA Rx request if enabled.
1119 Otherwise, set it to NULL */
1120 if(HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1122 hirda
->hdmarx
->XferAbortCallback
= IRDA_DMARxAbortCallback
;
1126 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1130 /* Disable the IRDA DMA Tx request if enabled */
1131 if(HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1133 /* Disable DMA Tx at UART level */
1134 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1136 /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
1137 if(hirda
->hdmatx
!= NULL
)
1139 /* IRDA Tx DMA Abort callback has already been initialised :
1140 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1143 if(HAL_DMA_Abort_IT(hirda
->hdmatx
) != HAL_OK
)
1145 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1154 /* Disable the IRDA DMA Rx request if enabled */
1155 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1157 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1159 /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
1160 if(hirda
->hdmarx
!= NULL
)
1162 /* IRDA Rx DMA Abort callback has already been initialised :
1163 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1166 if(HAL_DMA_Abort_IT(hirda
->hdmarx
) != HAL_OK
)
1168 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1178 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1181 /* Reset Tx and Rx transfer counters */
1182 hirda
->TxXferCount
= 0;
1183 hirda
->RxXferCount
= 0;
1185 /* Reset errorCode */
1186 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1188 /* Clear the Error flags in the ICR register */
1189 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1191 /* Restore hirda->gState and hirda->RxState to Ready */
1192 hirda
->gState
= HAL_IRDA_STATE_READY
;
1193 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1195 /* As no DMA to be aborted, call directly user Abort complete callback */
1196 HAL_IRDA_AbortCpltCallback(hirda
);
1203 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1204 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1205 * the configuration information for the specified UART module.
1206 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1207 * This procedure performs following operations :
1208 * - Disable IRDA Interrupts (Tx)
1209 * - Disable the DMA transfer in the peripheral register (if enabled)
1210 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1211 * - Set handle State to READY
1212 * - At abort completion, call user abort complete callback
1213 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1214 * considered as completed only when user abort complete callback is executed (not when exiting function).
1215 * @retval HAL status
1217 HAL_StatusTypeDef
HAL_IRDA_AbortTransmit_IT(IRDA_HandleTypeDef
*hirda
)
1219 /* Disable TXEIE and TCIE interrupts */
1220 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1222 /* Disable the IRDA DMA Tx request if enabled */
1223 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1225 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1227 /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
1228 if(hirda
->hdmatx
!= NULL
)
1230 /* Set the IRDA DMA Abort callback :
1231 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1232 hirda
->hdmatx
->XferAbortCallback
= IRDA_DMATxOnlyAbortCallback
;
1235 if(HAL_DMA_Abort_IT(hirda
->hdmatx
) != HAL_OK
)
1237 /* Call Directly hirda->hdmatx->XferAbortCallback function in case of error */
1238 hirda
->hdmatx
->XferAbortCallback(hirda
->hdmatx
);
1243 /* Reset Tx transfer counter */
1244 hirda
->TxXferCount
= 0;
1246 /* Restore hirda->gState to Ready */
1247 hirda
->gState
= HAL_IRDA_STATE_READY
;
1249 /* As no DMA to be aborted, call directly user Abort complete callback */
1250 HAL_IRDA_AbortTransmitCpltCallback(hirda
);
1255 /* Reset Tx transfer counter */
1256 hirda
->TxXferCount
= 0;
1258 /* Restore hirda->gState to Ready */
1259 hirda
->gState
= HAL_IRDA_STATE_READY
;
1261 /* As no DMA to be aborted, call directly user Abort complete callback */
1262 HAL_IRDA_AbortTransmitCpltCallback(hirda
);
1269 * @brief Abort ongoing Receive transfer (Interrupt mode).
1270 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1271 * the configuration information for the specified UART module.
1272 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1273 * This procedure performs following operations :
1274 * - Disable IRDA Interrupts (Rx)
1275 * - Disable the DMA transfer in the peripheral register (if enabled)
1276 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1277 * - Set handle State to READY
1278 * - At abort completion, call user abort complete callback
1279 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1280 * considered as completed only when user abort complete callback is executed (not when exiting function).
1281 * @retval HAL status
1283 HAL_StatusTypeDef
HAL_IRDA_AbortReceive_IT(IRDA_HandleTypeDef
*hirda
)
1285 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1286 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1287 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1289 /* Disable the IRDA DMA Rx request if enabled */
1290 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1292 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1294 /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
1295 if(hirda
->hdmarx
!= NULL
)
1297 /* Set the IRDA DMA Abort callback :
1298 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1299 hirda
->hdmarx
->XferAbortCallback
= IRDA_DMARxOnlyAbortCallback
;
1302 if(HAL_DMA_Abort_IT(hirda
->hdmarx
) != HAL_OK
)
1304 /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
1305 hirda
->hdmarx
->XferAbortCallback(hirda
->hdmarx
);
1310 /* Reset Rx transfer counter */
1311 hirda
->RxXferCount
= 0;
1313 /* Clear the Error flags in the ICR register */
1314 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1316 /* Restore hirda->RxState to Ready */
1317 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1319 /* As no DMA to be aborted, call directly user Abort complete callback */
1320 HAL_IRDA_AbortReceiveCpltCallback(hirda
);
1325 /* Reset Rx transfer counter */
1326 hirda
->RxXferCount
= 0;
1328 /* Clear the Error flags in the ICR register */
1329 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1331 /* Restore hirda->RxState to Ready */
1332 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1334 /* As no DMA to be aborted, call directly user Abort complete callback */
1335 HAL_IRDA_AbortReceiveCpltCallback(hirda
);
1342 * @brief Handle IRDA interrupt request.
1343 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1344 * the configuration information for the specified IRDA module.
1347 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef
*hirda
)
1349 uint32_t isrflags
= READ_REG(hirda
->Instance
->ISR
);
1350 uint32_t cr1its
= READ_REG(hirda
->Instance
->CR1
);
1352 uint32_t errorflags
;
1354 /* If no error occurs */
1355 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
));
1356 if (errorflags
== RESET
)
1358 /* IRDA in mode Receiver ---------------------------------------------------*/
1359 if(((isrflags
& USART_ISR_RXNE
) != RESET
) && ((cr1its
& USART_CR1_RXNEIE
) != RESET
))
1361 IRDA_Receive_IT(hirda
);
1366 /* If some errors occur */
1367 cr3its
= READ_REG(hirda
->Instance
->CR3
);
1368 if( (errorflags
!= RESET
)
1369 && ( ((cr3its
& USART_CR3_EIE
) != RESET
)
1370 || ((cr1its
& (USART_CR1_RXNEIE
| USART_CR1_PEIE
)) != RESET
)) )
1372 /* IRDA parity error interrupt occurred -------------------------------------*/
1373 if(((isrflags
& USART_ISR_PE
) != RESET
) && ((cr1its
& USART_CR1_PEIE
) != RESET
))
1375 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_PEF
);
1377 hirda
->ErrorCode
|= HAL_IRDA_ERROR_PE
;
1380 /* IRDA frame error interrupt occurred --------------------------------------*/
1381 if(((isrflags
& USART_ISR_FE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
1383 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_FEF
);
1385 hirda
->ErrorCode
|= HAL_IRDA_ERROR_FE
;
1388 /* IRDA noise error interrupt occurred --------------------------------------*/
1389 if(((isrflags
& USART_ISR_NE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
1391 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_NEF
);
1393 hirda
->ErrorCode
|= HAL_IRDA_ERROR_NE
;
1396 /* IRDA Over-Run interrupt occurred -----------------------------------------*/
1397 if(((isrflags
& USART_ISR_ORE
) != RESET
) &&
1398 (((cr1its
& USART_CR1_RXNEIE
) != RESET
) || ((cr3its
& USART_CR3_EIE
) != RESET
)))
1400 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_OREF
);
1402 hirda
->ErrorCode
|= HAL_IRDA_ERROR_ORE
;
1405 /* Call IRDA Error Call back function if need be --------------------------*/
1406 if(hirda
->ErrorCode
!= HAL_IRDA_ERROR_NONE
)
1408 /* IRDA in mode Receiver ---------------------------------------------------*/
1409 if(((isrflags
& USART_ISR_RXNE
) != RESET
) && ((cr1its
& USART_CR1_RXNEIE
) != RESET
))
1411 IRDA_Receive_IT(hirda
);
1414 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1415 consider error as blocking */
1416 if (((hirda
->ErrorCode
& HAL_IRDA_ERROR_ORE
) != RESET
) ||
1417 (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
)))
1419 /* Blocking error : transfer is aborted
1420 Set the IRDA state ready to be able to start again the process,
1421 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1422 IRDA_EndRxTransfer(hirda
);
1424 /* Disable the IRDA DMA Rx request if enabled */
1425 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1427 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1429 /* Abort the IRDA DMA Rx channel */
1430 if(hirda
->hdmarx
!= NULL
)
1432 /* Set the IRDA DMA Abort callback :
1433 will lead to call HAL_IRDA_ErrorCallback() at end of DMA abort procedure */
1434 hirda
->hdmarx
->XferAbortCallback
= IRDA_DMAAbortOnError
;
1437 if(HAL_DMA_Abort_IT(hirda
->hdmarx
) != HAL_OK
)
1439 /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
1440 hirda
->hdmarx
->XferAbortCallback(hirda
->hdmarx
);
1445 /* Call user error callback */
1446 HAL_IRDA_ErrorCallback(hirda
);
1451 /* Call user error callback */
1452 HAL_IRDA_ErrorCallback(hirda
);
1457 /* Non Blocking error : transfer could go on.
1458 Error is notified to user through user error callback */
1459 HAL_IRDA_ErrorCallback(hirda
);
1460 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1465 } /* End if some error occurs */
1467 /* IRDA in mode Transmitter ------------------------------------------------*/
1468 if(((isrflags
& USART_ISR_TXE
) != RESET
) && ((cr1its
& USART_CR1_TXEIE
) != RESET
))
1470 IRDA_Transmit_IT(hirda
);
1474 /* IRDA in mode Transmitter (transmission end) -----------------------------*/
1475 if(((isrflags
& USART_ISR_TC
) != RESET
) && ((cr1its
& USART_CR1_TCIE
) != RESET
))
1477 IRDA_EndTransmit_IT(hirda
);
1484 * @brief Tx Transfer completed callback.
1485 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1486 * the configuration information for the specified IRDA module.
1489 __weak
void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef
*hirda
)
1491 /* Prevent unused argument(s) compilation warning */
1494 /* NOTE : This function should not be modified, when the callback is needed,
1495 the HAL_IRDA_TxCpltCallback can be implemented in the user file.
1500 * @brief Tx Half Transfer completed callback.
1501 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1502 * the configuration information for the specified USART module.
1505 __weak
void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef
*hirda
)
1507 /* Prevent unused argument(s) compilation warning */
1510 /* NOTE : This function should not be modified, when the callback is needed,
1511 the HAL_IRDA_TxHalfCpltCallback can be implemented in the user file.
1516 * @brief Rx Transfer completed callback.
1517 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1518 * the configuration information for the specified IRDA module.
1521 __weak
void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef
*hirda
)
1523 /* Prevent unused argument(s) compilation warning */
1526 /* NOTE : This function should not be modified, when the callback is needed,
1527 the HAL_IRDA_RxCpltCallback can be implemented in the user file.
1532 * @brief Rx Half Transfer complete callback.
1533 * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains
1534 * the configuration information for the specified IRDA module.
1537 __weak
void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef
*hirda
)
1539 /* Prevent unused argument(s) compilation warning */
1542 /* NOTE : This function should not be modified, when the callback is needed,
1543 the HAL_IRDA_RxHalfCpltCallback can be implemented in the user file.
1548 * @brief IRDA error callback.
1549 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1550 * the configuration information for the specified IRDA module.
1553 __weak
void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef
*hirda
)
1555 /* Prevent unused argument(s) compilation warning */
1558 /* NOTE : This function should not be modified, when the callback is needed,
1559 the HAL_IRDA_ErrorCallback can be implemented in the user file.
1564 * @brief IRDA Abort Complete callback.
1565 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1566 * the configuration information for the specified IRDA module.
1569 __weak
void HAL_IRDA_AbortCpltCallback (IRDA_HandleTypeDef
*hirda
)
1571 /* Prevent unused argument(s) compilation warning */
1574 /* NOTE : This function should not be modified, when the callback is needed,
1575 the HAL_IRDA_AbortCpltCallback can be implemented in the user file.
1580 * @brief IRDA Abort Complete callback.
1581 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1582 * the configuration information for the specified IRDA module.
1585 __weak
void HAL_IRDA_AbortTransmitCpltCallback (IRDA_HandleTypeDef
*hirda
)
1587 /* Prevent unused argument(s) compilation warning */
1590 /* NOTE : This function should not be modified, when the callback is needed,
1591 the HAL_IRDA_AbortTransmitCpltCallback can be implemented in the user file.
1596 * @brief IRDA Abort Receive Complete callback.
1597 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1598 * the configuration information for the specified IRDA module.
1601 __weak
void HAL_IRDA_AbortReceiveCpltCallback (IRDA_HandleTypeDef
*hirda
)
1603 /* Prevent unused argument(s) compilation warning */
1606 /* NOTE : This function should not be modified, when the callback is needed,
1607 the HAL_IRDA_AbortReceiveCpltCallback can be implemented in the user file.
1615 /** @defgroup IRDA_Exported_Functions_Group4 Peripheral State and Error functions
1616 * @brief IRDA State and Errors functions
1619 ==============================================================================
1620 ##### Peripheral State and Error functions #####
1621 ==============================================================================
1623 This subsection provides a set of functions allowing to return the State of IrDA
1624 communication process and also return Peripheral Errors occurred during communication process
1625 (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state
1626 of the IRDA peripheral handle.
1627 (+) HAL_IRDA_GetError() checks in run-time errors that could occur during
1635 * @brief Return the IRDA handle state.
1636 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1637 * the configuration information for the specified IRDA module.
1640 HAL_IRDA_StateTypeDef
HAL_IRDA_GetState(IRDA_HandleTypeDef
*hirda
)
1642 /* Return IRDA handle state */
1643 uint32_t temp1
= 0x00, temp2
= 0x00;
1644 temp1
= hirda
->gState
;
1645 temp2
= hirda
->RxState
;
1647 return (HAL_IRDA_StateTypeDef
)(temp1
| temp2
);
1651 * @brief Return the IRDA handle error code.
1652 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1653 * the configuration information for the specified IRDA module.
1654 * @retval IRDA Error Code
1656 uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef
*hirda
)
1658 return hirda
->ErrorCode
;
1669 /** @defgroup IRDA_Private_Functions IRDA Private Functions
1674 * @brief Configure the IRDA peripheral.
1675 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1676 * the configuration information for the specified IRDA module.
1679 static HAL_StatusTypeDef
IRDA_SetConfig(IRDA_HandleTypeDef
*hirda
)
1681 uint32_t tmpreg
= 0x00000000;
1682 IRDA_ClockSourceTypeDef clocksource
= IRDA_CLOCKSOURCE_UNDEFINED
;
1683 HAL_StatusTypeDef ret
= HAL_OK
;
1685 /* Check the communication parameters */
1686 assert_param(IS_IRDA_BAUDRATE(hirda
->Init
.BaudRate
));
1687 assert_param(IS_IRDA_WORD_LENGTH(hirda
->Init
.WordLength
));
1688 assert_param(IS_IRDA_PARITY(hirda
->Init
.Parity
));
1689 assert_param(IS_IRDA_TX_RX_MODE(hirda
->Init
.Mode
));
1690 assert_param(IS_IRDA_PRESCALER(hirda
->Init
.Prescaler
));
1691 assert_param(IS_IRDA_POWERMODE(hirda
->Init
.PowerMode
));
1693 /*-------------------------- USART CR1 Configuration -----------------------*/
1694 /* Configure the IRDA Word Length, Parity and transfer Mode:
1695 Set the M bits according to hirda->Init.WordLength value
1696 Set PCE and PS bits according to hirda->Init.Parity value
1697 Set TE and RE bits according to hirda->Init.Mode value */
1698 tmpreg
= (uint32_t)hirda
->Init
.WordLength
| hirda
->Init
.Parity
| hirda
->Init
.Mode
;
1700 MODIFY_REG(hirda
->Instance
->CR1
, IRDA_CR1_FIELDS
, tmpreg
);
1702 /*-------------------------- USART CR3 Configuration -----------------------*/
1703 MODIFY_REG(hirda
->Instance
->CR3
, USART_CR3_IRLP
, hirda
->Init
.PowerMode
);
1705 /*-------------------------- USART GTPR Configuration ----------------------*/
1706 MODIFY_REG(hirda
->Instance
->GTPR
, USART_GTPR_PSC
, hirda
->Init
.Prescaler
);
1708 /*-------------------------- USART BRR Configuration -----------------------*/
1709 IRDA_GETCLOCKSOURCE(hirda
, clocksource
);
1710 switch (clocksource
)
1712 case IRDA_CLOCKSOURCE_PCLK1
:
1713 hirda
->Instance
->BRR
= (uint16_t)((HAL_RCC_GetPCLK1Freq() + (hirda
->Init
.BaudRate
/2)) / hirda
->Init
.BaudRate
);
1715 case IRDA_CLOCKSOURCE_PCLK2
:
1716 hirda
->Instance
->BRR
= (uint16_t)((HAL_RCC_GetPCLK2Freq() + (hirda
->Init
.BaudRate
/2)) / hirda
->Init
.BaudRate
);
1718 case IRDA_CLOCKSOURCE_HSI
:
1719 hirda
->Instance
->BRR
= (uint16_t)((HSI_VALUE
+ (hirda
->Init
.BaudRate
/2)) / hirda
->Init
.BaudRate
);
1721 case IRDA_CLOCKSOURCE_SYSCLK
:
1722 hirda
->Instance
->BRR
= (uint16_t)((HAL_RCC_GetSysClockFreq() + (hirda
->Init
.BaudRate
/2)) / hirda
->Init
.BaudRate
);
1724 case IRDA_CLOCKSOURCE_LSE
:
1725 hirda
->Instance
->BRR
= (uint16_t)((LSE_VALUE
+ (hirda
->Init
.BaudRate
/2)) / hirda
->Init
.BaudRate
);
1727 case IRDA_CLOCKSOURCE_UNDEFINED
:
1737 * @brief Check the IRDA Idle State.
1738 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1739 * the configuration information for the specified IRDA module.
1740 * @retval HAL status
1742 static HAL_StatusTypeDef
IRDA_CheckIdleState(IRDA_HandleTypeDef
*hirda
)
1744 uint32_t tickstart
= 0;
1746 /* Initialize the IRDA ErrorCode */
1747 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1749 /* Init tickstart for timeout managment*/
1750 tickstart
= HAL_GetTick();
1752 /* Check if the Transmitter is enabled */
1753 if((hirda
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
1755 /* Wait until TEACK flag is set */
1756 if(IRDA_WaitOnFlagUntilTimeout(hirda
, USART_ISR_TEACK
, RESET
, tickstart
, IRDA_TEACK_REACK_TIMEOUT
) != HAL_OK
)
1758 /* Timeout occurred */
1763 /* Initialize the IRDA state*/
1764 hirda
->gState
= HAL_IRDA_STATE_READY
;
1765 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1767 /* Process Unlocked */
1768 __HAL_UNLOCK(hirda
);
1774 * @brief Handle IRDA Communication Timeout.
1775 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1776 * the configuration information for the specified IRDA module.
1777 * @param Flag Specifies the IRDA flag to check.
1778 * @param Status Flag status (SET or RESET)
1779 * @param Tickstart Tick start value
1780 * @param Timeout Timeout duration
1781 * @retval HAL status
1783 static HAL_StatusTypeDef
IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef
*hirda
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
)
1785 /* Wait until flag is set */
1786 while((__HAL_IRDA_GET_FLAG(hirda
, Flag
) ? SET
: RESET
) == Status
)
1788 /* Check for the Timeout */
1789 if(Timeout
!= HAL_MAX_DELAY
)
1791 if((Timeout
== 0) || ((HAL_GetTick()-Tickstart
) > Timeout
))
1793 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1794 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
));
1795 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1797 hirda
->gState
= HAL_IRDA_STATE_READY
;
1798 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1800 /* Process Unlocked */
1801 __HAL_UNLOCK(hirda
);
1811 * @brief End ongoing Tx transfer on IRDA peripheral (following error detection or Transmit completion).
1812 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1813 * the configuration information for the specified IRDA module.
1816 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef
*hirda
)
1818 /* Disable TXEIE and TCIE interrupts */
1819 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1821 /* At end of Tx process, restore hirda->gState to Ready */
1822 hirda
->gState
= HAL_IRDA_STATE_READY
;
1827 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
1828 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1829 * the configuration information for the specified IRDA module.
1832 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef
*hirda
)
1834 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1835 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1836 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1838 /* At end of Rx process, restore hirda->RxState to Ready */
1839 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1844 * @brief DMA IRDA transmit process complete callback.
1845 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
1846 * the configuration information for the specified DMA module.
1849 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
1851 IRDA_HandleTypeDef
* hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
1853 /* DMA Normal mode */
1854 if ( HAL_IS_BIT_CLR(hdma
->Instance
->CR
, DMA_SxCR_CIRC
) )
1856 hirda
->TxXferCount
= 0;
1858 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
1859 in the IRDA CR3 register */
1860 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1862 /* Enable the IRDA Transmit Complete Interrupt */
1863 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
1865 /* DMA Circular mode */
1868 HAL_IRDA_TxCpltCallback(hirda
);
1874 * @brief DMA IRDA transmit process half complete callback.
1875 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
1876 * the configuration information for the specified DMA module.
1879 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef
*hdma
)
1881 IRDA_HandleTypeDef
* hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
1883 HAL_IRDA_TxHalfCpltCallback(hirda
);
1887 * @brief DMA IRDA receive process complete callback.
1888 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1889 * the configuration information for the specified DMA module.
1892 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
1894 IRDA_HandleTypeDef
* hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
1896 /* DMA Normal mode */
1897 if ( HAL_IS_BIT_CLR(hdma
->Instance
->CR
, DMA_SxCR_CIRC
) )
1899 hirda
->RxXferCount
= 0;
1901 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1902 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
1903 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1905 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
1906 in the IRDA CR3 register */
1907 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1909 /* At end of Rx process, restore hirda->RxState to Ready */
1910 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1913 HAL_IRDA_RxCpltCallback(hirda
);
1917 * @brief DMA IRDA receive process half complete callback.
1918 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1919 * the configuration information for the specified DMA module.
1922 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef
*hdma
)
1924 IRDA_HandleTypeDef
* hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
1926 HAL_IRDA_RxHalfCpltCallback(hirda
);
1930 * @brief DMA IRDA communication error callback.
1931 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
1932 * the configuration information for the specified DMA module.
1935 static void IRDA_DMAError(DMA_HandleTypeDef
*hdma
)
1937 IRDA_HandleTypeDef
* hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
1939 /* Stop IRDA DMA Tx request if ongoing */
1940 if ( (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
1941 &&(HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
)) )
1943 hirda
->TxXferCount
= 0;
1944 IRDA_EndTxTransfer(hirda
);
1947 /* Stop IRDA DMA Rx request if ongoing */
1948 if ( (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
1949 &&(HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
)) )
1951 hirda
->RxXferCount
= 0;
1952 IRDA_EndRxTransfer(hirda
);
1955 hirda
->ErrorCode
|= HAL_IRDA_ERROR_DMA
;
1956 HAL_IRDA_ErrorCallback(hirda
);
1960 * @brief DMA IRDA communication abort callback, when initiated by HAL services on Error
1961 * (To be called at end of DMA Abort procedure following error occurrence).
1962 * @param hdma DMA handle.
1965 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
1967 IRDA_HandleTypeDef
* hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
1968 hirda
->RxXferCount
= 0;
1969 hirda
->TxXferCount
= 0;
1971 HAL_IRDA_ErrorCallback(hirda
);
1975 * @brief DMA IRDA Tx communication abort callback, when initiated by user
1976 * (To be called at end of DMA Tx Abort procedure following user abort request).
1977 * @note When this callback is executed, User Abort complete call back is called only if no
1978 * Abort still ongoing for Rx DMA Handle.
1979 * @param hdma DMA handle.
1982 static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
1984 IRDA_HandleTypeDef
* hirda
= (IRDA_HandleTypeDef
* )(hdma
->Parent
);
1986 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1988 /* Check if an Abort process is still ongoing */
1989 if(hirda
->hdmarx
!= NULL
)
1991 if(hirda
->hdmarx
->XferAbortCallback
!= NULL
)
1997 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
1998 hirda
->TxXferCount
= 0;
1999 hirda
->RxXferCount
= 0;
2001 /* Reset errorCode */
2002 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
2004 /* Clear the Error flags in the ICR register */
2005 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
2007 /* Restore hirda->gState and hirda->RxState to Ready */
2008 hirda
->gState
= HAL_IRDA_STATE_READY
;
2009 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2011 /* Call user Abort complete callback */
2012 HAL_IRDA_AbortCpltCallback(hirda
);
2017 * @brief DMA IRDA Rx communication abort callback, when initiated by user
2018 * (To be called at end of DMA Rx Abort procedure following user abort request).
2019 * @note When this callback is executed, User Abort complete call back is called only if no
2020 * Abort still ongoing for Tx DMA Handle.
2021 * @param hdma DMA handle.
2024 static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
2026 IRDA_HandleTypeDef
* hirda
= (IRDA_HandleTypeDef
* )(hdma
->Parent
);
2028 hirda
->hdmarx
->XferAbortCallback
= NULL
;
2030 /* Check if an Abort process is still ongoing */
2031 if(hirda
->hdmatx
!= NULL
)
2033 if(hirda
->hdmatx
->XferAbortCallback
!= NULL
)
2039 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2040 hirda
->TxXferCount
= 0;
2041 hirda
->RxXferCount
= 0;
2043 /* Reset errorCode */
2044 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
2046 /* Clear the Error flags in the ICR register */
2047 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
2049 /* Restore hirda->gState and hirda->RxState to Ready */
2050 hirda
->gState
= HAL_IRDA_STATE_READY
;
2051 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2053 /* Call user Abort complete callback */
2054 HAL_IRDA_AbortCpltCallback(hirda
);
2059 * @brief DMA IRDA Tx communication abort callback, when initiated by user by a call to
2060 * HAL_IRDA_AbortTransmit_IT API (Abort only Tx transfer)
2061 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2062 * and leads to user Tx Abort Complete callback execution).
2063 * @param hdma DMA handle.
2066 static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2068 IRDA_HandleTypeDef
* hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2070 hirda
->TxXferCount
= 0;
2072 /* Restore hirda->gState to Ready */
2073 hirda
->gState
= HAL_IRDA_STATE_READY
;
2075 /* Call user Abort complete callback */
2076 HAL_IRDA_AbortTransmitCpltCallback(hirda
);
2080 * @brief DMA IRDA Rx communication abort callback, when initiated by user by a call to
2081 * HAL_IRDA_AbortReceive_IT API (Abort only Rx transfer)
2082 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2083 * and leads to user Rx Abort Complete callback execution).
2084 * @param hdma DMA handle.
2087 static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2089 IRDA_HandleTypeDef
* hirda
= ( IRDA_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
2091 hirda
->RxXferCount
= 0;
2093 /* Clear the Error flags in the ICR register */
2094 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
2096 /* Restore hirda->RxState to Ready */
2097 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2099 /* Call user Abort complete callback */
2100 HAL_IRDA_AbortReceiveCpltCallback(hirda
);
2104 * @brief Send an amount of data in interrupt mode.
2105 * @note Function is called under interruption only, once
2106 * interruptions have been enabled by HAL_IRDA_Transmit_IT().
2107 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2108 * the configuration information for the specified IRDA module.
2109 * @retval HAL status
2111 static HAL_StatusTypeDef
IRDA_Transmit_IT(IRDA_HandleTypeDef
*hirda
)
2115 /* Check that a Tx process is ongoing */
2116 if(hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
2118 if(hirda
->TxXferCount
== 0)
2120 /* Disable the IRDA Transmit Data Register Empty Interrupt */
2121 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_TXEIE
);
2123 /* Enable the IRDA Transmit Complete Interrupt */
2124 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
2130 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
2132 tmp
= (uint16_t*) hirda
->pTxBuffPtr
;
2133 hirda
->Instance
->TDR
= (*tmp
& (uint16_t)0x01FF);
2134 hirda
->pTxBuffPtr
+= 2;
2138 hirda
->Instance
->TDR
= (uint8_t)(*hirda
->pTxBuffPtr
++ & (uint8_t)0xFF);
2140 hirda
->TxXferCount
--;
2152 * @brief Wrap up transmission in non-blocking mode.
2153 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2154 * the configuration information for the specified IRDA module.
2155 * @retval HAL status
2157 static HAL_StatusTypeDef
IRDA_EndTransmit_IT(IRDA_HandleTypeDef
*hirda
)
2159 /* Disable the IRDA Transmit Complete Interrupt */
2160 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
2162 /* Tx process is ended, restore hirda->gState to Ready */
2163 hirda
->gState
= HAL_IRDA_STATE_READY
;
2165 HAL_IRDA_TxCpltCallback(hirda
);
2171 * @brief Receive an amount of data in interrupt mode.
2172 * @note Function is called under interruption only, once
2173 * interruptions have been enabled by HAL_IRDA_Receive_IT()
2174 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2175 * the configuration information for the specified IRDA module.
2176 * @retval HAL status
2178 static HAL_StatusTypeDef
IRDA_Receive_IT(IRDA_HandleTypeDef
*hirda
)
2181 uint16_t uhMask
= hirda
->Mask
;
2184 /* Check that a Rx process is ongoing */
2185 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
2187 uhdata
= (uint16_t) READ_REG(hirda
->Instance
->RDR
);
2188 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
2190 tmp
= (uint16_t*) hirda
->pRxBuffPtr
;
2191 *tmp
= (uint16_t)(uhdata
& uhMask
);
2192 hirda
->pRxBuffPtr
+=2;
2196 *hirda
->pRxBuffPtr
++ = (uint8_t)(uhdata
& (uint8_t)uhMask
);
2199 if(--hirda
->RxXferCount
== 0)
2201 /* Disable the IRDA Parity Error Interrupt and RXNE interrupt */
2202 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
2204 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
2205 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
2207 /* Rx process is completed, restore hirda->RxState to Ready */
2208 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2210 HAL_IRDA_RxCpltCallback(hirda
);
2219 /* Clear RXNE interrupt flag */
2220 __HAL_IRDA_SEND_REQ(hirda
, IRDA_RXDATA_FLUSH_REQUEST
);
2230 #endif /* HAL_IRDA_MODULE_ENABLED */
2239 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/