stm32f1xx_hal_irda.c 72 KB

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