source: asp3_wo_tecs/trunk/arch/arm_m_gcc/stm32f4xx_stm32cube/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_irda.c@ 303

Last change on this file since 303 was 303, checked in by ertl-honda, 7 years ago

nucleo_f401re依存部の追加

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