stm32f4xx_hal_irda.c 47 KB

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