stm32l4xx_hal_irda.c 105 KB

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