stm32f4xx_hal_irda.c 93 KB

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