You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2241 lines
76 KiB

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