stm32l4xx_hal_irda.c 85 KB

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