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.
 
 
 

2899 lines
100 KiB

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