stm32f7xx_hal_irda.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_irda.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  7. * @brief IRDA HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the IrDA SIR ENDEC block (IrDA):
  10. * + Initialization and de-initialization methods
  11. * + IO operation methods
  12. * + Peripheral Control methods
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The IRDA HAL driver can be used as follows:
  20. (#) Declare a IRDA_HandleTypeDef handle structure.
  21. (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API:
  22. (##) Enable the USARTx interface clock.
  23. (##) IRDA pins configuration:
  24. (+++) Enable the clock for the IRDA GPIOs.
  25. (+++) Configure these IRDA pins as alternate function pull-up.
  26. (##) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
  27. and HAL_IRDA_Receive_IT() APIs):
  28. (+++) Configure the USARTx interrupt priority.
  29. (+++) Enable the NVIC USART IRQ handle.
  30. (##) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
  31. and HAL_IRDA_Receive_DMA() APIs):
  32. (+++) Declare a DMA handle structure for the Tx/Rx stream.
  33. (+++) Enable the DMAx interface clock.
  34. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  35. (+++) Configure the DMA Tx/Rx Stream.
  36. (+++) Associate the initialized DMA handle to the IRDA DMA Tx/Rx handle.
  37. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
  38. (#) Program the Baud Rate, Word Length, Parity, IrDA Mode, Prescaler
  39. and Mode(Receiver/Transmitter) in the hirda Init structure.
  40. (#) Initialize the IRDA registers by calling the HAL_IRDA_Init() API:
  41. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  42. by calling the customized HAL_IRDA_MspInit() API.
  43. -@@- The specific IRDA interrupts (Transmission complete interrupt,
  44. RXNE interrupt and Error Interrupts) will be managed using the macros
  45. __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
  46. (#) Three operation modes are available within this driver :
  47. *** Polling mode IO operation ***
  48. =================================
  49. [..]
  50. (+) Send an amount of data in blocking mode using HAL_IRDA_Transmit()
  51. (+) Receive an amount of data in blocking mode using HAL_IRDA_Receive()
  52. *** Interrupt mode IO operation ***
  53. ===================================
  54. [..]
  55. (+) Send an amount of data in non blocking mode using HAL_IRDA_Transmit_IT()
  56. (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
  57. add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
  58. (+) Receive an amount of data in non blocking mode using HAL_IRDA_Receive_IT()
  59. (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
  60. add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
  61. (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
  62. add his own code by customization of function pointer HAL_IRDA_ErrorCallback
  63. *** DMA mode IO operation ***
  64. =============================
  65. [..]
  66. (+) Send an amount of data in non blocking mode (DMA) using HAL_IRDA_Transmit_DMA()
  67. (+) At transmission end of transfer HAL_IRDA_TxCpltCallback is executed and user can
  68. add his own code by customization of function pointer HAL_IRDA_TxCpltCallback
  69. (+) Receive an amount of data in non blocking mode (DMA) using HAL_IRDA_Receive_DMA()
  70. (+) At reception end of transfer HAL_IRDA_RxCpltCallback is executed and user can
  71. add his own code by customization of function pointer HAL_IRDA_RxCpltCallback
  72. (+) In case of transfer Error, HAL_IRDA_ErrorCallback() function is executed and user can
  73. add his own code by customization of function pointer HAL_IRDA_ErrorCallback
  74. *** IRDA HAL driver macros list ***
  75. ===================================
  76. [..]
  77. Below the list of most used macros in IRDA HAL driver.
  78. (+) __HAL_IRDA_ENABLE: Enable the IRDA peripheral
  79. (+) __HAL_IRDA_DISABLE: Disable the IRDA peripheral
  80. (+) __HAL_IRDA_GET_FLAG : Checks whether the specified IRDA flag is set or not
  81. (+) __HAL_IRDA_CLEAR_FLAG : Clears the specified IRDA pending flag
  82. (+) __HAL_IRDA_ENABLE_IT: Enables the specified IRDA interrupt
  83. (+) __HAL_IRDA_DISABLE_IT: Disables the specified IRDA interrupt
  84. (@) You can refer to the IRDA HAL driver header file for more useful macros
  85. @endverbatim
  86. ******************************************************************************
  87. * @attention
  88. *
  89. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  90. *
  91. * Redistribution and use in source and binary forms, with or without modification,
  92. * are permitted provided that the following conditions are met:
  93. * 1. Redistributions of source code must retain the above copyright notice,
  94. * this list of conditions and the following disclaimer.
  95. * 2. Redistributions in binary form must reproduce the above copyright notice,
  96. * this list of conditions and the following disclaimer in the documentation
  97. * and/or other materials provided with the distribution.
  98. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  99. * may be used to endorse or promote products derived from this software
  100. * without specific prior written permission.
  101. *
  102. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  103. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  104. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  105. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  106. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  107. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  108. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  109. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  110. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  111. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  112. *
  113. ******************************************************************************
  114. */
  115. /* Includes ------------------------------------------------------------------*/
  116. #include "stm32f7xx_hal.h"
  117. /** @addtogroup STM32F7xx_HAL_Driver
  118. * @{
  119. */
  120. /** @defgroup IRDA IRDA
  121. * @brief HAL IRDA module driver
  122. * @{
  123. */
  124. #ifdef HAL_IRDA_MODULE_ENABLED
  125. /* Private typedef -----------------------------------------------------------*/
  126. /* Private define ------------------------------------------------------------*/
  127. /** @addtogroup IRDA_Private_Constants
  128. * @{
  129. */
  130. #define TEACK_REACK_TIMEOUT 1000
  131. #define HAL_IRDA_TXDMA_TIMEOUTVALUE 22000
  132. #define IRDA_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE \
  133. | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE))
  134. /**
  135. * @}
  136. */
  137. /* Private macro -------------------------------------------------------------*/
  138. /* Private variables ---------------------------------------------------------*/
  139. /* Private function prototypes -----------------------------------------------*/
  140. /** @addtogroup IRDA_Private_Functions
  141. * @{
  142. */
  143. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  144. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma);
  145. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  146. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma);
  147. static void IRDA_DMAError(DMA_HandleTypeDef *hdma);
  148. static void IRDA_SetConfig (IRDA_HandleTypeDef *hirda);
  149. static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda);
  150. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  151. static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
  152. static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda);
  153. static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
  154. /**
  155. * @}
  156. */
  157. /* Exported functions --------------------------------------------------------*/
  158. /** @defgroup IRDA_Exported_Functions IrDA Exported Functions
  159. * @{
  160. */
  161. /** @defgroup IRDA_Exported_Functions_Group1 IrDA Initialization and de-initialization functions
  162. * @brief Initialization and Configuration functions
  163. *
  164. @verbatim
  165. ===============================================================================
  166. ##### Initialization and Configuration functions #####
  167. ===============================================================================
  168. [..]
  169. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  170. in IrDA mode.
  171. (+) For the asynchronous mode only these parameters can be configured:
  172. (++) BaudRate
  173. (++) WordLength
  174. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  175. in the data register is transmitted but is changed by the parity bit.
  176. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  177. please refer to Reference manual for possible IRDA frame formats.
  178. (++) Prescaler: A pulse of width less than two and greater than one PSC period(s) may or may
  179. not be rejected. The receiver set up time should be managed by software. The IrDA physical layer
  180. specification specifies a minimum of 10 ms delay between transmission and
  181. reception (IrDA is a half duplex protocol).
  182. (++) Mode: Receiver/transmitter modes
  183. (++) IrDAMode: the IrDA can operate in the Normal mode or in the Low power mode.
  184. [..]
  185. The HAL_IRDA_Init() API follows IRDA configuration procedures (details for the procedures
  186. are available in reference manual).
  187. @endverbatim
  188. * @{
  189. */
  190. /**
  191. * @brief Initializes the IRDA mode according to the specified
  192. * parameters in the IRDA_InitTypeDef and create the associated handle.
  193. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  194. * the configuration information for the specified IRDA module.
  195. * @retval HAL status
  196. */
  197. HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
  198. {
  199. /* Check the IRDA handle allocation */
  200. if(hirda == NULL)
  201. {
  202. return HAL_ERROR;
  203. }
  204. /* Check the USART/UART associated to the IRDA handle */
  205. assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  206. if(hirda->State == HAL_IRDA_STATE_RESET)
  207. {
  208. /* Allocate lock resource and initialize it */
  209. hirda->Lock = HAL_UNLOCKED;
  210. /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  211. HAL_IRDA_MspInit(hirda);
  212. }
  213. hirda->State = HAL_IRDA_STATE_BUSY;
  214. /* Disable the Peripheral to update the configuration registers */
  215. __HAL_IRDA_DISABLE(hirda);
  216. /* Set the IRDA Communication parameters */
  217. IRDA_SetConfig(hirda);
  218. /* In IRDA mode, the following bits must be kept cleared:
  219. - LINEN, STOP and CLKEN bits in the USART_CR2 register,
  220. - SCEN and HDSEL bits in the USART_CR3 register.*/
  221. hirda->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN | USART_CR2_STOP);
  222. hirda->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL);
  223. /* set the UART/USART in IRDA mode */
  224. hirda->Instance->CR3 |= USART_CR3_IREN;
  225. /* Enable the Peripheral */
  226. __HAL_IRDA_ENABLE(hirda);
  227. /* TEACK and/or REACK to check before moving hirda->State to Ready */
  228. return (IRDA_CheckIdleState(hirda));
  229. }
  230. /**
  231. * @brief DeInitializes the IRDA peripheral
  232. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  233. * the configuration information for the specified IRDA module.
  234. * @retval HAL status
  235. */
  236. HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
  237. {
  238. /* Check the IRDA handle allocation */
  239. if(hirda == NULL)
  240. {
  241. return HAL_ERROR;
  242. }
  243. /* Check the parameters */
  244. assert_param(IS_IRDA_INSTANCE(hirda->Instance));
  245. hirda->State = HAL_IRDA_STATE_BUSY;
  246. /* DeInit the low level hardware */
  247. HAL_IRDA_MspDeInit(hirda);
  248. /* Disable the Peripheral */
  249. __HAL_IRDA_DISABLE(hirda);
  250. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  251. hirda->State = HAL_IRDA_STATE_RESET;
  252. /* Release Lock */
  253. __HAL_UNLOCK(hirda);
  254. return HAL_OK;
  255. }
  256. /**
  257. * @brief IRDA MSP Init.
  258. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  259. * the configuration information for the specified IRDA module.
  260. * @retval None
  261. */
  262. __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
  263. {
  264. /* NOTE : This function Should not be modified, when the callback is needed,
  265. the HAL_IRDA_MspInit could be implemented in the user file
  266. */
  267. }
  268. /**
  269. * @brief IRDA MSP DeInit.
  270. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  271. * the configuration information for the specified IRDA module.
  272. * @retval None
  273. */
  274. __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
  275. {
  276. /* NOTE : This function Should not be modified, when the callback is needed,
  277. the HAL_IRDA_MspDeInit could be implemented in the user file
  278. */
  279. }
  280. /**
  281. * @}
  282. */
  283. /** @defgroup IRDA_Exported_Functions_Group2 IO operation functions
  284. * @brief IRDA Transmit/Receive functions
  285. *
  286. @verbatim
  287. ===============================================================================
  288. ##### IO operation functions #####
  289. ===============================================================================
  290. This subsection provides a set of functions allowing to manage the IRDA data transfers.
  291. [..]
  292. IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
  293. on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
  294. is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
  295. While receiving data, transmission should be avoided as the data to be transmitted
  296. could be corrupted.
  297. (#) There are two modes of transfer:
  298. (++) Blocking mode: the communication is performed in polling mode.
  299. The HAL status of all data processing is returned by the same function
  300. after finishing transfer.
  301. (++) No-Blocking mode: the communication is performed using Interrupts
  302. or DMA, these API's return the HAL status.
  303. The end of the data processing will be indicated through the
  304. dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
  305. using DMA mode.
  306. The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
  307. will be executed respectively at the end of the Transmit or Receive process
  308. The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
  309. (#) Blocking mode API's are :
  310. (++) HAL_IRDA_Transmit()
  311. (++) HAL_IRDA_Receive()
  312. (#) Non-Blocking mode API's with Interrupt are :
  313. (++) HAL_IRDA_Transmit_IT()
  314. (++) HAL_IRDA_Receive_IT()
  315. (++) HAL_IRDA_IRQHandler()
  316. (++) IRDA_Transmit_IT()
  317. (++) IRDA_Receive_IT()
  318. (#) Non-Blocking mode functions with DMA are :
  319. (++) HAL_IRDA_Transmit_DMA()
  320. (++) HAL_IRDA_Receive_DMA()
  321. (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
  322. (++) HAL_IRDA_TxCpltCallback()
  323. (++) HAL_IRDA_RxCpltCallback()
  324. (++) HAL_IRDA_ErrorCallback()
  325. @endverbatim
  326. * @{
  327. */
  328. /**
  329. * @brief Sends an amount of data in blocking mode.
  330. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  331. * the configuration information for the specified IRDA module.
  332. * @param pData: Pointer to data buffer
  333. * @param Size: Amount of data to be sent
  334. * @param Timeout: Specify timeout value
  335. * @retval HAL status
  336. */
  337. HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  338. {
  339. uint16_t* tmp;
  340. if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_RX))
  341. {
  342. if((pData == NULL) || (Size == 0))
  343. {
  344. return HAL_ERROR;
  345. }
  346. /* Process Locked */
  347. __HAL_LOCK(hirda);
  348. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  349. if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  350. {
  351. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  352. }
  353. else
  354. {
  355. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  356. }
  357. hirda->TxXferSize = Size;
  358. hirda->TxXferCount = Size;
  359. while(hirda->TxXferCount > 0)
  360. {
  361. hirda->TxXferCount--;
  362. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
  363. {
  364. return HAL_TIMEOUT;
  365. }
  366. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  367. {
  368. tmp = (uint16_t*) pData;
  369. hirda->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  370. pData +=2;
  371. }
  372. else
  373. {
  374. hirda->Instance->TDR = (*pData++ & (uint8_t)0xFF);
  375. }
  376. }
  377. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, Timeout) != HAL_OK)
  378. {
  379. return HAL_TIMEOUT;
  380. }
  381. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  382. {
  383. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  384. }
  385. else
  386. {
  387. hirda->State = HAL_IRDA_STATE_READY;
  388. }
  389. /* Process Unlocked */
  390. __HAL_UNLOCK(hirda);
  391. return HAL_OK;
  392. }
  393. else
  394. {
  395. return HAL_BUSY;
  396. }
  397. }
  398. /**
  399. * @brief Receive an amount of data in blocking mode.
  400. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  401. * the configuration information for the specified IRDA module.
  402. * @param pData: Pointer to data buffer
  403. * @param Size: Amount of data to be received
  404. * @param Timeout: Specify timeout value
  405. * @retval HAL status
  406. */
  407. HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  408. {
  409. uint16_t* tmp;
  410. uint16_t uhMask;
  411. if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_TX))
  412. {
  413. if((pData == NULL) || (Size == 0))
  414. {
  415. return HAL_ERROR;
  416. }
  417. /* Process Locked */
  418. __HAL_LOCK(hirda);
  419. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  420. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  421. {
  422. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  423. }
  424. else
  425. {
  426. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  427. }
  428. hirda->RxXferSize = Size;
  429. hirda->RxXferCount = Size;
  430. /* Computation of the mask to apply to the RDR register
  431. of the UART associated to the IRDA */
  432. IRDA_MASK_COMPUTATION(hirda);
  433. uhMask = hirda->Mask;
  434. /* Check data remaining to be received */
  435. while(hirda->RxXferCount > 0)
  436. {
  437. hirda->RxXferCount--;
  438. if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  439. {
  440. return HAL_TIMEOUT;
  441. }
  442. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  443. {
  444. tmp = (uint16_t*) pData ;
  445. *tmp = (uint16_t)(hirda->Instance->RDR & uhMask);
  446. pData +=2;
  447. }
  448. else
  449. {
  450. *pData++ = (uint8_t)(hirda->Instance->RDR & (uint8_t)uhMask);
  451. }
  452. }
  453. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  454. {
  455. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  456. }
  457. else
  458. {
  459. hirda->State = HAL_IRDA_STATE_READY;
  460. }
  461. /* Process Unlocked */
  462. __HAL_UNLOCK(hirda);
  463. return HAL_OK;
  464. }
  465. else
  466. {
  467. return HAL_BUSY;
  468. }
  469. }
  470. /**
  471. * @brief Send an amount of data in non blocking mode.
  472. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  473. * the configuration information for the specified IRDA module.
  474. * @param pData: Pointer to data buffer
  475. * @param Size: Amount of data to be sent
  476. * @retval HAL status
  477. */
  478. HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  479. {
  480. if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_RX))
  481. {
  482. if((pData == NULL) || (Size == 0))
  483. {
  484. return HAL_ERROR;
  485. }
  486. /* Process Locked */
  487. __HAL_LOCK(hirda);
  488. hirda->pTxBuffPtr = pData;
  489. hirda->TxXferSize = Size;
  490. hirda->TxXferCount = Size;
  491. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  492. if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  493. {
  494. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  495. }
  496. else
  497. {
  498. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  499. }
  500. /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  501. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
  502. /* Process Unlocked */
  503. __HAL_UNLOCK(hirda);
  504. /* Enable the IRDA Transmit Complete Interrupt */
  505. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TC);
  506. return HAL_OK;
  507. }
  508. else
  509. {
  510. return HAL_BUSY;
  511. }
  512. }
  513. /**
  514. * @brief Receives an amount of data in non blocking mode.
  515. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  516. * the configuration information for the specified IRDA module.
  517. * @param pData: Pointer to data buffer
  518. * @param Size: Amount of data to be received
  519. * @retval HAL status
  520. */
  521. HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  522. {
  523. if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_TX))
  524. {
  525. if((pData == NULL) || (Size == 0))
  526. {
  527. return HAL_ERROR;
  528. }
  529. /* Process Locked */
  530. __HAL_LOCK(hirda);
  531. hirda->pRxBuffPtr = pData;
  532. hirda->RxXferSize = Size;
  533. hirda->RxXferCount = Size;
  534. /* Computation of the mask to apply to the RDR register
  535. of the UART associated to the IRDA */
  536. IRDA_MASK_COMPUTATION(hirda);
  537. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  538. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  539. {
  540. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  541. }
  542. else
  543. {
  544. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  545. }
  546. /* Enable the IRDA Parity Error Interrupt */
  547. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_PE);
  548. /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  549. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
  550. /* Process Unlocked */
  551. __HAL_UNLOCK(hirda);
  552. /* Enable the IRDA Data Register not empty Interrupt */
  553. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_RXNE);
  554. return HAL_OK;
  555. }
  556. else
  557. {
  558. return HAL_BUSY;
  559. }
  560. }
  561. /**
  562. * @brief Sends an amount of data in non blocking mode.
  563. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  564. * the configuration information for the specified IRDA module.
  565. * @param pData: Pointer to data buffer
  566. * @param Size: Amount of data to be sent
  567. * @retval HAL status
  568. */
  569. HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  570. {
  571. uint32_t *tmp;
  572. if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_RX))
  573. {
  574. if((pData == NULL) || (Size == 0))
  575. {
  576. return HAL_ERROR;
  577. }
  578. /* Process Locked */
  579. __HAL_LOCK(hirda);
  580. hirda->pTxBuffPtr = pData;
  581. hirda->TxXferSize = Size;
  582. hirda->TxXferCount = Size;
  583. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  584. if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  585. {
  586. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  587. }
  588. else
  589. {
  590. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  591. }
  592. /* Set the IRDA DMA transfer complete callback */
  593. hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
  594. /* Set the IRDA DMA half transfer complete callback */
  595. hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;
  596. /* Set the DMA error callback */
  597. hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
  598. /* Enable the IRDA transmit DMA channel */
  599. tmp = (uint32_t*)&pData;
  600. HAL_DMA_Start_IT(hirda->hdmatx, *(uint32_t*)tmp, (uint32_t)&hirda->Instance->TDR, Size);
  601. /* Clear the TC flag in the SR register by writing 0 to it */
  602. __HAL_IRDA_CLEAR_IT(hirda, IRDA_FLAG_TC);
  603. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  604. in the IRDA CR3 register */
  605. hirda->Instance->CR3 |= USART_CR3_DMAT;
  606. /* Process Unlocked */
  607. __HAL_UNLOCK(hirda);
  608. return HAL_OK;
  609. }
  610. else
  611. {
  612. return HAL_BUSY;
  613. }
  614. }
  615. /**
  616. * @brief Receives an amount of data in non blocking mode.
  617. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  618. * the configuration information for the specified IRDA module.
  619. * @param pData: Pointer to data buffer
  620. * @param Size: Amount of data to be received
  621. * @note When the IRDA parity is enabled (PCE = 1) the data received contain the parity bit.
  622. * @retval HAL status
  623. */
  624. HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
  625. {
  626. uint32_t *tmp;
  627. if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_TX))
  628. {
  629. if((pData == NULL) || (Size == 0))
  630. {
  631. return HAL_ERROR;
  632. }
  633. /* Process Locked */
  634. __HAL_LOCK(hirda);
  635. hirda->pRxBuffPtr = pData;
  636. hirda->RxXferSize = Size;
  637. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  638. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  639. {
  640. hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
  641. }
  642. else
  643. {
  644. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  645. }
  646. /* Set the IRDA DMA transfer complete callback */
  647. hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
  648. /* Set the IRDA DMA half transfer complete callback */
  649. hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;
  650. /* Set the DMA error callback */
  651. hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
  652. /* Enable the DMA channel */
  653. tmp = (uint32_t*)&pData;
  654. HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->RDR, *(uint32_t*)tmp, Size);
  655. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  656. in the IRDA CR3 register */
  657. hirda->Instance->CR3 |= USART_CR3_DMAR;
  658. /* Process Unlocked */
  659. __HAL_UNLOCK(hirda);
  660. return HAL_OK;
  661. }
  662. else
  663. {
  664. return HAL_BUSY;
  665. }
  666. }
  667. /**
  668. * @brief Pauses the DMA Transfer.
  669. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  670. * the configuration information for the specified IRDA module.
  671. * @retval HAL status
  672. */
  673. HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda)
  674. {
  675. /* Process Locked */
  676. __HAL_LOCK(hirda);
  677. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  678. {
  679. /* Disable the UART DMA Tx request */
  680. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  681. }
  682. else if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  683. {
  684. /* Disable the UART DMA Rx request */
  685. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  686. }
  687. else if (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  688. {
  689. /* Disable the UART DMA Tx & Rx requests */
  690. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  691. hirda->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  692. }
  693. /* Process Unlocked */
  694. __HAL_UNLOCK(hirda);
  695. return HAL_OK;
  696. }
  697. /**
  698. * @brief Resumes the DMA Transfer.
  699. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  700. * the configuration information for the specified UART module.
  701. * @retval HAL status
  702. */
  703. HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda)
  704. {
  705. /* Process Locked */
  706. __HAL_LOCK(hirda);
  707. if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
  708. {
  709. /* Enable the UART DMA Tx request */
  710. hirda->Instance->CR3 |= USART_CR3_DMAT;
  711. }
  712. else if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
  713. {
  714. /* Clear the Overrun flag before resuming the Rx transfer*/
  715. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_OREF);
  716. /* Enable the UART DMA Rx request */
  717. hirda->Instance->CR3 |= USART_CR3_DMAR;
  718. }
  719. else if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  720. {
  721. /* Clear the Overrun flag before resuming the Rx transfer*/
  722. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_OREF);
  723. /* Enable the UART DMA Tx & Rx request */
  724. hirda->Instance->CR3 |= USART_CR3_DMAT;
  725. hirda->Instance->CR3 |= USART_CR3_DMAR;
  726. }
  727. /* Process Unlocked */
  728. __HAL_UNLOCK(hirda);
  729. return HAL_OK;
  730. }
  731. /**
  732. * @brief Stops the DMA Transfer.
  733. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  734. * the configuration information for the specified UART module.
  735. * @retval HAL status
  736. */
  737. HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda)
  738. {
  739. /* The Lock is not implemented on this API to allow the user application
  740. to call the HAL IRDA API under callbacks HAL_IRDA_TxCpltCallback() / HAL_IRDA_RxCpltCallback() /
  741. HAL_IRDA_TxHalfCpltCallback / HAL_IRDA_RxHalfCpltCallback:
  742. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  743. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  744. the stream and the corresponding call back is executed. */
  745. /* Disable the UART Tx/Rx DMA requests */
  746. hirda->Instance->CR3 &= ~USART_CR3_DMAT;
  747. hirda->Instance->CR3 &= ~USART_CR3_DMAR;
  748. /* Abort the UART DMA tx channel */
  749. if(hirda->hdmatx != NULL)
  750. {
  751. HAL_DMA_Abort(hirda->hdmatx);
  752. }
  753. /* Abort the UART DMA rx channel */
  754. if(hirda->hdmarx != NULL)
  755. {
  756. HAL_DMA_Abort(hirda->hdmarx);
  757. }
  758. hirda->State = HAL_IRDA_STATE_READY;
  759. return HAL_OK;
  760. }
  761. /**
  762. * @brief This function handles IRDA interrupt request.
  763. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  764. * the configuration information for the specified IRDA module.
  765. * @retval None
  766. */
  767. void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
  768. {
  769. /* IRDA parity error interrupt occurred -------------------------------------*/
  770. if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_PE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_PE) != RESET))
  771. {
  772. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_PEF);
  773. hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
  774. /* Set the IRDA state ready to be able to start again the process */
  775. hirda->State = HAL_IRDA_STATE_READY;
  776. }
  777. /* IRDA frame error interrupt occurred --------------------------------------*/
  778. if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_FE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR) != RESET))
  779. {
  780. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_FEF);
  781. hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
  782. /* Set the IRDA state ready to be able to start again the process */
  783. hirda->State = HAL_IRDA_STATE_READY;
  784. }
  785. /* IRDA noise error interrupt occurred --------------------------------------*/
  786. if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_NE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR) != RESET))
  787. {
  788. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_NEF);
  789. hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
  790. /* Set the IRDA state ready to be able to start again the process */
  791. hirda->State = HAL_IRDA_STATE_READY;
  792. }
  793. /* IRDA Over-Run interrupt occurred -----------------------------------------*/
  794. if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_ORE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR) != RESET))
  795. {
  796. __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_OREF);
  797. hirda->ErrorCode |= HAL_IRDA_ERROR_ORE;
  798. /* Set the IRDA state ready to be able to start again the process */
  799. hirda->State = HAL_IRDA_STATE_READY;
  800. }
  801. /* Call IRDA Error Call back function if need be --------------------------*/
  802. if(hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
  803. {
  804. HAL_IRDA_ErrorCallback(hirda);
  805. }
  806. /* IRDA in mode Receiver ---------------------------------------------------*/
  807. if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_RXNE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_RXNE) != RESET))
  808. {
  809. IRDA_Receive_IT(hirda);
  810. /* Clear RXNE interrupt flag */
  811. __HAL_IRDA_SEND_REQ(hirda, IRDA_RXDATA_FLUSH_REQUEST);
  812. }
  813. /* IRDA in mode Transmitter ------------------------------------------------*/
  814. if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_TXE) != RESET) &&(__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TXE) != RESET))
  815. {
  816. IRDA_Transmit_IT(hirda);
  817. }
  818. /* IRDA in mode Transmitter (transmission end) -----------------------------*/
  819. if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_TC) != RESET) &&(__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TC) != RESET))
  820. {
  821. IRDA_EndTransmit_IT(hirda);
  822. }
  823. }
  824. /**
  825. * @brief Tx Transfer complete callbacks.
  826. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  827. * the configuration information for the specified IRDA module.
  828. * @retval None
  829. */
  830. __weak void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  831. {
  832. /* NOTE : This function should not be modified, when the callback is needed,
  833. the HAL_IRDA_TxHalfCpltCallback can be implemented in the user file
  834. */
  835. }
  836. /**
  837. * @brief Tx Half Transfer completed callbacks.
  838. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  839. * the configuration information for the specified USART module.
  840. * @retval None
  841. */
  842. __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
  843. {
  844. /* NOTE : This function should not be modified, when the callback is needed,
  845. the HAL_IRDA_TxCpltCallback can be implemented in the user file
  846. */
  847. }
  848. /**
  849. * @brief Rx Transfer complete callbacks.
  850. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  851. * the configuration information for the specified IRDA module.
  852. * @retval None
  853. */
  854. __weak void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef *hirda)
  855. {
  856. /* NOTE : This function should not be modified, when the callback is needed,
  857. the HAL_IRDA_RxHalfCpltCallback can be implemented in the user file
  858. */
  859. }
  860. /**
  861. * @brief Rx Half Transfer complete callbacks.
  862. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  863. * the configuration information for the specified IRDA module.
  864. * @retval None
  865. */
  866. __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
  867. {
  868. /* NOTE : This function should not be modified, when the callback is needed,
  869. the HAL_IRDA_RxCpltCallback can be implemented in the user file
  870. */
  871. }
  872. /**
  873. * @brief IRDA error callbacks.
  874. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  875. * the configuration information for the specified IRDA module.
  876. * @retval None
  877. */
  878. __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
  879. {
  880. /* NOTE : This function should not be modified, when the callback is needed,
  881. the HAL_IRDA_ErrorCallback can be implemented in the user file
  882. */
  883. }
  884. /**
  885. * @}
  886. */
  887. /** @defgroup IRDA_Exported_Functions_Group3 Peripheral Control functions
  888. * @brief IRDA control functions
  889. *
  890. @verbatim
  891. ===============================================================================
  892. ##### Peripheral Control functions #####
  893. ===============================================================================
  894. [..]
  895. This subsection provides a set of functions allowing to control the IRDA.
  896. (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state of the IRDA peripheral.
  897. (+) IRDA_SetConfig() API is used to configure the IRDA communications parameters.
  898. @endverbatim
  899. * @{
  900. */
  901. /**
  902. * @brief Returns the IRDA state.
  903. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  904. * the configuration information for the specified IRDA module.
  905. * @retval HAL state
  906. */
  907. HAL_IRDA_StateTypeDef HAL_IRDA_GetState(IRDA_HandleTypeDef *hirda)
  908. {
  909. return hirda->State;
  910. }
  911. /**
  912. * @brief Return the IRDA error code
  913. * @param hirda : pointer to a IRDA_HandleTypeDef structure that contains
  914. * the configuration information for the specified IRDA.
  915. * @retval IRDA Error Code
  916. */
  917. uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef *hirda)
  918. {
  919. return hirda->ErrorCode;
  920. }
  921. /**
  922. * @}
  923. */
  924. /**
  925. * @brief Configure the IRDA peripheral
  926. * @param hirda: irda handle
  927. * @retval None
  928. */
  929. static void IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
  930. {
  931. uint32_t tmpreg = 0x00000000;
  932. uint32_t clocksource = 0x00000000;
  933. /* Check the communication parameters */
  934. assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));
  935. assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
  936. assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
  937. assert_param(IS_IRDA_TX_RX_MODE(hirda->Init.Mode));
  938. assert_param(IS_IRDA_PRESCALER(hirda->Init.Prescaler));
  939. assert_param(IS_IRDA_POWERMODE(hirda->Init.PowerMode));
  940. /*-------------------------- USART CR1 Configuration -----------------------*/
  941. /* Configure the IRDA Word Length, Parity and transfer Mode:
  942. Set the M bits according to hirda->Init.WordLength value
  943. Set PCE and PS bits according to hirda->Init.Parity value
  944. Set TE and RE bits according to hirda->Init.Mode value */
  945. tmpreg = (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode ;
  946. MODIFY_REG(hirda->Instance->CR1, IRDA_CR1_FIELDS, tmpreg);
  947. /*-------------------------- USART CR3 Configuration -----------------------*/
  948. MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.PowerMode);
  949. /*-------------------------- USART GTPR Configuration ----------------------*/
  950. MODIFY_REG(hirda->Instance->GTPR, (uint32_t)USART_GTPR_PSC, hirda->Init.Prescaler);
  951. /*-------------------------- USART BRR Configuration -----------------------*/
  952. IRDA_GETCLOCKSOURCE(hirda, clocksource);
  953. switch (clocksource)
  954. {
  955. case IRDA_CLOCKSOURCE_PCLK1:
  956. hirda->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK1Freq() / hirda->Init.BaudRate);
  957. break;
  958. case IRDA_CLOCKSOURCE_PCLK2:
  959. hirda->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK2Freq() / hirda->Init.BaudRate);
  960. break;
  961. case IRDA_CLOCKSOURCE_HSI:
  962. hirda->Instance->BRR = (uint16_t)(HSI_VALUE / hirda->Init.BaudRate);
  963. break;
  964. case IRDA_CLOCKSOURCE_SYSCLK:
  965. hirda->Instance->BRR = (uint16_t)(HAL_RCC_GetSysClockFreq() / hirda->Init.BaudRate);
  966. break;
  967. case IRDA_CLOCKSOURCE_LSE:
  968. hirda->Instance->BRR = (uint16_t)(LSE_VALUE / hirda->Init.BaudRate);
  969. break;
  970. default:
  971. break;
  972. }
  973. }
  974. /**
  975. * @brief Check the IRDA Idle State
  976. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  977. * the configuration information for the specified IRDA module.
  978. * @retval HAL status
  979. */
  980. static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda)
  981. {
  982. /* Initialize the IRDA ErrorCode */
  983. hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
  984. /* Check if the Transmitter is enabled */
  985. if((hirda->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  986. {
  987. /* Wait until TEACK flag is set */
  988. if(IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_TEACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
  989. {
  990. hirda->State= HAL_IRDA_STATE_TIMEOUT;
  991. return HAL_TIMEOUT;
  992. }
  993. }
  994. /* Check if the Receiver is enabled */
  995. if((hirda->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  996. {
  997. if(IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_REACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
  998. {
  999. hirda->State= HAL_IRDA_STATE_TIMEOUT;
  1000. return HAL_TIMEOUT;
  1001. }
  1002. }
  1003. /* Process Unlocked */
  1004. __HAL_UNLOCK(hirda);
  1005. /* Initialize the IRDA state*/
  1006. hirda->State= HAL_IRDA_STATE_READY;
  1007. return HAL_OK;
  1008. }
  1009. /**
  1010. * @brief This function handles IRDA Communication Timeout.
  1011. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1012. * the configuration information for the specified IRDA module.
  1013. * @param Flag: specifies the IRDA flag to check.
  1014. * @param Status: The new Flag status (SET or RESET).
  1015. * @param Timeout: Timeout duration
  1016. * @retval HAL status
  1017. */
  1018. static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1019. {
  1020. uint32_t tickstart = 0x00;
  1021. tickstart = HAL_GetTick();
  1022. /* Wait until flag is set */
  1023. if(Status == RESET)
  1024. {
  1025. while(__HAL_IRDA_GET_FLAG(hirda, Flag) == RESET)
  1026. {
  1027. /* Check for the Timeout */
  1028. if(Timeout != HAL_MAX_DELAY)
  1029. {
  1030. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1031. {
  1032. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1033. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
  1034. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
  1035. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  1036. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1037. hirda->State= HAL_IRDA_STATE_READY;
  1038. /* Process Unlocked */
  1039. __HAL_UNLOCK(hirda);
  1040. return HAL_TIMEOUT;
  1041. }
  1042. }
  1043. }
  1044. }
  1045. else
  1046. {
  1047. while(__HAL_IRDA_GET_FLAG(hirda, Flag) != RESET)
  1048. {
  1049. /* Check for the Timeout */
  1050. if(Timeout != HAL_MAX_DELAY)
  1051. {
  1052. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1053. {
  1054. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1055. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
  1056. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
  1057. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  1058. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1059. hirda->State= HAL_IRDA_STATE_READY;
  1060. /* Process Unlocked */
  1061. __HAL_UNLOCK(hirda);
  1062. return HAL_TIMEOUT;
  1063. }
  1064. }
  1065. }
  1066. }
  1067. return HAL_OK;
  1068. }
  1069. /**
  1070. * @brief Send an amount of data in non blocking mode.
  1071. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1072. * the configuration information for the specified IRDA module.
  1073. * @retval HAL status
  1074. */
  1075. static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
  1076. {
  1077. uint16_t* tmp;
  1078. if((hirda->State == HAL_IRDA_STATE_BUSY_TX) || (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX))
  1079. {
  1080. if(hirda->TxXferCount == 0)
  1081. {
  1082. /* Disable the IRDA Transmit Complete Interrupt */
  1083. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
  1084. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  1085. {
  1086. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  1087. }
  1088. else
  1089. {
  1090. /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  1091. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1092. hirda->State = HAL_IRDA_STATE_READY;
  1093. }
  1094. HAL_IRDA_TxCpltCallback(hirda);
  1095. return HAL_OK;
  1096. }
  1097. else
  1098. {
  1099. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  1100. {
  1101. tmp = (uint16_t*) hirda->pTxBuffPtr;
  1102. hirda->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  1103. hirda->pTxBuffPtr += 2;
  1104. }
  1105. else
  1106. {
  1107. hirda->Instance->TDR = (uint8_t)(*hirda->pTxBuffPtr++ & (uint8_t)0xFF);
  1108. }
  1109. hirda->TxXferCount--;
  1110. return HAL_OK;
  1111. }
  1112. }
  1113. else
  1114. {
  1115. return HAL_BUSY;
  1116. }
  1117. }
  1118. /**
  1119. * @brief Wraps up transmission in non blocking mode.
  1120. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1121. * the configuration information for the specified IRDA module.
  1122. * @retval HAL status
  1123. */
  1124. static HAL_StatusTypeDef IRDA_EndTransmit_IT(IRDA_HandleTypeDef *hirda)
  1125. {
  1126. /* Disable the IRDA Transmit Complete Interrupt */
  1127. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TC);
  1128. /* Check if a receive process is ongoing or not */
  1129. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  1130. {
  1131. hirda->State = HAL_IRDA_STATE_BUSY_RX;
  1132. }
  1133. else
  1134. {
  1135. /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  1136. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1137. hirda->State = HAL_IRDA_STATE_READY;
  1138. }
  1139. HAL_IRDA_TxCpltCallback(hirda);
  1140. return HAL_OK;
  1141. }
  1142. /**
  1143. * @brief Receive an amount of data in non blocking mode.
  1144. * Function called under interruption only, once
  1145. * interruptions have been enabled by HAL_IRDA_Receive_IT()
  1146. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains
  1147. * the configuration information for the specified IRDA module.
  1148. * @retval HAL status
  1149. */
  1150. static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
  1151. {
  1152. uint16_t* tmp;
  1153. uint16_t uhMask = hirda->Mask;
  1154. if ((hirda->State == HAL_IRDA_STATE_BUSY_RX) || (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX))
  1155. {
  1156. if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
  1157. {
  1158. tmp = (uint16_t*) hirda->pRxBuffPtr ;
  1159. *tmp = (uint16_t)(hirda->Instance->RDR & uhMask);
  1160. hirda->pRxBuffPtr +=2;
  1161. }
  1162. else
  1163. {
  1164. *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->RDR & (uint8_t)uhMask);
  1165. }
  1166. if(--hirda->RxXferCount == 0)
  1167. {
  1168. while(HAL_IS_BIT_SET(hirda->Instance->ISR, IRDA_FLAG_RXNE))
  1169. {
  1170. }
  1171. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
  1172. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  1173. {
  1174. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  1175. }
  1176. else
  1177. {
  1178. /* Disable the IRDA Parity Error Interrupt */
  1179. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
  1180. /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
  1181. __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
  1182. hirda->State = HAL_IRDA_STATE_READY;
  1183. }
  1184. HAL_IRDA_RxCpltCallback(hirda);
  1185. return HAL_OK;
  1186. }
  1187. return HAL_OK;
  1188. }
  1189. else
  1190. {
  1191. return HAL_BUSY;
  1192. }
  1193. }
  1194. /**
  1195. * @brief DMA IRDA Tx transfer completed callback
  1196. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1197. * the configuration information for the specified DMA module.
  1198. * @retval None
  1199. */
  1200. static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1201. {
  1202. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1203. /* DMA Normal mode*/
  1204. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1205. {
  1206. hirda->TxXferCount = 0;
  1207. /* Disable the DMA transfer for transmit request by setting the DMAT bit
  1208. in the IRDA CR3 register */
  1209. hirda->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
  1210. /* Enable the IRDA Transmit Complete Interrupt */
  1211. __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TC);
  1212. }
  1213. /* DMA Circular mode */
  1214. else
  1215. {
  1216. HAL_IRDA_TxCpltCallback(hirda);
  1217. }
  1218. }
  1219. /**
  1220. * @brief DMA IRDA receive process half complete callback
  1221. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1222. * the configuration information for the specified DMA module.
  1223. * @retval None
  1224. */
  1225. static void IRDA_DMATransmitHalfCplt(DMA_HandleTypeDef *hdma)
  1226. {
  1227. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1228. HAL_IRDA_TxHalfCpltCallback(hirda);
  1229. }
  1230. /**
  1231. * @brief DMA IRDA Rx Transfer completed callback
  1232. * @param hdma: DMA handle
  1233. * @retval None
  1234. */
  1235. static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1236. {
  1237. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1238. /* DMA Normal mode */
  1239. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1240. {
  1241. hirda->RxXferCount = 0;
  1242. /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1243. in the IRDA CR3 register */
  1244. hirda->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAR);
  1245. if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
  1246. {
  1247. hirda->State = HAL_IRDA_STATE_BUSY_TX;
  1248. }
  1249. else
  1250. {
  1251. hirda->State = HAL_IRDA_STATE_READY;
  1252. }
  1253. }
  1254. HAL_IRDA_RxCpltCallback(hirda);
  1255. }
  1256. /**
  1257. * @brief DMA IRDA receive process half complete callback
  1258. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1259. * the configuration information for the specified DMA module.
  1260. * @retval None
  1261. */
  1262. static void IRDA_DMAReceiveHalfCplt(DMA_HandleTypeDef *hdma)
  1263. {
  1264. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1265. HAL_IRDA_RxHalfCpltCallback(hirda);
  1266. }
  1267. /**
  1268. * @brief DMA IRDA communication error callback
  1269. * @param hdma: DMA handle
  1270. * @retval None
  1271. */
  1272. static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
  1273. {
  1274. IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1275. hirda->RxXferCount = 0;
  1276. hirda->TxXferCount = 0;
  1277. hirda->State= HAL_IRDA_STATE_READY;
  1278. hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
  1279. HAL_IRDA_ErrorCallback(hirda);
  1280. }
  1281. /**
  1282. * @}
  1283. */
  1284. /**
  1285. * @}
  1286. */
  1287. #endif /* HAL_IRDA_MODULE_ENABLED */
  1288. /**
  1289. * @}
  1290. */
  1291. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/