You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_irda.c 90KB

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