Subversion Repositories LedShow

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f1xx_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 SIR ENDEC block (IrDA):
  8.   *           + Initialization and de-initialization functions
  9.   *           + IO operation functions
  10.   *           + Peripheral Control functions
  11.   *           + Peripheral State and Errors functions  
  12.   @verbatim
  13.   ==============================================================================
  14.                         ##### How to use this driver #####
  15.   ==============================================================================
  16.   [..]
  17.     The IRDA HAL driver can be used as follows:
  18.  
  19.     (#) Declare a IRDA_HandleTypeDef handle structure.
  20.     (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API:
  21.         (##) Enable the USARTx interface clock.
  22.         (##) IRDA pins configuration:
  23.             (+++) Enable the clock for the IRDA GPIOs.
  24.             (+++) Configure the IRDA pins as alternate function pull-up.
  25.         (##) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
  26.              and HAL_IRDA_Receive_IT() APIs):
  27.             (+++) Configure the USARTx interrupt priority.
  28.             (+++) Enable the NVIC USART IRQ handle.
  29.         (##) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
  30.              and HAL_IRDA_Receive_DMA() APIs):
  31.             (+++) Declare a DMA handle structure for the Tx/Rx channel.
  32.             (+++) Enable the DMAx interface clock.
  33.             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  34.             (+++) Configure the DMA Tx/Rx channel.
  35.             (+++) Associate the initialized DMA handle to the IRDA DMA Tx/Rx handle.
  36.             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  37.             (+++) Configure the IRDAx interrupt priority and enable the NVIC USART IRQ handle
  38.                   (used for last byte sending completion detection in DMA non circular mode)
  39.  
  40.     (#) Program the Baud Rate, Word Length, Parity, IrDA Mode, Prescaler
  41.         and Mode(Receiver/Transmitter) in the hirda Init structure.
  42.  
  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.      [..]
  47.         (@) The specific IRDA interrupts (Transmission complete interrupt,
  48.              RXNE interrupt and Error Interrupts) will be managed using the macros
  49.              __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
  50.      [..]
  51.         Three operation modes are available within this driver :
  52.  
  53.      *** Polling mode IO operation ***
  54.      =================================
  55.      [..]    
  56.        (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
  57.        (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
  58.        
  59.      *** Interrupt mode IO operation ***
  60.      ===================================
  61.      [..]    
  62.        (+) Send an amount of data in non blocking mode using HAL_IRDA_Transmit_IT()
  63.        (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
  64.             add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
  65.        (+) Receive an amount of data in non blocking mode using HAL_IRDA_Receive_IT()
  66.        (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
  67.             add his own code by customization of function pointer HAL_IRDA_RxCpltCallback                                      
  68.        (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
  69.             add his own code by customization of function pointer HAL_IRDA_ErrorCallback
  70.  
  71.      *** DMA mode IO operation ***
  72.      ==============================
  73.      [..]
  74.        (+) Send an amount of data in non blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
  75.        (+) At transmission end of half transfer HAL_IRDA_TxHalfCpltCallback is executed and user can
  76.             add his own code by customization of function pointer HAL_IRDA_TxHalfCpltCallback
  77.        (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
  78.             add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
  79.        (+) Receive an amount of data in non blocking mode (DMA) using HAL_IRDA_Receive_DMA()
  80.        (+) At reception end of half transfer HAL_IRDA_RxHalfCpltCallback is executed and user can
  81.             add his own code by customization of function pointer HAL_IRDA_RxHalfCpltCallback
  82.        (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
  83.             add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
  84.        (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
  85.             add his own code by customization of function pointer HAL_IRDA_ErrorCallback
  86.        (+) Pause the DMA Transfer using HAL_IRDA_DMAPause()
  87.        (+) Resume the DMA Transfer using HAL_IRDA_DMAResume()
  88.        (+) Stop the DMA Transfer using HAL_IRDA_DMAStop()
  89.  
  90.      *** IRDA HAL driver macros list ***
  91.      ====================================
  92.      [..]
  93.        Below the list of most used macros in IRDA HAL driver.
  94.  
  95.        (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
  96.        (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
  97.        (+) __HAL_IRDA_GET_FLAG : Check whether the specified IRDA flag is set or not
  98.        (+) __HAL_IRDA_CLEAR_FLAG : Clear the specified IRDA pending flag
  99.        (+) __HAL_IRDA_ENABLE_IT: Enable the specified IRDA interrupt
  100.        (+) __HAL_IRDA_DISABLE_IT: Disable the specified IRDA interrupt
  101.        (+) __HAL_IRDA_GET_IT_SOURCE: Check whether the specified IRDA interrupt has occurred or not
  102.  
  103.      [..]
  104.        (@) You can refer to the IRDA HAL driver header file for more useful macros
  105.   @endverbatim
  106.      [..]
  107.        (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
  108.            in the data register is transmitted but is changed by the parity bit.
  109.            Depending on the frame length defined by the M bit (8-bits or 9-bits),
  110.            the possible IRDA frame formats are as listed in the following table:
  111.     +-------------------------------------------------------------+
  112.     |   M bit |  PCE bit  |            IRDA frame                 |
  113.     |---------------------|---------------------------------------|
  114.     |    0    |    0      |    | SB | 8 bit data | 1 STB |          |
  115.     |---------|-----------|---------------------------------------|
  116.     |    0    |    1      |    | SB | 7 bit data | PB | 1 STB |     |
  117.     |---------|-----------|---------------------------------------|
  118.     |    1    |    0      |    | SB | 9 bit data | 1 STB |          |
  119.     |---------|-----------|---------------------------------------|
  120.     |    1    |    1      |    | SB | 8 bit data | PB | 1 STB |     |
  121.     +-------------------------------------------------------------+
  122.   ******************************************************************************
  123.   * @attention
  124.   *
  125.   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  126.   *
  127.   * Redistribution and use in source and binary forms, with or without modification,
  128.   * are permitted provided that the following conditions are met:
  129.   *   1. Redistributions of source code must retain the above copyright notice,
  130.   *      this list of conditions and the following disclaimer.
  131.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  132.   *      this list of conditions and the following disclaimer in the documentation
  133.   *      and/or other materials provided with the distribution.
  134.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  135.   *      may be used to endorse or promote products derived from this software
  136.   *      without specific prior written permission.
  137.   *
  138.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  139.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  140.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  141.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  142.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  143.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  144.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  145.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  146.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  147.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  148.   *
  149.   ******************************************************************************
  150.   */
  151.  
  152. /* Includes ------------------------------------------------------------------*/
  153. #include "stm32f1xx_hal.h"
  154.  
  155. /** @addtogroup STM32F1xx_HAL_Driver
  156.   * @{
  157.   */
  158.  
  159. /** @defgroup IRDA IRDA
  160.   * @brief HAL IRDA module driver
  161.   * @{
  162.   */
  163. #ifdef HAL_IRDA_MODULE_ENABLED
  164.  
  165. /* Private typedef -----------------------------------------------------------*/
  166. /* Private define ------------------------------------------------------------*/
  167. /** @addtogroup IRDA_Private_Constants
  168.   * @{
  169.   */
  170. /**
  171.   * @}
  172.   */
  173. /* Private macro -------------------------------------------------------------*/
  174. /* Private variables ---------------------------------------------------------*/
  175. /* Private function prototypes -----------------------------------------------*/
  176. /** @addtogroup IRDA_Private_Functions
  177.   * @{
  178.   */
  179. static void IRDA_SetConfig (IRDA_HandleTypeDef *hirda);
  180. static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
  181. static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda);
  182. static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
  183. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  184. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma);
  185. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  186. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma);
  187. static void IRDA_DMAError(DMA_HandleTypeDef *hdma);
  188. static void IRDA_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  189. static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  190. static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  191. static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  192. static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  193. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Tickstart,uint32_t Timeout);
  194. static void IRDA_EndTxTransfer(IRDA_HandleTypeDef *hirda);
  195. static void IRDA_EndRxTransfer(IRDA_HandleTypeDef *hirda);
  196. /**
  197.   * @}
  198.   */
  199. /* Exported functions ---------------------------------------------------------*/
  200. /** @defgroup IRDA_Exported_Functions IrDA Exported Functions
  201.   * @{
  202.   */
  203.  
  204. /** @defgroup IRDA_Exported_Functions_Group1 IrDA Initialization and de-initialization functions
  205.   *  @brief    Initialization and Configuration functions
  206.   *
  207. @verbatim
  208.   ==============================================================================
  209.             ##### Initialization and Configuration functions #####
  210.   ==============================================================================
  211.     [..]
  212.     This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  213.     in IrDA mode.
  214.       (+) For the asynchronous mode only these parameters can be configured:
  215.         (++) BaudRate
  216.         (++) WordLength
  217.         (++) Parity: If the parity is enabled, then the MSB bit of the data written
  218.              in the data register is transmitted but is changed by the parity bit.
  219.              Depending on the frame length defined by the M bit (8-bits or 9-bits),
  220.              please refer to Reference manual for possible IRDA frame formats.
  221.         (++) Prescaler: A pulse of width less than two and greater than one PSC period(s) may or may
  222.              not be rejected. The receiver set up time should be managed by software. The IrDA physical layer
  223.              specification specifies a minimum of 10 ms delay between transmission and
  224.              reception (IrDA is a half duplex protocol).
  225.         (++) Mode: Receiver/transmitter modes
  226.         (++) IrDAMode: the IrDA can operate in the Normal mode or in the Low power mode.
  227.     [..]
  228.     The HAL_IRDA_Init() API follows IRDA configuration procedures (details for the procedures
  229.     are available in reference manual).
  230.  
  231. @endverbatim
  232.   * @{
  233.   */
  234.  
  235. /**
  236.   * @brief  Initializes the IRDA mode according to the specified
  237.   *         parameters in the IRDA_InitTypeDef and create the associated handle.
  238.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  239.   *                the configuration information for the specified IRDA module.
  240.   * @retval HAL status
  241.   */
  242. HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
  243. {
  244.   /* Check the IRDA handle allocation */
  245.   if(hirda == NULL)
  246.   {
  247.     return HAL_ERROR;
  248.   }
  249.  
  250.   /* Check the parameters */
  251.   assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  252.  
  253.   if(hirda->gState == HAL_IRDA_STATE_RESET)
  254.   {
  255.     /* Allocate lock resource and initialize it */
  256.     hirda->Lock = HAL_UNLOCKED;
  257.     /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  258.     HAL_IRDA_MspInit(hirda);
  259.   }
  260.  
  261.   hirda->gState = HAL_IRDA_STATE_BUSY;
  262.  
  263.   /* Disable the IRDA peripheral */
  264.   __HAL_IRDA_DISABLE(hirda);
  265.  
  266.   /* Set the IRDA communication parameters */
  267.   IRDA_SetConfig(hirda);
  268.  
  269.   /* In IrDA mode, the following bits must be kept cleared:
  270.   - LINEN, STOP and CLKEN bits in the USART_CR2 register,
  271.   - SCEN and HDSEL bits in the USART_CR3 register.*/
  272.   CLEAR_BIT(hirda->Instance->CR2, (USART_CR2_LINEN | USART_CR2_STOP | USART_CR2_CLKEN));
  273.   CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
  274.  
  275.   /* Enable the IRDA peripheral */
  276.   __HAL_IRDA_ENABLE(hirda);
  277.  
  278.   /* Set the prescaler */
  279.   MODIFY_REG(hirda->Instance->GTPR, USART_GTPR_PSC, hirda->Init.Prescaler);
  280.  
  281.   /* Configure the IrDA mode */
  282.   MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.IrDAMode);
  283.  
  284.   /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
  285.   SET_BIT(hirda->Instance->CR3, USART_CR3_IREN);
  286.  
  287.   /* Initialize the IRDA state*/
  288.   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  289.   hirda->gState= HAL_IRDA_STATE_READY;
  290.   hirda->RxState= HAL_IRDA_STATE_READY;
  291.  
  292.   return HAL_OK;
  293. }
  294.  
  295. /**
  296.   * @brief  DeInitializes the IRDA peripheral
  297.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  298.   *                the configuration information for the specified IRDA module.
  299.   * @retval HAL status
  300.   */
  301. HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
  302. {
  303.   /* Check the IRDA handle allocation */
  304.   if(hirda == NULL)
  305.   {
  306.     return HAL_ERROR;
  307.   }
  308.  
  309.   /* Check the parameters */
  310.   assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  311.  
  312.   hirda->gState = HAL_IRDA_STATE_BUSY;
  313.  
  314.   /* Disable the Peripheral */
  315.   __HAL_IRDA_DISABLE(hirda);
  316.  
  317.   /* DeInit the low level hardware */
  318.   HAL_IRDA_MspDeInit(hirda);
  319.  
  320.   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  321.   hirda->gState = HAL_IRDA_STATE_RESET;
  322.   hirda->RxState = HAL_IRDA_STATE_RESET;
  323.  
  324.   /* Release Lock */
  325.   __HAL_UNLOCK(hirda);
  326.  
  327.   return HAL_OK;
  328. }
  329.  
  330. /**
  331.   * @brief  IRDA MSP Init.
  332.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  333.   *                the configuration information for the specified IRDA module.
  334.   * @retval None
  335.   */
  336. __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
  337. {
  338.   /* Prevent unused argument(s) compilation warning */
  339.   UNUSED(hirda);
  340.   /* NOTE: This function should not be modified, when the callback is needed,
  341.            the HAL_IRDA_MspInit can be implemented in the user file
  342.   */
  343. }
  344.  
  345. /**
  346.   * @brief  IRDA MSP DeInit.
  347.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  348.   *                the configuration information for the specified IRDA module.
  349.   * @retval None
  350.   */
  351. __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
  352. {
  353.   /* Prevent unused argument(s) compilation warning */
  354.   UNUSED(hirda);
  355.   /* NOTE: This function should not be modified, when the callback is needed,
  356.            the HAL_IRDA_MspDeInit can be implemented in the user file
  357.   */
  358. }
  359.  
  360. /**
  361.   * @}
  362.   */
  363.  
  364. /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
  365.   *  @brief   IRDA Transmit and Receive functions
  366.   *
  367. @verbatim
  368.   ==============================================================================
  369.                          ##### IO operation functions #####
  370.   ==============================================================================
  371.   [..]
  372.     This subsection provides a set of functions allowing to manage the IRDA data transfers.
  373.     IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
  374.     on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
  375.     is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
  376.     While receiving data, transmission should be avoided as the data to be transmitted
  377.     could be corrupted.
  378.  
  379.     (#) There are two modes of transfer:
  380.         (++) Blocking mode: The communication is performed in polling mode.
  381.              The HAL status of all data processing is returned by the same function
  382.              after finishing transfer.  
  383.         (++) No-Blocking mode: The communication is performed using Interrupts
  384.              or DMA, these APIs return the HAL status.
  385.              The end of the data processing will be indicated through the
  386.              dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
  387.              using DMA mode.
  388.              The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
  389.              will be executed respectively at the end of the transmit or Receive process
  390.              The HAL_IRDA_ErrorCallback() user callback will be executed when a communication
  391.              error is detected
  392.  
  393.     (#) Blocking mode APIs are:
  394.         (++) HAL_IRDA_Transmit()
  395.         (++) HAL_IRDA_Receive()
  396.        
  397.     (#) Non Blocking mode APIs with Interrupt are:
  398.         (++) HAL_IRDA_Transmit_IT()
  399.         (++) HAL_IRDA_Receive_IT()
  400.         (++) HAL_IRDA_IRQHandler()
  401.  
  402.     (#) Non Blocking mode functions with DMA are:
  403.         (++) HAL_IRDA_Transmit_DMA()
  404.         (++) HAL_IRDA_Receive_DMA()
  405.         (++) HAL_IRDA_DMAPause()
  406.         (++) HAL_IRDA_DMAResume()
  407.         (++) HAL_IRDA_DMAStop()
  408.  
  409.     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  410.         (++) HAL_IRDA_TxHalfCpltCallback()
  411.         (++) HAL_IRDA_TxCpltCallback()
  412.         (++) HAL_IRDA_RxHalfCpltCallback()
  413.         (++) HAL_IRDA_RxCpltCallback()
  414.         (++) HAL_IRDA_ErrorCallback()
  415.  
  416. @endverbatim
  417.   * @{
  418.   */
  419.  
  420. /**
  421.   * @brief  Sends an amount of data in blocking mode.
  422.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  423.   *                the configuration information for the specified IRDA module.
  424.   * @param  pData: Pointer to data buffer
  425.   * @param  Size: Amount of data to be sent
  426.   * @param  Timeout: Specify timeout value  
  427.   * @retval HAL status
  428.   */
  429. HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  430. {
  431.   uint16_t* tmp;
  432.   uint32_t tickstart = 0U;
  433.  
  434.   /* Check that a Tx process is not already ongoing */
  435.   if(hirda->gState == HAL_IRDA_STATE_READY)
  436.   {
  437.     if((pData == NULL) || (Size == 0U))
  438.     {
  439.       return  HAL_ERROR;
  440.     }
  441.    
  442.     /* Process Locked */
  443.     __HAL_LOCK(hirda);
  444.    
  445.     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  446.     hirda->gState = HAL_IRDA_STATE_BUSY_TX;
  447.  
  448.     /* Init tickstart for timeout managment */
  449.     tickstart = HAL_GetTick();
  450.  
  451.     hirda->TxXferSize = Size;
  452.     hirda->TxXferCount = Size;
  453.     while(hirda->TxXferCount > 0U)
  454.     {
  455.       hirda->TxXferCount--;
  456.       if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  457.       {
  458.         if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  459.         {
  460.           return HAL_TIMEOUT;
  461.         }
  462.         tmp = (uint16_t*) pData;
  463.         hirda->Instance->DR = (*tmp & (uint16_t)0x01FF);
  464.         if(hirda->Init.Parity == IRDA_PARITY_NONE)
  465.         {
  466.           pData +=2U;
  467.         }
  468.         else
  469.         {
  470.           pData +=1U;
  471.         }
  472.       }
  473.       else
  474.       {
  475.         if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  476.         {
  477.           return HAL_TIMEOUT;
  478.         }
  479.         hirda->Instance->DR = (*pData++ & (uint8_t)0xFF);
  480.       }
  481.     }
  482.    
  483.     if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  484.     {
  485.       return HAL_TIMEOUT;
  486.     }
  487.    
  488.     /* At end of Tx process, restore hirda->gState to Ready */
  489.     hirda->gState = HAL_IRDA_STATE_READY;
  490.    
  491.     /* Process Unlocked */
  492.     __HAL_UNLOCK(hirda);
  493.    
  494.     return HAL_OK;
  495.   }
  496.   else
  497.   {
  498.     return HAL_BUSY;
  499.   }
  500. }
  501.  
  502. /**
  503.   * @brief  Receive an amount of data in blocking mode.
  504.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  505.   *                the configuration information for the specified IRDA module.
  506.   * @param  pData: Pointer to data buffer
  507.   * @param  Size: Amount of data to be received
  508.   * @param  Timeout: Specify timeout value    
  509.   * @retval HAL status
  510.   */
  511. HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  512. {
  513.   uint16_t* tmp;
  514.   uint32_t tickstart = 0U;
  515.  
  516.   /* Check that a Rx process is not already ongoing */
  517.   if(hirda->RxState == HAL_IRDA_STATE_READY)
  518.   {
  519.     if((pData == NULL) || (Size == 0U))
  520.     {
  521.       return  HAL_ERROR;
  522.     }
  523.    
  524.     /* Process Locked */
  525.     __HAL_LOCK(hirda);
  526.    
  527.     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  528.     hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
  529.  
  530.     /* Init tickstart for timeout managment */
  531.     tickstart = HAL_GetTick();
  532.  
  533.     hirda->RxXferSize = Size;
  534.     hirda->RxXferCount = Size;
  535.  
  536.     /* Check the remain data to be received */
  537.     while(hirda->RxXferCount > 0U)
  538.     {
  539.       hirda->RxXferCount--;
  540.       if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  541.       {
  542.         if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  543.         {
  544.           return HAL_TIMEOUT;
  545.         }
  546.         tmp = (uint16_t*)pData;
  547.         if(hirda->Init.Parity == IRDA_PARITY_NONE)
  548.         {
  549.           *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x01FF);
  550.           pData +=2U;
  551.         }
  552.         else
  553.         {
  554.           *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x00FF);
  555.           pData +=1U;
  556.         }
  557.       }
  558.       else
  559.       {
  560.         if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  561.         {
  562.           return HAL_TIMEOUT;
  563.         }
  564.         if(hirda->Init.Parity == IRDA_PARITY_NONE)
  565.         {
  566.           *pData++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x00FF);
  567.         }
  568.         else
  569.         {
  570.           *pData++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x007F);
  571.         }
  572.       }
  573.     }
  574.    
  575.     /* At end of Rx process, restore hirda->RxState to Ready */
  576.     hirda->RxState = HAL_IRDA_STATE_READY;
  577.    
  578.     /* Process Unlocked */
  579.     __HAL_UNLOCK(hirda);
  580.    
  581.     return HAL_OK;
  582.   }
  583.   else
  584.   {
  585.     return HAL_BUSY;
  586.   }
  587. }
  588.  
  589. /**
  590.   * @brief  Sends an amount of data in non blocking mode.
  591.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  592.   *                the configuration information for the specified IRDA module.
  593.   * @param  pData: Pointer to data buffer
  594.   * @param  Size: Amount of data to be sent
  595.   * @retval HAL status
  596.   */
  597. HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  598. {
  599.   /* Check that a Tx process is not already ongoing */
  600.   if(hirda->gState == HAL_IRDA_STATE_READY)
  601.   {
  602.     if((pData == NULL) || (Size == 0U))
  603.     {
  604.       return HAL_ERROR;
  605.     }
  606.     /* Process Locked */
  607.     __HAL_LOCK(hirda);
  608.    
  609.     hirda->pTxBuffPtr = pData;
  610.     hirda->TxXferSize = Size;
  611.     hirda->TxXferCount = Size;
  612.  
  613.     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  614.     hirda->gState = HAL_IRDA_STATE_BUSY_TX;
  615.  
  616.     /* Process Unlocked */
  617.     __HAL_UNLOCK(hirda);
  618.  
  619.     /* Enable the IRDA Transmit data register empty Interrupt */
  620.     __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TXE);
  621.    
  622.     return HAL_OK;
  623.   }
  624.   else
  625.   {
  626.     return HAL_BUSY;
  627.   }
  628. }
  629.  
  630. /**
  631.   * @brief  Receives an amount of data in non blocking mode.
  632.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  633.   *                the configuration information for the specified IRDA module.
  634.   * @param  pData: Pointer to data buffer
  635.   * @param  Size: Amount of data to be received
  636.   * @retval HAL status
  637.   */
  638. HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  639. {
  640.   /* Check that a Rx process is not already ongoing */
  641.   if(hirda->RxState == HAL_IRDA_STATE_READY)
  642.   {
  643.     if((pData == NULL) || (Size == 0U))
  644.     {
  645.       return HAL_ERROR;
  646.     }
  647.    
  648.     /* Process Locked */
  649.     __HAL_LOCK(hirda);
  650.    
  651.     hirda->pRxBuffPtr = pData;
  652.     hirda->RxXferSize = Size;
  653.     hirda->RxXferCount = Size;
  654.  
  655.     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  656.     hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
  657.    
  658.     /* Process Unlocked */
  659.     __HAL_UNLOCK(hirda);
  660.  
  661.     /* Enable the IRDA Parity Error Interrupt */
  662.     __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_PE);
  663.  
  664.     /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  665.     __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
  666.  
  667.     /* Enable the IRDA Data Register not empty Interrupt */
  668.     __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_RXNE);
  669.  
  670.     return HAL_OK;
  671.   }
  672.   else
  673.   {
  674.     return HAL_BUSY;
  675.   }
  676. }
  677.  
  678. /**
  679.   * @brief  Sends an amount of data in non blocking mode.
  680.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  681.   *                the configuration information for the specified IRDA module.
  682.   * @param  pData: Pointer to data buffer
  683.   * @param  Size: Amount of data to be sent
  684.   * @retval HAL status
  685.   */
  686. HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  687. {
  688.   uint32_t *tmp;
  689.  
  690.   /* Check that a Tx process is not already ongoing */
  691.   if(hirda->gState == HAL_IRDA_STATE_READY)
  692.   {
  693.     if((pData == NULL) || (Size == 0U))
  694.     {
  695.       return HAL_ERROR;
  696.     }
  697.  
  698.     /* Process Locked */
  699.     __HAL_LOCK(hirda);
  700.  
  701.     hirda->pTxBuffPtr = pData;
  702.     hirda->TxXferSize = Size;
  703.     hirda->TxXferCount = Size;
  704.  
  705.     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  706.     hirda->gState = HAL_IRDA_STATE_BUSY_TX;
  707.  
  708.     /* Set the IRDA DMA transfer complete callback */
  709.     hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
  710.  
  711.     /* Set the IRDA DMA half transfer complete callback */
  712.     hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;
  713.  
  714.     /* Set the DMA error callback */
  715.     hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
  716.  
  717.     /* Set the DMA abort callback */
  718.     hirda->hdmatx->XferAbortCallback = NULL;
  719.  
  720.     /* Enable the IRDA transmit DMA Channel */
  721.     tmp = (uint32_t*)&pData;
  722.     HAL_DMA_Start_IT(hirda->hdmatx, *(uint32_t*)tmp, (uint32_t)&hirda->Instance->DR, Size);
  723.  
  724.     /* Clear the TC flag in the SR register by writing 0 to it */
  725.     __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_FLAG_TC);
  726.    
  727.     /* Process Unlocked */
  728.     __HAL_UNLOCK(hirda);
  729.  
  730.     /* Enable the DMA transfer for transmit request by setting the DMAT bit
  731.        in the USART CR3 register */
  732.     SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  733.  
  734.     return HAL_OK;
  735.   }
  736.   else
  737.   {
  738.     return HAL_BUSY;
  739.   }
  740. }
  741.  
  742. /**
  743.   * @brief  Receives an amount of data in non blocking mode.
  744.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  745.   *                the configuration information for the specified IRDA module.
  746.   * @param  pData: Pointer to data buffer
  747.   * @param  Size: Amount of data to be received
  748.   * @note   When the IRDA parity is enabled (PCE = 1) the data received contain the parity bit.
  749.   * @retval HAL status
  750.   */
  751. HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  752. {
  753.   uint32_t *tmp;
  754.  
  755.   /* Check that a Rx process is not already ongoing */
  756.   if(hirda->RxState == HAL_IRDA_STATE_READY)
  757.   {
  758.     if((pData == NULL) || (Size == 0U))
  759.     {
  760.       return HAL_ERROR;
  761.     }
  762.  
  763.     /* Process Locked */
  764.     __HAL_LOCK(hirda);
  765.  
  766.     hirda->pRxBuffPtr = pData;
  767.     hirda->RxXferSize = Size;
  768.  
  769.     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  770.     hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
  771.  
  772.     /* Set the IRDA DMA transfer complete callback */
  773.     hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
  774.  
  775.     /* Set the IRDA DMA half transfer complete callback */
  776.     hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;
  777.  
  778.     /* Set the DMA error callback */
  779.     hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
  780.  
  781.     /* Set the DMA abort callback */
  782.     hirda->hdmarx->XferAbortCallback = NULL;
  783.  
  784.     /* Enable the DMA channel */
  785.     tmp = (uint32_t*)&pData;
  786.     HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->DR, *(uint32_t*)tmp, Size);
  787.  
  788.     /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
  789.     __HAL_IRDA_CLEAR_OREFLAG(hirda);
  790.  
  791.     /* Process Unlocked */
  792.     __HAL_UNLOCK(hirda);
  793.  
  794.     /* Enable the IRDA Parity Error Interrupt */
  795.     SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  796.  
  797.     /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  798.     SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  799.  
  800.     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  801.     in the USART CR3 register */
  802.     SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  803.  
  804.     return HAL_OK;
  805.   }
  806.   else
  807.   {
  808.     return HAL_BUSY;
  809.   }
  810. }
  811.  
  812. /**
  813.   * @brief Pauses the DMA Transfer.
  814.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  815.   *                the configuration information for the specified IRDA module.
  816.   * @retval HAL status
  817.   */
  818. HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda)
  819. {
  820.   uint32_t dmarequest = 0x00U;
  821.  
  822.   /* Process Locked */
  823.   __HAL_LOCK(hirda);
  824.  
  825.   dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT);
  826.   if((hirda->gState == HAL_IRDA_STATE_BUSY_TX) && dmarequest)
  827.   {
  828.     /* Disable the IRDA DMA Tx request */
  829.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  830.   }
  831.  
  832.   dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR);
  833.   if((hirda->RxState == HAL_IRDA_STATE_BUSY_RX) && dmarequest)
  834.   {
  835.     /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  836.     CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  837.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  838.  
  839.     /* Disable the IRDA DMA Rx request */
  840.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  841.   }
  842.  
  843.   /* Process Unlocked */
  844.   __HAL_UNLOCK(hirda);
  845.  
  846.   return HAL_OK;
  847. }
  848.  
  849. /**
  850.   * @brief Resumes the DMA Transfer.
  851.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  852.   *                the configuration information for the specified IRDA module.
  853.   * @retval HAL status
  854.   */
  855. HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda)
  856. {
  857.   /* Process Locked */
  858.   __HAL_LOCK(hirda);
  859.  
  860.   if(hirda->gState == HAL_IRDA_STATE_BUSY_TX)
  861.   {
  862.     /* Enable the IRDA DMA Tx request */
  863.     SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  864.   }
  865.  
  866.   if(hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
  867.   {
  868.     /* Clear the Overrun flag before resuming the Rx transfer */
  869.     __HAL_IRDA_CLEAR_OREFLAG(hirda);
  870.    
  871.     /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
  872.     SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  873.     SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  874.    
  875.     /* Enable the IRDA DMA Rx request */
  876.     SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  877.   }
  878.  
  879.   /* Process Unlocked */
  880.   __HAL_UNLOCK(hirda);
  881.  
  882.   return HAL_OK;
  883. }
  884.  
  885. /**
  886.   * @brief Stops the DMA Transfer.
  887.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  888.   *                the configuration information for the specified IRDA module.
  889.   * @retval HAL status
  890.   */
  891. HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda)
  892. {
  893.   uint32_t dmarequest = 0x00U;
  894.   /* The Lock is not implemented on this API to allow the user application
  895.   to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback():
  896.   when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  897.   and the correspond call back is executed HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback()
  898.   */
  899.  
  900.   /* Stop IRDA DMA Tx request if ongoing */
  901.   dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT);
  902.   if((hirda->gState == HAL_IRDA_STATE_BUSY_TX) && dmarequest)
  903.   {
  904.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  905.  
  906.     /* Abort the IRDA DMA Tx channel */
  907.     if(hirda->hdmatx != NULL)
  908.     {
  909.       HAL_DMA_Abort(hirda->hdmatx);
  910.     }
  911.     IRDA_EndTxTransfer(hirda);
  912.   }
  913.  
  914.   /* Stop IRDA DMA Rx request if ongoing */
  915.   dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR);
  916.   if((hirda->RxState == HAL_IRDA_STATE_BUSY_RX) && dmarequest)
  917.   {
  918.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  919.    
  920.     /* Abort the IRDA DMA Rx channel */
  921.     if(hirda->hdmarx != NULL)
  922.     {
  923.       HAL_DMA_Abort(hirda->hdmarx);
  924.     }
  925.     IRDA_EndRxTransfer(hirda);
  926.   }
  927.  
  928.   return HAL_OK;
  929. }
  930.  
  931. /**
  932.   * @brief  Abort ongoing transfers (blocking mode).
  933.   * @param  hirda IRDA handle.
  934.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  935.   *         This procedure performs following operations :
  936.   *           - Disable PPP Interrupts
  937.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  938.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  939.   *           - Set handle State to READY
  940.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  941.   * @retval HAL status
  942. */
  943. HAL_StatusTypeDef HAL_IRDA_Abort(IRDA_HandleTypeDef *hirda)
  944. {
  945.   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  946.   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  947.   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  948.  
  949.   /* Disable the IRDA DMA Tx request if enabled */
  950.   if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  951.   {
  952.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  953.  
  954.     /* Abort the IRDA DMA Tx channel: use blocking DMA Abort API (no callback) */
  955.     if(hirda->hdmatx != NULL)
  956.     {
  957.       /* Set the IRDA DMA Abort callback to Null.
  958.          No call back execution at end of DMA abort procedure */
  959.       hirda->hdmatx->XferAbortCallback = NULL;
  960.  
  961.       HAL_DMA_Abort(hirda->hdmatx);
  962.     }
  963.   }
  964.  
  965.   /* Disable the IRDA DMA Rx request if enabled */
  966.   if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  967.   {
  968.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  969.  
  970.     /* Abort the IRDA DMA Rx channel: use blocking DMA Abort API (no callback) */
  971.     if(hirda->hdmarx != NULL)
  972.     {
  973.       /* Set the IRDA DMA Abort callback to Null.
  974.          No call back execution at end of DMA abort procedure */
  975.       hirda->hdmarx->XferAbortCallback = NULL;
  976.  
  977.       HAL_DMA_Abort(hirda->hdmarx);
  978.     }
  979.   }
  980.  
  981.   /* Reset Tx and Rx transfer counters */
  982.   hirda->TxXferCount = 0x00U;
  983.   hirda->RxXferCount = 0x00U;
  984.  
  985.   /* Reset ErrorCode */
  986.   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  987.  
  988.   /* Restore hirda->RxState and hirda->gState to Ready */
  989.   hirda->RxState = HAL_IRDA_STATE_READY;
  990.   hirda->gState = HAL_IRDA_STATE_READY;
  991.  
  992.   return HAL_OK;
  993. }
  994.  
  995. /**
  996.   * @brief  Abort ongoing Transmit transfer (blocking mode).
  997.   * @param  hirda IRDA handle.
  998.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  999.   *         This procedure performs following operations :
  1000.   *           - Disable PPP Interrupts
  1001.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1002.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1003.   *           - Set handle State to READY
  1004.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1005.   * @retval HAL status
  1006. */
  1007. HAL_StatusTypeDef HAL_IRDA_AbortTransmit(IRDA_HandleTypeDef *hirda)
  1008. {
  1009.   /* Disable TXEIE and TCIE interrupts */
  1010.   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1011.  
  1012.   /* Disable the IRDA DMA Tx request if enabled */
  1013.   if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1014.   {
  1015.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1016.  
  1017.     /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
  1018.     if(hirda->hdmatx != NULL)
  1019.     {
  1020.       /* Set the IRDA DMA Abort callback to Null.
  1021.          No call back execution at end of DMA abort procedure */
  1022.       hirda->hdmatx->XferAbortCallback = NULL;
  1023.  
  1024.       HAL_DMA_Abort(hirda->hdmatx);
  1025.     }
  1026.   }
  1027.  
  1028.   /* Reset Tx transfer counter */
  1029.   hirda->TxXferCount = 0x00U;
  1030.  
  1031.   /* Restore hirda->gState to Ready */
  1032.   hirda->gState = HAL_IRDA_STATE_READY;
  1033.  
  1034.   return HAL_OK;
  1035. }
  1036.  
  1037. /**
  1038.   * @brief  Abort ongoing Receive transfer (blocking mode).
  1039.   * @param  hirda IRDA handle.
  1040.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1041.   *         This procedure performs following operations :
  1042.   *           - Disable PPP Interrupts
  1043.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1044.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1045.   *           - Set handle State to READY
  1046.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1047.   * @retval HAL status
  1048. */
  1049. HAL_StatusTypeDef HAL_IRDA_AbortReceive(IRDA_HandleTypeDef *hirda)
  1050. {
  1051.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1052.   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1053.   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1054.  
  1055.   /* Disable the IRDA DMA Rx request if enabled */
  1056.   if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1057.   {
  1058.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1059.  
  1060.     /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
  1061.     if(hirda->hdmarx != NULL)
  1062.     {
  1063.       /* Set the IRDA DMA Abort callback to Null.
  1064.          No call back execution at end of DMA abort procedure */
  1065.       hirda->hdmarx->XferAbortCallback = NULL;
  1066.  
  1067.       HAL_DMA_Abort(hirda->hdmarx);
  1068.     }
  1069.   }
  1070.  
  1071.   /* Reset Rx transfer counter */
  1072.   hirda->RxXferCount = 0x00U;
  1073.  
  1074.   /* Restore hirda->RxState to Ready */
  1075.   hirda->RxState = HAL_IRDA_STATE_READY;
  1076.  
  1077.   return HAL_OK;
  1078. }
  1079.  
  1080. /**
  1081.   * @brief  Abort ongoing transfers (Interrupt mode).
  1082.   * @param  hirda IRDA handle.
  1083.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1084.   *         This procedure performs following operations :
  1085.   *           - Disable PPP Interrupts
  1086.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1087.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1088.   *           - Set handle State to READY
  1089.   *           - At abort completion, call user abort complete callback
  1090.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1091.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  1092.   * @retval HAL status
  1093. */
  1094. HAL_StatusTypeDef HAL_IRDA_Abort_IT(IRDA_HandleTypeDef *hirda)
  1095. {
  1096.   uint32_t AbortCplt = 0x01U;
  1097.  
  1098.   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1099.   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1100.   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1101.  
  1102.   /* If DMA Tx and/or DMA Rx Handles are associated to IRDA Handle, DMA Abort complete callbacks should be initialised
  1103.      before any call to DMA Abort functions */
  1104.   /* DMA Tx Handle is valid */
  1105.   if(hirda->hdmatx != NULL)
  1106.   {
  1107.     /* Set DMA Abort Complete callback if IRDA DMA Tx request if enabled.
  1108.        Otherwise, set it to NULL */
  1109.     if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1110.     {
  1111.       hirda->hdmatx->XferAbortCallback = IRDA_DMATxAbortCallback;
  1112.     }
  1113.     else
  1114.     {
  1115.       hirda->hdmatx->XferAbortCallback = NULL;
  1116.     }
  1117.   }
  1118.   /* DMA Rx Handle is valid */
  1119.   if(hirda->hdmarx != NULL)
  1120.   {
  1121.     /* Set DMA Abort Complete callback if IRDA DMA Rx request if enabled.
  1122.        Otherwise, set it to NULL */
  1123.     if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1124.     {
  1125.       hirda->hdmarx->XferAbortCallback = IRDA_DMARxAbortCallback;
  1126.     }
  1127.     else
  1128.     {
  1129.       hirda->hdmarx->XferAbortCallback = NULL;
  1130.     }
  1131.   }
  1132.  
  1133.   /* Disable the IRDA DMA Tx request if enabled */
  1134.   if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1135.   {
  1136.     /* Disable DMA Tx at IRDA level */
  1137.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1138.  
  1139.     /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
  1140.     if(hirda->hdmatx != NULL)
  1141.     {
  1142.       /* IRDA Tx DMA Abort callback has already been initialised :
  1143.          will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  1144.  
  1145.       /* Abort DMA TX */
  1146.       if(HAL_DMA_Abort_IT(hirda->hdmatx) != HAL_OK)
  1147.       {
  1148.         hirda->hdmatx->XferAbortCallback = NULL;
  1149.       }
  1150.       else
  1151.       {
  1152.         AbortCplt = 0x00U;
  1153.       }
  1154.     }
  1155.   }
  1156.  
  1157.   /* Disable the IRDA DMA Rx request if enabled */
  1158.   if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1159.   {
  1160.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1161.  
  1162.     /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
  1163.     if(hirda->hdmarx != NULL)
  1164.     {
  1165.       /* IRDA Rx DMA Abort callback has already been initialised :
  1166.          will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  1167.  
  1168.       /* Abort DMA RX */
  1169.       if(HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
  1170.       {
  1171.         hirda->hdmarx->XferAbortCallback = NULL;
  1172.         AbortCplt = 0x01U;
  1173.       }
  1174.       else
  1175.       {
  1176.         AbortCplt = 0x00U;
  1177.       }
  1178.     }
  1179.   }
  1180.  
  1181.   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1182.   if(AbortCplt == 0x01U)
  1183.   {
  1184.     /* Reset Tx and Rx transfer counters */
  1185.     hirda->TxXferCount = 0x00U;
  1186.     hirda->RxXferCount = 0x00U;
  1187.  
  1188.     /* Reset ErrorCode */
  1189.     hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1190.  
  1191.     /* Restore hirda->gState and hirda->RxState to Ready */
  1192.     hirda->gState  = HAL_IRDA_STATE_READY;
  1193.     hirda->RxState = HAL_IRDA_STATE_READY;
  1194.  
  1195.     /* As no DMA to be aborted, call directly user Abort complete callback */
  1196.     HAL_IRDA_AbortCpltCallback(hirda);
  1197.   }
  1198.  
  1199.   return HAL_OK;
  1200. }
  1201.  
  1202. /**
  1203.   * @brief  Abort ongoing Transmit transfer (Interrupt mode).
  1204.   * @param  hirda IRDA handle.
  1205.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1206.   *         This procedure performs following operations :
  1207.   *           - Disable PPP Interrupts
  1208.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1209.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1210.   *           - Set handle State to READY
  1211.   *           - At abort completion, call user abort complete callback
  1212.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1213.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  1214.   * @retval HAL status
  1215. */
  1216. HAL_StatusTypeDef HAL_IRDA_AbortTransmit_IT(IRDA_HandleTypeDef *hirda)
  1217. {
  1218.   /* Disable TXEIE and TCIE interrupts */
  1219.   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1220.  
  1221.   /* Disable the IRDA DMA Tx request if enabled */
  1222.   if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1223.   {
  1224.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1225.  
  1226.     /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
  1227.     if(hirda->hdmatx != NULL)
  1228.     {
  1229.       /* Set the IRDA DMA Abort callback :
  1230.          will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  1231.       hirda->hdmatx->XferAbortCallback = IRDA_DMATxOnlyAbortCallback;
  1232.  
  1233.       /* Abort DMA TX */
  1234.       if(HAL_DMA_Abort_IT(hirda->hdmatx) != HAL_OK)
  1235.       {
  1236.         /* Call Directly hirda->hdmatx->XferAbortCallback function in case of error */
  1237.         hirda->hdmatx->XferAbortCallback(hirda->hdmatx);
  1238.       }
  1239.     }
  1240.     else
  1241.     {
  1242.       /* Reset Tx transfer counter */
  1243.       hirda->TxXferCount = 0x00U;
  1244.  
  1245.       /* Restore hirda->gState to Ready */
  1246.       hirda->gState = HAL_IRDA_STATE_READY;
  1247.  
  1248.       /* As no DMA to be aborted, call directly user Abort complete callback */
  1249.       HAL_IRDA_AbortTransmitCpltCallback(hirda);
  1250.     }
  1251.   }
  1252.   else
  1253.   {
  1254.     /* Reset Tx transfer counter */
  1255.     hirda->TxXferCount = 0x00U;
  1256.  
  1257.     /* Restore hirda->gState to Ready */
  1258.     hirda->gState = HAL_IRDA_STATE_READY;
  1259.  
  1260.     /* As no DMA to be aborted, call directly user Abort complete callback */
  1261.     HAL_IRDA_AbortTransmitCpltCallback(hirda);
  1262.   }
  1263.  
  1264.   return HAL_OK;
  1265. }
  1266.  
  1267. /**
  1268.   * @brief  Abort ongoing Receive transfer (Interrupt mode).
  1269.   * @param  hirda IRDA handle.
  1270.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1271.   *         This procedure performs following operations :
  1272.   *           - Disable PPP Interrupts
  1273.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1274.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1275.   *           - Set handle State to READY
  1276.   *           - At abort completion, call user abort complete callback
  1277.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1278.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  1279.   * @retval HAL status
  1280. */
  1281. HAL_StatusTypeDef HAL_IRDA_AbortReceive_IT(IRDA_HandleTypeDef *hirda)
  1282. {
  1283.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1284.   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1285.   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1286.  
  1287.   /* Disable the IRDA DMA Rx request if enabled */
  1288.   if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1289.   {
  1290.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1291.  
  1292.     /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
  1293.     if(hirda->hdmarx != NULL)
  1294.     {
  1295.       /* Set the IRDA DMA Abort callback :
  1296.          will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  1297.       hirda->hdmarx->XferAbortCallback = IRDA_DMARxOnlyAbortCallback;
  1298.  
  1299.       /* Abort DMA RX */
  1300.       if(HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
  1301.       {
  1302.         /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
  1303.         hirda->hdmarx->XferAbortCallback(hirda->hdmarx);
  1304.       }
  1305.     }
  1306.     else
  1307.     {
  1308.       /* Reset Rx transfer counter */
  1309.       hirda->RxXferCount = 0x00U;
  1310.  
  1311.       /* Restore hirda->RxState to Ready */
  1312.       hirda->RxState = HAL_IRDA_STATE_READY;
  1313.  
  1314.       /* As no DMA to be aborted, call directly user Abort complete callback */
  1315.       HAL_IRDA_AbortReceiveCpltCallback(hirda);
  1316.     }
  1317.   }
  1318.   else
  1319.   {
  1320.     /* Reset Rx transfer counter */
  1321.     hirda->RxXferCount = 0x00U;
  1322.  
  1323.     /* Restore hirda->RxState to Ready */
  1324.     hirda->RxState = HAL_IRDA_STATE_READY;
  1325.  
  1326.     /* As no DMA to be aborted, call directly user Abort complete callback */
  1327.     HAL_IRDA_AbortReceiveCpltCallback(hirda);
  1328.   }
  1329.  
  1330.   return HAL_OK;
  1331. }
  1332.  
  1333. /**
  1334.   * @brief  This function handles IRDA interrupt request.
  1335.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  1336.   *                the configuration information for the specified IRDA module.
  1337.   * @retval None
  1338.   */
  1339. void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
  1340. {
  1341.    uint32_t isrflags   = READ_REG(hirda->Instance->SR);
  1342.    uint32_t cr1its     = READ_REG(hirda->Instance->CR1);
  1343.    uint32_t cr3its     = READ_REG(hirda->Instance->CR3);
  1344.    uint32_t errorflags = 0x00U;
  1345.    uint32_t dmarequest = 0x00U;
  1346.  
  1347.   /* If no error occurs */
  1348.   errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
  1349.   if(errorflags == RESET)
  1350.   {
  1351.     /* IRDA in mode Receiver -----------------------------------------------*/
  1352.     if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1353.     {
  1354.       IRDA_Receive_IT(hirda);
  1355.       return;
  1356.     }
  1357.   }
  1358.  
  1359.   /* If some errors occur */
  1360.   if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
  1361.   {
  1362.     /* IRDA parity error interrupt occurred -------------------------------*/
  1363.     if(((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1364.     {
  1365.       hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
  1366.     }
  1367.  
  1368.     /* IRDA noise error interrupt occurred --------------------------------*/
  1369.     if(((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1370.     {
  1371.       hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
  1372.     }
  1373.  
  1374.     /* IRDA frame error interrupt occurred --------------------------------*/
  1375.     if(((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1376.     {
  1377.       hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
  1378.     }
  1379.  
  1380.     /* IRDA Over-Run interrupt occurred -----------------------------------*/
  1381.     if(((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1382.     {
  1383.       hirda->ErrorCode |= HAL_IRDA_ERROR_ORE;
  1384.     }
  1385.     /* Call IRDA Error Call back function if need be -----------------------*/
  1386.     if(hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
  1387.     {
  1388.       /* IRDA in mode Receiver ---------------------------------------------*/
  1389.       if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1390.       {
  1391.         IRDA_Receive_IT(hirda);
  1392.       }
  1393.  
  1394.       /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1395.          consider error as blocking */
  1396.       dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR);
  1397.       if(((hirda->ErrorCode & HAL_IRDA_ERROR_ORE) != RESET) || dmarequest)
  1398.       {
  1399.         /* Blocking error : transfer is aborted
  1400.            Set the IRDA state ready to be able to start again the process,
  1401.            Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1402.         IRDA_EndRxTransfer(hirda);
  1403.  
  1404.         /* Disable the IRDA DMA Rx request if enabled */
  1405.         if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1406.         {
  1407.           CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1408.  
  1409.           /* Abort the IRDA DMA Rx channel */
  1410.           if(hirda->hdmarx != NULL)
  1411.           {
  1412.             /* Set the IRDA DMA Abort callback :
  1413.             will lead to call HAL_IRDA_ErrorCallback() at end of DMA abort procedure */
  1414.             hirda->hdmarx->XferAbortCallback = IRDA_DMAAbortOnError;
  1415.  
  1416.             if(HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
  1417.             {
  1418.               /* Call Directly XferAbortCallback function in case of error */
  1419.               hirda->hdmarx->XferAbortCallback(hirda->hdmarx);
  1420.             }
  1421.           }
  1422.           else
  1423.           {
  1424.             /* Call user error callback */
  1425.             HAL_IRDA_ErrorCallback(hirda);
  1426.           }
  1427.         }
  1428.         else
  1429.         {
  1430.           /* Call user error callback */
  1431.           HAL_IRDA_ErrorCallback(hirda);
  1432.         }
  1433.       }
  1434.       else
  1435.       {
  1436.         /* Non Blocking error : transfer could go on.
  1437.            Error is notified to user through user error callback */
  1438.         HAL_IRDA_ErrorCallback(hirda);
  1439.         hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1440.       }
  1441.     }
  1442.     return;
  1443.   } /* End if some error occurs */
  1444.  
  1445.   /* IRDA in mode Transmitter ------------------------------------------------*/
  1446.   if(((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1447.   {
  1448.     IRDA_Transmit_IT(hirda);
  1449.     return;
  1450.   }
  1451.  
  1452.   /* IRDA in mode Transmitter end --------------------------------------------*/
  1453.   if(((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1454.   {
  1455.     IRDA_EndTransmit_IT(hirda);
  1456.     return;
  1457.   }
  1458. }
  1459.  
  1460. /**
  1461.   * @brief  Tx Transfer complete callbacks.
  1462.   * @param  hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1463.   *                the configuration information for the specified IRDA module.
  1464.   * @retval None
  1465.   */
  1466. __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
  1467. {
  1468.   /* Prevent unused argument(s) compilation warning */
  1469.   UNUSED(hirda);
  1470.   /* NOTE: This function should not be modified, when the callback is needed,
  1471.            the HAL_IRDA_TxCpltCallback can be implemented in the user file
  1472.   */
  1473. }
  1474.  
  1475. /**
  1476.   * @brief  Tx Half Transfer completed callbacks.
  1477.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  1478.   *                the configuration information for the specified USART module.
  1479.   * @retval None
  1480.   */
  1481. __weak void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  1482. {
  1483.   /* Prevent unused argument(s) compilation warning */
  1484.   UNUSED(hirda);
  1485.   /* NOTE: This function should not be modified, when the callback is needed,
  1486.            the HAL_IRDA_TxHalfCpltCallback can be implemented in the user file
  1487.   */
  1488. }
  1489.  
  1490. /**
  1491.   * @brief  Rx Transfer complete callbacks.
  1492.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  1493.   *                the configuration information for the specified IRDA module.
  1494.   * @retval None
  1495.   */
  1496. __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
  1497. {
  1498.   /* Prevent unused argument(s) compilation warning */
  1499.   UNUSED(hirda);
  1500.   /* NOTE: This function should not be modified, when the callback is needed,
  1501.            the HAL_IRDA_RxCpltCallback can be implemented in the user file
  1502.   */
  1503. }
  1504.  
  1505. /**
  1506.   * @brief  Rx Half Transfer complete callbacks.
  1507.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  1508.   *                the configuration information for the specified IRDA module.
  1509.   * @retval None
  1510.   */
  1511. __weak void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  1512. {
  1513.   /* Prevent unused argument(s) compilation warning */
  1514.   UNUSED(hirda);
  1515.   /* NOTE : This function should not be modified, when the callback is needed,
  1516.             the HAL_IRDA_RxHalfCpltCallback can be implemented in the user file
  1517.   */
  1518. }
  1519.  
  1520. /**
  1521.   * @brief IRDA error callbacks.
  1522.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  1523.   *                the configuration information for the specified IRDA module.
  1524.   * @retval None
  1525.   */
  1526. __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
  1527. {
  1528.   /* Prevent unused argument(s) compilation warning */
  1529.   UNUSED(hirda);
  1530.   /* NOTE : This function Should not be modified, when the callback is needed,
  1531.   the HAL_IRDA_ErrorCallback could be implemented in the user file
  1532.   */
  1533. }
  1534.  
  1535. /**
  1536.   * @brief  IRDA Abort Complete callback.
  1537.   * @param  hirda IRDA handle.
  1538.   * @retval None
  1539.   */
  1540. __weak void HAL_IRDA_AbortCpltCallback(IRDA_HandleTypeDef *hirda)
  1541. {
  1542.   /* Prevent unused argument(s) compilation warning */
  1543.   UNUSED(hirda);
  1544.  
  1545.   /* NOTE : This function should not be modified, when the callback is needed,
  1546.             the HAL_IRDA_AbortCpltCallback can be implemented in the user file.
  1547.    */
  1548. }
  1549.  
  1550. /**
  1551.   * @brief  IRDA Abort Transmit Complete callback.
  1552.   * @param  hirda IRDA handle.
  1553.   * @retval None
  1554.   */
  1555. __weak void HAL_IRDA_AbortTransmitCpltCallback(IRDA_HandleTypeDef *hirda)
  1556. {
  1557.   /* Prevent unused argument(s) compilation warning */
  1558.   UNUSED(hirda);
  1559.  
  1560.   /* NOTE : This function should not be modified, when the callback is needed,
  1561.             the HAL_IRDA_AbortTransmitCpltCallback can be implemented in the user file.
  1562.    */
  1563. }
  1564.  
  1565. /**
  1566.   * @brief  IRDA Abort ReceiveComplete callback.
  1567.   * @param  hirda IRDA handle.
  1568.   * @retval None
  1569.   */
  1570. __weak void HAL_IRDA_AbortReceiveCpltCallback(IRDA_HandleTypeDef *hirda)
  1571. {
  1572.   /* Prevent unused argument(s) compilation warning */
  1573.   UNUSED(hirda);
  1574.  
  1575.   /* NOTE : This function should not be modified, when the callback is needed,
  1576.             the HAL_IRDA_AbortReceiveCpltCallback can be implemented in the user file.
  1577.    */
  1578. }
  1579.  
  1580. /**
  1581.   * @}
  1582.   */
  1583.  
  1584. /** @defgroup IRDA_Exported_Functions_Group3 Peripheral State and Errors functions
  1585.   *  @brief   IRDA State and Errors functions
  1586.   *
  1587. @verbatim  
  1588.   ==============================================================================
  1589.                   ##### Peripheral State and Errors functions #####
  1590.   ==============================================================================  
  1591.   [..]
  1592.     This subsection provides a set of functions allowing to return the State of IrDA
  1593.     communication process and also return Peripheral Errors occurred during communication process
  1594.      (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state of the IrDA peripheral.
  1595.      (+) HAL_IRDA_GetError() check in run-time errors that could be occurred during communication.
  1596.      
  1597. @endverbatim
  1598.   * @{
  1599.   */
  1600.  
  1601. /**
  1602.   * @brief  Returns the IRDA state.
  1603.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  1604.   *                the configuration information for the specified IRDA module.
  1605.   * @retval HAL state
  1606.   */
  1607. HAL_IRDA_StateTypeDef HAL_IRDA_GetState(IRDA_HandleTypeDef *hirda)
  1608. {
  1609.   uint32_t temp1 = 0x00U, temp2 = 0x00U;
  1610.   temp1 = hirda->gState;
  1611.   temp2 = hirda->RxState;
  1612.  
  1613.   return (HAL_IRDA_StateTypeDef)(temp1 | temp2);
  1614. }
  1615.  
  1616. /**
  1617.   * @brief  Return the IRDA error code
  1618.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  1619.   *                the configuration information for the specified IRDA module.
  1620.   * @retval IRDA Error Code
  1621.   */
  1622. uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef *hirda)
  1623. {
  1624.   return hirda->ErrorCode;
  1625. }
  1626.  
  1627. /**
  1628.   * @}
  1629.   */
  1630.  
  1631. /**
  1632.   * @brief  DMA IRDA transmit process complete callback.
  1633.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1634.   *               the configuration information for the specified DMA module.
  1635.   * @retval None
  1636.   */
  1637. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1638. {
  1639.   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1640.   /* DMA Normal mode */
  1641.   if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  1642.   {
  1643.     hirda->TxXferCount = 0U;
  1644.    
  1645.     /* Disable the DMA transfer for transmit request by setting the DMAT bit
  1646.     in the IRDA CR3 register */
  1647.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1648.    
  1649.     /* Enable the IRDA Transmit Complete Interrupt */
  1650.     __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TC);
  1651.   }
  1652.   /* DMA Circular mode */
  1653.   else
  1654.   {
  1655.     HAL_IRDA_TxCpltCallback(hirda);
  1656.   }
  1657. }
  1658.  
  1659. /**
  1660.   * @brief DMA IRDA receive process half complete callback
  1661.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1662.   *                the configuration information for the specified DMA module.
  1663.   * @retval None
  1664.   */
  1665. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma)
  1666. {
  1667.   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1668.  
  1669.   HAL_IRDA_TxHalfCpltCallback(hirda);
  1670. }
  1671.  
  1672. /**
  1673.   * @brief  DMA IRDA receive process complete callback.
  1674.   * @param  hdma: DMA handle
  1675.   * @retval None
  1676.   */
  1677. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)  
  1678. {
  1679.   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1680.   /* DMA Normal mode */
  1681.   if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  1682.   {
  1683.     hirda->RxXferCount = 0U;
  1684.  
  1685.     /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1686.     CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  1687.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1688.    
  1689.     /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1690.     in the IRDA CR3 register */
  1691.     CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1692.  
  1693.     /* At end of Rx process, restore hirda->RxState to Ready */
  1694.     hirda->RxState = HAL_IRDA_STATE_READY;
  1695.   }
  1696.   HAL_IRDA_RxCpltCallback(hirda);
  1697. }
  1698.  
  1699. /**
  1700.   * @brief DMA IRDA receive process half complete callback
  1701.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1702.   *                the configuration information for the specified DMA module.
  1703.   * @retval None
  1704.   */
  1705. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma)
  1706. {
  1707.   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1708.   HAL_IRDA_RxHalfCpltCallback(hirda);
  1709. }
  1710.  
  1711. /**
  1712.   * @brief  DMA IRDA communication error callback.
  1713.   * @param  hdma: DMA handle
  1714.   * @retval None
  1715.   */
  1716. static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
  1717. {
  1718.   uint32_t dmarequest = 0x00U;
  1719.   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1720.  
  1721.   /* Stop IRDA DMA Tx request if ongoing */
  1722.   dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT);
  1723.   if((hirda->gState == HAL_IRDA_STATE_BUSY_TX) && dmarequest)
  1724.   {
  1725.     hirda->TxXferCount = 0U;
  1726.     IRDA_EndTxTransfer(hirda);
  1727.   }
  1728.  
  1729.   /* Stop IRDA DMA Rx request if ongoing */
  1730.   dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR);
  1731.   if((hirda->RxState == HAL_IRDA_STATE_BUSY_RX) && dmarequest)
  1732.   {
  1733.     hirda->RxXferCount = 0U;
  1734.     IRDA_EndRxTransfer(hirda);
  1735.   }
  1736.  
  1737.   hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
  1738.  
  1739.   HAL_IRDA_ErrorCallback(hirda);
  1740. }
  1741.  
  1742. /**
  1743.   * @brief  This function handles IRDA Communication Timeout.
  1744.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  1745.   *                the configuration information for the specified IRDA module.
  1746.   * @param  Flag: specifies the IRDA flag to check.
  1747.   * @param  Status: The new Flag status (SET or RESET).
  1748.   * @param  Tickstart: Tick start value
  1749.   * @param  Timeout: Timeout duration
  1750.   * @retval HAL status
  1751.   */
  1752. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1753. {
  1754.   /* Wait until flag is set */
  1755.   while((__HAL_IRDA_GET_FLAG(hirda, Flag) ? SET : RESET) == Status)
  1756.   {
  1757.     /* Check for the Timeout */
  1758.     if(Timeout != HAL_MAX_DELAY)
  1759.     {
  1760.       if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
  1761.       {
  1762.         /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1763.         CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  1764.         CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1765.        
  1766.         hirda->gState  = HAL_IRDA_STATE_READY;
  1767.         hirda->RxState = HAL_IRDA_STATE_READY;
  1768.        
  1769.         /* Process Unlocked */
  1770.         __HAL_UNLOCK(hirda);
  1771.        
  1772.         return HAL_TIMEOUT;
  1773.       }
  1774.     }
  1775.   }
  1776.   return HAL_OK;
  1777. }
  1778.  
  1779. /**
  1780.   * @brief  End ongoing Tx transfer on IRDA peripheral (following error detection or Transmit completion).
  1781.   * @param  hirda: IRDA handle.
  1782.   * @retval None
  1783.   */
  1784. static void IRDA_EndTxTransfer(IRDA_HandleTypeDef *hirda)
  1785. {
  1786.   /* Disable TXEIE and TCIE interrupts */
  1787.   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1788.  
  1789.   /* At end of Tx process, restore hirda->gState to Ready */
  1790.   hirda->gState = HAL_IRDA_STATE_READY;
  1791. }
  1792.  
  1793. /**
  1794.   * @brief  End ongoing Rx transfer on IRDA peripheral (following error detection or Reception completion).
  1795.   * @param  hirda: IRDA handle.
  1796.   * @retval None
  1797.   */
  1798. static void IRDA_EndRxTransfer(IRDA_HandleTypeDef *hirda)
  1799. {
  1800.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1801.   CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1802.   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1803.  
  1804.   /* At end of Rx process, restore hirda->RxState to Ready */
  1805.   hirda->RxState = HAL_IRDA_STATE_READY;
  1806. }
  1807.  
  1808. /**
  1809.   * @brief  DMA IRDA communication abort callback, when initiated by HAL services on Error
  1810.   *         (To be called at end of DMA Abort procedure following error occurrence).
  1811.   * @param  hdma DMA handle.
  1812.   * @retval None
  1813.   */
  1814. static void IRDA_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1815. {
  1816.   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1817.   hirda->RxXferCount = 0x00U;
  1818.   hirda->TxXferCount = 0x00U;
  1819.  
  1820.   HAL_IRDA_ErrorCallback(hirda);
  1821. }
  1822.  
  1823. /**
  1824.   * @brief  DMA IRDA Tx communication abort callback, when initiated by user
  1825.   *         (To be called at end of DMA Tx Abort procedure following user abort request).
  1826.   * @note   When this callback is executed, User Abort complete call back is called only if no
  1827.   *         Abort still ongoing for Rx DMA Handle.
  1828.   * @param  hdma DMA handle.
  1829.   * @retval None
  1830.   */
  1831. static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  1832. {
  1833.   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1834.  
  1835.   hirda->hdmatx->XferAbortCallback = NULL;
  1836.  
  1837.   /* Check if an Abort process is still ongoing */
  1838.   if(hirda->hdmarx != NULL)
  1839.   {
  1840.     if(hirda->hdmarx->XferAbortCallback != NULL)
  1841.     {
  1842.       return;
  1843.     }
  1844.   }
  1845.  
  1846.   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1847.   hirda->TxXferCount = 0x00U;
  1848.   hirda->RxXferCount = 0x00U;
  1849.  
  1850.   /* Reset ErrorCode */
  1851.   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1852.  
  1853.   /* Restore hirda->gState and hirda->RxState to Ready */
  1854.   hirda->gState  = HAL_IRDA_STATE_READY;
  1855.   hirda->RxState = HAL_IRDA_STATE_READY;
  1856.  
  1857.   /* Call user Abort complete callback */
  1858.   HAL_IRDA_AbortCpltCallback(hirda);
  1859. }
  1860.  
  1861. /**
  1862.   * @brief  DMA IRDA Rx communication abort callback, when initiated by user
  1863.   *         (To be called at end of DMA Rx Abort procedure following user abort request).
  1864.   * @note   When this callback is executed, User Abort complete call back is called only if no
  1865.   *         Abort still ongoing for Tx DMA Handle.
  1866.   * @param  hdma DMA handle.
  1867.   * @retval None
  1868.   */
  1869. static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  1870. {
  1871.   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1872.  
  1873.   hirda->hdmarx->XferAbortCallback = NULL;
  1874.  
  1875.   /* Check if an Abort process is still ongoing */
  1876.   if(hirda->hdmatx != NULL)
  1877.   {
  1878.     if(hirda->hdmatx->XferAbortCallback != NULL)
  1879.     {
  1880.       return;
  1881.     }
  1882.   }
  1883.  
  1884.   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1885.   hirda->TxXferCount = 0x00U;
  1886.   hirda->RxXferCount = 0x00U;
  1887.  
  1888.   /* Reset ErrorCode */
  1889.   hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1890.  
  1891.   /* Restore hirda->gState and hirda->RxState to Ready */
  1892.   hirda->gState  = HAL_IRDA_STATE_READY;
  1893.   hirda->RxState = HAL_IRDA_STATE_READY;
  1894.  
  1895.   /* Call user Abort complete callback */
  1896.   HAL_IRDA_AbortCpltCallback(hirda);
  1897. }
  1898.  
  1899. /**
  1900.   * @brief  DMA IRDA Tx communication abort callback, when initiated by user by a call to
  1901.   *         HAL_IRDA_AbortTransmit_IT API (Abort only Tx transfer)
  1902.   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  1903.   *         and leads to user Tx Abort Complete callback execution).
  1904.   * @param  hdma DMA handle.
  1905.   * @retval None
  1906.   */
  1907. static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1908. {
  1909.   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1910.  
  1911.   hirda->TxXferCount = 0x00U;
  1912.  
  1913.   /* Restore hirda->gState to Ready */
  1914.   hirda->gState = HAL_IRDA_STATE_READY;
  1915.  
  1916.   /* Call user Abort complete callback */
  1917.   HAL_IRDA_AbortTransmitCpltCallback(hirda);
  1918. }
  1919.  
  1920. /**
  1921.   * @brief  DMA IRDA Rx communication abort callback, when initiated by user by a call to
  1922.   *         HAL_IRDA_AbortReceive_IT API (Abort only Rx transfer)
  1923.   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  1924.   *         and leads to user Rx Abort Complete callback execution).
  1925.   * @param  hdma DMA handle.
  1926.   * @retval None
  1927.   */
  1928. static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1929. {
  1930.   IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1931.  
  1932.   hirda->RxXferCount = 0x00U;
  1933.  
  1934.   /* Restore hirda->RxState to Ready */
  1935.   hirda->RxState = HAL_IRDA_STATE_READY;
  1936.  
  1937.   /* Call user Abort complete callback */
  1938.   HAL_IRDA_AbortReceiveCpltCallback(hirda);
  1939. }
  1940.  
  1941. /**
  1942.   * @brief  Send an amount of data in non blocking mode.
  1943.   * @param  hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1944.   *                the configuration information for the specified IRDA module.
  1945.   * @retval HAL status
  1946.   */
  1947. static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
  1948. {
  1949.   uint16_t* tmp;
  1950.  
  1951.   /* Check that a Tx process is ongoing */
  1952.   if(hirda->gState == HAL_IRDA_STATE_BUSY_TX)
  1953.   {
  1954.     if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  1955.     {
  1956.       tmp = (uint16_t*) hirda->pTxBuffPtr;
  1957.       hirda->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
  1958.       if(hirda->Init.Parity == IRDA_PARITY_NONE)
  1959.       {
  1960.         hirda->pTxBuffPtr += 2U;
  1961.       }
  1962.       else
  1963.       {
  1964.         hirda->pTxBuffPtr += 1U;
  1965.       }
  1966.     }
  1967.     else
  1968.     {
  1969.       hirda->Instance->DR = (uint8_t)(*hirda->pTxBuffPtr++ & (uint8_t)0x00FF);
  1970.     }
  1971.  
  1972.     if(--hirda->TxXferCount == 0U)
  1973.     {
  1974.       /* Disable the IRDA Transmit Data Register Empty Interrupt */
  1975.       CLEAR_BIT(hirda->Instance->CR1, USART_CR1_TXEIE);
  1976.  
  1977.       /* Enable the IRDA Transmit Complete Interrupt */
  1978.       SET_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
  1979.     }
  1980.     return HAL_OK;
  1981.   }
  1982.   else
  1983.   {
  1984.     return HAL_BUSY;
  1985.   }
  1986. }
  1987.  
  1988. /**
  1989.   * @brief  Wraps up transmission in non blocking mode.
  1990.   * @param  hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1991.   *                the configuration information for the specified IRDA module.
  1992.   * @retval HAL status
  1993.   */
  1994. static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda)
  1995. {
  1996.   /* Disable the IRDA Transmit Complete Interrupt */    
  1997.   CLEAR_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
  1998.  
  1999.   /* Tx process is ended, restore hirda->gState to Ready */
  2000.   hirda->gState = HAL_IRDA_STATE_READY;
  2001.   HAL_IRDA_TxCpltCallback(hirda);
  2002.  
  2003.   return HAL_OK;
  2004. }
  2005.  
  2006. /**
  2007.   * @brief  Receives an amount of data in non blocking mode.
  2008.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  2009.   *                the configuration information for the specified IRDA module.
  2010.   * @retval HAL status
  2011.   */
  2012. static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
  2013. {
  2014.   uint16_t* tmp;
  2015.   uint16_t  uhdata;
  2016.  
  2017.   /* Check that a Rx process is ongoing */
  2018.   if(hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
  2019.   {
  2020.     uhdata = (uint16_t) READ_REG(hirda->Instance->DR);
  2021.     if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  2022.     {
  2023.       tmp = (uint16_t*) hirda->pRxBuffPtr;
  2024.       if(hirda->Init.Parity == IRDA_PARITY_NONE)
  2025.       {
  2026.         *tmp = (uint16_t)(uhdata & (uint16_t)0x01FF);
  2027.         hirda->pRxBuffPtr += 2U;
  2028.       }
  2029.       else
  2030.       {
  2031.         *tmp = (uint16_t)(uhdata & (uint16_t)0x00FF);
  2032.         hirda->pRxBuffPtr += 1U;
  2033.       }
  2034.     }
  2035.     else
  2036.     {
  2037.       if(hirda->Init.Parity == IRDA_PARITY_NONE)
  2038.       {
  2039.         *hirda->pRxBuffPtr++ = (uint8_t)(uhdata & (uint8_t)0x00FF);
  2040.       }
  2041.       else
  2042.       {
  2043.         *hirda->pRxBuffPtr++ = (uint8_t)(uhdata & (uint8_t)0x007F);
  2044.       }
  2045.     }
  2046.  
  2047.     if(--hirda->RxXferCount == 0U)
  2048.     {
  2049.       /* Disable the IRDA Data Register not empty Interrupt */
  2050.       __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
  2051.  
  2052.       /* Disable the IRDA Parity Error Interrupt */
  2053.       __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  2054.      
  2055.       /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  2056.       __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  2057.      
  2058.       /* Rx process is completed, restore hirda->RxState to Ready */
  2059.       hirda->RxState = HAL_IRDA_STATE_READY;
  2060.       HAL_IRDA_RxCpltCallback(hirda);
  2061.  
  2062.       return HAL_OK;
  2063.     }
  2064.     return HAL_OK;
  2065.   }
  2066.   else
  2067.   {
  2068.     return HAL_BUSY;
  2069.   }
  2070. }
  2071.  
  2072. /**
  2073.   * @brief  Configures the IRDA peripheral.
  2074.   * @param  hirda: Pointer to a IRDA_HandleTypeDef structure that contains
  2075.   *                the configuration information for the specified IRDA module.
  2076.   * @retval None
  2077.   */
  2078. static void IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
  2079. {
  2080.   /* Check the parameters */
  2081.   assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  2082.   assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));  
  2083.   assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
  2084.   assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
  2085.   assert_param(IS_IRDA_MODE(hirda->Init.Mode));
  2086.   assert_param(IS_IRDA_POWERMODE(hirda->Init.IrDAMode));
  2087.  
  2088.   /*-------------------------- USART CR2 Configuration ------------------------*/
  2089.   /* Clear STOP[13:12] bits */
  2090.   CLEAR_BIT(hirda->Instance->CR2, USART_CR2_STOP);
  2091.  
  2092.   /*-------------------------- USART CR1 Configuration -----------------------*/
  2093.   /* Clear M, PCE, PS, TE and RE bits */
  2094.   CLEAR_BIT(hirda->Instance->CR1, USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE);
  2095.  
  2096.   /* Configure the USART Word Length, Parity and mode:
  2097.   Set the M bits according to hirda->Init.WordLength value
  2098.   Set PCE and PS bits according to hirda->Init.Parity value
  2099.   Set TE and RE bits according to hirda->Init.Mode value */
  2100.   /* Write to USART CR1 */
  2101.   SET_BIT(hirda->Instance->CR1, (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode);
  2102.  
  2103.   /*-------------------------- USART CR3 Configuration -----------------------*/
  2104.   /* Clear CTSE and RTSE bits */
  2105.   CLEAR_BIT(hirda->Instance->CR3, USART_CR3_RTSE | USART_CR3_CTSE);
  2106.  
  2107.   /*-------------------------- USART BRR Configuration -----------------------*/
  2108.   if(hirda->Instance == USART1)
  2109.   {
  2110.     SET_BIT(hirda->Instance->BRR, IRDA_BRR(HAL_RCC_GetPCLK2Freq(), hirda->Init.BaudRate));
  2111.   }
  2112.   else
  2113.   {
  2114.     SET_BIT(hirda->Instance->BRR, IRDA_BRR(HAL_RCC_GetPCLK1Freq(), hirda->Init.BaudRate));
  2115.   }
  2116. }
  2117.  
  2118. /**
  2119.   * @}
  2120.   */
  2121.  
  2122. #endif /* HAL_IRDA_MODULE_ENABLED */
  2123. /**
  2124.   * @}
  2125.   */
  2126.  
  2127. /**
  2128.   * @}
  2129.   */
  2130.  
  2131. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  2132.