stm32f7xx_hal_irda.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_irda.c
  4. * @author MCD Application Team
  5. * @brief IRDA HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the IrDA (Infrared Data Association) Peripheral
  8. * (IRDA)
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral State and Errors functions
  12. * + Peripheral Control functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The IRDA HAL driver can be used as follows:
  20. (#) Declare a IRDA_HandleTypeDef handle structure (eg. IRDA_HandleTypeDef hirda).
  21. (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API
  22. in setting the associated USART or UART in IRDA mode:
  23. (++) Enable the USARTx/UARTx interface clock.
  24. (++) USARTx/UARTx pins configuration:
  25. (+++) Enable the clock for the USARTx/UARTx GPIOs.
  26. (+++) Configure these USARTx/UARTx pins (TX as alternate function pull-up, RX as alternate function Input).
  27. (++) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
  28. and HAL_IRDA_Receive_IT() APIs):
  29. (+++) Configure the USARTx/UARTx interrupt priority.
  30. (+++) Enable the NVIC USARTx/UARTx IRQ handle.
  31. (+++) The specific IRDA interrupts (Transmission complete interrupt,
  32. RXNE interrupt and Error Interrupts) will be managed using the macros
  33. __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
  34. (++) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
  35. and HAL_IRDA_Receive_DMA() APIs):
  36. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  37. (+++) Enable the DMAx interface clock.
  38. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  39. (+++) Configure the DMA Tx/Rx channel.
  40. (+++) Associate the initialized DMA handle to the IRDA DMA Tx/Rx handle.
  41. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  42. (#) Program the Baud Rate, Word Length, Parity, IrDA Mode, Prescaler
  43. and Mode(Receiver/Transmitter) in the hirda Init structure.
  44. (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
  45. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  46. by calling the customized HAL_IRDA_MspInit() API.
  47. -@@- The specific IRDA interrupts (Transmission complete interrupt,
  48. RXNE interrupt and Error Interrupts) will be managed using the macros
  49. __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
  50. (#) Three operation modes are available within this driver :
  51. *** Polling mode IO operation ***
  52. =================================
  53. [..]
  54. (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
  55. (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
  56. *** Interrupt mode IO operation ***
  57. ===================================
  58. [..]
  59. (+) Send an amount of data in non blocking mode using HAL_IRDA_Transmit_IT()
  60. (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
  61. add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
  62. (+) Receive an amount of data in non blocking mode using HAL_IRDA_Receive_IT()
  63. (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
  64. add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
  65. (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
  66. add his own code by customization of function pointer HAL_IRDA_ErrorCallback
  67. *** DMA mode IO operation ***
  68. =============================
  69. [..]
  70. (+) Send an amount of data in non blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
  71. (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
  72. add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
  73. (+) Receive an amount of data in non blocking mode (DMA) using HAL_IRDA_Receive_DMA()
  74. (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
  75. add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
  76. (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
  77. add his own code by customization of function pointer HAL_IRDA_ErrorCallback
  78. *** IRDA HAL driver macros list ***
  79. ===================================
  80. [..]
  81. Below the list of most used macros in IRDA HAL driver.
  82. (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
  83. (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
  84. (+) __HAL_IRDA_GET_FLAG : Checks whether the specified IRDA flag is set or not
  85. (+) __HAL_IRDA_CLEAR_FLAG : Clears the specified IRDA pending flag
  86. (+) __HAL_IRDA_ENABLE_IT: Enables the specified IRDA interrupt
  87. (+) __HAL_IRDA_DISABLE_IT: Disables the specified IRDA interrupt
  88. [..]
  89. (@) You can refer to the IRDA HAL driver header file for more useful macros
  90. @endverbatim
  91. ******************************************************************************
  92. * @attention
  93. *
  94. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  95. *
  96. * Redistribution and use in source and binary forms, with or without modification,
  97. * are permitted provided that the following conditions are met:
  98. * 1. Redistributions of source code must retain the above copyright notice,
  99. * this list of conditions and the following disclaimer.
  100. * 2. Redistributions in binary form must reproduce the above copyright notice,
  101. * this list of conditions and the following disclaimer in the documentation
  102. * and/or other materials provided with the distribution.
  103. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  104. * may be used to endorse or promote products derived from this software
  105. * without specific prior written permission.
  106. *
  107. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  108. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  109. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  110. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  111. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  112. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  113. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  114. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  115. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  116. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  117. *
  118. ******************************************************************************
  119. */
  120. /* Includes ------------------------------------------------------------------*/
  121. #include "stm32f7xx_hal.h"
  122. /** @addtogroup STM32F7xx_HAL_Driver
  123. * @{
  124. */
  125. /** @defgroup IRDA IRDA
  126. * @brief HAL IRDA module driver
  127. * @{
  128. */
  129. #ifdef HAL_IRDA_MODULE_ENABLED
  130. /* Private typedef -----------------------------------------------------------*/
  131. /* Private define ------------------------------------------------------------*/
  132. /** @defgroup IRDA_Private_Constants IRDA Private Constants
  133. * @{
  134. */
  135. #define IRDA_TEACK_REACK_TIMEOUT 1000U
  136. #define HAL_IRDA_TXDMA_TIMEOUTVALUE 22000U
  137. #define IRDA_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE \
  138. | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)) /*!< UART or USART CR1 fields of parameters set by IRDA_SetConfig API */
  139. /**
  140. * @}
  141. */
  142. /* Private macros ------------------------------------------------------------*/
  143. /* Private variables ---------------------------------------------------------*/
  144. /* Private function prototypes -----------------------------------------------*/
  145. /** @addtogroup IRDA_Private_Functions
  146. * @{
  147. */
  148. static HAL_StatusTypeDef IRDA_SetConfig(IRDA_HandleTypeDef *hirda);
  149. static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda);
  150. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  151. static void IRDA_EndTxTransfer(IRDA_HandleTypeDef *hirda);
  152. static void IRDA_EndRxTransfer(IRDA_HandleTypeDef *hirda);
  153. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  154. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma);
  155. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  156. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma);
  157. static void IRDA_DMAError(DMA_HandleTypeDef *hdma);
  158. static void IRDA_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  159. static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  160. static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  161. static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  162. static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  163. static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
  164. static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda);
  165. static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
  166. /**
  167. * @}
  168. */
  169. /* Exported functions --------------------------------------------------------*/
  170. /** @defgroup IRDA_Exported_Functions IRDA Exported Functions
  171. * @{
  172. */
  173. /** @defgroup IRDA_Exported_Functions_Group1 IrDA Initialization and de-initialization functions
  174. * @brief Initialization and Configuration functions
  175. *
  176. @verbatim
  177. ===============================================================================
  178. ##### Initialization and Configuration functions #####
  179. ===============================================================================
  180. [..]
  181. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  182. in IrDA mode.
  183. (+) For the asynchronous mode only these parameters can be configured:
  184. (++) BaudRate
  185. (++) WordLength
  186. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  187. in the data register is transmitted but is changed by the parity bit.
  188. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  189. please refer to Reference manual for possible IRDA frame formats.
  190. (++) Prescaler: A pulse of width less than two and greater than one PSC period(s) may or may
  191. not be rejected. The receiver set up time should be managed by software. The IrDA physical layer
  192. specification specifies a minimum of 10 ms delay between transmission and
  193. reception (IrDA is a half duplex protocol).
  194. (++) Mode: Receiver/transmitter modes
  195. (++) IrDAMode: the IrDA can operate in the Normal mode or in the Low power mode.
  196. [..]
  197. The HAL_IRDA_Init() API follows IRDA configuration procedures (details for the procedures
  198. are available in reference manual).
  199. @endverbatim
  200. * @{
  201. */
  202. /**
  203. * @brief Initialize the IRDA mode according to the specified
  204. * parameters in the IRDA_InitTypeDef and initialize the associated handle.
  205. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  206. * the configuration information for the specified IRDA module.
  207. * @retval HAL status
  208. */
  209. HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
  210. {
  211. /* Check the IRDA handle allocation */
  212. if(hirda == NULL)
  213. {
  214. return HAL_ERROR;
  215. }
  216. /* Check the USART/UART associated to the IRDA handle */
  217. assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  218. if(hirda->gState == HAL_IRDA_STATE_RESET)
  219. {
  220. /* Allocate lock resource and initialize it */
  221. hirda->Lock = HAL_UNLOCKED;
  222. /* Init the low level hardware : GPIO, CLOCK */
  223. HAL_IRDA_MspInit(hirda);
  224. }
  225. hirda->gState = HAL_IRDA_STATE_BUSY;
  226. /* Disable the Peripheral to update the configuration registers */
  227. __HAL_IRDA_DISABLE(hirda);
  228. /* Set the IRDA Communication parameters */
  229. if (IRDA_SetConfig(hirda) == HAL_ERROR)
  230. {
  231. return HAL_ERROR;
  232. }
  233. /* In IRDA mode, the following bits must be kept cleared:
  234. - LINEN, STOP and CLKEN bits in the USART_CR2 register,
  235. - SCEN and HDSEL bits in the USART_CR3 register.*/
  236. CLEAR_BIT(hirda->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN | USART_CR2_STOP));
  237. CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
  238. /* set the UART/USART in IRDA mode */
  239. hirda->Instance->CR3 |= USART_CR3_IREN;
  240. /* Enable the Peripheral */
  241. __HAL_IRDA_ENABLE(hirda);
  242. /* TEACK and/or REACK to check before moving hirda->gState and hirda->RxState to Ready */
  243. return (IRDA_CheckIdleState(hirda));
  244. }
  245. /**
  246. * @brief DeInitialize the IRDA peripheral.
  247. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  248. * the configuration information for the specified IRDA module.
  249. * @retval HAL status
  250. */
  251. HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
  252. {
  253. /* Check the IRDA handle allocation */
  254. if(hirda == NULL)
  255. {
  256. return HAL_ERROR;
  257. }
  258. /* Check the USART/UART associated to the IRDA handle */
  259. assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  260. hirda->gState = HAL_IRDA_STATE_BUSY;
  261. /* DeInit the low level hardware */
  262. HAL_IRDA_MspDeInit(hirda);
  263. /* Disable the Peripheral */
  264. __HAL_IRDA_DISABLE(hirda);
  265. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  266. hirda->gState = HAL_IRDA_STATE_RESET;
  267. hirda->RxState = HAL_IRDA_STATE_RESET;
  268. /* Release Lock */
  269. __HAL_UNLOCK(hirda);
  270. return HAL_OK;
  271. }
  272. /**
  273. * @brief Initialize the IRDA MSP.
  274. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  275. * the configuration information for the specified IRDA module.
  276. * @retval None
  277. */
  278. __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
  279. {
  280. /* Prevent unused argument(s) compilation warning */
  281. UNUSED(hirda);
  282. /* NOTE: This function should not be modified, when the callback is needed,
  283. the HAL_IRDA_MspInit can be implemented in the user file
  284. */
  285. }
  286. /**
  287. * @brief DeInitialize the IRDA MSP.
  288. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  289. * the configuration information for the specified IRDA module.
  290. * @retval None
  291. */
  292. __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
  293. {
  294. /* Prevent unused argument(s) compilation warning */
  295. UNUSED(hirda);
  296. /* NOTE: This function should not be modified, when the callback is needed,
  297. the HAL_IRDA_MspDeInit can be implemented in the user file
  298. */
  299. }
  300. /**
  301. * @}
  302. */
  303. /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
  304. * @brief IRDA Transmit and Receive functions
  305. *
  306. @verbatim
  307. ===============================================================================
  308. ##### IO operation functions #####
  309. ===============================================================================
  310. [..]
  311. This subsection provides a set of functions allowing to manage the IRDA data transfers.
  312. [..]
  313. IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
  314. on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
  315. is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
  316. While receiving data, transmission should be avoided as the data to be transmitted
  317. could be corrupted.
  318. (#) There are two modes of transfer:
  319. (++) Blocking mode: the communication is performed in polling mode.
  320. The HAL status of all data processing is returned by the same function
  321. after finishing transfer.
  322. (++) Non-Blocking mode: the communication is performed using Interrupts
  323. or DMA, these API's return the HAL status.
  324. The end of the data processing will be indicated through the
  325. dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
  326. using DMA mode.
  327. The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
  328. will be executed respectively at the end of the Transmit or Receive process
  329. The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
  330. (#) Blocking mode APIs are :
  331. (++) HAL_IRDA_Transmit()
  332. (++) HAL_IRDA_Receive()
  333. (#) Non-Blocking mode APIs with Interrupt are :
  334. (++) HAL_IRDA_Transmit_IT()
  335. (++) HAL_IRDA_Receive_IT()
  336. (++) HAL_IRDA_IRQHandler()
  337. (++) IRDA_Transmit_IT()
  338. (++) IRDA_Receive_IT()
  339. (#) Non-Blocking mode functions with DMA are :
  340. (++) HAL_IRDA_Transmit_DMA()
  341. (++) HAL_IRDA_Receive_DMA()
  342. (++) HAL_IRDA_DMAPause()
  343. (++) HAL_IRDA_DMAResume()
  344. (++) HAL_IRDA_DMAStop()
  345. (#) A set of Transfer Complete Callbacks are provided in Non-Blocking mode:
  346. (++) HAL_IRDA_TxHalfCpltCallback()
  347. (++) HAL_IRDA_TxCpltCallback()
  348. (++) HAL_IRDA_RxHalfCpltCallback()
  349. (++) HAL_IRDA_RxCpltCallback()
  350. (++) HAL_IRDA_ErrorCallback()
  351. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  352. (++) HAL_IRDA_Abort()
  353. (++) HAL_IRDA_AbortTransmit()
  354. (++) HAL_IRDA_AbortReceive()
  355. (++) HAL_IRDA_Abort_IT()
  356. (++) HAL_IRDA_AbortTransmit_IT()
  357. (++) HAL_IRDA_AbortReceive_IT()
  358. (#) For Abort services based on interrupts (HAL_IRDA_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  359. (++) HAL_IRDA_AbortCpltCallback()
  360. (++) HAL_IRDA_AbortTransmitCpltCallback()
  361. (++) HAL_IRDA_AbortReceiveCpltCallback()
  362. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  363. Errors are handled as follows :
  364. (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  365. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  366. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  367. and HAL_IRDA_ErrorCallback() user callback is executed. Transfer is kept ongoing on IRDA side.
  368. If user wants to abort it, Abort services should be called by user.
  369. (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  370. This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
  371. Error code is set to allow user to identify error type, and HAL_IRDA_ErrorCallback() user callback is executed.
  372. @endverbatim
  373. * @{
  374. */
  375. /**
  376. * @brief Send an amount of data in blocking mode.
  377. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  378. * the configuration information for the specified IRDA module.
  379. * @param pData Pointer to data buffer.
  380. * @param Size Amount of data to be sent.
  381. * @param Timeout Specify timeout value.
  382. * @retval HAL status
  383. */
  384. HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  385. {
  386. uint16_t* tmp;
  387. uint32_t tickstart = 0U;
  388. /* Check that a Tx process is not already ongoing */
  389. if(hirda->gState == HAL_IRDA_STATE_READY)
  390. {
  391. if((pData == NULL) || (Size == 0))
  392. {
  393. return HAL_ERROR;
  394. }
  395. /* Process Locked */
  396. __HAL_LOCK(hirda);
  397. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  398. hirda->gState = HAL_IRDA_STATE_BUSY_TX;
  399. /* Init tickstart for timeout managment*/
  400. tickstart = HAL_GetTick();
  401. hirda->TxXferSize = Size;
  402. hirda->TxXferCount = Size;
  403. while(hirda->TxXferCount > 0)
  404. {
  405. hirda->TxXferCount--;
  406. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  407. {
  408. return HAL_TIMEOUT;
  409. }
  410. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  411. {
  412. tmp = (uint16_t*) pData;
  413. hirda->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  414. pData += 2;
  415. }
  416. else
  417. {
  418. hirda->Instance->TDR = (*pData++ & (uint8_t)0xFF);
  419. }
  420. }
  421. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  422. {
  423. return HAL_TIMEOUT;
  424. }
  425. /* At end of Tx process, restore hirda->gState to Ready */
  426. hirda->gState = HAL_IRDA_STATE_READY;
  427. /* Process Unlocked */
  428. __HAL_UNLOCK(hirda);
  429. return HAL_OK;
  430. }
  431. else
  432. {
  433. return HAL_BUSY;
  434. }
  435. }
  436. /**
  437. * @brief Receive an amount of data in blocking mode.
  438. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  439. * the configuration information for the specified IRDA module.
  440. * @param pData Pointer to data buffer.
  441. * @param Size Amount of data to be received.
  442. * @param Timeout Specify timeout value.
  443. * @retval HAL status
  444. */
  445. HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  446. {
  447. uint16_t* tmp;
  448. uint16_t uhMask;
  449. uint32_t tickstart = 0;
  450. /* Check that a Rx process is not already ongoing */
  451. if(hirda->RxState == HAL_IRDA_STATE_READY)
  452. {
  453. if((pData == NULL) || (Size == 0))
  454. {
  455. return HAL_ERROR;
  456. }
  457. /* Process Locked */
  458. __HAL_LOCK(hirda);
  459. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  460. hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
  461. /* Init tickstart for timeout managment*/
  462. tickstart = HAL_GetTick();
  463. hirda->RxXferSize = Size;
  464. hirda->RxXferCount = Size;
  465. /* Computation of the mask to apply to RDR register
  466. of the UART associated to the IRDA */
  467. IRDA_MASK_COMPUTATION(hirda);
  468. uhMask = hirda->Mask;
  469. /* Check data remaining to be received */
  470. while(hirda->RxXferCount > 0)
  471. {
  472. hirda->RxXferCount--;
  473. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  474. {
  475. return HAL_TIMEOUT;
  476. }
  477. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  478. {
  479. tmp = (uint16_t*) pData ;
  480. *tmp = (uint16_t)(hirda->Instance->RDR & uhMask);
  481. pData +=2;
  482. }
  483. else
  484. {
  485. *pData++ = (uint8_t)(hirda->Instance->RDR & (uint8_t)uhMask);
  486. }
  487. }
  488. /* At end of Rx process, restore hirda->RxState to Ready */
  489. hirda->RxState = HAL_IRDA_STATE_READY;
  490. /* Process Unlocked */
  491. __HAL_UNLOCK(hirda);
  492. return HAL_OK;
  493. }
  494. else
  495. {
  496. return HAL_BUSY;
  497. }
  498. }
  499. /**
  500. * @brief Send an amount of data in interrupt mode.
  501. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  502. * the configuration information for the specified IRDA module.
  503. * @param pData Pointer to data buffer.
  504. * @param Size Amount of data to be sent.
  505. * @retval HAL status
  506. */
  507. HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  508. {
  509. /* Check that a Tx process is not already ongoing */
  510. if(hirda->gState == HAL_IRDA_STATE_READY)
  511. {
  512. if((pData == NULL) || (Size == 0))
  513. {
  514. return HAL_ERROR;
  515. }
  516. /* Process Locked */
  517. __HAL_LOCK(hirda);
  518. hirda->pTxBuffPtr = pData;
  519. hirda->TxXferSize = Size;
  520. hirda->TxXferCount = Size;
  521. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  522. hirda->gState = HAL_IRDA_STATE_BUSY_TX;
  523. /* Process Unlocked */
  524. __HAL_UNLOCK(hirda);
  525. /* Enable the IRDA Transmit Data Register Empty Interrupt */
  526. SET_BIT(hirda->Instance->CR1, USART_CR1_TXEIE);
  527. return HAL_OK;
  528. }
  529. else
  530. {
  531. return HAL_BUSY;
  532. }
  533. }
  534. /**
  535. * @brief Receive an amount of data in interrupt mode.
  536. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  537. * the configuration information for the specified IRDA module.
  538. * @param pData Pointer to data buffer.
  539. * @param Size Amount of data to be received.
  540. * @retval HAL status
  541. */
  542. HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  543. {
  544. /* Check that a Rx process is not already ongoing */
  545. if(hirda->RxState == HAL_IRDA_STATE_READY)
  546. {
  547. if((pData == NULL) || (Size == 0))
  548. {
  549. return HAL_ERROR;
  550. }
  551. /* Process Locked */
  552. __HAL_LOCK(hirda);
  553. hirda->pRxBuffPtr = pData;
  554. hirda->RxXferSize = Size;
  555. hirda->RxXferCount = Size;
  556. /* Computation of the mask to apply to the RDR register
  557. of the UART associated to the IRDA */
  558. IRDA_MASK_COMPUTATION(hirda);
  559. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  560. hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
  561. /* Process Unlocked */
  562. __HAL_UNLOCK(hirda);
  563. /* Enable the IRDA Parity Error and Data Register not empty Interrupts */
  564. SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
  565. /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  566. SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  567. return HAL_OK;
  568. }
  569. else
  570. {
  571. return HAL_BUSY;
  572. }
  573. }
  574. /**
  575. * @brief Send an amount of data in DMA mode.
  576. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  577. * the configuration information for the specified IRDA module.
  578. * @param pData pointer to data buffer.
  579. * @param Size amount of data to be sent.
  580. * @retval HAL status
  581. */
  582. HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  583. {
  584. /* Check that a Tx process is not already ongoing */
  585. if(hirda->gState == HAL_IRDA_STATE_READY)
  586. {
  587. if((pData == NULL) || (Size == 0))
  588. {
  589. return HAL_ERROR;
  590. }
  591. /* Process Locked */
  592. __HAL_LOCK(hirda);
  593. hirda->pTxBuffPtr = pData;
  594. hirda->TxXferSize = Size;
  595. hirda->TxXferCount = Size;
  596. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  597. hirda->gState = HAL_IRDA_STATE_BUSY_TX;
  598. /* Set the IRDA DMA transfer complete callback */
  599. hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
  600. /* Set the IRDA DMA half transfer complete callback */
  601. hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;
  602. /* Set the DMA error callback */
  603. hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
  604. /* Set the DMA abort callback */
  605. hirda->hdmatx->XferAbortCallback = NULL;
  606. /* Enable the IRDA transmit DMA channel */
  607. HAL_DMA_Start_IT(hirda->hdmatx, (uint32_t)hirda->pTxBuffPtr, (uint32_t)&hirda->Instance->TDR, Size);
  608. /* Clear the TC flag in the ICR register */
  609. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_TCF);
  610. /* Process Unlocked */
  611. __HAL_UNLOCK(hirda);
  612. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  613. in the USART CR3 register */
  614. SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  615. return HAL_OK;
  616. }
  617. else
  618. {
  619. return HAL_BUSY;
  620. }
  621. }
  622. /**
  623. * @brief Receive an amount of data in DMA mode.
  624. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  625. * the configuration information for the specified IRDA module.
  626. * @param pData Pointer to data buffer.
  627. * @param Size Amount of data to be received.
  628. * @note When the IRDA parity is enabled (PCE = 1), the received data contains
  629. * the parity bit (MSB position).
  630. * @retval HAL status
  631. */
  632. HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  633. {
  634. /* Check that a Rx process is not already ongoing */
  635. if(hirda->RxState == HAL_IRDA_STATE_READY)
  636. {
  637. if((pData == NULL) || (Size == 0))
  638. {
  639. return HAL_ERROR;
  640. }
  641. /* Process Locked */
  642. __HAL_LOCK(hirda);
  643. hirda->pRxBuffPtr = pData;
  644. hirda->RxXferSize = Size;
  645. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  646. hirda->RxState = HAL_IRDA_STATE_BUSY_RX;
  647. /* Set the IRDA DMA transfer complete callback */
  648. hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
  649. /* Set the IRDA DMA half transfer complete callback */
  650. hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;
  651. /* Set the DMA error callback */
  652. hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
  653. /* Set the DMA abort callback */
  654. hirda->hdmarx->XferAbortCallback = NULL;
  655. /* Enable the DMA channel */
  656. HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->RDR, (uint32_t)hirda->pRxBuffPtr, Size);
  657. /* Process Unlocked */
  658. __HAL_UNLOCK(hirda);
  659. /* Enable the UART Parity Error Interrupt */
  660. SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  661. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  662. SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  663. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  664. in the USART CR3 register */
  665. SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  666. return HAL_OK;
  667. }
  668. else
  669. {
  670. return HAL_BUSY;
  671. }
  672. }
  673. /**
  674. * @brief Pause the DMA Transfer.
  675. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  676. * the configuration information for the specified IRDA module.
  677. * @retval HAL status
  678. */
  679. HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda)
  680. {
  681. /* Process Locked */
  682. __HAL_LOCK(hirda);
  683. if ((hirda->gState == HAL_IRDA_STATE_BUSY_TX) &&
  684. (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT)))
  685. {
  686. /* Disable the IRDA DMA Tx request */
  687. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  688. }
  689. if ((hirda->RxState == HAL_IRDA_STATE_BUSY_RX) &&
  690. (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR)))
  691. {
  692. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  693. CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  694. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  695. /* Disable the IRDA DMA Rx request */
  696. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  697. }
  698. /* Process Unlocked */
  699. __HAL_UNLOCK(hirda);
  700. return HAL_OK;
  701. }
  702. /**
  703. * @brief Resume the DMA Transfer.
  704. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  705. * the configuration information for the specified UART module.
  706. * @retval HAL status
  707. */
  708. HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda)
  709. {
  710. /* Process Locked */
  711. __HAL_LOCK(hirda);
  712. if(hirda->gState == HAL_IRDA_STATE_BUSY_TX)
  713. {
  714. /* Enable the IRDA DMA Tx request */
  715. SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  716. }
  717. if(hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
  718. {
  719. /* Clear the Overrun flag before resuming the Rx transfer*/
  720. __HAL_IRDA_CLEAR_OREFLAG(hirda);
  721. /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
  722. SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  723. SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  724. /* Enable the IRDA DMA Rx request */
  725. SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  726. }
  727. /* Process Unlocked */
  728. __HAL_UNLOCK(hirda);
  729. return HAL_OK;
  730. }
  731. /**
  732. * @brief Stop the DMA Transfer.
  733. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  734. * the configuration information for the specified UART module.
  735. * @retval HAL status
  736. */
  737. HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda)
  738. {
  739. /* The Lock is not implemented on this API to allow the user application
  740. to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback() /
  741. HAL_IRDA_TxHalfCpltCallback / HAL_IRDA_RxHalfCpltCallback:
  742. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  743. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  744. the stream and the corresponding call back is executed. */
  745. /* Stop IRDA DMA Tx request if ongoing */
  746. if ((hirda->gState == HAL_IRDA_STATE_BUSY_TX) &&
  747. (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT)))
  748. {
  749. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  750. /* Abort the IRDA DMA Tx channel */
  751. if(hirda->hdmatx != NULL)
  752. {
  753. HAL_DMA_Abort(hirda->hdmatx);
  754. }
  755. IRDA_EndTxTransfer(hirda);
  756. }
  757. /* Stop IRDA DMA Rx request if ongoing */
  758. if ((hirda->RxState == HAL_IRDA_STATE_BUSY_RX) &&
  759. (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR)))
  760. {
  761. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  762. /* Abort the IRDA DMA Rx channel */
  763. if(hirda->hdmarx != NULL)
  764. {
  765. HAL_DMA_Abort(hirda->hdmarx);
  766. }
  767. IRDA_EndRxTransfer(hirda);
  768. }
  769. return HAL_OK;
  770. }
  771. /**
  772. * @brief Abort ongoing transfers (blocking mode).
  773. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  774. * the configuration information for the specified UART module.
  775. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  776. * This procedure performs following operations :
  777. * - Disable IRDA Interrupts (Tx and Rx)
  778. * - Disable the DMA transfer in the peripheral register (if enabled)
  779. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  780. * - Set handle State to READY
  781. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  782. * @retval HAL status
  783. */
  784. HAL_StatusTypeDef HAL_IRDA_Abort(IRDA_HandleTypeDef *hirda)
  785. {
  786. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  787. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  788. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  789. /* Disable the IRDA DMA Tx request if enabled */
  790. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  791. {
  792. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  793. /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
  794. if(hirda->hdmatx != NULL)
  795. {
  796. /* Set the IRDA DMA Abort callback to Null.
  797. No call back execution at end of DMA abort procedure */
  798. hirda->hdmatx->XferAbortCallback = NULL;
  799. HAL_DMA_Abort(hirda->hdmatx);
  800. }
  801. }
  802. /* Disable the IRDA DMA Rx request if enabled */
  803. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  804. {
  805. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  806. /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
  807. if(hirda->hdmarx != NULL)
  808. {
  809. /* Set the IRDA DMA Abort callback to Null.
  810. No call back execution at end of DMA abort procedure */
  811. hirda->hdmarx->XferAbortCallback = NULL;
  812. HAL_DMA_Abort(hirda->hdmarx);
  813. }
  814. }
  815. /* Reset Tx and Rx transfer counters */
  816. hirda->TxXferCount = 0;
  817. hirda->RxXferCount = 0;
  818. /* Clear the Error flags in the ICR register */
  819. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  820. /* Restore hirda->gState and hirda->RxState to Ready */
  821. hirda->gState = HAL_IRDA_STATE_READY;
  822. hirda->RxState = HAL_IRDA_STATE_READY;
  823. /* Reset Handle ErrorCode to No Error */
  824. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  825. return HAL_OK;
  826. }
  827. /**
  828. * @brief Abort ongoing Transmit transfer (blocking mode).
  829. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  830. * the configuration information for the specified UART module.
  831. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  832. * This procedure performs following operations :
  833. * - Disable IRDA Interrupts (Tx)
  834. * - Disable the DMA transfer in the peripheral register (if enabled)
  835. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  836. * - Set handle State to READY
  837. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  838. * @retval HAL status
  839. */
  840. HAL_StatusTypeDef HAL_IRDA_AbortTransmit(IRDA_HandleTypeDef *hirda)
  841. {
  842. /* Disable TXEIE and TCIE interrupts */
  843. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  844. /* Disable the IRDA DMA Tx request if enabled */
  845. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  846. {
  847. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  848. /* Abort the IRDA DMA Tx channel : use blocking DMA Abort API (no callback) */
  849. if(hirda->hdmatx != NULL)
  850. {
  851. /* Set the IRDA DMA Abort callback to Null.
  852. No call back execution at end of DMA abort procedure */
  853. hirda->hdmatx->XferAbortCallback = NULL;
  854. HAL_DMA_Abort(hirda->hdmatx);
  855. }
  856. }
  857. /* Reset Tx transfer counter */
  858. hirda->TxXferCount = 0;
  859. /* Restore hirda->gState to Ready */
  860. hirda->gState = HAL_IRDA_STATE_READY;
  861. return HAL_OK;
  862. }
  863. /**
  864. * @brief Abort ongoing Receive transfer (blocking mode).
  865. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  866. * the configuration information for the specified UART module.
  867. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  868. * This procedure performs following operations :
  869. * - Disable IRDA Interrupts (Rx)
  870. * - Disable the DMA transfer in the peripheral register (if enabled)
  871. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  872. * - Set handle State to READY
  873. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  874. * @retval HAL status
  875. */
  876. HAL_StatusTypeDef HAL_IRDA_AbortReceive(IRDA_HandleTypeDef *hirda)
  877. {
  878. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  879. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  880. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  881. /* Disable the IRDA DMA Rx request if enabled */
  882. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  883. {
  884. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  885. /* Abort the IRDA DMA Rx channel : use blocking DMA Abort API (no callback) */
  886. if(hirda->hdmarx != NULL)
  887. {
  888. /* Set the IRDA DMA Abort callback to Null.
  889. No call back execution at end of DMA abort procedure */
  890. hirda->hdmarx->XferAbortCallback = NULL;
  891. HAL_DMA_Abort(hirda->hdmarx);
  892. }
  893. }
  894. /* Reset Rx transfer counter */
  895. hirda->RxXferCount = 0;
  896. /* Clear the Error flags in the ICR register */
  897. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  898. /* Restore hirda->RxState to Ready */
  899. hirda->RxState = HAL_IRDA_STATE_READY;
  900. return HAL_OK;
  901. }
  902. /**
  903. * @brief Abort ongoing transfers (Interrupt mode).
  904. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  905. * the configuration information for the specified UART module.
  906. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  907. * This procedure performs following operations :
  908. * - Disable IRDA Interrupts (Tx and Rx)
  909. * - Disable the DMA transfer in the peripheral register (if enabled)
  910. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  911. * - Set handle State to READY
  912. * - At abort completion, call user abort complete callback
  913. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  914. * considered as completed only when user abort complete callback is executed (not when exiting function).
  915. * @retval HAL status
  916. */
  917. HAL_StatusTypeDef HAL_IRDA_Abort_IT(IRDA_HandleTypeDef *hirda)
  918. {
  919. uint32_t abortcplt = 1;
  920. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  921. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  922. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  923. /* If DMA Tx and/or DMA Rx Handles are associated to IRDA Handle, DMA Abort complete callbacks should be initialised
  924. before any call to DMA Abort functions */
  925. /* DMA Tx Handle is valid */
  926. if(hirda->hdmatx != NULL)
  927. {
  928. /* Set DMA Abort Complete callback if IRDA DMA Tx request if enabled.
  929. Otherwise, set it to NULL */
  930. if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  931. {
  932. hirda->hdmatx->XferAbortCallback = IRDA_DMATxAbortCallback;
  933. }
  934. else
  935. {
  936. hirda->hdmatx->XferAbortCallback = NULL;
  937. }
  938. }
  939. /* DMA Rx Handle is valid */
  940. if(hirda->hdmarx != NULL)
  941. {
  942. /* Set DMA Abort Complete callback if IRDA DMA Rx request if enabled.
  943. Otherwise, set it to NULL */
  944. if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  945. {
  946. hirda->hdmarx->XferAbortCallback = IRDA_DMARxAbortCallback;
  947. }
  948. else
  949. {
  950. hirda->hdmarx->XferAbortCallback = NULL;
  951. }
  952. }
  953. /* Disable the IRDA DMA Tx request if enabled */
  954. if(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  955. {
  956. /* Disable DMA Tx at UART level */
  957. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  958. /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
  959. if(hirda->hdmatx != NULL)
  960. {
  961. /* IRDA Tx DMA Abort callback has already been initialised :
  962. will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  963. /* Abort DMA TX */
  964. if(HAL_DMA_Abort_IT(hirda->hdmatx) != HAL_OK)
  965. {
  966. hirda->hdmatx->XferAbortCallback = NULL;
  967. }
  968. else
  969. {
  970. abortcplt = 0;
  971. }
  972. }
  973. }
  974. /* Disable the IRDA DMA Rx request if enabled */
  975. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  976. {
  977. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  978. /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
  979. if(hirda->hdmarx != NULL)
  980. {
  981. /* IRDA Rx DMA Abort callback has already been initialised :
  982. will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  983. /* Abort DMA RX */
  984. if(HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
  985. {
  986. hirda->hdmarx->XferAbortCallback = NULL;
  987. abortcplt = 1;
  988. }
  989. else
  990. {
  991. abortcplt = 0;
  992. }
  993. }
  994. }
  995. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  996. if (abortcplt == 1)
  997. {
  998. /* Reset Tx and Rx transfer counters */
  999. hirda->TxXferCount = 0;
  1000. hirda->RxXferCount = 0;
  1001. /* Reset errorCode */
  1002. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1003. /* Clear the Error flags in the ICR register */
  1004. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1005. /* Restore hirda->gState and hirda->RxState to Ready */
  1006. hirda->gState = HAL_IRDA_STATE_READY;
  1007. hirda->RxState = HAL_IRDA_STATE_READY;
  1008. /* As no DMA to be aborted, call directly user Abort complete callback */
  1009. HAL_IRDA_AbortCpltCallback(hirda);
  1010. }
  1011. return HAL_OK;
  1012. }
  1013. /**
  1014. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1015. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1016. * the configuration information for the specified UART module.
  1017. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1018. * This procedure performs following operations :
  1019. * - Disable IRDA Interrupts (Tx)
  1020. * - Disable the DMA transfer in the peripheral register (if enabled)
  1021. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1022. * - Set handle State to READY
  1023. * - At abort completion, call user abort complete callback
  1024. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1025. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1026. * @retval HAL status
  1027. */
  1028. HAL_StatusTypeDef HAL_IRDA_AbortTransmit_IT(IRDA_HandleTypeDef *hirda)
  1029. {
  1030. /* Disable TXEIE and TCIE interrupts */
  1031. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1032. /* Disable the IRDA DMA Tx request if enabled */
  1033. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT))
  1034. {
  1035. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1036. /* Abort the IRDA DMA Tx channel : use non blocking DMA Abort API (callback) */
  1037. if(hirda->hdmatx != NULL)
  1038. {
  1039. /* Set the IRDA DMA Abort callback :
  1040. will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  1041. hirda->hdmatx->XferAbortCallback = IRDA_DMATxOnlyAbortCallback;
  1042. /* Abort DMA TX */
  1043. if(HAL_DMA_Abort_IT(hirda->hdmatx) != HAL_OK)
  1044. {
  1045. /* Call Directly hirda->hdmatx->XferAbortCallback function in case of error */
  1046. hirda->hdmatx->XferAbortCallback(hirda->hdmatx);
  1047. }
  1048. }
  1049. else
  1050. {
  1051. /* Reset Tx transfer counter */
  1052. hirda->TxXferCount = 0;
  1053. /* Restore hirda->gState to Ready */
  1054. hirda->gState = HAL_IRDA_STATE_READY;
  1055. /* As no DMA to be aborted, call directly user Abort complete callback */
  1056. HAL_IRDA_AbortTransmitCpltCallback(hirda);
  1057. }
  1058. }
  1059. else
  1060. {
  1061. /* Reset Tx transfer counter */
  1062. hirda->TxXferCount = 0;
  1063. /* Restore hirda->gState to Ready */
  1064. hirda->gState = HAL_IRDA_STATE_READY;
  1065. /* As no DMA to be aborted, call directly user Abort complete callback */
  1066. HAL_IRDA_AbortTransmitCpltCallback(hirda);
  1067. }
  1068. return HAL_OK;
  1069. }
  1070. /**
  1071. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1072. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1073. * the configuration information for the specified UART module.
  1074. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1075. * This procedure performs following operations :
  1076. * - Disable IRDA Interrupts (Rx)
  1077. * - Disable the DMA transfer in the peripheral register (if enabled)
  1078. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1079. * - Set handle State to READY
  1080. * - At abort completion, call user abort complete callback
  1081. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1082. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1083. * @retval HAL status
  1084. */
  1085. HAL_StatusTypeDef HAL_IRDA_AbortReceive_IT(IRDA_HandleTypeDef *hirda)
  1086. {
  1087. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1088. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1089. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1090. /* Disable the IRDA DMA Rx request if enabled */
  1091. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1092. {
  1093. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1094. /* Abort the IRDA DMA Rx channel : use non blocking DMA Abort API (callback) */
  1095. if(hirda->hdmarx != NULL)
  1096. {
  1097. /* Set the IRDA DMA Abort callback :
  1098. will lead to call HAL_IRDA_AbortCpltCallback() at end of DMA abort procedure */
  1099. hirda->hdmarx->XferAbortCallback = IRDA_DMARxOnlyAbortCallback;
  1100. /* Abort DMA RX */
  1101. if(HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
  1102. {
  1103. /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
  1104. hirda->hdmarx->XferAbortCallback(hirda->hdmarx);
  1105. }
  1106. }
  1107. else
  1108. {
  1109. /* Reset Rx transfer counter */
  1110. hirda->RxXferCount = 0;
  1111. /* Clear the Error flags in the ICR register */
  1112. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1113. /* Restore hirda->RxState to Ready */
  1114. hirda->RxState = HAL_IRDA_STATE_READY;
  1115. /* As no DMA to be aborted, call directly user Abort complete callback */
  1116. HAL_IRDA_AbortReceiveCpltCallback(hirda);
  1117. }
  1118. }
  1119. else
  1120. {
  1121. /* Reset Rx transfer counter */
  1122. hirda->RxXferCount = 0;
  1123. /* Clear the Error flags in the ICR register */
  1124. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1125. /* Restore hirda->RxState to Ready */
  1126. hirda->RxState = HAL_IRDA_STATE_READY;
  1127. /* As no DMA to be aborted, call directly user Abort complete callback */
  1128. HAL_IRDA_AbortReceiveCpltCallback(hirda);
  1129. }
  1130. return HAL_OK;
  1131. }
  1132. /**
  1133. * @brief Handle IRDA interrupt request.
  1134. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1135. * the configuration information for the specified IRDA module.
  1136. * @retval None
  1137. */
  1138. void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
  1139. {
  1140. uint32_t isrflags = READ_REG(hirda->Instance->ISR);
  1141. uint32_t cr1its = READ_REG(hirda->Instance->CR1);
  1142. uint32_t cr3its;
  1143. uint32_t errorflags;
  1144. /* If no error occurs */
  1145. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
  1146. if (errorflags == RESET)
  1147. {
  1148. /* IRDA in mode Receiver ---------------------------------------------------*/
  1149. if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1150. {
  1151. IRDA_Receive_IT(hirda);
  1152. return;
  1153. }
  1154. }
  1155. /* If some errors occur */
  1156. cr3its = READ_REG(hirda->Instance->CR3);
  1157. if( (errorflags != RESET)
  1158. && ( ((cr3its & USART_CR3_EIE) != RESET)
  1159. || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)) )
  1160. {
  1161. /* IRDA parity error interrupt occurred -------------------------------------*/
  1162. if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1163. {
  1164. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_PEF);
  1165. hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
  1166. }
  1167. /* IRDA frame error interrupt occurred --------------------------------------*/
  1168. if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1169. {
  1170. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_FEF);
  1171. hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
  1172. }
  1173. /* IRDA noise error interrupt occurred --------------------------------------*/
  1174. if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1175. {
  1176. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_NEF);
  1177. hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
  1178. }
  1179. /* IRDA Over-Run interrupt occurred -----------------------------------------*/
  1180. if(((isrflags & USART_ISR_ORE) != RESET) &&
  1181. (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
  1182. {
  1183. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_OREF);
  1184. hirda->ErrorCode |= HAL_IRDA_ERROR_ORE;
  1185. }
  1186. /* Call IRDA Error Call back function if need be --------------------------*/
  1187. if(hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
  1188. {
  1189. /* IRDA in mode Receiver ---------------------------------------------------*/
  1190. if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1191. {
  1192. IRDA_Receive_IT(hirda);
  1193. }
  1194. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1195. consider error as blocking */
  1196. if (((hirda->ErrorCode & HAL_IRDA_ERROR_ORE) != RESET) ||
  1197. (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR)))
  1198. {
  1199. /* Blocking error : transfer is aborted
  1200. Set the IRDA state ready to be able to start again the process,
  1201. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1202. IRDA_EndRxTransfer(hirda);
  1203. /* Disable the IRDA DMA Rx request if enabled */
  1204. if (HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR))
  1205. {
  1206. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1207. /* Abort the IRDA DMA Rx channel */
  1208. if(hirda->hdmarx != NULL)
  1209. {
  1210. /* Set the IRDA DMA Abort callback :
  1211. will lead to call HAL_IRDA_ErrorCallback() at end of DMA abort procedure */
  1212. hirda->hdmarx->XferAbortCallback = IRDA_DMAAbortOnError;
  1213. /* Abort DMA RX */
  1214. if(HAL_DMA_Abort_IT(hirda->hdmarx) != HAL_OK)
  1215. {
  1216. /* Call Directly hirda->hdmarx->XferAbortCallback function in case of error */
  1217. hirda->hdmarx->XferAbortCallback(hirda->hdmarx);
  1218. }
  1219. }
  1220. else
  1221. {
  1222. /* Call user error callback */
  1223. HAL_IRDA_ErrorCallback(hirda);
  1224. }
  1225. }
  1226. else
  1227. {
  1228. /* Call user error callback */
  1229. HAL_IRDA_ErrorCallback(hirda);
  1230. }
  1231. }
  1232. else
  1233. {
  1234. /* Non Blocking error : transfer could go on.
  1235. Error is notified to user through user error callback */
  1236. HAL_IRDA_ErrorCallback(hirda);
  1237. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1238. }
  1239. }
  1240. return;
  1241. } /* End if some error occurs */
  1242. /* IRDA in mode Transmitter ------------------------------------------------*/
  1243. if(((isrflags & USART_ISR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1244. {
  1245. IRDA_Transmit_IT(hirda);
  1246. return;
  1247. }
  1248. /* IRDA in mode Transmitter (transmission end) -----------------------------*/
  1249. if(((isrflags & USART_ISR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1250. {
  1251. IRDA_EndTransmit_IT(hirda);
  1252. return;
  1253. }
  1254. }
  1255. /**
  1256. * @brief Tx Transfer completed callback.
  1257. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1258. * the configuration information for the specified IRDA module.
  1259. * @retval None
  1260. */
  1261. __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
  1262. {
  1263. /* Prevent unused argument(s) compilation warning */
  1264. UNUSED(hirda);
  1265. /* NOTE : This function should not be modified, when the callback is needed,
  1266. the HAL_IRDA_TxCpltCallback can be implemented in the user file.
  1267. */
  1268. }
  1269. /**
  1270. * @brief Tx Half Transfer completed callback.
  1271. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1272. * the configuration information for the specified USART module.
  1273. * @retval None
  1274. */
  1275. __weak void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  1276. {
  1277. /* Prevent unused argument(s) compilation warning */
  1278. UNUSED(hirda);
  1279. /* NOTE : This function should not be modified, when the callback is needed,
  1280. the HAL_IRDA_TxHalfCpltCallback can be implemented in the user file.
  1281. */
  1282. }
  1283. /**
  1284. * @brief Rx Transfer completed callback.
  1285. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1286. * the configuration information for the specified IRDA module.
  1287. * @retval None
  1288. */
  1289. __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
  1290. {
  1291. /* Prevent unused argument(s) compilation warning */
  1292. UNUSED(hirda);
  1293. /* NOTE : This function should not be modified, when the callback is needed,
  1294. the HAL_IRDA_RxCpltCallback can be implemented in the user file.
  1295. */
  1296. }
  1297. /**
  1298. * @brief Rx Half Transfer complete callback.
  1299. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1300. * the configuration information for the specified IRDA module.
  1301. * @retval None
  1302. */
  1303. __weak void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  1304. {
  1305. /* Prevent unused argument(s) compilation warning */
  1306. UNUSED(hirda);
  1307. /* NOTE : This function should not be modified, when the callback is needed,
  1308. the HAL_IRDA_RxHalfCpltCallback can be implemented in the user file.
  1309. */
  1310. }
  1311. /**
  1312. * @brief IRDA error callback.
  1313. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1314. * the configuration information for the specified IRDA module.
  1315. * @retval None
  1316. */
  1317. __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
  1318. {
  1319. /* Prevent unused argument(s) compilation warning */
  1320. UNUSED(hirda);
  1321. /* NOTE : This function should not be modified, when the callback is needed,
  1322. the HAL_IRDA_ErrorCallback can be implemented in the user file.
  1323. */
  1324. }
  1325. /**
  1326. * @brief IRDA Abort Complete callback.
  1327. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1328. * the configuration information for the specified IRDA module.
  1329. * @retval None
  1330. */
  1331. __weak void HAL_IRDA_AbortCpltCallback (IRDA_HandleTypeDef *hirda)
  1332. {
  1333. /* Prevent unused argument(s) compilation warning */
  1334. UNUSED(hirda);
  1335. /* NOTE : This function should not be modified, when the callback is needed,
  1336. the HAL_IRDA_AbortCpltCallback can be implemented in the user file.
  1337. */
  1338. }
  1339. /**
  1340. * @brief IRDA Abort Complete callback.
  1341. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1342. * the configuration information for the specified IRDA module.
  1343. * @retval None
  1344. */
  1345. __weak void HAL_IRDA_AbortTransmitCpltCallback (IRDA_HandleTypeDef *hirda)
  1346. {
  1347. /* Prevent unused argument(s) compilation warning */
  1348. UNUSED(hirda);
  1349. /* NOTE : This function should not be modified, when the callback is needed,
  1350. the HAL_IRDA_AbortTransmitCpltCallback can be implemented in the user file.
  1351. */
  1352. }
  1353. /**
  1354. * @brief IRDA Abort Receive Complete callback.
  1355. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1356. * the configuration information for the specified IRDA module.
  1357. * @retval None
  1358. */
  1359. __weak void HAL_IRDA_AbortReceiveCpltCallback (IRDA_HandleTypeDef *hirda)
  1360. {
  1361. /* Prevent unused argument(s) compilation warning */
  1362. UNUSED(hirda);
  1363. /* NOTE : This function should not be modified, when the callback is needed,
  1364. the HAL_IRDA_AbortReceiveCpltCallback can be implemented in the user file.
  1365. */
  1366. }
  1367. /**
  1368. * @}
  1369. */
  1370. /** @defgroup IRDA_Exported_Functions_Group4 Peripheral State and Error functions
  1371. * @brief IRDA State and Errors functions
  1372. *
  1373. @verbatim
  1374. ==============================================================================
  1375. ##### Peripheral State and Error functions #####
  1376. ==============================================================================
  1377. [..]
  1378. This subsection provides a set of functions allowing to return the State of IrDA
  1379. communication process and also return Peripheral Errors occurred during communication process
  1380. (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state
  1381. of the IRDA peripheral handle.
  1382. (+) HAL_IRDA_GetError() checks in run-time errors that could occur during
  1383. communication.
  1384. @endverbatim
  1385. * @{
  1386. */
  1387. /**
  1388. * @brief Return the IRDA handle state.
  1389. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1390. * the configuration information for the specified IRDA module.
  1391. * @retval HAL state
  1392. */
  1393. HAL_IRDA_StateTypeDef HAL_IRDA_GetState(IRDA_HandleTypeDef *hirda)
  1394. {
  1395. /* Return IRDA handle state */
  1396. uint32_t temp1= 0x00, temp2 = 0x00;
  1397. temp1 = hirda->gState;
  1398. temp2 = hirda->RxState;
  1399. return (HAL_IRDA_StateTypeDef)(temp1 | temp2);
  1400. }
  1401. /**
  1402. * @brief Return the IRDA handle error code.
  1403. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1404. * the configuration information for the specified IRDA module.
  1405. * @retval IRDA Error Code
  1406. */
  1407. uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef *hirda)
  1408. {
  1409. return hirda->ErrorCode;
  1410. }
  1411. /**
  1412. * @}
  1413. */
  1414. /**
  1415. * @}
  1416. */
  1417. /** @defgroup IRDA_Private_Functions IRDA Private Functions
  1418. * @{
  1419. */
  1420. /**
  1421. * @brief Configure the IRDA peripheral.
  1422. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1423. * the configuration information for the specified IRDA module.
  1424. * @retval None
  1425. */
  1426. static HAL_StatusTypeDef IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
  1427. {
  1428. uint32_t tmpreg = 0x00000000;
  1429. IRDA_ClockSourceTypeDef clocksource = IRDA_CLOCKSOURCE_UNDEFINED;
  1430. HAL_StatusTypeDef ret = HAL_OK;
  1431. /* Check the communication parameters */
  1432. assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));
  1433. assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
  1434. assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
  1435. assert_param(IS_IRDA_TX_RX_MODE(hirda->Init.Mode));
  1436. assert_param(IS_IRDA_PRESCALER(hirda->Init.Prescaler));
  1437. assert_param(IS_IRDA_POWERMODE(hirda->Init.PowerMode));
  1438. /*-------------------------- USART CR1 Configuration -----------------------*/
  1439. /* Configure the IRDA Word Length, Parity and transfer Mode:
  1440. Set the M bits according to hirda->Init.WordLength value
  1441. Set PCE and PS bits according to hirda->Init.Parity value
  1442. Set TE and RE bits according to hirda->Init.Mode value */
  1443. tmpreg = (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode ;
  1444. MODIFY_REG(hirda->Instance->CR1, IRDA_CR1_FIELDS, tmpreg);
  1445. /*-------------------------- USART CR3 Configuration -----------------------*/
  1446. MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.PowerMode);
  1447. /*-------------------------- USART GTPR Configuration ----------------------*/
  1448. MODIFY_REG(hirda->Instance->GTPR, USART_GTPR_PSC, hirda->Init.Prescaler);
  1449. /*-------------------------- USART BRR Configuration -----------------------*/
  1450. IRDA_GETCLOCKSOURCE(hirda, clocksource);
  1451. switch (clocksource)
  1452. {
  1453. case IRDA_CLOCKSOURCE_PCLK1:
  1454. hirda->Instance->BRR = (uint16_t)((HAL_RCC_GetPCLK1Freq() + (hirda->Init.BaudRate/2)) / hirda->Init.BaudRate);
  1455. break;
  1456. case IRDA_CLOCKSOURCE_PCLK2:
  1457. hirda->Instance->BRR = (uint16_t)((HAL_RCC_GetPCLK2Freq() + (hirda->Init.BaudRate/2)) / hirda->Init.BaudRate);
  1458. break;
  1459. case IRDA_CLOCKSOURCE_HSI:
  1460. hirda->Instance->BRR = (uint16_t)((HSI_VALUE + (hirda->Init.BaudRate/2)) / hirda->Init.BaudRate);
  1461. break;
  1462. case IRDA_CLOCKSOURCE_SYSCLK:
  1463. hirda->Instance->BRR = (uint16_t)((HAL_RCC_GetSysClockFreq() + (hirda->Init.BaudRate/2)) / hirda->Init.BaudRate);
  1464. break;
  1465. case IRDA_CLOCKSOURCE_LSE:
  1466. hirda->Instance->BRR = (uint16_t)((LSE_VALUE + (hirda->Init.BaudRate/2)) / hirda->Init.BaudRate);
  1467. break;
  1468. case IRDA_CLOCKSOURCE_UNDEFINED:
  1469. default:
  1470. ret = HAL_ERROR;
  1471. break;
  1472. }
  1473. return ret;
  1474. }
  1475. /**
  1476. * @brief Check the IRDA Idle State.
  1477. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1478. * the configuration information for the specified IRDA module.
  1479. * @retval HAL status
  1480. */
  1481. static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda)
  1482. {
  1483. uint32_t tickstart = 0;
  1484. /* Initialize the IRDA ErrorCode */
  1485. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1486. /* Init tickstart for timeout managment*/
  1487. tickstart = HAL_GetTick();
  1488. /* Check if the Transmitter is enabled */
  1489. if((hirda->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  1490. {
  1491. /* Wait until TEACK flag is set */
  1492. if(IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_TEACK, RESET, tickstart, IRDA_TEACK_REACK_TIMEOUT) != HAL_OK)
  1493. {
  1494. /* Timeout occurred */
  1495. return HAL_TIMEOUT;
  1496. }
  1497. }
  1498. /* Initialize the IRDA state*/
  1499. hirda->gState = HAL_IRDA_STATE_READY;
  1500. hirda->RxState = HAL_IRDA_STATE_READY;
  1501. /* Process Unlocked */
  1502. __HAL_UNLOCK(hirda);
  1503. return HAL_OK;
  1504. }
  1505. /**
  1506. * @brief Handle IRDA Communication Timeout.
  1507. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1508. * the configuration information for the specified IRDA module.
  1509. * @param Flag Specifies the IRDA flag to check.
  1510. * @param Status Flag status (SET or RESET)
  1511. * @param Tickstart Tick start value
  1512. * @param Timeout Timeout duration
  1513. * @retval HAL status
  1514. */
  1515. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1516. {
  1517. /* Wait until flag is set */
  1518. while((__HAL_IRDA_GET_FLAG(hirda, Flag) ? SET : RESET) == Status)
  1519. {
  1520. /* Check for the Timeout */
  1521. if(Timeout != HAL_MAX_DELAY)
  1522. {
  1523. if((Timeout == 0) || ((HAL_GetTick()-Tickstart) > Timeout))
  1524. {
  1525. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1526. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  1527. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1528. hirda->gState = HAL_IRDA_STATE_READY;
  1529. hirda->RxState = HAL_IRDA_STATE_READY;
  1530. /* Process Unlocked */
  1531. __HAL_UNLOCK(hirda);
  1532. return HAL_TIMEOUT;
  1533. }
  1534. }
  1535. }
  1536. return HAL_OK;
  1537. }
  1538. /**
  1539. * @brief End ongoing Tx transfer on IRDA peripheral (following error detection or Transmit completion).
  1540. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1541. * the configuration information for the specified IRDA module.
  1542. * @retval None
  1543. */
  1544. static void IRDA_EndTxTransfer(IRDA_HandleTypeDef *hirda)
  1545. {
  1546. /* Disable TXEIE and TCIE interrupts */
  1547. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1548. /* At end of Tx process, restore hirda->gState to Ready */
  1549. hirda->gState = HAL_IRDA_STATE_READY;
  1550. }
  1551. /**
  1552. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  1553. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1554. * the configuration information for the specified IRDA module.
  1555. * @retval None
  1556. */
  1557. static void IRDA_EndRxTransfer(IRDA_HandleTypeDef *hirda)
  1558. {
  1559. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1560. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1561. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1562. /* At end of Rx process, restore hirda->RxState to Ready */
  1563. hirda->RxState = HAL_IRDA_STATE_READY;
  1564. }
  1565. /**
  1566. * @brief DMA IRDA transmit process complete callback.
  1567. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1568. * the configuration information for the specified DMA module.
  1569. * @retval None
  1570. */
  1571. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1572. {
  1573. IRDA_HandleTypeDef* hirda = (IRDA_HandleTypeDef*)(hdma->Parent);
  1574. /* DMA Normal mode */
  1575. if ( HAL_IS_BIT_CLR(hdma->Instance->CR, DMA_SxCR_CIRC) )
  1576. {
  1577. hirda->TxXferCount = 0;
  1578. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  1579. in the IRDA CR3 register */
  1580. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
  1581. /* Enable the IRDA Transmit Complete Interrupt */
  1582. SET_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
  1583. }
  1584. /* DMA Circular mode */
  1585. else
  1586. {
  1587. HAL_IRDA_TxCpltCallback(hirda);
  1588. }
  1589. }
  1590. /**
  1591. * @brief DMA IRDA transmit process half complete callback.
  1592. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1593. * the configuration information for the specified DMA module.
  1594. * @retval None
  1595. */
  1596. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma)
  1597. {
  1598. IRDA_HandleTypeDef* hirda = (IRDA_HandleTypeDef*)(hdma->Parent);
  1599. HAL_IRDA_TxHalfCpltCallback(hirda);
  1600. }
  1601. /**
  1602. * @brief DMA IRDA receive process complete callback.
  1603. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1604. * the configuration information for the specified DMA module.
  1605. * @retval None
  1606. */
  1607. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1608. {
  1609. IRDA_HandleTypeDef* hirda = (IRDA_HandleTypeDef*)(hdma->Parent);
  1610. /* DMA Normal mode */
  1611. if ( HAL_IS_BIT_CLR(hdma->Instance->CR, DMA_SxCR_CIRC) )
  1612. {
  1613. hirda->RxXferCount = 0;
  1614. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1615. CLEAR_BIT(hirda->Instance->CR1, USART_CR1_PEIE);
  1616. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1617. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  1618. in the IRDA CR3 register */
  1619. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAR);
  1620. /* At end of Rx process, restore hirda->RxState to Ready */
  1621. hirda->RxState = HAL_IRDA_STATE_READY;
  1622. }
  1623. HAL_IRDA_RxCpltCallback(hirda);
  1624. }
  1625. /**
  1626. * @brief DMA IRDA receive process half complete callback.
  1627. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1628. * the configuration information for the specified DMA module.
  1629. * @retval None
  1630. */
  1631. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma)
  1632. {
  1633. IRDA_HandleTypeDef* hirda = (IRDA_HandleTypeDef*)(hdma->Parent);
  1634. HAL_IRDA_RxHalfCpltCallback(hirda);
  1635. }
  1636. /**
  1637. * @brief DMA IRDA communication error callback.
  1638. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1639. * the configuration information for the specified DMA module.
  1640. * @retval None
  1641. */
  1642. static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
  1643. {
  1644. IRDA_HandleTypeDef* hirda = (IRDA_HandleTypeDef*)(hdma->Parent);
  1645. /* Stop IRDA DMA Tx request if ongoing */
  1646. if ( (hirda->gState == HAL_IRDA_STATE_BUSY_TX)
  1647. &&(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT)) )
  1648. {
  1649. hirda->TxXferCount = 0;
  1650. IRDA_EndTxTransfer(hirda);
  1651. }
  1652. /* Stop IRDA DMA Rx request if ongoing */
  1653. if ( (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
  1654. &&(HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAR)) )
  1655. {
  1656. hirda->RxXferCount = 0;
  1657. IRDA_EndRxTransfer(hirda);
  1658. }
  1659. hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
  1660. HAL_IRDA_ErrorCallback(hirda);
  1661. }
  1662. /**
  1663. * @brief DMA IRDA communication abort callback, when initiated by HAL services on Error
  1664. * (To be called at end of DMA Abort procedure following error occurrence).
  1665. * @param hdma DMA handle.
  1666. * @retval None
  1667. */
  1668. static void IRDA_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1669. {
  1670. IRDA_HandleTypeDef* hirda = (IRDA_HandleTypeDef*)(hdma->Parent);
  1671. hirda->RxXferCount = 0;
  1672. hirda->TxXferCount = 0;
  1673. HAL_IRDA_ErrorCallback(hirda);
  1674. }
  1675. /**
  1676. * @brief DMA IRDA Tx communication abort callback, when initiated by user
  1677. * (To be called at end of DMA Tx Abort procedure following user abort request).
  1678. * @note When this callback is executed, User Abort complete call back is called only if no
  1679. * Abort still ongoing for Rx DMA Handle.
  1680. * @param hdma DMA handle.
  1681. * @retval None
  1682. */
  1683. static void IRDA_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  1684. {
  1685. IRDA_HandleTypeDef* hirda = (IRDA_HandleTypeDef* )(hdma->Parent);
  1686. hirda->hdmatx->XferAbortCallback = NULL;
  1687. /* Check if an Abort process is still ongoing */
  1688. if(hirda->hdmarx != NULL)
  1689. {
  1690. if(hirda->hdmarx->XferAbortCallback != NULL)
  1691. {
  1692. return;
  1693. }
  1694. }
  1695. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1696. hirda->TxXferCount = 0;
  1697. hirda->RxXferCount = 0;
  1698. /* Reset errorCode */
  1699. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1700. /* Clear the Error flags in the ICR register */
  1701. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1702. /* Restore hirda->gState and hirda->RxState to Ready */
  1703. hirda->gState = HAL_IRDA_STATE_READY;
  1704. hirda->RxState = HAL_IRDA_STATE_READY;
  1705. /* Call user Abort complete callback */
  1706. HAL_IRDA_AbortCpltCallback(hirda);
  1707. }
  1708. /**
  1709. * @brief DMA IRDA Rx communication abort callback, when initiated by user
  1710. * (To be called at end of DMA Rx Abort procedure following user abort request).
  1711. * @note When this callback is executed, User Abort complete call back is called only if no
  1712. * Abort still ongoing for Tx DMA Handle.
  1713. * @param hdma DMA handle.
  1714. * @retval None
  1715. */
  1716. static void IRDA_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  1717. {
  1718. IRDA_HandleTypeDef* hirda = (IRDA_HandleTypeDef* )(hdma->Parent);
  1719. hirda->hdmarx->XferAbortCallback = NULL;
  1720. /* Check if an Abort process is still ongoing */
  1721. if(hirda->hdmatx != NULL)
  1722. {
  1723. if(hirda->hdmatx->XferAbortCallback != NULL)
  1724. {
  1725. return;
  1726. }
  1727. }
  1728. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1729. hirda->TxXferCount = 0;
  1730. hirda->RxXferCount = 0;
  1731. /* Reset errorCode */
  1732. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  1733. /* Clear the Error flags in the ICR register */
  1734. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1735. /* Restore hirda->gState and hirda->RxState to Ready */
  1736. hirda->gState = HAL_IRDA_STATE_READY;
  1737. hirda->RxState = HAL_IRDA_STATE_READY;
  1738. /* Call user Abort complete callback */
  1739. HAL_IRDA_AbortCpltCallback(hirda);
  1740. }
  1741. /**
  1742. * @brief DMA IRDA Tx communication abort callback, when initiated by user by a call to
  1743. * HAL_IRDA_AbortTransmit_IT API (Abort only Tx transfer)
  1744. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  1745. * and leads to user Tx Abort Complete callback execution).
  1746. * @param hdma DMA handle.
  1747. * @retval None
  1748. */
  1749. static void IRDA_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1750. {
  1751. IRDA_HandleTypeDef* hirda = (IRDA_HandleTypeDef*)(hdma->Parent);
  1752. hirda->TxXferCount = 0;
  1753. /* Restore hirda->gState to Ready */
  1754. hirda->gState = HAL_IRDA_STATE_READY;
  1755. /* Call user Abort complete callback */
  1756. HAL_IRDA_AbortTransmitCpltCallback(hirda);
  1757. }
  1758. /**
  1759. * @brief DMA IRDA Rx communication abort callback, when initiated by user by a call to
  1760. * HAL_IRDA_AbortReceive_IT API (Abort only Rx transfer)
  1761. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  1762. * and leads to user Rx Abort Complete callback execution).
  1763. * @param hdma DMA handle.
  1764. * @retval None
  1765. */
  1766. static void IRDA_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1767. {
  1768. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1769. hirda->RxXferCount = 0;
  1770. /* Clear the Error flags in the ICR register */
  1771. __HAL_IRDA_CLEAR_FLAG(hirda, IRDA_CLEAR_OREF | IRDA_CLEAR_NEF | IRDA_CLEAR_PEF | IRDA_CLEAR_FEF);
  1772. /* Restore hirda->RxState to Ready */
  1773. hirda->RxState = HAL_IRDA_STATE_READY;
  1774. /* Call user Abort complete callback */
  1775. HAL_IRDA_AbortReceiveCpltCallback(hirda);
  1776. }
  1777. /**
  1778. * @brief Send an amount of data in interrupt mode.
  1779. * @note Function is called under interruption only, once
  1780. * interruptions have been enabled by HAL_IRDA_Transmit_IT().
  1781. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1782. * the configuration information for the specified IRDA module.
  1783. * @retval HAL status
  1784. */
  1785. static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
  1786. {
  1787. uint16_t* tmp;
  1788. /* Check that a Tx process is ongoing */
  1789. if(hirda->gState == HAL_IRDA_STATE_BUSY_TX)
  1790. {
  1791. if(hirda->TxXferCount == 0)
  1792. {
  1793. /* Disable the IRDA Transmit Data Register Empty Interrupt */
  1794. CLEAR_BIT(hirda->Instance->CR1, USART_CR1_TXEIE);
  1795. /* Enable the IRDA Transmit Complete Interrupt */
  1796. SET_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
  1797. return HAL_OK;
  1798. }
  1799. else
  1800. {
  1801. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  1802. {
  1803. tmp = (uint16_t*) hirda->pTxBuffPtr;
  1804. hirda->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  1805. hirda->pTxBuffPtr += 2;
  1806. }
  1807. else
  1808. {
  1809. hirda->Instance->TDR = (uint8_t)(*hirda->pTxBuffPtr++ & (uint8_t)0xFF);
  1810. }
  1811. hirda->TxXferCount--;
  1812. return HAL_OK;
  1813. }
  1814. }
  1815. else
  1816. {
  1817. return HAL_BUSY;
  1818. }
  1819. }
  1820. /**
  1821. * @brief Wrap up transmission in non-blocking mode.
  1822. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1823. * the configuration information for the specified IRDA module.
  1824. * @retval HAL status
  1825. */
  1826. static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda)
  1827. {
  1828. /* Disable the IRDA Transmit Complete Interrupt */
  1829. CLEAR_BIT(hirda->Instance->CR1, USART_CR1_TCIE);
  1830. /* Tx process is ended, restore hirda->gState to Ready */
  1831. hirda->gState = HAL_IRDA_STATE_READY;
  1832. HAL_IRDA_TxCpltCallback(hirda);
  1833. return HAL_OK;
  1834. }
  1835. /**
  1836. * @brief Receive an amount of data in interrupt mode.
  1837. * @note Function is called under interruption only, once
  1838. * interruptions have been enabled by HAL_IRDA_Receive_IT()
  1839. * @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  1840. * the configuration information for the specified IRDA module.
  1841. * @retval HAL status
  1842. */
  1843. static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
  1844. {
  1845. uint16_t* tmp;
  1846. uint16_t uhMask = hirda->Mask;
  1847. uint16_t uhdata;
  1848. /* Check that a Rx process is ongoing */
  1849. if (hirda->RxState == HAL_IRDA_STATE_BUSY_RX)
  1850. {
  1851. uhdata = (uint16_t) READ_REG(hirda->Instance->RDR);
  1852. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  1853. {
  1854. tmp = (uint16_t*) hirda->pRxBuffPtr ;
  1855. *tmp = (uint16_t)(uhdata & uhMask);
  1856. hirda->pRxBuffPtr +=2;
  1857. }
  1858. else
  1859. {
  1860. *hirda->pRxBuffPtr++ = (uint8_t)(uhdata & (uint8_t)uhMask);
  1861. }
  1862. if(--hirda->RxXferCount == 0)
  1863. {
  1864. /* Disable the IRDA Parity Error Interrupt and RXNE interrupt */
  1865. CLEAR_BIT(hirda->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1866. /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  1867. CLEAR_BIT(hirda->Instance->CR3, USART_CR3_EIE);
  1868. /* Rx process is completed, restore hirda->RxState to Ready */
  1869. hirda->RxState = HAL_IRDA_STATE_READY;
  1870. HAL_IRDA_RxCpltCallback(hirda);
  1871. return HAL_OK;
  1872. }
  1873. return HAL_OK;
  1874. }
  1875. else
  1876. {
  1877. /* Clear RXNE interrupt flag */
  1878. __HAL_IRDA_SEND_REQ(hirda, IRDA_RXDATA_FLUSH_REQUEST);
  1879. return HAL_BUSY;
  1880. }
  1881. }
  1882. /**
  1883. * @}
  1884. */
  1885. #endif /* HAL_IRDA_MODULE_ENABLED */
  1886. /**
  1887. * @}
  1888. */
  1889. /**
  1890. * @}
  1891. */
  1892. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/