stm32f1xx_hal_irda.c 50 KB

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