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