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

2109 lines
70 KiB

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