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.
 
 
 

2380 lines
84 KiB

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