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