1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_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     Table 1. IOs for the STM32F3xx devices
148     +--------------------------------+
149     |       IOs    |   TSC functions |
150     |--------------|-----------------|
151     |   PA0  (AF)  |   TSC_G1_IO1    |
152     |   PA1  (AF)  |   TSC_G1_IO2    |
153     |   PA2  (AF)  |   TSC_G1_IO3    |
154     |   PA3  (AF)  |   TSC_G1_IO4    |
155     |--------------|-----------------|
156     |   PA4 (AF)   |   TSC_G2_IO1    |
157     |   PA5 (AF)   |   TSC_G2_IO2    |
158     |   PA6 (AF)   |   TSC_G2_IO3    |
159     |   PA7 (AF)   |   TSC_G2_IO4    |
160     |--------------|-----------------|
161     |   PC5  (AF)  |   TSC_G3_IO1    |
162     |   PB0  (AF)  |   TSC_G3_IO2    |
163     |   PB1  (AF)  |   TSC_G3_IO3    |
164     |   PB2  (AF)  |   TSC_G3_IO4    |
165     |--------------|-----------------|
166     |   PA9  (AF)  |   TSC_G4_IO1    |
167     |   PA10 (AF)  |   TSC_G4_IO2    |
168     |   PA13 (AF)  |   TSC_G4_IO3    |
169     |   PA14 (AF)  |   TSC_G4_IO4    |
170     |--------------|-----------------|
171     |   PB3  (AF)  |   TSC_G5_IO1    |
172     |   PB4  (AF)  |   TSC_G5_IO2    |
173     |   PB6  (AF)  |   TSC_G5_IO3    |
174     |   PB7  (AF)  |   TSC_G5_IO4    |
175     |--------------|-----------------|
176     |   PB11 (AF)  |   TSC_G6_IO1    |
177     |   PB12 (AF)  |   TSC_G6_IO2    |
178     |   PB13 (AF)  |   TSC_G6_IO3    |
179     |   PB14 (AF)  |   TSC_G6_IO4    |
180     |--------------|-----------------|
181     |   PE2  (AF)  |   TSC_G7_IO1    |
182     |   PE3  (AF)  |   TSC_G7_IO2    |
183     |   PE4  (AF)  |   TSC_G7_IO3    |
184     |   PE5  (AF)  |   TSC_G7_IO4    |
185     |--------------|-----------------|
186     |   PD12 (AF)  |   TSC_G8_IO1    |
187     |   PD13 (AF)  |   TSC_G8_IO2    |
188     |   PD14 (AF)  |   TSC_G8_IO3    |
189     |   PD15 (AF)  |   TSC_G8_IO4    |
190     |--------------|-----------------|
191     |   PB8  (AF)  |   TSC_SYNC      |
192     |   PB10 (AF)  |                 |
193     +--------------------------------+
194 
195   */
196 
197 /* Includes ------------------------------------------------------------------*/
198 #include "stm32f3xx_hal.h"
199 
200 /** @addtogroup STM32F3xx_HAL_Driver
201   * @{
202   */
203 
204 /** @defgroup TSC TSC
205   * @brief HAL TSC module driver
206   * @{
207   */
208 
209 #ifdef HAL_TSC_MODULE_ENABLED
210 
211 /* Private typedef -----------------------------------------------------------*/
212 /* Private define ------------------------------------------------------------*/
213 /* Private macro -------------------------------------------------------------*/
214 /* Private variables ---------------------------------------------------------*/
215 /* Private function prototypes -----------------------------------------------*/
216 static uint32_t TSC_extract_groups(uint32_t iomask);
217 
218 /* Exported functions --------------------------------------------------------*/
219 
220 /** @defgroup TSC_Exported_Functions TSC Exported Functions
221   * @{
222   */
223 
224 /** @defgroup TSC_Exported_Functions_Group1 Initialization and de-initialization functions
225   *  @brief    Initialization and Configuration functions
226   *
227 @verbatim
228  ===============================================================================
229               ##### Initialization and de-initialization functions #####
230  ===============================================================================
231     [..]  This section provides functions allowing to:
232       (+) Initialize and configure the TSC.
233       (+) De-initialize the TSC.
234 @endverbatim
235   * @{
236   */
237 
238 /**
239   * @brief  Initialize the TSC peripheral according to the specified parameters
240   *         in the TSC_InitTypeDef structure and initialize the associated handle.
241   * @param  htsc TSC handle
242   * @retval HAL status
243   */
HAL_TSC_Init(TSC_HandleTypeDef * htsc)244 HAL_StatusTypeDef HAL_TSC_Init(TSC_HandleTypeDef *htsc)
245 {
246   /* Check TSC handle allocation */
247   if (htsc == NULL)
248   {
249     return HAL_ERROR;
250   }
251 
252   /* Check the parameters */
253   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
254   assert_param(IS_TSC_CTPH(htsc->Init.CTPulseHighLength));
255   assert_param(IS_TSC_CTPL(htsc->Init.CTPulseLowLength));
256   assert_param(IS_TSC_SS(htsc->Init.SpreadSpectrum));
257   assert_param(IS_TSC_SSD(htsc->Init.SpreadSpectrumDeviation));
258   assert_param(IS_TSC_SS_PRESC(htsc->Init.SpreadSpectrumPrescaler));
259   assert_param(IS_TSC_PG_PRESC(htsc->Init.PulseGeneratorPrescaler));
260   assert_param(IS_TSC_PG_PRESC_VS_CTPL(htsc->Init.PulseGeneratorPrescaler, htsc->Init.CTPulseLowLength));
261   assert_param(IS_TSC_MCV(htsc->Init.MaxCountValue));
262   assert_param(IS_TSC_IODEF(htsc->Init.IODefaultMode));
263   assert_param(IS_TSC_SYNC_POL(htsc->Init.SynchroPinPolarity));
264   assert_param(IS_TSC_ACQ_MODE(htsc->Init.AcquisitionMode));
265   assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
266   assert_param(IS_TSC_GROUP(htsc->Init.ChannelIOs));
267   assert_param(IS_TSC_GROUP(htsc->Init.ShieldIOs));
268   assert_param(IS_TSC_GROUP(htsc->Init.SamplingIOs));
269 
270   if (htsc->State == HAL_TSC_STATE_RESET)
271   {
272     /* Allocate lock resource and initialize it */
273     htsc->Lock = HAL_UNLOCKED;
274 
275 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
276     /* Init the TSC Callback settings */
277     htsc->ConvCpltCallback  = HAL_TSC_ConvCpltCallback; /* Legacy weak ConvCpltCallback     */
278     htsc->ErrorCallback     = HAL_TSC_ErrorCallback;    /* Legacy weak ErrorCallback        */
279 
280     if (htsc->MspInitCallback == NULL)
281     {
282       htsc->MspInitCallback = HAL_TSC_MspInit; /* Legacy weak MspInit  */
283     }
284 
285     /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
286     htsc->MspInitCallback(htsc);
287 #else
288     /* Init the low level hardware : GPIO, CLOCK, CORTEX */
289     HAL_TSC_MspInit(htsc);
290 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
291   }
292 
293   /* Initialize the TSC state */
294   htsc->State = HAL_TSC_STATE_BUSY;
295 
296   /*--------------------------------------------------------------------------*/
297   /* Set TSC parameters */
298 
299   /* Enable TSC */
300   htsc->Instance->CR = TSC_CR_TSCE;
301 
302   /* Set all functions */
303   htsc->Instance->CR |= (htsc->Init.CTPulseHighLength |
304                          htsc->Init.CTPulseLowLength |
305                          (htsc->Init.SpreadSpectrumDeviation << TSC_CR_SSD_Pos) |
306                          htsc->Init.SpreadSpectrumPrescaler |
307                          htsc->Init.PulseGeneratorPrescaler |
308                          htsc->Init.MaxCountValue |
309                          htsc->Init.SynchroPinPolarity |
310                          htsc->Init.AcquisitionMode);
311 
312   /* Spread spectrum */
313   if (htsc->Init.SpreadSpectrum == ENABLE)
314   {
315     htsc->Instance->CR |= TSC_CR_SSE;
316   }
317 
318   /* Disable Schmitt trigger hysteresis on all used TSC IOs */
319   htsc->Instance->IOHCR = (~(htsc->Init.ChannelIOs | htsc->Init.ShieldIOs | htsc->Init.SamplingIOs));
320 
321   /* Set channel and shield IOs */
322   htsc->Instance->IOCCR = (htsc->Init.ChannelIOs | htsc->Init.ShieldIOs);
323 
324   /* Set sampling IOs */
325   htsc->Instance->IOSCR = htsc->Init.SamplingIOs;
326 
327   /* Set the groups to be acquired */
328   htsc->Instance->IOGCSR = TSC_extract_groups(htsc->Init.ChannelIOs);
329 
330   /* Disable interrupts */
331   htsc->Instance->IER &= (~(TSC_IT_EOA | TSC_IT_MCE));
332 
333   /* Clear flags */
334   htsc->Instance->ICR = (TSC_FLAG_EOA | TSC_FLAG_MCE);
335 
336   /*--------------------------------------------------------------------------*/
337 
338   /* Initialize the TSC state */
339   htsc->State = HAL_TSC_STATE_READY;
340 
341   /* Return function status */
342   return HAL_OK;
343 }
344 
345 /**
346   * @brief  Deinitialize the TSC peripheral registers to their default reset values.
347   * @param  htsc TSC handle
348   * @retval HAL status
349   */
HAL_TSC_DeInit(TSC_HandleTypeDef * htsc)350 HAL_StatusTypeDef HAL_TSC_DeInit(TSC_HandleTypeDef *htsc)
351 {
352   /* Check TSC handle allocation */
353   if (htsc == NULL)
354   {
355     return HAL_ERROR;
356   }
357 
358   /* Check the parameters */
359   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
360 
361   /* Change TSC state */
362   htsc->State = HAL_TSC_STATE_BUSY;
363 
364 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
365   if (htsc->MspDeInitCallback == NULL)
366   {
367     htsc->MspDeInitCallback = HAL_TSC_MspDeInit; /* Legacy weak MspDeInit  */
368   }
369 
370   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
371   htsc->MspDeInitCallback(htsc);
372 #else
373   /* DeInit the low level hardware */
374   HAL_TSC_MspDeInit(htsc);
375 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
376 
377   /* Change TSC state */
378   htsc->State = HAL_TSC_STATE_RESET;
379 
380   /* Process unlocked */
381   __HAL_UNLOCK(htsc);
382 
383   /* Return function status */
384   return HAL_OK;
385 }
386 
387 /**
388   * @brief  Initialize the TSC MSP.
389   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
390   *         the configuration information for the specified TSC.
391   * @retval None
392   */
HAL_TSC_MspInit(TSC_HandleTypeDef * htsc)393 __weak void HAL_TSC_MspInit(TSC_HandleTypeDef *htsc)
394 {
395   /* Prevent unused argument(s) compilation warning */
396   UNUSED(htsc);
397 
398   /* NOTE : This function should not be modified, when the callback is needed,
399             the HAL_TSC_MspInit could be implemented in the user file.
400    */
401 }
402 
403 /**
404   * @brief  DeInitialize the TSC MSP.
405   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
406   *         the configuration information for the specified TSC.
407   * @retval None
408   */
HAL_TSC_MspDeInit(TSC_HandleTypeDef * htsc)409 __weak void HAL_TSC_MspDeInit(TSC_HandleTypeDef *htsc)
410 {
411   /* Prevent unused argument(s) compilation warning */
412   UNUSED(htsc);
413 
414   /* NOTE : This function should not be modified, when the callback is needed,
415             the HAL_TSC_MspDeInit could be implemented in the user file.
416    */
417 }
418 
419 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
420 /**
421   * @brief  Register a User TSC Callback
422   *         To be used instead of the weak predefined callback
423   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
424   *                the configuration information for the specified TSC.
425   * @param  CallbackID ID of the callback to be registered
426   *         This parameter can be one of the following values:
427   *          @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
428   *          @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
429   *          @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
430   *          @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
431   * @param  pCallback pointer to the Callback function
432   * @retval HAL status
433   */
HAL_TSC_RegisterCallback(TSC_HandleTypeDef * htsc,HAL_TSC_CallbackIDTypeDef CallbackID,pTSC_CallbackTypeDef pCallback)434 HAL_StatusTypeDef HAL_TSC_RegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID,
435                                            pTSC_CallbackTypeDef pCallback)
436 {
437   HAL_StatusTypeDef status = HAL_OK;
438 
439   if (pCallback == NULL)
440   {
441     /* Update the error code */
442     htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
443 
444     return HAL_ERROR;
445   }
446   /* Process locked */
447   __HAL_LOCK(htsc);
448 
449   if (HAL_TSC_STATE_READY == htsc->State)
450   {
451     switch (CallbackID)
452     {
453       case HAL_TSC_CONV_COMPLETE_CB_ID :
454         htsc->ConvCpltCallback = pCallback;
455         break;
456 
457       case HAL_TSC_ERROR_CB_ID :
458         htsc->ErrorCallback = pCallback;
459         break;
460 
461       case HAL_TSC_MSPINIT_CB_ID :
462         htsc->MspInitCallback = pCallback;
463         break;
464 
465       case HAL_TSC_MSPDEINIT_CB_ID :
466         htsc->MspDeInitCallback = pCallback;
467         break;
468 
469       default :
470         /* Update the error code */
471         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
472 
473         /* Return error status */
474         status =  HAL_ERROR;
475         break;
476     }
477   }
478   else if (HAL_TSC_STATE_RESET == htsc->State)
479   {
480     switch (CallbackID)
481     {
482       case HAL_TSC_MSPINIT_CB_ID :
483         htsc->MspInitCallback = pCallback;
484         break;
485 
486       case HAL_TSC_MSPDEINIT_CB_ID :
487         htsc->MspDeInitCallback = pCallback;
488         break;
489 
490       default :
491         /* Update the error code */
492         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
493 
494         /* Return error status */
495         status =  HAL_ERROR;
496         break;
497     }
498   }
499   else
500   {
501     /* Update the error code */
502     htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
503 
504     /* Return error status */
505     status =  HAL_ERROR;
506   }
507 
508   /* Release Lock */
509   __HAL_UNLOCK(htsc);
510   return status;
511 }
512 
513 /**
514   * @brief  Unregister an TSC Callback
515   *         TSC callback is redirected to the weak predefined callback
516   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
517   *                the configuration information for the specified TSC.
518   * @param  CallbackID ID of the callback to be unregistered
519   *         This parameter can be one of the following values:
520   *         This parameter can be one of the following values:
521   *          @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
522   *          @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
523   *          @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
524   *          @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
525   * @retval HAL status
526   */
HAL_TSC_UnRegisterCallback(TSC_HandleTypeDef * htsc,HAL_TSC_CallbackIDTypeDef CallbackID)527 HAL_StatusTypeDef HAL_TSC_UnRegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID)
528 {
529   HAL_StatusTypeDef status = HAL_OK;
530 
531   /* Process locked */
532   __HAL_LOCK(htsc);
533 
534   if (HAL_TSC_STATE_READY == htsc->State)
535   {
536     switch (CallbackID)
537     {
538       case HAL_TSC_CONV_COMPLETE_CB_ID :
539         htsc->ConvCpltCallback = HAL_TSC_ConvCpltCallback;       /* Legacy weak ConvCpltCallback      */
540         break;
541 
542       case HAL_TSC_ERROR_CB_ID :
543         htsc->ErrorCallback = HAL_TSC_ErrorCallback;               /* Legacy weak ErrorCallback        */
544         break;
545 
546       case HAL_TSC_MSPINIT_CB_ID :
547         htsc->MspInitCallback = HAL_TSC_MspInit;                   /* Legacy weak MspInit              */
548         break;
549 
550       case HAL_TSC_MSPDEINIT_CB_ID :
551         htsc->MspDeInitCallback = HAL_TSC_MspDeInit;               /* Legacy weak MspDeInit            */
552         break;
553 
554       default :
555         /* Update the error code */
556         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
557 
558         /* Return error status */
559         status =  HAL_ERROR;
560         break;
561     }
562   }
563   else if (HAL_TSC_STATE_RESET == htsc->State)
564   {
565     switch (CallbackID)
566     {
567       case HAL_TSC_MSPINIT_CB_ID :
568         htsc->MspInitCallback = HAL_TSC_MspInit;                   /* Legacy weak MspInit              */
569         break;
570 
571       case HAL_TSC_MSPDEINIT_CB_ID :
572         htsc->MspDeInitCallback = HAL_TSC_MspDeInit;               /* Legacy weak MspDeInit            */
573         break;
574 
575       default :
576         /* Update the error code */
577         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
578 
579         /* Return error status */
580         status =  HAL_ERROR;
581         break;
582     }
583   }
584   else
585   {
586     /* Update the error code */
587     htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
588 
589     /* Return error status */
590     status =  HAL_ERROR;
591   }
592 
593   /* Release Lock */
594   __HAL_UNLOCK(htsc);
595   return status;
596 }
597 
598 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
599 
600 /**
601   * @}
602   */
603 
604 /** @defgroup TSC_Exported_Functions_Group2 Input and Output operation functions
605   *  @brief    Input and Output operation functions
606   *
607 @verbatim
608  ===============================================================================
609              ##### IO Operation functions #####
610  ===============================================================================
611     [..]  This section provides functions allowing to:
612       (+) Start acquisition in polling mode.
613       (+) Start acquisition in interrupt mode.
614       (+) Stop conversion in polling mode.
615       (+) Stop conversion in interrupt mode.
616       (+) Poll for acquisition completed.
617       (+) Get group acquisition status.
618       (+) Get group acquisition value.
619 @endverbatim
620   * @{
621   */
622 
623 /**
624   * @brief  Start the acquisition.
625   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
626   *         the configuration information for the specified TSC.
627   * @retval HAL status
628   */
HAL_TSC_Start(TSC_HandleTypeDef * htsc)629 HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef *htsc)
630 {
631   /* Check the parameters */
632   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
633 
634   /* Process locked */
635   __HAL_LOCK(htsc);
636 
637   /* Change TSC state */
638   htsc->State = HAL_TSC_STATE_BUSY;
639 
640   /* Clear interrupts */
641   __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
642 
643   /* Clear flags */
644   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
645 
646   /* Set touch sensing IOs not acquired to the specified IODefaultMode */
647   if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
648   {
649     __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
650   }
651   else
652   {
653     __HAL_TSC_SET_IODEF_INFLOAT(htsc);
654   }
655 
656   /* Launch the acquisition */
657   __HAL_TSC_START_ACQ(htsc);
658 
659   /* Process unlocked */
660   __HAL_UNLOCK(htsc);
661 
662   /* Return function status */
663   return HAL_OK;
664 }
665 
666 /**
667   * @brief  Start the acquisition in interrupt mode.
668   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
669   *         the configuration information for the specified TSC.
670   * @retval HAL status.
671   */
HAL_TSC_Start_IT(TSC_HandleTypeDef * htsc)672 HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef *htsc)
673 {
674   /* Check the parameters */
675   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
676   assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
677 
678   /* Process locked */
679   __HAL_LOCK(htsc);
680 
681   /* Change TSC state */
682   htsc->State = HAL_TSC_STATE_BUSY;
683 
684   /* Enable end of acquisition interrupt */
685   __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA);
686 
687   /* Enable max count error interrupt (optional) */
688   if (htsc->Init.MaxCountInterrupt == ENABLE)
689   {
690     __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE);
691   }
692   else
693   {
694     __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE);
695   }
696 
697   /* Clear flags */
698   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
699 
700   /* Set touch sensing IOs not acquired to the specified IODefaultMode */
701   if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
702   {
703     __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
704   }
705   else
706   {
707     __HAL_TSC_SET_IODEF_INFLOAT(htsc);
708   }
709 
710   /* Launch the acquisition */
711   __HAL_TSC_START_ACQ(htsc);
712 
713   /* Process unlocked */
714   __HAL_UNLOCK(htsc);
715 
716   /* Return function status */
717   return HAL_OK;
718 }
719 
720 /**
721   * @brief  Stop the acquisition previously launched in polling mode.
722   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
723   *         the configuration information for the specified TSC.
724   * @retval HAL status
725   */
HAL_TSC_Stop(TSC_HandleTypeDef * htsc)726 HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef *htsc)
727 {
728   /* Check the parameters */
729   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
730 
731   /* Process locked */
732   __HAL_LOCK(htsc);
733 
734   /* Stop the acquisition */
735   __HAL_TSC_STOP_ACQ(htsc);
736 
737   /* Set touch sensing IOs in low power mode (output push-pull) */
738   __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
739 
740   /* Clear flags */
741   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
742 
743   /* Change TSC state */
744   htsc->State = HAL_TSC_STATE_READY;
745 
746   /* Process unlocked */
747   __HAL_UNLOCK(htsc);
748 
749   /* Return function status */
750   return HAL_OK;
751 }
752 
753 /**
754   * @brief  Stop the acquisition previously launched in interrupt mode.
755   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
756   *         the configuration information for the specified TSC.
757   * @retval HAL status
758   */
HAL_TSC_Stop_IT(TSC_HandleTypeDef * htsc)759 HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef *htsc)
760 {
761   /* Check the parameters */
762   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
763 
764   /* Process locked */
765   __HAL_LOCK(htsc);
766 
767   /* Stop the acquisition */
768   __HAL_TSC_STOP_ACQ(htsc);
769 
770   /* Set touch sensing IOs in low power mode (output push-pull) */
771   __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
772 
773   /* Disable interrupts */
774   __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
775 
776   /* Clear flags */
777   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
778 
779   /* Change TSC state */
780   htsc->State = HAL_TSC_STATE_READY;
781 
782   /* Process unlocked */
783   __HAL_UNLOCK(htsc);
784 
785   /* Return function status */
786   return HAL_OK;
787 }
788 
789 /**
790   * @brief  Start acquisition and wait until completion.
791   * @note   There is no need of a timeout parameter as the max count error is already
792   *         managed by the TSC peripheral.
793   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
794   *         the configuration information for the specified TSC.
795   * @retval HAL state
796   */
HAL_TSC_PollForAcquisition(TSC_HandleTypeDef * htsc)797 HAL_StatusTypeDef HAL_TSC_PollForAcquisition(TSC_HandleTypeDef *htsc)
798 {
799   /* Check the parameters */
800   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
801 
802   /* Process locked */
803   __HAL_LOCK(htsc);
804 
805   /* Check end of acquisition */
806   while (HAL_TSC_GetState(htsc) == HAL_TSC_STATE_BUSY)
807   {
808     /* The timeout (max count error) is managed by the TSC peripheral itself. */
809   }
810 
811   /* Process unlocked */
812   __HAL_UNLOCK(htsc);
813 
814   return HAL_OK;
815 }
816 
817 /**
818   * @brief  Get the acquisition status for a group.
819   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
820   *         the configuration information for the specified TSC.
821   * @param  gx_index Index of the group
822   * @retval Group status
823   */
HAL_TSC_GroupGetStatus(const TSC_HandleTypeDef * htsc,uint32_t gx_index)824 TSC_GroupStatusTypeDef HAL_TSC_GroupGetStatus(const TSC_HandleTypeDef *htsc, uint32_t gx_index)
825 {
826   /* Check the parameters */
827   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
828   assert_param(IS_TSC_GROUP_INDEX(gx_index));
829 
830   /* Return the group status */
831   return (__HAL_TSC_GET_GROUP_STATUS(htsc, gx_index));
832 }
833 
834 /**
835   * @brief  Get the acquisition measure for a group.
836   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
837   *         the configuration information for the specified TSC.
838   * @param  gx_index Index of the group
839   * @retval Acquisition measure
840   */
HAL_TSC_GroupGetValue(const TSC_HandleTypeDef * htsc,uint32_t gx_index)841 uint32_t HAL_TSC_GroupGetValue(const TSC_HandleTypeDef *htsc, uint32_t gx_index)
842 {
843   /* Check the parameters */
844   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
845   assert_param(IS_TSC_GROUP_INDEX(gx_index));
846 
847   /* Return the group acquisition counter */
848   return htsc->Instance->IOGXCR[gx_index];
849 }
850 
851 /**
852   * @}
853   */
854 
855 /** @defgroup TSC_Exported_Functions_Group3 Peripheral Control functions
856   *  @brief    Peripheral Control functions
857   *
858 @verbatim
859  ===============================================================================
860              ##### Peripheral Control functions #####
861  ===============================================================================
862     [..]  This section provides functions allowing to:
863       (+) Configure TSC IOs
864       (+) Discharge TSC IOs
865 @endverbatim
866   * @{
867   */
868 
869 /**
870   * @brief  Configure TSC IOs.
871   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
872   *         the configuration information for the specified TSC.
873   * @param  config Pointer to the configuration structure.
874   * @retval HAL status
875   */
HAL_TSC_IOConfig(TSC_HandleTypeDef * htsc,const TSC_IOConfigTypeDef * config)876 HAL_StatusTypeDef HAL_TSC_IOConfig(TSC_HandleTypeDef *htsc, const TSC_IOConfigTypeDef *config)
877 {
878   /* Check the parameters */
879   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
880   assert_param(IS_TSC_GROUP(config->ChannelIOs));
881   assert_param(IS_TSC_GROUP(config->ShieldIOs));
882   assert_param(IS_TSC_GROUP(config->SamplingIOs));
883 
884   /* Process locked */
885   __HAL_LOCK(htsc);
886 
887   /* Stop acquisition */
888   __HAL_TSC_STOP_ACQ(htsc);
889 
890   /* Disable Schmitt trigger hysteresis on all used TSC IOs */
891   htsc->Instance->IOHCR = (~(config->ChannelIOs | config->ShieldIOs | config->SamplingIOs));
892 
893   /* Set channel and shield IOs */
894   htsc->Instance->IOCCR = (config->ChannelIOs | config->ShieldIOs);
895 
896   /* Set sampling IOs */
897   htsc->Instance->IOSCR = config->SamplingIOs;
898 
899   /* Set groups to be acquired */
900   htsc->Instance->IOGCSR = TSC_extract_groups(config->ChannelIOs);
901 
902   /* Process unlocked */
903   __HAL_UNLOCK(htsc);
904 
905   /* Return function status */
906   return HAL_OK;
907 }
908 
909 /**
910   * @brief  Discharge TSC IOs.
911   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
912   *         the configuration information for the specified TSC.
913   * @param  choice This parameter can be set to ENABLE or DISABLE.
914   * @retval HAL status
915   */
HAL_TSC_IODischarge(TSC_HandleTypeDef * htsc,FunctionalState choice)916 HAL_StatusTypeDef HAL_TSC_IODischarge(TSC_HandleTypeDef *htsc, FunctionalState choice)
917 {
918   /* Check the parameters */
919   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
920 
921   /* Process locked */
922   __HAL_LOCK(htsc);
923 
924   if (choice == ENABLE)
925   {
926     __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
927   }
928   else
929   {
930     __HAL_TSC_SET_IODEF_INFLOAT(htsc);
931   }
932 
933   /* Process unlocked */
934   __HAL_UNLOCK(htsc);
935 
936   /* Return the group acquisition counter */
937   return HAL_OK;
938 }
939 
940 /**
941   * @}
942   */
943 
944 /** @defgroup TSC_Exported_Functions_Group4 Peripheral State and Errors functions
945   *  @brief   Peripheral State and Errors functions
946   *
947 @verbatim
948  ===============================================================================
949             ##### State and Errors functions #####
950  ===============================================================================
951     [..]
952     This subsection provides functions allowing to
953       (+) Get TSC state.
954 
955 @endverbatim
956   * @{
957   */
958 
959 /**
960   * @brief  Return the TSC handle state.
961   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
962   *         the configuration information for the specified TSC.
963   * @retval HAL state
964   */
HAL_TSC_GetState(TSC_HandleTypeDef * htsc)965 HAL_TSC_StateTypeDef HAL_TSC_GetState(TSC_HandleTypeDef *htsc)
966 {
967   /* Check the parameters */
968   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
969 
970   if (htsc->State == HAL_TSC_STATE_BUSY)
971   {
972     /* Check end of acquisition flag */
973     if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
974     {
975       /* Check max count error flag */
976       if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
977       {
978         /* Change TSC state */
979         htsc->State = HAL_TSC_STATE_ERROR;
980       }
981       else
982       {
983         /* Change TSC state */
984         htsc->State = HAL_TSC_STATE_READY;
985       }
986     }
987   }
988 
989   /* Return TSC state */
990   return htsc->State;
991 }
992 
993 /**
994   * @}
995   */
996 
997 /** @defgroup TSC_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
998   * @{
999   */
1000 
1001 /**
1002   * @brief  Handle TSC interrupt request.
1003   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
1004   *         the configuration information for the specified TSC.
1005   * @retval None
1006   */
HAL_TSC_IRQHandler(TSC_HandleTypeDef * htsc)1007 void HAL_TSC_IRQHandler(TSC_HandleTypeDef *htsc)
1008 {
1009   /* Check the parameters */
1010   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
1011 
1012   /* Check if the end of acquisition occurred */
1013   if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
1014   {
1015     /* Clear EOA flag */
1016     __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA);
1017   }
1018 
1019   /* Check if max count error occurred */
1020   if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
1021   {
1022     /* Clear MCE flag */
1023     __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE);
1024     /* Change TSC state */
1025     htsc->State = HAL_TSC_STATE_ERROR;
1026 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
1027     htsc->ErrorCallback(htsc);
1028 #else
1029     /* Conversion completed callback */
1030     HAL_TSC_ErrorCallback(htsc);
1031 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
1032   }
1033   else
1034   {
1035     /* Change TSC state */
1036     htsc->State = HAL_TSC_STATE_READY;
1037 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
1038     htsc->ConvCpltCallback(htsc);
1039 #else
1040     /* Conversion completed callback */
1041     HAL_TSC_ConvCpltCallback(htsc);
1042 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
1043   }
1044 }
1045 
1046 /**
1047   * @brief  Acquisition completed callback in non-blocking mode.
1048   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
1049   *         the configuration information for the specified TSC.
1050   * @retval None
1051   */
HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef * htsc)1052 __weak void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef *htsc)
1053 {
1054   /* Prevent unused argument(s) compilation warning */
1055   UNUSED(htsc);
1056 
1057   /* NOTE : This function should not be modified, when the callback is needed,
1058             the HAL_TSC_ConvCpltCallback could be implemented in the user file.
1059    */
1060 }
1061 
1062 /**
1063   * @brief  Error callback in non-blocking mode.
1064   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
1065   *         the configuration information for the specified TSC.
1066   * @retval None
1067   */
HAL_TSC_ErrorCallback(TSC_HandleTypeDef * htsc)1068 __weak void HAL_TSC_ErrorCallback(TSC_HandleTypeDef *htsc)
1069 {
1070   /* Prevent unused argument(s) compilation warning */
1071   UNUSED(htsc);
1072 
1073   /* NOTE : This function should not be modified, when the callback is needed,
1074             the HAL_TSC_ErrorCallback could be implemented in the user file.
1075    */
1076 }
1077 
1078 /**
1079   * @}
1080   */
1081 
1082 /**
1083   * @}
1084   */
1085 
1086 /* Private functions ---------------------------------------------------------*/
1087 /** @defgroup TSC_Private_Functions TSC Private Functions
1088   * @{
1089   */
1090 
1091 /**
1092   * @brief  Utility function used to set the acquired groups mask.
1093   * @param  iomask Channels IOs mask
1094   * @retval Acquired groups mask
1095   */
TSC_extract_groups(uint32_t iomask)1096 static uint32_t TSC_extract_groups(uint32_t iomask)
1097 {
1098   uint32_t groups = 0UL;
1099   uint32_t idx;
1100 
1101   for (idx = 0UL; idx < (uint32_t)TSC_NB_OF_GROUPS; idx++)
1102   {
1103     if ((iomask & (0x0FUL << (idx * 4UL))) != 0UL)
1104     {
1105       groups |= (1UL << idx);
1106     }
1107   }
1108 
1109   return groups;
1110 }
1111 
1112 /**
1113   * @}
1114   */
1115 
1116 #endif /* HAL_TSC_MODULE_ENABLED */
1117 
1118 /**
1119   * @}
1120   */
1121 
1122 /**
1123   * @}
1124   */
1125 
1126