stm32g0xx_hal_irda.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_hal_irda.c
  4. * @author MCD Application Team
  5. * @brief IRDA HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the IrDA (Infrared Data Association) Peripheral
  8. * (IRDA)
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral State and Errors functions
  12. * + Peripheral Control functions
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2018 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ==============================================================================
  27. ##### How to use this driver #####
  28. ==============================================================================
  29. [..]
  30. The IRDA HAL driver can be used as follows:
  31. (#) Declare a IRDA_HandleTypeDef handle structure (eg. IRDA_HandleTypeDef hirda).
  32. (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API
  33. in setting the associated USART or UART in IRDA mode:
  34. (++) Enable the USARTx/UARTx interface clock.
  35. (++) USARTx/UARTx pins configuration:
  36. (+++) Enable the clock for the USARTx/UARTx GPIOs.
  37. (+++) Configure these USARTx/UARTx pins (TX as alternate function pull-up, RX as alternate function Input).
  38. (++) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
  39. and HAL_IRDA_Receive_IT() APIs):
  40. (+++) Configure the USARTx/UARTx interrupt priority.
  41. (+++) Enable the NVIC USARTx/UARTx IRQ handle.
  42. (+++) The specific IRDA interrupts (Transmission complete interrupt,
  43. RXNE interrupt and Error Interrupts) will be managed using the macros
  44. __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
  45. (++) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
  46. and HAL_IRDA_Receive_DMA() APIs):
  47. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  48. (+++) Enable the DMAx interface clock.
  49. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  50. (+++) Configure the DMA Tx/Rx channel.
  51. (+++) Associate the initialized DMA handle to the IRDA DMA Tx/Rx handle.
  52. (+++) Configure the priority and enable the NVIC for the transfer
  53. complete interrupt on the DMA Tx/Rx channel.
  54. (#) Program the Baud Rate, Word Length and Parity and Mode(Receiver/Transmitter),
  55. the normal or low power mode and the clock prescaler in the hirda handle Init structure.
  56. (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
  57. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  58. by calling the customized HAL_IRDA_MspInit() API.
  59. -@@- The specific IRDA interrupts (Transmission complete interrupt,
  60. RXNE interrupt and Error Interrupts) will be managed using the macros
  61. __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
  62. (#) Three operation modes are available within this driver :
  63. *** Polling mode IO operation ***
  64. =================================
  65. [..]
  66. (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
  67. (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
  68. *** Interrupt mode IO operation ***
  69. ===================================
  70. [..]
  71. (+) Send an amount of data in non-blocking mode using HAL_IRDA_Transmit_IT()
  72. (+) At transmission end of transfer HAL_IRDA_TxCpltCallback() is executed and user can
  73. add his own code by customization of function pointer HAL_IRDA_TxCpltCallback()
  74. (+) Receive an amount of data in non-blocking mode using HAL_IRDA_Receive_IT()
  75. (+) At reception end of transfer HAL_IRDA_RxCpltCallback() is executed and user can
  76. add his own code by customization of function pointer HAL_IRDA_RxCpltCallback()
  77. (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
  78. add his own code by customization of function pointer HAL_IRDA_ErrorCallback()
  79. *** DMA mode IO operation ***
  80. ==============================
  81. [..]
  82. (+) Send an amount of data in non-blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
  83. (+) At transmission half of transfer HAL_IRDA_TxHalfCpltCallback() is executed and user can
  84. add his own code by customization of function pointer HAL_IRDA_TxHalfCpltCallback()
  85. (+) At transmission end of transfer HAL_IRDA_TxCpltCallback() is executed and user can
  86. add his own code by customization of function pointer HAL_IRDA_TxCpltCallback()
  87. (+) Receive an amount of data in non-blocking mode (DMA) using HAL_IRDA_Receive_DMA()
  88. (+) At reception half of transfer HAL_IRDA_RxHalfCpltCallback() is executed and user can
  89. add his own code by customization of function pointer HAL_IRDA_RxHalfCpltCallback()
  90. (+) At reception end of transfer HAL_IRDA_RxCpltCallback() is executed and user can
  91. add his own code by customization of function pointer HAL_IRDA_RxCpltCallback()
  92. (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
  93. add his own code by customization of function pointer HAL_IRDA_ErrorCallback()
  94. *** IRDA HAL driver macros list ***
  95. ====================================
  96. [..]
  97. Below the list of most used macros in IRDA HAL driver.
  98. (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
  99. (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
  100. (+) __HAL_IRDA_GET_FLAG : Check whether the specified IRDA flag is set or not
  101. (+) __HAL_IRDA_CLEAR_FLAG : Clear the specified IRDA pending flag
  102. (+) __HAL_IRDA_ENABLE_IT: Enable the specified IRDA interrupt
  103. (+) __HAL_IRDA_DISABLE_IT: Disable the specified IRDA interrupt
  104. (+) __HAL_IRDA_GET_IT_SOURCE: Check whether or not the specified IRDA interrupt is enabled
  105. [..]
  106. (@) You can refer to the IRDA HAL driver header file for more useful macros
  107. ##### Callback registration #####
  108. ==================================
  109. [..]
  110. The compilation define USE_HAL_IRDA_REGISTER_CALLBACKS when set to 1
  111. allows the user to configure dynamically the driver callbacks.
  112. [..]
  113. Use Function HAL_IRDA_RegisterCallback() to register a user callback.
  114. Function HAL_IRDA_RegisterCallback() allows to register following callbacks:
  115. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  116. (+) TxCpltCallback : Tx Complete Callback.
  117. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  118. (+) RxCpltCallback : Rx Complete Callback.
  119. (+) ErrorCallback : Error Callback.
  120. (+) AbortCpltCallback : Abort Complete Callback.
  121. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  122. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  123. (+) MspInitCallback : IRDA MspInit.
  124. (+) MspDeInitCallback : IRDA MspDeInit.
  125. This function takes as parameters the HAL peripheral handle, the Callback ID
  126. and a pointer to the user callback function.
  127. [..]
  128. Use function HAL_IRDA_UnRegisterCallback() to reset a callback to the default
  129. weak function.
  130. HAL_IRDA_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  131. and the Callback ID.
  132. This function allows to reset following callbacks:
  133. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  134. (+) TxCpltCallback : Tx Complete Callback.
  135. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  136. (+) RxCpltCallback : Rx Complete Callback.
  137. (+) ErrorCallback : Error Callback.
  138. (+) AbortCpltCallback : Abort Complete Callback.
  139. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  140. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  141. (+) MspInitCallback : IRDA MspInit.
  142. (+) MspDeInitCallback : IRDA MspDeInit.
  143. [..]
  144. By default, after the HAL_IRDA_Init() and when the state is HAL_IRDA_STATE_RESET
  145. all callbacks are set to the corresponding weak functions:
  146. examples HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxHalfCpltCallback().
  147. Exception done for MspInit and MspDeInit functions that are respectively
  148. reset to the legacy weak functions in the HAL_IRDA_Init()
  149. and HAL_IRDA_DeInit() only when these callbacks are null (not registered beforehand).
  150. If not, MspInit or MspDeInit are not null, the HAL_IRDA_Init() and HAL_IRDA_DeInit()
  151. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  152. [..]
  153. Callbacks can be registered/unregistered in HAL_IRDA_STATE_READY state only.
  154. Exception done MspInit/MspDeInit that can be registered/unregistered
  155. in HAL_IRDA_STATE_READY or HAL_IRDA_STATE_RESET state, thus registered (user)
  156. MspInit/DeInit callbacks can be used during the Init/DeInit.
  157. In that case first register the MspInit/MspDeInit user callbacks
  158. using HAL_IRDA_RegisterCallback() before calling HAL_IRDA_DeInit()
  159. or HAL_IRDA_Init() function.
  160. [..]
  161. When The compilation define USE_HAL_IRDA_REGISTER_CALLBACKS is set to 0 or
  162. not defined, the callback registration feature is not available
  163. and weak callbacks are used.
  164. @endverbatim
  165. ******************************************************************************
  166. */
  167. /* Includes ------------------------------------------------------------------*/
  168. #include "stm32g0xx_hal.h"
  169. /** @addtogroup STM32G0xx_HAL_Driver
  170. * @{
  171. */
  172. /** @defgroup IRDA IRDA
  173. * @brief HAL IRDA module driver
  174. * @{
  175. */
  176. #ifdef HAL_IRDA_MODULE_ENABLED
  177. /* Private typedef -----------------------------------------------------------*/
  178. /* Private define ------------------------------------------------------------*/
  179. /** @defgroup IRDA_Private_Constants IRDA Private Constants
  180. * @{
  181. */
  182. #define IRDA_TEACK_REACK_TIMEOUT 1000U /*!< IRDA TX or RX enable acknowledge time-out value */
  183. #define IRDA_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE \
  184. | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)) /*!< UART or USART CR1 fields of parameters set by IRDA_SetConfig API */
  185. #define USART_BRR_MIN 0x10U /*!< USART BRR minimum authorized value */
  186. #define USART_BRR_MAX 0x0000FFFFU /*!< USART BRR maximum authorized value */
  187. /**
  188. * @}
  189. */
  190. /* Private macros ------------------------------------------------------------*/
  191. /** @defgroup IRDA_Private_Macros IRDA Private Macros
  192. * @{
  193. */
  194. /** @brief BRR division operation to set BRR register in 16-bit oversampling mode.
  195. * @param __PCLK__ IRDA clock source.
  196. * @param __BAUD__ Baud rate set by the user.
  197. * @param __PRESCALER__ IRDA clock prescaler value.
  198. * @retval Division result
  199. */
  200. #define IRDA_DIV_SAMPLING16(__PCLK__, __BAUD__, __PRESCALER__) ((((__PCLK__)/IRDAPrescTable[(__PRESCALER__)])\
  201. + ((__BAUD__)/2U)) / (__BAUD__))
  202. /**
  203. * @}
  204. */
  205. /* Private variables ---------------------------------------------------------*/
  206. /* Private function prototypes -----------------------------------------------*/
  207. /** @addtogroup IRDA_Private_Functions
  208. * @{
  209. */
  210. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  211. void IRDA_InitCallbacksToDefault(IRDA_HandleTypeDef *hirda);
  212. #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
  213. static HAL_StatusTypeDef IRDA_SetConfig(IRDA_HandleTypeDef *hirda);
  214. static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda);
  215. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status,
  216. uint32_t Tickstart, uint32_t Timeout);
  217. static void IRDA_EndTxTransfer(IRDA_HandleTypeDef *hirda);
  218. static void IRDA_EndRxTransfer(IRDA_HandleTypeDef *hirda);
  219. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  220. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma);
  221. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  222. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma);
  223. static void IRDA_DMAError(DMA_HandleTypeDef *hdma);
  224. static void IRDA_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  225. static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  226. static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  227. static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  228. static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  229. static void IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
  230. static void IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda);
  231. static void IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
  232. /**
  233. * @}
  234. */
  235. /* Exported functions --------------------------------------------------------*/
  236. /** @defgroup IRDA_Exported_Functions IRDA Exported Functions
  237. * @{
  238. */
  239. /** @defgroup IRDA_Exported_Functions_Group1 Initialization and de-initialization functions
  240. * @brief Initialization and Configuration functions
  241. *
  242. @verbatim
  243. ==============================================================================
  244. ##### Initialization and Configuration functions #####
  245. ==============================================================================
  246. [..]
  247. This subsection provides a set of functions allowing to initialize the USARTx
  248. in asynchronous IRDA mode.
  249. (+) For the asynchronous mode only these parameters can be configured:
  250. (++) Baud Rate
  251. (++) Word Length
  252. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  253. in the data register is transmitted but is changed by the parity bit.
  254. (++) Power mode
  255. (++) Prescaler setting
  256. (++) Receiver/transmitter modes
  257. [..]
  258. The HAL_IRDA_Init() API follows the USART asynchronous configuration procedures
  259. (details for the procedures are available in reference manual).
  260. @endverbatim
  261. Depending on the frame length defined by the M1 and M0 bits (7-bit,
  262. 8-bit or 9-bit), the possible IRDA frame formats are listed in the
  263. following table.
  264. Table 1. IRDA frame format.
  265. +-----------------------------------------------------------------------+
  266. | M1 bit | M0 bit | PCE bit | IRDA frame |
  267. |---------|---------|-----------|---------------------------------------|
  268. | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  269. |---------|---------|-----------|---------------------------------------|
  270. | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  271. |---------|---------|-----------|---------------------------------------|
  272. | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  273. |---------|---------|-----------|---------------------------------------|
  274. | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  275. |---------|---------|-----------|---------------------------------------|
  276. | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  277. |---------|---------|-----------|---------------------------------------|
  278. | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  279. +-----------------------------------------------------------------------+
  280. * @{
  281. */
  282. /**
  283. * @brief Initialize the IRDA mode according to the specified
  284. * parameters in the IRDA_InitTypeDef and initialize the associated handle.
  285. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  286. * the configuration information for the specified IRDA module.
  287. * @retval HAL status
  288. */
  289. HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
  290. {
  291. /* Check the IRDA handle allocation */
  292. if (hirda == NULL)
  293. {
  294. return HAL_ERROR;
  295. }
  296. /* Check the USART/UART associated to the IRDA handle */
  297. assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  298. if (hirda->gState == HAL_IRDA_STATE_RESET)
  299. {
  300. /* Allocate lock resource and initialize it */
  301. hirda->Lock = HAL_UNLOCKED;
  302. #if USE_HAL_IRDA_REGISTER_CALLBACKS == 1
  303. IRDA_InitCallbacksToDefault(hirda);
  304. if (hirda->MspInitCallback == NULL)
  305. {
  306. hirda->MspInitCallback = HAL_IRDA_MspInit;
  307. }
  308. /* Init the low level hardware */
  309. hirda->MspInitCallback(hirda);
  310. #else
  311. /* Init the low level hardware : GPIO, CLOCK */
  312. HAL_IRDA_MspInit(hirda);
  313. #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
  314. }
  315. hirda->gState = HAL_IRDA_STATE_BUSY;
  316. /* Disable the Peripheral to update the configuration registers */
  317. __HAL_IRDA_DISABLE(hirda);
  318. /* Set the IRDA Communication parameters */
  319. if (IRDA_SetConfig(hirda) == HAL_ERROR)
  320. {
  321. return HAL_ERROR;
  322. }
  323. /* In IRDA mode, the following bits must be kept cleared:
  324. - LINEN, STOP and CLKEN bits in the USART_CR2 register,
  325. - SCEN and HDSEL bits in the USART_CR3 register.*/
  326. CLEAR_BIT(hirda->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN | USART_CR2_STOP));
  327. CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
  328. /* set the UART/USART in IRDA mode */
  329. hirda->Instance->CR3 |= USART_CR3_IREN;
  330. /* Enable the Peripheral */
  331. __HAL_IRDA_ENABLE(hirda);
  332. /* TEACK and/or REACK to check before moving hirda->gState and hirda->RxState to Ready */
  333. return (IRDA_CheckIdleState(hirda));
  334. }
  335. /**
  336. * @brief DeInitialize the IRDA peripheral.
  337. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  338. * the configuration information for the specified IRDA module.
  339. * @retval HAL status
  340. */
  341. HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
  342. {
  343. /* Check the IRDA handle allocation */
  344. if (hirda == NULL)
  345. {
  346. return HAL_ERROR;
  347. }
  348. /* Check the USART/UART associated to the IRDA handle */
  349. assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  350. hirda->gState = HAL_IRDA_STATE_BUSY;
  351. /* DeInit the low level hardware */
  352. #if USE_HAL_IRDA_REGISTER_CALLBACKS == 1
  353. if (hirda->MspDeInitCallback == NULL)
  354. {
  355. hirda->MspDeInitCallback = HAL_IRDA_MspDeInit;
  356. }
  357. /* DeInit the low level hardware */
  358. hirda->MspDeInitCallback(hirda);
  359. #else
  360. HAL_IRDA_MspDeInit(hirda);
  361. #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
  362. /* Disable the Peripheral */
  363. __HAL_IRDA_DISABLE(hirda);
  364. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  365. hirda->gState = HAL_IRDA_STATE_RESET;
  366. hirda->RxState = HAL_IRDA_STATE_RESET;
  367. /* Process Unlock */
  368. __HAL_UNLOCK(hirda);
  369. return HAL_OK;
  370. }
  371. /**
  372. * @brief Initialize the IRDA MSP.
  373. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  374. * the configuration information for the specified IRDA module.
  375. * @retval None
  376. */
  377. __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
  378. {
  379. /* Prevent unused argument(s) compilation warning */
  380. UNUSED(hirda);
  381. /* NOTE: This function should not be modified, when the callback is needed,
  382. the HAL_IRDA_MspInit can be implemented in the user file
  383. */
  384. }
  385. /**
  386. * @brief DeInitialize the IRDA MSP.
  387. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  388. * the configuration information for the specified IRDA module.
  389. * @retval None
  390. */
  391. __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
  392. {
  393. /* Prevent unused argument(s) compilation warning */
  394. UNUSED(hirda);
  395. /* NOTE: This function should not be modified, when the callback is needed,
  396. the HAL_IRDA_MspDeInit can be implemented in the user file
  397. */
  398. }
  399. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  400. /**
  401. * @brief Register a User IRDA Callback
  402. * To be used to override the weak predefined callback
  403. * @note The HAL_IRDA_RegisterCallback() may be called before HAL_IRDA_Init() in HAL_IRDA_STATE_RESET
  404. * to register callbacks for HAL_IRDA_MSPINIT_CB_ID and HAL_IRDA_MSPDEINIT_CB_ID
  405. * @param hirda irda handle
  406. * @param CallbackID ID of the callback to be registered
  407. * This parameter can be one of the following values:
  408. * @arg @ref HAL_IRDA_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  409. * @arg @ref HAL_IRDA_TX_COMPLETE_CB_ID Tx Complete Callback ID
  410. * @arg @ref HAL_IRDA_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  411. * @arg @ref HAL_IRDA_RX_COMPLETE_CB_ID Rx Complete Callback ID
  412. * @arg @ref HAL_IRDA_ERROR_CB_ID Error Callback ID
  413. * @arg @ref HAL_IRDA_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  414. * @arg @ref HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  415. * @arg @ref HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  416. * @arg @ref HAL_IRDA_MSPINIT_CB_ID MspInit Callback ID
  417. * @arg @ref HAL_IRDA_MSPDEINIT_CB_ID MspDeInit Callback ID
  418. * @param pCallback pointer to the Callback function
  419. * @retval HAL status
  420. */
  421. HAL_StatusTypeDef HAL_IRDA_RegisterCallback(IRDA_HandleTypeDef *hirda, HAL_IRDA_CallbackIDTypeDef CallbackID,
  422. pIRDA_CallbackTypeDef pCallback)
  423. {
  424. HAL_StatusTypeDef status = HAL_OK;
  425. if (pCallback == NULL)
  426. {
  427. /* Update the error code */
  428. hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
  429. return HAL_ERROR;
  430. }
  431. if (hirda->gState == HAL_IRDA_STATE_READY)
  432. {
  433. switch (CallbackID)
  434. {
  435. case HAL_IRDA_TX_HALFCOMPLETE_CB_ID :
  436. hirda->TxHalfCpltCallback = pCallback;
  437. break;
  438. case HAL_IRDA_TX_COMPLETE_CB_ID :
  439. hirda->TxCpltCallback = pCallback;
  440. break;
  441. case HAL_IRDA_RX_HALFCOMPLETE_CB_ID :
  442. hirda->RxHalfCpltCallback = pCallback;
  443. break;
  444. case HAL_IRDA_RX_COMPLETE_CB_ID :
  445. hirda->RxCpltCallback = pCallback;
  446. break;
  447. case HAL_IRDA_ERROR_CB_ID :
  448. hirda->ErrorCallback = pCallback;
  449. break;
  450. case HAL_IRDA_ABORT_COMPLETE_CB_ID :
  451. hirda->AbortCpltCallback = pCallback;
  452. break;
  453. case HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID :
  454. hirda->AbortTransmitCpltCallback = pCallback;
  455. break;
  456. case HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID :
  457. hirda->AbortReceiveCpltCallback = pCallback;
  458. break;
  459. case HAL_IRDA_MSPINIT_CB_ID :
  460. hirda->MspInitCallback = pCallback;
  461. break;
  462. case HAL_IRDA_MSPDEINIT_CB_ID :
  463. hirda->MspDeInitCallback = pCallback;
  464. break;
  465. default :
  466. /* Update the error code */
  467. hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
  468. /* Return error status */
  469. status = HAL_ERROR;
  470. break;
  471. }
  472. }
  473. else if (hirda->gState == HAL_IRDA_STATE_RESET)
  474. {
  475. switch (CallbackID)
  476. {
  477. case HAL_IRDA_MSPINIT_CB_ID :
  478. hirda->MspInitCallback = pCallback;
  479. break;
  480. case HAL_IRDA_MSPDEINIT_CB_ID :
  481. hirda->MspDeInitCallback = pCallback;
  482. break;
  483. default :
  484. /* Update the error code */
  485. hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
  486. /* Return error status */
  487. status = HAL_ERROR;
  488. break;
  489. }
  490. }
  491. else
  492. {
  493. /* Update the error code */
  494. hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
  495. /* Return error status */
  496. status = HAL_ERROR;
  497. }
  498. return status;
  499. }
  500. /**
  501. * @brief Unregister an IRDA callback
  502. * IRDA callback is redirected to the weak predefined callback
  503. * @note The HAL_IRDA_UnRegisterCallback() may be called before HAL_IRDA_Init() in HAL_IRDA_STATE_RESET
  504. * to un-register callbacks for HAL_IRDA_MSPINIT_CB_ID and HAL_IRDA_MSPDEINIT_CB_ID
  505. * @param hirda irda handle
  506. * @param CallbackID ID of the callback to be unregistered
  507. * This parameter can be one of the following values:
  508. * @arg @ref HAL_IRDA_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  509. * @arg @ref HAL_IRDA_TX_COMPLETE_CB_ID Tx Complete Callback ID
  510. * @arg @ref HAL_IRDA_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  511. * @arg @ref HAL_IRDA_RX_COMPLETE_CB_ID Rx Complete Callback ID
  512. * @arg @ref HAL_IRDA_ERROR_CB_ID Error Callback ID
  513. * @arg @ref HAL_IRDA_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  514. * @arg @ref HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  515. * @arg @ref HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  516. * @arg @ref HAL_IRDA_MSPINIT_CB_ID MspInit Callback ID
  517. * @arg @ref HAL_IRDA_MSPDEINIT_CB_ID MspDeInit Callback ID
  518. * @retval HAL status
  519. */
  520. HAL_StatusTypeDef HAL_IRDA_UnRegisterCallback(IRDA_HandleTypeDef *hirda, HAL_IRDA_CallbackIDTypeDef CallbackID)
  521. {
  522. HAL_StatusTypeDef status = HAL_OK;
  523. if (HAL_IRDA_STATE_READY == hirda->gState)
  524. {
  525. switch (CallbackID)
  526. {
  527. case HAL_IRDA_TX_HALFCOMPLETE_CB_ID :
  528. hirda->TxHalfCpltCallback = HAL_IRDA_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  529. break;
  530. case HAL_IRDA_TX_COMPLETE_CB_ID :
  531. hirda->TxCpltCallback = HAL_IRDA_TxCpltCallback; /* Legacy weak TxCpltCallback */
  532. break;
  533. case HAL_IRDA_RX_HALFCOMPLETE_CB_ID :
  534. hirda->RxHalfCpltCallback = HAL_IRDA_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  535. break;
  536. case HAL_IRDA_RX_COMPLETE_CB_ID :
  537. hirda->RxCpltCallback = HAL_IRDA_RxCpltCallback; /* Legacy weak RxCpltCallback */
  538. break;
  539. case HAL_IRDA_ERROR_CB_ID :
  540. hirda->ErrorCallback = HAL_IRDA_ErrorCallback; /* Legacy weak ErrorCallback */
  541. break;
  542. case HAL_IRDA_ABORT_COMPLETE_CB_ID :
  543. hirda->AbortCpltCallback = HAL_IRDA_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  544. break;
  545. case HAL_IRDA_ABORT_TRANSMIT_COMPLETE_CB_ID :
  546. hirda->AbortTransmitCpltCallback = HAL_IRDA_AbortTransmitCpltCallback; /* Legacy weak
  547. AbortTransmitCpltCallback */
  548. break;
  549. case HAL_IRDA_ABORT_RECEIVE_COMPLETE_CB_ID :
  550. hirda->AbortReceiveCpltCallback = HAL_IRDA_AbortReceiveCpltCallback; /* Legacy weak
  551. AbortReceiveCpltCallback */
  552. break;
  553. case HAL_IRDA_MSPINIT_CB_ID :
  554. hirda->MspInitCallback = HAL_IRDA_MspInit; /* Legacy weak MspInitCallback */
  555. break;
  556. case HAL_IRDA_MSPDEINIT_CB_ID :
  557. hirda->MspDeInitCallback = HAL_IRDA_MspDeInit; /* Legacy weak MspDeInitCallback */
  558. break;
  559. default :
  560. /* Update the error code */
  561. hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
  562. /* Return error status */
  563. status = HAL_ERROR;
  564. break;
  565. }
  566. }
  567. else if (HAL_IRDA_STATE_RESET == hirda->gState)
  568. {
  569. switch (CallbackID)
  570. {
  571. case HAL_IRDA_MSPINIT_CB_ID :
  572. hirda->MspInitCallback = HAL_IRDA_MspInit;
  573. break;
  574. case HAL_IRDA_MSPDEINIT_CB_ID :
  575. hirda->MspDeInitCallback = HAL_IRDA_MspDeInit;
  576. break;
  577. default :
  578. /* Update the error code */
  579. hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
  580. /* Return error status */
  581. status = HAL_ERROR;
  582. break;
  583. }
  584. }
  585. else
  586. {
  587. /* Update the error code */
  588. hirda->ErrorCode |= HAL_IRDA_ERROR_INVALID_CALLBACK;
  589. /* Return error status */
  590. status = HAL_ERROR;
  591. }
  592. return status;
  593. }
  594. #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
  595. /**
  596. * @}
  597. */
  598. /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
  599. * @brief IRDA Transmit and Receive functions
  600. *
  601. @verbatim
  602. ===============================================================================
  603. ##### IO operation functions #####
  604. ===============================================================================
  605. [..]
  606. This subsection provides a set of functions allowing to manage the IRDA data transfers.
  607. [..]
  608. IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
  609. on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
  610. is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
  611. While receiving data, transmission should be avoided as the data to be transmitted
  612. could be corrupted.
  613. [..]
  614. (#) There are two modes of transfer:
  615. (++) Blocking mode: the communication is performed in polling mode.
  616. The HAL status of all data processing is returned by the same function
  617. after finishing transfer.
  618. (++) Non-Blocking mode: the communication is performed using Interrupts
  619. or DMA, these API's return the HAL status.
  620. The end of the data processing will be indicated through the
  621. dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
  622. using DMA mode.
  623. The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
  624. will be executed respectively at the end of the Transmit or Receive process
  625. The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
  626. (#) Blocking mode APIs are :
  627. (++) HAL_IRDA_Transmit()
  628. (++) HAL_IRDA_Receive()
  629. (#) Non Blocking mode APIs with Interrupt are :
  630. (++) HAL_IRDA_Transmit_IT()
  631. (++) HAL_IRDA_Receive_IT()
  632. (++) HAL_IRDA_IRQHandler()
  633. (#) Non Blocking mode functions with DMA are :
  634. (++) HAL_IRDA_Transmit_DMA()
  635. (++) HAL_IRDA_Receive_DMA()
  636. (++) HAL_IRDA_DMAPause()
  637. (++) HAL_IRDA_DMAResume()
  638. (++) HAL_IRDA_DMAStop()
  639. (#) A set of Transfer Complete Callbacks are provided in Non Blocking mode:
  640. (++) HAL_IRDA_TxHalfCpltCallback()
  641. (++) HAL_IRDA_TxCpltCallback()
  642. (++) HAL_IRDA_RxHalfCpltCallback()
  643. (++) HAL_IRDA_RxCpltCallback()
  644. (++) HAL_IRDA_ErrorCallback()
  645. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  646. (++) HAL_IRDA_Abort()
  647. (++) HAL_IRDA_AbortTransmit()
  648. (++) HAL_IRDA_AbortReceive()
  649. (++) HAL_IRDA_Abort_IT()
  650. (++) HAL_IRDA_AbortTransmit_IT()
  651. (++) HAL_IRDA_AbortReceive_IT()
  652. (#) For Abort services based on interrupts (HAL_IRDA_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  653. (++) HAL_IRDA_AbortCpltCallback()
  654. (++) HAL_IRDA_AbortTransmitCpltCallback()
  655. (++) HAL_IRDA_AbortReceiveCpltCallback()
  656. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  657. Errors are handled as follows :
  658. (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  659. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error
  660. in Interrupt mode reception .
  661. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user
  662. to identify error type, and HAL_IRDA_ErrorCallback() user callback is executed.
  663. Transfer is kept ongoing on IRDA side.
  664. If user wants to abort it, Abort services should be called by user.
  665. (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  666. This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
  667. Error code is set to allow user to identify error type, and
  668. HAL_IRDA_ErrorCallback() user callback is executed.
  669. @endverbatim
  670. * @{
  671. */
  672. /**
  673. * @brief Send an amount of data in blocking mode.
  674. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  675. * the sent data is handled as a set of u16. In this case, Size must reflect the number
  676. * of u16 available through pData.
  677. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  678. * the configuration information for the specified IRDA module.
  679. * @param pData Pointer to data buffer (u8 or u16 data elements).
  680. * @param Size Amount of data elements (u8 or u16) to be sent.
  681. * @param Timeout Specify timeout value.
  682. * @retval HAL status
  683. */
  684. /**
  685. * @note When IRDA parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  686. * address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  687. * (as sent data will be handled using u16 pointer cast). Depending on compilation chain,
  688. * use of specific alignment compilation directives or pragmas might be required
  689. * to ensure proper alignment for pData.
  690. */
  691. HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
  692. {
  693. const uint8_t *pdata8bits;
  694. const uint16_t *pdata16bits;
  695. uint32_t tickstart;
  696. /* Check that a Tx process is not already ongoing */
  697. if (hirda->gState == HAL_IRDA_STATE_READY)
  698. {
  699. if ((pData == NULL) || (Size == 0U))
  700. {
  701. return HAL_ERROR;
  702. }
  703. /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  704. should be aligned on a u16 frontier, as data to be filled into TDR will be
  705. handled through a u16 cast. */
  706. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  707. {
  708. if ((((uint32_t)pData) & 1U) != 0U)
  709. {
  710. return HAL_ERROR;
  711. }
  712. }
  713. /* Process Locked */
  714. __HAL_LOCK(hirda);
  715. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  716. hirda->gState = HAL_IRDA_STATE_BUSY_TX;
  717. /* Init tickstart for timeout management */
  718. tickstart = HAL_GetTick();
  719. hirda->TxXferSize = Size;
  720. hirda->TxXferCount = Size;
  721. /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
  722. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  723. {
  724. pdata8bits = NULL;
  725. pdata16bits = (const uint16_t *) pData; /* Derogation R.11.3 */
  726. }
  727. else
  728. {
  729. pdata8bits = pData;
  730. pdata16bits = NULL;
  731. }
  732. while (hirda->TxXferCount > 0U)
  733. {
  734. hirda->TxXferCount--;
  735. if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  736. {
  737. return HAL_TIMEOUT;
  738. }
  739. if (pdata8bits == NULL)
  740. {
  741. hirda->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU);
  742. pdata16bits++;
  743. }
  744. else
  745. {
  746. hirda->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU);
  747. pdata8bits++;
  748. }
  749. }
  750. if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  751. {
  752. return HAL_TIMEOUT;
  753. }
  754. /* At end of Tx process, restore hirda->gState to Ready */
  755. hirda->gState = HAL_IRDA_STATE_READY;
  756. /* Process Unlocked */
  757. __HAL_UNLOCK(hirda);
  758. return HAL_OK;
  759. }
  760. else
  761. {
  762. return HAL_BUSY;
  763. }
  764. }
  765. /**
  766. * @brief Receive an amount of data in blocking mode.
  767. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  768. * the received data is handled as a set of u16. In this case, Size must reflect the number
  769. * of u16 available through pData.
  770. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  771. * the configuration information for the specified IRDA module.
  772. * @param pData Pointer to data buffer (u8 or u16 data elements).
  773. * @param Size Amount of data elements (u8 or u16) to be received.
  774. * @param Timeout Specify timeout value.
  775. * @retval HAL status
  776. */
  777. /**
  778. * @note When IRDA parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  779. * address of user data buffer for storing data to be received, should be aligned on a half word frontier
  780. * (16 bits) (as received data will be handled using u16 pointer cast). Depending on compilation chain,
  781. * use of specific alignment compilation directives or pragmas might be required
  782. * to ensure proper alignment for pData.
  783. */
  784. HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  785. {
  786. uint8_t *pdata8bits;
  787. uint16_t *pdata16bits;
  788. uint16_t uhMask;
  789. uint32_t tickstart;
  790. /* Check that a Rx process is not already ongoing */
  791. if (hirda->RxState == HAL_IRDA_STATE_READY)
  792. {
  793. if ((pData == NULL) || (Size == 0U))
  794. {
  795. return HAL_ERROR;
  796. }
  797. /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  798. should be aligned on a u16 frontier, as data to be received from RDR will be
  799. handled through a u16 cast. */
  800. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  801. {
  802. if ((((uint32_t)pData) & 1U) != 0U)
  803. {
  804. return HAL_ERROR;
  805. }
  806. }
  807. /* Process Locked */
  808. __HAL_LOCK(hirda);
  809. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  810. hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
  811. /* Init tickstart for timeout management */
  812. tickstart = HAL_GetTick();
  813. hirda->RxXferSize = Size;
  814. hirda->RxXferCount = Size;
  815. /* Computation of the mask to apply to RDR register
  816. of the UART associated to the IRDA */
  817. IRDA_MASK_COMPUTATION(hirda);
  818. uhMask = hirda->Mask;
  819. /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  820. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  821. {
  822. pdata8bits = NULL;
  823. pdata16bits = (uint16_t *) pData; /* Derogation R.11.3 */
  824. }
  825. else
  826. {
  827. pdata8bits = pData;
  828. pdata16bits = NULL;
  829. }
  830. /* Check data remaining to be received */
  831. while (hirda->RxXferCount > 0U)
  832. {
  833. hirda->RxXferCount--;
  834. if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  835. {
  836. return HAL_TIMEOUT;
  837. }
  838. if (pdata8bits == NULL)
  839. {
  840. *pdata16bits = (uint16_t)(hirda->Instance->RDR & uhMask);
  841. pdata16bits++;
  842. }
  843. else
  844. {
  845. *pdata8bits = (uint8_t)(hirda->Instance->RDR & (uint8_t)uhMask);
  846. pdata8bits++;
  847. }
  848. }
  849. /* At end of Rx process, restore hirda->RxState to Ready */
  850. hirda->RxState = HAL_IRDA_STATE_READY;
  851. /* Process Unlocked */
  852. __HAL_UNLOCK(hirda);
  853. return HAL_OK;
  854. }
  855. else
  856. {
  857. return HAL_BUSY;
  858. }
  859. }
  860. /**
  861. * @brief Send an amount of data in interrupt mode.
  862. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  863. * the sent data is handled as a set of u16. In this case, Size must reflect the number
  864. * of u16 available through pData.
  865. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  866. * the configuration information for the specified IRDA module.
  867. * @param pData Pointer to data buffer (u8 or u16 data elements).
  868. * @param Size Amount of data elements (u8 or u16) to be sent.
  869. * @retval HAL status
  870. */
  871. /**
  872. * @note When IRDA parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  873. * address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  874. * (as sent data will be handled using u16 pointer cast). Depending on compilation chain,
  875. * use of specific alignment compilation directives or pragmas might be required
  876. * to ensure proper alignment for pData.
  877. */
  878. HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, const uint8_t *pData, uint16_t Size)
  879. {
  880. /* Check that a Tx process is not already ongoing */
  881. if (hirda->gState == HAL_IRDA_STATE_READY)
  882. {
  883. if ((pData == NULL) || (Size == 0U))
  884. {
  885. return HAL_ERROR;
  886. }
  887. /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  888. should be aligned on a u16 frontier, as data to be filled into TDR will be
  889. handled through a u16 cast. */
  890. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  891. {
  892. if ((((uint32_t)pData) & 1U) != 0U)
  893. {
  894. return HAL_ERROR;
  895. }
  896. }
  897. /* Process Locked */
  898. __HAL_LOCK(hirda);
  899. hirda->pTxBuffPtr = pData;
  900. hirda->TxXferSize = Size;
  901. hirda->TxXferCount = Size;
  902. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  903. hirda->gState = HAL_IRDA_STATE_BUSY_TX;
  904. /* Process Unlocked */
  905. __HAL_UNLOCK(hirda);
  906. /* Enable the IRDA Transmit Data Register Empty Interrupt */
  907. SET_BIT(hirda->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  908. return HAL_OK;
  909. }
  910. else
  911. {
  912. return HAL_BUSY;
  913. }
  914. }
  915. /**
  916. * @brief Receive an amount of data in interrupt mode.
  917. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  918. * the received data is handled as a set of u16. In this case, Size must reflect the number
  919. * of u16 available through pData.
  920. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  921. * the configuration information for the specified IRDA module.
  922. * @param pData Pointer to data buffer (u8 or u16 data elements).
  923. * @param Size Amount of data elements (u8 or u16) to be received.
  924. * @retval HAL status
  925. */
  926. /**
  927. * @note When IRDA parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  928. * address of user data buffer for storing data to be received, should be aligned on a half word frontier
  929. * (16 bits) (as received data will be handled using u16 pointer cast). Depending on compilation chain,
  930. * use of specific alignment compilation directives or pragmas might be required
  931. * to ensure proper alignment for pData.
  932. */
  933. HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  934. {
  935. /* Check that a Rx process is not already ongoing */
  936. if (hirda->RxState == HAL_IRDA_STATE_READY)
  937. {
  938. if ((pData == NULL) || (Size == 0U))
  939. {
  940. return HAL_ERROR;
  941. }
  942. /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  943. should be aligned on a u16 frontier, as data to be received from RDR will be
  944. handled through a u16 cast. */
  945. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  946. {
  947. if ((((uint32_t)pData) & 1U) != 0U)
  948. {
  949. return HAL_ERROR;
  950. }
  951. }
  952. /* Process Locked */
  953. __HAL_LOCK(hirda);
  954. hirda->pRxBuffPtr = pData;
  955. hirda->RxXferSize = Size;
  956. hirda->RxXferCount = Size;
  957. /* Computation of the mask to apply to the RDR register
  958. of the UART associated to the IRDA */
  959. IRDA_MASK_COMPUTATION(hirda);
  960. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  961. hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
  962. /* Process Unlocked */
  963. __HAL_UNLOCK(hirda);
  964. if (hirda->Init.Parity != IRDA_PARITY_NONE)
  965. {
  966. /* Enable the IRDA Parity Error and Data Register not empty Interrupts */
  967. SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
  968. }
  969. else
  970. {
  971. /* Enable the IRDA Data Register not empty Interrupts */
  972. SET_BIT(hirda->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  973. }
  974. /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  975. SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  976. return HAL_OK;
  977. }
  978. else
  979. {
  980. return HAL_BUSY;
  981. }
  982. }
  983. /**
  984. * @brief Send an amount of data in DMA mode.
  985. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  986. * the sent data is handled as a set of u16. In this case, Size must reflect the number
  987. * of u16 available through pData.
  988. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  989. * the configuration information for the specified IRDA module.
  990. * @param pData pointer to data buffer (u8 or u16 data elements).
  991. * @param Size Amount of data elements (u8 or u16) to be sent.
  992. * @retval HAL status
  993. */
  994. /**
  995. * @note When IRDA parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  996. * address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  997. * (as sent data will be handled by DMA from halfword frontier). Depending on compilation chain,
  998. * use of specific alignment compilation directives or pragmas might be required
  999. * to ensure proper alignment for pData.
  1000. */
  1001. HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, const uint8_t *pData, uint16_t Size)
  1002. {
  1003. /* Check that a Tx process is not already ongoing */
  1004. if (hirda->gState == HAL_IRDA_STATE_READY)
  1005. {
  1006. if ((pData == NULL) || (Size == 0U))
  1007. {
  1008. return HAL_ERROR;
  1009. }
  1010. /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  1011. should be aligned on a u16 frontier, as data copy into TDR will be
  1012. handled by DMA from a u16 frontier. */
  1013. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  1014. {
  1015. if ((((uint32_t)pData) & 1U) != 0U)
  1016. {
  1017. return HAL_ERROR;
  1018. }
  1019. }
  1020. /* Process Locked */
  1021. __HAL_LOCK(hirda);
  1022. hirda->pTxBuffPtr = pData;
  1023. hirda->TxXferSize = Size;
  1024. hirda->TxXferCount = Size;
  1025. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1026. hirda->gState = HAL_IRDA_STATE_BUSY_TX;
  1027. /* Set the IRDA DMA transfer complete callback */
  1028. hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
  1029. /* Set the IRDA DMA half transfer complete callback */
  1030. hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;
  1031. /* Set the DMA error callback */
  1032. hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
  1033. /* Set the DMA abort callback */
  1034. hirda->hdmatx->XferAbortCallback = NULL;
  1035. /* Enable the IRDA transmit DMA channel */
  1036. if (HAL_DMA_Start_IT(hirda->hdmatx, (uint32_t)hirda->pTxBuffPtr, (uint32_t)&hirda->Instance->TDR, Size) == HAL_OK)
  1037. {
  1038. /* Clear the TC flag in the ICR register */
  1039. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_TCF);
  1040. /* Process Unlocked */
  1041. __HAL_UNLOCK(hirda);
  1042. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1043. in the USART CR3 register */
  1044. SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1045. return HAL_OK;
  1046. }
  1047. else
  1048. {
  1049. /* Set error code to DMA */
  1050. hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
  1051. /* Process Unlocked */
  1052. __HAL_UNLOCK(hirda);
  1053. /* Restore hirda->gState to ready */
  1054. hirda->gState = HAL_IRDA_STATE_READY;
  1055. return HAL_ERROR;
  1056. }
  1057. }
  1058. else
  1059. {
  1060. return HAL_BUSY;
  1061. }
  1062. }
  1063. /**
  1064. * @brief Receive an amount of data in DMA mode.
  1065. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1066. * the received data is handled as a set of u16. In this case, Size must reflect the number
  1067. * of u16 available through pData.
  1068. * @note When the IRDA parity is enabled (PCE = 1), the received data contains
  1069. * the parity bit (MSB position).
  1070. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1071. * the configuration information for the specified IRDA module.
  1072. * @param pData Pointer to data buffer (u8 or u16 data elements).
  1073. * @param Size Amount of data elements (u8 or u16) to be received.
  1074. * @retval HAL status
  1075. */
  1076. /**
  1077. * @note When IRDA parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1078. * address of user data buffer for storing data to be received, should be aligned on a half word frontier
  1079. * (16 bits) (as received data will be handled by DMA from halfword frontier). Depending on compilation chain,
  1080. * use of specific alignment compilation directives or pragmas might be required
  1081. * to ensure proper alignment for pData.
  1082. */
  1083. HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  1084. {
  1085. /* Check that a Rx process is not already ongoing */
  1086. if (hirda->RxState == HAL_IRDA_STATE_READY)
  1087. {
  1088. if ((pData == NULL) || (Size == 0U))
  1089. {
  1090. return HAL_ERROR;
  1091. }
  1092. /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  1093. should be aligned on a u16 frontier, as data copy from RDR will be
  1094. handled by DMA from a u16 frontier. */
  1095. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  1096. {
  1097. if ((((uint32_t)pData) & 1U) != 0U)
  1098. {
  1099. return HAL_ERROR;
  1100. }
  1101. }
  1102. /* Process Locked */
  1103. __HAL_LOCK(hirda);
  1104. hirda->pRxBuffPtr = pData;
  1105. hirda->RxXferSize = Size;
  1106. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1107. hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
  1108. /* Set the IRDA DMA transfer complete callback */
  1109. hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
  1110. /* Set the IRDA DMA half transfer complete callback */
  1111. hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;
  1112. /* Set the DMA error callback */
  1113. hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
  1114. /* Set the DMA abort callback */
  1115. hirda->hdmarx->XferAbortCallback = NULL;
  1116. /* Enable the DMA channel */
  1117. if (HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->RDR, (uint32_t)hirda->pRxBuffPtr, Size) == HAL_OK)
  1118. {
  1119. /* Process Unlocked */
  1120. __HAL_UNLOCK(hirda);
  1121. if (hirda->Init.Parity != IRDA_PARITY_NONE)
  1122. {
  1123. /* Enable the UART Parity Error Interrupt */
  1124. SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  1125. }
  1126. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1127. SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1128. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1129. in the USART CR3 register */
  1130. SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1131. return HAL_OK;
  1132. }
  1133. else
  1134. {
  1135. /* Set error code to DMA */
  1136. hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
  1137. /* Process Unlocked */
  1138. __HAL_UNLOCK(hirda);
  1139. /* Restore hirda->RxState to ready */
  1140. hirda->RxState = HAL_IRDA_STATE_READY;
  1141. return HAL_ERROR;
  1142. }
  1143. }
  1144. else
  1145. {
  1146. return HAL_BUSY;
  1147. }
  1148. }
  1149. /**
  1150. * @brief Pause the DMA Transfer.
  1151. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1152. * the configuration information for the specified IRDA module.
  1153. * @retval HAL status
  1154. */
  1155. HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda)
  1156. {
  1157. /* Process Locked */
  1158. __HAL_LOCK(hirda);
  1159. if (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
  1160. {
  1161. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1162. {
  1163. /* Disable the IRDA DMA Tx request */
  1164. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1165. }
  1166. }
  1167. if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
  1168. {
  1169. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1170. {
  1171. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1172. CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  1173. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1174. /* Disable the IRDA DMA Rx request */
  1175. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1176. }
  1177. }
  1178. /* Process Unlocked */
  1179. __HAL_UNLOCK(hirda);
  1180. return HAL_OK;
  1181. }
  1182. /**
  1183. * @brief Resume the DMA Transfer.
  1184. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1185. * the configuration information for the specified UART module.
  1186. * @retval HAL status
  1187. */
  1188. HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda)
  1189. {
  1190. /* Process Locked */
  1191. __HAL_LOCK(hirda);
  1192. if (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
  1193. {
  1194. /* Enable the IRDA DMA Tx request */
  1195. SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1196. }
  1197. if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
  1198. {
  1199. /* Clear the Overrun flag before resuming the Rx transfer*/
  1200. __HAL_IRDA_CLEAR_OREFLAG(hirda);
  1201. /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1202. if (hirda->Init.Parity != IRDA_PARITY_NONE)
  1203. {
  1204. SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  1205. }
  1206. SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1207. /* Enable the IRDA DMA Rx request */
  1208. SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1209. }
  1210. /* Process Unlocked */
  1211. __HAL_UNLOCK(hirda);
  1212. return HAL_OK;
  1213. }
  1214. /**
  1215. * @brief Stop the DMA Transfer.
  1216. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1217. * the configuration information for the specified UART module.
  1218. * @retval HAL status
  1219. */
  1220. HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda)
  1221. {
  1222. /* The Lock is not implemented on this API to allow the user application
  1223. to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback() /
  1224. HAL_IRDA_TxHalfCpltCallback / HAL_IRDA_RxHalfCpltCallback:
  1225. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  1226. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  1227. the stream and the corresponding call back is executed. */
  1228. /* Stop IRDA DMA Tx request if ongoing */
  1229. if (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
  1230. {
  1231. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1232. {
  1233. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1234. /* Abort the IRDA DMA Tx channel */
  1235. if (hirda->hdmatx != NULL)
  1236. {
  1237. if (HAL_DMA_Abort(hirda->hdmatx) != HAL_OK)
  1238. {
  1239. if (HAL_DMA_GetError(hirda->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1240. {
  1241. /* Set error code to DMA */
  1242. hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
  1243. return HAL_TIMEOUT;
  1244. }
  1245. }
  1246. }
  1247. IRDA_EndTxTransfer(hirda);
  1248. }
  1249. }
  1250. /* Stop IRDA DMA Rx request if ongoing */
  1251. if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
  1252. {
  1253. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1254. {
  1255. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1256. /* Abort the IRDA DMA Rx channel */
  1257. if (hirda->hdmarx != NULL)
  1258. {
  1259. if (HAL_DMA_Abort(hirda->hdmarx) != HAL_OK)
  1260. {
  1261. if (HAL_DMA_GetError(hirda->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1262. {
  1263. /* Set error code to DMA */
  1264. hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
  1265. return HAL_TIMEOUT;
  1266. }
  1267. }
  1268. }
  1269. IRDA_EndRxTransfer(hirda);
  1270. }
  1271. }
  1272. return HAL_OK;
  1273. }
  1274. /**
  1275. * @brief Abort ongoing transfers (blocking mode).
  1276. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1277. * the configuration information for the specified UART module.
  1278. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1279. * This procedure performs following operations :
  1280. * - Disable IRDA Interrupts (Tx and Rx)
  1281. * - Disable the DMA transfer in the peripheral register (if enabled)
  1282. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1283. * - Set handle State to READY
  1284. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1285. * @retval HAL status
  1286. */
  1287. HAL_StatusTypeDef HAL_IRDA_Abort(IRDA_HandleTypeDef *hirda)
  1288. {
  1289. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1290. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | \
  1291. USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1292. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1293. /* Disable the IRDA DMA Tx request if enabled */
  1294. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1295. {
  1296. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1297. /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
  1298. if (hirda->hdmatx != NULL)
  1299. {
  1300. /* Set the IRDA DMA Abort callback to Null.
  1301. No call back execution at end of DMA abort procedure */
  1302. hirda->hdmatx->XferAbortCallback = NULL;
  1303. if (HAL_DMA_Abort(hirda->hdmatx) != HAL_OK)
  1304. {
  1305. if (HAL_DMA_GetError(hirda->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1306. {
  1307. /* Set error code to DMA */
  1308. hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
  1309. return HAL_TIMEOUT;
  1310. }
  1311. }
  1312. }
  1313. }
  1314. /* Disable the IRDA DMA Rx request if enabled */
  1315. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1316. {
  1317. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1318. /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
  1319. if (hirda->hdmarx != NULL)
  1320. {
  1321. /* Set the IRDA DMA Abort callback to Null.
  1322. No call back execution at end of DMA abort procedure */
  1323. hirda->hdmarx->XferAbortCallback = NULL;
  1324. if (HAL_DMA_Abort(hirda->hdmarx) != HAL_OK)
  1325. {
  1326. if (HAL_DMA_GetError(hirda->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1327. {
  1328. /* Set error code to DMA */
  1329. hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
  1330. return HAL_TIMEOUT;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. /* Reset Tx and Rx transfer counters */
  1336. hirda->TxXferCount = 0U;
  1337. hirda->RxXferCount = 0U;
  1338. /* Clear the Error flags in the ICR register */
  1339. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1340. /* Restore hirda->gState and hirda->RxState to Ready */
  1341. hirda->gState = HAL_IRDA_STATE_READY;
  1342. hirda->RxState = HAL_IRDA_STATE_READY;
  1343. /* Reset Handle ErrorCode to No Error */
  1344. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1345. return HAL_OK;
  1346. }
  1347. /**
  1348. * @brief Abort ongoing Transmit transfer (blocking mode).
  1349. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1350. * the configuration information for the specified UART module.
  1351. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1352. * This procedure performs following operations :
  1353. * - Disable IRDA Interrupts (Tx)
  1354. * - Disable the DMA transfer in the peripheral register (if enabled)
  1355. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1356. * - Set handle State to READY
  1357. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1358. * @retval HAL status
  1359. */
  1360. HAL_StatusTypeDef HAL_IRDA_AbortTransmit(IRDA_HandleTypeDef *hirda)
  1361. {
  1362. /* Disable TXEIE and TCIE interrupts */
  1363. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1364. /* Disable the IRDA DMA Tx request if enabled */
  1365. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1366. {
  1367. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1368. /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
  1369. if (hirda->hdmatx != NULL)
  1370. {
  1371. /* Set the IRDA DMA Abort callback to Null.
  1372. No call back execution at end of DMA abort procedure */
  1373. hirda->hdmatx->XferAbortCallback = NULL;
  1374. if (HAL_DMA_Abort(hirda->hdmatx) != HAL_OK)
  1375. {
  1376. if (HAL_DMA_GetError(hirda->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1377. {
  1378. /* Set error code to DMA */
  1379. hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
  1380. return HAL_TIMEOUT;
  1381. }
  1382. }
  1383. }
  1384. }
  1385. /* Reset Tx transfer counter */
  1386. hirda->TxXferCount = 0U;
  1387. /* Restore hirda->gState to Ready */
  1388. hirda->gState = HAL_IRDA_STATE_READY;
  1389. return HAL_OK;
  1390. }
  1391. /**
  1392. * @brief Abort ongoing Receive transfer (blocking mode).
  1393. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1394. * the configuration information for the specified UART module.
  1395. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1396. * This procedure performs following operations :
  1397. * - Disable IRDA Interrupts (Rx)
  1398. * - Disable the DMA transfer in the peripheral register (if enabled)
  1399. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1400. * - Set handle State to READY
  1401. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1402. * @retval HAL status
  1403. */
  1404. HAL_StatusTypeDef HAL_IRDA_AbortReceive(IRDA_HandleTypeDef *hirda)
  1405. {
  1406. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1407. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  1408. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1409. /* Disable the IRDA DMA Rx request if enabled */
  1410. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1411. {
  1412. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1413. /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
  1414. if (hirda->hdmarx != NULL)
  1415. {
  1416. /* Set the IRDA DMA Abort callback to Null.
  1417. No call back execution at end of DMA abort procedure */
  1418. hirda->hdmarx->XferAbortCallback = NULL;
  1419. if (HAL_DMA_Abort(hirda->hdmarx) != HAL_OK)
  1420. {
  1421. if (HAL_DMA_GetError(hirda->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1422. {
  1423. /* Set error code to DMA */
  1424. hirda->ErrorCode = HAL_IRDA_ERROR_DMA;
  1425. return HAL_TIMEOUT;
  1426. }
  1427. }
  1428. }
  1429. }
  1430. /* Reset Rx transfer counter */
  1431. hirda->RxXferCount = 0U;
  1432. /* Clear the Error flags in the ICR register */
  1433. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1434. /* Restore hirda->RxState to Ready */
  1435. hirda->RxState = HAL_IRDA_STATE_READY;
  1436. return HAL_OK;
  1437. }
  1438. /**
  1439. * @brief Abort ongoing transfers (Interrupt mode).
  1440. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1441. * the configuration information for the specified UART module.
  1442. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1443. * This procedure performs following operations :
  1444. * - Disable IRDA Interrupts (Tx and Rx)
  1445. * - Disable the DMA transfer in the peripheral register (if enabled)
  1446. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1447. * - Set handle State to READY
  1448. * - At abort completion, call user abort complete callback
  1449. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1450. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1451. * @retval HAL status
  1452. */
  1453. HAL_StatusTypeDef HAL_IRDA_Abort_IT(IRDA_HandleTypeDef *hirda)
  1454. {
  1455. uint32_t abortcplt = 1U;
  1456. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1457. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | \
  1458. USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1459. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1460. /* If DMA Tx and/or DMA Rx Handles are associated to IRDA Handle, DMA Abort complete callbacks should be initialised
  1461. before any call to DMA Abort functions */
  1462. /* DMA Tx Handle is valid */
  1463. if (hirda->hdmatx != NULL)
  1464. {
  1465. /* Set DMA Abort Complete callback if IRDA DMA Tx request if enabled.
  1466. Otherwise, set it to NULL */
  1467. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1468. {
  1469. hirda->hdmatx->XferAbortCallback = IRDA_DMATxAbortCallback;
  1470. }
  1471. else
  1472. {
  1473. hirda->hdmatx->XferAbortCallback = NULL;
  1474. }
  1475. }
  1476. /* DMA Rx Handle is valid */
  1477. if (hirda->hdmarx != NULL)
  1478. {
  1479. /* Set DMA Abort Complete callback if IRDA DMA Rx request if enabled.
  1480. Otherwise, set it to NULL */
  1481. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1482. {
  1483. hirda->hdmarx->XferAbortCallback = IRDA_DMARxAbortCallback;
  1484. }
  1485. else
  1486. {
  1487. hirda->hdmarx->XferAbortCallback = NULL;
  1488. }
  1489. }
  1490. /* Disable the IRDA DMA Tx request if enabled */
  1491. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1492. {
  1493. /* Disable DMA Tx at UART level */
  1494. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1495. /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
  1496. if (hirda->hdmatx != NULL)
  1497. {
  1498. /* IRDA Tx DMA Abort callback has already been initialised :
  1499. will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  1500. /* Abort DMA TX */
  1501. if (HAL_DMA_Abort_IT(hirda->hdmatx) != HAL_OK)
  1502. {
  1503. hirda->hdmatx->XferAbortCallback = NULL;
  1504. }
  1505. else
  1506. {
  1507. abortcplt = 0U;
  1508. }
  1509. }
  1510. }
  1511. /* Disable the IRDA DMA Rx request if enabled */
  1512. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1513. {
  1514. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1515. /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
  1516. if (hirda->hdmarx != NULL)
  1517. {
  1518. /* IRDA Rx DMA Abort callback has already been initialised :
  1519. will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  1520. /* Abort DMA RX */
  1521. if (HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
  1522. {
  1523. hirda->hdmarx->XferAbortCallback = NULL;
  1524. abortcplt = 1U;
  1525. }
  1526. else
  1527. {
  1528. abortcplt = 0U;
  1529. }
  1530. }
  1531. }
  1532. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1533. if (abortcplt == 1U)
  1534. {
  1535. /* Reset Tx and Rx transfer counters */
  1536. hirda->TxXferCount = 0U;
  1537. hirda->RxXferCount = 0U;
  1538. /* Reset errorCode */
  1539. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1540. /* Clear the Error flags in the ICR register */
  1541. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1542. /* Restore hirda->gState and hirda->RxState to Ready */
  1543. hirda->gState = HAL_IRDA_STATE_READY;
  1544. hirda->RxState = HAL_IRDA_STATE_READY;
  1545. /* As no DMA to be aborted, call directly user Abort complete callback */
  1546. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  1547. /* Call registered Abort complete callback */
  1548. hirda->AbortCpltCallback(hirda);
  1549. #else
  1550. /* Call legacy weak Abort complete callback */
  1551. HAL_IRDA_AbortCpltCallback(hirda);
  1552. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  1553. }
  1554. return HAL_OK;
  1555. }
  1556. /**
  1557. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1558. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1559. * the configuration information for the specified UART module.
  1560. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1561. * This procedure performs following operations :
  1562. * - Disable IRDA Interrupts (Tx)
  1563. * - Disable the DMA transfer in the peripheral register (if enabled)
  1564. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1565. * - Set handle State to READY
  1566. * - At abort completion, call user abort complete callback
  1567. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1568. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1569. * @retval HAL status
  1570. */
  1571. HAL_StatusTypeDef HAL_IRDA_AbortTransmit_IT(IRDA_HandleTypeDef *hirda)
  1572. {
  1573. /* Disable TXEIE and TCIE interrupts */
  1574. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1575. /* Disable the IRDA DMA Tx request if enabled */
  1576. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1577. {
  1578. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1579. /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
  1580. if (hirda->hdmatx != NULL)
  1581. {
  1582. /* Set the IRDA DMA Abort callback :
  1583. will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  1584. hirda->hdmatx->XferAbortCallback = IRDA_DMATxOnlyAbortCallback;
  1585. /* Abort DMA TX */
  1586. if (HAL_DMA_Abort_IT(hirda->hdmatx) != HAL_OK)
  1587. {
  1588. /* Call Directly hirda->hdmatx->XferAbortCallback function in case of error */
  1589. hirda->hdmatx->XferAbortCallback(hirda->hdmatx);
  1590. }
  1591. }
  1592. else
  1593. {
  1594. /* Reset Tx transfer counter */
  1595. hirda->TxXferCount = 0U;
  1596. /* Restore hirda->gState to Ready */
  1597. hirda->gState = HAL_IRDA_STATE_READY;
  1598. /* As no DMA to be aborted, call directly user Abort complete callback */
  1599. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  1600. /* Call registered Abort Transmit Complete Callback */
  1601. hirda->AbortTransmitCpltCallback(hirda);
  1602. #else
  1603. /* Call legacy weak Abort Transmit Complete Callback */
  1604. HAL_IRDA_AbortTransmitCpltCallback(hirda);
  1605. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  1606. }
  1607. }
  1608. else
  1609. {
  1610. /* Reset Tx transfer counter */
  1611. hirda->TxXferCount = 0U;
  1612. /* Restore hirda->gState to Ready */
  1613. hirda->gState = HAL_IRDA_STATE_READY;
  1614. /* As no DMA to be aborted, call directly user Abort complete callback */
  1615. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  1616. /* Call registered Abort Transmit Complete Callback */
  1617. hirda->AbortTransmitCpltCallback(hirda);
  1618. #else
  1619. /* Call legacy weak Abort Transmit Complete Callback */
  1620. HAL_IRDA_AbortTransmitCpltCallback(hirda);
  1621. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  1622. }
  1623. return HAL_OK;
  1624. }
  1625. /**
  1626. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1627. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1628. * the configuration information for the specified UART module.
  1629. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1630. * This procedure performs following operations :
  1631. * - Disable IRDA Interrupts (Rx)
  1632. * - Disable the DMA transfer in the peripheral register (if enabled)
  1633. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1634. * - Set handle State to READY
  1635. * - At abort completion, call user abort complete callback
  1636. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1637. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1638. * @retval HAL status
  1639. */
  1640. HAL_StatusTypeDef HAL_IRDA_AbortReceive_IT(IRDA_HandleTypeDef *hirda)
  1641. {
  1642. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1643. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  1644. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1645. /* Disable the IRDA DMA Rx request if enabled */
  1646. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1647. {
  1648. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1649. /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
  1650. if (hirda->hdmarx != NULL)
  1651. {
  1652. /* Set the IRDA DMA Abort callback :
  1653. will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  1654. hirda->hdmarx->XferAbortCallback = IRDA_DMARxOnlyAbortCallback;
  1655. /* Abort DMA RX */
  1656. if (HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
  1657. {
  1658. /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
  1659. hirda->hdmarx->XferAbortCallback(hirda->hdmarx);
  1660. }
  1661. }
  1662. else
  1663. {
  1664. /* Reset Rx transfer counter */
  1665. hirda->RxXferCount = 0U;
  1666. /* Clear the Error flags in the ICR register */
  1667. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1668. /* Restore hirda->RxState to Ready */
  1669. hirda->RxState = HAL_IRDA_STATE_READY;
  1670. /* As no DMA to be aborted, call directly user Abort complete callback */
  1671. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  1672. /* Call registered Abort Receive Complete Callback */
  1673. hirda->AbortReceiveCpltCallback(hirda);
  1674. #else
  1675. /* Call legacy weak Abort Receive Complete Callback */
  1676. HAL_IRDA_AbortReceiveCpltCallback(hirda);
  1677. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  1678. }
  1679. }
  1680. else
  1681. {
  1682. /* Reset Rx transfer counter */
  1683. hirda->RxXferCount = 0U;
  1684. /* Clear the Error flags in the ICR register */
  1685. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1686. /* Restore hirda->RxState to Ready */
  1687. hirda->RxState = HAL_IRDA_STATE_READY;
  1688. /* As no DMA to be aborted, call directly user Abort complete callback */
  1689. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  1690. /* Call registered Abort Receive Complete Callback */
  1691. hirda->AbortReceiveCpltCallback(hirda);
  1692. #else
  1693. /* Call legacy weak Abort Receive Complete Callback */
  1694. HAL_IRDA_AbortReceiveCpltCallback(hirda);
  1695. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  1696. }
  1697. return HAL_OK;
  1698. }
  1699. /**
  1700. * @brief Handle IRDA interrupt request.
  1701. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1702. * the configuration information for the specified IRDA module.
  1703. * @retval None
  1704. */
  1705. void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
  1706. {
  1707. uint32_t isrflags = READ_REG(hirda->Instance->ISR);
  1708. uint32_t cr1its = READ_REG(hirda->Instance->CR1);
  1709. uint32_t cr3its;
  1710. uint32_t errorflags;
  1711. uint32_t errorcode;
  1712. /* If no error occurs */
  1713. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
  1714. if (errorflags == 0U)
  1715. {
  1716. /* IRDA in mode Receiver ---------------------------------------------------*/
  1717. if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) && ((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U))
  1718. {
  1719. IRDA_Receive_IT(hirda);
  1720. return;
  1721. }
  1722. }
  1723. /* If some errors occur */
  1724. cr3its = READ_REG(hirda->Instance->CR3);
  1725. if ((errorflags != 0U)
  1726. && (((cr3its & USART_CR3_EIE) != 0U)
  1727. || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U)))
  1728. {
  1729. /* IRDA parity error interrupt occurred -------------------------------------*/
  1730. if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
  1731. {
  1732. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_PEF);
  1733. hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
  1734. }
  1735. /* IRDA frame error interrupt occurred --------------------------------------*/
  1736. if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1737. {
  1738. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_FEF);
  1739. hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
  1740. }
  1741. /* IRDA noise error interrupt occurred --------------------------------------*/
  1742. if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1743. {
  1744. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_NEF);
  1745. hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
  1746. }
  1747. /* IRDA Over-Run interrupt occurred -----------------------------------------*/
  1748. if (((isrflags & USART_ISR_ORE) != 0U) &&
  1749. (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) || ((cr3its & USART_CR3_EIE) != 0U)))
  1750. {
  1751. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_OREF);
  1752. hirda->ErrorCode |= HAL_IRDA_ERROR_ORE;
  1753. }
  1754. /* Call IRDA Error Call back function if need be --------------------------*/
  1755. if (hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
  1756. {
  1757. /* IRDA in mode Receiver ---------------------------------------------------*/
  1758. if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) && ((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U))
  1759. {
  1760. IRDA_Receive_IT(hirda);
  1761. }
  1762. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1763. consider error as blocking */
  1764. errorcode = hirda->ErrorCode;
  1765. if ((HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR)) ||
  1766. ((errorcode & HAL_IRDA_ERROR_ORE) != 0U))
  1767. {
  1768. /* Blocking error : transfer is aborted
  1769. Set the IRDA state ready to be able to start again the process,
  1770. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1771. IRDA_EndRxTransfer(hirda);
  1772. /* Disable the IRDA DMA Rx request if enabled */
  1773. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1774. {
  1775. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1776. /* Abort the IRDA DMA Rx channel */
  1777. if (hirda->hdmarx != NULL)
  1778. {
  1779. /* Set the IRDA DMA Abort callback :
  1780. will lead to call HAL_IRDA_ErrorCallback() at end of DMA abort procedure */
  1781. hirda->hdmarx->XferAbortCallback = IRDA_DMAAbortOnError;
  1782. /* Abort DMA RX */
  1783. if (HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
  1784. {
  1785. /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
  1786. hirda->hdmarx->XferAbortCallback(hirda->hdmarx);
  1787. }
  1788. }
  1789. else
  1790. {
  1791. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  1792. /* Call registered user error callback */
  1793. hirda->ErrorCallback(hirda);
  1794. #else
  1795. /* Call legacy weak user error callback */
  1796. HAL_IRDA_ErrorCallback(hirda);
  1797. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  1798. }
  1799. }
  1800. else
  1801. {
  1802. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  1803. /* Call registered user error callback */
  1804. hirda->ErrorCallback(hirda);
  1805. #else
  1806. /* Call legacy weak user error callback */
  1807. HAL_IRDA_ErrorCallback(hirda);
  1808. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  1809. }
  1810. }
  1811. else
  1812. {
  1813. /* Non Blocking error : transfer could go on.
  1814. Error is notified to user through user error callback */
  1815. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  1816. /* Call registered user error callback */
  1817. hirda->ErrorCallback(hirda);
  1818. #else
  1819. /* Call legacy weak user error callback */
  1820. HAL_IRDA_ErrorCallback(hirda);
  1821. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  1822. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1823. }
  1824. }
  1825. return;
  1826. } /* End if some error occurs */
  1827. /* IRDA in mode Transmitter ------------------------------------------------*/
  1828. if (((isrflags & USART_ISR_TXE_TXFNF) != 0U) && ((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U))
  1829. {
  1830. IRDA_Transmit_IT(hirda);
  1831. return;
  1832. }
  1833. /* IRDA in mode Transmitter (transmission end) -----------------------------*/
  1834. if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
  1835. {
  1836. IRDA_EndTransmit_IT(hirda);
  1837. return;
  1838. }
  1839. }
  1840. /**
  1841. * @brief Tx Transfer completed callback.
  1842. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1843. * the configuration information for the specified IRDA module.
  1844. * @retval None
  1845. */
  1846. __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
  1847. {
  1848. /* Prevent unused argument(s) compilation warning */
  1849. UNUSED(hirda);
  1850. /* NOTE : This function should not be modified, when the callback is needed,
  1851. the HAL_IRDA_TxCpltCallback can be implemented in the user file.
  1852. */
  1853. }
  1854. /**
  1855. * @brief Tx Half Transfer completed callback.
  1856. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1857. * the configuration information for the specified USART module.
  1858. * @retval None
  1859. */
  1860. __weak void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  1861. {
  1862. /* Prevent unused argument(s) compilation warning */
  1863. UNUSED(hirda);
  1864. /* NOTE : This function should not be modified, when the callback is needed,
  1865. the HAL_IRDA_TxHalfCpltCallback can be implemented in the user file.
  1866. */
  1867. }
  1868. /**
  1869. * @brief Rx Transfer completed callback.
  1870. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1871. * the configuration information for the specified IRDA module.
  1872. * @retval None
  1873. */
  1874. __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
  1875. {
  1876. /* Prevent unused argument(s) compilation warning */
  1877. UNUSED(hirda);
  1878. /* NOTE : This function should not be modified, when the callback is needed,
  1879. the HAL_IRDA_RxCpltCallback can be implemented in the user file.
  1880. */
  1881. }
  1882. /**
  1883. * @brief Rx Half Transfer complete callback.
  1884. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1885. * the configuration information for the specified IRDA module.
  1886. * @retval None
  1887. */
  1888. __weak void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  1889. {
  1890. /* Prevent unused argument(s) compilation warning */
  1891. UNUSED(hirda);
  1892. /* NOTE : This function should not be modified, when the callback is needed,
  1893. the HAL_IRDA_RxHalfCpltCallback can be implemented in the user file.
  1894. */
  1895. }
  1896. /**
  1897. * @brief IRDA error callback.
  1898. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1899. * the configuration information for the specified IRDA module.
  1900. * @retval None
  1901. */
  1902. __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
  1903. {
  1904. /* Prevent unused argument(s) compilation warning */
  1905. UNUSED(hirda);
  1906. /* NOTE : This function should not be modified, when the callback is needed,
  1907. the HAL_IRDA_ErrorCallback can be implemented in the user file.
  1908. */
  1909. }
  1910. /**
  1911. * @brief IRDA Abort Complete callback.
  1912. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1913. * the configuration information for the specified IRDA module.
  1914. * @retval None
  1915. */
  1916. __weak void HAL_IRDA_AbortCpltCallback(IRDA_HandleTypeDef *hirda)
  1917. {
  1918. /* Prevent unused argument(s) compilation warning */
  1919. UNUSED(hirda);
  1920. /* NOTE : This function should not be modified, when the callback is needed,
  1921. the HAL_IRDA_AbortCpltCallback can be implemented in the user file.
  1922. */
  1923. }
  1924. /**
  1925. * @brief IRDA Abort Complete callback.
  1926. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1927. * the configuration information for the specified IRDA module.
  1928. * @retval None
  1929. */
  1930. __weak void HAL_IRDA_AbortTransmitCpltCallback(IRDA_HandleTypeDef *hirda)
  1931. {
  1932. /* Prevent unused argument(s) compilation warning */
  1933. UNUSED(hirda);
  1934. /* NOTE : This function should not be modified, when the callback is needed,
  1935. the HAL_IRDA_AbortTransmitCpltCallback can be implemented in the user file.
  1936. */
  1937. }
  1938. /**
  1939. * @brief IRDA Abort Receive Complete callback.
  1940. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1941. * the configuration information for the specified IRDA module.
  1942. * @retval None
  1943. */
  1944. __weak void HAL_IRDA_AbortReceiveCpltCallback(IRDA_HandleTypeDef *hirda)
  1945. {
  1946. /* Prevent unused argument(s) compilation warning */
  1947. UNUSED(hirda);
  1948. /* NOTE : This function should not be modified, when the callback is needed,
  1949. the HAL_IRDA_AbortReceiveCpltCallback can be implemented in the user file.
  1950. */
  1951. }
  1952. /**
  1953. * @}
  1954. */
  1955. /** @defgroup IRDA_Exported_Functions_Group4 Peripheral State and Error functions
  1956. * @brief IRDA State and Errors functions
  1957. *
  1958. @verbatim
  1959. ==============================================================================
  1960. ##### Peripheral State and Error functions #####
  1961. ==============================================================================
  1962. [..]
  1963. This subsection provides a set of functions allowing to return the State of IrDA
  1964. communication process and also return Peripheral Errors occurred during communication process
  1965. (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state
  1966. of the IRDA peripheral handle.
  1967. (+) HAL_IRDA_GetError() checks in run-time errors that could occur during
  1968. communication.
  1969. @endverbatim
  1970. * @{
  1971. */
  1972. /**
  1973. * @brief Return the IRDA handle state.
  1974. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1975. * the configuration information for the specified IRDA module.
  1976. * @retval HAL state
  1977. */
  1978. HAL_IRDA_StateTypeDef HAL_IRDA_GetState(const IRDA_HandleTypeDef *hirda)
  1979. {
  1980. /* Return IRDA handle state */
  1981. uint32_t temp1;
  1982. uint32_t temp2;
  1983. temp1 = (uint32_t)hirda->gState;
  1984. temp2 = (uint32_t)hirda->RxState;
  1985. return (HAL_IRDA_StateTypeDef)(temp1 | temp2);
  1986. }
  1987. /**
  1988. * @brief Return the IRDA handle error code.
  1989. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1990. * the configuration information for the specified IRDA module.
  1991. * @retval IRDA Error Code
  1992. */
  1993. uint32_t HAL_IRDA_GetError(const IRDA_HandleTypeDef *hirda)
  1994. {
  1995. return hirda->ErrorCode;
  1996. }
  1997. /**
  1998. * @}
  1999. */
  2000. /**
  2001. * @}
  2002. */
  2003. /** @defgroup IRDA_Private_Functions IRDA Private Functions
  2004. * @{
  2005. */
  2006. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2007. /**
  2008. * @brief Initialize the callbacks to their default values.
  2009. * @param hirda IRDA handle.
  2010. * @retval none
  2011. */
  2012. void IRDA_InitCallbacksToDefault(IRDA_HandleTypeDef *hirda)
  2013. {
  2014. /* Init the IRDA Callback settings */
  2015. hirda->TxHalfCpltCallback = HAL_IRDA_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  2016. hirda->TxCpltCallback = HAL_IRDA_TxCpltCallback; /* Legacy weak TxCpltCallback */
  2017. hirda->RxHalfCpltCallback = HAL_IRDA_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  2018. hirda->RxCpltCallback = HAL_IRDA_RxCpltCallback; /* Legacy weak RxCpltCallback */
  2019. hirda->ErrorCallback = HAL_IRDA_ErrorCallback; /* Legacy weak ErrorCallback */
  2020. hirda->AbortCpltCallback = HAL_IRDA_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  2021. hirda->AbortTransmitCpltCallback = HAL_IRDA_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  2022. hirda->AbortReceiveCpltCallback = HAL_IRDA_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
  2023. }
  2024. #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
  2025. /**
  2026. * @brief Configure the IRDA peripheral.
  2027. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  2028. * the configuration information for the specified IRDA module.
  2029. * @retval HAL status
  2030. */
  2031. static HAL_StatusTypeDef IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
  2032. {
  2033. uint32_t tmpreg;
  2034. IRDA_ClockSourceTypeDef clocksource;
  2035. HAL_StatusTypeDef ret = HAL_OK;
  2036. static const uint16_t IRDAPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
  2037. uint32_t pclk;
  2038. /* Check the communication parameters */
  2039. assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));
  2040. assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
  2041. assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
  2042. assert_param(IS_IRDA_TX_RX_MODE(hirda->Init.Mode));
  2043. assert_param(IS_IRDA_PRESCALER(hirda->Init.Prescaler));
  2044. assert_param(IS_IRDA_POWERMODE(hirda->Init.PowerMode));
  2045. assert_param(IS_IRDA_CLOCKPRESCALER(hirda->Init.ClockPrescaler));
  2046. /*-------------------------- USART CR1 Configuration -----------------------*/
  2047. /* Configure the IRDA Word Length, Parity and transfer Mode:
  2048. Set the M bits according to hirda->Init.WordLength value
  2049. Set PCE and PS bits according to hirda->Init.Parity value
  2050. Set TE and RE bits according to hirda->Init.Mode value */
  2051. tmpreg = (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode ;
  2052. MODIFY_REG(hirda->Instance->CR1, IRDA_CR1_FIELDS, tmpreg);
  2053. /*-------------------------- USART CR3 Configuration -----------------------*/
  2054. MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.PowerMode);
  2055. /*--------------------- USART clock PRESC Configuration ----------------*/
  2056. /* Configure
  2057. * - IRDA Clock Prescaler: set PRESCALER according to hirda->Init.ClockPrescaler value */
  2058. MODIFY_REG(hirda->Instance->PRESC, USART_PRESC_PRESCALER, hirda->Init.ClockPrescaler);
  2059. /*-------------------------- USART GTPR Configuration ----------------------*/
  2060. MODIFY_REG(hirda->Instance->GTPR, (uint16_t)USART_GTPR_PSC, (uint16_t)hirda->Init.Prescaler);
  2061. /*-------------------------- USART BRR Configuration -----------------------*/
  2062. IRDA_GETCLOCKSOURCE(hirda, clocksource);
  2063. tmpreg = 0U;
  2064. switch (clocksource)
  2065. {
  2066. case IRDA_CLOCKSOURCE_PCLK1:
  2067. pclk = HAL_RCC_GetPCLK1Freq();
  2068. tmpreg = (uint32_t)(IRDA_DIV_SAMPLING16(pclk, hirda->Init.BaudRate, hirda->Init.ClockPrescaler));
  2069. break;
  2070. case IRDA_CLOCKSOURCE_HSI:
  2071. tmpreg = (uint32_t)(IRDA_DIV_SAMPLING16(HSI_VALUE, hirda->Init.BaudRate, hirda->Init.ClockPrescaler));
  2072. break;
  2073. case IRDA_CLOCKSOURCE_SYSCLK:
  2074. pclk = HAL_RCC_GetSysClockFreq();
  2075. tmpreg = (uint32_t)(IRDA_DIV_SAMPLING16(pclk, hirda->Init.BaudRate, hirda->Init.ClockPrescaler));
  2076. break;
  2077. case IRDA_CLOCKSOURCE_LSE:
  2078. tmpreg = (uint32_t)(IRDA_DIV_SAMPLING16((uint32_t)LSE_VALUE, hirda->Init.BaudRate, hirda->Init.ClockPrescaler));
  2079. break;
  2080. default:
  2081. ret = HAL_ERROR;
  2082. break;
  2083. }
  2084. /* USARTDIV must be greater than or equal to 0d16 */
  2085. if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
  2086. {
  2087. hirda->Instance->BRR = (uint16_t)tmpreg;
  2088. }
  2089. else
  2090. {
  2091. ret = HAL_ERROR;
  2092. }
  2093. return ret;
  2094. }
  2095. /**
  2096. * @brief Check the IRDA Idle State.
  2097. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  2098. * the configuration information for the specified IRDA module.
  2099. * @retval HAL status
  2100. */
  2101. static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda)
  2102. {
  2103. uint32_t tickstart;
  2104. /* Initialize the IRDA ErrorCode */
  2105. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  2106. /* Init tickstart for timeout management */
  2107. tickstart = HAL_GetTick();
  2108. /* Check if the Transmitter is enabled */
  2109. if ((hirda->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  2110. {
  2111. /* Wait until TEACK flag is set */
  2112. if (IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_TEACK, RESET, tickstart, IRDA_TEACK_REACK_TIMEOUT) != HAL_OK)
  2113. {
  2114. /* Timeout occurred */
  2115. return HAL_TIMEOUT;
  2116. }
  2117. }
  2118. /* Check if the Receiver is enabled */
  2119. if ((hirda->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  2120. {
  2121. /* Wait until REACK flag is set */
  2122. if (IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_REACK, RESET, tickstart, IRDA_TEACK_REACK_TIMEOUT) != HAL_OK)
  2123. {
  2124. /* Timeout occurred */
  2125. return HAL_TIMEOUT;
  2126. }
  2127. }
  2128. /* Initialize the IRDA state*/
  2129. hirda->gState = HAL_IRDA_STATE_READY;
  2130. hirda->RxState = HAL_IRDA_STATE_READY;
  2131. /* Process Unlocked */
  2132. __HAL_UNLOCK(hirda);
  2133. return HAL_OK;
  2134. }
  2135. /**
  2136. * @brief Handle IRDA Communication Timeout. It waits
  2137. * until a flag is no longer in the specified status.
  2138. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  2139. * the configuration information for the specified IRDA module.
  2140. * @param Flag Specifies the IRDA flag to check.
  2141. * @param Status The actual Flag status (SET or RESET)
  2142. * @param Tickstart Tick start value
  2143. * @param Timeout Timeout duration
  2144. * @retval HAL status
  2145. */
  2146. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status,
  2147. uint32_t Tickstart, uint32_t Timeout)
  2148. {
  2149. /* Wait until flag is set */
  2150. while ((__HAL_IRDA_GET_FLAG(hirda, Flag) ? SET : RESET) == Status)
  2151. {
  2152. /* Check for the Timeout */
  2153. if (Timeout != HAL_MAX_DELAY)
  2154. {
  2155. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  2156. {
  2157. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
  2158. interrupts for the interrupt process */
  2159. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
  2160. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  2161. hirda->gState = HAL_IRDA_STATE_READY;
  2162. hirda->RxState = HAL_IRDA_STATE_READY;
  2163. /* Process Unlocked */
  2164. __HAL_UNLOCK(hirda);
  2165. return HAL_TIMEOUT;
  2166. }
  2167. }
  2168. }
  2169. return HAL_OK;
  2170. }
  2171. /**
  2172. * @brief End ongoing Tx transfer on IRDA peripheral (following error detection or Transmit completion).
  2173. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  2174. * the configuration information for the specified IRDA module.
  2175. * @retval None
  2176. */
  2177. static void IRDA_EndTxTransfer(IRDA_HandleTypeDef *hirda)
  2178. {
  2179. /* Disable TXEIE and TCIE interrupts */
  2180. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  2181. /* At end of Tx process, restore hirda->gState to Ready */
  2182. hirda->gState = HAL_IRDA_STATE_READY;
  2183. }
  2184. /**
  2185. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  2186. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  2187. * the configuration information for the specified IRDA module.
  2188. * @retval None
  2189. */
  2190. static void IRDA_EndRxTransfer(IRDA_HandleTypeDef *hirda)
  2191. {
  2192. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  2193. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  2194. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  2195. /* At end of Rx process, restore hirda->RxState to Ready */
  2196. hirda->RxState = HAL_IRDA_STATE_READY;
  2197. }
  2198. /**
  2199. * @brief DMA IRDA transmit process complete callback.
  2200. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2201. * the configuration information for the specified DMA module.
  2202. * @retval None
  2203. */
  2204. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2205. {
  2206. IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
  2207. /* DMA Normal mode */
  2208. if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
  2209. {
  2210. hirda->TxXferCount = 0U;
  2211. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  2212. in the IRDA CR3 register */
  2213. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  2214. /* Enable the IRDA Transmit Complete Interrupt */
  2215. SET_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
  2216. }
  2217. /* DMA Circular mode */
  2218. else
  2219. {
  2220. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2221. /* Call registered Tx complete callback */
  2222. hirda->TxCpltCallback(hirda);
  2223. #else
  2224. /* Call legacy weak Tx complete callback */
  2225. HAL_IRDA_TxCpltCallback(hirda);
  2226. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  2227. }
  2228. }
  2229. /**
  2230. * @brief DMA IRDA transmit process half complete callback.
  2231. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2232. * the configuration information for the specified DMA module.
  2233. * @retval None
  2234. */
  2235. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma)
  2236. {
  2237. IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
  2238. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2239. /* Call registered Tx Half complete callback */
  2240. hirda->TxHalfCpltCallback(hirda);
  2241. #else
  2242. /* Call legacy weak Tx complete callback */
  2243. HAL_IRDA_TxHalfCpltCallback(hirda);
  2244. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  2245. }
  2246. /**
  2247. * @brief DMA IRDA receive process complete callback.
  2248. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2249. * the configuration information for the specified DMA module.
  2250. * @retval None
  2251. */
  2252. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2253. {
  2254. IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
  2255. /* DMA Normal mode */
  2256. if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
  2257. {
  2258. hirda->RxXferCount = 0U;
  2259. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  2260. CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  2261. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  2262. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  2263. in the IRDA CR3 register */
  2264. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  2265. /* At end of Rx process, restore hirda->RxState to Ready */
  2266. hirda->RxState = HAL_IRDA_STATE_READY;
  2267. }
  2268. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2269. /* Call registered Rx complete callback */
  2270. hirda->RxCpltCallback(hirda);
  2271. #else
  2272. /* Call legacy weak Rx complete callback */
  2273. HAL_IRDA_RxCpltCallback(hirda);
  2274. #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
  2275. }
  2276. /**
  2277. * @brief DMA IRDA receive process half complete callback.
  2278. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2279. * the configuration information for the specified DMA module.
  2280. * @retval None
  2281. */
  2282. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma)
  2283. {
  2284. IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
  2285. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2286. /*Call registered Rx Half complete callback*/
  2287. hirda->RxHalfCpltCallback(hirda);
  2288. #else
  2289. /* Call legacy weak Rx Half complete callback */
  2290. HAL_IRDA_RxHalfCpltCallback(hirda);
  2291. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  2292. }
  2293. /**
  2294. * @brief DMA IRDA communication error callback.
  2295. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  2296. * the configuration information for the specified DMA module.
  2297. * @retval None
  2298. */
  2299. static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
  2300. {
  2301. IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
  2302. /* Stop IRDA DMA Tx request if ongoing */
  2303. if (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
  2304. {
  2305. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  2306. {
  2307. hirda->TxXferCount = 0U;
  2308. IRDA_EndTxTransfer(hirda);
  2309. }
  2310. }
  2311. /* Stop IRDA DMA Rx request if ongoing */
  2312. if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
  2313. {
  2314. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  2315. {
  2316. hirda->RxXferCount = 0U;
  2317. IRDA_EndRxTransfer(hirda);
  2318. }
  2319. }
  2320. hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
  2321. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2322. /* Call registered user error callback */
  2323. hirda->ErrorCallback(hirda);
  2324. #else
  2325. /* Call legacy weak user error callback */
  2326. HAL_IRDA_ErrorCallback(hirda);
  2327. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  2328. }
  2329. /**
  2330. * @brief DMA IRDA communication abort callback, when initiated by HAL services on Error
  2331. * (To be called at end of DMA Abort procedure following error occurrence).
  2332. * @param hdma DMA handle.
  2333. * @retval None
  2334. */
  2335. static void IRDA_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2336. {
  2337. IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
  2338. hirda->RxXferCount = 0U;
  2339. hirda->TxXferCount = 0U;
  2340. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2341. /* Call registered user error callback */
  2342. hirda->ErrorCallback(hirda);
  2343. #else
  2344. /* Call legacy weak user error callback */
  2345. HAL_IRDA_ErrorCallback(hirda);
  2346. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  2347. }
  2348. /**
  2349. * @brief DMA IRDA Tx communication abort callback, when initiated by user
  2350. * (To be called at end of DMA Tx Abort procedure following user abort request).
  2351. * @note When this callback is executed, User Abort complete call back is called only if no
  2352. * Abort still ongoing for Rx DMA Handle.
  2353. * @param hdma DMA handle.
  2354. * @retval None
  2355. */
  2356. static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  2357. {
  2358. IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
  2359. hirda->hdmatx->XferAbortCallback = NULL;
  2360. /* Check if an Abort process is still ongoing */
  2361. if (hirda->hdmarx != NULL)
  2362. {
  2363. if (hirda->hdmarx->XferAbortCallback != NULL)
  2364. {
  2365. return;
  2366. }
  2367. }
  2368. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2369. hirda->TxXferCount = 0U;
  2370. hirda->RxXferCount = 0U;
  2371. /* Reset errorCode */
  2372. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  2373. /* Clear the Error flags in the ICR register */
  2374. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  2375. /* Restore hirda->gState and hirda->RxState to Ready */
  2376. hirda->gState = HAL_IRDA_STATE_READY;
  2377. hirda->RxState = HAL_IRDA_STATE_READY;
  2378. /* Call user Abort complete callback */
  2379. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2380. /* Call registered Abort complete callback */
  2381. hirda->AbortCpltCallback(hirda);
  2382. #else
  2383. /* Call legacy weak Abort complete callback */
  2384. HAL_IRDA_AbortCpltCallback(hirda);
  2385. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  2386. }
  2387. /**
  2388. * @brief DMA IRDA Rx communication abort callback, when initiated by user
  2389. * (To be called at end of DMA Rx Abort procedure following user abort request).
  2390. * @note When this callback is executed, User Abort complete call back is called only if no
  2391. * Abort still ongoing for Tx DMA Handle.
  2392. * @param hdma DMA handle.
  2393. * @retval None
  2394. */
  2395. static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  2396. {
  2397. IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
  2398. hirda->hdmarx->XferAbortCallback = NULL;
  2399. /* Check if an Abort process is still ongoing */
  2400. if (hirda->hdmatx != NULL)
  2401. {
  2402. if (hirda->hdmatx->XferAbortCallback != NULL)
  2403. {
  2404. return;
  2405. }
  2406. }
  2407. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2408. hirda->TxXferCount = 0U;
  2409. hirda->RxXferCount = 0U;
  2410. /* Reset errorCode */
  2411. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  2412. /* Clear the Error flags in the ICR register */
  2413. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  2414. /* Restore hirda->gState and hirda->RxState to Ready */
  2415. hirda->gState = HAL_IRDA_STATE_READY;
  2416. hirda->RxState = HAL_IRDA_STATE_READY;
  2417. /* Call user Abort complete callback */
  2418. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2419. /* Call registered Abort complete callback */
  2420. hirda->AbortCpltCallback(hirda);
  2421. #else
  2422. /* Call legacy weak Abort complete callback */
  2423. HAL_IRDA_AbortCpltCallback(hirda);
  2424. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  2425. }
  2426. /**
  2427. * @brief DMA IRDA Tx communication abort callback, when initiated by user by a call to
  2428. * HAL_IRDA_AbortTransmit_IT API (Abort only Tx transfer)
  2429. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  2430. * and leads to user Tx Abort Complete callback execution).
  2431. * @param hdma DMA handle.
  2432. * @retval None
  2433. */
  2434. static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2435. {
  2436. IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)(hdma->Parent);
  2437. hirda->TxXferCount = 0U;
  2438. /* Restore hirda->gState to Ready */
  2439. hirda->gState = HAL_IRDA_STATE_READY;
  2440. /* Call user Abort complete callback */
  2441. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2442. /* Call registered Abort Transmit Complete Callback */
  2443. hirda->AbortTransmitCpltCallback(hirda);
  2444. #else
  2445. /* Call legacy weak Abort Transmit Complete Callback */
  2446. HAL_IRDA_AbortTransmitCpltCallback(hirda);
  2447. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  2448. }
  2449. /**
  2450. * @brief DMA IRDA Rx communication abort callback, when initiated by user by a call to
  2451. * HAL_IRDA_AbortReceive_IT API (Abort only Rx transfer)
  2452. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  2453. * and leads to user Rx Abort Complete callback execution).
  2454. * @param hdma DMA handle.
  2455. * @retval None
  2456. */
  2457. static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2458. {
  2459. IRDA_HandleTypeDef *hirda = (IRDA_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2460. hirda->RxXferCount = 0U;
  2461. /* Clear the Error flags in the ICR register */
  2462. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  2463. /* Restore hirda->RxState to Ready */
  2464. hirda->RxState = HAL_IRDA_STATE_READY;
  2465. /* Call user Abort complete callback */
  2466. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2467. /* Call registered Abort Receive Complete Callback */
  2468. hirda->AbortReceiveCpltCallback(hirda);
  2469. #else
  2470. /* Call legacy weak Abort Receive Complete Callback */
  2471. HAL_IRDA_AbortReceiveCpltCallback(hirda);
  2472. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  2473. }
  2474. /**
  2475. * @brief Send an amount of data in interrupt mode.
  2476. * @note Function is called under interruption only, once
  2477. * interruptions have been enabled by HAL_IRDA_Transmit_IT().
  2478. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  2479. * the configuration information for the specified IRDA module.
  2480. * @retval None
  2481. */
  2482. static void IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
  2483. {
  2484. const uint16_t *tmp;
  2485. /* Check that a Tx process is ongoing */
  2486. if (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
  2487. {
  2488. if (hirda->TxXferCount == 0U)
  2489. {
  2490. /* Disable the IRDA Transmit Data Register Empty Interrupt */
  2491. CLEAR_BIT(hirda->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  2492. /* Enable the IRDA Transmit Complete Interrupt */
  2493. SET_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
  2494. }
  2495. else
  2496. {
  2497. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  2498. {
  2499. tmp = (const uint16_t *) hirda->pTxBuffPtr; /* Derogation R.11.3 */
  2500. hirda->Instance->TDR = (uint16_t)(*tmp & 0x01FFU);
  2501. hirda->pTxBuffPtr += 2U;
  2502. }
  2503. else
  2504. {
  2505. hirda->Instance->TDR = (uint8_t)(*hirda->pTxBuffPtr & 0xFFU);
  2506. hirda->pTxBuffPtr++;
  2507. }
  2508. hirda->TxXferCount--;
  2509. }
  2510. }
  2511. }
  2512. /**
  2513. * @brief Wrap up transmission in non-blocking mode.
  2514. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  2515. * the configuration information for the specified IRDA module.
  2516. * @retval None
  2517. */
  2518. static void IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda)
  2519. {
  2520. /* Disable the IRDA Transmit Complete Interrupt */
  2521. CLEAR_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
  2522. /* Tx process is ended, restore hirda->gState to Ready */
  2523. hirda->gState = HAL_IRDA_STATE_READY;
  2524. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2525. /* Call registered Tx complete callback */
  2526. hirda->TxCpltCallback(hirda);
  2527. #else
  2528. /* Call legacy weak Tx complete callback */
  2529. HAL_IRDA_TxCpltCallback(hirda);
  2530. #endif /* USE_HAL_IRDA_REGISTER_CALLBACK */
  2531. }
  2532. /**
  2533. * @brief Receive an amount of data in interrupt mode.
  2534. * @note Function is called under interruption only, once
  2535. * interruptions have been enabled by HAL_IRDA_Receive_IT()
  2536. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  2537. * the configuration information for the specified IRDA module.
  2538. * @retval None
  2539. */
  2540. static void IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
  2541. {
  2542. uint16_t *tmp;
  2543. uint16_t uhMask = hirda->Mask;
  2544. uint16_t uhdata;
  2545. /* Check that a Rx process is ongoing */
  2546. if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
  2547. {
  2548. uhdata = (uint16_t) READ_REG(hirda->Instance->RDR);
  2549. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  2550. {
  2551. tmp = (uint16_t *) hirda->pRxBuffPtr; /* Derogation R.11.3 */
  2552. *tmp = (uint16_t)(uhdata & uhMask);
  2553. hirda->pRxBuffPtr += 2U;
  2554. }
  2555. else
  2556. {
  2557. *hirda->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
  2558. hirda->pRxBuffPtr++;
  2559. }
  2560. hirda->RxXferCount--;
  2561. if (hirda->RxXferCount == 0U)
  2562. {
  2563. /* Disable the IRDA Parity Error Interrupt and RXNE interrupt */
  2564. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  2565. /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  2566. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  2567. /* Rx process is completed, restore hirda->RxState to Ready */
  2568. hirda->RxState = HAL_IRDA_STATE_READY;
  2569. #if (USE_HAL_IRDA_REGISTER_CALLBACKS == 1)
  2570. /* Call registered Rx complete callback */
  2571. hirda->RxCpltCallback(hirda);
  2572. #else
  2573. /* Call legacy weak Rx complete callback */
  2574. HAL_IRDA_RxCpltCallback(hirda);
  2575. #endif /* USE_HAL_IRDA_REGISTER_CALLBACKS */
  2576. }
  2577. }
  2578. else
  2579. {
  2580. /* Clear RXNE interrupt flag */
  2581. __HAL_IRDA_SEND_REQ(hirda, IRDA_RXDATA_FLUSH_REQUEST);
  2582. }
  2583. }
  2584. /**
  2585. * @}
  2586. */
  2587. #endif /* HAL_IRDA_MODULE_ENABLED */
  2588. /**
  2589. * @}
  2590. */
  2591. /**
  2592. * @}
  2593. */