2 ******************************************************************************
3 * @file stm32g4xx_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 "stm32g4xx_hal.h"
190 /** @addtogroup STM32G4xx_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
721 (#) There are two modes of transfer:
722 (++) Blocking mode: the communication is performed in polling mode.
723 The HAL status of all data processing is returned by the same function
724 after finishing transfer.
725 (++) Non-Blocking mode: the communication is performed using Interrupts
726 or DMA, these API's return the HAL status.
727 The end of the data processing will be indicated through the
728 dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
730 The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
731 will be executed respectively at the end of the Transmit or Receive process
732 The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
734 (#) Blocking mode APIs are :
735 (++) HAL_IRDA_Transmit()
736 (++) HAL_IRDA_Receive()
738 (#) Non Blocking mode APIs with Interrupt are :
739 (++) HAL_IRDA_Transmit_IT()
740 (++) HAL_IRDA_Receive_IT()
741 (++) HAL_IRDA_IRQHandler()
743 (#) Non Blocking mode functions with DMA are :
744 (++) HAL_IRDA_Transmit_DMA()
745 (++) HAL_IRDA_Receive_DMA()
746 (++) HAL_IRDA_DMAPause()
747 (++) HAL_IRDA_DMAResume()
748 (++) HAL_IRDA_DMAStop()
750 (#) A set of Transfer Complete Callbacks are provided in Non Blocking mode:
751 (++) HAL_IRDA_TxHalfCpltCallback()
752 (++) HAL_IRDA_TxCpltCallback()
753 (++) HAL_IRDA_RxHalfCpltCallback()
754 (++) HAL_IRDA_RxCpltCallback()
755 (++) HAL_IRDA_ErrorCallback()
757 (#) Non-Blocking mode transfers could be aborted using Abort API's :
759 (+) HAL_IRDA_AbortTransmit()
760 (+) HAL_IRDA_AbortReceive()
761 (+) HAL_IRDA_Abort_IT()
762 (+) HAL_IRDA_AbortTransmit_IT()
763 (+) HAL_IRDA_AbortReceive_IT()
765 (#) For Abort services based on interrupts (HAL_IRDA_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
766 (+) HAL_IRDA_AbortCpltCallback()
767 (+) HAL_IRDA_AbortTransmitCpltCallback()
768 (+) HAL_IRDA_AbortReceiveCpltCallback()
770 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
771 Errors are handled as follows :
772 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
773 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
774 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
775 and HAL_IRDA_ErrorCallback() user callback is executed. Transfer is kept ongoing on IRDA side.
776 If user wants to abort it, Abort services should be called by user.
777 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
778 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
779 Error code is set to allow user to identify error type, and HAL_IRDA_ErrorCallback() user callback is executed.
786 * @brief Send an amount of data in blocking mode.
787 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
788 * the configuration information for the specified IRDA module.
789 * @param pData Pointer to data buffer.
790 * @param Size Amount of data to be sent.
791 * @param Timeout Specify timeout value.
794 HAL_StatusTypeDef
HAL_IRDA_Transmit(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
797 uint16_t *pdata16bits
;
800 /* Check that a Tx process is not already ongoing */
801 if (hirda
->gState
== HAL_IRDA_STATE_READY
)
803 if ((pData
== NULL
) || (Size
== 0U))
811 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
812 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
814 /* Init tickstart for timeout managment*/
815 tickstart
= HAL_GetTick();
817 hirda
->TxXferSize
= Size
;
818 hirda
->TxXferCount
= Size
;
820 /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
821 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
824 pdata16bits
= (uint16_t *) pData
; /* Derogation R.11.3 */
832 while (hirda
->TxXferCount
> 0U)
834 hirda
->TxXferCount
--;
836 if (IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
840 if (pdata8bits
== NULL
)
842 hirda
->Instance
->TDR
= (uint16_t)(*pdata16bits
& 0x01FFU
);
847 hirda
->Instance
->TDR
= (uint8_t)(*pdata8bits
& 0xFFU
);
852 if (IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_TC
, RESET
, tickstart
, Timeout
) != HAL_OK
)
857 /* At end of Tx process, restore hirda->gState to Ready */
858 hirda
->gState
= HAL_IRDA_STATE_READY
;
860 /* Process Unlocked */
872 * @brief Receive an amount of data in blocking mode.
873 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
874 * the configuration information for the specified IRDA module.
875 * @param pData Pointer to data buffer.
876 * @param Size Amount of data to be received.
877 * @param Timeout Specify timeout value.
880 HAL_StatusTypeDef
HAL_IRDA_Receive(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
883 uint16_t *pdata16bits
;
887 /* Check that a Rx process is not already ongoing */
888 if (hirda
->RxState
== HAL_IRDA_STATE_READY
)
890 if ((pData
== NULL
) || (Size
== 0U))
898 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
899 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
901 /* Init tickstart for timeout managment*/
902 tickstart
= HAL_GetTick();
904 hirda
->RxXferSize
= Size
;
905 hirda
->RxXferCount
= Size
;
907 /* Computation of the mask to apply to RDR register
908 of the UART associated to the IRDA */
909 IRDA_MASK_COMPUTATION(hirda
);
910 uhMask
= hirda
->Mask
;
912 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
913 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
916 pdata16bits
= (uint16_t *) pData
; /* Derogation R.11.3 */
924 /* Check data remaining to be received */
925 while (hirda
->RxXferCount
> 0U)
927 hirda
->RxXferCount
--;
929 if (IRDA_WaitOnFlagUntilTimeout(hirda
, IRDA_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
933 if (pdata8bits
== NULL
)
935 *pdata16bits
= (uint16_t)(hirda
->Instance
->RDR
& uhMask
);
940 *pdata8bits
= (uint8_t)(hirda
->Instance
->RDR
& (uint8_t)uhMask
);
945 /* At end of Rx process, restore hirda->RxState to Ready */
946 hirda
->RxState
= HAL_IRDA_STATE_READY
;
948 /* Process Unlocked */
960 * @brief Send an amount of data in interrupt mode.
961 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
962 * the configuration information for the specified IRDA module.
963 * @param pData Pointer to data buffer.
964 * @param Size Amount of data to be sent.
967 HAL_StatusTypeDef
HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
969 /* Check that a Tx process is not already ongoing */
970 if (hirda
->gState
== HAL_IRDA_STATE_READY
)
972 if ((pData
== NULL
) || (Size
== 0U))
980 hirda
->pTxBuffPtr
= pData
;
981 hirda
->TxXferSize
= Size
;
982 hirda
->TxXferCount
= Size
;
984 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
985 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
987 /* Process Unlocked */
990 /* Enable the IRDA Transmit Data Register Empty Interrupt */
991 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
1002 * @brief Receive an amount of data in interrupt mode.
1003 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1004 * the configuration information for the specified IRDA module.
1005 * @param pData Pointer to data buffer.
1006 * @param Size Amount of data to be received.
1007 * @retval HAL status
1009 HAL_StatusTypeDef
HAL_IRDA_Receive_IT(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
1011 /* Check that a Rx process is not already ongoing */
1012 if (hirda
->RxState
== HAL_IRDA_STATE_READY
)
1014 if ((pData
== NULL
) || (Size
== 0U))
1019 /* Process Locked */
1022 hirda
->pRxBuffPtr
= pData
;
1023 hirda
->RxXferSize
= Size
;
1024 hirda
->RxXferCount
= Size
;
1026 /* Computation of the mask to apply to the RDR register
1027 of the UART associated to the IRDA */
1028 IRDA_MASK_COMPUTATION(hirda
);
1030 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1031 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
1033 /* Process Unlocked */
1034 __HAL_UNLOCK(hirda
);
1036 /* Enable the IRDA Parity Error and Data Register not empty Interrupts */
1037 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE_RXFNEIE
);
1039 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1040 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1051 * @brief Send an amount of data in DMA mode.
1052 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1053 * the configuration information for the specified IRDA module.
1054 * @param pData pointer to data buffer.
1055 * @param Size amount of data to be sent.
1056 * @retval HAL status
1058 HAL_StatusTypeDef
HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
1060 /* Check that a Tx process is not already ongoing */
1061 if (hirda
->gState
== HAL_IRDA_STATE_READY
)
1063 if ((pData
== NULL
) || (Size
== 0U))
1068 /* Process Locked */
1071 hirda
->pTxBuffPtr
= pData
;
1072 hirda
->TxXferSize
= Size
;
1073 hirda
->TxXferCount
= Size
;
1075 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1076 hirda
->gState
= HAL_IRDA_STATE_BUSY_TX
;
1078 /* Set the IRDA DMA transfer complete callback */
1079 hirda
->hdmatx
->XferCpltCallback
= IRDA_DMATransmitCplt
;
1081 /* Set the IRDA DMA half transfer complete callback */
1082 hirda
->hdmatx
->XferHalfCpltCallback
= IRDA_DMATransmitHalfCplt
;
1084 /* Set the DMA error callback */
1085 hirda
->hdmatx
->XferErrorCallback
= IRDA_DMAError
;
1087 /* Set the DMA abort callback */
1088 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1090 /* Enable the IRDA transmit DMA channel */
1091 if (HAL_DMA_Start_IT(hirda
->hdmatx
, (uint32_t)hirda
->pTxBuffPtr
, (uint32_t)&hirda
->Instance
->TDR
, Size
) == HAL_OK
)
1093 /* Clear the TC flag in the ICR register */
1094 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_TCF
);
1096 /* Process Unlocked */
1097 __HAL_UNLOCK(hirda
);
1099 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1100 in the USART CR3 register */
1101 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1107 /* Set error code to DMA */
1108 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1110 /* Process Unlocked */
1111 __HAL_UNLOCK(hirda
);
1113 /* Restore hirda->gState to ready */
1114 hirda
->gState
= HAL_IRDA_STATE_READY
;
1126 * @brief Receive an amount of data in DMA mode.
1127 * @note When the IRDA parity is enabled (PCE = 1), the received data contains
1128 * the parity bit (MSB position).
1129 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1130 * the configuration information for the specified IRDA module.
1131 * @param pData Pointer to data buffer.
1132 * @param Size Amount of data to be received.
1133 * @retval HAL status
1135 HAL_StatusTypeDef
HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef
*hirda
, uint8_t *pData
, uint16_t Size
)
1137 /* Check that a Rx process is not already ongoing */
1138 if (hirda
->RxState
== HAL_IRDA_STATE_READY
)
1140 if ((pData
== NULL
) || (Size
== 0U))
1145 /* Process Locked */
1148 hirda
->pRxBuffPtr
= pData
;
1149 hirda
->RxXferSize
= Size
;
1151 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1152 hirda
->RxState
= HAL_IRDA_STATE_BUSY_RX
;
1154 /* Set the IRDA DMA transfer complete callback */
1155 hirda
->hdmarx
->XferCpltCallback
= IRDA_DMAReceiveCplt
;
1157 /* Set the IRDA DMA half transfer complete callback */
1158 hirda
->hdmarx
->XferHalfCpltCallback
= IRDA_DMAReceiveHalfCplt
;
1160 /* Set the DMA error callback */
1161 hirda
->hdmarx
->XferErrorCallback
= IRDA_DMAError
;
1163 /* Set the DMA abort callback */
1164 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1166 /* Enable the DMA channel */
1167 if (HAL_DMA_Start_IT(hirda
->hdmarx
, (uint32_t)&hirda
->Instance
->RDR
, (uint32_t)hirda
->pRxBuffPtr
, Size
) == HAL_OK
)
1169 /* Process Unlocked */
1170 __HAL_UNLOCK(hirda
);
1172 /* Enable the UART Parity Error Interrupt */
1173 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
1175 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1176 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1178 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1179 in the USART CR3 register */
1180 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1186 /* Set error code to DMA */
1187 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1189 /* Process Unlocked */
1190 __HAL_UNLOCK(hirda
);
1192 /* Restore hirda->RxState to ready */
1193 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1206 * @brief Pause the DMA Transfer.
1207 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1208 * the configuration information for the specified IRDA module.
1209 * @retval HAL status
1211 HAL_StatusTypeDef
HAL_IRDA_DMAPause(IRDA_HandleTypeDef
*hirda
)
1213 /* Process Locked */
1216 if (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
1218 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1220 /* Disable the IRDA DMA Tx request */
1221 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1224 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
1226 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1228 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1229 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
1230 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1232 /* Disable the IRDA DMA Rx request */
1233 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1237 /* Process Unlocked */
1238 __HAL_UNLOCK(hirda
);
1244 * @brief Resume the DMA Transfer.
1245 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1246 * the configuration information for the specified UART module.
1247 * @retval HAL status
1249 HAL_StatusTypeDef
HAL_IRDA_DMAResume(IRDA_HandleTypeDef
*hirda
)
1251 /* Process Locked */
1254 if (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
1256 /* Enable the IRDA DMA Tx request */
1257 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1259 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
1261 /* Clear the Overrun flag before resuming the Rx transfer*/
1262 __HAL_IRDA_CLEAR_OREFLAG(hirda
);
1264 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1265 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
1266 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1268 /* Enable the IRDA DMA Rx request */
1269 SET_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1272 /* Process Unlocked */
1273 __HAL_UNLOCK(hirda
);
1279 * @brief Stop the DMA Transfer.
1280 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1281 * the configuration information for the specified UART module.
1282 * @retval HAL status
1284 HAL_StatusTypeDef
HAL_IRDA_DMAStop(IRDA_HandleTypeDef
*hirda
)
1286 /* The Lock is not implemented on this API to allow the user application
1287 to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback() /
1288 HAL_IRDA_TxHalfCpltCallback / HAL_IRDA_RxHalfCpltCallback:
1289 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1290 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1291 the stream and the corresponding call back is executed. */
1293 /* Stop IRDA DMA Tx request if ongoing */
1294 if (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
1296 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1298 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1300 /* Abort the IRDA DMA Tx channel */
1301 if (hirda
->hdmatx
!= NULL
)
1303 if (HAL_DMA_Abort(hirda
->hdmatx
) != HAL_OK
)
1305 if (HAL_DMA_GetError(hirda
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1307 /* Set error code to DMA */
1308 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1315 IRDA_EndTxTransfer(hirda
);
1319 /* Stop IRDA DMA Rx request if ongoing */
1320 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
1322 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1324 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1326 /* Abort the IRDA DMA Rx channel */
1327 if (hirda
->hdmarx
!= NULL
)
1329 if (HAL_DMA_Abort(hirda
->hdmarx
) != HAL_OK
)
1331 if (HAL_DMA_GetError(hirda
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1333 /* Set error code to DMA */
1334 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1341 IRDA_EndRxTransfer(hirda
);
1349 * @brief Abort ongoing transfers (blocking mode).
1350 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1351 * the configuration information for the specified UART module.
1352 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1353 * This procedure performs following operations :
1354 * - Disable IRDA Interrupts (Tx and Rx)
1355 * - Disable the DMA transfer in the peripheral register (if enabled)
1356 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1357 * - Set handle State to READY
1358 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1359 * @retval HAL status
1361 HAL_StatusTypeDef
HAL_IRDA_Abort(IRDA_HandleTypeDef
*hirda
)
1363 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1364 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1365 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1367 /* Disable the IRDA DMA Tx request if enabled */
1368 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1370 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1372 /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
1373 if (hirda
->hdmatx
!= NULL
)
1375 /* Set the IRDA DMA Abort callback to Null.
1376 No call back execution at end of DMA abort procedure */
1377 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1379 if (HAL_DMA_Abort(hirda
->hdmatx
) != HAL_OK
)
1381 if (HAL_DMA_GetError(hirda
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1383 /* Set error code to DMA */
1384 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1392 /* Disable the IRDA DMA Rx request if enabled */
1393 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1395 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1397 /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
1398 if (hirda
->hdmarx
!= NULL
)
1400 /* Set the IRDA DMA Abort callback to Null.
1401 No call back execution at end of DMA abort procedure */
1402 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1404 if (HAL_DMA_Abort(hirda
->hdmarx
) != HAL_OK
)
1406 if (HAL_DMA_GetError(hirda
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1408 /* Set error code to DMA */
1409 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1417 /* Reset Tx and Rx transfer counters */
1418 hirda
->TxXferCount
= 0U;
1419 hirda
->RxXferCount
= 0U;
1421 /* Clear the Error flags in the ICR register */
1422 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1424 /* Restore hirda->gState and hirda->RxState to Ready */
1425 hirda
->gState
= HAL_IRDA_STATE_READY
;
1426 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1428 /* Reset Handle ErrorCode to No Error */
1429 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1435 * @brief Abort ongoing Transmit transfer (blocking mode).
1436 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1437 * the configuration information for the specified UART module.
1438 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1439 * This procedure performs following operations :
1440 * - Disable IRDA Interrupts (Tx)
1441 * - Disable the DMA transfer in the peripheral register (if enabled)
1442 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1443 * - Set handle State to READY
1444 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1445 * @retval HAL status
1447 HAL_StatusTypeDef
HAL_IRDA_AbortTransmit(IRDA_HandleTypeDef
*hirda
)
1449 /* Disable TXEIE and TCIE interrupts */
1450 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1452 /* Disable the IRDA DMA Tx request if enabled */
1453 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1455 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1457 /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
1458 if (hirda
->hdmatx
!= NULL
)
1460 /* Set the IRDA DMA Abort callback to Null.
1461 No call back execution at end of DMA abort procedure */
1462 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1464 if (HAL_DMA_Abort(hirda
->hdmatx
) != HAL_OK
)
1466 if (HAL_DMA_GetError(hirda
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1468 /* Set error code to DMA */
1469 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1477 /* Reset Tx transfer counter */
1478 hirda
->TxXferCount
= 0U;
1480 /* Restore hirda->gState to Ready */
1481 hirda
->gState
= HAL_IRDA_STATE_READY
;
1487 * @brief Abort ongoing Receive transfer (blocking mode).
1488 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1489 * the configuration information for the specified UART module.
1490 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1491 * This procedure performs following operations :
1492 * - Disable IRDA Interrupts (Rx)
1493 * - Disable the DMA transfer in the peripheral register (if enabled)
1494 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1495 * - Set handle State to READY
1496 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1497 * @retval HAL status
1499 HAL_StatusTypeDef
HAL_IRDA_AbortReceive(IRDA_HandleTypeDef
*hirda
)
1501 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1502 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
1503 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1505 /* Disable the IRDA DMA Rx request if enabled */
1506 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1508 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1510 /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
1511 if (hirda
->hdmarx
!= NULL
)
1513 /* Set the IRDA DMA Abort callback to Null.
1514 No call back execution at end of DMA abort procedure */
1515 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1517 if (HAL_DMA_Abort(hirda
->hdmarx
) != HAL_OK
)
1519 if (HAL_DMA_GetError(hirda
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1521 /* Set error code to DMA */
1522 hirda
->ErrorCode
= HAL_IRDA_ERROR_DMA
;
1530 /* Reset Rx transfer counter */
1531 hirda
->RxXferCount
= 0U;
1533 /* Clear the Error flags in the ICR register */
1534 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1536 /* Restore hirda->RxState to Ready */
1537 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1543 * @brief Abort ongoing transfers (Interrupt mode).
1544 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1545 * the configuration information for the specified UART module.
1546 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1547 * This procedure performs following operations :
1548 * - Disable IRDA Interrupts (Tx and Rx)
1549 * - Disable the DMA transfer in the peripheral register (if enabled)
1550 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1551 * - Set handle State to READY
1552 * - At abort completion, call user abort complete callback
1553 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1554 * considered as completed only when user abort complete callback is executed (not when exiting function).
1555 * @retval HAL status
1557 HAL_StatusTypeDef
HAL_IRDA_Abort_IT(IRDA_HandleTypeDef
*hirda
)
1559 uint32_t abortcplt
= 1U;
1561 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1562 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1563 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1565 /* If DMA Tx and/or DMA Rx Handles are associated to IRDA Handle, DMA Abort complete callbacks should be initialised
1566 before any call to DMA Abort functions */
1567 /* DMA Tx Handle is valid */
1568 if (hirda
->hdmatx
!= NULL
)
1570 /* Set DMA Abort Complete callback if IRDA DMA Tx request if enabled.
1571 Otherwise, set it to NULL */
1572 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1574 hirda
->hdmatx
->XferAbortCallback
= IRDA_DMATxAbortCallback
;
1578 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1581 /* DMA Rx Handle is valid */
1582 if (hirda
->hdmarx
!= NULL
)
1584 /* Set DMA Abort Complete callback if IRDA DMA Rx request if enabled.
1585 Otherwise, set it to NULL */
1586 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1588 hirda
->hdmarx
->XferAbortCallback
= IRDA_DMARxAbortCallback
;
1592 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1596 /* Disable the IRDA DMA Tx request if enabled */
1597 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1599 /* Disable DMA Tx at UART level */
1600 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1602 /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
1603 if (hirda
->hdmatx
!= NULL
)
1605 /* IRDA Tx DMA Abort callback has already been initialised :
1606 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1609 if (HAL_DMA_Abort_IT(hirda
->hdmatx
) != HAL_OK
)
1611 hirda
->hdmatx
->XferAbortCallback
= NULL
;
1620 /* Disable the IRDA DMA Rx request if enabled */
1621 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1623 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1625 /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
1626 if (hirda
->hdmarx
!= NULL
)
1628 /* IRDA Rx DMA Abort callback has already been initialised :
1629 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1632 if (HAL_DMA_Abort_IT(hirda
->hdmarx
) != HAL_OK
)
1634 hirda
->hdmarx
->XferAbortCallback
= NULL
;
1644 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1645 if (abortcplt
== 1U)
1647 /* Reset Tx and Rx transfer counters */
1648 hirda
->TxXferCount
= 0U;
1649 hirda
->RxXferCount
= 0U;
1651 /* Reset errorCode */
1652 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1654 /* Clear the Error flags in the ICR register */
1655 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1657 /* Restore hirda->gState and hirda->RxState to Ready */
1658 hirda
->gState
= HAL_IRDA_STATE_READY
;
1659 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1661 /* As no DMA to be aborted, call directly user Abort complete callback */
1662 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1663 /* Call registered Abort complete callback */
1664 hirda
->AbortCpltCallback(hirda
);
1666 /* Call legacy weak Abort complete callback */
1667 HAL_IRDA_AbortCpltCallback(hirda
);
1668 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1675 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1676 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1677 * the configuration information for the specified UART module.
1678 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1679 * This procedure performs following operations :
1680 * - Disable IRDA Interrupts (Tx)
1681 * - Disable the DMA transfer in the peripheral register (if enabled)
1682 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1683 * - Set handle State to READY
1684 * - At abort completion, call user abort complete callback
1685 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1686 * considered as completed only when user abort complete callback is executed (not when exiting function).
1687 * @retval HAL status
1689 HAL_StatusTypeDef
HAL_IRDA_AbortTransmit_IT(IRDA_HandleTypeDef
*hirda
)
1691 /* Disable TXEIE and TCIE interrupts */
1692 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1694 /* Disable the IRDA DMA Tx request if enabled */
1695 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
1697 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
1699 /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
1700 if (hirda
->hdmatx
!= NULL
)
1702 /* Set the IRDA DMA Abort callback :
1703 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1704 hirda
->hdmatx
->XferAbortCallback
= IRDA_DMATxOnlyAbortCallback
;
1707 if (HAL_DMA_Abort_IT(hirda
->hdmatx
) != HAL_OK
)
1709 /* Call Directly hirda->hdmatx->XferAbortCallback function in case of error */
1710 hirda
->hdmatx
->XferAbortCallback(hirda
->hdmatx
);
1715 /* Reset Tx transfer counter */
1716 hirda
->TxXferCount
= 0U;
1718 /* Restore hirda->gState to Ready */
1719 hirda
->gState
= HAL_IRDA_STATE_READY
;
1721 /* As no DMA to be aborted, call directly user Abort complete callback */
1722 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1723 /* Call registered Abort Transmit Complete Callback */
1724 hirda
->AbortTransmitCpltCallback(hirda
);
1726 /* Call legacy weak Abort Transmit Complete Callback */
1727 HAL_IRDA_AbortTransmitCpltCallback(hirda
);
1728 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1733 /* Reset Tx transfer counter */
1734 hirda
->TxXferCount
= 0U;
1736 /* Restore hirda->gState to Ready */
1737 hirda
->gState
= HAL_IRDA_STATE_READY
;
1739 /* As no DMA to be aborted, call directly user Abort complete callback */
1740 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1741 /* Call registered Abort Transmit Complete Callback */
1742 hirda
->AbortTransmitCpltCallback(hirda
);
1744 /* Call legacy weak Abort Transmit Complete Callback */
1745 HAL_IRDA_AbortTransmitCpltCallback(hirda
);
1746 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1753 * @brief Abort ongoing Receive transfer (Interrupt mode).
1754 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1755 * the configuration information for the specified UART module.
1756 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1757 * This procedure performs following operations :
1758 * - Disable IRDA Interrupts (Rx)
1759 * - Disable the DMA transfer in the peripheral register (if enabled)
1760 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1761 * - Set handle State to READY
1762 * - At abort completion, call user abort complete callback
1763 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1764 * considered as completed only when user abort complete callback is executed (not when exiting function).
1765 * @retval HAL status
1767 HAL_StatusTypeDef
HAL_IRDA_AbortReceive_IT(IRDA_HandleTypeDef
*hirda
)
1769 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1770 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
1771 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
1773 /* Disable the IRDA DMA Rx request if enabled */
1774 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1776 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1778 /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
1779 if (hirda
->hdmarx
!= NULL
)
1781 /* Set the IRDA DMA Abort callback :
1782 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1783 hirda
->hdmarx
->XferAbortCallback
= IRDA_DMARxOnlyAbortCallback
;
1786 if (HAL_DMA_Abort_IT(hirda
->hdmarx
) != HAL_OK
)
1788 /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
1789 hirda
->hdmarx
->XferAbortCallback(hirda
->hdmarx
);
1794 /* Reset Rx transfer counter */
1795 hirda
->RxXferCount
= 0U;
1797 /* Clear the Error flags in the ICR register */
1798 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1800 /* Restore hirda->RxState to Ready */
1801 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1803 /* As no DMA to be aborted, call directly user Abort complete callback */
1804 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1805 /* Call registered Abort Receive Complete Callback */
1806 hirda
->AbortReceiveCpltCallback(hirda
);
1808 /* Call legacy weak Abort Receive Complete Callback */
1809 HAL_IRDA_AbortReceiveCpltCallback(hirda
);
1810 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1815 /* Reset Rx transfer counter */
1816 hirda
->RxXferCount
= 0U;
1818 /* Clear the Error flags in the ICR register */
1819 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
1821 /* Restore hirda->RxState to Ready */
1822 hirda
->RxState
= HAL_IRDA_STATE_READY
;
1824 /* As no DMA to be aborted, call directly user Abort complete callback */
1825 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1826 /* Call registered Abort Receive Complete Callback */
1827 hirda
->AbortReceiveCpltCallback(hirda
);
1829 /* Call legacy weak Abort Receive Complete Callback */
1830 HAL_IRDA_AbortReceiveCpltCallback(hirda
);
1831 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1838 * @brief Handle IRDA interrupt request.
1839 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1840 * the configuration information for the specified IRDA module.
1843 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef
*hirda
)
1845 uint32_t isrflags
= READ_REG(hirda
->Instance
->ISR
);
1846 uint32_t cr1its
= READ_REG(hirda
->Instance
->CR1
);
1848 uint32_t errorflags
;
1851 /* If no error occurs */
1852 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
));
1853 if (errorflags
== 0U)
1855 /* IRDA in mode Receiver ---------------------------------------------------*/
1856 if (((isrflags
& USART_ISR_RXNE_RXFNE
) != 0U) && ((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U))
1858 IRDA_Receive_IT(hirda
);
1863 /* If some errors occur */
1864 cr3its
= READ_REG(hirda
->Instance
->CR3
);
1865 if ((errorflags
!= 0U)
1866 && (((cr3its
& USART_CR3_EIE
) != 0U)
1867 || ((cr1its
& (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
)) != 0U)))
1869 /* IRDA parity error interrupt occurred -------------------------------------*/
1870 if (((isrflags
& USART_ISR_PE
) != 0U) && ((cr1its
& USART_CR1_PEIE
) != 0U))
1872 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_PEF
);
1874 hirda
->ErrorCode
|= HAL_IRDA_ERROR_PE
;
1877 /* IRDA frame error interrupt occurred --------------------------------------*/
1878 if (((isrflags
& USART_ISR_FE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
1880 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_FEF
);
1882 hirda
->ErrorCode
|= HAL_IRDA_ERROR_FE
;
1885 /* IRDA noise error interrupt occurred --------------------------------------*/
1886 if (((isrflags
& USART_ISR_NE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
1888 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_NEF
);
1890 hirda
->ErrorCode
|= HAL_IRDA_ERROR_NE
;
1893 /* IRDA Over-Run interrupt occurred -----------------------------------------*/
1894 if (((isrflags
& USART_ISR_ORE
) != 0U) &&
1895 (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U) || ((cr3its
& USART_CR3_EIE
) != 0U)))
1897 __HAL_IRDA_CLEAR_IT(hirda
, IRDA_CLEAR_OREF
);
1899 hirda
->ErrorCode
|= HAL_IRDA_ERROR_ORE
;
1902 /* Call IRDA Error Call back function if need be --------------------------*/
1903 if (hirda
->ErrorCode
!= HAL_IRDA_ERROR_NONE
)
1905 /* IRDA in mode Receiver ---------------------------------------------------*/
1906 if (((isrflags
& USART_ISR_RXNE_RXFNE
) != 0U) && ((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U))
1908 IRDA_Receive_IT(hirda
);
1911 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1912 consider error as blocking */
1913 errorcode
= hirda
->ErrorCode
;
1914 if ((HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
)) ||
1915 ((errorcode
& HAL_IRDA_ERROR_ORE
) != 0U))
1917 /* Blocking error : transfer is aborted
1918 Set the IRDA state ready to be able to start again the process,
1919 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1920 IRDA_EndRxTransfer(hirda
);
1922 /* Disable the IRDA DMA Rx request if enabled */
1923 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
1925 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
1927 /* Abort the IRDA DMA Rx channel */
1928 if (hirda
->hdmarx
!= NULL
)
1930 /* Set the IRDA DMA Abort callback :
1931 will lead to call HAL_IRDA_ErrorCallback() at end of DMA abort procedure */
1932 hirda
->hdmarx
->XferAbortCallback
= IRDA_DMAAbortOnError
;
1935 if (HAL_DMA_Abort_IT(hirda
->hdmarx
) != HAL_OK
)
1937 /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
1938 hirda
->hdmarx
->XferAbortCallback(hirda
->hdmarx
);
1943 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1944 /* Call registered user error callback */
1945 hirda
->ErrorCallback(hirda
);
1947 /* Call legacy weak user error callback */
1948 HAL_IRDA_ErrorCallback(hirda
);
1949 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1954 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1955 /* Call registered user error callback */
1956 hirda
->ErrorCallback(hirda
);
1958 /* Call legacy weak user error callback */
1959 HAL_IRDA_ErrorCallback(hirda
);
1960 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1965 /* Non Blocking error : transfer could go on.
1966 Error is notified to user through user error callback */
1967 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1968 /* Call registered user error callback */
1969 hirda
->ErrorCallback(hirda
);
1971 /* Call legacy weak user error callback */
1972 HAL_IRDA_ErrorCallback(hirda
);
1973 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1974 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
1979 } /* End if some error occurs */
1981 /* IRDA in mode Transmitter ------------------------------------------------*/
1982 if (((isrflags
& USART_ISR_TXE_TXFNF
) != 0U) && ((cr1its
& USART_CR1_TXEIE_TXFNFIE
) != 0U))
1984 IRDA_Transmit_IT(hirda
);
1988 /* IRDA in mode Transmitter (transmission end) -----------------------------*/
1989 if (((isrflags
& USART_ISR_TC
) != 0U) && ((cr1its
& USART_CR1_TCIE
) != 0U))
1991 IRDA_EndTransmit_IT(hirda
);
1998 * @brief Tx Transfer completed callback.
1999 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2000 * the configuration information for the specified IRDA module.
2003 __weak
void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef
*hirda
)
2005 /* Prevent unused argument(s) compilation warning */
2008 /* NOTE : This function should not be modified, when the callback is needed,
2009 the HAL_IRDA_TxCpltCallback can be implemented in the user file.
2014 * @brief Tx Half Transfer completed callback.
2015 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2016 * the configuration information for the specified USART module.
2019 __weak
void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef
*hirda
)
2021 /* Prevent unused argument(s) compilation warning */
2024 /* NOTE : This function should not be modified, when the callback is needed,
2025 the HAL_IRDA_TxHalfCpltCallback can be implemented in the user file.
2030 * @brief Rx Transfer completed callback.
2031 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2032 * the configuration information for the specified IRDA module.
2035 __weak
void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef
*hirda
)
2037 /* Prevent unused argument(s) compilation warning */
2040 /* NOTE : This function should not be modified, when the callback is needed,
2041 the HAL_IRDA_RxCpltCallback can be implemented in the user file.
2046 * @brief Rx Half Transfer complete callback.
2047 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2048 * the configuration information for the specified IRDA module.
2051 __weak
void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef
*hirda
)
2053 /* Prevent unused argument(s) compilation warning */
2056 /* NOTE : This function should not be modified, when the callback is needed,
2057 the HAL_IRDA_RxHalfCpltCallback can be implemented in the user file.
2062 * @brief IRDA error callback.
2063 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2064 * the configuration information for the specified IRDA module.
2067 __weak
void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef
*hirda
)
2069 /* Prevent unused argument(s) compilation warning */
2072 /* NOTE : This function should not be modified, when the callback is needed,
2073 the HAL_IRDA_ErrorCallback can be implemented in the user file.
2078 * @brief IRDA Abort Complete callback.
2079 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2080 * the configuration information for the specified IRDA module.
2083 __weak
void HAL_IRDA_AbortCpltCallback(IRDA_HandleTypeDef
*hirda
)
2085 /* Prevent unused argument(s) compilation warning */
2088 /* NOTE : This function should not be modified, when the callback is needed,
2089 the HAL_IRDA_AbortCpltCallback can be implemented in the user file.
2094 * @brief IRDA Abort Complete callback.
2095 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2096 * the configuration information for the specified IRDA module.
2099 __weak
void HAL_IRDA_AbortTransmitCpltCallback(IRDA_HandleTypeDef
*hirda
)
2101 /* Prevent unused argument(s) compilation warning */
2104 /* NOTE : This function should not be modified, when the callback is needed,
2105 the HAL_IRDA_AbortTransmitCpltCallback can be implemented in the user file.
2110 * @brief IRDA Abort Receive Complete callback.
2111 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2112 * the configuration information for the specified IRDA module.
2115 __weak
void HAL_IRDA_AbortReceiveCpltCallback(IRDA_HandleTypeDef
*hirda
)
2117 /* Prevent unused argument(s) compilation warning */
2120 /* NOTE : This function should not be modified, when the callback is needed,
2121 the HAL_IRDA_AbortReceiveCpltCallback can be implemented in the user file.
2129 /** @defgroup IRDA_Exported_Functions_Group4 Peripheral State and Error functions
2130 * @brief IRDA State and Errors functions
2133 ==============================================================================
2134 ##### Peripheral State and Error functions #####
2135 ==============================================================================
2137 This subsection provides a set of functions allowing to return the State of IrDA
2138 communication process and also return Peripheral Errors occurred during communication process
2139 (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state
2140 of the IRDA peripheral handle.
2141 (+) HAL_IRDA_GetError() checks in run-time errors that could occur during
2149 * @brief Return the IRDA handle state.
2150 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2151 * the configuration information for the specified IRDA module.
2154 HAL_IRDA_StateTypeDef
HAL_IRDA_GetState(IRDA_HandleTypeDef
*hirda
)
2156 /* Return IRDA handle state */
2159 temp1
= (uint32_t)hirda
->gState
;
2160 temp2
= (uint32_t)hirda
->RxState
;
2162 return (HAL_IRDA_StateTypeDef
)(temp1
| temp2
);
2166 * @brief Return the IRDA handle error code.
2167 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2168 * the configuration information for the specified IRDA module.
2169 * @retval IRDA Error Code
2171 uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef
*hirda
)
2173 return hirda
->ErrorCode
;
2184 /** @defgroup IRDA_Private_Functions IRDA Private Functions
2188 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2190 * @brief Initialize the callbacks to their default values.
2191 * @param hirda IRDA handle.
2194 void IRDA_InitCallbacksToDefault(IRDA_HandleTypeDef
*hirda
)
2196 /* Init the IRDA Callback settings */
2197 hirda
->TxHalfCpltCallback
= HAL_IRDA_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
2198 hirda
->TxCpltCallback
= HAL_IRDA_TxCpltCallback
; /* Legacy weak TxCpltCallback */
2199 hirda
->RxHalfCpltCallback
= HAL_IRDA_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
2200 hirda
->RxCpltCallback
= HAL_IRDA_RxCpltCallback
; /* Legacy weak RxCpltCallback */
2201 hirda
->ErrorCallback
= HAL_IRDA_ErrorCallback
; /* Legacy weak ErrorCallback */
2202 hirda
->AbortCpltCallback
= HAL_IRDA_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
2203 hirda
->AbortTransmitCpltCallback
= HAL_IRDA_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
2204 hirda
->AbortReceiveCpltCallback
= HAL_IRDA_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
2207 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
2210 * @brief Configure the IRDA peripheral.
2211 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2212 * the configuration information for the specified IRDA module.
2213 * @retval HAL status
2215 static HAL_StatusTypeDef
IRDA_SetConfig(IRDA_HandleTypeDef
*hirda
)
2218 IRDA_ClockSourceTypeDef clocksource
;
2219 HAL_StatusTypeDef ret
= HAL_OK
;
2220 const uint16_t IRDAPrescTable
[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
2222 /* Check the communication parameters */
2223 assert_param(IS_IRDA_BAUDRATE(hirda
->Init
.BaudRate
));
2224 assert_param(IS_IRDA_WORD_LENGTH(hirda
->Init
.WordLength
));
2225 assert_param(IS_IRDA_PARITY(hirda
->Init
.Parity
));
2226 assert_param(IS_IRDA_TX_RX_MODE(hirda
->Init
.Mode
));
2227 assert_param(IS_IRDA_PRESCALER(hirda
->Init
.Prescaler
));
2228 assert_param(IS_IRDA_POWERMODE(hirda
->Init
.PowerMode
));
2229 assert_param(IS_IRDA_CLOCKPRESCALER(hirda
->Init
.ClockPrescaler
));
2231 /*-------------------------- USART CR1 Configuration -----------------------*/
2232 /* Configure the IRDA Word Length, Parity and transfer Mode:
2233 Set the M bits according to hirda->Init.WordLength value
2234 Set PCE and PS bits according to hirda->Init.Parity value
2235 Set TE and RE bits according to hirda->Init.Mode value */
2236 tmpreg
= (uint32_t)hirda
->Init
.WordLength
| hirda
->Init
.Parity
| hirda
->Init
.Mode
;
2238 MODIFY_REG(hirda
->Instance
->CR1
, IRDA_CR1_FIELDS
, tmpreg
);
2240 /*-------------------------- USART CR3 Configuration -----------------------*/
2241 MODIFY_REG(hirda
->Instance
->CR3
, USART_CR3_IRLP
, hirda
->Init
.PowerMode
);
2243 /*--------------------- USART clock PRESC Configuration ----------------*/
2245 * - IRDA Clock Prescaler: set PRESCALER according to hirda->Init.ClockPrescaler value */
2246 MODIFY_REG(hirda
->Instance
->PRESC
, USART_PRESC_PRESCALER
, hirda
->Init
.ClockPrescaler
);
2248 /*-------------------------- USART GTPR Configuration ----------------------*/
2249 MODIFY_REG(hirda
->Instance
->GTPR
, (uint16_t)USART_GTPR_PSC
, hirda
->Init
.Prescaler
);
2251 /*-------------------------- USART BRR Configuration -----------------------*/
2252 IRDA_GETCLOCKSOURCE(hirda
, clocksource
);
2254 switch (clocksource
)
2256 case IRDA_CLOCKSOURCE_PCLK1
:
2257 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2259 case IRDA_CLOCKSOURCE_PCLK2
:
2260 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2262 case IRDA_CLOCKSOURCE_HSI
:
2263 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16(HSI_VALUE
, hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2265 case IRDA_CLOCKSOURCE_SYSCLK
:
2266 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2268 case IRDA_CLOCKSOURCE_LSE
:
2269 tmpreg
= (uint16_t)(IRDA_DIV_SAMPLING16((uint32_t)LSE_VALUE
, hirda
->Init
.BaudRate
, hirda
->Init
.ClockPrescaler
));
2276 /* USARTDIV must be greater than or equal to 0d16 */
2277 if ((tmpreg
>= USART_BRR_MIN
) && (tmpreg
<= USART_BRR_MAX
))
2279 hirda
->Instance
->BRR
= tmpreg
;
2290 * @brief Check the IRDA Idle State.
2291 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2292 * the configuration information for the specified IRDA module.
2293 * @retval HAL status
2295 static HAL_StatusTypeDef
IRDA_CheckIdleState(IRDA_HandleTypeDef
*hirda
)
2299 /* Initialize the IRDA ErrorCode */
2300 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
2302 /* Init tickstart for timeout managment*/
2303 tickstart
= HAL_GetTick();
2305 /* Check if the Transmitter is enabled */
2306 if ((hirda
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
2308 /* Wait until TEACK flag is set */
2309 if (IRDA_WaitOnFlagUntilTimeout(hirda
, USART_ISR_TEACK
, RESET
, tickstart
, IRDA_TEACK_REACK_TIMEOUT
) != HAL_OK
)
2311 /* Timeout occurred */
2315 /* Check if the Receiver is enabled */
2316 if ((hirda
->Instance
->CR1
& USART_CR1_RE
) == USART_CR1_RE
)
2318 /* Wait until REACK flag is set */
2319 if (IRDA_WaitOnFlagUntilTimeout(hirda
, USART_ISR_REACK
, RESET
, tickstart
, IRDA_TEACK_REACK_TIMEOUT
) != HAL_OK
)
2321 /* Timeout occurred */
2326 /* Initialize the IRDA state*/
2327 hirda
->gState
= HAL_IRDA_STATE_READY
;
2328 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2330 /* Process Unlocked */
2331 __HAL_UNLOCK(hirda
);
2337 * @brief Handle IRDA Communication Timeout.
2338 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2339 * the configuration information for the specified IRDA module.
2340 * @param Flag Specifies the IRDA flag to check.
2341 * @param Status Flag status (SET or RESET)
2342 * @param Tickstart Tick start value
2343 * @param Timeout Timeout duration
2344 * @retval HAL status
2346 static HAL_StatusTypeDef
IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef
*hirda
, uint32_t Flag
, FlagStatus Status
,
2347 uint32_t Tickstart
, uint32_t Timeout
)
2349 /* Wait until flag is set */
2350 while ((__HAL_IRDA_GET_FLAG(hirda
, Flag
) ? SET
: RESET
) == Status
)
2352 /* Check for the Timeout */
2353 if (Timeout
!= HAL_MAX_DELAY
)
2355 if (((HAL_GetTick() - Tickstart
) > Timeout
) || (Timeout
== 0U))
2357 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
2358 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
));
2359 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
2361 hirda
->gState
= HAL_IRDA_STATE_READY
;
2362 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2364 /* Process Unlocked */
2365 __HAL_UNLOCK(hirda
);
2375 * @brief End ongoing Tx transfer on IRDA peripheral (following error detection or Transmit completion).
2376 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2377 * the configuration information for the specified IRDA module.
2380 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef
*hirda
)
2382 /* Disable TXEIE and TCIE interrupts */
2383 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
2385 /* At end of Tx process, restore hirda->gState to Ready */
2386 hirda
->gState
= HAL_IRDA_STATE_READY
;
2391 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2392 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2393 * the configuration information for the specified IRDA module.
2396 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef
*hirda
)
2398 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2399 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
2400 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
2402 /* At end of Rx process, restore hirda->RxState to Ready */
2403 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2408 * @brief DMA IRDA transmit process complete callback.
2409 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2410 * the configuration information for the specified DMA module.
2413 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2415 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2417 /* DMA Normal mode */
2418 if (HAL_IS_BIT_CLR(hdma
->Instance
->CCR
, DMA_CCR_CIRC
))
2420 hirda
->TxXferCount
= 0U;
2422 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2423 in the IRDA CR3 register */
2424 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAT
);
2426 /* Enable the IRDA Transmit Complete Interrupt */
2427 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
2429 /* DMA Circular mode */
2432 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2433 /* Call registered Tx complete callback */
2434 hirda
->TxCpltCallback(hirda
);
2436 /* Call legacy weak Tx complete callback */
2437 HAL_IRDA_TxCpltCallback(hirda
);
2438 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2444 * @brief DMA IRDA transmit process half complete callback.
2445 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2446 * the configuration information for the specified DMA module.
2449 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef
*hdma
)
2451 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2453 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2454 /* Call registered Tx Half complete callback */
2455 hirda
->TxHalfCpltCallback(hirda
);
2457 /* Call legacy weak Tx complete callback */
2458 HAL_IRDA_TxHalfCpltCallback(hirda
);
2459 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2463 * @brief DMA IRDA receive process complete callback.
2464 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2465 * the configuration information for the specified DMA module.
2468 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
2470 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2472 /* DMA Normal mode */
2473 if (HAL_IS_BIT_CLR(hdma
->Instance
->CCR
, DMA_CCR_CIRC
))
2475 hirda
->RxXferCount
= 0U;
2477 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2478 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_PEIE
);
2479 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
2481 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2482 in the IRDA CR3 register */
2483 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_DMAR
);
2485 /* At end of Rx process, restore hirda->RxState to Ready */
2486 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2489 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2490 /* Call registered Rx complete callback */
2491 hirda
->RxCpltCallback(hirda
);
2493 /* Call legacy weak Rx complete callback */
2494 HAL_IRDA_RxCpltCallback(hirda
);
2495 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
2499 * @brief DMA IRDA receive process half complete callback.
2500 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2501 * the configuration information for the specified DMA module.
2504 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef
*hdma
)
2506 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2508 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2509 /*Call registered Rx Half complete callback*/
2510 hirda
->RxHalfCpltCallback(hirda
);
2512 /* Call legacy weak Rx Half complete callback */
2513 HAL_IRDA_RxHalfCpltCallback(hirda
);
2514 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2518 * @brief DMA IRDA communication error callback.
2519 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2520 * the configuration information for the specified DMA module.
2523 static void IRDA_DMAError(DMA_HandleTypeDef
*hdma
)
2525 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2527 /* Stop IRDA DMA Tx request if ongoing */
2528 if (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
2530 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAT
))
2532 hirda
->TxXferCount
= 0U;
2533 IRDA_EndTxTransfer(hirda
);
2537 /* Stop IRDA DMA Rx request if ongoing */
2538 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
2540 if (HAL_IS_BIT_SET(hirda
->Instance
->CR3
, USART_CR3_DMAR
))
2542 hirda
->RxXferCount
= 0U;
2543 IRDA_EndRxTransfer(hirda
);
2547 hirda
->ErrorCode
|= HAL_IRDA_ERROR_DMA
;
2548 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2549 /* Call registered user error callback */
2550 hirda
->ErrorCallback(hirda
);
2552 /* Call legacy weak user error callback */
2553 HAL_IRDA_ErrorCallback(hirda
);
2554 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2558 * @brief DMA IRDA communication abort callback, when initiated by HAL services on Error
2559 * (To be called at end of DMA Abort procedure following error occurrence).
2560 * @param hdma DMA handle.
2563 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
2565 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2566 hirda
->RxXferCount
= 0U;
2567 hirda
->TxXferCount
= 0U;
2569 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2570 /* Call registered user error callback */
2571 hirda
->ErrorCallback(hirda
);
2573 /* Call legacy weak user error callback */
2574 HAL_IRDA_ErrorCallback(hirda
);
2575 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2579 * @brief DMA IRDA Tx communication abort callback, when initiated by user
2580 * (To be called at end of DMA Tx Abort procedure following user abort request).
2581 * @note When this callback is executed, User Abort complete call back is called only if no
2582 * Abort still ongoing for Rx DMA Handle.
2583 * @param hdma DMA handle.
2586 static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
2588 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2590 hirda
->hdmatx
->XferAbortCallback
= NULL
;
2592 /* Check if an Abort process is still ongoing */
2593 if (hirda
->hdmarx
!= NULL
)
2595 if (hirda
->hdmarx
->XferAbortCallback
!= NULL
)
2601 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2602 hirda
->TxXferCount
= 0U;
2603 hirda
->RxXferCount
= 0U;
2605 /* Reset errorCode */
2606 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
2608 /* Clear the Error flags in the ICR register */
2609 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
2611 /* Restore hirda->gState and hirda->RxState to Ready */
2612 hirda
->gState
= HAL_IRDA_STATE_READY
;
2613 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2615 /* Call user Abort complete callback */
2616 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2617 /* Call registered Abort complete callback */
2618 hirda
->AbortCpltCallback(hirda
);
2620 /* Call legacy weak Abort complete callback */
2621 HAL_IRDA_AbortCpltCallback(hirda
);
2622 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2627 * @brief DMA IRDA Rx communication abort callback, when initiated by user
2628 * (To be called at end of DMA Rx Abort procedure following user abort request).
2629 * @note When this callback is executed, User Abort complete call back is called only if no
2630 * Abort still ongoing for Tx DMA Handle.
2631 * @param hdma DMA handle.
2634 static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
2636 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2638 hirda
->hdmarx
->XferAbortCallback
= NULL
;
2640 /* Check if an Abort process is still ongoing */
2641 if (hirda
->hdmatx
!= NULL
)
2643 if (hirda
->hdmatx
->XferAbortCallback
!= NULL
)
2649 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2650 hirda
->TxXferCount
= 0U;
2651 hirda
->RxXferCount
= 0U;
2653 /* Reset errorCode */
2654 hirda
->ErrorCode
= HAL_IRDA_ERROR_NONE
;
2656 /* Clear the Error flags in the ICR register */
2657 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
2659 /* Restore hirda->gState and hirda->RxState to Ready */
2660 hirda
->gState
= HAL_IRDA_STATE_READY
;
2661 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2663 /* Call user Abort complete callback */
2664 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2665 /* Call registered Abort complete callback */
2666 hirda
->AbortCpltCallback(hirda
);
2668 /* Call legacy weak Abort complete callback */
2669 HAL_IRDA_AbortCpltCallback(hirda
);
2670 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2675 * @brief DMA IRDA Tx communication abort callback, when initiated by user by a call to
2676 * HAL_IRDA_AbortTransmit_IT API (Abort only Tx transfer)
2677 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2678 * and leads to user Tx Abort Complete callback execution).
2679 * @param hdma DMA handle.
2682 static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2684 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)(hdma
->Parent
);
2686 hirda
->TxXferCount
= 0U;
2688 /* Restore hirda->gState to Ready */
2689 hirda
->gState
= HAL_IRDA_STATE_READY
;
2691 /* Call user Abort complete callback */
2692 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2693 /* Call registered Abort Transmit Complete Callback */
2694 hirda
->AbortTransmitCpltCallback(hirda
);
2696 /* Call legacy weak Abort Transmit Complete Callback */
2697 HAL_IRDA_AbortTransmitCpltCallback(hirda
);
2698 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2702 * @brief DMA IRDA Rx communication abort callback, when initiated by user by a call to
2703 * HAL_IRDA_AbortReceive_IT API (Abort only Rx transfer)
2704 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2705 * and leads to user Rx Abort Complete callback execution).
2706 * @param hdma DMA handle.
2709 static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2711 IRDA_HandleTypeDef
*hirda
= (IRDA_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
2713 hirda
->RxXferCount
= 0U;
2715 /* Clear the Error flags in the ICR register */
2716 __HAL_IRDA_CLEAR_FLAG(hirda
, IRDA_CLEAR_OREF
| IRDA_CLEAR_NEF
| IRDA_CLEAR_PEF
| IRDA_CLEAR_FEF
);
2718 /* Restore hirda->RxState to Ready */
2719 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2721 /* Call user Abort complete callback */
2722 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2723 /* Call registered Abort Receive Complete Callback */
2724 hirda
->AbortReceiveCpltCallback(hirda
);
2726 /* Call legacy weak Abort Receive Complete Callback */
2727 HAL_IRDA_AbortReceiveCpltCallback(hirda
);
2728 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2732 * @brief Send an amount of data in interrupt mode.
2733 * @note Function is called under interruption only, once
2734 * interruptions have been enabled by HAL_IRDA_Transmit_IT().
2735 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2736 * the configuration information for the specified IRDA module.
2739 static void IRDA_Transmit_IT(IRDA_HandleTypeDef
*hirda
)
2743 /* Check that a Tx process is ongoing */
2744 if (hirda
->gState
== HAL_IRDA_STATE_BUSY_TX
)
2746 if (hirda
->TxXferCount
== 0U)
2748 /* Disable the IRDA Transmit Data Register Empty Interrupt */
2749 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
2751 /* Enable the IRDA Transmit Complete Interrupt */
2752 SET_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
2756 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
2758 tmp
= (uint16_t *) hirda
->pTxBuffPtr
; /* Derogation R.11.3 */
2759 hirda
->Instance
->TDR
= (uint16_t)(*tmp
& 0x01FFU
);
2760 hirda
->pTxBuffPtr
+= 2U;
2764 hirda
->Instance
->TDR
= (uint8_t)(*hirda
->pTxBuffPtr
& 0xFFU
);
2765 hirda
->pTxBuffPtr
++;
2767 hirda
->TxXferCount
--;
2773 * @brief Wrap up transmission in non-blocking mode.
2774 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2775 * the configuration information for the specified IRDA module.
2778 static void IRDA_EndTransmit_IT(IRDA_HandleTypeDef
*hirda
)
2780 /* Disable the IRDA Transmit Complete Interrupt */
2781 CLEAR_BIT(hirda
->Instance
->CR1
, USART_CR1_TCIE
);
2783 /* Tx process is ended, restore hirda->gState to Ready */
2784 hirda
->gState
= HAL_IRDA_STATE_READY
;
2786 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2787 /* Call registered Tx complete callback */
2788 hirda
->TxCpltCallback(hirda
);
2790 /* Call legacy weak Tx complete callback */
2791 HAL_IRDA_TxCpltCallback(hirda
);
2792 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2796 * @brief Receive an amount of data in interrupt mode.
2797 * @note Function is called under interruption only, once
2798 * interruptions have been enabled by HAL_IRDA_Receive_IT()
2799 * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2800 * the configuration information for the specified IRDA module.
2803 static void IRDA_Receive_IT(IRDA_HandleTypeDef
*hirda
)
2806 uint16_t uhMask
= hirda
->Mask
;
2809 /* Check that a Rx process is ongoing */
2810 if (hirda
->RxState
== HAL_IRDA_STATE_BUSY_RX
)
2812 uhdata
= (uint16_t) READ_REG(hirda
->Instance
->RDR
);
2813 if ((hirda
->Init
.WordLength
== IRDA_WORDLENGTH_9B
) && (hirda
->Init
.Parity
== IRDA_PARITY_NONE
))
2815 tmp
= (uint16_t *) hirda
->pRxBuffPtr
; /* Derogation R.11.3 */
2816 *tmp
= (uint16_t)(uhdata
& uhMask
);
2817 hirda
->pRxBuffPtr
+= 2U;
2821 *hirda
->pRxBuffPtr
= (uint8_t)(uhdata
& (uint8_t)uhMask
);
2822 hirda
->pRxBuffPtr
++;
2825 hirda
->RxXferCount
--;
2826 if (hirda
->RxXferCount
== 0U)
2828 /* Disable the IRDA Parity Error Interrupt and RXNE interrupt */
2829 CLEAR_BIT(hirda
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
2831 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
2832 CLEAR_BIT(hirda
->Instance
->CR3
, USART_CR3_EIE
);
2834 /* Rx process is completed, restore hirda->RxState to Ready */
2835 hirda
->RxState
= HAL_IRDA_STATE_READY
;
2837 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2838 /* Call registered Rx complete callback */
2839 hirda
->RxCpltCallback(hirda
);
2841 /* Call legacy weak Rx complete callback */
2842 HAL_IRDA_RxCpltCallback(hirda
);
2843 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
2848 /* Clear RXNE interrupt flag */
2849 __HAL_IRDA_SEND_REQ(hirda
, IRDA_RXDATA_FLUSH_REQUEST
);
2857 #endif /* HAL_IRDA_MODULE_ENABLED */
2866 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/