stm32f4xx_hal_irda.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_irda.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-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. /* Prevent unused argument(s) compilation warning */
  269. UNUSED(hirda);
  270. /* NOTE : This function Should not be modified, when the callback is needed,
  271. the HAL_IRDA_MspInit could be implemented in the user file
  272. */
  273. }
  274. /**
  275. * @brief IRDA MSP DeInit.
  276. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  277. * the configuration information for the specified IRDA module.
  278. * @retval None
  279. */
  280. __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
  281. {
  282. /* Prevent unused argument(s) compilation warning */
  283. UNUSED(hirda);
  284. /* NOTE : This function Should not be modified, when the callback is needed,
  285. the HAL_IRDA_MspDeInit could be implemented in the user file
  286. */
  287. }
  288. /**
  289. * @}
  290. */
  291. /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
  292. * @brief IRDA Transmit/Receive functions
  293. *
  294. @verbatim
  295. ===============================================================================
  296. ##### IO operation functions #####
  297. ===============================================================================
  298. This subsection provides a set of functions allowing to manage the IRDA data transfers.
  299. [..]
  300. IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
  301. on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
  302. is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
  303. While receiving data, transmission should be avoided as the data to be transmitted
  304. could be corrupted.
  305. (#) There are two modes of transfer:
  306. (++) Blocking mode: The communication is performed in polling mode.
  307. The HAL status of all data processing is returned by the same function
  308. after finishing transfer.
  309. (++) No-Blocking mode: The communication is performed using Interrupts
  310. or DMA, These APIs return the HAL status.
  311. The end of the data processing will be indicated through the
  312. dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
  313. using DMA mode.
  314. The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
  315. will be executed respectively at the end of the transmit or Receive process
  316. The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
  317. (#) Blocking mode API's are :
  318. (++) HAL_IRDA_Transmit()
  319. (++) HAL_IRDA_Receive()
  320. (#) Non Blocking mode APIs with Interrupt are :
  321. (++) HAL_IRDA_Transmit_IT()
  322. (++) HAL_IRDA_Receive_IT()
  323. (++) HAL_IRDA_IRQHandler()
  324. (#) Non Blocking mode functions with DMA are :
  325. (++) HAL_IRDA_Transmit_DMA()
  326. (++) HAL_IRDA_Receive_DMA()
  327. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  328. (++) HAL_IRDA_TxCpltCallback()
  329. (++) HAL_IRDA_RxCpltCallback()
  330. (++) HAL_IRDA_ErrorCallback()
  331. @endverbatim
  332. * @{
  333. */
  334. /**
  335. * @brief Sends an amount of data in blocking mode.
  336. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  337. * the configuration information for the specified IRDA module.
  338. * @param pData: Pointer to data buffer
  339. * @param Size: Amount of data to be sent
  340. * @param Timeout: Specify timeout value
  341. * @retval HAL status
  342. */
  343. HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  344. {
  345. uint16_t* tmp;
  346. uint32_t tmp1 = 0;
  347. tmp1 = hirda->State;
  348. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_RX))
  349. {
  350. if((pData == NULL) || (Size == 0))
  351. {
  352. return HAL_ERROR;
  353. }
  354. /* Process Locked */
  355. __HAL_LOCK(hirda);
  356. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  357. if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  358. {
  359. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  360. }
  361. else
  362. {
  363. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  364. }
  365. hirda->TxXferSize = Size;
  366. hirda->TxXferCount = Size;
  367. while(hirda->TxXferCount > 0)
  368. {
  369. hirda->TxXferCount--;
  370. if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  371. {
  372. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
  373. {
  374. return HAL_TIMEOUT;
  375. }
  376. tmp = (uint16_t*) pData;
  377. hirda->Instance->DR = (*tmp & (uint16_t)0x01FF);
  378. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  379. {
  380. pData +=2;
  381. }
  382. else
  383. {
  384. pData +=1;
  385. }
  386. }
  387. else
  388. {
  389. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
  390. {
  391. return HAL_TIMEOUT;
  392. }
  393. hirda->Instance->DR = (*pData++ & (uint8_t)0xFF);
  394. }
  395. }
  396. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, Timeout) != HAL_OK)
  397. {
  398. return HAL_TIMEOUT;
  399. }
  400. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  401. {
  402. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  403. }
  404. else
  405. {
  406. hirda->State = HAL_IRDA_STATE_READY;
  407. }
  408. /* Process Unlocked */
  409. __HAL_UNLOCK(hirda);
  410. return HAL_OK;
  411. }
  412. else
  413. {
  414. return HAL_BUSY;
  415. }
  416. }
  417. /**
  418. * @brief Receive an amount of data in blocking mode.
  419. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  420. * the configuration information for the specified IRDA module.
  421. * @param pData: Pointer to data buffer
  422. * @param Size: Amount of data to be received
  423. * @param Timeout: Specify timeout value
  424. * @retval HAL status
  425. */
  426. HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  427. {
  428. uint16_t* tmp;
  429. uint32_t tmp1 = 0;
  430. tmp1 = hirda->State;
  431. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_TX))
  432. {
  433. if((pData == NULL) || (Size == 0))
  434. {
  435. return HAL_ERROR;
  436. }
  437. /* Process Locked */
  438. __HAL_LOCK(hirda);
  439. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  440. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  441. {
  442. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  443. }
  444. else
  445. {
  446. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  447. }
  448. hirda->RxXferSize = Size;
  449. hirda->RxXferCount = Size;
  450. /* Check the remain data to be received */
  451. while(hirda->RxXferCount > 0)
  452. {
  453. hirda->RxXferCount--;
  454. if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  455. {
  456. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  457. {
  458. return HAL_TIMEOUT;
  459. }
  460. tmp = (uint16_t*) pData ;
  461. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  462. {
  463. *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x01FF);
  464. pData +=2;
  465. }
  466. else
  467. {
  468. *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x00FF);
  469. pData +=1;
  470. }
  471. }
  472. else
  473. {
  474. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  475. {
  476. return HAL_TIMEOUT;
  477. }
  478. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  479. {
  480. *pData++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x00FF);
  481. }
  482. else
  483. {
  484. *pData++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x007F);
  485. }
  486. }
  487. }
  488. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  489. {
  490. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  491. }
  492. else
  493. {
  494. hirda->State = HAL_IRDA_STATE_READY;
  495. }
  496. /* Process Unlocked */
  497. __HAL_UNLOCK(hirda);
  498. return HAL_OK;
  499. }
  500. else
  501. {
  502. return HAL_BUSY;
  503. }
  504. }
  505. /**
  506. * @brief Send an amount of data in non blocking mode.
  507. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  508. * the configuration information for the specified IRDA module.
  509. * @param pData: Pointer to data buffer
  510. * @param Size: Amount of data to be sent
  511. * @retval HAL status
  512. */
  513. HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  514. {
  515. uint32_t tmp1 = 0;
  516. tmp1 = hirda->State;
  517. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_RX))
  518. {
  519. if((pData == NULL) || (Size == 0))
  520. {
  521. return HAL_ERROR;
  522. }
  523. /* Process Locked */
  524. __HAL_LOCK(hirda);
  525. hirda->pTxBuffPtr = pData;
  526. hirda->TxXferSize = Size;
  527. hirda->TxXferCount = Size;
  528. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  529. if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  530. {
  531. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  532. }
  533. else
  534. {
  535. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  536. }
  537. /* Process Unlocked */
  538. __HAL_UNLOCK(hirda);
  539. /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  540. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
  541. /* Enable the IRDA Transmit Data Register Empty Interrupt */
  542. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TXE);
  543. return HAL_OK;
  544. }
  545. else
  546. {
  547. return HAL_BUSY;
  548. }
  549. }
  550. /**
  551. * @brief Receives an amount of data in non blocking mode.
  552. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  553. * the configuration information for the specified IRDA module.
  554. * @param pData: Pointer to data buffer
  555. * @param Size: Amount of data to be received
  556. * @retval HAL status
  557. */
  558. HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  559. {
  560. uint32_t tmp1 = 0;
  561. tmp1 = hirda->State;
  562. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_TX))
  563. {
  564. if((pData == NULL) || (Size == 0))
  565. {
  566. return HAL_ERROR;
  567. }
  568. /* Process Locked */
  569. __HAL_LOCK(hirda);
  570. hirda->pRxBuffPtr = pData;
  571. hirda->RxXferSize = Size;
  572. hirda->RxXferCount = Size;
  573. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  574. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  575. {
  576. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  577. }
  578. else
  579. {
  580. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  581. }
  582. /* Process Unlocked */
  583. __HAL_UNLOCK(hirda);
  584. /* Enable the IRDA Data Register not empty Interrupt */
  585. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_RXNE);
  586. /* Enable the IRDA Parity Error Interrupt */
  587. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_PE);
  588. /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  589. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
  590. return HAL_OK;
  591. }
  592. else
  593. {
  594. return HAL_BUSY;
  595. }
  596. }
  597. /**
  598. * @brief Sends an amount of data in non blocking mode.
  599. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  600. * the configuration information for the specified IRDA module.
  601. * @param pData: Pointer to data buffer
  602. * @param Size: Amount of data to be sent
  603. * @retval HAL status
  604. */
  605. HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  606. {
  607. uint32_t *tmp;
  608. uint32_t tmp1 = 0;
  609. tmp1 = hirda->State;
  610. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_RX))
  611. {
  612. if((pData == NULL) || (Size == 0))
  613. {
  614. return HAL_ERROR;
  615. }
  616. /* Process Locked */
  617. __HAL_LOCK(hirda);
  618. hirda->pTxBuffPtr = pData;
  619. hirda->TxXferSize = Size;
  620. hirda->TxXferCount = Size;
  621. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  622. if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  623. {
  624. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  625. }
  626. else
  627. {
  628. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  629. }
  630. /* Set the IRDA DMA transfer complete callback */
  631. hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
  632. /* Set the IRDA DMA half transfer complete callback */
  633. hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;
  634. /* Set the DMA error callback */
  635. hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
  636. /* Enable the IRDA transmit DMA Stream */
  637. tmp = (uint32_t*)&pData;
  638. HAL_DMA_Start_IT(hirda->hdmatx, *(uint32_t*)tmp, (uint32_t)&hirda->Instance->DR, Size);
  639. /* Clear the TC flag in the SR register by writing 0 to it */
  640. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_FLAG_TC);
  641. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  642. in the USART CR3 register */
  643. hirda->Instance->CR3 |= USART_CR3_DMAT;
  644. /* Process Unlocked */
  645. __HAL_UNLOCK(hirda);
  646. return HAL_OK;
  647. }
  648. else
  649. {
  650. return HAL_BUSY;
  651. }
  652. }
  653. /**
  654. * @brief Receives an amount of data in non blocking mode.
  655. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  656. * the configuration information for the specified IRDA module.
  657. * @param pData: Pointer to data buffer
  658. * @param Size: Amount of data to be received
  659. * @note When the IRDA parity is enabled (PCE = 1) the data received contain the parity bit.
  660. * @retval HAL status
  661. */
  662. HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  663. {
  664. uint32_t *tmp;
  665. uint32_t tmp1 = 0;
  666. tmp1 = hirda->State;
  667. if((tmp1 == HAL_IRDA_STATE_READY) || (tmp1 == HAL_IRDA_STATE_BUSY_TX))
  668. {
  669. if((pData == NULL) || (Size == 0))
  670. {
  671. return HAL_ERROR;
  672. }
  673. /* Process Locked */
  674. __HAL_LOCK(hirda);
  675. hirda->pRxBuffPtr = pData;
  676. hirda->RxXferSize = Size;
  677. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  678. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  679. {
  680. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  681. }
  682. else
  683. {
  684. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  685. }
  686. /* Set the IRDA DMA transfer complete callback */
  687. hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
  688. /* Set the IRDA DMA half transfer complete callback */
  689. hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;
  690. /* Set the DMA error callback */
  691. hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
  692. /* Enable the DMA Stream */
  693. tmp = (uint32_t*)&pData;
  694. HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->DR, *(uint32_t*)tmp, Size);
  695. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  696. in the USART CR3 register */
  697. hirda->Instance->CR3 |= USART_CR3_DMAR;
  698. /* Process Unlocked */
  699. __HAL_UNLOCK(hirda);
  700. return HAL_OK;
  701. }
  702. else
  703. {
  704. return HAL_BUSY;
  705. }
  706. }
  707. /**
  708. * @brief Pauses the DMA Transfer.
  709. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  710. * the configuration information for the specified IRDA module.
  711. * @retval HAL status
  712. */
  713. HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda)
  714. {
  715. /* Process Locked */
  716. __HAL_LOCK(hirda);
  717. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  718. {
  719. /* Disable the UART DMA Tx request */
  720. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  721. }
  722. else if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  723. {
  724. /* Disable the UART DMA Rx request */
  725. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  726. }
  727. else if (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  728. {
  729. /* Disable the UART DMA Tx & Rx requests */
  730. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  731. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  732. }
  733. else
  734. {
  735. /* Process Unlocked */
  736. __HAL_UNLOCK(hirda);
  737. return HAL_ERROR;
  738. }
  739. /* Process Unlocked */
  740. __HAL_UNLOCK(hirda);
  741. return HAL_OK;
  742. }
  743. /**
  744. * @brief Resumes the DMA Transfer.
  745. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  746. * the configuration information for the specified UART module.
  747. * @retval HAL status
  748. */
  749. HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda)
  750. {
  751. /* Process Locked */
  752. __HAL_LOCK(hirda);
  753. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  754. {
  755. /* Enable the UART DMA Tx request */
  756. hirda->Instance->CR3 |= USART_CR3_DMAT;
  757. }
  758. else if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  759. {
  760. /* Clear the Overrun flag before resuming the Rx transfer */
  761. __HAL_IRDA_CLEAR_OREFLAG(hirda);
  762. /* Enable the UART DMA Rx request */
  763. hirda->Instance->CR3 |= USART_CR3_DMAR;
  764. }
  765. else if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  766. {
  767. /* Clear the Overrun flag before resuming the Rx transfer */
  768. __HAL_IRDA_CLEAR_OREFLAG(hirda);
  769. /* Enable the UART DMA Tx & Rx request */
  770. hirda->Instance->CR3 |= USART_CR3_DMAT;
  771. hirda->Instance->CR3 |= USART_CR3_DMAR;
  772. }
  773. else
  774. {
  775. /* Process Unlocked */
  776. __HAL_UNLOCK(hirda);
  777. return HAL_ERROR;
  778. }
  779. /* Process Unlocked */
  780. __HAL_UNLOCK(hirda);
  781. return HAL_OK;
  782. }
  783. /**
  784. * @brief Stops the DMA Transfer.
  785. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  786. * the configuration information for the specified UART module.
  787. * @retval HAL status
  788. */
  789. HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda)
  790. {
  791. /* The Lock is not implemented on this API to allow the user application
  792. to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
  793. when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  794. and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
  795. */
  796. /* Disable the UART Tx/Rx DMA requests */
  797. hirda->Instance->CR3 &= ~USART_CR3_DMAT;
  798. hirda->Instance->CR3 &= ~USART_CR3_DMAR;
  799. /* Abort the UART DMA tx Stream */
  800. if(hirda->hdmatx != NULL)
  801. {
  802. HAL_DMA_Abort(hirda->hdmatx);
  803. }
  804. /* Abort the UART DMA rx Stream */
  805. if(hirda->hdmarx != NULL)
  806. {
  807. HAL_DMA_Abort(hirda->hdmarx);
  808. }
  809. hirda->State = HAL_IRDA_STATE_READY;
  810. return HAL_OK;
  811. }
  812. /**
  813. * @brief This function handles IRDA interrupt request.
  814. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  815. * the configuration information for the specified IRDA module.
  816. * @retval None
  817. */
  818. void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
  819. {
  820. uint32_t tmp1 = 0, tmp2 =0;
  821. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_PE);
  822. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_PE);
  823. /* IRDA parity error interrupt occurred -------------------------------------*/
  824. if((tmp1 != RESET) && (tmp2 != RESET))
  825. {
  826. __HAL_IRDA_CLEAR_PEFLAG(hirda);
  827. hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
  828. }
  829. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_FE);
  830. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR);
  831. /* IRDA frame error interrupt occurred --------------------------------------*/
  832. if((tmp1 != RESET) && (tmp2 != RESET))
  833. {
  834. __HAL_IRDA_CLEAR_FEFLAG(hirda);
  835. hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
  836. }
  837. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_NE);
  838. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR);
  839. /* IRDA noise error interrupt occurred --------------------------------------*/
  840. if((tmp1 != RESET) && (tmp2 != RESET))
  841. {
  842. __HAL_IRDA_CLEAR_NEFLAG(hirda);
  843. hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
  844. }
  845. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_ORE);
  846. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR);
  847. /* IRDA Over-Run interrupt occurred -----------------------------------------*/
  848. if((tmp1 != RESET) && (tmp2 != RESET))
  849. {
  850. __HAL_IRDA_CLEAR_OREFLAG(hirda);
  851. hirda->ErrorCode |= HAL_IRDA_ERROR_ORE;
  852. }
  853. /* Call the Error call Back in case of Errors */
  854. if(hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
  855. {
  856. /* Set the IRDA state ready to be able to start again the process */
  857. hirda->State = HAL_IRDA_STATE_READY;
  858. HAL_IRDA_ErrorCallback(hirda);
  859. }
  860. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_RXNE);
  861. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_RXNE);
  862. /* IRDA in mode Receiver ---------------------------------------------------*/
  863. if((tmp1 != RESET) && (tmp2 != RESET))
  864. {
  865. IRDA_Receive_IT(hirda);
  866. }
  867. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_TXE);
  868. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TXE);
  869. /* IRDA in mode Transmitter ------------------------------------------------*/
  870. if((tmp1 != RESET) &&(tmp2 != RESET))
  871. {
  872. IRDA_Transmit_IT(hirda);
  873. }
  874. tmp1 = __HAL_IRDA_GET_FLAG(hirda, IRDA_FLAG_TC);
  875. tmp2 = __HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TC);
  876. /* IRDA in mode Transmitter (transmission end) -----------------------------*/
  877. if((tmp1 != RESET) && (tmp2 != RESET))
  878. {
  879. IRDA_EndTransmit_IT(hirda);
  880. }
  881. }
  882. /**
  883. * @brief Tx Transfer complete callbacks.
  884. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  885. * the configuration information for the specified IRDA module.
  886. * @retval None
  887. */
  888. __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
  889. {
  890. /* Prevent unused argument(s) compilation warning */
  891. UNUSED(hirda);
  892. /* NOTE : This function Should not be modified, when the callback is needed,
  893. the HAL_IRDA_TxCpltCallback could be implemented in the user file
  894. */
  895. }
  896. /**
  897. * @brief Tx Half Transfer completed callbacks.
  898. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  899. * the configuration information for the specified USART module.
  900. * @retval None
  901. */
  902. __weak void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  903. {
  904. /* Prevent unused argument(s) compilation warning */
  905. UNUSED(hirda);
  906. /* NOTE: This function Should not be modified, when the callback is needed,
  907. the HAL_IRDA_TxHalfCpltCallback could be implemented in the user file
  908. */
  909. }
  910. /**
  911. * @brief Rx Transfer complete callbacks.
  912. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  913. * the configuration information for the specified IRDA module.
  914. * @retval None
  915. */
  916. __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
  917. {
  918. /* Prevent unused argument(s) compilation warning */
  919. UNUSED(hirda);
  920. /* NOTE : This function Should not be modified, when the callback is needed,
  921. the HAL_IRDA_RxCpltCallback could be implemented in the user file
  922. */
  923. }
  924. /**
  925. * @brief Rx Half Transfer complete callbacks.
  926. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  927. * the configuration information for the specified IRDA module.
  928. * @retval None
  929. */
  930. __weak void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  931. {
  932. /* Prevent unused argument(s) compilation warning */
  933. UNUSED(hirda);
  934. /* NOTE : This function Should not be modified, when the callback is needed,
  935. the HAL_IRDA_RxHalfCpltCallback could be implemented in the user file
  936. */
  937. }
  938. /**
  939. * @brief IRDA error callbacks.
  940. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  941. * the configuration information for the specified IRDA module.
  942. * @retval None
  943. */
  944. __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
  945. {
  946. /* Prevent unused argument(s) compilation warning */
  947. UNUSED(hirda);
  948. /* NOTE : This function Should not be modified, when the callback is needed,
  949. the HAL_IRDA_ErrorCallback could be implemented in the user file
  950. */
  951. }
  952. /**
  953. * @}
  954. */
  955. /** @defgroup IRDA_Exported_Functions_Group3 Peripheral State and Errors functions
  956. * @brief IRDA State and Errors functions
  957. *
  958. @verbatim
  959. ==============================================================================
  960. ##### Peripheral State and Errors functions #####
  961. ==============================================================================
  962. [..]
  963. This subsection provides a set of functions allowing to return the State of IrDA
  964. communication process and also return Peripheral Errors occurred during communication process
  965. (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state of the IrDA peripheral.
  966. (+) HAL_IRDA_GetError() check in run-time errors that could be occurred during communication.
  967. @endverbatim
  968. * @{
  969. */
  970. /**
  971. * @brief Returns the IRDA state.
  972. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  973. * the configuration information for the specified IRDA module.
  974. * @retval HAL state
  975. */
  976. HAL_IRDA_StateTypeDef HAL_IRDA_GetState(IRDA_HandleTypeDef *hirda)
  977. {
  978. return hirda->State;
  979. }
  980. /**
  981. * @brief Return the IARDA error code
  982. * @param hirda : pointer to a IRDA_HandleTypeDef structure that contains
  983. * the configuration information for the specified IRDA.
  984. * @retval IRDA Error Code
  985. */
  986. uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef *hirda)
  987. {
  988. return hirda->ErrorCode;
  989. }
  990. /**
  991. * @}
  992. */
  993. /**
  994. * @brief DMA IRDA transmit process complete callback.
  995. * @param hdma : DMA handle
  996. * @retval None
  997. */
  998. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  999. {
  1000. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1001. /* DMA Normal mode */
  1002. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1003. {
  1004. hirda->TxXferCount = 0;
  1005. /* Disable the DMA transfer for transmit request by setting the DMAT bit
  1006. in the IRDA CR3 register */
  1007. hirda->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAT);
  1008. /* Enable the IRDA Transmit Complete Interrupt */
  1009. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TC);
  1010. }
  1011. /* DMA Circular mode */
  1012. else
  1013. {
  1014. HAL_IRDA_TxCpltCallback(hirda);
  1015. }
  1016. }
  1017. /**
  1018. * @brief DMA IRDA receive process half complete callback
  1019. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1020. * the configuration information for the specified DMA module.
  1021. * @retval None
  1022. */
  1023. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma)
  1024. {
  1025. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1026. HAL_IRDA_TxHalfCpltCallback(hirda);
  1027. }
  1028. /**
  1029. * @brief DMA IRDA receive process complete callback.
  1030. * @param hdma: DMA handle
  1031. * @retval None
  1032. */
  1033. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1034. {
  1035. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1036. /* DMA Normal mode */
  1037. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1038. {
  1039. hirda->RxXferCount = 0;
  1040. /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1041. in the IRDA CR3 register */
  1042. hirda->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAR);
  1043. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  1044. {
  1045. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  1046. }
  1047. else
  1048. {
  1049. hirda->State = HAL_IRDA_STATE_READY;
  1050. }
  1051. }
  1052. HAL_IRDA_RxCpltCallback(hirda);
  1053. }
  1054. /**
  1055. * @brief DMA IRDA receive process half complete callback
  1056. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1057. * the configuration information for the specified DMA module.
  1058. * @retval None
  1059. */
  1060. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma)
  1061. {
  1062. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1063. HAL_IRDA_RxHalfCpltCallback(hirda);
  1064. }
  1065. /**
  1066. * @brief DMA IRDA communication error callback.
  1067. * @param hdma: DMA handle
  1068. * @retval None
  1069. */
  1070. static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
  1071. {
  1072. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1073. hirda->RxXferCount = 0;
  1074. hirda->TxXferCount = 0;
  1075. hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
  1076. hirda->State= HAL_IRDA_STATE_READY;
  1077. HAL_IRDA_ErrorCallback(hirda);
  1078. }
  1079. /**
  1080. * @brief This function handles IRDA Communication Timeout.
  1081. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1082. * the configuration information for the specified IRDA module.
  1083. * @param Flag: specifies the IRDA flag to check.
  1084. * @param Status: The new Flag status (SET or RESET).
  1085. * @param Timeout: Timeout duration
  1086. * @retval HAL status
  1087. */
  1088. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1089. {
  1090. uint32_t tickstart = 0;
  1091. /* Get tick */
  1092. tickstart = HAL_GetTick();
  1093. /* Wait until flag is set */
  1094. if(Status == RESET)
  1095. {
  1096. while(__HAL_IRDA_GET_FLAG(hirda, Flag) == RESET)
  1097. {
  1098. /* Check for the Timeout */
  1099. if(Timeout != HAL_MAX_DELAY)
  1100. {
  1101. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1102. {
  1103. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1104. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
  1105. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
  1106. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  1107. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1108. hirda->State= HAL_IRDA_STATE_READY;
  1109. /* Process Unlocked */
  1110. __HAL_UNLOCK(hirda);
  1111. return HAL_TIMEOUT;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. else
  1117. {
  1118. while(__HAL_IRDA_GET_FLAG(hirda, Flag) != RESET)
  1119. {
  1120. /* Check for the Timeout */
  1121. if(Timeout != HAL_MAX_DELAY)
  1122. {
  1123. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1124. {
  1125. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1126. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
  1127. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
  1128. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  1129. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1130. hirda->State= HAL_IRDA_STATE_READY;
  1131. /* Process Unlocked */
  1132. __HAL_UNLOCK(hirda);
  1133. return HAL_TIMEOUT;
  1134. }
  1135. }
  1136. }
  1137. }
  1138. return HAL_OK;
  1139. }
  1140. /**
  1141. * @brief Send an amount of data in non blocking mode.
  1142. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1143. * the configuration information for the specified IRDA module.
  1144. * @retval HAL status
  1145. */
  1146. static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
  1147. {
  1148. uint16_t* tmp;
  1149. uint32_t tmp1 = 0;
  1150. tmp1 = hirda->State;
  1151. if((tmp1 == HAL_IRDA_STATE_BUSY_TX) || (tmp1 == HAL_IRDA_STATE_BUSY_TX_RX))
  1152. {
  1153. if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  1154. {
  1155. tmp = (uint16_t*) hirda->pTxBuffPtr;
  1156. hirda->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
  1157. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  1158. {
  1159. hirda->pTxBuffPtr += 2;
  1160. }
  1161. else
  1162. {
  1163. hirda->pTxBuffPtr += 1;
  1164. }
  1165. }
  1166. else
  1167. {
  1168. hirda->Instance->DR = (uint8_t)(*hirda->pTxBuffPtr++ & (uint8_t)0x00FF);
  1169. }
  1170. if(--hirda->TxXferCount == 0)
  1171. {
  1172. /* Disable the IRDA Transmit Data Register Empty Interrupt */
  1173. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
  1174. /* Enable the IRDA Transmit Complete Interrupt */
  1175. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TC);
  1176. }
  1177. return HAL_OK;
  1178. }
  1179. else
  1180. {
  1181. return HAL_BUSY;
  1182. }
  1183. }
  1184. /**
  1185. * @brief Wraps up transmission in non blocking mode.
  1186. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1187. * the configuration information for the specified IRDA module.
  1188. * @retval HAL status
  1189. */
  1190. static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda)
  1191. {
  1192. /* Disable the IRDA Transmit Complete Interrupt */
  1193. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TC);
  1194. /* Check if a receive process is ongoing or not */
  1195. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  1196. {
  1197. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  1198. }
  1199. else
  1200. {
  1201. /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  1202. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1203. hirda->State = HAL_IRDA_STATE_READY;
  1204. }
  1205. HAL_IRDA_TxCpltCallback(hirda);
  1206. return HAL_OK;
  1207. }
  1208. /**
  1209. * @brief Receives an amount of data in non blocking mode.
  1210. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1211. * the configuration information for the specified IRDA module.
  1212. * @retval HAL status
  1213. */
  1214. static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
  1215. {
  1216. uint16_t* tmp;
  1217. uint32_t tmp1 = 0;
  1218. tmp1 = hirda->State;
  1219. if((tmp1 == HAL_IRDA_STATE_BUSY_RX) || (tmp1 == HAL_IRDA_STATE_BUSY_TX_RX))
  1220. {
  1221. if(hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  1222. {
  1223. tmp = (uint16_t*) hirda->pRxBuffPtr;
  1224. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  1225. {
  1226. *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x01FF);
  1227. hirda->pRxBuffPtr += 2;
  1228. }
  1229. else
  1230. {
  1231. *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x00FF);
  1232. hirda->pRxBuffPtr += 1;
  1233. }
  1234. }
  1235. else
  1236. {
  1237. if(hirda->Init.Parity == IRDA_PARITY_NONE)
  1238. {
  1239. *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x00FF);
  1240. }
  1241. else
  1242. {
  1243. *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x007F);
  1244. }
  1245. }
  1246. if(--hirda->RxXferCount == 0)
  1247. {
  1248. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
  1249. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  1250. {
  1251. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  1252. }
  1253. else
  1254. {
  1255. /* Disable the IRDA Parity Error Interrupt */
  1256. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  1257. /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  1258. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1259. hirda->State = HAL_IRDA_STATE_READY;
  1260. }
  1261. HAL_IRDA_RxCpltCallback(hirda);
  1262. return HAL_OK;
  1263. }
  1264. return HAL_OK;
  1265. }
  1266. else
  1267. {
  1268. return HAL_BUSY;
  1269. }
  1270. }
  1271. /**
  1272. * @brief Configures the IRDA peripheral.
  1273. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1274. * the configuration information for the specified IRDA module.
  1275. * @retval None
  1276. */
  1277. static void IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
  1278. {
  1279. uint32_t tmpreg = 0x00;
  1280. /* Check the parameters */
  1281. assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  1282. assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));
  1283. assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
  1284. assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
  1285. assert_param(IS_IRDA_MODE(hirda->Init.Mode));
  1286. /*-------------------------- IRDA CR2 Configuration ------------------------*/
  1287. /* Clear STOP[13:12] bits */
  1288. hirda->Instance->CR2 &= (uint32_t)~((uint32_t)USART_CR2_STOP);
  1289. /*-------------------------- USART CR1 Configuration -----------------------*/
  1290. tmpreg = hirda->Instance->CR1;
  1291. /* Clear M, PCE, PS, TE and RE bits */
  1292. tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
  1293. USART_CR1_RE));
  1294. /* Configure the USART Word Length, Parity and mode:
  1295. Set the M bits according to hirda->Init.WordLength value
  1296. Set PCE and PS bits according to hirda->Init.Parity value
  1297. Set TE and RE bits according to hirda->Init.Mode value */
  1298. tmpreg |= (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode;
  1299. /* Write to USART CR1 */
  1300. hirda->Instance->CR1 = (uint32_t)tmpreg;
  1301. /*-------------------------- USART CR3 Configuration -----------------------*/
  1302. /* Clear CTSE and RTSE bits */
  1303. hirda->Instance->CR3 &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
  1304. /*-------------------------- USART BRR Configuration -----------------------*/
  1305. if((hirda->Instance == USART1) || (hirda->Instance == USART6))
  1306. {
  1307. hirda->Instance->BRR = IRDA_BRR(HAL_RCC_GetPCLK2Freq(), hirda->Init.BaudRate);
  1308. }
  1309. else
  1310. {
  1311. hirda->Instance->BRR = IRDA_BRR(HAL_RCC_GetPCLK1Freq(), hirda->Init.BaudRate);
  1312. }
  1313. }
  1314. /**
  1315. * @}
  1316. */
  1317. #endif /* HAL_IRDA_MODULE_ENABLED */
  1318. /**
  1319. * @}
  1320. */
  1321. /**
  1322. * @}
  1323. */
  1324. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/