25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

2709 satır
94 KiB

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