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