2 ******************************************************************************
3 * @file stm32h7xx_hal_irda.c
4 * @author MCD Application Team
5 * @brief IRDA HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the IrDA (Infrared Data Association) Peripheral
9 * + Initialization and de-initialization functions
10 * + IO operation functions
11 * + Peripheral State and Errors functions
12 * + Peripheral Control functions
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 (eg. IRDA_HandleTypeDef hirda).
22 (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API
23 in setting the associated USART or UART in IRDA mode:
24 (++) Enable the USARTx/UARTx interface clock.
25 (++) USARTx/UARTx pins configuration:
26 (+++) Enable the clock for the USARTx/UARTx GPIOs.
27 (+++) Configure these USARTx/UARTx pins (TX as alternate function pull-up, RX as alternate function Input).
28 (++) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
29 and HAL_IRDA_Receive_IT() APIs):
30 (+++) Configure the USARTx/UARTx interrupt priority.
31 (+++) Enable the NVIC USARTx/UARTx IRQ handle.
32 (+++) The specific IRDA interrupts (Transmission complete interrupt,
33 RXNE interrupt and Error Interrupts) will be managed using the macros
34 __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
36 (++) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
37 and HAL_IRDA_Receive_DMA() APIs):
38 (+++) Declare a DMA handle structure for the Tx/Rx channel.
39 (+++) Enable the DMAx interface clock.
40 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
41 (+++) Configure the DMA Tx/Rx channel.
42 (+++) Associate the initialized DMA handle to the IRDA DMA Tx/Rx handle.
43 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
45 (#) Program the Baud Rate, Word Length and Parity and Mode(Receiver/Transmitter),
46 the normal or low power mode and the clock prescaler in the hirda handle Init structure.
48 (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
49 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
50 by calling the customized HAL_IRDA_MspInit() API.
52 -@@- The specific IRDA interrupts (Transmission complete interrupt,
53 RXNE interrupt and Error Interrupts) will be managed using the macros
54 __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
56 (#) Three operation modes are available within this driver :
58 *** Polling mode IO operation ***
59 =================================
61 (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
62 (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
64 *** Interrupt mode IO operation ***
65 ===================================
67 (+) Send an amount of data in non-blocking mode using HAL_IRDA_Transmit_IT()
68 (+) At transmission end of transfer HAL_IRDA_TxCpltCallback() is executed and user can
69 add his own code by customization of function pointer HAL_IRDA_TxCpltCallback()
70 (+) Receive an amount of data in non-blocking mode using HAL_IRDA_Receive_IT()
71 (+) At reception end of transfer HAL_IRDA_RxCpltCallback() is executed and user can
72 add his own code by customization of function pointer HAL_IRDA_RxCpltCallback()
73 (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
74 add his own code by customization of function pointer HAL_IRDA_ErrorCallback()
76 *** DMA mode IO operation ***
77 ==============================
79 (+) Send an amount of data in non-blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
80 (+) At transmission half of transfer HAL_IRDA_TxHalfCpltCallback() is executed and user can
81 add his own code by customization of function pointer HAL_IRDA_TxHalfCpltCallback()
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 half of transfer HAL_IRDA_RxHalfCpltCallback() is executed and user can
86 add his own code by customization of function pointer HAL_IRDA_RxHalfCpltCallback()
87 (+) At reception end of transfer HAL_IRDA_RxCpltCallback() is executed and user can
88 add his own code by customization of function pointer HAL_IRDA_RxCpltCallback()
89 (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
90 add his own code by customization of function pointer HAL_IRDA_ErrorCallback()
92 *** IRDA HAL driver macros list ***
93 ====================================
95 Below the list of most used macros in IRDA HAL driver.
97 (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
98 (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
99 (+) __HAL_IRDA_GET_FLAG : Check whether the specified IRDA flag is set or not
100 (+) __HAL_IRDA_CLEAR_FLAG : Clear the specified IRDA pending flag
101 (+) __HAL_IRDA_ENABLE_IT: Enable the specified IRDA interrupt
102 (+) __HAL_IRDA_DISABLE_IT: Disable the specified IRDA interrupt
103 (+) __HAL_IRDA_GET_IT_SOURCE: Check whether or not the specified IRDA interrupt is enabled
106 (@) You can refer to the IRDA HAL driver header file for more useful macros
108 ##### Callback registration #####
109 ==================================
112 The compilation define USE_HAL_IRDA_REGISTER_CALLBACKS when set to 1
113 allows the user to configure dynamically the driver callbacks.
116 Use Function @ref HAL_IRDA_RegisterCallback() to register a user callback.
117 Function @ref HAL_IRDA_RegisterCallback() allows to register following callbacks:
118 (+) TxHalfCpltCallback : Tx Half Complete Callback.
119 (+) TxCpltCallback : Tx Complete Callback.
120 (+) RxHalfCpltCallback : Rx Half Complete Callback.
121 (+) RxCpltCallback : Rx Complete Callback.
122 (+) ErrorCallback : Error Callback.
123 (+) AbortCpltCallback : Abort Complete Callback.
124 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
125 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
126 (+) MspInitCallback : IRDA MspInit.
127 (+) MspDeInitCallback : IRDA MspDeInit.
128 This function takes as parameters the HAL peripheral handle, the Callback ID
129 and a pointer to the user callback function.
132 Use function @ref HAL_IRDA_UnRegisterCallback() to reset a callback to the default
133 weak (surcharged) function.
134 @ref HAL_IRDA_UnRegisterCallback() takes as parameters the HAL peripheral handle,
136 This function allows to reset following callbacks:
137 (+) TxHalfCpltCallback : Tx Half Complete Callback.
138 (+) TxCpltCallback : Tx Complete Callback.
139 (+) RxHalfCpltCallback : Rx Half Complete Callback.
140 (+) RxCpltCallback : Rx Complete Callback.
141 (+) ErrorCallback : Error Callback.
142 (+) AbortCpltCallback : Abort Complete Callback.
143 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
144 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
145 (+) MspInitCallback : IRDA MspInit.
146 (+) MspDeInitCallback : IRDA MspDeInit.
149 By default, after the @ref HAL_IRDA_Init() and when the state is HAL_IRDA_STATE_RESET
150 all callbacks are set to the corresponding weak (surcharged) functions:
151 examples @ref HAL_IRDA_TxCpltCallback(), @ref HAL_IRDA_RxHalfCpltCallback().
152 Exception done for MspInit and MspDeInit functions that are respectively
153 reset to the legacy weak (surcharged) functions in the @ref HAL_IRDA_Init()
154 and @ref HAL_IRDA_DeInit() only when these callbacks are null (not registered beforehand).
155 If not, MspInit or MspDeInit are not null, the @ref HAL_IRDA_Init() and @ref HAL_IRDA_DeInit()
156 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
159 Callbacks can be registered/unregistered in HAL_IRDA_STATE_READY state only.
160 Exception done MspInit/MspDeInit that can be registered/unregistered
161 in HAL_IRDA_STATE_READY or HAL_IRDA_STATE_RESET state, thus registered (user)
162 MspInit/DeInit callbacks can be used during the Init/DeInit.
163 In that case first register the MspInit/MspDeInit user callbacks
164 using @ref HAL_IRDA_RegisterCallback() before calling @ref HAL_IRDA_DeInit()
165 or @ref HAL_IRDA_Init() function.
168 When The compilation define USE_HAL_IRDA_REGISTER_CALLBACKS is set to 0 or
169 not defined, the callback registration feature is not available
170 and weak (surcharged) callbacks are used.
173 ******************************************************************************
176 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
177 * All rights reserved.</center></h2>
179 * This software component is licensed by ST under BSD 3-Clause license,
180 * the "License"; You may not use this file except in compliance with the
181 * License. You may obtain a copy of the License at:
182 * opensource.org/licenses/BSD-3-Clause
184 ******************************************************************************
187 /* Includes ------------------------------------------------------------------*/
188 #include "stm32h7xx_hal.h"
190 /** @addtogroup STM32H7xx_HAL_Driver
194 /** @defgroup IRDA IRDA
195 * @brief HAL IRDA module driver
199 #ifdef HAL_IRDA_MODULE_ENABLED
201 /* Private typedef -----------------------------------------------------------*/
202 /* Private define ------------------------------------------------------------*/
203 /** @defgroup IRDA_Private_Constants IRDA Private Constants
206 #define IRDA_TEACK_REACK_TIMEOUT 1000U /*!< IRDA TX or RX enable acknowledge time-out value */
208 #define IRDA_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE \
209 | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)) /*!< UART or USART CR1 fields of parameters set by IRDA_SetConfig API */
211 #define USART_BRR_MIN 0x10U /*!< USART BRR minimum authorized value */
213 #define USART_BRR_MAX 0x0000FFFFU /*!< USART BRR maximum authorized value */
218 /* Private macros ------------------------------------------------------------*/
219 /** @defgroup IRDA_Private_Macros IRDA Private Macros
222 /** @brief BRR division operation to set BRR register in 16-bit oversampling mode.
223 * @param __PCLK__ IRDA clock source.
224 * @param __BAUD__ Baud rate set by the user.
225 * @param __PRESCALER__ IRDA clock prescaler value.
226 * @retval Division result
228 #define IRDA_DIV_SAMPLING16(__PCLK__, __BAUD__, __PRESCALER__) ((((__PCLK__)/IRDAPrescTable[(__PRESCALER__)])\
229 + ((__BAUD__)/2U)) / (__BAUD__))
234 /* Private variables ---------------------------------------------------------*/
235 /* Private function prototypes -----------------------------------------------*/
236 /** @addtogroup IRDA_Private_Functions
239 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
240 void IRDA_InitCallbacksToDefault(IRDA_HandleTypeDef
*hirda
);
241 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
242 static HAL_StatusTypeDef
IRDA_SetConfig(IRDA_HandleTypeDef
*hirda
);
243 static HAL_StatusTypeDef
IRDA_CheckIdleState(IRDA_HandleTypeDef
*hirda
);
244 static HAL_StatusTypeDef
IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef
*hirda
, uint32_t Flag
, FlagStatus Status
,
245 uint32_t Tickstart
, uint32_t Timeout
);
246 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef
*hirda
);
247 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef
*hirda
);
248 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
249 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef
*hdma
);
250 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
251 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef
*hdma
);
252 static void IRDA_DMAError(DMA_HandleTypeDef
*hdma
);
253 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
254 static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
);
255 static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
);
256 static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
257 static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
258 static void IRDA_Transmit_IT(IRDA_HandleTypeDef
*hirda
);
259 static void IRDA_EndTransmit_IT(IRDA_HandleTypeDef
*hirda
);
260 static void IRDA_Receive_IT(IRDA_HandleTypeDef
*hirda
);
265 /* Exported functions --------------------------------------------------------*/
267 /** @defgroup IRDA_Exported_Functions IRDA Exported Functions
271 /** @defgroup IRDA_Exported_Functions_Group1 Initialization and de-initialization functions
272 * @brief Initialization and Configuration functions
275 ==============================================================================
276 ##### Initialization and Configuration functions #####
277 ==============================================================================
279 This subsection provides a set of functions allowing to initialize the USARTx
280 in asynchronous IRDA mode.
281 (+) For the asynchronous mode only these parameters can be configured:
284 (++) Parity: If the parity is enabled, then the MSB bit of the data written
285 in the data register is transmitted but is changed by the parity bit.
287 (++) Prescaler setting
288 (++) Receiver/transmitter modes
291 The HAL_IRDA_Init() API follows the USART asynchronous configuration procedures
292 (details for the procedures are available in reference manual).
296 Depending on the frame length defined by the M1 and M0 bits (7-bit,
297 8-bit or 9-bit), the possible IRDA frame formats are listed in the
300 Table 1. IRDA frame format.
301 +-----------------------------------------------------------------------+
302 | M1 bit | M0 bit | PCE bit | IRDA frame |
303 |---------|---------|-----------|---------------------------------------|
304 | 0 | 0 | 0 | | SB | 8 bit data | STB | |
305 |---------|---------|-----------|---------------------------------------|
306 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
307 |---------|---------|-----------|---------------------------------------|
308 | 0 | 1 | 0 | | SB | 9 bit data | STB | |
309 |---------|---------|-----------|---------------------------------------|
310 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
311 |---------|---------|-----------|---------------------------------------|
312 | 1 | 0 | 0 | | SB | 7 bit data | STB | |
313 |---------|---------|-----------|---------------------------------------|
314 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
315 +-----------------------------------------------------------------------+
321 * @brief Initialize the IRDA mode according to the specified
322 * parameters in the IRDA_InitTypeDef and initialize the associated handle.
323 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
324 * the configuration information for the specified IRDA module.
327 HAL_StatusTypeDef
HAL_IRDA_Init(IRDA_HandleTypeDef
*hirda
)
329 /* Check the IRDA handle allocation */
335 /* Check the USART/UART associated to the IRDA handle */
336 assert_param(IS_IRDA_INSTANCE(hirda
->Instance
));
338 if (hirda
->gState
== HAL_IRDA_STATE_RESET
)
340 /* Allocate lock resource and initialize it */
341 hirda
->Lock
= HAL_UNLOCKED
;
343 #if USE_HAL_IRDA_REGISTER_CALLBACKS == 1
344 IRDA_InitCallbacksToDefault(hirda
);
346 if (hirda
->MspInitCallback
== NULL
)
348 hirda
->MspInitCallback
= HAL_IRDA_MspInit
;
351 /* Init the low level hardware */
352 hirda
->MspInitCallback(hirda
);
354 /* Init the low level hardware : GPIO, CLOCK */
355 HAL_IRDA_MspInit(hirda
);
356 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
359 hirda
->gState
= HAL_IRDA_STATE_BUSY
;
361 /* Disable the Peripheral to update the configuration registers */
362 __HAL_IRDA_DISABLE(hirda
);
364 /* Set the IRDA Communication parameters */
365 if (IRDA_SetConfig(hirda
) == HAL_ERROR
)
370 /* In IRDA mode, the following bits must be kept cleared:
371 - LINEN, STOP and CLKEN bits in the USART_CR2 register,
372 - SCEN and HDSEL bits in the USART_CR3 register.*/
373 CLEAR_BIT(hirda
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
| USART_CR2_STOP
));
374 CLEAR_BIT(hirda
->Instance
->CR3
, (USART_CR3_SCEN
| USART_CR3_HDSEL
));
376 /* set the UART/USART in IRDA mode */
377 hirda
->Instance
->CR3
|= USART_CR3_IREN
;
379 /* Enable the Peripheral */
380 __HAL_IRDA_ENABLE(hirda
);
382 /* TEACK and/or REACK to check before moving hirda->gState and hirda->RxState to Ready */
383 return (IRDA_CheckIdleState(hirda
));
387 * @brief DeInitialize the IRDA peripheral.
388 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
389 * the configuration information for the specified IRDA module.
392 HAL_StatusTypeDef
HAL_IRDA_DeInit(IRDA_HandleTypeDef
*hirda
)
394 /* Check the IRDA handle allocation */
400 /* Check the USART/UART associated to the IRDA handle */
401 assert_param(IS_IRDA_INSTANCE(hirda
->Instance
));
403 hirda
->gState
= HAL_IRDA_STATE_BUSY
;
405 /* DeInit the low level hardware */
406 #if USE_HAL_IRDA_REGISTER_CALLBACKS == 1
407 if (hirda
->MspDeInitCallback
== NULL
)
409 hirda
->MspDeInitCallback
= HAL_IRDA_MspDeInit
;
411 /* DeInit the low level hardware */
412 hirda
->MspDeInitCallback(hirda
);
414 HAL_IRDA_MspDeInit(hirda
);
415 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
416 /* Disable the Peripheral */
417 __HAL_IRDA_DISABLE(hirda
);
419 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
420 hirda
->gState
= HAL_IRDA_STATE_RESET
;
421 hirda
->RxState
= HAL_IRDA_STATE_RESET
;
430 * @brief Initialize the IRDA MSP.
431 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
432 * the configuration information for the specified IRDA module.
435 __weak
void HAL_IRDA_MspInit(IRDA_HandleTypeDef
*hirda
)
437 /* Prevent unused argument(s) compilation warning */
440 /* NOTE: This function should not be modified, when the callback is needed,
441 the HAL_IRDA_MspInit can be implemented in the user file
446 * @brief DeInitialize the IRDA MSP.
447 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
448 * the configuration information for the specified IRDA module.
451 __weak
void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef
*hirda
)
453 /* Prevent unused argument(s) compilation warning */
456 /* NOTE: This function should not be modified, when the callback is needed,
457 the HAL_IRDA_MspDeInit can be implemented in the user file
461 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
463 * @brief Register a User IRDA Callback
464 * To be used instead of the weak predefined callback
465 * @param hirda irda handle
466 * @param CallbackID ID of the callback to be registered
467 * This parameter can be one of the following values:
468 * @arg @ref HAL_IRDA_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
469 * @arg @ref HAL_IRDA_TX_COMPLETE_CB_ID Tx Complete Callback ID
470 * @arg @ref HAL_IRDA_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
471 * @arg @ref HAL_IRDA_RX_COMPLETE_CB_ID Rx Complete Callback ID
472 * @arg @ref HAL_IRDA_ERROR_CB_ID Error Callback ID
473 * @arg @ref HAL_IRDA_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
474 * @arg @ref HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
475 * @arg @ref HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
476 * @arg @ref HAL_IRDA_MSPINIT_CB_ID MspInit Callback ID
477 * @arg @ref HAL_IRDA_MSPDEINIT_CB_ID MspDeInit Callback ID
478 * @param pCallback pointer to the Callback function
481 HAL_StatusTypeDef
HAL_IRDA_RegisterCallback(IRDA_HandleTypeDef
*hirda
, HAL_IRDA_CallbackIDTypeDef CallbackID
,
482 pIRDA_CallbackTypeDef pCallback
)
484 HAL_StatusTypeDef status
= HAL_OK
;
486 if (pCallback
== NULL
)
488 /* Update the error code */
489 hirda
->ErrorCode
|= HAL_IRDA_ERROR_INVALID_CALLBACK
;
496 if (hirda
->gState
== HAL_IRDA_STATE_READY
)
500 case HAL_IRDA_TX_HALFCOMPLETE_CB_ID
:
501 hirda
->TxHalfCpltCallback
= pCallback
;
504 case HAL_IRDA_TX_COMPLETE_CB_ID
:
505 hirda
->TxCpltCallback
= pCallback
;
508 case HAL_IRDA_RX_HALFCOMPLETE_CB_ID
:
509 hirda
->RxHalfCpltCallback
= pCallback
;
512 case HAL_IRDA_RX_COMPLETE_CB_ID
:
513 hirda
->RxCpltCallback
= pCallback
;
516 case HAL_IRDA_ERROR_CB_ID
:
517 hirda
->ErrorCallback
= pCallback
;
520 case HAL_IRDA_ABORT_COMPLETE_CB_ID
:
521 hirda
->AbortCpltCallback
= pCallback
;
524 case HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID
:
525 hirda
->AbortTransmitCpltCallback
= pCallback
;
528 case HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID
:
529 hirda
->AbortReceiveCpltCallback
= pCallback
;
532 case HAL_IRDA_MSPINIT_CB_ID
:
533 hirda
->MspInitCallback
= pCallback
;
536 case HAL_IRDA_MSPDEINIT_CB_ID
:
537 hirda
->MspDeInitCallback
= pCallback
;
541 /* Update the error code */
542 hirda
->ErrorCode
|= HAL_IRDA_ERROR_INVALID_CALLBACK
;
544 /* Return error status */
549 else if (hirda
->gState
== HAL_IRDA_STATE_RESET
)
553 case HAL_IRDA_MSPINIT_CB_ID
:
554 hirda
->MspInitCallback
= pCallback
;
557 case HAL_IRDA_MSPDEINIT_CB_ID
:
558 hirda
->MspDeInitCallback
= pCallback
;
562 /* Update the error code */
563 hirda
->ErrorCode
|= HAL_IRDA_ERROR_INVALID_CALLBACK
;
565 /* Return error status */
572 /* Update the error code */
573 hirda
->ErrorCode
|= HAL_IRDA_ERROR_INVALID_CALLBACK
;
575 /* Return error status */
586 * @brief Unregister an IRDA callback
587 * IRDA callback is redirected to the weak predefined callback
588 * @param hirda irda handle
589 * @param CallbackID ID of the callback to be unregistered
590 * This parameter can be one of the following values:
591 * @arg @ref HAL_IRDA_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
592 * @arg @ref HAL_IRDA_TX_COMPLETE_CB_ID Tx Complete Callback ID
593 * @arg @ref HAL_IRDA_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
594 * @arg @ref HAL_IRDA_RX_COMPLETE_CB_ID Rx Complete Callback ID
595 * @arg @ref HAL_IRDA_ERROR_CB_ID Error Callback ID
596 * @arg @ref HAL_IRDA_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
597 * @arg @ref HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
598 * @arg @ref HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
599 * @arg @ref HAL_IRDA_MSPINIT_CB_ID MspInit Callback ID
600 * @arg @ref HAL_IRDA_MSPDEINIT_CB_ID MspDeInit Callback ID
603 HAL_StatusTypeDef
HAL_IRDA_UnRegisterCallback(IRDA_HandleTypeDef
*hirda
, HAL_IRDA_CallbackIDTypeDef CallbackID
)
605 HAL_StatusTypeDef status
= HAL_OK
;
610 if (HAL_IRDA_STATE_READY
== hirda
->gState
)
614 case HAL_IRDA_TX_HALFCOMPLETE_CB_ID
:
615 hirda
->TxHalfCpltCallback
= HAL_IRDA_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
618 case HAL_IRDA_TX_COMPLETE_CB_ID
:
619 hirda
->TxCpltCallback
= HAL_IRDA_TxCpltCallback
; /* Legacy weak TxCpltCallback */
622 case HAL_IRDA_RX_HALFCOMPLETE_CB_ID
:
623 hirda
->RxHalfCpltCallback
= HAL_IRDA_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
626 case HAL_IRDA_RX_COMPLETE_CB_ID
:
627 hirda
->RxCpltCallback
= HAL_IRDA_RxCpltCallback
; /* Legacy weak RxCpltCallback */
630 case HAL_IRDA_ERROR_CB_ID
:
631 hirda
->ErrorCallback
= HAL_IRDA_ErrorCallback
; /* Legacy weak ErrorCallback */
634 case HAL_IRDA_ABORT_COMPLETE_CB_ID
:
635 hirda
->AbortCpltCallback
= HAL_IRDA_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
638 case HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID
:
639 hirda
->AbortTransmitCpltCallback
= HAL_IRDA_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
642 case HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID
:
643 hirda
->AbortReceiveCpltCallback
= HAL_IRDA_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
646 case HAL_IRDA_MSPINIT_CB_ID
:
647 hirda
->MspInitCallback
= HAL_IRDA_MspInit
; /* Legacy weak MspInitCallback */
650 case HAL_IRDA_MSPDEINIT_CB_ID
:
651 hirda
->MspDeInitCallback
= HAL_IRDA_MspDeInit
; /* Legacy weak MspDeInitCallback */
655 /* Update the error code */
656 hirda
->ErrorCode
|= HAL_IRDA_ERROR_INVALID_CALLBACK
;
658 /* Return error status */
663 else if (HAL_IRDA_STATE_RESET
== hirda
->gState
)
667 case HAL_IRDA_MSPINIT_CB_ID
:
668 hirda
->MspInitCallback
= HAL_IRDA_MspInit
;
671 case HAL_IRDA_MSPDEINIT_CB_ID
:
672 hirda
->MspDeInitCallback
= HAL_IRDA_MspDeInit
;
676 /* Update the error code */
677 hirda
->ErrorCode
|= HAL_IRDA_ERROR_INVALID_CALLBACK
;
679 /* Return error status */
686 /* Update the error code */
687 hirda
->ErrorCode
|= HAL_IRDA_ERROR_INVALID_CALLBACK
;
689 /* Return error status */
698 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
704 /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
705 * @brief IRDA Transmit and Receive functions
708 ===============================================================================
709 ##### IO operation functions #####
710 ===============================================================================
712 This subsection provides a set of functions allowing to manage the IRDA data transfers.
715 IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
716 on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
717 is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
718 While receiving data, transmission should be avoided as the data to be transmitted
722 (#) There are two modes of transfer:
723 (++) Blocking mode: the communication is performed in polling mode.
724 The HAL status of all data processing is returned by the same function
725 after finishing transfer.
726 (++) Non-Blocking mode: the communication is performed using Interrupts
727 or DMA, these API's return the HAL status.
728 The end of the data processing will be indicated through the
729 dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
731 The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
732 will be executed respectively at the end of the Transmit or Receive process
733 The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
735 (#) Blocking mode APIs are :
736 (++) HAL_IRDA_Transmit()
737 (++) HAL_IRDA_Receive()
739 (#) Non Blocking mode APIs with Interrupt are :
740 (++) HAL_IRDA_Transmit_IT()
741 (++) HAL_IRDA_Receive_IT()
742 (++) HAL_IRDA_IRQHandler()
744 (#) Non Blocking mode functions with DMA are :
745 (++) HAL_IRDA_Transmit_DMA()
746 (++) HAL_IRDA_Receive_DMA()
747 (++) HAL_IRDA_DMAPause()
748 (++) HAL_IRDA_DMAResume()
749 (++) HAL_IRDA_DMAStop()
751 (#) A set of Transfer Complete Callbacks are provided in Non Blocking mode:
752 (++) HAL_IRDA_TxHalfCpltCallback()
753 (++) HAL_IRDA_TxCpltCallback()
754 (++) HAL_IRDA_RxHalfCpltCallback()
755 (++) HAL_IRDA_RxCpltCallback()
756 (++) HAL_IRDA_ErrorCallback()
758 (#) Non-Blocking mode transfers could be aborted using Abort API's :
759 (++) HAL_IRDA_Abort()
760 (++) HAL_IRDA_AbortTransmit()
761 (++) HAL_IRDA_AbortReceive()
762 (++) HAL_IRDA_Abort_IT()
763 (++) HAL_IRDA_AbortTransmit_IT()
764 (++) HAL_IRDA_AbortReceive_IT()
766 (#) For Abort services based on interrupts (HAL_IRDA_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
767 (++) HAL_IRDA_AbortCpltCallback()
768 (++) HAL_IRDA_AbortTransmitCpltCallback()
769 (++) HAL_IRDA_AbortReceiveCpltCallback()
771 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
772 Errors are handled as follows :
773 (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
774 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
775 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
776 and HAL_IRDA_ErrorCallback() user callback is executed. Transfer is kept ongoing on IRDA side.
777 If user wants to abort it, Abort services should be called by user.
778 (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
779 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
780 Error code is set to allow user to identify error type, and HAL_IRDA_ErrorCallback() user callback is executed.
787 * @brief Send an amount of data in blocking mode.
788 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
789 * the sent data is handled as a set of u16. In this case, Size must reflect the number
790 * of u16 available through pData.
791 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
792 * the configuration information for the specified IRDA module.
793 * @param pData Pointer to data buffer (u8 or u16 data elements).
794 * @param Size Amount of data elements (u8 or u16) to be sent.
795 * @param Timeout Specify timeout value.
798 HAL_StatusTypeDef
HAL_IRDA_Transmit(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
801 uint16_t *pdata16bits
;
804 /* Check that a Tx process is not already ongoing */
805 if (hirda
->gState
== HAL_IRDA_STATE_READY
)
807 if ((pData
== NULL
) || (Size
== 0U))
815 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
816 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
818 /* Init tickstart for timeout managment*/
819 tickstart
= HAL_GetTick();
821 hirda
->TxXferSize
= Size
;
822 hirda
->TxXferCount
= Size
;
824 /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
825 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
828 pdata16bits
= (uint16_t *) pData
; /* Derogation R.11.3 */
836 while (hirda
->TxXferCount
> 0U)
838 hirda
->TxXferCount
--;
840 if (IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
844 if (pdata8bits
== NULL
)
846 hirda
->Instance
->TDR
= (uint16_t)(*pdata16bits
& 0x01FFU
);
851 hirda
->Instance
->TDR
= (uint8_t)(*pdata8bits
& 0xFFU
);
856 if (IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_TC
, RESET
, tickstart
, Timeout
) != HAL_OK
)
861 /* At end of Tx process, restore hirda->gState to Ready */
862 hirda
->gState
= HAL_IRDA_STATE_READY
;
864 /* Process Unlocked */
876 * @brief Receive an amount of data in blocking mode.
877 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
878 * the received data is handled as a set of u16. In this case, Size must reflect the number
879 * of u16 available through pData.
880 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
881 * the configuration information for the specified IRDA module.
882 * @param pData Pointer to data buffer (u8 or u16 data elements).
883 * @param Size Amount of data elements (u8 or u16) to be received.
884 * @param Timeout Specify timeout value.
887 HAL_StatusTypeDef
HAL_IRDA_Receive(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
890 uint16_t *pdata16bits
;
894 /* Check that a Rx process is not already ongoing */
895 if (hirda
->RxState
== HAL_IRDA_STATE_READY
)
897 if ((pData
== NULL
) || (Size
== 0U))
905 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
906 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
908 /* Init tickstart for timeout managment*/
909 tickstart
= HAL_GetTick();
911 hirda
->RxXferSize
= Size
;
912 hirda
->RxXferCount
= Size
;
914 /* Computation of the mask to apply to RDR register
915 of the UART associated to the IRDA */
916 IRDA_MASK_COMPUTATION(hirda
);
917 uhMask
= hirda
->Mask
;
919 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
920 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
923 pdata16bits
= (uint16_t *) pData
; /* Derogation R.11.3 */
931 /* Check data remaining to be received */
932 while (hirda
->RxXferCount
> 0U)
934 hirda
->RxXferCount
--;
936 if (IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
940 if (pdata8bits
== NULL
)
942 *pdata16bits
= (uint16_t)(hirda
->Instance
->RDR
& uhMask
);
947 *pdata8bits
= (uint8_t)(hirda
->Instance
->RDR
& (uint8_t)uhMask
);
952 /* At end of Rx process, restore hirda->RxState to Ready */
953 hirda
->RxState
= HAL_IRDA_STATE_READY
;
955 /* Process Unlocked */
967 * @brief Send an amount of data in interrupt mode.
968 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
969 * the sent data is handled as a set of u16. In this case, Size must reflect the number
970 * of u16 available through pData.
971 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
972 * the configuration information for the specified IRDA module.
973 * @param pData Pointer to data buffer (u8 or u16 data elements).
974 * @param Size Amount of data elements (u8 or u16) to be sent.
977 HAL_StatusTypeDef
HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
979 /* Check that a Tx process is not already ongoing */
980 if (hirda
->gState
== HAL_IRDA_STATE_READY
)
982 if ((pData
== NULL
) || (Size
== 0U))
990 hirda
->pTxBuffPtr
= pData
;
991 hirda
->TxXferSize
= Size
;
992 hirda
->TxXferCount
= Size
;
994 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
995 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
997 /* Process Unlocked */
1000 /* Enable the IRDA Transmit Data Register Empty Interrupt */
1001 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
1012 * @brief Receive an amount of data in interrupt mode.
1013 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1014 * the received data is handled as a set of u16. In this case, Size must reflect the number
1015 * of u16 available through pData.
1016 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1017 * the configuration information for the specified IRDA module.
1018 * @param pData Pointer to data buffer (u8 or u16 data elements).
1019 * @param Size Amount of data elements (u8 or u16) to be received.
1020 * @retval HAL status
1022 HAL_StatusTypeDef
HAL_IRDA_Receive_IT(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
1024 /* Check that a Rx process is not already ongoing */
1025 if (hirda
->RxState
== HAL_IRDA_STATE_READY
)
1027 if ((pData
== NULL
) || (Size
== 0U))
1032 /* Process Locked */
1035 hirda
->pRxBuffPtr
= pData
;
1036 hirda
->RxXferSize
= Size
;
1037 hirda
->RxXferCount
= Size
;
1039 /* Computation of the mask to apply to the RDR register
1040 of the UART associated to the IRDA */
1041 IRDA_MASK_COMPUTATION(hirda
);
1043 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1044 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
1046 /* Process Unlocked */
1047 __HAL_UNLOCK(hirda
);
1049 /* Enable the IRDA Parity Error and Data Register not empty Interrupts */
1050 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE_RXFNEIE
);
1052 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1053 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1064 * @brief Send an amount of data in DMA mode.
1065 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1066 * the sent data is handled as a set of u16. In this case, Size must reflect the number
1067 * of u16 available through pData.
1068 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1069 * the configuration information for the specified IRDA module.
1070 * @param pData pointer to data buffer (u8 or u16 data elements).
1071 * @param Size Amount of data elements (u8 or u16) to be sent.
1072 * @retval HAL status
1074 HAL_StatusTypeDef
HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
1076 /* Check that a Tx process is not already ongoing */
1077 if (hirda
->gState
== HAL_IRDA_STATE_READY
)
1079 if ((pData
== NULL
) || (Size
== 0U))
1084 /* Process Locked */
1087 hirda
->pTxBuffPtr
= pData
;
1088 hirda
->TxXferSize
= Size
;
1089 hirda
->TxXferCount
= Size
;
1091 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1092 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
1094 /* Set the IRDA DMA transfer complete callback */
1095 hirda
->hdmatx
->XferCpltCallback
= IRDA_DMATransmitCplt
;
1097 /* Set the IRDA DMA half transfer complete callback */
1098 hirda
->hdmatx
->XferHalfCpltCallback
= IRDA_DMATransmitHalfCplt
;
1100 /* Set the DMA error callback */
1101 hirda
->hdmatx
->XferErrorCallback
= IRDA_DMAError
;
1103 /* Set the DMA abort callback */
1104 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1106 /* Enable the IRDA transmit DMA channel */
1107 if (HAL_DMA_Start_IT(hirda
->hdmatx
, (uint32_t)hirda
->pTxBuffPtr
, (uint32_t)&hirda
->Instance
->TDR
, Size
) == HAL_OK
)
1109 /* Clear the TC flag in the ICR register */
1110 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_TCF
);
1112 /* Process Unlocked */
1113 __HAL_UNLOCK(hirda
);
1115 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1116 in the USART CR3 register */
1117 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1123 /* Set error code to DMA */
1124 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1126 /* Process Unlocked */
1127 __HAL_UNLOCK(hirda
);
1129 /* Restore hirda->gState to ready */
1130 hirda
->gState
= HAL_IRDA_STATE_READY
;
1142 * @brief Receive an amount of data in DMA mode.
1143 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1144 * the received data is handled as a set of u16. In this case, Size must reflect the number
1145 * of u16 available through pData.
1146 * @note When the IRDA parity is enabled (PCE = 1), the received data contains
1147 * the parity bit (MSB position).
1148 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1149 * the configuration information for the specified IRDA module.
1150 * @param pData Pointer to data buffer (u8 or u16 data elements).
1151 * @param Size Amount of data elements (u8 or u16) to be received.
1152 * @retval HAL status
1154 HAL_StatusTypeDef
HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
1156 /* Check that a Rx process is not already ongoing */
1157 if (hirda
->RxState
== HAL_IRDA_STATE_READY
)
1159 if ((pData
== NULL
) || (Size
== 0U))
1164 /* Process Locked */
1167 hirda
->pRxBuffPtr
= pData
;
1168 hirda
->RxXferSize
= Size
;
1170 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1171 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
1173 /* Set the IRDA DMA transfer complete callback */
1174 hirda
->hdmarx
->XferCpltCallback
= IRDA_DMAReceiveCplt
;
1176 /* Set the IRDA DMA half transfer complete callback */
1177 hirda
->hdmarx
->XferHalfCpltCallback
= IRDA_DMAReceiveHalfCplt
;
1179 /* Set the DMA error callback */
1180 hirda
->hdmarx
->XferErrorCallback
= IRDA_DMAError
;
1182 /* Set the DMA abort callback */
1183 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1185 /* Enable the DMA channel */
1186 if (HAL_DMA_Start_IT(hirda
->hdmarx
, (uint32_t)&hirda
->Instance
->RDR
, (uint32_t)hirda
->pRxBuffPtr
, Size
) == HAL_OK
)
1188 /* Process Unlocked */
1189 __HAL_UNLOCK(hirda
);
1191 /* Enable the UART Parity Error Interrupt */
1192 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
1194 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1195 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1197 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1198 in the USART CR3 register */
1199 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1205 /* Set error code to DMA */
1206 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1208 /* Process Unlocked */
1209 __HAL_UNLOCK(hirda
);
1211 /* Restore hirda->RxState to ready */
1212 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1225 * @brief Pause the DMA Transfer.
1226 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1227 * the configuration information for the specified IRDA module.
1228 * @retval HAL status
1230 HAL_StatusTypeDef
HAL_IRDA_DMAPause(IRDA_HandleTypeDef
*hirda
)
1232 /* Process Locked */
1235 if (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
1237 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1239 /* Disable the IRDA DMA Tx request */
1240 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1243 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
1245 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1247 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1248 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
1249 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1251 /* Disable the IRDA DMA Rx request */
1252 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1256 /* Process Unlocked */
1257 __HAL_UNLOCK(hirda
);
1263 * @brief Resume the DMA Transfer.
1264 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1265 * the configuration information for the specified UART module.
1266 * @retval HAL status
1268 HAL_StatusTypeDef
HAL_IRDA_DMAResume(IRDA_HandleTypeDef
*hirda
)
1270 /* Process Locked */
1273 if (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
1275 /* Enable the IRDA DMA Tx request */
1276 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1278 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
1280 /* Clear the Overrun flag before resuming the Rx transfer*/
1281 __HAL_IRDA_CLEAR_OREFLAG(hirda
);
1283 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1284 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
1285 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1287 /* Enable the IRDA DMA Rx request */
1288 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1291 /* Process Unlocked */
1292 __HAL_UNLOCK(hirda
);
1298 * @brief Stop the DMA Transfer.
1299 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1300 * the configuration information for the specified UART module.
1301 * @retval HAL status
1303 HAL_StatusTypeDef
HAL_IRDA_DMAStop(IRDA_HandleTypeDef
*hirda
)
1305 /* The Lock is not implemented on this API to allow the user application
1306 to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback() /
1307 HAL_IRDA_TxHalfCpltCallback / HAL_IRDA_RxHalfCpltCallback:
1308 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1309 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1310 the stream and the corresponding call back is executed. */
1312 /* Stop IRDA DMA Tx request if ongoing */
1313 if (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
1315 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1317 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1319 /* Abort the IRDA DMA Tx channel */
1320 if (hirda
->hdmatx
!= NULL
)
1322 if (HAL_DMA_Abort(hirda
->hdmatx
) != HAL_OK
)
1324 if (HAL_DMA_GetError(hirda
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1326 /* Set error code to DMA */
1327 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1334 IRDA_EndTxTransfer(hirda
);
1338 /* Stop IRDA DMA Rx request if ongoing */
1339 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
1341 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1343 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1345 /* Abort the IRDA DMA Rx channel */
1346 if (hirda
->hdmarx
!= NULL
)
1348 if (HAL_DMA_Abort(hirda
->hdmarx
) != HAL_OK
)
1350 if (HAL_DMA_GetError(hirda
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1352 /* Set error code to DMA */
1353 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1360 IRDA_EndRxTransfer(hirda
);
1368 * @brief Abort ongoing transfers (blocking mode).
1369 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1370 * the configuration information for the specified UART module.
1371 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1372 * This procedure performs following operations :
1373 * - Disable IRDA Interrupts (Tx and Rx)
1374 * - Disable the DMA transfer in the peripheral register (if enabled)
1375 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1376 * - Set handle State to READY
1377 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1378 * @retval HAL status
1380 HAL_StatusTypeDef
HAL_IRDA_Abort(IRDA_HandleTypeDef
*hirda
)
1382 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1383 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1384 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1386 /* Disable the IRDA DMA Tx request if enabled */
1387 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1389 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1391 /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
1392 if (hirda
->hdmatx
!= NULL
)
1394 /* Set the IRDA DMA Abort callback to Null.
1395 No call back execution at end of DMA abort procedure */
1396 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1398 if (HAL_DMA_Abort(hirda
->hdmatx
) != HAL_OK
)
1400 if (HAL_DMA_GetError(hirda
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1402 /* Set error code to DMA */
1403 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1411 /* Disable the IRDA DMA Rx request if enabled */
1412 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1414 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1416 /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
1417 if (hirda
->hdmarx
!= NULL
)
1419 /* Set the IRDA DMA Abort callback to Null.
1420 No call back execution at end of DMA abort procedure */
1421 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1423 if (HAL_DMA_Abort(hirda
->hdmarx
) != HAL_OK
)
1425 if (HAL_DMA_GetError(hirda
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1427 /* Set error code to DMA */
1428 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1436 /* Reset Tx and Rx transfer counters */
1437 hirda
->TxXferCount
= 0U;
1438 hirda
->RxXferCount
= 0U;
1440 /* Clear the Error flags in the ICR register */
1441 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1443 /* Restore hirda->gState and hirda->RxState to Ready */
1444 hirda
->gState
= HAL_IRDA_STATE_READY
;
1445 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1447 /* Reset Handle ErrorCode to No Error */
1448 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1454 * @brief Abort ongoing Transmit transfer (blocking mode).
1455 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1456 * the configuration information for the specified UART module.
1457 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1458 * This procedure performs following operations :
1459 * - Disable IRDA Interrupts (Tx)
1460 * - Disable the DMA transfer in the peripheral register (if enabled)
1461 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1462 * - Set handle State to READY
1463 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1464 * @retval HAL status
1466 HAL_StatusTypeDef
HAL_IRDA_AbortTransmit(IRDA_HandleTypeDef
*hirda
)
1468 /* Disable TXEIE and TCIE interrupts */
1469 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1471 /* Disable the IRDA DMA Tx request if enabled */
1472 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1474 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1476 /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
1477 if (hirda
->hdmatx
!= NULL
)
1479 /* Set the IRDA DMA Abort callback to Null.
1480 No call back execution at end of DMA abort procedure */
1481 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1483 if (HAL_DMA_Abort(hirda
->hdmatx
) != HAL_OK
)
1485 if (HAL_DMA_GetError(hirda
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1487 /* Set error code to DMA */
1488 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1496 /* Reset Tx transfer counter */
1497 hirda
->TxXferCount
= 0U;
1499 /* Restore hirda->gState to Ready */
1500 hirda
->gState
= HAL_IRDA_STATE_READY
;
1506 * @brief Abort ongoing Receive transfer (blocking mode).
1507 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1508 * the configuration information for the specified UART module.
1509 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1510 * This procedure performs following operations :
1511 * - Disable IRDA Interrupts (Rx)
1512 * - Disable the DMA transfer in the peripheral register (if enabled)
1513 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1514 * - Set handle State to READY
1515 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1516 * @retval HAL status
1518 HAL_StatusTypeDef
HAL_IRDA_AbortReceive(IRDA_HandleTypeDef
*hirda
)
1520 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1521 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
1522 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1524 /* Disable the IRDA DMA Rx request if enabled */
1525 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1527 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1529 /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
1530 if (hirda
->hdmarx
!= NULL
)
1532 /* Set the IRDA DMA Abort callback to Null.
1533 No call back execution at end of DMA abort procedure */
1534 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1536 if (HAL_DMA_Abort(hirda
->hdmarx
) != HAL_OK
)
1538 if (HAL_DMA_GetError(hirda
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1540 /* Set error code to DMA */
1541 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1549 /* Reset Rx transfer counter */
1550 hirda
->RxXferCount
= 0U;
1552 /* Clear the Error flags in the ICR register */
1553 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1555 /* Restore hirda->RxState to Ready */
1556 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1562 * @brief Abort ongoing transfers (Interrupt mode).
1563 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1564 * the configuration information for the specified UART module.
1565 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1566 * This procedure performs following operations :
1567 * - Disable IRDA Interrupts (Tx and Rx)
1568 * - Disable the DMA transfer in the peripheral register (if enabled)
1569 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1570 * - Set handle State to READY
1571 * - At abort completion, call user abort complete callback
1572 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1573 * considered as completed only when user abort complete callback is executed (not when exiting function).
1574 * @retval HAL status
1576 HAL_StatusTypeDef
HAL_IRDA_Abort_IT(IRDA_HandleTypeDef
*hirda
)
1578 uint32_t abortcplt
= 1U;
1580 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1581 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1582 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1584 /* If DMA Tx and/or DMA Rx Handles are associated to IRDA Handle, DMA Abort complete callbacks should be initialised
1585 before any call to DMA Abort functions */
1586 /* DMA Tx Handle is valid */
1587 if (hirda
->hdmatx
!= NULL
)
1589 /* Set DMA Abort Complete callback if IRDA DMA Tx request if enabled.
1590 Otherwise, set it to NULL */
1591 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1593 hirda
->hdmatx
->XferAbortCallback
= IRDA_DMATxAbortCallback
;
1597 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1600 /* DMA Rx Handle is valid */
1601 if (hirda
->hdmarx
!= NULL
)
1603 /* Set DMA Abort Complete callback if IRDA DMA Rx request if enabled.
1604 Otherwise, set it to NULL */
1605 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1607 hirda
->hdmarx
->XferAbortCallback
= IRDA_DMARxAbortCallback
;
1611 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1615 /* Disable the IRDA DMA Tx request if enabled */
1616 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1618 /* Disable DMA Tx at UART level */
1619 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1621 /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
1622 if (hirda
->hdmatx
!= NULL
)
1624 /* IRDA Tx DMA Abort callback has already been initialised :
1625 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1628 if (HAL_DMA_Abort_IT(hirda
->hdmatx
) != HAL_OK
)
1630 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1639 /* Disable the IRDA DMA Rx request if enabled */
1640 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1642 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1644 /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
1645 if (hirda
->hdmarx
!= NULL
)
1647 /* IRDA Rx DMA Abort callback has already been initialised :
1648 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1651 if (HAL_DMA_Abort_IT(hirda
->hdmarx
) != HAL_OK
)
1653 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1663 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1664 if (abortcplt
== 1U)
1666 /* Reset Tx and Rx transfer counters */
1667 hirda
->TxXferCount
= 0U;
1668 hirda
->RxXferCount
= 0U;
1670 /* Reset errorCode */
1671 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1673 /* Clear the Error flags in the ICR register */
1674 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1676 /* Restore hirda->gState and hirda->RxState to Ready */
1677 hirda
->gState
= HAL_IRDA_STATE_READY
;
1678 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1680 /* As no DMA to be aborted, call directly user Abort complete callback */
1681 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1682 /* Call registered Abort complete callback */
1683 hirda
->AbortCpltCallback(hirda
);
1685 /* Call legacy weak Abort complete callback */
1686 HAL_IRDA_AbortCpltCallback(hirda
);
1687 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1694 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1695 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1696 * the configuration information for the specified UART module.
1697 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1698 * This procedure performs following operations :
1699 * - Disable IRDA Interrupts (Tx)
1700 * - Disable the DMA transfer in the peripheral register (if enabled)
1701 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1702 * - Set handle State to READY
1703 * - At abort completion, call user abort complete callback
1704 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1705 * considered as completed only when user abort complete callback is executed (not when exiting function).
1706 * @retval HAL status
1708 HAL_StatusTypeDef
HAL_IRDA_AbortTransmit_IT(IRDA_HandleTypeDef
*hirda
)
1710 /* Disable TXEIE and TCIE interrupts */
1711 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1713 /* Disable the IRDA DMA Tx request if enabled */
1714 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1716 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1718 /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
1719 if (hirda
->hdmatx
!= NULL
)
1721 /* Set the IRDA DMA Abort callback :
1722 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1723 hirda
->hdmatx
->XferAbortCallback
= IRDA_DMATxOnlyAbortCallback
;
1726 if (HAL_DMA_Abort_IT(hirda
->hdmatx
) != HAL_OK
)
1728 /* Call Directly hirda->hdmatx->XferAbortCallback function in case of error */
1729 hirda
->hdmatx
->XferAbortCallback(hirda
->hdmatx
);
1734 /* Reset Tx transfer counter */
1735 hirda
->TxXferCount
= 0U;
1737 /* Restore hirda->gState to Ready */
1738 hirda
->gState
= HAL_IRDA_STATE_READY
;
1740 /* As no DMA to be aborted, call directly user Abort complete callback */
1741 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1742 /* Call registered Abort Transmit Complete Callback */
1743 hirda
->AbortTransmitCpltCallback(hirda
);
1745 /* Call legacy weak Abort Transmit Complete Callback */
1746 HAL_IRDA_AbortTransmitCpltCallback(hirda
);
1747 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1752 /* Reset Tx transfer counter */
1753 hirda
->TxXferCount
= 0U;
1755 /* Restore hirda->gState to Ready */
1756 hirda
->gState
= HAL_IRDA_STATE_READY
;
1758 /* As no DMA to be aborted, call directly user Abort complete callback */
1759 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1760 /* Call registered Abort Transmit Complete Callback */
1761 hirda
->AbortTransmitCpltCallback(hirda
);
1763 /* Call legacy weak Abort Transmit Complete Callback */
1764 HAL_IRDA_AbortTransmitCpltCallback(hirda
);
1765 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1772 * @brief Abort ongoing Receive transfer (Interrupt mode).
1773 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1774 * the configuration information for the specified UART module.
1775 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1776 * This procedure performs following operations :
1777 * - Disable IRDA Interrupts (Rx)
1778 * - Disable the DMA transfer in the peripheral register (if enabled)
1779 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1780 * - Set handle State to READY
1781 * - At abort completion, call user abort complete callback
1782 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1783 * considered as completed only when user abort complete callback is executed (not when exiting function).
1784 * @retval HAL status
1786 HAL_StatusTypeDef
HAL_IRDA_AbortReceive_IT(IRDA_HandleTypeDef
*hirda
)
1788 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1789 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
1790 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1792 /* Disable the IRDA DMA Rx request if enabled */
1793 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1795 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1797 /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
1798 if (hirda
->hdmarx
!= NULL
)
1800 /* Set the IRDA DMA Abort callback :
1801 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1802 hirda
->hdmarx
->XferAbortCallback
= IRDA_DMARxOnlyAbortCallback
;
1805 if (HAL_DMA_Abort_IT(hirda
->hdmarx
) != HAL_OK
)
1807 /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
1808 hirda
->hdmarx
->XferAbortCallback(hirda
->hdmarx
);
1813 /* Reset Rx transfer counter */
1814 hirda
->RxXferCount
= 0U;
1816 /* Clear the Error flags in the ICR register */
1817 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1819 /* Restore hirda->RxState to Ready */
1820 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1822 /* As no DMA to be aborted, call directly user Abort complete callback */
1823 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1824 /* Call registered Abort Receive Complete Callback */
1825 hirda
->AbortReceiveCpltCallback(hirda
);
1827 /* Call legacy weak Abort Receive Complete Callback */
1828 HAL_IRDA_AbortReceiveCpltCallback(hirda
);
1829 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1834 /* Reset Rx transfer counter */
1835 hirda
->RxXferCount
= 0U;
1837 /* Clear the Error flags in the ICR register */
1838 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1840 /* Restore hirda->RxState to Ready */
1841 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1843 /* As no DMA to be aborted, call directly user Abort complete callback */
1844 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1845 /* Call registered Abort Receive Complete Callback */
1846 hirda
->AbortReceiveCpltCallback(hirda
);
1848 /* Call legacy weak Abort Receive Complete Callback */
1849 HAL_IRDA_AbortReceiveCpltCallback(hirda
);
1850 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1857 * @brief Handle IRDA interrupt request.
1858 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1859 * the configuration information for the specified IRDA module.
1862 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef
*hirda
)
1864 uint32_t isrflags
= READ_REG(hirda
->Instance
->ISR
);
1865 uint32_t cr1its
= READ_REG(hirda
->Instance
->CR1
);
1867 uint32_t errorflags
;
1870 /* If no error occurs */
1871 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
));
1872 if (errorflags
== 0U)
1874 /* IRDA in mode Receiver ---------------------------------------------------*/
1875 if (((isrflags
& USART_ISR_RXNE_RXFNE
) != 0U) && ((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U))
1877 IRDA_Receive_IT(hirda
);
1882 /* If some errors occur */
1883 cr3its
= READ_REG(hirda
->Instance
->CR3
);
1884 if ((errorflags
!= 0U)
1885 && (((cr3its
& USART_CR3_EIE
) != 0U)
1886 || ((cr1its
& (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
)) != 0U)))
1888 /* IRDA parity error interrupt occurred -------------------------------------*/
1889 if (((isrflags
& USART_ISR_PE
) != 0U) && ((cr1its
& USART_CR1_PEIE
) != 0U))
1891 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_PEF
);
1893 hirda
->ErrorCode
|= HAL_IRDA_ERROR_PE
;
1896 /* IRDA frame error interrupt occurred --------------------------------------*/
1897 if (((isrflags
& USART_ISR_FE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
1899 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_FEF
);
1901 hirda
->ErrorCode
|= HAL_IRDA_ERROR_FE
;
1904 /* IRDA noise error interrupt occurred --------------------------------------*/
1905 if (((isrflags
& USART_ISR_NE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
1907 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_NEF
);
1909 hirda
->ErrorCode
|= HAL_IRDA_ERROR_NE
;
1912 /* IRDA Over-Run interrupt occurred -----------------------------------------*/
1913 if (((isrflags
& USART_ISR_ORE
) != 0U) &&
1914 (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U) || ((cr3its
& USART_CR3_EIE
) != 0U)))
1916 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_OREF
);
1918 hirda
->ErrorCode
|= HAL_IRDA_ERROR_ORE
;
1921 /* Call IRDA Error Call back function if need be --------------------------*/
1922 if (hirda
->ErrorCode
!= HAL_IRDA_ERROR_NONE
)
1924 /* IRDA in mode Receiver ---------------------------------------------------*/
1925 if (((isrflags
& USART_ISR_RXNE_RXFNE
) != 0U) && ((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U))
1927 IRDA_Receive_IT(hirda
);
1930 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1931 consider error as blocking */
1932 errorcode
= hirda
->ErrorCode
;
1933 if ((HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
)) ||
1934 ((errorcode
& HAL_IRDA_ERROR_ORE
) != 0U))
1936 /* Blocking error : transfer is aborted
1937 Set the IRDA state ready to be able to start again the process,
1938 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1939 IRDA_EndRxTransfer(hirda
);
1941 /* Disable the IRDA DMA Rx request if enabled */
1942 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1944 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1946 /* Abort the IRDA DMA Rx channel */
1947 if (hirda
->hdmarx
!= NULL
)
1949 /* Set the IRDA DMA Abort callback :
1950 will lead to call HAL_IRDA_ErrorCallback() at end of DMA abort procedure */
1951 hirda
->hdmarx
->XferAbortCallback
= IRDA_DMAAbortOnError
;
1954 if (HAL_DMA_Abort_IT(hirda
->hdmarx
) != HAL_OK
)
1956 /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
1957 hirda
->hdmarx
->XferAbortCallback(hirda
->hdmarx
);
1962 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1963 /* Call registered user error callback */
1964 hirda
->ErrorCallback(hirda
);
1966 /* Call legacy weak user error callback */
1967 HAL_IRDA_ErrorCallback(hirda
);
1968 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1973 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1974 /* Call registered user error callback */
1975 hirda
->ErrorCallback(hirda
);
1977 /* Call legacy weak user error callback */
1978 HAL_IRDA_ErrorCallback(hirda
);
1979 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1984 /* Non Blocking error : transfer could go on.
1985 Error is notified to user through user error callback */
1986 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1987 /* Call registered user error callback */
1988 hirda
->ErrorCallback(hirda
);
1990 /* Call legacy weak user error callback */
1991 HAL_IRDA_ErrorCallback(hirda
);
1992 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1993 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1998 } /* End if some error occurs */
2000 /* IRDA in mode Transmitter ------------------------------------------------*/
2001 if (((isrflags
& USART_ISR_TXE_TXFNF
) != 0U) && ((cr1its
& USART_CR1_TXEIE_TXFNFIE
) != 0U))
2003 IRDA_Transmit_IT(hirda
);
2007 /* IRDA in mode Transmitter (transmission end) -----------------------------*/
2008 if (((isrflags
& USART_ISR_TC
) != 0U) && ((cr1its
& USART_CR1_TCIE
) != 0U))
2010 IRDA_EndTransmit_IT(hirda
);
2017 * @brief Tx Transfer completed callback.
2018 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2019 * the configuration information for the specified IRDA module.
2022 __weak
void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef
*hirda
)
2024 /* Prevent unused argument(s) compilation warning */
2027 /* NOTE : This function should not be modified, when the callback is needed,
2028 the HAL_IRDA_TxCpltCallback can be implemented in the user file.
2033 * @brief Tx Half Transfer completed callback.
2034 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2035 * the configuration information for the specified USART module.
2038 __weak
void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef
*hirda
)
2040 /* Prevent unused argument(s) compilation warning */
2043 /* NOTE : This function should not be modified, when the callback is needed,
2044 the HAL_IRDA_TxHalfCpltCallback can be implemented in the user file.
2049 * @brief Rx Transfer completed callback.
2050 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2051 * the configuration information for the specified IRDA module.
2054 __weak
void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef
*hirda
)
2056 /* Prevent unused argument(s) compilation warning */
2059 /* NOTE : This function should not be modified, when the callback is needed,
2060 the HAL_IRDA_RxCpltCallback can be implemented in the user file.
2065 * @brief Rx Half Transfer complete callback.
2066 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2067 * the configuration information for the specified IRDA module.
2070 __weak
void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef
*hirda
)
2072 /* Prevent unused argument(s) compilation warning */
2075 /* NOTE : This function should not be modified, when the callback is needed,
2076 the HAL_IRDA_RxHalfCpltCallback can be implemented in the user file.
2081 * @brief IRDA error callback.
2082 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2083 * the configuration information for the specified IRDA module.
2086 __weak
void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef
*hirda
)
2088 /* Prevent unused argument(s) compilation warning */
2091 /* NOTE : This function should not be modified, when the callback is needed,
2092 the HAL_IRDA_ErrorCallback can be implemented in the user file.
2097 * @brief IRDA Abort Complete callback.
2098 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2099 * the configuration information for the specified IRDA module.
2102 __weak
void HAL_IRDA_AbortCpltCallback(IRDA_HandleTypeDef
*hirda
)
2104 /* Prevent unused argument(s) compilation warning */
2107 /* NOTE : This function should not be modified, when the callback is needed,
2108 the HAL_IRDA_AbortCpltCallback can be implemented in the user file.
2113 * @brief IRDA Abort Complete callback.
2114 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2115 * the configuration information for the specified IRDA module.
2118 __weak
void HAL_IRDA_AbortTransmitCpltCallback(IRDA_HandleTypeDef
*hirda
)
2120 /* Prevent unused argument(s) compilation warning */
2123 /* NOTE : This function should not be modified, when the callback is needed,
2124 the HAL_IRDA_AbortTransmitCpltCallback can be implemented in the user file.
2129 * @brief IRDA Abort Receive Complete callback.
2130 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2131 * the configuration information for the specified IRDA module.
2134 __weak
void HAL_IRDA_AbortReceiveCpltCallback(IRDA_HandleTypeDef
*hirda
)
2136 /* Prevent unused argument(s) compilation warning */
2139 /* NOTE : This function should not be modified, when the callback is needed,
2140 the HAL_IRDA_AbortReceiveCpltCallback can be implemented in the user file.
2148 /** @defgroup IRDA_Exported_Functions_Group4 Peripheral State and Error functions
2149 * @brief IRDA State and Errors functions
2152 ==============================================================================
2153 ##### Peripheral State and Error functions #####
2154 ==============================================================================
2156 This subsection provides a set of functions allowing to return the State of IrDA
2157 communication process and also return Peripheral Errors occurred during communication process
2158 (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state
2159 of the IRDA peripheral handle.
2160 (+) HAL_IRDA_GetError() checks in run-time errors that could occur during
2168 * @brief Return the IRDA handle state.
2169 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2170 * the configuration information for the specified IRDA module.
2173 HAL_IRDA_StateTypeDef
HAL_IRDA_GetState(IRDA_HandleTypeDef
*hirda
)
2175 /* Return IRDA handle state */
2178 temp1
= (uint32_t)hirda
->gState
;
2179 temp2
= (uint32_t)hirda
->RxState
;
2181 return (HAL_IRDA_StateTypeDef
)(temp1
| temp2
);
2185 * @brief Return the IRDA handle error code.
2186 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2187 * the configuration information for the specified IRDA module.
2188 * @retval IRDA Error Code
2190 uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef
*hirda
)
2192 return hirda
->ErrorCode
;
2203 /** @defgroup IRDA_Private_Functions IRDA Private Functions
2207 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2209 * @brief Initialize the callbacks to their default values.
2210 * @param hirda IRDA handle.
2213 void IRDA_InitCallbacksToDefault(IRDA_HandleTypeDef
*hirda
)
2215 /* Init the IRDA Callback settings */
2216 hirda
->TxHalfCpltCallback
= HAL_IRDA_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
2217 hirda
->TxCpltCallback
= HAL_IRDA_TxCpltCallback
; /* Legacy weak TxCpltCallback */
2218 hirda
->RxHalfCpltCallback
= HAL_IRDA_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
2219 hirda
->RxCpltCallback
= HAL_IRDA_RxCpltCallback
; /* Legacy weak RxCpltCallback */
2220 hirda
->ErrorCallback
= HAL_IRDA_ErrorCallback
; /* Legacy weak ErrorCallback */
2221 hirda
->AbortCpltCallback
= HAL_IRDA_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
2222 hirda
->AbortTransmitCpltCallback
= HAL_IRDA_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
2223 hirda
->AbortReceiveCpltCallback
= HAL_IRDA_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
2226 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
2229 * @brief Configure the IRDA peripheral.
2230 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2231 * the configuration information for the specified IRDA module.
2232 * @retval HAL status
2234 static HAL_StatusTypeDef
IRDA_SetConfig(IRDA_HandleTypeDef
*hirda
)
2237 IRDA_ClockSourceTypeDef clocksource
;
2238 HAL_StatusTypeDef ret
= HAL_OK
;
2239 const uint16_t IRDAPrescTable
[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
2240 PLL2_ClocksTypeDef pll2_clocks
;
2241 PLL3_ClocksTypeDef pll3_clocks
;
2244 /* Check the communication parameters */
2245 assert_param(IS_IRDA_BAUDRATE(hirda
->Init
.BaudRate
));
2246 assert_param(IS_IRDA_WORD_LENGTH(hirda
->Init
.WordLength
));
2247 assert_param(IS_IRDA_PARITY(hirda
->Init
.Parity
));
2248 assert_param(IS_IRDA_TX_RX_MODE(hirda
->Init
.Mode
));
2249 assert_param(IS_IRDA_PRESCALER(hirda
->Init
.Prescaler
));
2250 assert_param(IS_IRDA_POWERMODE(hirda
->Init
.PowerMode
));
2251 assert_param(IS_IRDA_CLOCKPRESCALER(hirda
->Init
.ClockPrescaler
));
2253 /*-------------------------- USART CR1 Configuration -----------------------*/
2254 /* Configure the IRDA Word Length, Parity and transfer Mode:
2255 Set the M bits according to hirda->Init.WordLength value
2256 Set PCE and PS bits according to hirda->Init.Parity value
2257 Set TE and RE bits according to hirda->Init.Mode value */
2258 tmpreg
= (uint32_t)hirda
->Init
.WordLength
| hirda
->Init
.Parity
| hirda
->Init
.Mode
;
2260 MODIFY_REG(hirda
->Instance
->CR1
, IRDA_CR1_FIELDS
, tmpreg
);
2262 /*-------------------------- USART CR3 Configuration -----------------------*/
2263 MODIFY_REG(hirda
->Instance
->CR3
, USART_CR3_IRLP
, hirda
->Init
.PowerMode
);
2265 /*--------------------- USART clock PRESC Configuration ----------------*/
2267 * - IRDA Clock Prescaler: set PRESCALER according to hirda->Init.ClockPrescaler value */
2268 MODIFY_REG(hirda
->Instance
->PRESC
, USART_PRESC_PRESCALER
, hirda
->Init
.ClockPrescaler
);
2270 /*-------------------------- USART GTPR Configuration ----------------------*/
2271 MODIFY_REG(hirda
->Instance
->GTPR
, (uint16_t)USART_GTPR_PSC
, (uint16_t)hirda
->Init
.Prescaler
);
2273 /*-------------------------- USART BRR Configuration -----------------------*/
2274 IRDA_GETCLOCKSOURCE(hirda
, clocksource
);
2276 switch (clocksource
)
2278 case IRDA_CLOCKSOURCE_D2PCLK1
:
2279 pclk
= HAL_RCC_GetPCLK1Freq();
2280 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16(pclk
, hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2282 case IRDA_CLOCKSOURCE_D2PCLK2
:
2283 pclk
= HAL_RCC_GetPCLK2Freq();
2284 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16(pclk
, hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2286 case IRDA_CLOCKSOURCE_PLL2Q
:
2287 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks
);
2288 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16(pll2_clocks
.PLL2_Q_Frequency
, hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2290 case IRDA_CLOCKSOURCE_PLL3Q
:
2291 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks
);
2292 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16(pll3_clocks
.PLL3_Q_Frequency
, hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2294 case IRDA_CLOCKSOURCE_CSI
:
2295 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16(CSI_VALUE
, hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2297 case IRDA_CLOCKSOURCE_HSI
:
2298 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16(HSI_VALUE
, hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2300 case IRDA_CLOCKSOURCE_LSE
:
2301 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16((uint32_t)LSE_VALUE
, hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2308 /* USARTDIV must be greater than or equal to 0d16 */
2309 if ((tmpreg
>= USART_BRR_MIN
) && (tmpreg
<= USART_BRR_MAX
))
2311 hirda
->Instance
->BRR
= tmpreg
;
2322 * @brief Check the IRDA Idle State.
2323 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2324 * the configuration information for the specified IRDA module.
2325 * @retval HAL status
2327 static HAL_StatusTypeDef
IRDA_CheckIdleState(IRDA_HandleTypeDef
*hirda
)
2331 /* Initialize the IRDA ErrorCode */
2332 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
2334 /* Init tickstart for timeout managment*/
2335 tickstart
= HAL_GetTick();
2337 /* Check if the Transmitter is enabled */
2338 if ((hirda
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
2340 /* Wait until TEACK flag is set */
2341 if (IRDA_WaitOnFlagUntilTimeout(hirda
, USART_ISR_TEACK
, RESET
, tickstart
, IRDA_TEACK_REACK_TIMEOUT
) != HAL_OK
)
2343 /* Timeout occurred */
2347 /* Check if the Receiver is enabled */
2348 if ((hirda
->Instance
->CR1
& USART_CR1_RE
) == USART_CR1_RE
)
2350 /* Wait until REACK flag is set */
2351 if (IRDA_WaitOnFlagUntilTimeout(hirda
, USART_ISR_REACK
, RESET
, tickstart
, IRDA_TEACK_REACK_TIMEOUT
) != HAL_OK
)
2353 /* Timeout occurred */
2358 /* Initialize the IRDA state*/
2359 hirda
->gState
= HAL_IRDA_STATE_READY
;
2360 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2362 /* Process Unlocked */
2363 __HAL_UNLOCK(hirda
);
2369 * @brief Handle IRDA Communication Timeout.
2370 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2371 * the configuration information for the specified IRDA module.
2372 * @param Flag Specifies the IRDA flag to check.
2373 * @param Status Flag status (SET or RESET)
2374 * @param Tickstart Tick start value
2375 * @param Timeout Timeout duration
2376 * @retval HAL status
2378 static HAL_StatusTypeDef
IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef
*hirda
, uint32_t Flag
, FlagStatus Status
,
2379 uint32_t Tickstart
, uint32_t Timeout
)
2381 /* Wait until flag is set */
2382 while ((__HAL_IRDA_GET_FLAG(hirda
, Flag
) ? SET
: RESET
) == Status
)
2384 /* Check for the Timeout */
2385 if (Timeout
!= HAL_MAX_DELAY
)
2387 if (((HAL_GetTick() - Tickstart
) > Timeout
) || (Timeout
== 0U))
2389 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
2390 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
));
2391 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
2393 hirda
->gState
= HAL_IRDA_STATE_READY
;
2394 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2396 /* Process Unlocked */
2397 __HAL_UNLOCK(hirda
);
2407 * @brief End ongoing Tx transfer on IRDA peripheral (following error detection or Transmit completion).
2408 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2409 * the configuration information for the specified IRDA module.
2412 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef
*hirda
)
2414 /* Disable TXEIE and TCIE interrupts */
2415 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
2417 /* At end of Tx process, restore hirda->gState to Ready */
2418 hirda
->gState
= HAL_IRDA_STATE_READY
;
2423 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2424 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2425 * the configuration information for the specified IRDA module.
2428 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef
*hirda
)
2430 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2431 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
2432 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
2434 /* At end of Rx process, restore hirda->RxState to Ready */
2435 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2440 * @brief DMA IRDA transmit process complete callback.
2441 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2442 * the configuration information for the specified DMA module.
2445 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2447 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2449 /* DMA Normal mode */
2450 if (hdma
->Init
.Mode
!= DMA_CIRCULAR
)
2452 hirda
->TxXferCount
= 0U;
2454 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2455 in the IRDA CR3 register */
2456 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
2458 /* Enable the IRDA Transmit Complete Interrupt */
2459 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
2461 /* DMA Circular mode */
2464 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2465 /* Call registered Tx complete callback */
2466 hirda
->TxCpltCallback(hirda
);
2468 /* Call legacy weak Tx complete callback */
2469 HAL_IRDA_TxCpltCallback(hirda
);
2470 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2476 * @brief DMA IRDA transmit process half complete callback.
2477 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2478 * the configuration information for the specified DMA module.
2481 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef
*hdma
)
2483 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2485 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2486 /* Call registered Tx Half complete callback */
2487 hirda
->TxHalfCpltCallback(hirda
);
2489 /* Call legacy weak Tx complete callback */
2490 HAL_IRDA_TxHalfCpltCallback(hirda
);
2491 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2495 * @brief DMA IRDA receive process complete callback.
2496 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2497 * the configuration information for the specified DMA module.
2500 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
2502 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2504 /* DMA Normal mode */
2505 if (hdma
->Init
.Mode
!= DMA_CIRCULAR
)
2507 hirda
->RxXferCount
= 0U;
2509 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2510 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
2511 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
2513 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2514 in the IRDA CR3 register */
2515 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
2517 /* At end of Rx process, restore hirda->RxState to Ready */
2518 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2521 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2522 /* Call registered Rx complete callback */
2523 hirda
->RxCpltCallback(hirda
);
2525 /* Call legacy weak Rx complete callback */
2526 HAL_IRDA_RxCpltCallback(hirda
);
2527 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
2531 * @brief DMA IRDA receive process half complete callback.
2532 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2533 * the configuration information for the specified DMA module.
2536 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef
*hdma
)
2538 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2540 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2541 /*Call registered Rx Half complete callback*/
2542 hirda
->RxHalfCpltCallback(hirda
);
2544 /* Call legacy weak Rx Half complete callback */
2545 HAL_IRDA_RxHalfCpltCallback(hirda
);
2546 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2550 * @brief DMA IRDA communication error callback.
2551 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2552 * the configuration information for the specified DMA module.
2555 static void IRDA_DMAError(DMA_HandleTypeDef
*hdma
)
2557 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2559 /* Stop IRDA DMA Tx request if ongoing */
2560 if (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
2562 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
2564 hirda
->TxXferCount
= 0U;
2565 IRDA_EndTxTransfer(hirda
);
2569 /* Stop IRDA DMA Rx request if ongoing */
2570 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
2572 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
2574 hirda
->RxXferCount
= 0U;
2575 IRDA_EndRxTransfer(hirda
);
2579 hirda
->ErrorCode
|= HAL_IRDA_ERROR_DMA
;
2580 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2581 /* Call registered user error callback */
2582 hirda
->ErrorCallback(hirda
);
2584 /* Call legacy weak user error callback */
2585 HAL_IRDA_ErrorCallback(hirda
);
2586 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2590 * @brief DMA IRDA communication abort callback, when initiated by HAL services on Error
2591 * (To be called at end of DMA Abort procedure following error occurrence).
2592 * @param hdma DMA handle.
2595 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
2597 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2598 hirda
->RxXferCount
= 0U;
2599 hirda
->TxXferCount
= 0U;
2601 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2602 /* Call registered user error callback */
2603 hirda
->ErrorCallback(hirda
);
2605 /* Call legacy weak user error callback */
2606 HAL_IRDA_ErrorCallback(hirda
);
2607 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2611 * @brief DMA IRDA Tx communication abort callback, when initiated by user
2612 * (To be called at end of DMA Tx Abort procedure following user abort request).
2613 * @note When this callback is executed, User Abort complete call back is called only if no
2614 * Abort still ongoing for Rx DMA Handle.
2615 * @param hdma DMA handle.
2618 static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
2620 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2622 hirda
->hdmatx
->XferAbortCallback
= NULL
;
2624 /* Check if an Abort process is still ongoing */
2625 if (hirda
->hdmarx
!= NULL
)
2627 if (hirda
->hdmarx
->XferAbortCallback
!= NULL
)
2633 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2634 hirda
->TxXferCount
= 0U;
2635 hirda
->RxXferCount
= 0U;
2637 /* Reset errorCode */
2638 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
2640 /* Clear the Error flags in the ICR register */
2641 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
2643 /* Restore hirda->gState and hirda->RxState to Ready */
2644 hirda
->gState
= HAL_IRDA_STATE_READY
;
2645 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2647 /* Call user Abort complete callback */
2648 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2649 /* Call registered Abort complete callback */
2650 hirda
->AbortCpltCallback(hirda
);
2652 /* Call legacy weak Abort complete callback */
2653 HAL_IRDA_AbortCpltCallback(hirda
);
2654 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2659 * @brief DMA IRDA Rx communication abort callback, when initiated by user
2660 * (To be called at end of DMA Rx Abort procedure following user abort request).
2661 * @note When this callback is executed, User Abort complete call back is called only if no
2662 * Abort still ongoing for Tx DMA Handle.
2663 * @param hdma DMA handle.
2666 static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
2668 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2670 hirda
->hdmarx
->XferAbortCallback
= NULL
;
2672 /* Check if an Abort process is still ongoing */
2673 if (hirda
->hdmatx
!= NULL
)
2675 if (hirda
->hdmatx
->XferAbortCallback
!= NULL
)
2681 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2682 hirda
->TxXferCount
= 0U;
2683 hirda
->RxXferCount
= 0U;
2685 /* Reset errorCode */
2686 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
2688 /* Clear the Error flags in the ICR register */
2689 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
2691 /* Restore hirda->gState and hirda->RxState to Ready */
2692 hirda
->gState
= HAL_IRDA_STATE_READY
;
2693 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2695 /* Call user Abort complete callback */
2696 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2697 /* Call registered Abort complete callback */
2698 hirda
->AbortCpltCallback(hirda
);
2700 /* Call legacy weak Abort complete callback */
2701 HAL_IRDA_AbortCpltCallback(hirda
);
2702 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2707 * @brief DMA IRDA Tx communication abort callback, when initiated by user by a call to
2708 * HAL_IRDA_AbortTransmit_IT API (Abort only Tx transfer)
2709 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2710 * and leads to user Tx Abort Complete callback execution).
2711 * @param hdma DMA handle.
2714 static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2716 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2718 hirda
->TxXferCount
= 0U;
2720 /* Restore hirda->gState to Ready */
2721 hirda
->gState
= HAL_IRDA_STATE_READY
;
2723 /* Call user Abort complete callback */
2724 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2725 /* Call registered Abort Transmit Complete Callback */
2726 hirda
->AbortTransmitCpltCallback(hirda
);
2728 /* Call legacy weak Abort Transmit Complete Callback */
2729 HAL_IRDA_AbortTransmitCpltCallback(hirda
);
2730 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2734 * @brief DMA IRDA Rx communication abort callback, when initiated by user by a call to
2735 * HAL_IRDA_AbortReceive_IT API (Abort only Rx transfer)
2736 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2737 * and leads to user Rx Abort Complete callback execution).
2738 * @param hdma DMA handle.
2741 static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2743 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
2745 hirda
->RxXferCount
= 0U;
2747 /* Clear the Error flags in the ICR register */
2748 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
2750 /* Restore hirda->RxState to Ready */
2751 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2753 /* Call user Abort complete callback */
2754 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2755 /* Call registered Abort Receive Complete Callback */
2756 hirda
->AbortReceiveCpltCallback(hirda
);
2758 /* Call legacy weak Abort Receive Complete Callback */
2759 HAL_IRDA_AbortReceiveCpltCallback(hirda
);
2760 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2764 * @brief Send an amount of data in interrupt mode.
2765 * @note Function is called under interruption only, once
2766 * interruptions have been enabled by HAL_IRDA_Transmit_IT().
2767 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2768 * the configuration information for the specified IRDA module.
2771 static void IRDA_Transmit_IT(IRDA_HandleTypeDef
*hirda
)
2775 /* Check that a Tx process is ongoing */
2776 if (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
2778 if (hirda
->TxXferCount
== 0U)
2780 /* Disable the IRDA Transmit Data Register Empty Interrupt */
2781 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
2783 /* Enable the IRDA Transmit Complete Interrupt */
2784 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
2788 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
2790 tmp
= (uint16_t *) hirda
->pTxBuffPtr
; /* Derogation R.11.3 */
2791 hirda
->Instance
->TDR
= (uint16_t)(*tmp
& 0x01FFU
);
2792 hirda
->pTxBuffPtr
+= 2U;
2796 hirda
->Instance
->TDR
= (uint8_t)(*hirda
->pTxBuffPtr
& 0xFFU
);
2797 hirda
->pTxBuffPtr
++;
2799 hirda
->TxXferCount
--;
2805 * @brief Wrap up transmission in non-blocking mode.
2806 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2807 * the configuration information for the specified IRDA module.
2810 static void IRDA_EndTransmit_IT(IRDA_HandleTypeDef
*hirda
)
2812 /* Disable the IRDA Transmit Complete Interrupt */
2813 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
2815 /* Tx process is ended, restore hirda->gState to Ready */
2816 hirda
->gState
= HAL_IRDA_STATE_READY
;
2818 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2819 /* Call registered Tx complete callback */
2820 hirda
->TxCpltCallback(hirda
);
2822 /* Call legacy weak Tx complete callback */
2823 HAL_IRDA_TxCpltCallback(hirda
);
2824 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2828 * @brief Receive an amount of data in interrupt mode.
2829 * @note Function is called under interruption only, once
2830 * interruptions have been enabled by HAL_IRDA_Receive_IT()
2831 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2832 * the configuration information for the specified IRDA module.
2835 static void IRDA_Receive_IT(IRDA_HandleTypeDef
*hirda
)
2838 uint16_t uhMask
= hirda
->Mask
;
2841 /* Check that a Rx process is ongoing */
2842 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
2844 uhdata
= (uint16_t) READ_REG(hirda
->Instance
->RDR
);
2845 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
2847 tmp
= (uint16_t *) hirda
->pRxBuffPtr
; /* Derogation R.11.3 */
2848 *tmp
= (uint16_t)(uhdata
& uhMask
);
2849 hirda
->pRxBuffPtr
+= 2U;
2853 *hirda
->pRxBuffPtr
= (uint8_t)(uhdata
& (uint8_t)uhMask
);
2854 hirda
->pRxBuffPtr
++;
2857 hirda
->RxXferCount
--;
2858 if (hirda
->RxXferCount
== 0U)
2860 /* Disable the IRDA Parity Error Interrupt and RXNE interrupt */
2861 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
2863 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
2864 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
2866 /* Rx process is completed, restore hirda->RxState to Ready */
2867 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2869 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2870 /* Call registered Rx complete callback */
2871 hirda
->RxCpltCallback(hirda
);
2873 /* Call legacy weak Rx complete callback */
2874 HAL_IRDA_RxCpltCallback(hirda
);
2875 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
2880 /* Clear RXNE interrupt flag */
2881 __HAL_IRDA_SEND_REQ(hirda
, IRDA_RXDATA_FLUSH_REQUEST
);
2889 #endif /* HAL_IRDA_MODULE_ENABLED */
2898 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/