Subversion Repositories FuelGauge

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f0xx_hal_tsc.c
4
  * @author  MCD Application Team
5
  * @brief   This file provides firmware functions to manage the following
6
  *          functionalities of the Touch Sensing Controller (TSC) peripheral:
7
  *           + Initialization and De-initialization
8
  *           + Channel IOs, Shield IOs and Sampling IOs configuration
9
  *           + Start and Stop an acquisition
10
  *           + Read acquisition result
11
  *           + Interrupts and flags management
12
  *
13
  @verbatim
14
================================================================================
15
                       ##### TSC specific features #####
16
================================================================================
17
  [..]
18
  (#) Proven and robust surface charge transfer acquisition principle
19
 
20
  (#) Supports up to 3 capacitive sensing channels per group
21
 
22
  (#) Capacitive sensing channels can be acquired in parallel offering a very good
23
      response time
24
 
25
  (#) Spread spectrum feature to improve system robustness in noisy environments
26
 
27
  (#) Full hardware management of the charge transfer acquisition sequence
28
 
29
  (#) Programmable charge transfer frequency
30
 
31
  (#) Programmable sampling capacitor I/O pin
32
 
33
  (#) Programmable channel I/O pin
34
 
35
  (#) Programmable max count value to avoid long acquisition when a channel is faulty
36
 
37
  (#) Dedicated end of acquisition and max count error flags with interrupt capability
38
 
39
  (#) One sampling capacitor for up to 3 capacitive sensing channels to reduce the system
40
      components
41
 
42
  (#) Compatible with proximity, touchkey, linear and rotary touch sensor implementation
43
 
44
                          ##### How to use this driver #####
45
================================================================================
46
  [..]
47
    (#) Enable the TSC interface clock using __HAL_RCC_TSC_CLK_ENABLE() macro.
48
 
49
    (#) GPIO pins configuration
50
      (++) Enable the clock for the TSC GPIOs using __HAL_RCC_GPIOx_CLK_ENABLE() macro.
51
      (++) Configure the TSC pins used as sampling IOs in alternate function output Open-Drain mode,
52
           and TSC pins used as channel/shield IOs in alternate function output Push-Pull mode
53
           using HAL_GPIO_Init() function.
54
 
55
    (#) Interrupts configuration
56
      (++) Configure the NVIC (if the interrupt model is used) using HAL_NVIC_SetPriority()
57
           and HAL_NVIC_EnableIRQ() and function.
58
 
59
    (#) TSC configuration
60
      (++) Configure all TSC parameters and used TSC IOs using HAL_TSC_Init() function.
61
 
62
 [..]   TSC peripheral alternate functions are mapped on AF9.
63
 
64
  *** Acquisition sequence ***
65
  ===================================
66
  [..]
67
    (+) Discharge all IOs using HAL_TSC_IODischarge() function.
68
    (+) Wait a certain time allowing a good discharge of all capacitors. This delay depends
69
        of the sampling capacitor and electrodes design.
70
    (+) Select the channel IOs to be acquired using HAL_TSC_IOConfig() function.
71
    (+) Launch the acquisition using either HAL_TSC_Start() or HAL_TSC_Start_IT() function.
72
        If the synchronized mode is selected, the acquisition will start as soon as the signal
73
        is received on the synchro pin.
74
    (+) Wait the end of acquisition using either HAL_TSC_PollForAcquisition() or
75
        HAL_TSC_GetState() function or using WFI instruction for example.
76
    (+) Check the group acquisition status using HAL_TSC_GroupGetStatus() function.
77
    (+) Read the acquisition value using HAL_TSC_GroupGetValue() function.
78
 
79
     *** Callback registration ***
80
     =============================================
81
 
82
  [..]
83
     The compilation flag USE_HAL_TSC_REGISTER_CALLBACKS when set to 1
84
     allows the user to configure dynamically the driver callbacks.
85
     Use Functions @ref HAL_TSC_RegisterCallback() to register an interrupt callback.
86
 
87
  [..]
88
     Function @ref HAL_TSC_RegisterCallback() allows to register following callbacks:
89
       (+) ConvCpltCallback   : callback for conversion complete process.
90
       (+) ErrorCallback      : callback for error detection.
91
       (+) MspInitCallback    : callback for Msp Init.
92
       (+) MspDeInitCallback  : callback for Msp DeInit.
93
  [..]
94
     This function takes as parameters the HAL peripheral handle, the Callback ID
95
     and a pointer to the user callback function.
96
 
97
  [..]
98
     Use function @ref HAL_TSC_UnRegisterCallback to reset a callback to the default
99
     weak function.
100
     @ref HAL_TSC_UnRegisterCallback takes as parameters the HAL peripheral handle,
101
     and the Callback ID.
102
  [..]
103
     This function allows to reset following callbacks:
104
       (+) ConvCpltCallback   : callback for conversion complete process.
105
       (+) ErrorCallback      : callback for error detection.
106
       (+) MspInitCallback    : callback for Msp Init.
107
       (+) MspDeInitCallback  : callback for Msp DeInit.
108
 
109
  [..]
110
     By default, after the @ref HAL_TSC_Init() and when the state is @ref HAL_TSC_STATE_RESET
111
     all callbacks are set to the corresponding weak functions:
112
     examples @ref HAL_TSC_ConvCpltCallback(), @ref HAL_TSC_ErrorCallback().
113
     Exception done for MspInit and MspDeInit functions that are
114
     reset to the legacy weak functions in the @ref HAL_TSC_Init()/ @ref HAL_TSC_DeInit() only when
115
     these callbacks are null (not registered beforehand).
116
     If MspInit or MspDeInit are not null, the @ref HAL_TSC_Init()/ @ref HAL_TSC_DeInit()
117
     keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
118
 
119
  [..]
120
     Callbacks can be registered/unregistered in @ref HAL_TSC_STATE_READY state only.
121
     Exception done MspInit/MspDeInit functions that can be registered/unregistered
122
     in @ref HAL_TSC_STATE_READY or @ref HAL_TSC_STATE_RESET state,
123
     thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
124
     Then, the user first registers the MspInit/MspDeInit user callbacks
125
     using @ref HAL_TSC_RegisterCallback() before calling @ref HAL_TSC_DeInit()
126
     or @ref HAL_TSC_Init() function.
127
 
128
  [..]
129
     When the compilation flag USE_HAL_TSC_REGISTER_CALLBACKS is set to 0 or
130
     not defined, the callback registration feature is not available and all callbacks
131
     are set to the corresponding weak functions.
132
 
133
  @endverbatim
134
  ******************************************************************************
135
  ******************************************************************************
136
  * @attention
137
  *
138
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
139
  * All rights reserved.</center></h2>
140
  *
141
  * This software component is licensed by ST under BSD 3-Clause license,
142
  * the "License"; You may not use this file except in compliance with the
143
  * License. You may obtain a copy of the License at:
144
  *                        opensource.org/licenses/BSD-3-Clause
145
  *
146
  ******************************************************************************
147
  */
148
 
149
/* Includes ------------------------------------------------------------------*/
150
#include "stm32f0xx_hal.h"
151
 
152
#if defined(TSC)
153
/** @addtogroup STM32F0xx_HAL_Driver
154
  * @{
155
  */
156
 
157
/** @defgroup TSC TSC
158
  * @brief HAL TSC module driver
159
  * @{
160
  */
161
 
162
#ifdef HAL_TSC_MODULE_ENABLED
163
 
164
/* Private typedef -----------------------------------------------------------*/
165
/* Private define ------------------------------------------------------------*/
166
/* Private macro -------------------------------------------------------------*/
167
/* Private variables ---------------------------------------------------------*/
168
/* Private function prototypes -----------------------------------------------*/
169
static uint32_t TSC_extract_groups(uint32_t iomask);
170
 
171
/* Exported functions --------------------------------------------------------*/
172
 
173
/** @defgroup TSC_Exported_Functions TSC Exported Functions
174
  * @{
175
  */
176
 
177
/** @defgroup TSC_Exported_Functions_Group1 Initialization and de-initialization functions
178
 *  @brief    Initialization and Configuration functions
179
 *
180
@verbatim
181
 ===============================================================================
182
              ##### Initialization and de-initialization functions #####
183
 ===============================================================================
184
    [..]  This section provides functions allowing to:
185
      (+) Initialize and configure the TSC.
186
      (+) De-initialize the TSC.
187
@endverbatim
188
  * @{
189
  */
190
 
191
/**
192
  * @brief  Initialize the TSC peripheral according to the specified parameters
193
  *         in the TSC_InitTypeDef structure and initialize the associated handle.
194
  * @param  htsc TSC handle
195
  * @retval HAL status
196
  */
197
HAL_StatusTypeDef HAL_TSC_Init(TSC_HandleTypeDef *htsc)
198
{
199
  /* Check TSC handle allocation */
200
  if (htsc == NULL)
201
  {
202
    return HAL_ERROR;
203
  }
204
 
205
  /* Check the parameters */
206
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
207
  assert_param(IS_TSC_CTPH(htsc->Init.CTPulseHighLength));
208
  assert_param(IS_TSC_CTPL(htsc->Init.CTPulseLowLength));
209
  assert_param(IS_TSC_SS(htsc->Init.SpreadSpectrum));
210
  assert_param(IS_TSC_SSD(htsc->Init.SpreadSpectrumDeviation));
211
  assert_param(IS_TSC_SS_PRESC(htsc->Init.SpreadSpectrumPrescaler));
212
  assert_param(IS_TSC_PG_PRESC(htsc->Init.PulseGeneratorPrescaler));
213
  assert_param(IS_TSC_MCV(htsc->Init.MaxCountValue));
214
  assert_param(IS_TSC_IODEF(htsc->Init.IODefaultMode));
215
  assert_param(IS_TSC_SYNC_POL(htsc->Init.SynchroPinPolarity));
216
  assert_param(IS_TSC_ACQ_MODE(htsc->Init.AcquisitionMode));
217
  assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
218
  assert_param(IS_TSC_GROUP(htsc->Init.ChannelIOs));
219
  assert_param(IS_TSC_GROUP(htsc->Init.ShieldIOs));
220
  assert_param(IS_TSC_GROUP(htsc->Init.SamplingIOs));
221
 
222
  if (htsc->State == HAL_TSC_STATE_RESET)
223
  {
224
    /* Allocate lock resource and initialize it */
225
    htsc->Lock = HAL_UNLOCKED;
226
 
227
#if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
228
    /* Init the TSC Callback settings */
229
    htsc->ConvCpltCallback  = HAL_TSC_ConvCpltCallback; /* Legacy weak ConvCpltCallback     */
230
    htsc->ErrorCallback     = HAL_TSC_ErrorCallback;    /* Legacy weak ErrorCallback        */
231
 
232
    if (htsc->MspInitCallback == NULL)
233
    {
234
      htsc->MspInitCallback = HAL_TSC_MspInit; /* Legacy weak MspInit  */
235
    }
236
 
237
    /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
238
    htsc->MspInitCallback(htsc);
239
#else
240
    /* Init the low level hardware : GPIO, CLOCK, CORTEX */
241
    HAL_TSC_MspInit(htsc);
242
#endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
243
  }
244
 
245
  /* Initialize the TSC state */
246
  htsc->State = HAL_TSC_STATE_BUSY;
247
 
248
  /*--------------------------------------------------------------------------*/
249
  /* Set TSC parameters */
250
 
251
  /* Enable TSC */
252
  htsc->Instance->CR = TSC_CR_TSCE;
253
 
254
  /* Set all functions */
255
  htsc->Instance->CR |= (htsc->Init.CTPulseHighLength |
256
                         htsc->Init.CTPulseLowLength |
257
                         (htsc->Init.SpreadSpectrumDeviation << TSC_CR_SSD_Pos) |
258
                         htsc->Init.SpreadSpectrumPrescaler |
259
                         htsc->Init.PulseGeneratorPrescaler |
260
                         htsc->Init.MaxCountValue |
261
                         htsc->Init.SynchroPinPolarity |
262
                         htsc->Init.AcquisitionMode);
263
 
264
  /* Spread spectrum */
265
  if (htsc->Init.SpreadSpectrum == ENABLE)
266
  {
267
    htsc->Instance->CR |= TSC_CR_SSE;
268
  }
269
 
270
  /* Disable Schmitt trigger hysteresis on all used TSC IOs */
271
  htsc->Instance->IOHCR = (~(htsc->Init.ChannelIOs | htsc->Init.ShieldIOs | htsc->Init.SamplingIOs));
272
 
273
  /* Set channel and shield IOs */
274
  htsc->Instance->IOCCR = (htsc->Init.ChannelIOs | htsc->Init.ShieldIOs);
275
 
276
  /* Set sampling IOs */
277
  htsc->Instance->IOSCR = htsc->Init.SamplingIOs;
278
 
279
  /* Set the groups to be acquired */
280
  htsc->Instance->IOGCSR = TSC_extract_groups(htsc->Init.ChannelIOs);
281
 
282
  /* Disable interrupts */
283
  htsc->Instance->IER &= (~(TSC_IT_EOA | TSC_IT_MCE));
284
 
285
  /* Clear flags */
286
  htsc->Instance->ICR = (TSC_FLAG_EOA | TSC_FLAG_MCE);
287
 
288
  /*--------------------------------------------------------------------------*/
289
 
290
  /* Initialize the TSC state */
291
  htsc->State = HAL_TSC_STATE_READY;
292
 
293
  /* Return function status */
294
  return HAL_OK;
295
}
296
 
297
/**
298
  * @brief  Deinitialize the TSC peripheral registers to their default reset values.
299
  * @param  htsc TSC handle
300
  * @retval HAL status
301
  */
302
HAL_StatusTypeDef HAL_TSC_DeInit(TSC_HandleTypeDef *htsc)
303
{
304
  /* Check TSC handle allocation */
305
  if (htsc == NULL)
306
  {
307
    return HAL_ERROR;
308
  }
309
 
310
  /* Check the parameters */
311
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
312
 
313
  /* Change TSC state */
314
  htsc->State = HAL_TSC_STATE_BUSY;
315
 
316
#if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
317
  if (htsc->MspDeInitCallback == NULL)
318
  {
319
    htsc->MspDeInitCallback = HAL_TSC_MspDeInit; /* Legacy weak MspDeInit  */
320
  }
321
 
322
  /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
323
  htsc->MspDeInitCallback(htsc);
324
#else
325
  /* DeInit the low level hardware */
326
  HAL_TSC_MspDeInit(htsc);
327
#endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
328
 
329
  /* Change TSC state */
330
  htsc->State = HAL_TSC_STATE_RESET;
331
 
332
  /* Process unlocked */
333
  __HAL_UNLOCK(htsc);
334
 
335
  /* Return function status */
336
  return HAL_OK;
337
}
338
 
339
/**
340
  * @brief  Initialize the TSC MSP.
341
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
342
  *         the configuration information for the specified TSC.
343
  * @retval None
344
  */
345
__weak void HAL_TSC_MspInit(TSC_HandleTypeDef *htsc)
346
{
347
  /* Prevent unused argument(s) compilation warning */
348
  UNUSED(htsc);
349
 
350
  /* NOTE : This function should not be modified, when the callback is needed,
351
            the HAL_TSC_MspInit could be implemented in the user file.
352
   */
353
}
354
 
355
/**
356
  * @brief  DeInitialize the TSC MSP.
357
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
358
  *         the configuration information for the specified TSC.
359
  * @retval None
360
  */
361
__weak void HAL_TSC_MspDeInit(TSC_HandleTypeDef *htsc)
362
{
363
  /* Prevent unused argument(s) compilation warning */
364
  UNUSED(htsc);
365
 
366
  /* NOTE : This function should not be modified, when the callback is needed,
367
            the HAL_TSC_MspDeInit could be implemented in the user file.
368
   */
369
}
370
 
371
#if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
372
/**
373
  * @brief  Register a User TSC Callback
374
  *         To be used instead of the weak predefined callback
375
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
376
  *                the configuration information for the specified TSC.
377
  * @param  CallbackID ID of the callback to be registered
378
  *         This parameter can be one of the following values:
379
  *          @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
380
  *          @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
381
  *          @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
382
  *          @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
383
  * @param  pCallback pointer to the Callback function
384
  * @retval HAL status
385
  */
386
HAL_StatusTypeDef HAL_TSC_RegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID, pTSC_CallbackTypeDef pCallback)
387
{
388
  HAL_StatusTypeDef status = HAL_OK;
389
 
390
  if (pCallback == NULL)
391
  {
392
    /* Update the error code */
393
    htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
394
 
395
    return HAL_ERROR;
396
  }
397
  /* Process locked */
398
  __HAL_LOCK(htsc);
399
 
400
  if (HAL_TSC_STATE_READY == htsc->State)
401
  {
402
    switch (CallbackID)
403
    {
404
      case HAL_TSC_CONV_COMPLETE_CB_ID :
405
        htsc->ConvCpltCallback = pCallback;
406
        break;
407
 
408
      case HAL_TSC_ERROR_CB_ID :
409
        htsc->ErrorCallback = pCallback;
410
        break;
411
 
412
      case HAL_TSC_MSPINIT_CB_ID :
413
        htsc->MspInitCallback = pCallback;
414
        break;
415
 
416
      case HAL_TSC_MSPDEINIT_CB_ID :
417
        htsc->MspDeInitCallback = pCallback;
418
        break;
419
 
420
      default :
421
        /* Update the error code */
422
        htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
423
 
424
        /* Return error status */
425
        status =  HAL_ERROR;
426
        break;
427
    }
428
  }
429
  else if (HAL_TSC_STATE_RESET == htsc->State)
430
  {
431
    switch (CallbackID)
432
    {
433
      case HAL_TSC_MSPINIT_CB_ID :
434
        htsc->MspInitCallback = pCallback;
435
        break;
436
 
437
      case HAL_TSC_MSPDEINIT_CB_ID :
438
        htsc->MspDeInitCallback = pCallback;
439
        break;
440
 
441
      default :
442
        /* Update the error code */
443
        htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
444
 
445
        /* Return error status */
446
        status =  HAL_ERROR;
447
        break;
448
    }
449
  }
450
  else
451
  {
452
    /* Update the error code */
453
    htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
454
 
455
    /* Return error status */
456
    status =  HAL_ERROR;
457
  }
458
 
459
  /* Release Lock */
460
  __HAL_UNLOCK(htsc);
461
  return status;
462
}
463
 
464
/**
465
  * @brief  Unregister an TSC Callback
466
  *         TSC callback is redirected to the weak predefined callback
467
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
468
  *                the configuration information for the specified TSC.
469
  * @param  CallbackID ID of the callback to be unregistered
470
  *         This parameter can be one of the following values:
471
  *         This parameter can be one of the following values:
472
  *          @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
473
  *          @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
474
  *          @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
475
  *          @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
476
  * @retval HAL status
477
  */
478
HAL_StatusTypeDef HAL_TSC_UnRegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID)
479
{
480
  HAL_StatusTypeDef status = HAL_OK;
481
 
482
  /* Process locked */
483
  __HAL_LOCK(htsc);
484
 
485
  if (HAL_TSC_STATE_READY == htsc->State)
486
  {
487
    switch (CallbackID)
488
    {
489
      case HAL_TSC_CONV_COMPLETE_CB_ID :
490
        htsc->ConvCpltCallback = HAL_TSC_ConvCpltCallback;       /* Legacy weak ConvCpltCallback      */
491
        break;
492
 
493
      case HAL_TSC_ERROR_CB_ID :
494
        htsc->ErrorCallback = HAL_TSC_ErrorCallback;               /* Legacy weak ErrorCallback        */
495
        break;
496
 
497
      case HAL_TSC_MSPINIT_CB_ID :
498
        htsc->MspInitCallback = HAL_TSC_MspInit;                   /* Legacy weak MspInit              */
499
        break;
500
 
501
      case HAL_TSC_MSPDEINIT_CB_ID :
502
        htsc->MspDeInitCallback = HAL_TSC_MspDeInit;               /* Legacy weak MspDeInit            */
503
        break;
504
 
505
      default :
506
        /* Update the error code */
507
        htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
508
 
509
        /* Return error status */
510
        status =  HAL_ERROR;
511
        break;
512
    }
513
  }
514
  else if (HAL_TSC_STATE_RESET == htsc->State)
515
  {
516
    switch (CallbackID)
517
    {
518
      case HAL_TSC_MSPINIT_CB_ID :
519
        htsc->MspInitCallback = HAL_TSC_MspInit;                   /* Legacy weak MspInit              */
520
        break;
521
 
522
      case HAL_TSC_MSPDEINIT_CB_ID :
523
        htsc->MspDeInitCallback = HAL_TSC_MspDeInit;               /* Legacy weak MspDeInit            */
524
        break;
525
 
526
      default :
527
        /* Update the error code */
528
        htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
529
 
530
        /* Return error status */
531
        status =  HAL_ERROR;
532
        break;
533
    }
534
  }
535
  else
536
  {
537
    /* Update the error code */
538
    htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
539
 
540
    /* Return error status */
541
    status =  HAL_ERROR;
542
  }
543
 
544
  /* Release Lock */
545
  __HAL_UNLOCK(htsc);
546
  return status;
547
}
548
 
549
#endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
550
 
551
/**
552
  * @}
553
  */
554
 
555
/** @defgroup TSC_Exported_Functions_Group2 Input and Output operation functions
556
 *  @brief    Input and Output operation functions
557
 *
558
@verbatim
559
 ===============================================================================
560
             ##### IO Operation functions #####
561
 ===============================================================================
562
    [..]  This section provides functions allowing to:
563
      (+) Start acquisition in polling mode.
564
      (+) Start acquisition in interrupt mode.
565
      (+) Stop conversion in polling mode.
566
      (+) Stop conversion in interrupt mode.
567
      (+) Poll for acquisition completed.
568
      (+) Get group acquisition status.
569
      (+) Get group acquisition value.
570
@endverbatim
571
  * @{
572
  */
573
 
574
/**
575
  * @brief  Start the acquisition.
576
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
577
  *         the configuration information for the specified TSC.
578
  * @retval HAL status
579
  */
580
HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef *htsc)
581
{
582
  /* Check the parameters */
583
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
584
 
585
  /* Process locked */
586
  __HAL_LOCK(htsc);
587
 
588
  /* Change TSC state */
589
  htsc->State = HAL_TSC_STATE_BUSY;
590
 
591
  /* Clear interrupts */
592
  __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
593
 
594
  /* Clear flags */
595
  __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
596
 
597
  /* Set touch sensing IOs not acquired to the specified IODefaultMode */
598
  if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
599
  {
600
    __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
601
  }
602
  else
603
  {
604
    __HAL_TSC_SET_IODEF_INFLOAT(htsc);
605
  }
606
 
607
  /* Launch the acquisition */
608
  __HAL_TSC_START_ACQ(htsc);
609
 
610
  /* Process unlocked */
611
  __HAL_UNLOCK(htsc);
612
 
613
  /* Return function status */
614
  return HAL_OK;
615
}
616
 
617
/**
618
  * @brief  Start the acquisition in interrupt mode.
619
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
620
  *         the configuration information for the specified TSC.
621
  * @retval HAL status.
622
  */
623
HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef *htsc)
624
{
625
  /* Check the parameters */
626
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
627
  assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
628
 
629
  /* Process locked */
630
  __HAL_LOCK(htsc);
631
 
632
  /* Change TSC state */
633
  htsc->State = HAL_TSC_STATE_BUSY;
634
 
635
  /* Enable end of acquisition interrupt */
636
  __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA);
637
 
638
  /* Enable max count error interrupt (optional) */
639
  if (htsc->Init.MaxCountInterrupt == ENABLE)
640
  {
641
    __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE);
642
  }
643
  else
644
  {
645
    __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE);
646
  }
647
 
648
  /* Clear flags */
649
  __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
650
 
651
  /* Set touch sensing IOs not acquired to the specified IODefaultMode */
652
  if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
653
  {
654
    __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
655
  }
656
  else
657
  {
658
    __HAL_TSC_SET_IODEF_INFLOAT(htsc);
659
  }
660
 
661
  /* Launch the acquisition */
662
  __HAL_TSC_START_ACQ(htsc);
663
 
664
  /* Process unlocked */
665
  __HAL_UNLOCK(htsc);
666
 
667
  /* Return function status */
668
  return HAL_OK;
669
}
670
 
671
/**
672
  * @brief  Stop the acquisition previously launched in polling mode.
673
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
674
  *         the configuration information for the specified TSC.
675
  * @retval HAL status
676
  */
677
HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef *htsc)
678
{
679
  /* Check the parameters */
680
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
681
 
682
  /* Process locked */
683
  __HAL_LOCK(htsc);
684
 
685
  /* Stop the acquisition */
686
  __HAL_TSC_STOP_ACQ(htsc);
687
 
688
  /* Set touch sensing IOs in low power mode (output push-pull) */
689
  __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
690
 
691
  /* Clear flags */
692
  __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
693
 
694
  /* Change TSC state */
695
  htsc->State = HAL_TSC_STATE_READY;
696
 
697
  /* Process unlocked */
698
  __HAL_UNLOCK(htsc);
699
 
700
  /* Return function status */
701
  return HAL_OK;
702
}
703
 
704
/**
705
  * @brief  Stop the acquisition previously launched in interrupt mode.
706
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
707
  *         the configuration information for the specified TSC.
708
  * @retval HAL status
709
  */
710
HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef *htsc)
711
{
712
  /* Check the parameters */
713
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
714
 
715
  /* Process locked */
716
  __HAL_LOCK(htsc);
717
 
718
  /* Stop the acquisition */
719
  __HAL_TSC_STOP_ACQ(htsc);
720
 
721
  /* Set touch sensing IOs in low power mode (output push-pull) */
722
  __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
723
 
724
  /* Disable interrupts */
725
  __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
726
 
727
  /* Clear flags */
728
  __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
729
 
730
  /* Change TSC state */
731
  htsc->State = HAL_TSC_STATE_READY;
732
 
733
  /* Process unlocked */
734
  __HAL_UNLOCK(htsc);
735
 
736
  /* Return function status */
737
  return HAL_OK;
738
}
739
 
740
/**
741
  * @brief  Start acquisition and wait until completion.
742
  * @note   There is no need of a timeout parameter as the max count error is already
743
  *         managed by the TSC peripheral.
744
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
745
  *         the configuration information for the specified TSC.
746
  * @retval HAL state
747
  */
748
HAL_StatusTypeDef HAL_TSC_PollForAcquisition(TSC_HandleTypeDef *htsc)
749
{
750
  /* Check the parameters */
751
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
752
 
753
  /* Process locked */
754
  __HAL_LOCK(htsc);
755
 
756
  /* Check end of acquisition */
757
  while (HAL_TSC_GetState(htsc) == HAL_TSC_STATE_BUSY)
758
  {
759
    /* The timeout (max count error) is managed by the TSC peripheral itself. */
760
  }
761
 
762
  /* Process unlocked */
763
  __HAL_UNLOCK(htsc);
764
 
765
  return HAL_OK;
766
}
767
 
768
/**
769
  * @brief  Get the acquisition status for a group.
770
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
771
  *         the configuration information for the specified TSC.
772
  * @param  gx_index Index of the group
773
  * @retval Group status
774
  */
775
TSC_GroupStatusTypeDef HAL_TSC_GroupGetStatus(TSC_HandleTypeDef *htsc, uint32_t gx_index)
776
{
777
  /* Check the parameters */
778
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
779
  assert_param(IS_TSC_GROUP_INDEX(gx_index));
780
 
781
  /* Return the group status */
782
  return (__HAL_TSC_GET_GROUP_STATUS(htsc, gx_index));
783
}
784
 
785
/**
786
  * @brief  Get the acquisition measure for a group.
787
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
788
  *         the configuration information for the specified TSC.
789
  * @param  gx_index Index of the group
790
  * @retval Acquisition measure
791
  */
792
uint32_t HAL_TSC_GroupGetValue(TSC_HandleTypeDef *htsc, uint32_t gx_index)
793
{
794
  /* Check the parameters */
795
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
796
  assert_param(IS_TSC_GROUP_INDEX(gx_index));
797
 
798
  /* Return the group acquisition counter */
799
  return htsc->Instance->IOGXCR[gx_index];
800
}
801
 
802
/**
803
  * @}
804
  */
805
 
806
/** @defgroup TSC_Exported_Functions_Group3 Peripheral Control functions
807
 *  @brief    Peripheral Control functions
808
 *
809
@verbatim
810
 ===============================================================================
811
             ##### Peripheral Control functions #####
812
 ===============================================================================
813
    [..]  This section provides functions allowing to:
814
      (+) Configure TSC IOs
815
      (+) Discharge TSC IOs
816
@endverbatim
817
  * @{
818
  */
819
 
820
/**
821
  * @brief  Configure TSC IOs.
822
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
823
  *         the configuration information for the specified TSC.
824
  * @param  config Pointer to the configuration structure.
825
  * @retval HAL status
826
  */
827
HAL_StatusTypeDef HAL_TSC_IOConfig(TSC_HandleTypeDef *htsc, TSC_IOConfigTypeDef *config)
828
{
829
  /* Check the parameters */
830
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
831
  assert_param(IS_TSC_GROUP(config->ChannelIOs));
832
  assert_param(IS_TSC_GROUP(config->ShieldIOs));
833
  assert_param(IS_TSC_GROUP(config->SamplingIOs));
834
 
835
  /* Process locked */
836
  __HAL_LOCK(htsc);
837
 
838
  /* Stop acquisition */
839
  __HAL_TSC_STOP_ACQ(htsc);
840
 
841
  /* Disable Schmitt trigger hysteresis on all used TSC IOs */
842
  htsc->Instance->IOHCR = (~(config->ChannelIOs | config->ShieldIOs | config->SamplingIOs));
843
 
844
  /* Set channel and shield IOs */
845
  htsc->Instance->IOCCR = (config->ChannelIOs | config->ShieldIOs);
846
 
847
  /* Set sampling IOs */
848
  htsc->Instance->IOSCR = config->SamplingIOs;
849
 
850
  /* Set groups to be acquired */
851
  htsc->Instance->IOGCSR = TSC_extract_groups(config->ChannelIOs);
852
 
853
  /* Process unlocked */
854
  __HAL_UNLOCK(htsc);
855
 
856
  /* Return function status */
857
  return HAL_OK;
858
}
859
 
860
/**
861
  * @brief  Discharge TSC IOs.
862
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
863
  *         the configuration information for the specified TSC.
864
  * @param  choice This parameter can be set to ENABLE or DISABLE.
865
  * @retval HAL status
866
  */
867
HAL_StatusTypeDef HAL_TSC_IODischarge(TSC_HandleTypeDef *htsc, FunctionalState choice)
868
{
869
  /* Check the parameters */
870
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
871
 
872
  /* Process locked */
873
  __HAL_LOCK(htsc);
874
 
875
  if (choice == ENABLE)
876
  {
877
    __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
878
  }
879
  else
880
  {
881
    __HAL_TSC_SET_IODEF_INFLOAT(htsc);
882
  }
883
 
884
  /* Process unlocked */
885
  __HAL_UNLOCK(htsc);
886
 
887
  /* Return the group acquisition counter */
888
  return HAL_OK;
889
}
890
 
891
/**
892
  * @}
893
  */
894
 
895
/** @defgroup TSC_Exported_Functions_Group4 Peripheral State and Errors functions
896
 *  @brief   Peripheral State and Errors functions
897
 *
898
@verbatim
899
 ===============================================================================
900
            ##### State and Errors functions #####
901
 ===============================================================================
902
    [..]
903
    This subsection provides functions allowing to
904
      (+) Get TSC state.
905
 
906
@endverbatim
907
  * @{
908
  */
909
 
910
/**
911
  * @brief  Return the TSC handle state.
912
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
913
  *         the configuration information for the specified TSC.
914
  * @retval HAL state
915
  */
916
HAL_TSC_StateTypeDef HAL_TSC_GetState(TSC_HandleTypeDef *htsc)
917
{
918
  /* Check the parameters */
919
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
920
 
921
  if (htsc->State == HAL_TSC_STATE_BUSY)
922
  {
923
    /* Check end of acquisition flag */
924
    if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
925
    {
926
      /* Check max count error flag */
927
      if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
928
      {
929
        /* Change TSC state */
930
        htsc->State = HAL_TSC_STATE_ERROR;
931
      }
932
      else
933
      {
934
        /* Change TSC state */
935
        htsc->State = HAL_TSC_STATE_READY;
936
      }
937
    }
938
  }
939
 
940
  /* Return TSC state */
941
  return htsc->State;
942
}
943
 
944
/**
945
  * @}
946
  */
947
 
948
/** @defgroup TSC_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
949
 * @{
950
 */
951
 
952
/**
953
  * @brief  Handle TSC interrupt request.
954
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
955
  *         the configuration information for the specified TSC.
956
  * @retval None
957
  */
958
void HAL_TSC_IRQHandler(TSC_HandleTypeDef *htsc)
959
{
960
  /* Check the parameters */
961
  assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
962
 
963
  /* Check if the end of acquisition occurred */
964
  if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
965
  {
966
    /* Clear EOA flag */
967
    __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA);
968
  }
969
 
970
  /* Check if max count error occurred */
971
  if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
972
  {
973
    /* Clear MCE flag */
974
    __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE);
975
    /* Change TSC state */
976
    htsc->State = HAL_TSC_STATE_ERROR;
977
#if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
978
    htsc->ErrorCallback(htsc);
979
#else
980
    /* Conversion completed callback */
981
    HAL_TSC_ErrorCallback(htsc);
982
#endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
983
  }
984
  else
985
  {
986
    /* Change TSC state */
987
    htsc->State = HAL_TSC_STATE_READY;
988
#if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
989
    htsc->ConvCpltCallback(htsc);
990
#else
991
    /* Conversion completed callback */
992
    HAL_TSC_ConvCpltCallback(htsc);
993
#endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
994
  }
995
}
996
 
997
/**
998
  * @brief  Acquisition completed callback in non-blocking mode.
999
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
1000
  *         the configuration information for the specified TSC.
1001
  * @retval None
1002
  */
1003
__weak void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef *htsc)
1004
{
1005
  /* Prevent unused argument(s) compilation warning */
1006
  UNUSED(htsc);
1007
 
1008
  /* NOTE : This function should not be modified, when the callback is needed,
1009
            the HAL_TSC_ConvCpltCallback could be implemented in the user file.
1010
   */
1011
}
1012
 
1013
/**
1014
  * @brief  Error callback in non-blocking mode.
1015
  * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
1016
  *         the configuration information for the specified TSC.
1017
  * @retval None
1018
  */
1019
__weak void HAL_TSC_ErrorCallback(TSC_HandleTypeDef *htsc)
1020
{
1021
  /* Prevent unused argument(s) compilation warning */
1022
  UNUSED(htsc);
1023
 
1024
  /* NOTE : This function should not be modified, when the callback is needed,
1025
            the HAL_TSC_ErrorCallback could be implemented in the user file.
1026
   */
1027
}
1028
 
1029
/**
1030
  * @}
1031
  */
1032
 
1033
/**
1034
  * @}
1035
  */
1036
 
1037
/* Private functions ---------------------------------------------------------*/
1038
/** @defgroup TSC_Private_Functions TSC Private Functions
1039
  * @{
1040
  */
1041
 
1042
/**
1043
  * @brief  Utility function used to set the acquired groups mask.
1044
  * @param  iomask Channels IOs mask
1045
  * @retval Acquired groups mask
1046
  */
1047
static uint32_t TSC_extract_groups(uint32_t iomask)
1048
{
1049
  uint32_t groups = 0UL;
1050
  uint32_t idx;
1051
 
1052
  for (idx = 0UL; idx < (uint32_t)TSC_NB_OF_GROUPS; idx++)
1053
  {
1054
    if ((iomask & (0x0FUL << (idx * 4UL))) != 0UL )
1055
    {
1056
      groups |= (1UL << idx);
1057
    }
1058
  }
1059
 
1060
  return groups;
1061
}
1062
 
1063
/**
1064
  * @}
1065
  */
1066
 
1067
#endif /* HAL_TSC_MODULE_ENABLED */
1068
 
1069
/**
1070
  * @}
1071
  */
1072
 
1073
/**
1074
  * @}
1075
  */
1076
 
1077
#endif /* TSC */
1078
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/