stm32f1xx_hal_irda.c 93 KB

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