stm32f1xx_hal_irda.c 93 KB

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