stm32l1xx_hal_irda.c 50 KB

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