1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_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
8   *          (IRDA)
9   *           + Initialization and de-initialization functions
10   *           + IO operation functions
11   *           + Peripheral State and Errors functions
12   *           + Peripheral Control functions
13   *
14   ******************************************************************************
15   * @attention
16   *
17   * Copyright (c) 2016 STMicroelectronics.
18   * All rights reserved.
19   *
20   * This software is licensed under terms that can be found in the LICENSE file
21   * in the root directory of this software component.
22   * If no LICENSE file comes with this software, it is provided AS-IS.
23   *
24   ******************************************************************************
25   @verbatim
26   ==============================================================================
27                         ##### How to use this driver #####
28   ==============================================================================
29   [..]
30     The IRDA HAL driver can be used as follows:
31 
32     (#) Declare a IRDA_HandleTypeDef handle structure (eg. IRDA_HandleTypeDef hirda).
33     (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API
34         in setting the associated USART or UART in IRDA mode:
35         (++) Enable the USARTx/UARTx interface clock.
36         (++) USARTx/UARTx pins configuration:
37             (+++) Enable the clock for the USARTx/UARTx GPIOs.
38             (+++) Configure these USARTx/UARTx pins (TX as alternate function pull-up, RX as alternate function Input).
39         (++) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
40              and HAL_IRDA_Receive_IT() APIs):
41             (+++) Configure the USARTx/UARTx interrupt priority.
42             (+++) Enable the NVIC USARTx/UARTx IRQ handle.
43             (+++) The specific IRDA interrupts (Transmission complete interrupt,
44                   RXNE interrupt and Error Interrupts) will be managed using the macros
45                   __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
46 
47         (++) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
48              and HAL_IRDA_Receive_DMA() APIs):
49             (+++) Declare a DMA handle structure for the Tx/Rx channel.
50             (+++) Enable the DMAx interface clock.
51             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
52             (+++) Configure the DMA Tx/Rx channel.
53             (+++) Associate the initialized DMA handle to the IRDA DMA Tx/Rx handle.
54             (+++) Configure the priority and enable the NVIC for the transfer
55                   complete interrupt on the DMA Tx/Rx channel.
56 
57     (#) Program the Baud Rate, Word Length and Parity and Mode(Receiver/Transmitter),
58         the normal or low power mode and the clock prescaler in the hirda handle Init structure.
59 
60     (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
61         (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
62              by calling the customized HAL_IRDA_MspInit() API.
63 
64          -@@- The specific IRDA interrupts (Transmission complete interrupt,
65              RXNE interrupt and Error Interrupts) will be managed using the macros
66              __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
67 
68     (#) Three operation modes are available within this driver :
69 
70      *** Polling mode IO operation ***
71      =================================
72      [..]
73        (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
74        (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
75 
76      *** Interrupt mode IO operation ***
77      ===================================
78      [..]
79        (+) Send an amount of data in non-blocking mode using HAL_IRDA_Transmit_IT()
80        (+) At transmission end of transfer HAL_IRDA_TxCpltCallback() is executed and user can
81             add his own code by customization of function pointer HAL_IRDA_TxCpltCallback()
82        (+) Receive an amount of data in non-blocking mode using HAL_IRDA_Receive_IT()
83        (+) At reception end of transfer HAL_IRDA_RxCpltCallback() is executed and user can
84             add his own code by customization of function pointer HAL_IRDA_RxCpltCallback()
85        (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
86             add his own code by customization of function pointer HAL_IRDA_ErrorCallback()
87 
88      *** DMA mode IO operation ***
89      ==============================
90      [..]
91        (+) Send an amount of data in non-blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
92        (+) At transmission half of transfer HAL_IRDA_TxHalfCpltCallback() is executed and user can
93             add his own code by customization of function pointer HAL_IRDA_TxHalfCpltCallback()
94        (+) At transmission end of transfer HAL_IRDA_TxCpltCallback() is executed and user can
95             add his own code by customization of function pointer HAL_IRDA_TxCpltCallback()
96        (+) Receive an amount of data in non-blocking mode (DMA) using HAL_IRDA_Receive_DMA()
97        (+) At reception half of transfer HAL_IRDA_RxHalfCpltCallback() is executed and user can
98             add his own code by customization of function pointer HAL_IRDA_RxHalfCpltCallback()
99        (+) At reception end of transfer HAL_IRDA_RxCpltCallback() is executed and user can
100             add his own code by customization of function pointer HAL_IRDA_RxCpltCallback()
101        (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
102             add his own code by customization of function pointer HAL_IRDA_ErrorCallback()
103 
104      *** IRDA HAL driver macros list ***
105      ====================================
106      [..]
107        Below the list of most used macros in IRDA HAL driver.
108 
109        (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
110        (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
111        (+) __HAL_IRDA_GET_FLAG : Check whether the specified IRDA flag is set or not
112        (+) __HAL_IRDA_CLEAR_FLAG : Clear the specified IRDA pending flag
113        (+) __HAL_IRDA_ENABLE_IT: Enable the specified IRDA interrupt
114        (+) __HAL_IRDA_DISABLE_IT: Disable the specified IRDA interrupt
115        (+) __HAL_IRDA_GET_IT_SOURCE: Check whether or not the specified IRDA interrupt is enabled
116 
117      [..]
118        (@) You can refer to the IRDA HAL driver header file for more useful macros
119 
120     ##### Callback registration #####
121     ==================================
122 
123     [..]
124     The compilation define USE_HAL_IRDA_REGISTER_CALLBACKS when set to 1
125     allows the user to configure dynamically the driver callbacks.
126 
127     [..]
128     Use Function HAL_IRDA_RegisterCallback() to register a user callback.
129     Function HAL_IRDA_RegisterCallback() allows to register following callbacks:
130     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
131     (+) TxCpltCallback            : Tx Complete Callback.
132     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
133     (+) RxCpltCallback            : Rx Complete Callback.
134     (+) ErrorCallback             : Error Callback.
135     (+) AbortCpltCallback         : Abort Complete Callback.
136     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
137     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
138     (+) MspInitCallback           : IRDA MspInit.
139     (+) MspDeInitCallback         : IRDA MspDeInit.
140     This function takes as parameters the HAL peripheral handle, the Callback ID
141     and a pointer to the user callback function.
142 
143     [..]
144     Use function HAL_IRDA_UnRegisterCallback() to reset a callback to the default
145     weak (surcharged) function.
146     HAL_IRDA_UnRegisterCallback() takes as parameters the HAL peripheral handle,
147     and the Callback ID.
148     This function allows to reset following callbacks:
149     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
150     (+) TxCpltCallback            : Tx Complete Callback.
151     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
152     (+) RxCpltCallback            : Rx Complete Callback.
153     (+) ErrorCallback             : Error Callback.
154     (+) AbortCpltCallback         : Abort Complete Callback.
155     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
156     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
157     (+) MspInitCallback           : IRDA MspInit.
158     (+) MspDeInitCallback         : IRDA MspDeInit.
159 
160     [..]
161     By default, after the HAL_IRDA_Init() and when the state is HAL_IRDA_STATE_RESET
162     all callbacks are set to the corresponding weak (surcharged) functions:
163     examples HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxHalfCpltCallback().
164     Exception done for MspInit and MspDeInit functions that are respectively
165     reset to the legacy weak (surcharged) functions in the HAL_IRDA_Init()
166     and HAL_IRDA_DeInit() only when these callbacks are null (not registered beforehand).
167     If not, MspInit or MspDeInit are not null, the HAL_IRDA_Init() and HAL_IRDA_DeInit()
168     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
169 
170     [..]
171     Callbacks can be registered/unregistered in HAL_IRDA_STATE_READY state only.
172     Exception done MspInit/MspDeInit that can be registered/unregistered
173     in HAL_IRDA_STATE_READY or HAL_IRDA_STATE_RESET state, thus registered (user)
174     MspInit/DeInit callbacks can be used during the Init/DeInit.
175     In that case first register the MspInit/MspDeInit user callbacks
176     using HAL_IRDA_RegisterCallback() before calling HAL_IRDA_DeInit()
177     or HAL_IRDA_Init() function.
178 
179     [..]
180     When The compilation define USE_HAL_IRDA_REGISTER_CALLBACKS is set to 0 or
181     not defined, the callback registration feature is not available
182     and weak (surcharged) callbacks are used.
183 
184   @endverbatim
185   ******************************************************************************
186   */
187 
188 /* Includes ------------------------------------------------------------------*/
189 #include "stm32f3xx_hal.h"
190 
191 /** @addtogroup STM32F3xx_HAL_Driver
192   * @{
193   */
194 
195 /** @defgroup IRDA IRDA
196   * @brief HAL IRDA module driver
197   * @{
198   */
199 
200 #ifdef HAL_IRDA_MODULE_ENABLED
201 
202 /* Private typedef -----------------------------------------------------------*/
203 /* Private define ------------------------------------------------------------*/
204 /** @defgroup IRDA_Private_Constants IRDA Private Constants
205   * @{
206   */
207 #define IRDA_TEACK_REACK_TIMEOUT            1000U                                   /*!< IRDA TX or RX enable acknowledge time-out value  */
208 
209 #define IRDA_CR1_FIELDS  ((uint32_t)(USART_CR1_M | USART_CR1_PCE \
210                                      | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE))  /*!< UART or USART CR1 fields of parameters set by IRDA_SetConfig API */
211 
212 #define USART_BRR_MIN    0x10U        /*!< USART BRR minimum authorized value */
213 
214 #define USART_BRR_MAX    0x0000FFFFU  /*!< USART BRR maximum authorized value */
215 /**
216   * @}
217   */
218 
219 /* Private macros ------------------------------------------------------------*/
220 /** @defgroup IRDA_Private_Macros IRDA Private Macros
221   * @{
222   */
223 /** @brief  BRR division operation to set BRR register in 16-bit oversampling mode.
224   * @param  __PCLK__ IRDA clock source.
225   * @param  __BAUD__ Baud rate set by the user.
226   * @retval Division result
227   */
228 #define IRDA_DIV_SAMPLING16(__PCLK__, __BAUD__)  (((__PCLK__) + ((__BAUD__)/2U)) / (__BAUD__))
229 /**
230   * @}
231   */
232 
233 /* Private variables ---------------------------------------------------------*/
234 /* Private function prototypes -----------------------------------------------*/
235 /** @addtogroup IRDA_Private_Functions
236   * @{
237   */
238 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
239 void IRDA_InitCallbacksToDefault(IRDA_HandleTypeDef *hirda);
240 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
241 static HAL_StatusTypeDef IRDA_SetConfig(IRDA_HandleTypeDef *hirda);
242 static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda);
243 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status,
244                                                      uint32_t Tickstart, uint32_t Timeout);
245 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef *hirda);
246 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef *hirda);
247 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
248 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma);
249 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
250 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma);
251 static void IRDA_DMAError(DMA_HandleTypeDef *hdma);
252 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef *hdma);
253 static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
254 static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
255 static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
256 static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
257 static void IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
258 static void IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda);
259 static void IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
260 /**
261   * @}
262   */
263 
264 /* Exported functions --------------------------------------------------------*/
265 
266 /** @defgroup IRDA_Exported_Functions IRDA Exported Functions
267   * @{
268   */
269 
270 /** @defgroup IRDA_Exported_Functions_Group1 Initialization and de-initialization functions
271   *  @brief    Initialization and Configuration functions
272   *
273 @verbatim
274   ==============================================================================
275               ##### Initialization and Configuration functions #####
276   ==============================================================================
277   [..]
278   This subsection provides a set of functions allowing to initialize the USARTx
279   in asynchronous IRDA mode.
280   (+) For the asynchronous mode only these parameters can be configured:
281       (++) Baud Rate
282       (++) Word Length
283       (++) Parity: If the parity is enabled, then the MSB bit of the data written
284            in the data register is transmitted but is changed by the parity bit.
285       (++) Power mode
286       (++) Prescaler setting
287       (++) Receiver/transmitter modes
288 
289   [..]
290   The HAL_IRDA_Init() API follows the USART asynchronous configuration procedures
291   (details for the procedures are available in reference manual).
292 
293 @endverbatim
294 
295   Depending on the frame length defined either by the M bit (8-bits or 9-bits)
296   or by the M1 and M0 bits (7-bit, 8-bit or 9-bit), the possible IRDA frame
297   formats are listed in the following table.
298 
299     Table 1. IRDA frame format.
300     +-----------------------------------------------------------------------+
301     |       M bit       |  PCE bit  |             IRDA frame                |
302     |-------------------|-----------|---------------------------------------|
303     |         0         |     0     |    | SB |    8-bit data   | STB |     |
304     |-------------------|-----------|---------------------------------------|
305     |         0         |     1     |    | SB | 7-bit data | PB | STB |     |
306     |-------------------|-----------|---------------------------------------|
307     |         1         |     0     |    | SB |    9-bit data   | STB |     |
308     |-------------------|-----------|---------------------------------------|
309     |         1         |     1     |    | SB | 8-bit data | PB | STB |     |
310     +-----------------------------------------------------------------------+
311     |  M1 bit |  M0 bit |  PCE bit  |             IRDA frame                |
312     |---------|---------|-----------|---------------------------------------|
313     |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |
314     |---------|---------|-----------|---------------------------------------|
315     |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
316     |---------|---------|-----------|---------------------------------------|
317     |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |
318     |---------|---------|-----------|---------------------------------------|
319     |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
320     |---------|---------|-----------|---------------------------------------|
321     |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |
322     |---------|---------|-----------|---------------------------------------|
323     |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |
324     +-----------------------------------------------------------------------+
325 
326   * @{
327   */
328 
329 /**
330   * @brief Initialize the IRDA mode according to the specified
331   *        parameters in the IRDA_InitTypeDef and initialize the associated handle.
332   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
333   *               the configuration information for the specified IRDA module.
334   * @retval HAL status
335   */
HAL_IRDA_Init(IRDA_HandleTypeDef * hirda)336 HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
337 {
338   /* Check the IRDA handle allocation */
339   if (hirda == NULL)
340   {
341     return HAL_ERROR;
342   }
343 
344   /* Check the USART/UART associated to the IRDA handle */
345   assert_param(IS_IRDA_INSTANCE(hirda->Instance));
346 
347   if (hirda->gState == HAL_IRDA_STATE_RESET)
348   {
349     /* Allocate lock resource and initialize it */
350     hirda->Lock = HAL_UNLOCKED;
351 
352 #if USE_HAL_IRDA_REGISTER_CALLBACKS == 1
353     IRDA_InitCallbacksToDefault(hirda);
354 
355     if (hirda->MspInitCallback == NULL)
356     {
357       hirda->MspInitCallback = HAL_IRDA_MspInit;
358     }
359 
360     /* Init the low level hardware */
361     hirda->MspInitCallback(hirda);
362 #else
363     /* Init the low level hardware : GPIO, CLOCK */
364     HAL_IRDA_MspInit(hirda);
365 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
366   }
367 
368   hirda->gState = HAL_IRDA_STATE_BUSY;
369 
370   /* Disable the Peripheral to update the configuration registers */
371   __HAL_IRDA_DISABLE(hirda);
372 
373   /* Set the IRDA Communication parameters */
374   if (IRDA_SetConfig(hirda) == HAL_ERROR)
375   {
376     return HAL_ERROR;
377   }
378 
379   /* In IRDA mode, the following bits must be kept cleared:
380   - LINEN, STOP and CLKEN bits in the USART_CR2 register,
381   - SCEN and HDSEL bits in the USART_CR3 register.*/
382   CLEAR_BIT(hirda->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN | USART_CR2_STOP));
383   CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
384 
385   /* set the UART/USART in IRDA mode */
386   hirda->Instance->CR3 |= USART_CR3_IREN;
387 
388   /* Enable the Peripheral */
389   __HAL_IRDA_ENABLE(hirda);
390 
391   /* TEACK and/or REACK to check before moving hirda->gState and hirda->RxState to Ready */
392   return (IRDA_CheckIdleState(hirda));
393 }
394 
395 /**
396   * @brief DeInitialize the IRDA peripheral.
397   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
398   *               the configuration information for the specified IRDA module.
399   * @retval HAL status
400   */
HAL_IRDA_DeInit(IRDA_HandleTypeDef * hirda)401 HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
402 {
403   /* Check the IRDA handle allocation */
404   if (hirda == NULL)
405   {
406     return HAL_ERROR;
407   }
408 
409   /* Check the USART/UART associated to the IRDA handle */
410   assert_param(IS_IRDA_INSTANCE(hirda->Instance));
411 
412   hirda->gState = HAL_IRDA_STATE_BUSY;
413 
414   /* DeInit the low level hardware */
415 #if USE_HAL_IRDA_REGISTER_CALLBACKS == 1
416   if (hirda->MspDeInitCallback == NULL)
417   {
418     hirda->MspDeInitCallback = HAL_IRDA_MspDeInit;
419   }
420   /* DeInit the low level hardware */
421   hirda->MspDeInitCallback(hirda);
422 #else
423   HAL_IRDA_MspDeInit(hirda);
424 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
425   /* Disable the Peripheral */
426   __HAL_IRDA_DISABLE(hirda);
427 
428   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
429   hirda->gState    = HAL_IRDA_STATE_RESET;
430   hirda->RxState   = HAL_IRDA_STATE_RESET;
431 
432   /* Process Unlock */
433   __HAL_UNLOCK(hirda);
434 
435   return HAL_OK;
436 }
437 
438 /**
439   * @brief Initialize the IRDA MSP.
440   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
441   *               the configuration information for the specified IRDA module.
442   * @retval None
443   */
HAL_IRDA_MspInit(IRDA_HandleTypeDef * hirda)444 __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
445 {
446   /* Prevent unused argument(s) compilation warning */
447   UNUSED(hirda);
448 
449   /* NOTE: This function should not be modified, when the callback is needed,
450            the HAL_IRDA_MspInit can be implemented in the user file
451    */
452 }
453 
454 /**
455   * @brief DeInitialize the IRDA MSP.
456   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
457   *               the configuration information for the specified IRDA module.
458   * @retval None
459   */
HAL_IRDA_MspDeInit(IRDA_HandleTypeDef * hirda)460 __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
461 {
462   /* Prevent unused argument(s) compilation warning */
463   UNUSED(hirda);
464 
465   /* NOTE: This function should not be modified, when the callback is needed,
466            the HAL_IRDA_MspDeInit can be implemented in the user file
467    */
468 }
469 
470 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
471 /**
472   * @brief  Register a User IRDA Callback
473   *         To be used instead of the weak predefined callback
474   * @note   The HAL_IRDA_RegisterCallback() may be called before HAL_IRDA_Init() in HAL_IRDA_STATE_RESET
475   *         to register callbacks for HAL_IRDA_MSPINIT_CB_ID and HAL_IRDA_MSPDEINIT_CB_ID
476   * @param  hirda irda handle
477   * @param  CallbackID ID of the callback to be registered
478   *         This parameter can be one of the following values:
479   *           @arg @ref HAL_IRDA_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
480   *           @arg @ref HAL_IRDA_TX_COMPLETE_CB_ID Tx Complete Callback ID
481   *           @arg @ref HAL_IRDA_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
482   *           @arg @ref HAL_IRDA_RX_COMPLETE_CB_ID Rx Complete Callback ID
483   *           @arg @ref HAL_IRDA_ERROR_CB_ID Error Callback ID
484   *           @arg @ref HAL_IRDA_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
485   *           @arg @ref HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
486   *           @arg @ref HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
487   *           @arg @ref HAL_IRDA_MSPINIT_CB_ID MspInit Callback ID
488   *           @arg @ref HAL_IRDA_MSPDEINIT_CB_ID MspDeInit Callback ID
489   * @param  pCallback pointer to the Callback function
490   * @retval HAL status
491   */
HAL_IRDA_RegisterCallback(IRDA_HandleTypeDef * hirda,HAL_IRDA_CallbackIDTypeDef CallbackID,pIRDA_CallbackTypeDef pCallback)492 HAL_StatusTypeDef HAL_IRDA_RegisterCallback(IRDA_HandleTypeDef *hirda, HAL_IRDA_CallbackIDTypeDef CallbackID,
493                                             pIRDA_CallbackTypeDef pCallback)
494 {
495   HAL_StatusTypeDef status = HAL_OK;
496 
497   if (pCallback == NULL)
498   {
499     /* Update the error code */
500     hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
501 
502     return HAL_ERROR;
503   }
504 
505   if (hirda->gState == HAL_IRDA_STATE_READY)
506   {
507     switch (CallbackID)
508     {
509       case HAL_IRDA_TX_HALFCOMPLETE_CB_ID :
510         hirda->TxHalfCpltCallback = pCallback;
511         break;
512 
513       case HAL_IRDA_TX_COMPLETE_CB_ID :
514         hirda->TxCpltCallback = pCallback;
515         break;
516 
517       case HAL_IRDA_RX_HALFCOMPLETE_CB_ID :
518         hirda->RxHalfCpltCallback = pCallback;
519         break;
520 
521       case HAL_IRDA_RX_COMPLETE_CB_ID :
522         hirda->RxCpltCallback = pCallback;
523         break;
524 
525       case HAL_IRDA_ERROR_CB_ID :
526         hirda->ErrorCallback = pCallback;
527         break;
528 
529       case HAL_IRDA_ABORT_COMPLETE_CB_ID :
530         hirda->AbortCpltCallback = pCallback;
531         break;
532 
533       case HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID :
534         hirda->AbortTransmitCpltCallback = pCallback;
535         break;
536 
537       case HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID :
538         hirda->AbortReceiveCpltCallback = pCallback;
539         break;
540 
541       case HAL_IRDA_MSPINIT_CB_ID :
542         hirda->MspInitCallback = pCallback;
543         break;
544 
545       case HAL_IRDA_MSPDEINIT_CB_ID :
546         hirda->MspDeInitCallback = pCallback;
547         break;
548 
549       default :
550         /* Update the error code */
551         hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
552 
553         /* Return error status */
554         status =  HAL_ERROR;
555         break;
556     }
557   }
558   else if (hirda->gState == HAL_IRDA_STATE_RESET)
559   {
560     switch (CallbackID)
561     {
562       case HAL_IRDA_MSPINIT_CB_ID :
563         hirda->MspInitCallback = pCallback;
564         break;
565 
566       case HAL_IRDA_MSPDEINIT_CB_ID :
567         hirda->MspDeInitCallback = pCallback;
568         break;
569 
570       default :
571         /* Update the error code */
572         hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
573 
574         /* Return error status */
575         status =  HAL_ERROR;
576         break;
577     }
578   }
579   else
580   {
581     /* Update the error code */
582     hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
583 
584     /* Return error status */
585     status =  HAL_ERROR;
586   }
587 
588   return status;
589 }
590 
591 /**
592   * @brief  Unregister an IRDA callback
593   *         IRDA callback is redirected to the weak predefined callback
594   * @note   The HAL_IRDA_UnRegisterCallback() may be called before HAL_IRDA_Init() in HAL_IRDA_STATE_RESET
595   *         to un-register callbacks for HAL_IRDA_MSPINIT_CB_ID and HAL_IRDA_MSPDEINIT_CB_ID
596   * @param  hirda irda handle
597   * @param  CallbackID ID of the callback to be unregistered
598   *         This parameter can be one of the following values:
599   *           @arg @ref HAL_IRDA_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
600   *           @arg @ref HAL_IRDA_TX_COMPLETE_CB_ID Tx Complete Callback ID
601   *           @arg @ref HAL_IRDA_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
602   *           @arg @ref HAL_IRDA_RX_COMPLETE_CB_ID Rx Complete Callback ID
603   *           @arg @ref HAL_IRDA_ERROR_CB_ID Error Callback ID
604   *           @arg @ref HAL_IRDA_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
605   *           @arg @ref HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
606   *           @arg @ref HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
607   *           @arg @ref HAL_IRDA_MSPINIT_CB_ID MspInit Callback ID
608   *           @arg @ref HAL_IRDA_MSPDEINIT_CB_ID MspDeInit Callback ID
609   * @retval HAL status
610   */
HAL_IRDA_UnRegisterCallback(IRDA_HandleTypeDef * hirda,HAL_IRDA_CallbackIDTypeDef CallbackID)611 HAL_StatusTypeDef HAL_IRDA_UnRegisterCallback(IRDA_HandleTypeDef *hirda, HAL_IRDA_CallbackIDTypeDef CallbackID)
612 {
613   HAL_StatusTypeDef status = HAL_OK;
614 
615   if (HAL_IRDA_STATE_READY == hirda->gState)
616   {
617     switch (CallbackID)
618     {
619       case HAL_IRDA_TX_HALFCOMPLETE_CB_ID :
620         hirda->TxHalfCpltCallback = HAL_IRDA_TxHalfCpltCallback;               /* Legacy weak  TxHalfCpltCallback    */
621         break;
622 
623       case HAL_IRDA_TX_COMPLETE_CB_ID :
624         hirda->TxCpltCallback = HAL_IRDA_TxCpltCallback;                       /* Legacy weak TxCpltCallback         */
625         break;
626 
627       case HAL_IRDA_RX_HALFCOMPLETE_CB_ID :
628         hirda->RxHalfCpltCallback = HAL_IRDA_RxHalfCpltCallback;               /* Legacy weak RxHalfCpltCallback     */
629         break;
630 
631       case HAL_IRDA_RX_COMPLETE_CB_ID :
632         hirda->RxCpltCallback = HAL_IRDA_RxCpltCallback;                       /* Legacy weak RxCpltCallback         */
633         break;
634 
635       case HAL_IRDA_ERROR_CB_ID :
636         hirda->ErrorCallback = HAL_IRDA_ErrorCallback;                         /* Legacy weak ErrorCallback          */
637         break;
638 
639       case HAL_IRDA_ABORT_COMPLETE_CB_ID :
640         hirda->AbortCpltCallback = HAL_IRDA_AbortCpltCallback;                 /* Legacy weak AbortCpltCallback      */
641         break;
642 
643       case HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID :
644         hirda->AbortTransmitCpltCallback = HAL_IRDA_AbortTransmitCpltCallback; /* Legacy weak
645                                                                                   AbortTransmitCpltCallback          */
646         break;
647 
648       case HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID :
649         hirda->AbortReceiveCpltCallback = HAL_IRDA_AbortReceiveCpltCallback;   /* Legacy weak
650                                                                                   AbortReceiveCpltCallback           */
651         break;
652 
653       case HAL_IRDA_MSPINIT_CB_ID :
654         hirda->MspInitCallback = HAL_IRDA_MspInit;                             /* Legacy weak MspInitCallback        */
655         break;
656 
657       case HAL_IRDA_MSPDEINIT_CB_ID :
658         hirda->MspDeInitCallback = HAL_IRDA_MspDeInit;                         /* Legacy weak MspDeInitCallback      */
659         break;
660 
661       default :
662         /* Update the error code */
663         hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
664 
665         /* Return error status */
666         status =  HAL_ERROR;
667         break;
668     }
669   }
670   else if (HAL_IRDA_STATE_RESET == hirda->gState)
671   {
672     switch (CallbackID)
673     {
674       case HAL_IRDA_MSPINIT_CB_ID :
675         hirda->MspInitCallback = HAL_IRDA_MspInit;
676         break;
677 
678       case HAL_IRDA_MSPDEINIT_CB_ID :
679         hirda->MspDeInitCallback = HAL_IRDA_MspDeInit;
680         break;
681 
682       default :
683         /* Update the error code */
684         hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
685 
686         /* Return error status */
687         status =  HAL_ERROR;
688         break;
689     }
690   }
691   else
692   {
693     /* Update the error code */
694     hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
695 
696     /* Return error status */
697     status =  HAL_ERROR;
698   }
699 
700   return status;
701 }
702 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
703 
704 /**
705   * @}
706   */
707 
708 /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
709   *  @brief   IRDA Transmit and Receive functions
710   *
711 @verbatim
712  ===============================================================================
713                          ##### IO operation functions #####
714  ===============================================================================
715   [..]
716     This subsection provides a set of functions allowing to manage the IRDA data transfers.
717 
718   [..]
719     IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
720     on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
721     is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
722     While receiving data, transmission should be avoided as the data to be transmitted
723     could be corrupted.
724 
725   [..]
726     (#) There are two modes of transfer:
727         (++) Blocking mode: the communication is performed in polling mode.
728              The HAL status of all data processing is returned by the same function
729              after finishing transfer.
730         (++) Non-Blocking mode: the communication is performed using Interrupts
731              or DMA, these API's return the HAL status.
732              The end of the data processing will be indicated through the
733              dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
734              using DMA mode.
735              The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
736              will be executed respectively at the end of the Transmit or Receive process
737              The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
738 
739     (#) Blocking mode APIs are :
740         (++) HAL_IRDA_Transmit()
741         (++) HAL_IRDA_Receive()
742 
743     (#) Non Blocking mode APIs with Interrupt are :
744         (++) HAL_IRDA_Transmit_IT()
745         (++) HAL_IRDA_Receive_IT()
746         (++) HAL_IRDA_IRQHandler()
747 
748     (#) Non Blocking mode functions with DMA are :
749         (++) HAL_IRDA_Transmit_DMA()
750         (++) HAL_IRDA_Receive_DMA()
751         (++) HAL_IRDA_DMAPause()
752         (++) HAL_IRDA_DMAResume()
753         (++) HAL_IRDA_DMAStop()
754 
755     (#) A set of Transfer Complete Callbacks are provided in Non Blocking mode:
756         (++) HAL_IRDA_TxHalfCpltCallback()
757         (++) HAL_IRDA_TxCpltCallback()
758         (++) HAL_IRDA_RxHalfCpltCallback()
759         (++) HAL_IRDA_RxCpltCallback()
760         (++) HAL_IRDA_ErrorCallback()
761 
762     (#) Non-Blocking mode transfers could be aborted using Abort API's :
763         (++) HAL_IRDA_Abort()
764         (++) HAL_IRDA_AbortTransmit()
765         (++) HAL_IRDA_AbortReceive()
766         (++) HAL_IRDA_Abort_IT()
767         (++) HAL_IRDA_AbortTransmit_IT()
768         (++) HAL_IRDA_AbortReceive_IT()
769 
770     (#) For Abort services based on interrupts (HAL_IRDA_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
771         (++) HAL_IRDA_AbortCpltCallback()
772         (++) HAL_IRDA_AbortTransmitCpltCallback()
773         (++) HAL_IRDA_AbortReceiveCpltCallback()
774 
775     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
776         Errors are handled as follows :
777         (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
778              to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error
779              in Interrupt mode reception .
780              Received character is then retrieved and stored in Rx buffer, Error code is set to allow user
781              to identify error type, and HAL_IRDA_ErrorCallback() user callback is executed.
782              Transfer is kept ongoing on IRDA side.
783              If user wants to abort it, Abort services should be called by user.
784         (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
785              This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
786              Error code is set to allow user to identify error type, and
787              HAL_IRDA_ErrorCallback() user callback is executed.
788 
789 @endverbatim
790   * @{
791   */
792 
793 /**
794   * @brief Send an amount of data in blocking mode.
795   * @note  When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
796   *        the sent data is handled as a set of u16. In this case, Size must reflect the number
797   *        of u16 available through pData.
798   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
799   *               the configuration information for the specified IRDA module.
800   * @param pData Pointer to data buffer (u8 or u16 data elements).
801   * @param Size Amount of data elements (u8 or u16) to be sent.
802   * @param Timeout Specify timeout value.
803   * @retval HAL status
804   */
HAL_IRDA_Transmit(IRDA_HandleTypeDef * hirda,const uint8_t * pData,uint16_t Size,uint32_t Timeout)805 HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
806 {
807   const uint8_t  *pdata8bits;
808   const uint16_t *pdata16bits;
809   uint32_t tickstart;
810 
811   /* Check that a Tx process is not already ongoing */
812   if (hirda->gState == HAL_IRDA_STATE_READY)
813   {
814     if ((pData == NULL) || (Size == 0U))
815     {
816       return  HAL_ERROR;
817     }
818 
819     /* Process Locked */
820     __HAL_LOCK(hirda);
821 
822     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
823     hirda->gState = HAL_IRDA_STATE_BUSY_TX;
824 
825     /* Init tickstart for timeout management */
826     tickstart = HAL_GetTick();
827 
828     hirda->TxXferSize = Size;
829     hirda->TxXferCount = Size;
830 
831     /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
832     if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
833     {
834       pdata8bits  = NULL;
835       pdata16bits = (const uint16_t *) pData; /* Derogation R.11.3 */
836     }
837     else
838     {
839       pdata8bits  = pData;
840       pdata16bits = NULL;
841     }
842 
843     while (hirda->TxXferCount > 0U)
844     {
845       hirda->TxXferCount--;
846 
847       if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
848       {
849         return HAL_TIMEOUT;
850       }
851       if (pdata8bits == NULL)
852       {
853         hirda->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU);
854         pdata16bits++;
855       }
856       else
857       {
858         hirda->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU);
859         pdata8bits++;
860       }
861     }
862 
863     if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
864     {
865       return HAL_TIMEOUT;
866     }
867 
868     /* At end of Tx process, restore hirda->gState to Ready */
869     hirda->gState = HAL_IRDA_STATE_READY;
870 
871     /* Process Unlocked */
872     __HAL_UNLOCK(hirda);
873 
874     return HAL_OK;
875   }
876   else
877   {
878     return HAL_BUSY;
879   }
880 }
881 
882 /**
883   * @brief Receive an amount of data in blocking mode.
884   * @note  When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
885   *        the received data is handled as a set of u16. In this case, Size must reflect the number
886   *        of u16 available through pData.
887   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
888   *                the configuration information for the specified IRDA module.
889   * @param pData Pointer to data buffer (u8 or u16 data elements).
890   * @param Size Amount of data elements (u8 or u16) to be received.
891   * @param Timeout Specify timeout value.
892   * @retval HAL status
893   */
HAL_IRDA_Receive(IRDA_HandleTypeDef * hirda,uint8_t * pData,uint16_t Size,uint32_t Timeout)894 HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
895 {
896   uint8_t  *pdata8bits;
897   uint16_t *pdata16bits;
898   uint16_t uhMask;
899   uint32_t tickstart;
900 
901   /* Check that a Rx process is not already ongoing */
902   if (hirda->RxState == HAL_IRDA_STATE_READY)
903   {
904     if ((pData == NULL) || (Size == 0U))
905     {
906       return  HAL_ERROR;
907     }
908 
909     /* Process Locked */
910     __HAL_LOCK(hirda);
911 
912     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
913     hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
914 
915     /* Init tickstart for timeout management */
916     tickstart = HAL_GetTick();
917 
918     hirda->RxXferSize = Size;
919     hirda->RxXferCount = Size;
920 
921     /* Computation of the mask to apply to RDR register
922        of the UART associated to the IRDA */
923     IRDA_MASK_COMPUTATION(hirda);
924     uhMask = hirda->Mask;
925 
926     /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
927     if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
928     {
929       pdata8bits  = NULL;
930       pdata16bits = (uint16_t *) pData; /* Derogation R.11.3 */
931     }
932     else
933     {
934       pdata8bits  = pData;
935       pdata16bits = NULL;
936     }
937 
938     /* Check data remaining to be received */
939     while (hirda->RxXferCount > 0U)
940     {
941       hirda->RxXferCount--;
942 
943       if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
944       {
945         return HAL_TIMEOUT;
946       }
947       if (pdata8bits == NULL)
948       {
949         *pdata16bits = (uint16_t)(hirda->Instance->RDR & uhMask);
950         pdata16bits++;
951       }
952       else
953       {
954         *pdata8bits = (uint8_t)(hirda->Instance->RDR & (uint8_t)uhMask);
955         pdata8bits++;
956       }
957     }
958 
959     /* At end of Rx process, restore hirda->RxState to Ready */
960     hirda->RxState = HAL_IRDA_STATE_READY;
961 
962     /* Process Unlocked */
963     __HAL_UNLOCK(hirda);
964 
965     return HAL_OK;
966   }
967   else
968   {
969     return HAL_BUSY;
970   }
971 }
972 
973 /**
974   * @brief Send an amount of data in interrupt mode.
975   * @note  When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
976   *        the sent data is handled as a set of u16. In this case, Size must reflect the number
977   *        of u16 available through pData.
978   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
979   *                the configuration information for the specified IRDA module.
980   * @param pData Pointer to data buffer (u8 or u16 data elements).
981   * @param Size Amount of data elements (u8 or u16) to be sent.
982   * @retval HAL status
983   */
HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef * hirda,const uint8_t * pData,uint16_t Size)984 HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, const uint8_t *pData, uint16_t Size)
985 {
986   /* Check that a Tx process is not already ongoing */
987   if (hirda->gState == HAL_IRDA_STATE_READY)
988   {
989     if ((pData == NULL) || (Size == 0U))
990     {
991       return HAL_ERROR;
992     }
993 
994     /* Process Locked */
995     __HAL_LOCK(hirda);
996 
997     hirda->pTxBuffPtr = pData;
998     hirda->TxXferSize = Size;
999     hirda->TxXferCount = Size;
1000 
1001     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1002     hirda->gState = HAL_IRDA_STATE_BUSY_TX;
1003 
1004     /* Process Unlocked */
1005     __HAL_UNLOCK(hirda);
1006 
1007     /* Enable the IRDA Transmit Data Register Empty Interrupt */
1008     SET_BIT(hirda->Instance->CR1, USART_CR1_TXEIE);
1009 
1010     return HAL_OK;
1011   }
1012   else
1013   {
1014     return HAL_BUSY;
1015   }
1016 }
1017 
1018 /**
1019   * @brief Receive an amount of data in interrupt mode.
1020   * @note  When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1021   *        the received data is handled as a set of u16. In this case, Size must reflect the number
1022   *        of u16 available through pData.
1023   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
1024   *                the configuration information for the specified IRDA module.
1025   * @param pData Pointer to data buffer (u8 or u16 data elements).
1026   * @param Size Amount of data elements (u8 or u16) to be received.
1027   * @retval HAL status
1028   */
HAL_IRDA_Receive_IT(IRDA_HandleTypeDef * hirda,uint8_t * pData,uint16_t Size)1029 HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
1030 {
1031   /* Check that a Rx process is not already ongoing */
1032   if (hirda->RxState == HAL_IRDA_STATE_READY)
1033   {
1034     if ((pData == NULL) || (Size == 0U))
1035     {
1036       return HAL_ERROR;
1037     }
1038 
1039     /* Process Locked */
1040     __HAL_LOCK(hirda);
1041 
1042     hirda->pRxBuffPtr = pData;
1043     hirda->RxXferSize = Size;
1044     hirda->RxXferCount = Size;
1045 
1046     /* Computation of the mask to apply to the RDR register
1047        of the UART associated to the IRDA */
1048     IRDA_MASK_COMPUTATION(hirda);
1049 
1050     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1051     hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
1052 
1053     /* Process Unlocked */
1054     __HAL_UNLOCK(hirda);
1055 
1056     if (hirda->Init.Parity != IRDA_PARITY_NONE)
1057     {
1058       /* Enable the IRDA Parity Error and Data Register not empty Interrupts */
1059       SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
1060     }
1061     else
1062     {
1063       /* Enable the IRDA Data Register not empty Interrupts */
1064       SET_BIT(hirda->Instance->CR1, USART_CR1_RXNEIE);
1065     }
1066 
1067     /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1068     SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1069 
1070     return HAL_OK;
1071   }
1072   else
1073   {
1074     return HAL_BUSY;
1075   }
1076 }
1077 
1078 /**
1079   * @brief Send an amount of data in DMA mode.
1080   * @note  When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1081   *        the sent data is handled as a set of u16. In this case, Size must reflect the number
1082   *        of u16 available through pData.
1083   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1084   *               the configuration information for the specified IRDA module.
1085   * @param pData pointer to data buffer (u8 or u16 data elements).
1086   * @param Size Amount of data elements (u8 or u16) to be sent.
1087   * @retval HAL status
1088   */
HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef * hirda,const uint8_t * pData,uint16_t Size)1089 HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, const uint8_t *pData, uint16_t Size)
1090 {
1091   /* Check that a Tx process is not already ongoing */
1092   if (hirda->gState == HAL_IRDA_STATE_READY)
1093   {
1094     if ((pData == NULL) || (Size == 0U))
1095     {
1096       return HAL_ERROR;
1097     }
1098 
1099     /* Process Locked */
1100     __HAL_LOCK(hirda);
1101 
1102     hirda->pTxBuffPtr = pData;
1103     hirda->TxXferSize = Size;
1104     hirda->TxXferCount = Size;
1105 
1106     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1107     hirda->gState = HAL_IRDA_STATE_BUSY_TX;
1108 
1109     /* Set the IRDA DMA transfer complete callback */
1110     hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
1111 
1112     /* Set the IRDA DMA half transfer complete callback */
1113     hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;
1114 
1115     /* Set the DMA error callback */
1116     hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
1117 
1118     /* Set the DMA abort callback */
1119     hirda->hdmatx->XferAbortCallback = NULL;
1120 
1121     /* Enable the IRDA transmit DMA channel */
1122     if (HAL_DMA_Start_IT(hirda->hdmatx, (uint32_t)hirda->pTxBuffPtr, (uint32_t)&hirda->Instance->TDR, Size) == HAL_OK)
1123     {
1124       /* Clear the TC flag in the ICR register */
1125       __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_TCF);
1126 
1127       /* Process Unlocked */
1128       __HAL_UNLOCK(hirda);
1129 
1130       /* Enable the DMA transfer for transmit request by setting the DMAT bit
1131          in the USART CR3 register */
1132       SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1133 
1134       return HAL_OK;
1135     }
1136     else
1137     {
1138       /* Set error code to DMA */
1139       hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
1140 
1141       /* Process Unlocked */
1142       __HAL_UNLOCK(hirda);
1143 
1144       /* Restore hirda->gState to ready */
1145       hirda->gState = HAL_IRDA_STATE_READY;
1146 
1147       return HAL_ERROR;
1148     }
1149   }
1150   else
1151   {
1152     return HAL_BUSY;
1153   }
1154 }
1155 
1156 /**
1157   * @brief Receive an amount of data in DMA mode.
1158   * @note  When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1159   *        the received data is handled as a set of u16. In this case, Size must reflect the number
1160   *        of u16 available through pData.
1161   * @note   When the IRDA parity is enabled (PCE = 1), the received data contains
1162   *         the parity bit (MSB position).
1163   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
1164   *               the configuration information for the specified IRDA module.
1165   * @param pData Pointer to data buffer (u8 or u16 data elements).
1166   * @param Size Amount of data elements (u8 or u16) to be received.
1167   * @retval HAL status
1168   */
HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef * hirda,uint8_t * pData,uint16_t Size)1169 HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
1170 {
1171   /* Check that a Rx process is not already ongoing */
1172   if (hirda->RxState == HAL_IRDA_STATE_READY)
1173   {
1174     if ((pData == NULL) || (Size == 0U))
1175     {
1176       return HAL_ERROR;
1177     }
1178 
1179     /* Process Locked */
1180     __HAL_LOCK(hirda);
1181 
1182     hirda->pRxBuffPtr = pData;
1183     hirda->RxXferSize = Size;
1184 
1185     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1186     hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
1187 
1188     /* Set the IRDA DMA transfer complete callback */
1189     hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
1190 
1191     /* Set the IRDA DMA half transfer complete callback */
1192     hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;
1193 
1194     /* Set the DMA error callback */
1195     hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
1196 
1197     /* Set the DMA abort callback */
1198     hirda->hdmarx->XferAbortCallback = NULL;
1199 
1200     /* Enable the DMA channel */
1201     if (HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->RDR, (uint32_t)hirda->pRxBuffPtr, Size) == HAL_OK)
1202     {
1203       /* Process Unlocked */
1204       __HAL_UNLOCK(hirda);
1205 
1206       if (hirda->Init.Parity != IRDA_PARITY_NONE)
1207       {
1208         /* Enable the UART Parity Error Interrupt */
1209         SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
1210       }
1211 
1212       /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1213       SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1214 
1215       /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1216          in the USART CR3 register */
1217       SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1218 
1219       return HAL_OK;
1220     }
1221     else
1222     {
1223       /* Set error code to DMA */
1224       hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
1225 
1226       /* Process Unlocked */
1227       __HAL_UNLOCK(hirda);
1228 
1229       /* Restore hirda->RxState to ready */
1230       hirda->RxState = HAL_IRDA_STATE_READY;
1231 
1232       return HAL_ERROR;
1233     }
1234   }
1235   else
1236   {
1237     return HAL_BUSY;
1238   }
1239 }
1240 
1241 
1242 /**
1243   * @brief Pause the DMA Transfer.
1244   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
1245   *                the configuration information for the specified IRDA module.
1246   * @retval HAL status
1247   */
HAL_IRDA_DMAPause(IRDA_HandleTypeDef * hirda)1248 HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda)
1249 {
1250   /* Process Locked */
1251   __HAL_LOCK(hirda);
1252 
1253   if (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
1254   {
1255     if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
1256     {
1257       /* Disable the IRDA DMA Tx request */
1258       CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1259     }
1260   }
1261   if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
1262   {
1263     if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1264     {
1265       /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1266       CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
1267       CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1268 
1269       /* Disable the IRDA DMA Rx request */
1270       CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1271     }
1272   }
1273 
1274   /* Process Unlocked */
1275   __HAL_UNLOCK(hirda);
1276 
1277   return HAL_OK;
1278 }
1279 
1280 /**
1281   * @brief Resume the DMA Transfer.
1282   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
1283   *                the configuration information for the specified UART module.
1284   * @retval HAL status
1285   */
HAL_IRDA_DMAResume(IRDA_HandleTypeDef * hirda)1286 HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda)
1287 {
1288   /* Process Locked */
1289   __HAL_LOCK(hirda);
1290 
1291   if (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
1292   {
1293     /* Enable the IRDA DMA Tx request */
1294     SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1295   }
1296   if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
1297   {
1298     /* Clear the Overrun flag before resuming the Rx transfer*/
1299     __HAL_IRDA_CLEAR_OREFLAG(hirda);
1300 
1301     /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */
1302     if (hirda->Init.Parity != IRDA_PARITY_NONE)
1303     {
1304       SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
1305     }
1306     SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1307 
1308     /* Enable the IRDA DMA Rx request */
1309     SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1310   }
1311 
1312   /* Process Unlocked */
1313   __HAL_UNLOCK(hirda);
1314 
1315   return HAL_OK;
1316 }
1317 
1318 /**
1319   * @brief Stop the DMA Transfer.
1320   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
1321   *                the configuration information for the specified UART module.
1322   * @retval HAL status
1323   */
HAL_IRDA_DMAStop(IRDA_HandleTypeDef * hirda)1324 HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda)
1325 {
1326   /* The Lock is not implemented on this API to allow the user application
1327      to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback() /
1328      HAL_IRDA_TxHalfCpltCallback / HAL_IRDA_RxHalfCpltCallback:
1329      indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1330      interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1331      the stream and the corresponding call back is executed. */
1332 
1333   /* Stop IRDA DMA Tx request if ongoing */
1334   if (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
1335   {
1336     if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
1337     {
1338       CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1339 
1340       /* Abort the IRDA DMA Tx channel */
1341       if (hirda->hdmatx != NULL)
1342       {
1343         if (HAL_DMA_Abort(hirda->hdmatx) != HAL_OK)
1344         {
1345           if (HAL_DMA_GetError(hirda->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1346           {
1347             /* Set error code to DMA */
1348             hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
1349 
1350             return HAL_TIMEOUT;
1351           }
1352         }
1353       }
1354 
1355       IRDA_EndTxTransfer(hirda);
1356     }
1357   }
1358 
1359   /* Stop IRDA DMA Rx request if ongoing */
1360   if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
1361   {
1362     if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1363     {
1364       CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1365 
1366       /* Abort the IRDA DMA Rx channel */
1367       if (hirda->hdmarx != NULL)
1368       {
1369         if (HAL_DMA_Abort(hirda->hdmarx) != HAL_OK)
1370         {
1371           if (HAL_DMA_GetError(hirda->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1372           {
1373             /* Set error code to DMA */
1374             hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
1375 
1376             return HAL_TIMEOUT;
1377           }
1378         }
1379       }
1380 
1381       IRDA_EndRxTransfer(hirda);
1382     }
1383   }
1384 
1385   return HAL_OK;
1386 }
1387 
1388 /**
1389   * @brief  Abort ongoing transfers (blocking mode).
1390   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
1391   *               the configuration information for the specified UART module.
1392   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1393   *         This procedure performs following operations :
1394   *           - Disable IRDA Interrupts (Tx and Rx)
1395   *           - Disable the DMA transfer in the peripheral register (if enabled)
1396   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1397   *           - Set handle State to READY
1398   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1399   * @retval HAL status
1400   */
HAL_IRDA_Abort(IRDA_HandleTypeDef * hirda)1401 HAL_StatusTypeDef HAL_IRDA_Abort(IRDA_HandleTypeDef *hirda)
1402 {
1403   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1404   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1405   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1406 
1407   /* Disable the IRDA DMA Tx request if enabled */
1408   if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
1409   {
1410     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1411 
1412     /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
1413     if (hirda->hdmatx != NULL)
1414     {
1415       /* Set the IRDA DMA Abort callback to Null.
1416          No call back execution at end of DMA abort procedure */
1417       hirda->hdmatx->XferAbortCallback = NULL;
1418 
1419       if (HAL_DMA_Abort(hirda->hdmatx) != HAL_OK)
1420       {
1421         if (HAL_DMA_GetError(hirda->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1422         {
1423           /* Set error code to DMA */
1424           hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
1425 
1426           return HAL_TIMEOUT;
1427         }
1428       }
1429     }
1430   }
1431 
1432   /* Disable the IRDA DMA Rx request if enabled */
1433   if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1434   {
1435     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1436 
1437     /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
1438     if (hirda->hdmarx != NULL)
1439     {
1440       /* Set the IRDA DMA Abort callback to Null.
1441          No call back execution at end of DMA abort procedure */
1442       hirda->hdmarx->XferAbortCallback = NULL;
1443 
1444       if (HAL_DMA_Abort(hirda->hdmarx) != HAL_OK)
1445       {
1446         if (HAL_DMA_GetError(hirda->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1447         {
1448           /* Set error code to DMA */
1449           hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
1450 
1451           return HAL_TIMEOUT;
1452         }
1453       }
1454     }
1455   }
1456 
1457   /* Reset Tx and Rx transfer counters */
1458   hirda->TxXferCount = 0U;
1459   hirda->RxXferCount = 0U;
1460 
1461   /* Clear the Error flags in the ICR register */
1462   __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
1463 
1464   /* Restore hirda->gState and hirda->RxState to Ready */
1465   hirda->gState  = HAL_IRDA_STATE_READY;
1466   hirda->RxState = HAL_IRDA_STATE_READY;
1467 
1468   /* Reset Handle ErrorCode to No Error */
1469   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1470 
1471   return HAL_OK;
1472 }
1473 
1474 /**
1475   * @brief  Abort ongoing Transmit transfer (blocking mode).
1476   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
1477   *               the configuration information for the specified UART module.
1478   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1479   *         This procedure performs following operations :
1480   *           - Disable IRDA Interrupts (Tx)
1481   *           - Disable the DMA transfer in the peripheral register (if enabled)
1482   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1483   *           - Set handle State to READY
1484   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1485   * @retval HAL status
1486   */
HAL_IRDA_AbortTransmit(IRDA_HandleTypeDef * hirda)1487 HAL_StatusTypeDef HAL_IRDA_AbortTransmit(IRDA_HandleTypeDef *hirda)
1488 {
1489   /* Disable TXEIE and TCIE interrupts */
1490   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1491 
1492   /* Disable the IRDA DMA Tx request if enabled */
1493   if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
1494   {
1495     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1496 
1497     /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
1498     if (hirda->hdmatx != NULL)
1499     {
1500       /* Set the IRDA DMA Abort callback to Null.
1501          No call back execution at end of DMA abort procedure */
1502       hirda->hdmatx->XferAbortCallback = NULL;
1503 
1504       if (HAL_DMA_Abort(hirda->hdmatx) != HAL_OK)
1505       {
1506         if (HAL_DMA_GetError(hirda->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1507         {
1508           /* Set error code to DMA */
1509           hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
1510 
1511           return HAL_TIMEOUT;
1512         }
1513       }
1514     }
1515   }
1516 
1517   /* Reset Tx transfer counter */
1518   hirda->TxXferCount = 0U;
1519 
1520   /* Restore hirda->gState to Ready */
1521   hirda->gState = HAL_IRDA_STATE_READY;
1522 
1523   return HAL_OK;
1524 }
1525 
1526 /**
1527   * @brief  Abort ongoing Receive transfer (blocking mode).
1528   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
1529   *               the configuration information for the specified UART module.
1530   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1531   *         This procedure performs following operations :
1532   *           - Disable IRDA Interrupts (Rx)
1533   *           - Disable the DMA transfer in the peripheral register (if enabled)
1534   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1535   *           - Set handle State to READY
1536   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1537   * @retval HAL status
1538   */
HAL_IRDA_AbortReceive(IRDA_HandleTypeDef * hirda)1539 HAL_StatusTypeDef HAL_IRDA_AbortReceive(IRDA_HandleTypeDef *hirda)
1540 {
1541   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1542   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1543   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1544 
1545   /* Disable the IRDA DMA Rx request if enabled */
1546   if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1547   {
1548     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1549 
1550     /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
1551     if (hirda->hdmarx != NULL)
1552     {
1553       /* Set the IRDA DMA Abort callback to Null.
1554          No call back execution at end of DMA abort procedure */
1555       hirda->hdmarx->XferAbortCallback = NULL;
1556 
1557       if (HAL_DMA_Abort(hirda->hdmarx) != HAL_OK)
1558       {
1559         if (HAL_DMA_GetError(hirda->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1560         {
1561           /* Set error code to DMA */
1562           hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
1563 
1564           return HAL_TIMEOUT;
1565         }
1566       }
1567     }
1568   }
1569 
1570   /* Reset Rx transfer counter */
1571   hirda->RxXferCount = 0U;
1572 
1573   /* Clear the Error flags in the ICR register */
1574   __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
1575 
1576   /* Restore hirda->RxState to Ready */
1577   hirda->RxState = HAL_IRDA_STATE_READY;
1578 
1579   return HAL_OK;
1580 }
1581 
1582 /**
1583   * @brief  Abort ongoing transfers (Interrupt mode).
1584   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
1585   *               the configuration information for the specified UART module.
1586   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1587   *         This procedure performs following operations :
1588   *           - Disable IRDA Interrupts (Tx and Rx)
1589   *           - Disable the DMA transfer in the peripheral register (if enabled)
1590   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1591   *           - Set handle State to READY
1592   *           - At abort completion, call user abort complete callback
1593   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1594   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1595   * @retval HAL status
1596   */
HAL_IRDA_Abort_IT(IRDA_HandleTypeDef * hirda)1597 HAL_StatusTypeDef HAL_IRDA_Abort_IT(IRDA_HandleTypeDef *hirda)
1598 {
1599   uint32_t abortcplt = 1U;
1600 
1601   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1602   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1603   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1604 
1605   /* If DMA Tx and/or DMA Rx Handles are associated to IRDA Handle, DMA Abort complete callbacks should be initialised
1606      before any call to DMA Abort functions */
1607   /* DMA Tx Handle is valid */
1608   if (hirda->hdmatx != NULL)
1609   {
1610     /* Set DMA Abort Complete callback if IRDA DMA Tx request if enabled.
1611        Otherwise, set it to NULL */
1612     if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
1613     {
1614       hirda->hdmatx->XferAbortCallback = IRDA_DMATxAbortCallback;
1615     }
1616     else
1617     {
1618       hirda->hdmatx->XferAbortCallback = NULL;
1619     }
1620   }
1621   /* DMA Rx Handle is valid */
1622   if (hirda->hdmarx != NULL)
1623   {
1624     /* Set DMA Abort Complete callback if IRDA DMA Rx request if enabled.
1625        Otherwise, set it to NULL */
1626     if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1627     {
1628       hirda->hdmarx->XferAbortCallback = IRDA_DMARxAbortCallback;
1629     }
1630     else
1631     {
1632       hirda->hdmarx->XferAbortCallback = NULL;
1633     }
1634   }
1635 
1636   /* Disable the IRDA DMA Tx request if enabled */
1637   if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
1638   {
1639     /* Disable DMA Tx at UART level */
1640     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1641 
1642     /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
1643     if (hirda->hdmatx != NULL)
1644     {
1645       /* IRDA Tx DMA Abort callback has already been initialised :
1646          will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1647 
1648       /* Abort DMA TX */
1649       if (HAL_DMA_Abort_IT(hirda->hdmatx) != HAL_OK)
1650       {
1651         hirda->hdmatx->XferAbortCallback = NULL;
1652       }
1653       else
1654       {
1655         abortcplt = 0U;
1656       }
1657     }
1658   }
1659 
1660   /* Disable the IRDA DMA Rx request if enabled */
1661   if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1662   {
1663     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1664 
1665     /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
1666     if (hirda->hdmarx != NULL)
1667     {
1668       /* IRDA Rx DMA Abort callback has already been initialised :
1669          will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1670 
1671       /* Abort DMA RX */
1672       if (HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
1673       {
1674         hirda->hdmarx->XferAbortCallback = NULL;
1675         abortcplt = 1U;
1676       }
1677       else
1678       {
1679         abortcplt = 0U;
1680       }
1681     }
1682   }
1683 
1684   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1685   if (abortcplt == 1U)
1686   {
1687     /* Reset Tx and Rx transfer counters */
1688     hirda->TxXferCount = 0U;
1689     hirda->RxXferCount = 0U;
1690 
1691     /* Reset errorCode */
1692     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1693 
1694     /* Clear the Error flags in the ICR register */
1695     __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
1696 
1697     /* Restore hirda->gState and hirda->RxState to Ready */
1698     hirda->gState  = HAL_IRDA_STATE_READY;
1699     hirda->RxState = HAL_IRDA_STATE_READY;
1700 
1701     /* As no DMA to be aborted, call directly user Abort complete callback */
1702 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1703     /* Call registered Abort complete callback */
1704     hirda->AbortCpltCallback(hirda);
1705 #else
1706     /* Call legacy weak Abort complete callback */
1707     HAL_IRDA_AbortCpltCallback(hirda);
1708 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1709   }
1710 
1711   return HAL_OK;
1712 }
1713 
1714 /**
1715   * @brief  Abort ongoing Transmit transfer (Interrupt mode).
1716   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
1717   *               the configuration information for the specified UART module.
1718   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1719   *         This procedure performs following operations :
1720   *           - Disable IRDA Interrupts (Tx)
1721   *           - Disable the DMA transfer in the peripheral register (if enabled)
1722   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1723   *           - Set handle State to READY
1724   *           - At abort completion, call user abort complete callback
1725   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1726   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1727   * @retval HAL status
1728   */
HAL_IRDA_AbortTransmit_IT(IRDA_HandleTypeDef * hirda)1729 HAL_StatusTypeDef HAL_IRDA_AbortTransmit_IT(IRDA_HandleTypeDef *hirda)
1730 {
1731   /* Disable TXEIE and TCIE interrupts */
1732   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1733 
1734   /* Disable the IRDA DMA Tx request if enabled */
1735   if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
1736   {
1737     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1738 
1739     /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
1740     if (hirda->hdmatx != NULL)
1741     {
1742       /* Set the IRDA DMA Abort callback :
1743          will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1744       hirda->hdmatx->XferAbortCallback = IRDA_DMATxOnlyAbortCallback;
1745 
1746       /* Abort DMA TX */
1747       if (HAL_DMA_Abort_IT(hirda->hdmatx) != HAL_OK)
1748       {
1749         /* Call Directly hirda->hdmatx->XferAbortCallback function in case of error */
1750         hirda->hdmatx->XferAbortCallback(hirda->hdmatx);
1751       }
1752     }
1753     else
1754     {
1755       /* Reset Tx transfer counter */
1756       hirda->TxXferCount = 0U;
1757 
1758       /* Restore hirda->gState to Ready */
1759       hirda->gState = HAL_IRDA_STATE_READY;
1760 
1761       /* As no DMA to be aborted, call directly user Abort complete callback */
1762 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1763       /* Call registered Abort Transmit Complete Callback */
1764       hirda->AbortTransmitCpltCallback(hirda);
1765 #else
1766       /* Call legacy weak Abort Transmit Complete Callback */
1767       HAL_IRDA_AbortTransmitCpltCallback(hirda);
1768 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1769     }
1770   }
1771   else
1772   {
1773     /* Reset Tx transfer counter */
1774     hirda->TxXferCount = 0U;
1775 
1776     /* Restore hirda->gState to Ready */
1777     hirda->gState = HAL_IRDA_STATE_READY;
1778 
1779     /* As no DMA to be aborted, call directly user Abort complete callback */
1780 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1781     /* Call registered Abort Transmit Complete Callback */
1782     hirda->AbortTransmitCpltCallback(hirda);
1783 #else
1784     /* Call legacy weak Abort Transmit Complete Callback */
1785     HAL_IRDA_AbortTransmitCpltCallback(hirda);
1786 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1787   }
1788 
1789   return HAL_OK;
1790 }
1791 
1792 /**
1793   * @brief  Abort ongoing Receive transfer (Interrupt mode).
1794   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
1795   *               the configuration information for the specified UART module.
1796   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1797   *         This procedure performs following operations :
1798   *           - Disable IRDA Interrupts (Rx)
1799   *           - Disable the DMA transfer in the peripheral register (if enabled)
1800   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1801   *           - Set handle State to READY
1802   *           - At abort completion, call user abort complete callback
1803   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1804   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1805   * @retval HAL status
1806   */
HAL_IRDA_AbortReceive_IT(IRDA_HandleTypeDef * hirda)1807 HAL_StatusTypeDef HAL_IRDA_AbortReceive_IT(IRDA_HandleTypeDef *hirda)
1808 {
1809   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1810   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1811   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1812 
1813   /* Disable the IRDA DMA Rx request if enabled */
1814   if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1815   {
1816     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1817 
1818     /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
1819     if (hirda->hdmarx != NULL)
1820     {
1821       /* Set the IRDA DMA Abort callback :
1822          will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1823       hirda->hdmarx->XferAbortCallback = IRDA_DMARxOnlyAbortCallback;
1824 
1825       /* Abort DMA RX */
1826       if (HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
1827       {
1828         /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
1829         hirda->hdmarx->XferAbortCallback(hirda->hdmarx);
1830       }
1831     }
1832     else
1833     {
1834       /* Reset Rx transfer counter */
1835       hirda->RxXferCount = 0U;
1836 
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);
1839 
1840       /* Restore hirda->RxState to Ready */
1841       hirda->RxState = HAL_IRDA_STATE_READY;
1842 
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);
1847 #else
1848       /* Call legacy weak Abort Receive Complete Callback */
1849       HAL_IRDA_AbortReceiveCpltCallback(hirda);
1850 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1851     }
1852   }
1853   else
1854   {
1855     /* Reset Rx transfer counter */
1856     hirda->RxXferCount = 0U;
1857 
1858     /* Clear the Error flags in the ICR register */
1859     __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
1860 
1861     /* Restore hirda->RxState to Ready */
1862     hirda->RxState = HAL_IRDA_STATE_READY;
1863 
1864     /* As no DMA to be aborted, call directly user Abort complete callback */
1865 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1866     /* Call registered Abort Receive Complete Callback */
1867     hirda->AbortReceiveCpltCallback(hirda);
1868 #else
1869     /* Call legacy weak Abort Receive Complete Callback */
1870     HAL_IRDA_AbortReceiveCpltCallback(hirda);
1871 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1872   }
1873 
1874   return HAL_OK;
1875 }
1876 
1877 /**
1878   * @brief Handle IRDA interrupt request.
1879   * @param hirda  Pointer to a IRDA_HandleTypeDef structure that contains
1880   *               the configuration information for the specified IRDA module.
1881   * @retval None
1882   */
HAL_IRDA_IRQHandler(IRDA_HandleTypeDef * hirda)1883 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
1884 {
1885   uint32_t isrflags   = READ_REG(hirda->Instance->ISR);
1886   uint32_t cr1its     = READ_REG(hirda->Instance->CR1);
1887   uint32_t cr3its;
1888   uint32_t errorflags;
1889   uint32_t errorcode;
1890 
1891   /* If no error occurs */
1892   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
1893   if (errorflags == 0U)
1894   {
1895     /* IRDA in mode Receiver ---------------------------------------------------*/
1896     if (((isrflags & USART_ISR_RXNE) != 0U) && ((cr1its & USART_CR1_RXNEIE) != 0U))
1897     {
1898       IRDA_Receive_IT(hirda);
1899       return;
1900     }
1901   }
1902 
1903   /* If some errors occur */
1904   cr3its = READ_REG(hirda->Instance->CR3);
1905   if ((errorflags != 0U)
1906       && (((cr3its & USART_CR3_EIE) != 0U)
1907           || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != 0U)))
1908   {
1909     /* IRDA parity error interrupt occurred -------------------------------------*/
1910     if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
1911     {
1912       __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_PEF);
1913 
1914       hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
1915     }
1916 
1917     /* IRDA frame error interrupt occurred --------------------------------------*/
1918     if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
1919     {
1920       __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_FEF);
1921 
1922       hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
1923     }
1924 
1925     /* IRDA noise error interrupt occurred --------------------------------------*/
1926     if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
1927     {
1928       __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_NEF);
1929 
1930       hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
1931     }
1932 
1933     /* IRDA Over-Run interrupt occurred -----------------------------------------*/
1934     if (((isrflags & USART_ISR_ORE) != 0U) &&
1935         (((cr1its & USART_CR1_RXNEIE) != 0U) || ((cr3its & USART_CR3_EIE) != 0U)))
1936     {
1937       __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_OREF);
1938 
1939       hirda->ErrorCode |= HAL_IRDA_ERROR_ORE;
1940     }
1941 
1942     /* Call IRDA Error Call back function if need be --------------------------*/
1943     if (hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
1944     {
1945       /* IRDA in mode Receiver ---------------------------------------------------*/
1946       if (((isrflags & USART_ISR_RXNE) != 0U) && ((cr1its & USART_CR1_RXNEIE) != 0U))
1947       {
1948         IRDA_Receive_IT(hirda);
1949       }
1950 
1951       /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1952          consider error as blocking */
1953       errorcode = hirda->ErrorCode;
1954       if ((HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR)) ||
1955           ((errorcode & HAL_IRDA_ERROR_ORE) != 0U))
1956       {
1957         /* Blocking error : transfer is aborted
1958            Set the IRDA state ready to be able to start again the process,
1959            Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1960         IRDA_EndRxTransfer(hirda);
1961 
1962         /* Disable the IRDA DMA Rx request if enabled */
1963         if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1964         {
1965           CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1966 
1967           /* Abort the IRDA DMA Rx channel */
1968           if (hirda->hdmarx != NULL)
1969           {
1970             /* Set the IRDA DMA Abort callback :
1971                will lead to call HAL_IRDA_ErrorCallback() at end of DMA abort procedure */
1972             hirda->hdmarx->XferAbortCallback = IRDA_DMAAbortOnError;
1973 
1974             /* Abort DMA RX */
1975             if (HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
1976             {
1977               /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
1978               hirda->hdmarx->XferAbortCallback(hirda->hdmarx);
1979             }
1980           }
1981           else
1982           {
1983 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1984             /* Call registered user error callback */
1985             hirda->ErrorCallback(hirda);
1986 #else
1987             /* Call legacy weak user error callback */
1988             HAL_IRDA_ErrorCallback(hirda);
1989 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
1990           }
1991         }
1992         else
1993         {
1994 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
1995           /* Call registered user error callback */
1996           hirda->ErrorCallback(hirda);
1997 #else
1998           /* Call legacy weak user error callback */
1999           HAL_IRDA_ErrorCallback(hirda);
2000 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2001         }
2002       }
2003       else
2004       {
2005         /* Non Blocking error : transfer could go on.
2006            Error is notified to user through user error callback */
2007 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2008         /* Call registered user error callback */
2009         hirda->ErrorCallback(hirda);
2010 #else
2011         /* Call legacy weak user error callback */
2012         HAL_IRDA_ErrorCallback(hirda);
2013 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2014         hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
2015       }
2016     }
2017     return;
2018 
2019   } /* End if some error occurs */
2020 
2021   /* IRDA in mode Transmitter ------------------------------------------------*/
2022   if (((isrflags & USART_ISR_TXE) != 0U) && ((cr1its & USART_CR1_TXEIE) != 0U))
2023   {
2024     IRDA_Transmit_IT(hirda);
2025     return;
2026   }
2027 
2028   /* IRDA in mode Transmitter (transmission end) -----------------------------*/
2029   if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
2030   {
2031     IRDA_EndTransmit_IT(hirda);
2032     return;
2033   }
2034 
2035 }
2036 
2037 /**
2038   * @brief  Tx Transfer completed callback.
2039   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2040   *               the configuration information for the specified IRDA module.
2041   * @retval None
2042   */
HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef * hirda)2043 __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
2044 {
2045   /* Prevent unused argument(s) compilation warning */
2046   UNUSED(hirda);
2047 
2048   /* NOTE : This function should not be modified, when the callback is needed,
2049             the HAL_IRDA_TxCpltCallback can be implemented in the user file.
2050    */
2051 }
2052 
2053 /**
2054   * @brief  Tx Half Transfer completed callback.
2055   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2056   *               the configuration information for the specified USART module.
2057   * @retval None
2058   */
HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef * hirda)2059 __weak void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
2060 {
2061   /* Prevent unused argument(s) compilation warning */
2062   UNUSED(hirda);
2063 
2064   /* NOTE : This function should not be modified, when the callback is needed,
2065             the HAL_IRDA_TxHalfCpltCallback can be implemented in the user file.
2066    */
2067 }
2068 
2069 /**
2070   * @brief  Rx Transfer completed callback.
2071   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2072   *               the configuration information for the specified IRDA module.
2073   * @retval None
2074   */
HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef * hirda)2075 __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
2076 {
2077   /* Prevent unused argument(s) compilation warning */
2078   UNUSED(hirda);
2079 
2080   /* NOTE : This function should not be modified, when the callback is needed,
2081             the HAL_IRDA_RxCpltCallback can be implemented in the user file.
2082    */
2083 }
2084 
2085 /**
2086   * @brief  Rx Half Transfer complete callback.
2087   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2088   *               the configuration information for the specified IRDA module.
2089   * @retval None
2090   */
HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef * hirda)2091 __weak void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
2092 {
2093   /* Prevent unused argument(s) compilation warning */
2094   UNUSED(hirda);
2095 
2096   /* NOTE : This function should not be modified, when the callback is needed,
2097             the HAL_IRDA_RxHalfCpltCallback can be implemented in the user file.
2098    */
2099 }
2100 
2101 /**
2102   * @brief  IRDA error callback.
2103   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2104   *               the configuration information for the specified IRDA module.
2105   * @retval None
2106   */
HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef * hirda)2107 __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
2108 {
2109   /* Prevent unused argument(s) compilation warning */
2110   UNUSED(hirda);
2111 
2112   /* NOTE : This function should not be modified, when the callback is needed,
2113             the HAL_IRDA_ErrorCallback can be implemented in the user file.
2114    */
2115 }
2116 
2117 /**
2118   * @brief  IRDA Abort Complete callback.
2119   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2120   *               the configuration information for the specified IRDA module.
2121   * @retval None
2122   */
HAL_IRDA_AbortCpltCallback(IRDA_HandleTypeDef * hirda)2123 __weak void HAL_IRDA_AbortCpltCallback(IRDA_HandleTypeDef *hirda)
2124 {
2125   /* Prevent unused argument(s) compilation warning */
2126   UNUSED(hirda);
2127 
2128   /* NOTE : This function should not be modified, when the callback is needed,
2129             the HAL_IRDA_AbortCpltCallback can be implemented in the user file.
2130    */
2131 }
2132 
2133 /**
2134   * @brief  IRDA Abort Complete callback.
2135   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2136   *               the configuration information for the specified IRDA module.
2137   * @retval None
2138   */
HAL_IRDA_AbortTransmitCpltCallback(IRDA_HandleTypeDef * hirda)2139 __weak void HAL_IRDA_AbortTransmitCpltCallback(IRDA_HandleTypeDef *hirda)
2140 {
2141   /* Prevent unused argument(s) compilation warning */
2142   UNUSED(hirda);
2143 
2144   /* NOTE : This function should not be modified, when the callback is needed,
2145             the HAL_IRDA_AbortTransmitCpltCallback can be implemented in the user file.
2146    */
2147 }
2148 
2149 /**
2150   * @brief  IRDA Abort Receive Complete callback.
2151   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2152   *               the configuration information for the specified IRDA module.
2153   * @retval None
2154   */
HAL_IRDA_AbortReceiveCpltCallback(IRDA_HandleTypeDef * hirda)2155 __weak void HAL_IRDA_AbortReceiveCpltCallback(IRDA_HandleTypeDef *hirda)
2156 {
2157   /* Prevent unused argument(s) compilation warning */
2158   UNUSED(hirda);
2159 
2160   /* NOTE : This function should not be modified, when the callback is needed,
2161             the HAL_IRDA_AbortReceiveCpltCallback can be implemented in the user file.
2162    */
2163 }
2164 
2165 /**
2166   * @}
2167   */
2168 
2169 /** @defgroup IRDA_Exported_Functions_Group4 Peripheral State and Error functions
2170   *  @brief   IRDA State and Errors functions
2171   *
2172 @verbatim
2173   ==============================================================================
2174             ##### Peripheral State and Error functions #####
2175   ==============================================================================
2176   [..]
2177     This subsection provides a set of functions allowing to return the State of IrDA
2178     communication process and also return Peripheral Errors occurred during communication process
2179      (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state
2180          of the IRDA peripheral handle.
2181      (+) HAL_IRDA_GetError() checks in run-time errors that could occur during
2182          communication.
2183 
2184 @endverbatim
2185   * @{
2186   */
2187 
2188 /**
2189   * @brief Return the IRDA handle state.
2190   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2191   *                the configuration information for the specified IRDA module.
2192   * @retval HAL state
2193   */
HAL_IRDA_GetState(const IRDA_HandleTypeDef * hirda)2194 HAL_IRDA_StateTypeDef HAL_IRDA_GetState(const IRDA_HandleTypeDef *hirda)
2195 {
2196   /* Return IRDA handle state */
2197   uint32_t temp1;
2198   uint32_t temp2;
2199   temp1 = (uint32_t)hirda->gState;
2200   temp2 = (uint32_t)hirda->RxState;
2201 
2202   return (HAL_IRDA_StateTypeDef)(temp1 | temp2);
2203 }
2204 
2205 /**
2206   * @brief Return the IRDA handle error code.
2207   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2208   *               the configuration information for the specified IRDA module.
2209   * @retval IRDA Error Code
2210   */
HAL_IRDA_GetError(const IRDA_HandleTypeDef * hirda)2211 uint32_t HAL_IRDA_GetError(const IRDA_HandleTypeDef *hirda)
2212 {
2213   return hirda->ErrorCode;
2214 }
2215 
2216 /**
2217   * @}
2218   */
2219 
2220 /**
2221   * @}
2222   */
2223 
2224 /** @defgroup IRDA_Private_Functions IRDA Private Functions
2225   * @{
2226   */
2227 
2228 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2229 /**
2230   * @brief  Initialize the callbacks to their default values.
2231   * @param  hirda IRDA handle.
2232   * @retval none
2233   */
IRDA_InitCallbacksToDefault(IRDA_HandleTypeDef * hirda)2234 void IRDA_InitCallbacksToDefault(IRDA_HandleTypeDef *hirda)
2235 {
2236   /* Init the IRDA Callback settings */
2237   hirda->TxHalfCpltCallback        = HAL_IRDA_TxHalfCpltCallback;        /* Legacy weak TxHalfCpltCallback        */
2238   hirda->TxCpltCallback            = HAL_IRDA_TxCpltCallback;            /* Legacy weak TxCpltCallback            */
2239   hirda->RxHalfCpltCallback        = HAL_IRDA_RxHalfCpltCallback;        /* Legacy weak RxHalfCpltCallback        */
2240   hirda->RxCpltCallback            = HAL_IRDA_RxCpltCallback;            /* Legacy weak RxCpltCallback            */
2241   hirda->ErrorCallback             = HAL_IRDA_ErrorCallback;             /* Legacy weak ErrorCallback             */
2242   hirda->AbortCpltCallback         = HAL_IRDA_AbortCpltCallback;         /* Legacy weak AbortCpltCallback         */
2243   hirda->AbortTransmitCpltCallback = HAL_IRDA_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
2244   hirda->AbortReceiveCpltCallback  = HAL_IRDA_AbortReceiveCpltCallback;  /* Legacy weak AbortReceiveCpltCallback  */
2245 
2246 }
2247 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
2248 
2249 /**
2250   * @brief Configure the IRDA peripheral.
2251   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2252   *               the configuration information for the specified IRDA module.
2253   * @retval HAL status
2254   */
IRDA_SetConfig(IRDA_HandleTypeDef * hirda)2255 static HAL_StatusTypeDef IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
2256 {
2257   uint32_t tmpreg;
2258   IRDA_ClockSourceTypeDef clocksource;
2259   HAL_StatusTypeDef ret = HAL_OK;
2260   uint32_t pclk;
2261 
2262   /* Check the communication parameters */
2263   assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));
2264   assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
2265   assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
2266   assert_param(IS_IRDA_TX_RX_MODE(hirda->Init.Mode));
2267   assert_param(IS_IRDA_PRESCALER(hirda->Init.Prescaler));
2268   assert_param(IS_IRDA_POWERMODE(hirda->Init.PowerMode));
2269 
2270   /*-------------------------- USART CR1 Configuration -----------------------*/
2271   /* Configure the IRDA Word Length, Parity and transfer Mode:
2272      Set the M bits according to hirda->Init.WordLength value
2273      Set PCE and PS bits according to hirda->Init.Parity value
2274      Set TE and RE bits according to hirda->Init.Mode value */
2275   tmpreg = (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode ;
2276 
2277   MODIFY_REG(hirda->Instance->CR1, IRDA_CR1_FIELDS, tmpreg);
2278 
2279   /*-------------------------- USART CR3 Configuration -----------------------*/
2280   MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.PowerMode);
2281 
2282 
2283   /*-------------------------- USART GTPR Configuration ----------------------*/
2284   MODIFY_REG(hirda->Instance->GTPR, (uint16_t)USART_GTPR_PSC, (uint16_t)hirda->Init.Prescaler);
2285 
2286   /*-------------------------- USART BRR Configuration -----------------------*/
2287   IRDA_GETCLOCKSOURCE(hirda, clocksource);
2288   tmpreg =   0U;
2289   switch (clocksource)
2290   {
2291     case IRDA_CLOCKSOURCE_PCLK1:
2292       pclk = HAL_RCC_GetPCLK1Freq();
2293       tmpreg = (uint32_t)(IRDA_DIV_SAMPLING16(pclk, hirda->Init.BaudRate));
2294       break;
2295     case IRDA_CLOCKSOURCE_PCLK2:
2296       pclk = HAL_RCC_GetPCLK2Freq();
2297       tmpreg = (uint32_t)(IRDA_DIV_SAMPLING16(pclk, hirda->Init.BaudRate));
2298       break;
2299     case IRDA_CLOCKSOURCE_HSI:
2300       tmpreg = (uint32_t)(IRDA_DIV_SAMPLING16(HSI_VALUE, hirda->Init.BaudRate));
2301       break;
2302     case IRDA_CLOCKSOURCE_SYSCLK:
2303       pclk = HAL_RCC_GetSysClockFreq();
2304       tmpreg = (uint32_t)(IRDA_DIV_SAMPLING16(pclk, hirda->Init.BaudRate));
2305       break;
2306     case IRDA_CLOCKSOURCE_LSE:
2307       tmpreg = (uint32_t)(IRDA_DIV_SAMPLING16((uint32_t)LSE_VALUE, hirda->Init.BaudRate));
2308       break;
2309     default:
2310       ret = HAL_ERROR;
2311       break;
2312   }
2313 
2314   /* USARTDIV must be greater than or equal to 0d16 */
2315   if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
2316   {
2317     hirda->Instance->BRR = (uint16_t)tmpreg;
2318   }
2319   else
2320   {
2321     ret = HAL_ERROR;
2322   }
2323 
2324   return ret;
2325 }
2326 
2327 /**
2328   * @brief Check the IRDA Idle State.
2329   * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
2330   *               the configuration information for the specified IRDA module.
2331   * @retval HAL status
2332   */
IRDA_CheckIdleState(IRDA_HandleTypeDef * hirda)2333 static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda)
2334 {
2335   uint32_t tickstart;
2336 
2337   /* Initialize the IRDA ErrorCode */
2338   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
2339 
2340   /* Init tickstart for timeout management */
2341   tickstart = HAL_GetTick();
2342 
2343   /* Check if the Transmitter is enabled */
2344   if ((hirda->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
2345   {
2346     /* Wait until TEACK flag is set */
2347     if (IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_TEACK, RESET, tickstart, IRDA_TEACK_REACK_TIMEOUT) != HAL_OK)
2348     {
2349       /* Timeout occurred */
2350       return HAL_TIMEOUT;
2351     }
2352   }
2353   /* Check if the Receiver is enabled */
2354   if ((hirda->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
2355   {
2356     /* Wait until REACK flag is set */
2357     if (IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_REACK, RESET, tickstart, IRDA_TEACK_REACK_TIMEOUT) != HAL_OK)
2358     {
2359       /* Timeout occurred */
2360       return HAL_TIMEOUT;
2361     }
2362   }
2363 
2364   /* Initialize the IRDA state*/
2365   hirda->gState  = HAL_IRDA_STATE_READY;
2366   hirda->RxState = HAL_IRDA_STATE_READY;
2367 
2368   /* Process Unlocked */
2369   __HAL_UNLOCK(hirda);
2370 
2371   return HAL_OK;
2372 }
2373 
2374 /**
2375   * @brief  Handle IRDA Communication Timeout. It waits
2376   *         until a flag is no longer in the specified status.
2377   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2378   *               the configuration information for the specified IRDA module.
2379   * @param  Flag Specifies the IRDA flag to check.
2380   * @param  Status The actual Flag status (SET or RESET)
2381   * @param  Tickstart Tick start value
2382   * @param  Timeout Timeout duration
2383   * @retval HAL status
2384   */
IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef * hirda,uint32_t Flag,FlagStatus Status,uint32_t Tickstart,uint32_t Timeout)2385 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status,
2386                                                      uint32_t Tickstart, uint32_t Timeout)
2387 {
2388   /* Wait until flag is set */
2389   while ((__HAL_IRDA_GET_FLAG(hirda, Flag) ? SET : RESET) == Status)
2390   {
2391     /* Check for the Timeout */
2392     if (Timeout != HAL_MAX_DELAY)
2393     {
2394       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
2395       {
2396         /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
2397            interrupts for the interrupt process */
2398         CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
2399         CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
2400 
2401         hirda->gState  = HAL_IRDA_STATE_READY;
2402         hirda->RxState = HAL_IRDA_STATE_READY;
2403 
2404         /* Process Unlocked */
2405         __HAL_UNLOCK(hirda);
2406         return HAL_TIMEOUT;
2407       }
2408     }
2409   }
2410   return HAL_OK;
2411 }
2412 
2413 
2414 /**
2415   * @brief  End ongoing Tx transfer on IRDA peripheral (following error detection or Transmit completion).
2416   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2417   *               the configuration information for the specified IRDA module.
2418   * @retval None
2419   */
IRDA_EndTxTransfer(IRDA_HandleTypeDef * hirda)2420 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef *hirda)
2421 {
2422   /* Disable TXEIE and TCIE interrupts */
2423   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
2424 
2425   /* At end of Tx process, restore hirda->gState to Ready */
2426   hirda->gState = HAL_IRDA_STATE_READY;
2427 }
2428 
2429 /**
2430   * @brief  End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2431   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2432   *               the configuration information for the specified IRDA module.
2433   * @retval None
2434   */
IRDA_EndRxTransfer(IRDA_HandleTypeDef * hirda)2435 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef *hirda)
2436 {
2437   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2438   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
2439   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
2440 
2441   /* At end of Rx process, restore hirda->RxState to Ready */
2442   hirda->RxState = HAL_IRDA_STATE_READY;
2443 }
2444 
2445 
2446 /**
2447   * @brief  DMA IRDA transmit process complete callback.
2448   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2449   *              the configuration information for the specified DMA module.
2450   * @retval None
2451   */
IRDA_DMATransmitCplt(DMA_HandleTypeDef * hdma)2452 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2453 {
2454   IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
2455 
2456   /* DMA Normal mode */
2457   if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
2458   {
2459     hirda->TxXferCount = 0U;
2460 
2461     /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2462        in the IRDA CR3 register */
2463     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
2464 
2465     /* Enable the IRDA Transmit Complete Interrupt */
2466     SET_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
2467   }
2468   /* DMA Circular mode */
2469   else
2470   {
2471 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2472     /* Call registered Tx complete callback */
2473     hirda->TxCpltCallback(hirda);
2474 #else
2475     /* Call legacy weak Tx complete callback */
2476     HAL_IRDA_TxCpltCallback(hirda);
2477 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2478   }
2479 
2480 }
2481 
2482 /**
2483   * @brief  DMA IRDA transmit process half complete callback.
2484   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2485   *              the configuration information for the specified DMA module.
2486   * @retval None
2487   */
IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef * hdma)2488 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma)
2489 {
2490   IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
2491 
2492 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2493   /* Call registered Tx Half complete callback */
2494   hirda->TxHalfCpltCallback(hirda);
2495 #else
2496   /* Call legacy weak Tx complete callback */
2497   HAL_IRDA_TxHalfCpltCallback(hirda);
2498 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2499 }
2500 
2501 /**
2502   * @brief  DMA IRDA receive process complete callback.
2503   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2504   *               the configuration information for the specified DMA module.
2505   * @retval None
2506   */
IRDA_DMAReceiveCplt(DMA_HandleTypeDef * hdma)2507 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2508 {
2509   IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
2510 
2511   /* DMA Normal mode */
2512   if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
2513   {
2514     hirda->RxXferCount = 0U;
2515 
2516     /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2517     CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
2518     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
2519 
2520     /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2521        in the IRDA CR3 register */
2522     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
2523 
2524     /* At end of Rx process, restore hirda->RxState to Ready */
2525     hirda->RxState = HAL_IRDA_STATE_READY;
2526   }
2527 
2528 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2529   /* Call registered Rx complete callback */
2530   hirda->RxCpltCallback(hirda);
2531 #else
2532   /* Call legacy weak Rx complete callback */
2533   HAL_IRDA_RxCpltCallback(hirda);
2534 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
2535 }
2536 
2537 /**
2538   * @brief DMA IRDA receive process half complete callback.
2539   * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2540   *              the configuration information for the specified DMA module.
2541   * @retval None
2542   */
IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef * hdma)2543 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma)
2544 {
2545   IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
2546 
2547 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2548   /*Call registered Rx Half complete callback*/
2549   hirda->RxHalfCpltCallback(hirda);
2550 #else
2551   /* Call legacy weak Rx Half complete callback */
2552   HAL_IRDA_RxHalfCpltCallback(hirda);
2553 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2554 }
2555 
2556 /**
2557   * @brief DMA IRDA communication error callback.
2558   * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2559   *              the configuration information for the specified DMA module.
2560   * @retval None
2561   */
IRDA_DMAError(DMA_HandleTypeDef * hdma)2562 static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
2563 {
2564   IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
2565 
2566   /* Stop IRDA DMA Tx request if ongoing */
2567   if (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
2568   {
2569     if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
2570     {
2571       hirda->TxXferCount = 0U;
2572       IRDA_EndTxTransfer(hirda);
2573     }
2574   }
2575 
2576   /* Stop IRDA DMA Rx request if ongoing */
2577   if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
2578   {
2579     if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
2580     {
2581       hirda->RxXferCount = 0U;
2582       IRDA_EndRxTransfer(hirda);
2583     }
2584   }
2585 
2586   hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
2587 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2588   /* Call registered user error callback */
2589   hirda->ErrorCallback(hirda);
2590 #else
2591   /* Call legacy weak user error callback */
2592   HAL_IRDA_ErrorCallback(hirda);
2593 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2594 }
2595 
2596 /**
2597   * @brief  DMA IRDA communication abort callback, when initiated by HAL services on Error
2598   *         (To be called at end of DMA Abort procedure following error occurrence).
2599   * @param  hdma DMA handle.
2600   * @retval None
2601   */
IRDA_DMAAbortOnError(DMA_HandleTypeDef * hdma)2602 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2603 {
2604   IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
2605   hirda->RxXferCount = 0U;
2606   hirda->TxXferCount = 0U;
2607 
2608 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2609   /* Call registered user error callback */
2610   hirda->ErrorCallback(hirda);
2611 #else
2612   /* Call legacy weak user error callback */
2613   HAL_IRDA_ErrorCallback(hirda);
2614 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2615 }
2616 
2617 /**
2618   * @brief  DMA IRDA Tx communication abort callback, when initiated by user
2619   *         (To be called at end of DMA Tx Abort procedure following user abort request).
2620   * @note   When this callback is executed, User Abort complete call back is called only if no
2621   *         Abort still ongoing for Rx DMA Handle.
2622   * @param  hdma DMA handle.
2623   * @retval None
2624   */
IRDA_DMATxAbortCallback(DMA_HandleTypeDef * hdma)2625 static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2626 {
2627   IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
2628 
2629   hirda->hdmatx->XferAbortCallback = NULL;
2630 
2631   /* Check if an Abort process is still ongoing */
2632   if (hirda->hdmarx != NULL)
2633   {
2634     if (hirda->hdmarx->XferAbortCallback != NULL)
2635     {
2636       return;
2637     }
2638   }
2639 
2640   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2641   hirda->TxXferCount = 0U;
2642   hirda->RxXferCount = 0U;
2643 
2644   /* Reset errorCode */
2645   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
2646 
2647   /* Clear the Error flags in the ICR register */
2648   __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
2649 
2650   /* Restore hirda->gState and hirda->RxState to Ready */
2651   hirda->gState  = HAL_IRDA_STATE_READY;
2652   hirda->RxState = HAL_IRDA_STATE_READY;
2653 
2654   /* Call user Abort complete callback */
2655 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2656   /* Call registered Abort complete callback */
2657   hirda->AbortCpltCallback(hirda);
2658 #else
2659   /* Call legacy weak Abort complete callback */
2660   HAL_IRDA_AbortCpltCallback(hirda);
2661 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2662 }
2663 
2664 
2665 /**
2666   * @brief  DMA IRDA Rx communication abort callback, when initiated by user
2667   *         (To be called at end of DMA Rx Abort procedure following user abort request).
2668   * @note   When this callback is executed, User Abort complete call back is called only if no
2669   *         Abort still ongoing for Tx DMA Handle.
2670   * @param  hdma DMA handle.
2671   * @retval None
2672   */
IRDA_DMARxAbortCallback(DMA_HandleTypeDef * hdma)2673 static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2674 {
2675   IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
2676 
2677   hirda->hdmarx->XferAbortCallback = NULL;
2678 
2679   /* Check if an Abort process is still ongoing */
2680   if (hirda->hdmatx != NULL)
2681   {
2682     if (hirda->hdmatx->XferAbortCallback != NULL)
2683     {
2684       return;
2685     }
2686   }
2687 
2688   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2689   hirda->TxXferCount = 0U;
2690   hirda->RxXferCount = 0U;
2691 
2692   /* Reset errorCode */
2693   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
2694 
2695   /* Clear the Error flags in the ICR register */
2696   __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
2697 
2698   /* Restore hirda->gState and hirda->RxState to Ready */
2699   hirda->gState  = HAL_IRDA_STATE_READY;
2700   hirda->RxState = HAL_IRDA_STATE_READY;
2701 
2702   /* Call user Abort complete callback */
2703 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2704   /* Call registered Abort complete callback */
2705   hirda->AbortCpltCallback(hirda);
2706 #else
2707   /* Call legacy weak Abort complete callback */
2708   HAL_IRDA_AbortCpltCallback(hirda);
2709 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2710 }
2711 
2712 
2713 /**
2714   * @brief  DMA IRDA Tx communication abort callback, when initiated by user by a call to
2715   *         HAL_IRDA_AbortTransmit_IT API (Abort only Tx transfer)
2716   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2717   *         and leads to user Tx Abort Complete callback execution).
2718   * @param  hdma DMA handle.
2719   * @retval None
2720   */
IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef * hdma)2721 static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2722 {
2723   IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
2724 
2725   hirda->TxXferCount = 0U;
2726 
2727   /* Restore hirda->gState to Ready */
2728   hirda->gState = HAL_IRDA_STATE_READY;
2729 
2730   /* Call user Abort complete callback */
2731 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2732   /* Call registered Abort Transmit Complete Callback */
2733   hirda->AbortTransmitCpltCallback(hirda);
2734 #else
2735   /* Call legacy weak Abort Transmit Complete Callback */
2736   HAL_IRDA_AbortTransmitCpltCallback(hirda);
2737 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2738 }
2739 
2740 /**
2741   * @brief  DMA IRDA Rx communication abort callback, when initiated by user by a call to
2742   *         HAL_IRDA_AbortReceive_IT API (Abort only Rx transfer)
2743   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2744   *         and leads to user Rx Abort Complete callback execution).
2745   * @param  hdma DMA handle.
2746   * @retval None
2747   */
IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef * hdma)2748 static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2749 {
2750   IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2751 
2752   hirda->RxXferCount = 0U;
2753 
2754   /* Clear the Error flags in the ICR register */
2755   __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
2756 
2757   /* Restore hirda->RxState to Ready */
2758   hirda->RxState = HAL_IRDA_STATE_READY;
2759 
2760   /* Call user Abort complete callback */
2761 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2762   /* Call registered Abort Receive Complete Callback */
2763   hirda->AbortReceiveCpltCallback(hirda);
2764 #else
2765   /* Call legacy weak Abort Receive Complete Callback */
2766   HAL_IRDA_AbortReceiveCpltCallback(hirda);
2767 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2768 }
2769 
2770 /**
2771   * @brief  Send an amount of data in interrupt mode.
2772   * @note   Function is called under interruption only, once
2773   *         interruptions have been enabled by HAL_IRDA_Transmit_IT().
2774   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2775   *               the configuration information for the specified IRDA module.
2776   * @retval None
2777   */
IRDA_Transmit_IT(IRDA_HandleTypeDef * hirda)2778 static void IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
2779 {
2780   const uint16_t *tmp;
2781 
2782   /* Check that a Tx process is ongoing */
2783   if (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
2784   {
2785     if (hirda->TxXferCount == 0U)
2786     {
2787       /* Disable the IRDA Transmit Data Register Empty Interrupt */
2788       CLEAR_BIT(hirda->Instance->CR1, USART_CR1_TXEIE);
2789 
2790       /* Enable the IRDA Transmit Complete Interrupt */
2791       SET_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
2792     }
2793     else
2794     {
2795       if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
2796       {
2797         tmp = (const uint16_t *) hirda->pTxBuffPtr; /* Derogation R.11.3 */
2798         hirda->Instance->TDR = (uint16_t)(*tmp & 0x01FFU);
2799         hirda->pTxBuffPtr += 2U;
2800       }
2801       else
2802       {
2803         hirda->Instance->TDR = (uint8_t)(*hirda->pTxBuffPtr & 0xFFU);
2804         hirda->pTxBuffPtr++;
2805       }
2806       hirda->TxXferCount--;
2807     }
2808   }
2809 }
2810 
2811 /**
2812   * @brief  Wrap up transmission in non-blocking mode.
2813   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2814   *               the configuration information for the specified IRDA module.
2815   * @retval None
2816   */
IRDA_EndTransmit_IT(IRDA_HandleTypeDef * hirda)2817 static void IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda)
2818 {
2819   /* Disable the IRDA Transmit Complete Interrupt */
2820   CLEAR_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
2821 
2822   /* Tx process is ended, restore hirda->gState to Ready */
2823   hirda->gState = HAL_IRDA_STATE_READY;
2824 
2825 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2826   /* Call registered Tx complete callback */
2827   hirda->TxCpltCallback(hirda);
2828 #else
2829   /* Call legacy weak Tx complete callback */
2830   HAL_IRDA_TxCpltCallback(hirda);
2831 #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
2832 }
2833 
2834 /**
2835   * @brief  Receive an amount of data in interrupt mode.
2836   * @note   Function is called under interruption only, once
2837   *         interruptions have been enabled by HAL_IRDA_Receive_IT()
2838   * @param  hirda Pointer to a IRDA_HandleTypeDef structure that contains
2839   *               the configuration information for the specified IRDA module.
2840   * @retval None
2841   */
IRDA_Receive_IT(IRDA_HandleTypeDef * hirda)2842 static void IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
2843 {
2844   uint16_t *tmp;
2845   uint16_t  uhMask = hirda->Mask;
2846   uint16_t  uhdata;
2847 
2848   /* Check that a Rx process is ongoing */
2849   if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
2850   {
2851     uhdata = (uint16_t) READ_REG(hirda->Instance->RDR);
2852     if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
2853     {
2854       tmp = (uint16_t *) hirda->pRxBuffPtr; /* Derogation R.11.3 */
2855       *tmp = (uint16_t)(uhdata & uhMask);
2856       hirda->pRxBuffPtr  += 2U;
2857     }
2858     else
2859     {
2860       *hirda->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
2861       hirda->pRxBuffPtr++;
2862     }
2863 
2864     hirda->RxXferCount--;
2865     if (hirda->RxXferCount == 0U)
2866     {
2867       /* Disable the IRDA Parity Error Interrupt and RXNE interrupt */
2868       CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
2869 
2870       /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
2871       CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
2872 
2873       /* Rx process is completed, restore hirda->RxState to Ready */
2874       hirda->RxState = HAL_IRDA_STATE_READY;
2875 
2876 #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
2877       /* Call registered Rx complete callback */
2878       hirda->RxCpltCallback(hirda);
2879 #else
2880       /* Call legacy weak Rx complete callback */
2881       HAL_IRDA_RxCpltCallback(hirda);
2882 #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
2883     }
2884   }
2885   else
2886   {
2887     /* Clear RXNE interrupt flag */
2888     __HAL_IRDA_SEND_REQ(hirda, IRDA_RXDATA_FLUSH_REQUEST);
2889   }
2890 }
2891 
2892 /**
2893   * @}
2894   */
2895 
2896 #endif /* HAL_IRDA_MODULE_ENABLED */
2897 /**
2898   * @}
2899   */
2900 
2901 /**
2902   * @}
2903   */
2904 
2905