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