stm32h7xx_hal_irda.c 78 KB

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