Merge pull request #11270 from haslinghuis/rename_attr
[betaflight.git] / lib / main / STM32F4 / Drivers / STM32F4xx_HAL_Driver / Src / stm32f4xx_hal_irda.c
blob8aaeb3fedea7c4d245cec3a320a43d45915416f0
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal_irda.c
4 * @author MCD Application Team
5 * @version V1.7.1
6 * @date 14-April-2017
7 * @brief IRDA HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the IrDA SIR ENDEC block (IrDA):
10 * + Initialization and de-initialization methods
11 * + IO operation methods
12 * + Peripheral Control methods
14 @verbatim
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
18 [..]
19 The IRDA HAL driver can be used as follows:
21 (#) Declare a IRDA_HandleTypeDef handle structure.
22 (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API:
23 (##) Enable the USARTx interface clock.
24 (##) IRDA pins configuration:
25 (+++) Enable the clock for the IRDA GPIOs.
26 (+++) Configure these IRDA pins as alternate function pull-up.
27 (##) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
28 and HAL_IRDA_Receive_IT() APIs):
29 (+++) Configure the USARTx interrupt priority.
30 (+++) Enable the NVIC USART IRQ handle.
31 (##) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
32 and HAL_IRDA_Receive_DMA() APIs):
33 (+++) Declare a DMA handle structure for the Tx/Rx stream.
34 (+++) Enable the DMAx interface clock.
35 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
36 (+++) Configure the DMA Tx/Rx Stream.
37 (+++) Associate the initialized DMA handle to the IRDA DMA Tx/Rx handle.
38 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
40 (#) Program the Baud Rate, Word Length, Parity, IrDA Mode, Prescaler
41 and Mode(Receiver/Transmitter) in the hirda Init structure.
43 (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
44 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
45 by calling the customized HAL_IRDA_MspInit() API.
46 -@@- The specific IRDA interrupts (Transmission complete interrupt,
47 RXNE interrupt and Error Interrupts) will be managed using the macros
48 __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
50 (#) Three operation modes are available within this driver :
52 *** Polling mode IO operation ***
53 =================================
54 [..]
55 (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
56 (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
58 *** Interrupt mode IO operation ***
59 ===================================
60 [..]
61 (+) Send an amount of data in non blocking mode using HAL_IRDA_Transmit_IT()
62 (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
63 add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
64 (+) Receive an amount of data in non blocking mode using HAL_IRDA_Receive_IT()
65 (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
66 add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
67 (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
68 add his own code by customization of function pointer HAL_IRDA_ErrorCallback
70 *** DMA mode IO operation ***
71 =============================
72 [..]
73 (+) Send an amount of data in non blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
74 (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
75 add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
76 (+) Receive an amount of data in non blocking mode (DMA) using HAL_IRDA_Receive_DMA()
77 (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
78 add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
79 (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
80 add his own code by customization of function pointer HAL_IRDA_ErrorCallback
82 *** IRDA HAL driver macros list ***
83 ===================================
84 [..]
85 Below the list of most used macros in IRDA HAL driver.
87 (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
88 (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
89 (+) __HAL_IRDA_GET_FLAG : Checks whether the specified IRDA flag is set or not
90 (+) __HAL_IRDA_CLEAR_FLAG : Clears the specified IRDA pending flag
91 (+) __HAL_IRDA_ENABLE_IT: Enables the specified IRDA interrupt
92 (+) __HAL_IRDA_DISABLE_IT: Disables the specified IRDA interrupt
94 [..]
95 (@) You can refer to the IRDA HAL driver header file for more useful macros
97 @endverbatim
98 ******************************************************************************
99 * @attention
101 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
103 * Redistribution and use in source and binary forms, with or without modification,
104 * are permitted provided that the following conditions are met:
105 * 1. Redistributions of source code must retain the above copyright notice,
106 * this list of conditions and the following disclaimer.
107 * 2. Redistributions in binary form must reproduce the above copyright notice,
108 * this list of conditions and the following disclaimer in the documentation
109 * and/or other materials provided with the distribution.
110 * 3. Neither the name of STMicroelectronics nor the names of its contributors
111 * may be used to endorse or promote products derived from this software
112 * without specific prior written permission.
114 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
115 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
116 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
117 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
118 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
119 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
120 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
121 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
122 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
123 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
125 ******************************************************************************
128 /* Includes ------------------------------------------------------------------*/
129 #include "stm32f4xx_hal.h"
131 /** @addtogroup STM32F4xx_HAL_Driver
132 * @{
135 /** @defgroup IRDA IRDA
136 * @brief HAL IRDA module driver
137 * @{
140 #ifdef HAL_IRDA_MODULE_ENABLED
142 /* Private typedef -----------------------------------------------------------*/
143 /* Private define ------------------------------------------------------------*/
144 /** @addtogroup IRDA_Private_Constants
145 * @{
148 * @}
150 /* Private macro -------------------------------------------------------------*/
151 /* Private variables ---------------------------------------------------------*/
152 /* Private function prototypes -----------------------------------------------*/
153 /** @addtogroup IRDA_Private_Functions
154 * @{
156 static void IRDA_SetConfig (IRDA_HandleTypeDef *hirda);
157 static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
158 static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda);
159 static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
160 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
161 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma);
162 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
163 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma);
164 static void IRDA_DMAError(DMA_HandleTypeDef *hdma);
165 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef *hdma);
166 static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
167 static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
168 static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
169 static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
170 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Tickstart,uint32_t Timeout);
171 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef *hirda);
172 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef *hirda);
174 * @}
176 /* Exported functions --------------------------------------------------------*/
177 /** @defgroup IRDA_Exported_Functions IrDA Exported Functions
178 * @{
181 /** @defgroup IRDA_Exported_Functions_Group1 IrDA Initialization and de-initialization functions
182 * @brief Initialization and Configuration functions
184 @verbatim
186 ===============================================================================
187 ##### Initialization and Configuration functions #####
188 ===============================================================================
189 [..]
190 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
191 in IrDA mode.
192 (+) For the asynchronous mode only these parameters can be configured:
193 (++) BaudRate
194 (++) WordLength
195 (++) Parity: If the parity is enabled, then the MSB bit of the data written
196 in the data register is transmitted but is changed by the parity bit.
197 Depending on the frame length defined by the M bit (8-bits or 9-bits),
198 please refer to Reference manual for possible IRDA frame formats.
199 (++) Prescaler: A pulse of width less than two and greater than one PSC period(s) may or may
200 not be rejected. The receiver set up time should be managed by software. The IrDA physical layer
201 specification specifies a minimum of 10 ms delay between transmission and
202 reception (IrDA is a half duplex protocol).
203 (++) Mode: Receiver/transmitter modes
204 (++) IrDAMode: the IrDA can operate in the Normal mode or in the Low power mode.
205 [..]
206 The HAL_IRDA_Init() API follows IRDA configuration procedures (details for the procedures
207 are available in reference manual).
209 @endverbatim
210 * @{
214 * @brief Initializes the IRDA mode according to the specified
215 * parameters in the IRDA_InitTypeDef and create the associated handle.
216 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
217 * the configuration information for the specified IRDA module.
218 * @retval HAL status
220 HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
222 /* Check the IRDA handle allocation */
223 if(hirda == NULL)
225 return HAL_ERROR;
228 /* Check the IRDA instance parameters */
229 assert_param(IS_IRDA_INSTANCE(hirda->Instance));
230 /* Check the IRDA mode parameter in the IRDA handle */
231 assert_param(IS_IRDA_POWERMODE(hirda->Init.IrDAMode));
233 if(hirda->gState == HAL_IRDA_STATE_RESET)
235 /* Allocate lock resource and initialize it */
236 hirda->Lock = HAL_UNLOCKED;
237 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
238 HAL_IRDA_MspInit(hirda);
241 hirda->gState = HAL_IRDA_STATE_BUSY;
243 /* Disable the IRDA peripheral */
244 __HAL_IRDA_DISABLE(hirda);
246 /* Set the IRDA communication parameters */
247 IRDA_SetConfig(hirda);
249 /* In IrDA mode, the following bits must be kept cleared:
250 - LINEN, STOP and CLKEN bits in the USART_CR2 register,
251 - SCEN and HDSEL bits in the USART_CR3 register.*/
252 CLEAR_BIT(hirda->Instance->CR2, USART_CR2_LINEN | USART_CR2_STOP | USART_CR2_CLKEN);
253 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_SCEN | USART_CR3_HDSEL);
255 /* Enable the IRDA peripheral */
256 __HAL_IRDA_ENABLE(hirda);
258 /* Set the prescaler */
259 MODIFY_REG(hirda->Instance->GTPR, USART_GTPR_PSC, hirda->Init.Prescaler);
261 /* Configure the IrDA mode */
262 MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.IrDAMode);
264 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
265 SET_BIT(hirda->Instance->CR3, USART_CR3_IREN);
267 /* Initialize the IRDA state*/
268 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
269 hirda->gState= HAL_IRDA_STATE_READY;
270 hirda->RxState= HAL_IRDA_STATE_READY;
272 return HAL_OK;
276 * @brief DeInitializes the IRDA peripheral
277 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
278 * the configuration information for the specified IRDA module.
279 * @retval HAL status
281 HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
283 /* Check the IRDA handle allocation */
284 if(hirda == NULL)
286 return HAL_ERROR;
289 /* Check the parameters */
290 assert_param(IS_IRDA_INSTANCE(hirda->Instance));
292 hirda->gState = HAL_IRDA_STATE_BUSY;
294 /* Disable the Peripheral */
295 __HAL_IRDA_DISABLE(hirda);
297 /* DeInit the low level hardware */
298 HAL_IRDA_MspDeInit(hirda);
300 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
302 hirda->gState = HAL_IRDA_STATE_RESET;
303 hirda->RxState = HAL_IRDA_STATE_RESET;
305 /* Release Lock */
306 __HAL_UNLOCK(hirda);
308 return HAL_OK;
312 * @brief IRDA MSP Init.
313 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
314 * the configuration information for the specified IRDA module.
315 * @retval None
317 __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
319 /* Prevent unused argument(s) compilation warning */
320 UNUSED(hirda);
321 /* NOTE : This function Should not be modified, when the callback is needed,
322 the HAL_IRDA_MspInit could be implemented in the user file
327 * @brief IRDA MSP DeInit.
328 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
329 * the configuration information for the specified IRDA module.
330 * @retval None
332 __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
334 /* Prevent unused argument(s) compilation warning */
335 UNUSED(hirda);
336 /* NOTE : This function Should not be modified, when the callback is needed,
337 the HAL_IRDA_MspDeInit could be implemented in the user file
342 * @}
345 /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
346 * @brief IRDA Transmit/Receive functions
348 @verbatim
349 ===============================================================================
350 ##### IO operation functions #####
351 ===============================================================================
352 This subsection provides a set of functions allowing to manage the IRDA data transfers.
353 [..]
354 IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
355 on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
356 is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
357 While receiving data, transmission should be avoided as the data to be transmitted
358 could be corrupted.
360 (#) There are two modes of transfer:
361 (++) Blocking mode: The communication is performed in polling mode.
362 The HAL status of all data processing is returned by the same function
363 after finishing transfer.
364 (++) No-Blocking mode: The communication is performed using Interrupts
365 or DMA, These APIs return the HAL status.
366 The end of the data processing will be indicated through the
367 dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
368 using DMA mode.
369 The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
370 will be executed respectively at the end of the transmit or Receive process
371 The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
373 (#) Blocking mode API's are :
374 (++) HAL_IRDA_Transmit()
375 (++) HAL_IRDA_Receive()
377 (#) Non Blocking mode APIs with Interrupt are :
378 (++) HAL_IRDA_Transmit_IT()
379 (++) HAL_IRDA_Receive_IT()
380 (++) HAL_IRDA_IRQHandler()
382 (#) Non Blocking mode functions with DMA are :
383 (++) HAL_IRDA_Transmit_DMA()
384 (++) HAL_IRDA_Receive_DMA()
386 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
387 (++) HAL_IRDA_TxCpltCallback()
388 (++) HAL_IRDA_RxCpltCallback()
389 (++) HAL_IRDA_ErrorCallback()
391 @endverbatim
392 * @{
396 * @brief Sends an amount of data in blocking mode.
397 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
398 * the configuration information for the specified IRDA module.
399 * @param pData: Pointer to data buffer
400 * @param Size: Amount of data to be sent
401 * @param Timeout: Specify timeout value
402 * @retval HAL status
404 HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
406 uint16_t* tmp;
407 uint32_t tickstart = 0U;
409 /* Check that a Tx process is not already ongoing */
410 if(hirda->gState == HAL_IRDA_STATE_READY)
412 if((pData == NULL) || (Size == 0))
414 return HAL_ERROR;
417 /* Process Locked */
418 __HAL_LOCK(hirda);
420 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
421 hirda->gState = HAL_IRDA_STATE_BUSY_TX;
423 /* Init tickstart for timeout managment*/
424 tickstart = HAL_GetTick();
426 hirda->TxXferSize = Size;
427 hirda->TxXferCount = Size;
428 while(hirda->TxXferCount > 0U)
430 hirda->TxXferCount--;
431 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
433 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
435 return HAL_TIMEOUT;
437 tmp = (uint16_t*) pData;
438 hirda->Instance->DR = (*tmp & (uint16_t)0x01FF);
439 if(hirda->Init.Parity == IRDA_PARITY_NONE)
441 pData +=2;
443 else
445 pData +=1;
448 else
450 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
452 return HAL_TIMEOUT;
454 hirda->Instance->DR = (*pData++ & (uint8_t)0xFF);
458 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
460 return HAL_TIMEOUT;
463 /* At end of Tx process, restore hirda->gState to Ready */
464 hirda->gState = HAL_IRDA_STATE_READY;
466 /* Process Unlocked */
467 __HAL_UNLOCK(hirda);
469 return HAL_OK;
471 else
473 return HAL_BUSY;
478 * @brief Receive an amount of data in blocking mode.
479 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
480 * the configuration information for the specified IRDA module.
481 * @param pData: Pointer to data buffer
482 * @param Size: Amount of data to be received
483 * @param Timeout: Specify timeout value
484 * @retval HAL status
486 HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
488 uint16_t* tmp;
489 uint32_t tickstart = 0U;
491 /* Check that a Rx process is not already ongoing */
492 if(hirda->RxState == HAL_IRDA_STATE_READY)
494 if((pData == NULL) || (Size == 0))
496 return HAL_ERROR;
499 /* Process Locked */
500 __HAL_LOCK(hirda);
502 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
503 hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
505 /* Init tickstart for timeout managment*/
506 tickstart = HAL_GetTick();
508 hirda->RxXferSize = Size;
509 hirda->RxXferCount = Size;
510 /* Check the remain data to be received */
511 while(hirda->RxXferCount > 0U)
513 hirda->RxXferCount--;
514 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
516 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
518 return HAL_TIMEOUT;
520 tmp = (uint16_t*) pData ;
521 if(hirda->Init.Parity == IRDA_PARITY_NONE)
523 *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x01FF);
524 pData +=2;
526 else
528 *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x00FF);
529 pData +=1;
532 else
534 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
536 return HAL_TIMEOUT;
538 if(hirda->Init.Parity == IRDA_PARITY_NONE)
540 *pData++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x00FF);
542 else
544 *pData++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x007F);
549 /* At end of Rx process, restore hirda->RxState to Ready */
550 hirda->RxState = HAL_IRDA_STATE_READY;
552 /* Process Unlocked */
553 __HAL_UNLOCK(hirda);
555 return HAL_OK;
557 else
559 return HAL_BUSY;
564 * @brief Send an amount of data in non blocking mode.
565 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
566 * the configuration information for the specified IRDA module.
567 * @param pData: Pointer to data buffer
568 * @param Size: Amount of data to be sent
569 * @retval HAL status
571 HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
573 /* Check that a Tx process is not already ongoing */
574 if(hirda->gState == HAL_IRDA_STATE_READY)
576 if((pData == NULL) || (Size == 0))
578 return HAL_ERROR;
580 /* Process Locked */
581 __HAL_LOCK(hirda);
583 hirda->pTxBuffPtr = pData;
584 hirda->TxXferSize = Size;
585 hirda->TxXferCount = Size;
586 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
587 hirda->gState = HAL_IRDA_STATE_BUSY_TX;
589 /* Process Unlocked */
590 __HAL_UNLOCK(hirda);
592 /* Enable the IRDA Transmit Data Register Empty Interrupt */
593 SET_BIT(hirda->Instance->CR1, USART_CR1_TXEIE);
595 return HAL_OK;
597 else
599 return HAL_BUSY;
604 * @brief Receives an amount of data in non blocking mode.
605 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
606 * the configuration information for the specified IRDA module.
607 * @param pData: Pointer to data buffer
608 * @param Size: Amount of data to be received
609 * @retval HAL status
611 HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
613 /* Check that a Rx process is not already ongoing */
614 if(hirda->RxState == HAL_IRDA_STATE_READY)
616 if((pData == NULL) || (Size == 0))
618 return HAL_ERROR;
621 /* Process Locked */
622 __HAL_LOCK(hirda);
624 hirda->pRxBuffPtr = pData;
625 hirda->RxXferSize = Size;
626 hirda->RxXferCount = Size;
627 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
628 hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
630 /* Process Unlocked */
631 __HAL_UNLOCK(hirda);
633 /* Enable the IRDA Parity Error and Data Register not empty Interrupts */
634 SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
636 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
637 SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
639 return HAL_OK;
641 else
643 return HAL_BUSY;
648 * @brief Sends an amount of data in non blocking mode.
649 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
650 * the configuration information for the specified IRDA module.
651 * @param pData: Pointer to data buffer
652 * @param Size: Amount of data to be sent
653 * @retval HAL status
655 HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
657 uint32_t *tmp;
659 /* Check that a Tx process is not already ongoing */
660 if(hirda->gState == HAL_IRDA_STATE_READY)
662 if((pData == NULL) || (Size == 0))
664 return HAL_ERROR;
667 /* Process Locked */
668 __HAL_LOCK(hirda);
670 hirda->pTxBuffPtr = pData;
671 hirda->TxXferSize = Size;
672 hirda->TxXferCount = Size;
673 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
674 hirda->gState = HAL_IRDA_STATE_BUSY_TX;
676 /* Set the IRDA DMA transfer complete callback */
677 hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
679 /* Set the IRDA DMA half transfer complete callback */
680 hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;
682 /* Set the DMA error callback */
683 hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
685 /* Set the DMA abort callback */
686 hirda->hdmatx->XferAbortCallback = NULL;
688 /* Enable the IRDA transmit DMA Stream */
689 tmp = (uint32_t*)&pData;
690 HAL_DMA_Start_IT(hirda->hdmatx, *(uint32_t*)tmp, (uint32_t)&hirda->Instance->DR, Size);
692 /* Clear the TC flag in the SR register by writing 0 to it */
693 __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_FLAG_TC);
695 /* Process Unlocked */
696 __HAL_UNLOCK(hirda);
698 /* Enable the DMA transfer for transmit request by setting the DMAT bit
699 in the USART CR3 register */
700 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
702 return HAL_OK;
704 else
706 return HAL_BUSY;
711 * @brief Receives an amount of data in non blocking mode.
712 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
713 * the configuration information for the specified IRDA module.
714 * @param pData: Pointer to data buffer
715 * @param Size: Amount of data to be received
716 * @note When the IRDA parity is enabled (PCE = 1) the data received contain the parity bit.
717 * @retval HAL status
719 HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
721 uint32_t *tmp;
723 /* Check that a Rx process is not already ongoing */
724 if(hirda->RxState == HAL_IRDA_STATE_READY)
726 if((pData == NULL) || (Size == 0))
728 return HAL_ERROR;
731 /* Process Locked */
732 __HAL_LOCK(hirda);
734 hirda->pRxBuffPtr = pData;
735 hirda->RxXferSize = Size;
736 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
737 hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
739 /* Set the IRDA DMA transfer complete callback */
740 hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
742 /* Set the IRDA DMA half transfer complete callback */
743 hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;
745 /* Set the DMA error callback */
746 hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
748 /* Set the DMA abort callback */
749 hirda->hdmarx->XferAbortCallback = NULL;
751 /* Enable the DMA Stream */
752 tmp = (uint32_t*)&pData;
753 HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->DR, *(uint32_t*)tmp, Size);
755 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
756 __HAL_IRDA_CLEAR_OREFLAG(hirda);
758 /* Process Unlocked */
759 __HAL_UNLOCK(hirda);
761 /* Enable the IRDA Parity Error Interrupt */
762 SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
764 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
765 SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
767 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
768 in the USART CR3 register */
769 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
771 return HAL_OK;
773 else
775 return HAL_BUSY;
780 * @brief Pauses the DMA Transfer.
781 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
782 * the configuration information for the specified IRDA module.
783 * @retval HAL status
785 HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda)
787 uint32_t dmarequest = 0x00U;
789 /* Process Locked */
790 __HAL_LOCK(hirda);
792 dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT);
793 if((hirda->gState == HAL_IRDA_STATE_BUSY_TX) && dmarequest)
795 /* Disable the IRDA DMA Tx request */
796 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
799 dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR);
800 if((hirda->RxState == HAL_IRDA_STATE_BUSY_RX) && dmarequest)
802 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
803 CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
804 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
806 /* Disable the IRDA DMA Rx request */
807 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
810 /* Process Unlocked */
811 __HAL_UNLOCK(hirda);
813 return HAL_OK;
817 * @brief Resumes the DMA Transfer.
818 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
819 * the configuration information for the specified IRDA module.
820 * @retval HAL status
822 HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda)
824 /* Process Locked */
825 __HAL_LOCK(hirda);
827 if(hirda->gState == HAL_IRDA_STATE_BUSY_TX)
829 /* Enable the IRDA DMA Tx request */
830 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
832 if(hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
834 /* Clear the Overrun flag before resuming the Rx transfer */
835 __HAL_IRDA_CLEAR_OREFLAG(hirda);
837 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
838 SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
839 SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
841 /* Enable the IRDA DMA Rx request */
842 SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
845 /* Process Unlocked */
846 __HAL_UNLOCK(hirda);
848 return HAL_OK;
852 * @brief Stops the DMA Transfer.
853 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
854 * the configuration information for the specified IRDA module.
855 * @retval HAL status
857 HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda)
859 uint32_t dmarequest = 0x00U;
860 /* The Lock is not implemented on this API to allow the user application
861 to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback():
862 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
863 and the correspond call back is executed HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback()
866 /* Stop IRDA DMA Tx request if ongoing */
867 dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT);
868 if((hirda->gState == HAL_IRDA_STATE_BUSY_TX) && dmarequest)
870 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
872 /* Abort the IRDA DMA Tx channel */
873 if(hirda->hdmatx != NULL)
875 HAL_DMA_Abort(hirda->hdmatx);
877 IRDA_EndTxTransfer(hirda);
880 /* Stop IRDA DMA Rx request if ongoing */
881 dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR);
882 if((hirda->RxState == HAL_IRDA_STATE_BUSY_RX) && dmarequest)
884 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
886 /* Abort the IRDA DMA Rx channel */
887 if(hirda->hdmarx != NULL)
889 HAL_DMA_Abort(hirda->hdmarx);
891 IRDA_EndRxTransfer(hirda);
893 return HAL_OK;
897 * @brief Abort ongoing transfers (blocking mode).
898 * @param hirda IRDA handle.
899 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
900 * This procedure performs following operations :
901 * - Disable PPP Interrupts
902 * - Disable the DMA transfer in the peripheral register (if enabled)
903 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
904 * - Set handle State to READY
905 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
906 * @retval HAL status
908 HAL_StatusTypeDef HAL_IRDA_Abort(IRDA_HandleTypeDef *hirda)
910 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
911 CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
912 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
914 /* Disable the IRDA DMA Tx request if enabled */
915 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
917 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
919 /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
920 if(hirda->hdmatx != NULL)
922 /* Set the IRDA DMA Abort callback to Null.
923 No call back execution at end of DMA abort procedure */
924 hirda->hdmatx->XferAbortCallback = NULL;
926 HAL_DMA_Abort(hirda->hdmatx);
930 /* Disable the IRDA DMA Rx request if enabled */
931 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
933 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
935 /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
936 if(hirda->hdmarx != NULL)
938 /* Set the IRDA DMA Abort callback to Null.
939 No call back execution at end of DMA abort procedure */
940 hirda->hdmarx->XferAbortCallback = NULL;
942 HAL_DMA_Abort(hirda->hdmarx);
946 /* Reset Tx and Rx transfer counters */
947 hirda->TxXferCount = 0x00U;
948 hirda->RxXferCount = 0x00U;
950 /* Reset ErrorCode */
951 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
953 /* Restore hirda->RxState and hirda->gState to Ready */
954 hirda->RxState = HAL_IRDA_STATE_READY;
955 hirda->gState = HAL_IRDA_STATE_READY;
957 return HAL_OK;
961 * @brief Abort ongoing Transmit transfer (blocking mode).
962 * @param hirda IRDA handle.
963 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
964 * This procedure performs following operations :
965 * - Disable PPP Interrupts
966 * - Disable the DMA transfer in the peripheral register (if enabled)
967 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
968 * - Set handle State to READY
969 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
970 * @retval HAL status
972 HAL_StatusTypeDef HAL_IRDA_AbortTransmit(IRDA_HandleTypeDef *hirda)
974 /* Disable TXEIE and TCIE interrupts */
975 CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
977 /* Disable the IRDA DMA Tx request if enabled */
978 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
980 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
982 /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
983 if(hirda->hdmatx != NULL)
985 /* Set the IRDA DMA Abort callback to Null.
986 No call back execution at end of DMA abort procedure */
987 hirda->hdmatx->XferAbortCallback = NULL;
989 HAL_DMA_Abort(hirda->hdmatx);
993 /* Reset Tx transfer counter */
994 hirda->TxXferCount = 0x00U;
996 /* Restore hirda->gState to Ready */
997 hirda->gState = HAL_IRDA_STATE_READY;
999 return HAL_OK;
1003 * @brief Abort ongoing Receive transfer (blocking mode).
1004 * @param hirda IRDA handle.
1005 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1006 * This procedure performs following operations :
1007 * - Disable PPP Interrupts
1008 * - Disable the DMA transfer in the peripheral register (if enabled)
1009 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1010 * - Set handle State to READY
1011 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1012 * @retval HAL status
1014 HAL_StatusTypeDef HAL_IRDA_AbortReceive(IRDA_HandleTypeDef *hirda)
1016 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1017 CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1018 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1020 /* Disable the IRDA DMA Rx request if enabled */
1021 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1023 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1025 /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
1026 if(hirda->hdmarx != NULL)
1028 /* Set the IRDA DMA Abort callback to Null.
1029 No call back execution at end of DMA abort procedure */
1030 hirda->hdmarx->XferAbortCallback = NULL;
1032 HAL_DMA_Abort(hirda->hdmarx);
1036 /* Reset Rx transfer counter */
1037 hirda->RxXferCount = 0x00U;
1039 /* Restore hirda->RxState to Ready */
1040 hirda->RxState = HAL_IRDA_STATE_READY;
1042 return HAL_OK;
1046 * @brief Abort ongoing transfers (Interrupt mode).
1047 * @param hirda IRDA handle.
1048 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1049 * This procedure performs following operations :
1050 * - Disable PPP Interrupts
1051 * - Disable the DMA transfer in the peripheral register (if enabled)
1052 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1053 * - Set handle State to READY
1054 * - At abort completion, call user abort complete callback
1055 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1056 * considered as completed only when user abort complete callback is executed (not when exiting function).
1057 * @retval HAL status
1059 HAL_StatusTypeDef HAL_IRDA_Abort_IT(IRDA_HandleTypeDef *hirda)
1061 uint32_t AbortCplt = 1U;
1063 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1064 CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1065 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1067 /* If DMA Tx and/or DMA Rx Handles are associated to IRDA Handle, DMA Abort complete callbacks should be initialised
1068 before any call to DMA Abort functions */
1069 /* DMA Tx Handle is valid */
1070 if(hirda->hdmatx != NULL)
1072 /* Set DMA Abort Complete callback if IRDA DMA Tx request if enabled.
1073 Otherwise, set it to NULL */
1074 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
1076 hirda->hdmatx->XferAbortCallback = IRDA_DMATxAbortCallback;
1078 else
1080 hirda->hdmatx->XferAbortCallback = NULL;
1083 /* DMA Rx Handle is valid */
1084 if(hirda->hdmarx != NULL)
1086 /* Set DMA Abort Complete callback if IRDA DMA Rx request if enabled.
1087 Otherwise, set it to NULL */
1088 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1090 hirda->hdmarx->XferAbortCallback = IRDA_DMARxAbortCallback;
1092 else
1094 hirda->hdmarx->XferAbortCallback = NULL;
1098 /* Disable the IRDA DMA Tx request if enabled */
1099 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
1101 /* Disable DMA Tx at IRDA level */
1102 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1104 /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
1105 if(hirda->hdmatx != NULL)
1107 /* IRDA Tx DMA Abort callback has already been initialised :
1108 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1110 /* Abort DMA TX */
1111 if(HAL_DMA_Abort_IT(hirda->hdmatx) != HAL_OK)
1113 hirda->hdmatx->XferAbortCallback = NULL;
1115 else
1117 AbortCplt = 0x00U;
1122 /* Disable the IRDA DMA Rx request if enabled */
1123 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1125 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1127 /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
1128 if(hirda->hdmarx != NULL)
1130 /* IRDA Rx DMA Abort callback has already been initialised :
1131 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1133 /* Abort DMA RX */
1134 if(HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
1136 hirda->hdmarx->XferAbortCallback = NULL;
1137 AbortCplt = 0x01U;
1139 else
1141 AbortCplt = 0x00U;
1146 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1147 if(AbortCplt == 0x01U)
1149 /* Reset Tx and Rx transfer counters */
1150 hirda->TxXferCount = 0x00U;
1151 hirda->RxXferCount = 0x00U;
1153 /* Reset ErrorCode */
1154 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1156 /* Restore hirda->gState and hirda->RxState to Ready */
1157 hirda->gState = HAL_IRDA_STATE_READY;
1158 hirda->RxState = HAL_IRDA_STATE_READY;
1160 /* As no DMA to be aborted, call directly user Abort complete callback */
1161 HAL_IRDA_AbortCpltCallback(hirda);
1164 return HAL_OK;
1168 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1169 * @param hirda IRDA handle.
1170 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1171 * This procedure performs following operations :
1172 * - Disable PPP Interrupts
1173 * - Disable the DMA transfer in the peripheral register (if enabled)
1174 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1175 * - Set handle State to READY
1176 * - At abort completion, call user abort complete callback
1177 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1178 * considered as completed only when user abort complete callback is executed (not when exiting function).
1179 * @retval HAL status
1181 HAL_StatusTypeDef HAL_IRDA_AbortTransmit_IT(IRDA_HandleTypeDef *hirda)
1183 /* Disable TXEIE and TCIE interrupts */
1184 CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1186 /* Disable the IRDA DMA Tx request if enabled */
1187 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
1189 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1191 /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
1192 if(hirda->hdmatx != NULL)
1194 /* Set the IRDA DMA Abort callback :
1195 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1196 hirda->hdmatx->XferAbortCallback = IRDA_DMATxOnlyAbortCallback;
1198 /* Abort DMA TX */
1199 if(HAL_DMA_Abort_IT(hirda->hdmatx) != HAL_OK)
1201 /* Call Directly hirda->hdmatx->XferAbortCallback function in case of error */
1202 hirda->hdmatx->XferAbortCallback(hirda->hdmatx);
1205 else
1207 /* Reset Tx transfer counter */
1208 hirda->TxXferCount = 0x00U;
1210 /* Restore hirda->gState to Ready */
1211 hirda->gState = HAL_IRDA_STATE_READY;
1213 /* As no DMA to be aborted, call directly user Abort complete callback */
1214 HAL_IRDA_AbortTransmitCpltCallback(hirda);
1217 else
1219 /* Reset Tx transfer counter */
1220 hirda->TxXferCount = 0x00U;
1222 /* Restore hirda->gState to Ready */
1223 hirda->gState = HAL_IRDA_STATE_READY;
1225 /* As no DMA to be aborted, call directly user Abort complete callback */
1226 HAL_IRDA_AbortTransmitCpltCallback(hirda);
1229 return HAL_OK;
1233 * @brief Abort ongoing Receive transfer (Interrupt mode).
1234 * @param hirda IRDA handle.
1235 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1236 * This procedure performs following operations :
1237 * - Disable PPP Interrupts
1238 * - Disable the DMA transfer in the peripheral register (if enabled)
1239 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1240 * - Set handle State to READY
1241 * - At abort completion, call user abort complete callback
1242 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1243 * considered as completed only when user abort complete callback is executed (not when exiting function).
1244 * @retval HAL status
1246 HAL_StatusTypeDef HAL_IRDA_AbortReceive_IT(IRDA_HandleTypeDef *hirda)
1248 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1249 CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1250 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1252 /* Disable the IRDA DMA Rx request if enabled */
1253 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1255 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1257 /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
1258 if(hirda->hdmarx != NULL)
1260 /* Set the IRDA DMA Abort callback :
1261 will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
1262 hirda->hdmarx->XferAbortCallback = IRDA_DMARxOnlyAbortCallback;
1264 /* Abort DMA RX */
1265 if(HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
1267 /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
1268 hirda->hdmarx->XferAbortCallback(hirda->hdmarx);
1271 else
1273 /* Reset Rx transfer counter */
1274 hirda->RxXferCount = 0x00U;
1276 /* Restore hirda->RxState to Ready */
1277 hirda->RxState = HAL_IRDA_STATE_READY;
1279 /* As no DMA to be aborted, call directly user Abort complete callback */
1280 HAL_IRDA_AbortReceiveCpltCallback(hirda);
1283 else
1285 /* Reset Rx transfer counter */
1286 hirda->RxXferCount = 0x00U;
1288 /* Restore hirda->RxState to Ready */
1289 hirda->RxState = HAL_IRDA_STATE_READY;
1291 /* As no DMA to be aborted, call directly user Abort complete callback */
1292 HAL_IRDA_AbortReceiveCpltCallback(hirda);
1295 return HAL_OK;
1299 * @brief This function handles IRDA interrupt request.
1300 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1301 * the configuration information for the specified IRDA module.
1302 * @retval None
1304 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
1306 uint32_t isrflags = READ_REG(hirda->Instance->SR);
1307 uint32_t cr1its = READ_REG(hirda->Instance->CR1);
1308 uint32_t cr3its = READ_REG(hirda->Instance->CR3);
1309 uint32_t errorflags = 0x00U;
1310 uint32_t dmarequest = 0x00U;
1312 /* If no error occurs */
1313 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
1314 if(errorflags == RESET)
1316 /* IRDA in mode Receiver -----------------------------------------------*/
1317 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1319 IRDA_Receive_IT(hirda);
1320 return;
1324 /* If some errors occur */
1325 if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
1327 /* IRDA parity error interrupt occurred -------------------------------*/
1328 if(((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1330 hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
1333 /* IRDA noise error interrupt occurred --------------------------------*/
1334 if(((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1336 hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
1339 /* IRDA frame error interrupt occurred --------------------------------*/
1340 if(((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1342 hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
1345 /* IRDA Over-Run interrupt occurred -----------------------------------*/
1346 if(((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1348 hirda->ErrorCode |= HAL_IRDA_ERROR_ORE;
1350 /* Call IRDA Error Call back function if need be -----------------------*/
1351 if(hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
1353 /* IRDA in mode Receiver ---------------------------------------------*/
1354 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1356 IRDA_Receive_IT(hirda);
1359 dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR);
1360 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1361 consider error as blocking */
1362 if(((hirda->ErrorCode & HAL_IRDA_ERROR_ORE) != RESET) || dmarequest)
1364 /* Blocking error : transfer is aborted
1365 Set the IRDA state ready to be able to start again the process,
1366 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1367 IRDA_EndRxTransfer(hirda);
1369 /* Disable the IRDA DMA Rx request if enabled */
1370 if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
1372 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1374 /* Abort the IRDA DMA Rx channel */
1375 if(hirda->hdmarx != NULL)
1377 /* Set the IRDA DMA Abort callback :
1378 will lead to call HAL_IRDA_ErrorCallback() at end of DMA abort procedure */
1379 hirda->hdmarx->XferAbortCallback = IRDA_DMAAbortOnError;
1381 if(HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
1383 /* Call Directly XferAbortCallback function in case of error */
1384 hirda->hdmarx->XferAbortCallback(hirda->hdmarx);
1387 else
1389 /* Call user error callback */
1390 HAL_IRDA_ErrorCallback(hirda);
1393 else
1395 /* Call user error callback */
1396 HAL_IRDA_ErrorCallback(hirda);
1399 else
1401 /* Non Blocking error : transfer could go on.
1402 Error is notified to user through user error callback */
1403 HAL_IRDA_ErrorCallback(hirda);
1404 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1407 return;
1408 } /* End if some error occurs */
1410 /* IRDA in mode Transmitter ------------------------------------------------*/
1411 if(((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1413 IRDA_Transmit_IT(hirda);
1414 return;
1417 /* IRDA in mode Transmitter end --------------------------------------------*/
1418 if(((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
1420 IRDA_EndTransmit_IT(hirda);
1421 return;
1426 * @brief End ongoing Tx transfer on IRDA peripheral (following error detection or Transmit completion).
1427 * @param hirda: IRDA handle.
1428 * @retval None
1430 static void IRDA_EndTxTransfer(IRDA_HandleTypeDef *hirda)
1432 /* Disable TXEIE and TCIE interrupts */
1433 CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1435 /* At end of Tx process, restore hirda->gState to Ready */
1436 hirda->gState = HAL_IRDA_STATE_READY;
1440 * @brief End ongoing Rx transfer on IRDA peripheral (following error detection or Reception completion).
1441 * @param hirda: IRDA handle.
1442 * @retval None
1444 static void IRDA_EndRxTransfer(IRDA_HandleTypeDef *hirda)
1446 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1447 CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1448 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1450 /* At end of Rx process, restore hirda->RxState to Ready */
1451 hirda->RxState = HAL_IRDA_STATE_READY;
1455 * @brief DMA IRDA communication abort callback, when initiated by HAL services on Error
1456 * (To be called at end of DMA Abort procedure following error occurrence).
1457 * @param hdma DMA handle.
1458 * @retval None
1460 static void IRDA_DMAAbortOnError(DMA_HandleTypeDef *hdma)
1462 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1463 hirda->RxXferCount = 0x00U;
1464 hirda->TxXferCount = 0x00U;
1466 HAL_IRDA_ErrorCallback(hirda);
1470 * @brief DMA IRDA Tx communication abort callback, when initiated by user
1471 * (To be called at end of DMA Tx Abort procedure following user abort request).
1472 * @note When this callback is executed, User Abort complete call back is called only if no
1473 * Abort still ongoing for Rx DMA Handle.
1474 * @param hdma DMA handle.
1475 * @retval None
1477 static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
1479 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1481 hirda->hdmatx->XferAbortCallback = NULL;
1483 /* Check if an Abort process is still ongoing */
1484 if(hirda->hdmarx != NULL)
1486 if(hirda->hdmarx->XferAbortCallback != NULL)
1488 return;
1492 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
1493 hirda->TxXferCount = 0x00U;
1494 hirda->RxXferCount = 0x00U;
1496 /* Reset ErrorCode */
1497 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1499 /* Restore hirda->gState and hirda->RxState to Ready */
1500 hirda->gState = HAL_IRDA_STATE_READY;
1501 hirda->RxState = HAL_IRDA_STATE_READY;
1503 /* Call user Abort complete callback */
1504 HAL_IRDA_AbortCpltCallback(hirda);
1508 * @brief DMA IRDA Rx communication abort callback, when initiated by user
1509 * (To be called at end of DMA Rx Abort procedure following user abort request).
1510 * @note When this callback is executed, User Abort complete call back is called only if no
1511 * Abort still ongoing for Tx DMA Handle.
1512 * @param hdma DMA handle.
1513 * @retval None
1515 static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
1517 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1519 hirda->hdmarx->XferAbortCallback = NULL;
1521 /* Check if an Abort process is still ongoing */
1522 if(hirda->hdmatx != NULL)
1524 if(hirda->hdmatx->XferAbortCallback != NULL)
1526 return;
1530 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
1531 hirda->TxXferCount = 0x00U;
1532 hirda->RxXferCount = 0x00U;
1534 /* Reset ErrorCode */
1535 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
1537 /* Restore hirda->gState and hirda->RxState to Ready */
1538 hirda->gState = HAL_IRDA_STATE_READY;
1539 hirda->RxState = HAL_IRDA_STATE_READY;
1541 /* Call user Abort complete callback */
1542 HAL_IRDA_AbortCpltCallback(hirda);
1546 * @brief DMA IRDA Tx communication abort callback, when initiated by user by a call to
1547 * HAL_IRDA_AbortTransmit_IT API (Abort only Tx transfer)
1548 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
1549 * and leads to user Tx Abort Complete callback execution).
1550 * @param hdma DMA handle.
1551 * @retval None
1553 static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
1555 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1557 hirda->TxXferCount = 0x00U;
1559 /* Restore hirda->gState to Ready */
1560 hirda->gState = HAL_IRDA_STATE_READY;
1562 /* Call user Abort complete callback */
1563 HAL_IRDA_AbortTransmitCpltCallback(hirda);
1567 * @brief DMA IRDA Rx communication abort callback, when initiated by user by a call to
1568 * HAL_IRDA_AbortReceive_IT API (Abort only Rx transfer)
1569 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
1570 * and leads to user Rx Abort Complete callback execution).
1571 * @param hdma DMA handle.
1572 * @retval None
1574 static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
1576 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1578 hirda->RxXferCount = 0x00U;
1580 /* Restore hirda->RxState to Ready */
1581 hirda->RxState = HAL_IRDA_STATE_READY;
1583 /* Call user Abort complete callback */
1584 HAL_IRDA_AbortReceiveCpltCallback(hirda);
1588 * @brief Tx Transfer complete callbacks.
1589 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1590 * the configuration information for the specified IRDA module.
1591 * @retval None
1593 __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
1595 /* Prevent unused argument(s) compilation warning */
1596 UNUSED(hirda);
1597 /* NOTE : This function Should not be modified, when the callback is needed,
1598 the HAL_IRDA_TxCpltCallback could be implemented in the user file
1603 * @brief Tx Half Transfer completed callbacks.
1604 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1605 * the configuration information for the specified USART module.
1606 * @retval None
1608 __weak void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
1610 /* Prevent unused argument(s) compilation warning */
1611 UNUSED(hirda);
1612 /* NOTE: This function Should not be modified, when the callback is needed,
1613 the HAL_IRDA_TxHalfCpltCallback could be implemented in the user file
1618 * @brief Rx Transfer complete callbacks.
1619 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1620 * the configuration information for the specified IRDA module.
1621 * @retval None
1623 __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
1625 /* Prevent unused argument(s) compilation warning */
1626 UNUSED(hirda);
1627 /* NOTE : This function Should not be modified, when the callback is needed,
1628 the HAL_IRDA_RxCpltCallback could be implemented in the user file
1633 * @brief Rx Half Transfer complete callbacks.
1634 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1635 * the configuration information for the specified IRDA module.
1636 * @retval None
1638 __weak void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
1640 /* Prevent unused argument(s) compilation warning */
1641 UNUSED(hirda);
1642 /* NOTE : This function Should not be modified, when the callback is needed,
1643 the HAL_IRDA_RxHalfCpltCallback could be implemented in the user file
1648 * @brief IRDA error callbacks.
1649 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1650 * the configuration information for the specified IRDA module.
1651 * @retval None
1653 __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
1655 /* Prevent unused argument(s) compilation warning */
1656 UNUSED(hirda);
1657 /* NOTE : This function Should not be modified, when the callback is needed,
1658 the HAL_IRDA_ErrorCallback could be implemented in the user file
1663 * @brief IRDA Abort Complete callback.
1664 * @param hirda IRDA handle.
1665 * @retval None
1667 __weak void HAL_IRDA_AbortCpltCallback(IRDA_HandleTypeDef *hirda)
1669 /* Prevent unused argument(s) compilation warning */
1670 UNUSED(hirda);
1672 /* NOTE : This function should not be modified, when the callback is needed,
1673 the HAL_IRDA_AbortCpltCallback can be implemented in the user file.
1678 * @brief IRDA Abort Transmit Complete callback.
1679 * @param hirda IRDA handle.
1680 * @retval None
1682 __weak void HAL_IRDA_AbortTransmitCpltCallback(IRDA_HandleTypeDef *hirda)
1684 /* Prevent unused argument(s) compilation warning */
1685 UNUSED(hirda);
1687 /* NOTE : This function should not be modified, when the callback is needed,
1688 the HAL_IRDA_AbortTransmitCpltCallback can be implemented in the user file.
1693 * @brief IRDA Abort ReceiveComplete callback.
1694 * @param hirda IRDA handle.
1695 * @retval None
1697 __weak void HAL_IRDA_AbortReceiveCpltCallback(IRDA_HandleTypeDef *hirda)
1699 /* Prevent unused argument(s) compilation warning */
1700 UNUSED(hirda);
1702 /* NOTE : This function should not be modified, when the callback is needed,
1703 the HAL_IRDA_AbortReceiveCpltCallback can be implemented in the user file.
1708 * @}
1711 /** @defgroup IRDA_Exported_Functions_Group3 Peripheral State and Errors functions
1712 * @brief IRDA State and Errors functions
1714 @verbatim
1715 ==============================================================================
1716 ##### Peripheral State and Errors functions #####
1717 ==============================================================================
1718 [..]
1719 This subsection provides a set of functions allowing to return the State of IrDA
1720 communication process and also return Peripheral Errors occurred during communication process
1721 (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state of the IrDA peripheral.
1722 (+) HAL_IRDA_GetError() check in run-time errors that could be occurred during communication.
1724 @endverbatim
1725 * @{
1729 * @brief Returns the IRDA state.
1730 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1731 * the configuration information for the specified IRDA module.
1732 * @retval HAL state
1734 HAL_IRDA_StateTypeDef HAL_IRDA_GetState(IRDA_HandleTypeDef *hirda)
1736 uint32_t temp1 = 0x00U, temp2 = 0x00U;
1737 temp1 = hirda->gState;
1738 temp2 = hirda->RxState;
1740 return (HAL_IRDA_StateTypeDef)(temp1 | temp2);
1744 * @brief Return the IARDA error code
1745 * @param hirda : pointer to a IRDA_HandleTypeDef structure that contains
1746 * the configuration information for the specified IRDA.
1747 * @retval IRDA Error Code
1749 uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef *hirda)
1751 return hirda->ErrorCode;
1755 * @}
1759 * @brief DMA IRDA transmit process complete callback.
1760 * @param hdma : DMA handle
1761 * @retval None
1763 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1765 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1766 /* DMA Normal mode */
1767 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
1769 hirda->TxXferCount = 0U;
1771 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1772 in the IRDA CR3 register */
1773 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
1775 /* Enable the IRDA Transmit Complete Interrupt */
1776 SET_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
1778 /* DMA Circular mode */
1779 else
1781 HAL_IRDA_TxCpltCallback(hirda);
1786 * @brief DMA IRDA receive process half complete callback
1787 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1788 * the configuration information for the specified DMA module.
1789 * @retval None
1791 static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma)
1793 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1795 HAL_IRDA_TxHalfCpltCallback(hirda);
1799 * @brief DMA IRDA receive process complete callback.
1800 * @param hdma: DMA handle
1801 * @retval None
1803 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1805 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1806 /* DMA Normal mode */
1807 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
1809 hirda->RxXferCount = 0U;
1811 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1812 CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
1813 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1815 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1816 in the IRDA CR3 register */
1817 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
1819 /* At end of Rx process, restore hirda->RxState to Ready */
1820 hirda->RxState = HAL_IRDA_STATE_READY;
1823 HAL_IRDA_RxCpltCallback(hirda);
1827 * @brief DMA IRDA receive process half complete callback
1828 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1829 * the configuration information for the specified DMA module.
1830 * @retval None
1832 static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma)
1834 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1835 HAL_IRDA_RxHalfCpltCallback(hirda);
1839 * @brief DMA IRDA communication error callback.
1840 * @param hdma: DMA handle
1841 * @retval None
1843 static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
1845 uint32_t dmarequest = 0x00U;
1846 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1848 /* Stop IRDA DMA Tx request if ongoing */
1849 dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT);
1850 if((hirda->gState == HAL_IRDA_STATE_BUSY_TX) && dmarequest)
1852 hirda->TxXferCount = 0U;
1853 IRDA_EndTxTransfer(hirda);
1856 /* Stop IRDA DMA Rx request if ongoing */
1857 dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR);
1858 if((hirda->RxState == HAL_IRDA_STATE_BUSY_RX) && dmarequest)
1860 hirda->RxXferCount = 0U;
1861 IRDA_EndRxTransfer(hirda);
1864 hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
1866 HAL_IRDA_ErrorCallback(hirda);
1870 * @brief This function handles IRDA Communication Timeout.
1871 * @param hirda pointer to a IRDA_HandleTypeDef structure that contains
1872 * the configuration information for the specified IRDA module.
1873 * @param Flag specifies the IRDA flag to check.
1874 * @param Status The new Flag status (SET or RESET).
1875 * @param Tickstart Tick start value
1876 * @param Timeout Timeout duration
1877 * @retval HAL status
1879 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
1881 /* Wait until flag is set */
1882 while((__HAL_IRDA_GET_FLAG(hirda, Flag) ? SET : RESET) == Status)
1884 /* Check for the Timeout */
1885 if(Timeout != HAL_MAX_DELAY)
1887 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
1889 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1890 CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
1891 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1893 hirda->gState = HAL_IRDA_STATE_READY;
1894 hirda->RxState = HAL_IRDA_STATE_READY;
1896 /* Process Unlocked */
1897 __HAL_UNLOCK(hirda);
1899 return HAL_TIMEOUT;
1903 return HAL_OK;
1907 * @brief Send an amount of data in non blocking mode.
1908 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1909 * the configuration information for the specified IRDA module.
1910 * @retval HAL status
1912 static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
1914 uint16_t* tmp;
1916 /* Check that a Tx process is ongoing */
1917 if(hirda->gState == HAL_IRDA_STATE_BUSY_TX)
1919 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
1921 tmp = (uint16_t*) hirda->pTxBuffPtr;
1922 hirda->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
1923 if(hirda->Init.Parity == IRDA_PARITY_NONE)
1925 hirda->pTxBuffPtr += 2U;
1927 else
1929 hirda->pTxBuffPtr += 1U;
1932 else
1934 hirda->Instance->DR = (uint8_t)(*hirda->pTxBuffPtr++ & (uint8_t)0x00FF);
1937 if(--hirda->TxXferCount == 0U)
1939 /* Disable the IRDA Transmit Data Register Empty Interrupt */
1940 CLEAR_BIT(hirda->Instance->CR1, USART_CR1_TXEIE);
1942 /* Enable the IRDA Transmit Complete Interrupt */
1943 SET_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
1946 return HAL_OK;
1948 else
1950 return HAL_BUSY;
1955 * @brief Wraps up transmission in non blocking mode.
1956 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1957 * the configuration information for the specified IRDA module.
1958 * @retval HAL status
1960 static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda)
1962 /* Disable the IRDA Transmit Complete Interrupt */
1963 CLEAR_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
1965 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
1966 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
1968 /* Tx process is ended, restore hirda->gState to Ready */
1969 hirda->gState = HAL_IRDA_STATE_READY;
1971 HAL_IRDA_TxCpltCallback(hirda);
1973 return HAL_OK;
1977 * @brief Receives an amount of data in non blocking mode.
1978 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
1979 * the configuration information for the specified IRDA module.
1980 * @retval HAL status
1982 static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
1984 uint16_t* tmp;
1985 uint16_t uhdata;
1987 /* Check that a Rx process is ongoing */
1988 if(hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
1990 uhdata = (uint16_t) READ_REG(hirda->Instance->DR);
1991 if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
1993 tmp = (uint16_t*) hirda->pRxBuffPtr;
1994 if(hirda->Init.Parity == IRDA_PARITY_NONE)
1996 *tmp = (uint16_t)(uhdata & (uint16_t)0x01FF);
1997 hirda->pRxBuffPtr += 2U;
1999 else
2001 *tmp = (uint16_t)(uhdata & (uint16_t)0x00FF);
2002 hirda->pRxBuffPtr += 1U;
2005 else
2007 if(hirda->Init.Parity == IRDA_PARITY_NONE)
2009 *hirda->pRxBuffPtr++ = (uint8_t)(uhdata & (uint8_t)0x00FF);
2011 else
2013 *hirda->pRxBuffPtr++ = (uint8_t)(uhdata & (uint8_t)0x007F);
2017 if(--hirda->RxXferCount == 0U)
2019 /* Disable the IRDA Data Register not empty Interrupt */
2020 CLEAR_BIT(hirda->Instance->CR1, USART_CR1_RXNEIE);
2022 /* Disable the IRDA Parity Error Interrupt */
2023 CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
2025 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
2026 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
2028 /* Rx process is completed, restore hirda->RxState to Ready */
2029 hirda->RxState = HAL_IRDA_STATE_READY;
2031 HAL_IRDA_RxCpltCallback(hirda);
2033 return HAL_OK;
2035 return HAL_OK;
2037 else
2039 return HAL_BUSY;
2044 * @brief Configures the IRDA peripheral.
2045 * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
2046 * the configuration information for the specified IRDA module.
2047 * @retval None
2049 static void IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
2051 /* Check the parameters */
2052 assert_param(IS_IRDA_INSTANCE(hirda->Instance));
2053 assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));
2054 assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
2055 assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
2056 assert_param(IS_IRDA_MODE(hirda->Init.Mode));
2058 /*-------------------------- IRDA CR2 Configuration ------------------------*/
2059 /* Clear STOP[13:12] bits */
2060 CLEAR_BIT(hirda->Instance->CR2, USART_CR2_STOP);
2062 /*-------------------------- USART CR1 Configuration -----------------------*/
2063 /* Clear M, PCE, PS, TE and RE bits */
2064 CLEAR_BIT(hirda->Instance->CR1, USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE);
2066 /* Configure the USART Word Length, Parity and mode:
2067 Set the M bits according to hirda->Init.WordLength value
2068 Set PCE and PS bits according to hirda->Init.Parity value
2069 Set TE and RE bits according to hirda->Init.Mode value */
2070 /* Write to USART CR1 */
2071 SET_BIT(hirda->Instance->CR1, (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode);
2073 /*-------------------------- USART CR3 Configuration -----------------------*/
2074 /* Clear CTSE and RTSE bits */
2075 CLEAR_BIT(hirda->Instance->CR3, USART_CR3_RTSE | USART_CR3_CTSE);
2077 /*-------------------------- USART BRR Configuration -----------------------*/
2078 #if defined(USART6)
2079 if((hirda->Instance == USART1) || (hirda->Instance == USART6))
2081 SET_BIT(hirda->Instance->BRR, IRDA_BRR(HAL_RCC_GetPCLK2Freq(), hirda->Init.BaudRate));
2083 #else
2084 if(hirda->Instance == USART1)
2086 SET_BIT(hirda->Instance->BRR, IRDA_BRR(HAL_RCC_GetPCLK2Freq(), hirda->Init.BaudRate));
2088 #endif /* USART6 */
2089 else
2091 SET_BIT(hirda->Instance->BRR, IRDA_BRR(HAL_RCC_GetPCLK1Freq(), hirda->Init.BaudRate));
2096 * @}
2099 #endif /* HAL_IRDA_MODULE_ENABLED */
2101 * @}
2105 * @}
2108 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/