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.
 
 
 

2879 lines
99 KiB

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