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