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.
 
 
 

1478 lines
46 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_irda.c
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 19-June-2014
  7. * @brief IRDA HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the IrDA SIR ENDEC block (IrDA):
  10. * + Initialization and de-initialization methods
  11. * + IO operation methods
  12. * + Peripheral Control methods
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The IRDA HAL driver can be used as follows:
  20. (#) Declare a IRDA_HandleTypeDef handle structure.
  21. (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API:
  22. (##) Enable the USARTx interface clock.
  23. (##) IRDA pins configuration:
  24. (+++) Enable the clock for the IRDA GPIOs.
  25. (+++) Configure these IRDA pins as alternate function pull-up.
  26. (##) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
  27. and HAL_IRDA_Receive_IT() APIs):
  28. (+++) Configure the USARTx interrupt priority.
  29. (+++) Enable the NVIC USART IRQ handle.
  30. (##) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
  31. and HAL_IRDA_Receive_DMA() APIs):
  32. (+++) Declare a DMA handle structure for the Tx/Rx stream.
  33. (+++) Enable the DMAx interface clock.
  34. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  35. (+++) Configure the DMA Tx/Rx Stream.
  36. (+++) Associate the initilalized DMA handle to the IRDA DMA Tx/Rx handle.
  37. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
  38. (#) Program the Baud Rate, Word Length, Parity, IrDA Mode, Prescaler
  39. and Mode(Receiver/Transmitter) in the hirda Init structure.
  40. (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
  41. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  42. by calling the customed HAL_IRDA_MspInit() API.
  43. -@@- The specific IRDA interrupts (Transmission complete interrupt,
  44. RXNE interrupt and Error Interrupts) will be managed using the macros
  45. __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
  46. (#) Three operation modes are available within this driver :
  47. *** Polling mode IO operation ***
  48. =================================
  49. [..]
  50. (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
  51. (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
  52. *** Interrupt mode IO operation ***
  53. ===================================
  54. [..]
  55. (+) Send an amount of data in non blocking mode using HAL_IRDA_Transmit_IT()
  56. (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
  57. add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
  58. (+) Receive an amount of data in non blocking mode using HAL_IRDA_Receive_IT()
  59. (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
  60. add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
  61. (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
  62. add his own code by customization of function pointer HAL_IRDA_ErrorCallback
  63. *** DMA mode IO operation ***
  64. =============================
  65. [..]
  66. (+) Send an amount of data in non blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
  67. (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
  68. add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
  69. (+) Receive an amount of data in non blocking mode (DMA) using HAL_IRDA_Receive_DMA()
  70. (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
  71. add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
  72. (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
  73. add his own code by customization of function pointer HAL_IRDA_ErrorCallback
  74. *** IRDA HAL driver macros list ***
  75. ===================================
  76. [..]
  77. Below the list of most used macros in IRDA HAL driver.
  78. (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
  79. (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
  80. (+) __HAL_IRDA_GET_FLAG : Checks whether the specified IRDA flag is set or not
  81. (+) __HAL_IRDA_CLEAR_FLAG : Clears the specified IRDA pending flag
  82. (+) __HAL_IRDA_ENABLE_IT: Enables the specified IRDA interrupt
  83. (+) __HAL_IRDA_DISABLE_IT: Disables the specified IRDA interrupt
  84. (@) You can refer to the IRDA HAL driver header file for more useful macros
  85. @endverbatim
  86. ******************************************************************************
  87. * @attention
  88. *
  89. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  90. *
  91. * Redistribution and use in source and binary forms, with or without modification,
  92. * are permitted provided that the following conditions are met:
  93. * 1. Redistributions of source code must retain the above copyright notice,
  94. * this list of conditions and the following disclaimer.
  95. * 2. Redistributions in binary form must reproduce the above copyright notice,
  96. * this list of conditions and the following disclaimer in the documentation
  97. * and/or other materials provided with the distribution.
  98. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  99. * may be used to endorse or promote products derived from this software
  100. * without specific prior written permission.
  101. *
  102. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  103. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  104. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  105. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  106. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  107. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  108. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  109. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  110. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  111. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  112. *
  113. ******************************************************************************
  114. */
  115. /* Includes ------------------------------------------------------------------*/
  116. #include "stm32f4xx_hal.h"
  117. /** @addtogroup STM32F4xx_HAL_Driver
  118. * @{
  119. */
  120. /** @defgroup IRDA
  121. * @brief HAL IRDA module driver
  122. * @{
  123. */
  124. #ifdef HAL_IRDA_MODULE_ENABLED
  125. /* Private typedef -----------------------------------------------------------*/
  126. /* Private define ------------------------------------------------------------*/
  127. #define IRDA_TIMEOUT_VALUE 22000
  128. /* Private macro -------------------------------------------------------------*/
  129. /* Private variables ---------------------------------------------------------*/
  130. /* Private function prototypes -----------------------------------------------*/
  131. static void IRDA_SetConfig (IRDA_HandleTypeDef *hirda);
  132. static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
  133. static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
  134. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  135. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma);
  136. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  137. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma);
  138. static void IRDA_DMAError(DMA_HandleTypeDef *hdma);
  139. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  140. /* Private functions ---------------------------------------------------------*/
  141. /** @defgroup IRDA_Private_Functions
  142. * @{
  143. */
  144. /** @defgroup IRDA_Group1 IrDA Initialization and de-initialization functions
  145. * @brief Initialization and Configuration functions
  146. *
  147. @verbatim
  148. ===============================================================================
  149. ##### Initialization and Configuration functions #####
  150. ===============================================================================
  151. [..]
  152. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  153. in IrDA mode.
  154. (+) For the asynchronous mode only these parameters can be configured:
  155. (++) BaudRate
  156. (++) WordLength
  157. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  158. in the data register is transmitted but is changed by the parity bit.
  159. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  160. please refer to Reference manual for possible IRDA frame formats.
  161. (++) Prescaler: A pulse of width less than two and greater than one PSC period(s) may or may
  162. not be rejected. The receiver set up time should be managed by software. The IrDA physical layer
  163. specification specifies a minimum of 10 ms delay between transmission and
  164. reception (IrDA is a half duplex protocol).
  165. (++) Mode: Receiver/transmitter modes
  166. (++) IrDAMode: the IrDA can operate in the Normal mode or in the Low power mode.
  167. [..]
  168. The HAL_IRDA_Init() API follows IRDA configuration procedures (details for the procedures
  169. are available in reference manual).
  170. @endverbatim
  171. * @{
  172. */
  173. /**
  174. * @brief Initializes the IRDA mode according to the specified
  175. * parameters in the IRDA_InitTypeDef and create the associated handle.
  176. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  177. * the configuration information for the specified IRDA module.
  178. * @retval HAL status
  179. */
  180. HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
  181. {
  182. /* Check the IRDA handle allocation */
  183. if(hirda == NULL)
  184. {
  185. return HAL_ERROR;
  186. }
  187. /* Check the IRDA instance parameters */
  188. assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  189. /* Check the IRDA mode parameter in the IRDA handle */
  190. assert_param(IS_IRDA_POWERMODE(hirda->Init.IrDAMode));
  191. if(hirda->State == HAL_IRDA_STATE_RESET)
  192. {
  193. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  194. HAL_IRDA_MspInit(hirda);
  195. }
  196. hirda->State = HAL_IRDA_STATE_BUSY;
  197. /* Disable the IRDA peripheral */
  198. __IRDA_DISABLE(hirda);
  199. /* Set the IRDA communication parameters */
  200. IRDA_SetConfig(hirda);
  201. /* In IrDA mode, the following bits must be kept cleared:
  202. - LINEN, STOP and CLKEN bits in the USART_CR2 register,
  203. - SCEN and HDSEL bits in the USART_CR3 register.*/
  204. hirda->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_STOP | USART_CR2_CLKEN);
  205. hirda->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL);
  206. /* Enable the IRDA peripheral */
  207. __IRDA_ENABLE(hirda);
  208. /* Set the prescaler */
  209. MODIFY_REG(hirda->Instance->GTPR, USART_GTPR_PSC, hirda->Init.Prescaler);
  210. /* Configure the IrDA mode */
  211. MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.IrDAMode);
  212. /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
  213. hirda->Instance->CR3 |= USART_CR3_IREN;
  214. /* Initialize the IRDA state*/
  215. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  216. hirda->State= HAL_IRDA_STATE_READY;
  217. return HAL_OK;
  218. }
  219. /**
  220. * @brief DeInitializes the IRDA peripheral
  221. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  222. * the configuration information for the specified IRDA module.
  223. * @retval HAL status
  224. */
  225. HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
  226. {
  227. /* Check the IRDA handle allocation */
  228. if(hirda == NULL)
  229. {
  230. return HAL_ERROR;
  231. }
  232. /* Check the parameters */
  233. assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  234. hirda->State = HAL_IRDA_STATE_BUSY;
  235. /* DeInit the low level hardware */
  236. HAL_IRDA_MspDeInit(hirda);
  237. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  238. hirda->State = HAL_IRDA_STATE_RESET;
  239. /* Release Lock */
  240. __HAL_UNLOCK(hirda);
  241. return HAL_OK;
  242. }
  243. /**
  244. * @brief IRDA MSP Init.
  245. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  246. * the configuration information for the specified IRDA module.
  247. * @retval None
  248. */
  249. __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
  250. {
  251. /* NOTE : This function Should not be modified, when the callback is needed,
  252. the HAL_IRDA_MspInit could be implenetd in the user file
  253. */
  254. }
  255. /**
  256. * @brief IRDA MSP DeInit.
  257. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  258. * the configuration information for the specified IRDA module.
  259. * @retval None
  260. */
  261. __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
  262. {
  263. /* NOTE : This function Should not be modified, when the callback is needed,
  264. the HAL_IRDA_MspDeInit could be implenetd in the user file
  265. */
  266. }
  267. /**
  268. * @}
  269. */
  270. /** @defgroup IRDA_Group2 IO operation functions
  271. * @brief IRDA Transmit/Receive functions
  272. *
  273. @verbatim
  274. ===============================================================================
  275. ##### IO operation functions #####
  276. ===============================================================================
  277. This subsection provides a set of functions allowing to manage the IRDA data transfers.
  278. [..]
  279. IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
  280. on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
  281. is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
  282. While receiving data, transmission should be avoided as the data to be transmitted
  283. could be corrupted.
  284. (#) There are two modes of transfer:
  285. (++) Blocking mode: The communication is performed in polling mode.
  286. The HAL status of all data processing is returned by the same function
  287. after finishing transfer.
  288. (++) No-Blocking mode: The communication is performed using Interrupts
  289. or DMA, These APIs return the HAL status.
  290. The end of the data processing will be indicated through the
  291. dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
  292. using DMA mode.
  293. The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
  294. will be executed respectivelly at the end of the transmit or Receive process
  295. The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
  296. (#) Blocking mode API's are :
  297. (++) HAL_IRDA_Transmit()
  298. (++) HAL_IRDA_Receive()
  299. (#) Non Blocking mode APIs with Interrupt are :
  300. (++) HAL_IRDA_Transmit_IT()
  301. (++) HAL_IRDA_Receive_IT()
  302. (++) HAL_IRDA_IRQHandler()
  303. (#) Non Blocking mode functions with DMA are :
  304. (++) HAL_IRDA_Transmit_DMA()
  305. (++) HAL_IRDA_Receive_DMA()
  306. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  307. (++) HAL_IRDA_TxCpltCallback()
  308. (++) HAL_IRDA_RxCpltCallback()
  309. (++) HAL_IRDA_ErrorCallback()
  310. @endverbatim
  311. * @{
  312. */
  313. /**
  314. * @brief Sends an amount of data in blocking mode.
  315. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  316. * the configuration information for the specified IRDA module.
  317. * @param pData: Pointer to data buffer
  318. * @param Size: Amount of data to be sent
  319. * @param Timeout: Specify timeout value
  320. * @retval HAL status
  321. */
  322. HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  323. {
  324. uint16_t* tmp;
  325. uint32_t tmp1 = 0;
  326. tmp1 = hirda->State;
  327. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_RX))
  328. {
  329. if((pData == NULL) || (Size == 0))
  330. {
  331. return HAL_ERROR;
  332. }
  333. /* Process Locked */
  334. __HAL_LOCK(hirda);
  335. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  336. if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  337. {
  338. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  339. }
  340. else
  341. {
  342. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  343. }
  344. hirda->TxXferSize = Size;
  345. hirda->TxXferCount = Size;
  346. while(hirda->TxXferCount > 0)
  347. {
  348. hirda->TxXferCount--;
  349. if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  350. {
  351. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
  352. {
  353. return HAL_TIMEOUT;
  354. }
  355. tmp = (uint16_t*) pData;
  356. hirda->Instance->DR = (*tmp & (uint16_t)0x01FF);
  357. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  358. {
  359. pData +=2;
  360. }
  361. else
  362. {
  363. pData +=1;
  364. }
  365. }
  366. else
  367. {
  368. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
  369. {
  370. return HAL_TIMEOUT;
  371. }
  372. hirda->Instance->DR = (*pData++ & (uint8_t)0xFF);
  373. }
  374. }
  375. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, Timeout) != HAL_OK)
  376. {
  377. return HAL_TIMEOUT;
  378. }
  379. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  380. {
  381. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  382. }
  383. else
  384. {
  385. hirda->State = HAL_IRDA_STATE_READY;
  386. }
  387. /* Process Unlocked */
  388. __HAL_UNLOCK(hirda);
  389. return HAL_OK;
  390. }
  391. else
  392. {
  393. return HAL_BUSY;
  394. }
  395. }
  396. /**
  397. * @brief Receive an amount of data in blocking mode.
  398. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  399. * the configuration information for the specified IRDA module.
  400. * @param pData: Pointer to data buffer
  401. * @param Size: Amount of data to be received
  402. * @param Timeout: Specify timeout value
  403. * @retval HAL status
  404. */
  405. HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  406. {
  407. uint16_t* tmp;
  408. uint32_t tmp1 = 0;
  409. tmp1 = hirda->State;
  410. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_TX))
  411. {
  412. if((pData == NULL) || (Size == 0))
  413. {
  414. return HAL_ERROR;
  415. }
  416. /* Process Locked */
  417. __HAL_LOCK(hirda);
  418. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  419. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  420. {
  421. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  422. }
  423. else
  424. {
  425. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  426. }
  427. hirda->RxXferSize = Size;
  428. hirda->RxXferCount = Size;
  429. /* Check the remain data to be received */
  430. while(hirda->RxXferCount > 0)
  431. {
  432. hirda->RxXferCount--;
  433. if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  434. {
  435. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  436. {
  437. return HAL_TIMEOUT;
  438. }
  439. tmp = (uint16_t*) pData ;
  440. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  441. {
  442. *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x01FF);
  443. pData +=2;
  444. }
  445. else
  446. {
  447. *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x00FF);
  448. pData +=1;
  449. }
  450. }
  451. else
  452. {
  453. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  454. {
  455. return HAL_TIMEOUT;
  456. }
  457. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  458. {
  459. *pData++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x00FF);
  460. }
  461. else
  462. {
  463. *pData++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x007F);
  464. }
  465. }
  466. }
  467. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  468. {
  469. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  470. }
  471. else
  472. {
  473. hirda->State = HAL_IRDA_STATE_READY;
  474. }
  475. /* Process Unlocked */
  476. __HAL_UNLOCK(hirda);
  477. return HAL_OK;
  478. }
  479. else
  480. {
  481. return HAL_BUSY;
  482. }
  483. }
  484. /**
  485. * @brief Send an amount of data in non blocking mode.
  486. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  487. * the configuration information for the specified IRDA module.
  488. * @param pData: Pointer to data buffer
  489. * @param Size: Amount of data to be sent
  490. * @retval HAL status
  491. */
  492. HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  493. {
  494. uint32_t tmp1 = 0;
  495. tmp1 = hirda->State;
  496. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_RX))
  497. {
  498. if((pData == NULL) || (Size == 0))
  499. {
  500. return HAL_ERROR;
  501. }
  502. /* Process Locked */
  503. __HAL_LOCK(hirda);
  504. hirda->pTxBuffPtr = pData;
  505. hirda->TxXferSize = Size;
  506. hirda->TxXferCount = Size;
  507. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  508. if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  509. {
  510. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  511. }
  512. else
  513. {
  514. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  515. }
  516. /* Enable the IRDA Parity Error Interrupt */
  517. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_PE);
  518. /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  519. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
  520. /* Process Unlocked */
  521. __HAL_UNLOCK(hirda);
  522. /* Enable the IRDA Transmit Data Register Empty Interrupt */
  523. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TXE);
  524. return HAL_OK;
  525. }
  526. else
  527. {
  528. return HAL_BUSY;
  529. }
  530. }
  531. /**
  532. * @brief Receives an amount of data in non blocking mode.
  533. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  534. * the configuration information for the specified IRDA module.
  535. * @param pData: Pointer to data buffer
  536. * @param Size: Amount of data to be received
  537. * @retval HAL status
  538. */
  539. HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  540. {
  541. uint32_t tmp1 = 0;
  542. tmp1 = hirda->State;
  543. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_TX))
  544. {
  545. if((pData == NULL) || (Size == 0))
  546. {
  547. return HAL_ERROR;
  548. }
  549. /* Process Locked */
  550. __HAL_LOCK(hirda);
  551. hirda->pRxBuffPtr = pData;
  552. hirda->RxXferSize = Size;
  553. hirda->RxXferCount = Size;
  554. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  555. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  556. {
  557. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  558. }
  559. else
  560. {
  561. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  562. }
  563. /* Enable the IRDA Data Register not empty Interrupt */
  564. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_RXNE);
  565. /* Enable the IRDA Parity Error Interrupt */
  566. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_PE);
  567. /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  568. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
  569. /* Process Unlocked */
  570. __HAL_UNLOCK(hirda);
  571. return HAL_OK;
  572. }
  573. else
  574. {
  575. return HAL_BUSY;
  576. }
  577. }
  578. /**
  579. * @brief Sends an amount of data in non blocking mode.
  580. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  581. * the configuration information for the specified IRDA module.
  582. * @param pData: Pointer to data buffer
  583. * @param Size: Amount of data to be sent
  584. * @retval HAL status
  585. */
  586. HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  587. {
  588. uint32_t *tmp;
  589. uint32_t tmp1 = 0;
  590. tmp1 = hirda->State;
  591. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_RX))
  592. {
  593. if((pData == NULL) || (Size == 0))
  594. {
  595. return HAL_ERROR;
  596. }
  597. /* Process Locked */
  598. __HAL_LOCK(hirda);
  599. hirda->pTxBuffPtr = pData;
  600. hirda->TxXferSize = Size;
  601. hirda->TxXferCount = Size;
  602. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  603. if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  604. {
  605. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  606. }
  607. else
  608. {
  609. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  610. }
  611. /* Set the IRDA DMA transfert complete callback */
  612. hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
  613. /* Set the IRDA DMA half transfert complete callback */
  614. hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;
  615. /* Set the DMA error callback */
  616. hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
  617. /* Enable the IRDA transmit DMA Stream */
  618. tmp = (uint32_t*)&pData;
  619. HAL_DMA_Start_IT(hirda->hdmatx, *(uint32_t*)tmp, (uint32_t)&hirda->Instance->DR, Size);
  620. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  621. in the USART CR3 register */
  622. hirda->Instance->CR3 |= USART_CR3_DMAT;
  623. /* Process Unlocked */
  624. __HAL_UNLOCK(hirda);
  625. return HAL_OK;
  626. }
  627. else
  628. {
  629. return HAL_BUSY;
  630. }
  631. }
  632. /**
  633. * @brief Receives an amount of data in non blocking mode.
  634. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  635. * the configuration information for the specified IRDA module.
  636. * @param pData: Pointer to data buffer
  637. * @param Size: Amount of data to be received
  638. * @note When the IRDA parity is enabled (PCE = 1) the data received contain the parity bit.
  639. * @retval HAL status
  640. */
  641. HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  642. {
  643. uint32_t *tmp;
  644. uint32_t tmp1 = 0;
  645. tmp1 = hirda->State;
  646. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_TX))
  647. {
  648. if((pData == NULL) || (Size == 0))
  649. {
  650. return HAL_ERROR;
  651. }
  652. /* Process Locked */
  653. __HAL_LOCK(hirda);
  654. hirda->pRxBuffPtr = pData;
  655. hirda->RxXferSize = Size;
  656. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  657. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  658. {
  659. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  660. }
  661. else
  662. {
  663. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  664. }
  665. /* Set the IRDA DMA transfert complete callback */
  666. hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
  667. /* Set the IRDA DMA half transfert complete callback */
  668. hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;
  669. /* Set the DMA error callback */
  670. hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
  671. /* Enable the DMA Stream */
  672. tmp = (uint32_t*)&pData;
  673. HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->DR, *(uint32_t*)tmp, Size);
  674. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  675. in the USART CR3 register */
  676. hirda->Instance->CR3 |= USART_CR3_DMAR;
  677. /* Process Unlocked */
  678. __HAL_UNLOCK(hirda);
  679. return HAL_OK;
  680. }
  681. else
  682. {
  683. return HAL_BUSY;
  684. }
  685. }
  686. /**
  687. * @brief Pauses the DMA Transfer.
  688. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  689. * the configuration information for the specified IRDA module.
  690. * @retval HAL status
  691. */
  692. HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda)
  693. {
  694. /* Process Locked */
  695. __HAL_LOCK(hirda);
  696. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  697. {
  698. /* Disable the UART DMA Tx request */
  699. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  700. }
  701. else if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  702. {
  703. /* Disable the UART DMA Rx request */
  704. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  705. }
  706. else if (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  707. {
  708. /* Disable the UART DMA Tx & Rx requests */
  709. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  710. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  711. }
  712. /* Process Unlocked */
  713. __HAL_UNLOCK(hirda);
  714. return HAL_OK;
  715. }
  716. /**
  717. * @brief Resumes the DMA Transfer.
  718. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  719. * the configuration information for the specified UART module.
  720. * @retval HAL status
  721. */
  722. HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda)
  723. {
  724. /* Process Locked */
  725. __HAL_LOCK(hirda);
  726. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  727. {
  728. /* Enable the UART DMA Tx request */
  729. hirda->Instance->CR3 |= USART_CR3_DMAT;
  730. }
  731. else if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  732. {
  733. /* Clear the Overrun flag before resumming the Rx transfer*/
  734. __HAL_IRDA_CLEAR_OREFLAG(hirda);
  735. /* Enable the UART DMA Rx request */
  736. hirda->Instance->CR3 |= USART_CR3_DMAR;
  737. }
  738. else if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  739. {
  740. /* Clear the Overrun flag before resumming the Rx transfer*/
  741. __HAL_IRDA_CLEAR_OREFLAG(hirda);
  742. /* Enable the UART DMA Tx & Rx request */
  743. hirda->Instance->CR3 |= USART_CR3_DMAT;
  744. hirda->Instance->CR3 |= USART_CR3_DMAR;
  745. }
  746. /* Process Unlocked */
  747. __HAL_UNLOCK(hirda);
  748. return HAL_OK;
  749. }
  750. /**
  751. * @brief Stops the DMA Transfer.
  752. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  753. * the configuration information for the specified UART module.
  754. * @retval HAL status
  755. */
  756. HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda)
  757. {
  758. /* The Lock is not implemented on this API to allow the user application
  759. to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
  760. when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  761. and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
  762. */
  763. /* Disable the UART Tx/Rx DMA requests */
  764. hirda->Instance->CR3 &= ~USART_CR3_DMAT;
  765. hirda->Instance->CR3 &= ~USART_CR3_DMAR;
  766. /* Abort the UART DMA tx Stream */
  767. if(hirda->hdmatx != NULL)
  768. {
  769. HAL_DMA_Abort(hirda->hdmatx);
  770. }
  771. /* Abort the UART DMA rx Stream */
  772. if(hirda->hdmarx != NULL)
  773. {
  774. HAL_DMA_Abort(hirda->hdmarx);
  775. }
  776. hirda->State = HAL_IRDA_STATE_READY;
  777. return HAL_OK;
  778. }
  779. /**
  780. * @brief This function handles IRDA interrupt request.
  781. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  782. * the configuration information for the specified IRDA module.
  783. * @retval None
  784. */
  785. void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
  786. {
  787. uint32_t tmp1 = 0, tmp2 =0;
  788. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_PE);
  789. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_PE);
  790. /* IRDA parity error interrupt occurred -------------------------------------*/
  791. if((tmp1 != RESET) && (tmp2 != RESET))
  792. {
  793. __HAL_IRDA_CLEAR_PEFLAG(hirda);
  794. hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
  795. }
  796. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_FE);
  797. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR);
  798. /* IRDA frame error interrupt occurred --------------------------------------*/
  799. if((tmp1 != RESET) && (tmp2 != RESET))
  800. {
  801. __HAL_IRDA_CLEAR_FEFLAG(hirda);
  802. hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
  803. }
  804. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_NE);
  805. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR);
  806. /* IRDA noise error interrupt occurred --------------------------------------*/
  807. if((tmp1 != RESET) && (tmp2 != RESET))
  808. {
  809. __HAL_IRDA_CLEAR_NEFLAG(hirda);
  810. hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
  811. }
  812. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_ORE);
  813. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR);
  814. /* IRDA Over-Run interrupt occurred -----------------------------------------*/
  815. if((tmp1 != RESET) && (tmp2 != RESET))
  816. {
  817. __HAL_IRDA_CLEAR_OREFLAG(hirda);
  818. hirda->ErrorCode |= HAL_IRDA_ERROR_ORE;
  819. }
  820. /* Call the Error call Back in case of Errors */
  821. if(hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
  822. {
  823. /* Set the IRDA state ready to be able to start again the process */
  824. hirda->State = HAL_IRDA_STATE_READY;
  825. HAL_IRDA_ErrorCallback(hirda);
  826. }
  827. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_RXNE);
  828. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_RXNE);
  829. /* IRDA in mode Receiver ---------------------------------------------------*/
  830. if((tmp1 != RESET) && (tmp2 != RESET))
  831. {
  832. IRDA_Receive_IT(hirda);
  833. }
  834. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_TXE);
  835. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TXE);
  836. /* IRDA in mode Transmitter ------------------------------------------------*/
  837. if((tmp1 != RESET) &&(tmp2 != RESET))
  838. {
  839. IRDA_Transmit_IT(hirda);
  840. }
  841. }
  842. /**
  843. * @brief Tx Transfer complete callbacks.
  844. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  845. * the configuration information for the specified IRDA module.
  846. * @retval None
  847. */
  848. __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
  849. {
  850. /* NOTE : This function Should not be modified, when the callback is needed,
  851. the HAL_IRDA_TxCpltCallback could be implemented in the user file
  852. */
  853. }
  854. /**
  855. * @brief Tx Half Transfer completed callbacks.
  856. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  857. * the configuration information for the specified USART module.
  858. * @retval None
  859. */
  860. __weak void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  861. {
  862. /* NOTE: This function Should not be modified, when the callback is needed,
  863. the HAL_IRDA_TxHalfCpltCallback could be implemented in the user file
  864. */
  865. }
  866. /**
  867. * @brief Rx Transfer complete callbacks.
  868. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  869. * the configuration information for the specified IRDA module.
  870. * @retval None
  871. */
  872. __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
  873. {
  874. /* NOTE : This function Should not be modified, when the callback is needed,
  875. the HAL_IRDA_RxCpltCallback could be implemented in the user file
  876. */
  877. }
  878. /**
  879. * @brief Rx Half Transfer complete callbacks.
  880. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  881. * the configuration information for the specified IRDA module.
  882. * @retval None
  883. */
  884. __weak void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  885. {
  886. /* NOTE : This function Should not be modified, when the callback is needed,
  887. the HAL_IRDA_RxHalfCpltCallback could be implemented in the user file
  888. */
  889. }
  890. /**
  891. * @brief IRDA error callbacks.
  892. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  893. * the configuration information for the specified IRDA module.
  894. * @retval None
  895. */
  896. __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
  897. {
  898. /* NOTE : This function Should not be modified, when the callback is needed,
  899. the HAL_IRDA_ErrorCallback could be implemented in the user file
  900. */
  901. }
  902. /**
  903. * @}
  904. */
  905. /** @defgroup IRDA_Group3 Peripheral State and Errors functions
  906. * @brief IRDA State and Errors functions
  907. *
  908. @verbatim
  909. ==============================================================================
  910. ##### Peripheral State and Errors functions #####
  911. ==============================================================================
  912. [..]
  913. This subsection provides a set of functions allowing to return the State of IrDA
  914. communication process and also return Peripheral Errors occurred during communication process
  915. (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state of the IrDA peripheral.
  916. (+) HAL_IRDA_GetError() check in run-time errors that could be occurred during communication.
  917. @endverbatim
  918. * @{
  919. */
  920. /**
  921. * @brief Returns the IRDA state.
  922. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  923. * the configuration information for the specified IRDA module.
  924. * @retval HAL state
  925. */
  926. HAL_IRDA_StateTypeDef HAL_IRDA_GetState(IRDA_HandleTypeDef *hirda)
  927. {
  928. return hirda->State;
  929. }
  930. /**
  931. * @brief Return the IARDA error code
  932. * @param hirda : pointer to a IRDA_HandleTypeDef structure that contains
  933. * the configuration information for the specified IRDA.
  934. * @retval IRDA Error Code
  935. */
  936. uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef *hirda)
  937. {
  938. return hirda->ErrorCode;
  939. }
  940. /**
  941. * @}
  942. */
  943. /**
  944. * @brief DMA IRDA transmit process complete callback.
  945. * @param hdma : DMA handle
  946. * @retval None
  947. */
  948. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  949. {
  950. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  951. /* DMA Normal mode */
  952. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  953. {
  954. hirda->TxXferCount = 0;
  955. /* Disable the DMA transfer for transmit request by setting the DMAT bit
  956. in the IRDA CR3 register */
  957. hirda->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAT);
  958. /* Wait for IRDA TC Flag */
  959. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, IRDA_TIMEOUT_VALUE) != HAL_OK)
  960. {
  961. /* Timeout occurred */
  962. hirda->State = HAL_IRDA_STATE_TIMEOUT;
  963. HAL_IRDA_ErrorCallback(hirda);
  964. }
  965. else
  966. {
  967. /* No Timeout */
  968. /* Check if a receive process is ongoing or not */
  969. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  970. {
  971. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  972. }
  973. else
  974. {
  975. hirda->State = HAL_IRDA_STATE_READY;
  976. }
  977. HAL_IRDA_TxCpltCallback(hirda);
  978. }
  979. }
  980. /* DMA Circular mode */
  981. else
  982. {
  983. HAL_IRDA_TxCpltCallback(hirda);
  984. }
  985. }
  986. /**
  987. * @brief DMA IRDA receive process half complete callback
  988. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  989. * the configuration information for the specified DMA module.
  990. * @retval None
  991. */
  992. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma)
  993. {
  994. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  995. HAL_IRDA_TxHalfCpltCallback(hirda);
  996. }
  997. /**
  998. * @brief DMA IRDA receive process complete callback.
  999. * @param hdma: DMA handle
  1000. * @retval None
  1001. */
  1002. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1003. {
  1004. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1005. /* DMA Normal mode */
  1006. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1007. {
  1008. hirda->RxXferCount = 0;
  1009. /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1010. in the IRDA CR3 register */
  1011. hirda->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAR);
  1012. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  1013. {
  1014. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  1015. }
  1016. else
  1017. {
  1018. hirda->State = HAL_IRDA_STATE_READY;
  1019. }
  1020. }
  1021. HAL_IRDA_RxCpltCallback(hirda);
  1022. }
  1023. /**
  1024. * @brief DMA IRDA receive process half complete callback
  1025. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1026. * the configuration information for the specified DMA module.
  1027. * @retval None
  1028. */
  1029. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma)
  1030. {
  1031. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1032. HAL_IRDA_RxHalfCpltCallback(hirda);
  1033. }
  1034. /**
  1035. * @brief DMA IRDA communication error callback.
  1036. * @param hdma: DMA handle
  1037. * @retval None
  1038. */
  1039. static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
  1040. {
  1041. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1042. hirda->RxXferCount = 0;
  1043. hirda->TxXferCount = 0;
  1044. hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
  1045. hirda->State= HAL_IRDA_STATE_READY;
  1046. HAL_IRDA_ErrorCallback(hirda);
  1047. }
  1048. /**
  1049. * @brief This function handles IRDA Communication Timeout.
  1050. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1051. * the configuration information for the specified IRDA module.
  1052. * @param Flag: specifies the IRDA flag to check.
  1053. * @param Status: The new Flag status (SET or RESET).
  1054. * @param Timeout: Timeout duration
  1055. * @retval HAL status
  1056. */
  1057. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1058. {
  1059. uint32_t tickstart = 0;
  1060. /* Get tick */
  1061. tickstart = HAL_GetTick();
  1062. /* Wait until flag is set */
  1063. if(Status == RESET)
  1064. {
  1065. while(__HAL_IRDA_GET_FLAG(hirda, Flag) == RESET)
  1066. {
  1067. /* Check for the Timeout */
  1068. if(Timeout != HAL_MAX_DELAY)
  1069. {
  1070. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1071. {
  1072. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1073. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
  1074. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
  1075. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  1076. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1077. hirda->State= HAL_IRDA_STATE_READY;
  1078. /* Process Unlocked */
  1079. __HAL_UNLOCK(hirda);
  1080. return HAL_TIMEOUT;
  1081. }
  1082. }
  1083. }
  1084. }
  1085. else
  1086. {
  1087. while(__HAL_IRDA_GET_FLAG(hirda, Flag) != RESET)
  1088. {
  1089. /* Check for the Timeout */
  1090. if(Timeout != HAL_MAX_DELAY)
  1091. {
  1092. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1093. {
  1094. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1095. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
  1096. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
  1097. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  1098. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1099. hirda->State= HAL_IRDA_STATE_READY;
  1100. /* Process Unlocked */
  1101. __HAL_UNLOCK(hirda);
  1102. return HAL_TIMEOUT;
  1103. }
  1104. }
  1105. }
  1106. }
  1107. return HAL_OK;
  1108. }
  1109. /**
  1110. * @brief Send an amount of data in non blocking mode.
  1111. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1112. * the configuration information for the specified IRDA module.
  1113. * @retval HAL status
  1114. */
  1115. static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
  1116. {
  1117. uint16_t* tmp;
  1118. uint32_t tmp1 = 0;
  1119. tmp1 = hirda->State;
  1120. if((tmp1 == HAL_IRDA_STATE_BUSY_TX) || (tmp1 == HAL_IRDA_STATE_BUSY_TX_RX))
  1121. {
  1122. if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  1123. {
  1124. tmp = (uint16_t*) hirda->pTxBuffPtr;
  1125. hirda->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
  1126. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  1127. {
  1128. hirda->pTxBuffPtr += 2;
  1129. }
  1130. else
  1131. {
  1132. hirda->pTxBuffPtr += 1;
  1133. }
  1134. }
  1135. else
  1136. {
  1137. hirda->Instance->DR = (uint8_t)(*hirda->pTxBuffPtr++ & (uint8_t)0x00FF);
  1138. }
  1139. if(--hirda->TxXferCount == 0)
  1140. {
  1141. /* Disable the IRDA Transmit Data Register Empty Interrupt */
  1142. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
  1143. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  1144. {
  1145. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  1146. }
  1147. else
  1148. {
  1149. /* Disable the IRDA Parity Error Interrupt */
  1150. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  1151. /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  1152. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1153. hirda->State = HAL_IRDA_STATE_READY;
  1154. }
  1155. /* Wait on TC flag to be able to start a second transfer */
  1156. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, IRDA_TIMEOUT_VALUE) != HAL_OK)
  1157. {
  1158. return HAL_TIMEOUT;
  1159. }
  1160. HAL_IRDA_TxCpltCallback(hirda);
  1161. return HAL_OK;
  1162. }
  1163. return HAL_OK;
  1164. }
  1165. else
  1166. {
  1167. return HAL_BUSY;
  1168. }
  1169. }
  1170. /**
  1171. * @brief Receives an amount of data in non blocking mode.
  1172. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1173. * the configuration information for the specified IRDA module.
  1174. * @retval HAL status
  1175. */
  1176. static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
  1177. {
  1178. uint16_t* tmp;
  1179. uint32_t tmp1 = 0;
  1180. tmp1 = hirda->State;
  1181. if((tmp1 == HAL_IRDA_STATE_BUSY_RX) || (tmp1 == HAL_IRDA_STATE_BUSY_TX_RX))
  1182. {
  1183. if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  1184. {
  1185. tmp = (uint16_t*) hirda->pRxBuffPtr;
  1186. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  1187. {
  1188. *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x01FF);
  1189. hirda->pRxBuffPtr += 2;
  1190. }
  1191. else
  1192. {
  1193. *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x00FF);
  1194. hirda->pRxBuffPtr += 1;
  1195. }
  1196. }
  1197. else
  1198. {
  1199. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  1200. {
  1201. *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x00FF);
  1202. }
  1203. else
  1204. {
  1205. *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x007F);
  1206. }
  1207. }
  1208. if(--hirda->RxXferCount == 0)
  1209. {
  1210. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
  1211. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  1212. {
  1213. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  1214. }
  1215. else
  1216. {
  1217. /* Disable the IRDA Parity Error Interrupt */
  1218. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  1219. /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  1220. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1221. hirda->State = HAL_IRDA_STATE_READY;
  1222. }
  1223. HAL_IRDA_RxCpltCallback(hirda);
  1224. return HAL_OK;
  1225. }
  1226. return HAL_OK;
  1227. }
  1228. else
  1229. {
  1230. return HAL_BUSY;
  1231. }
  1232. }
  1233. /**
  1234. * @brief Configures the IRDA peripheral.
  1235. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1236. * the configuration information for the specified IRDA module.
  1237. * @retval None
  1238. */
  1239. static void IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
  1240. {
  1241. uint32_t tmpreg = 0x00;
  1242. /* Check the parameters */
  1243. assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  1244. assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));
  1245. assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
  1246. assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
  1247. assert_param(IS_IRDA_MODE(hirda->Init.Mode));
  1248. /*-------------------------- IRDA CR2 Configuration ------------------------*/
  1249. /* Clear STOP[13:12] bits */
  1250. hirda->Instance->CR2 &= (uint32_t)~((uint32_t)USART_CR2_STOP);
  1251. /*-------------------------- USART CR1 Configuration -----------------------*/
  1252. tmpreg = hirda->Instance->CR1;
  1253. /* Clear M, PCE, PS, TE and RE bits */
  1254. tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
  1255. USART_CR1_RE));
  1256. /* Configure the USART Word Length, Parity and mode:
  1257. Set the M bits according to hirda->Init.WordLength value
  1258. Set PCE and PS bits according to hirda->Init.Parity value
  1259. Set TE and RE bits according to hirda->Init.Mode value */
  1260. tmpreg |= (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode;
  1261. /* Write to USART CR1 */
  1262. hirda->Instance->CR1 = (uint32_t)tmpreg;
  1263. /*-------------------------- USART CR3 Configuration -----------------------*/
  1264. /* Clear CTSE and RTSE bits */
  1265. hirda->Instance->CR3 &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
  1266. /*-------------------------- USART BRR Configuration -----------------------*/
  1267. if((hirda->Instance == USART1) || (hirda->Instance == USART6))
  1268. {
  1269. hirda->Instance->BRR = __IRDA_BRR(HAL_RCC_GetPCLK2Freq(), hirda->Init.BaudRate);
  1270. }
  1271. else
  1272. {
  1273. hirda->Instance->BRR = __IRDA_BRR(HAL_RCC_GetPCLK1Freq(), hirda->Init.BaudRate);
  1274. }
  1275. }
  1276. /**
  1277. * @}
  1278. */
  1279. /**
  1280. * @}
  1281. */
  1282. #endif /* HAL_IRDA_MODULE_ENABLED */
  1283. /**
  1284. * @}
  1285. */
  1286. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/