Subversion Repositories DashDisplay

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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