stm32l4xx_hal_irda.c 78 KB

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