xref: /btstack/port/stm32-l451-miromico-sx1280/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tsc.c (revision 2fd737d36a1de5d778cacc671d4b4d8c4f3fed82)
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   @verbatim
14 ================================================================================
15                        ##### TSC specific features #####
16 ================================================================================
17   [..]
18   (#) Proven and robust surface charge transfer acquisition principle
19 
20   (#) Supports up to 3 capacitive sensing channels per group
21 
22   (#) Capacitive sensing channels can be acquired in parallel offering a very good
23       response time
24 
25   (#) Spread spectrum feature to improve system robustness in noisy environments
26 
27   (#) Full hardware management of the charge transfer acquisition sequence
28 
29   (#) Programmable charge transfer frequency
30 
31   (#) Programmable sampling capacitor I/O pin
32 
33   (#) Programmable channel I/O pin
34 
35   (#) Programmable max count value to avoid long acquisition when a channel is faulty
36 
37   (#) Dedicated end of acquisition and max count error flags with interrupt capability
38 
39   (#) One sampling capacitor for up to 3 capacitive sensing channels to reduce the system
40       components
41 
42   (#) Compatible with proximity, touchkey, linear and rotary touch sensor implementation
43 
44                           ##### How to use this driver #####
45 ================================================================================
46   [..]
47     (#) Enable the TSC interface clock using __HAL_RCC_TSC_CLK_ENABLE() macro.
48 
49     (#) GPIO pins configuration
50       (++) Enable the clock for the TSC GPIOs using __HAL_RCC_GPIOx_CLK_ENABLE() macro.
51       (++) Configure the TSC pins used as sampling IOs in alternate function output Open-Drain mode,
52            and TSC pins used as channel/shield IOs in alternate function output Push-Pull mode
53            using HAL_GPIO_Init() function.
54 
55     (#) Interrupts configuration
56       (++) Configure the NVIC (if the interrupt model is used) using HAL_NVIC_SetPriority()
57            and HAL_NVIC_EnableIRQ() and function.
58 
59     (#) TSC configuration
60       (++) Configure all TSC parameters and used TSC IOs using HAL_TSC_Init() function.
61 
62  [..]   TSC peripheral alternate functions are mapped on AF9.
63 
64   *** Acquisition sequence ***
65   ===================================
66   [..]
67     (+) Discharge all IOs using HAL_TSC_IODischarge() function.
68     (+) Wait a certain time allowing a good discharge of all capacitors. This delay depends
69         of the sampling capacitor and electrodes design.
70     (+) Select the channel IOs to be acquired using HAL_TSC_IOConfig() function.
71     (+) Launch the acquisition using either HAL_TSC_Start() or HAL_TSC_Start_IT() function.
72         If the synchronized mode is selected, the acquisition will start as soon as the signal
73         is received on the synchro pin.
74     (+) Wait the end of acquisition using either HAL_TSC_PollForAcquisition() or
75         HAL_TSC_GetState() function or using WFI instruction for example.
76     (+) Check the group acquisition status using HAL_TSC_GroupGetStatus() function.
77     (+) Read the acquisition value using HAL_TSC_GroupGetValue() function.
78 
79      *** Callback registration ***
80      =============================================
81 
82   [..]
83      The compilation flag USE_HAL_TSC_REGISTER_CALLBACKS when set to 1
84      allows the user to configure dynamically the driver callbacks.
85      Use Functions @ref HAL_TSC_RegisterCallback() to register an interrupt callback.
86 
87   [..]
88      Function @ref HAL_TSC_RegisterCallback() allows to register following callbacks:
89        (+) ConvCpltCallback   : callback for conversion complete process.
90        (+) ErrorCallback      : callback for error detection.
91        (+) MspInitCallback    : callback for Msp Init.
92        (+) MspDeInitCallback  : callback for Msp DeInit.
93   [..]
94      This function takes as parameters the HAL peripheral handle, the Callback ID
95      and a pointer to the user callback function.
96 
97   [..]
98      Use function @ref HAL_TSC_UnRegisterCallback to reset a callback to the default
99      weak function.
100      @ref HAL_TSC_UnRegisterCallback takes as parameters the HAL peripheral handle,
101      and the Callback ID.
102   [..]
103      This function allows to reset following callbacks:
104        (+) ConvCpltCallback   : callback for conversion complete process.
105        (+) ErrorCallback      : callback for error detection.
106        (+) MspInitCallback    : callback for Msp Init.
107        (+) MspDeInitCallback  : callback for Msp DeInit.
108 
109   [..]
110      By default, after the @ref HAL_TSC_Init() and when the state is @ref HAL_TSC_STATE_RESET
111      all callbacks are set to the corresponding weak functions:
112      examples @ref HAL_TSC_ConvCpltCallback(), @ref HAL_TSC_ErrorCallback().
113      Exception done for MspInit and MspDeInit functions that are
114      reset to the legacy weak functions in the @ref HAL_TSC_Init()/ @ref HAL_TSC_DeInit() only when
115      these callbacks are null (not registered beforehand).
116      If MspInit or MspDeInit are not null, the @ref HAL_TSC_Init()/ @ref HAL_TSC_DeInit()
117      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
118 
119   [..]
120      Callbacks can be registered/unregistered in @ref HAL_TSC_STATE_READY state only.
121      Exception done MspInit/MspDeInit functions that can be registered/unregistered
122      in @ref HAL_TSC_STATE_READY or @ref HAL_TSC_STATE_RESET state,
123      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
124      Then, the user first registers the MspInit/MspDeInit user callbacks
125      using @ref HAL_TSC_RegisterCallback() before calling @ref HAL_TSC_DeInit()
126      or @ref HAL_TSC_Init() function.
127 
128   [..]
129      When the compilation flag USE_HAL_TSC_REGISTER_CALLBACKS is set to 0 or
130      not defined, the callback registration feature is not available and all callbacks
131      are set to the corresponding weak functions.
132 
133   @endverbatim
134   ******************************************************************************
135 
136     Table 1. IOs for the STM32L4xx devices
137     +--------------------------------+
138     |       IOs    |   TSC functions |
139     |--------------|-----------------|
140     |   PB12 (AF)  |   TSC_G1_IO1    |
141     |   PB13 (AF)  |   TSC_G1_IO2    |
142     |   PB14 (AF)  |   TSC_G1_IO3    |
143     |   PB15 (AF)  |   TSC_G1_IO4    |
144     |--------------|-----------------|
145     |   PB4 (AF)   |   TSC_G2_IO1    |
146     |   PB5 (AF)   |   TSC_G2_IO2    |
147     |   PB6 (AF)   |   TSC_G2_IO3    |
148     |   PB7 (AF)   |   TSC_G2_IO4    |
149     |--------------|-----------------|
150     |   PA15 (AF)  |   TSC_G3_IO1    |
151     |   PC10 (AF)  |   TSC_G3_IO2    |
152     |   PC11 (AF)  |   TSC_G3_IO3    |
153     |   PC12 (AF)  |   TSC_G3_IO4    |
154     |--------------|-----------------|
155     |   PC6 (AF)   |   TSC_G4_IO1    |
156     |   PC7 (AF)   |   TSC_G4_IO2    |
157     |   PC8 (AF)   |   TSC_G4_IO3    |
158     |   PC9 (AF)   |   TSC_G4_IO4    |
159     |--------------|-----------------|
160     |   PE10 (AF)  |   TSC_G5_IO1    |
161     |   PE11 (AF)  |   TSC_G5_IO2    |
162     |   PE12 (AF)  |   TSC_G5_IO3    |
163     |   PE13 (AF)  |   TSC_G5_IO4    |
164     |--------------|-----------------|
165     |   PD10 (AF)  |   TSC_G6_IO1    |
166     |   PD11 (AF)  |   TSC_G6_IO2    |
167     |   PD12 (AF)  |   TSC_G6_IO3    |
168     |   PD13 (AF)  |   TSC_G6_IO4    |
169     |--------------|-----------------|
170     |   PE2 (AF)   |   TSC_G7_IO1    |
171     |   PE3 (AF)   |   TSC_G7_IO2    |
172     |   PE4 (AF)   |   TSC_G7_IO3    |
173     |   PE5 (AF)   |   TSC_G7_IO4    |
174     |--------------|-----------------|
175     |   PF14 (AF)  |   TSC_G8_IO1    |
176     |   PF15 (AF)  |   TSC_G8_IO2    |
177     |   PG0 (AF)   |   TSC_G8_IO3    |
178     |   PG1 (AF)   |   TSC_G8_IO4    |
179     |--------------|-----------------|
180     |   PB10 (AF)  |   TSC_SYNC      |
181     |   PD2 (AF)   |                 |
182     +--------------------------------+
183 
184   ******************************************************************************
185   * @attention
186   *
187   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
188   * All rights reserved.</center></h2>
189   *
190   * This software component is licensed by ST under BSD 3-Clause license,
191   * the "License"; You may not use this file except in compliance with the
192   * License. You may obtain a copy of the License at:
193   *                        opensource.org/licenses/BSD-3-Clause
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_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, pTSC_CallbackTypeDef pCallback)
435 {
436   HAL_StatusTypeDef status = HAL_OK;
437 
438   if (pCallback == NULL)
439   {
440     /* Update the error code */
441     htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
442 
443     return HAL_ERROR;
444   }
445   /* Process locked */
446   __HAL_LOCK(htsc);
447 
448   if (HAL_TSC_STATE_READY == htsc->State)
449   {
450     switch (CallbackID)
451     {
452       case HAL_TSC_CONV_COMPLETE_CB_ID :
453         htsc->ConvCpltCallback = pCallback;
454         break;
455 
456       case HAL_TSC_ERROR_CB_ID :
457         htsc->ErrorCallback = pCallback;
458         break;
459 
460       case HAL_TSC_MSPINIT_CB_ID :
461         htsc->MspInitCallback = pCallback;
462         break;
463 
464       case HAL_TSC_MSPDEINIT_CB_ID :
465         htsc->MspDeInitCallback = pCallback;
466         break;
467 
468       default :
469         /* Update the error code */
470         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
471 
472         /* Return error status */
473         status =  HAL_ERROR;
474         break;
475     }
476   }
477   else if (HAL_TSC_STATE_RESET == htsc->State)
478   {
479     switch (CallbackID)
480     {
481       case HAL_TSC_MSPINIT_CB_ID :
482         htsc->MspInitCallback = pCallback;
483         break;
484 
485       case HAL_TSC_MSPDEINIT_CB_ID :
486         htsc->MspDeInitCallback = pCallback;
487         break;
488 
489       default :
490         /* Update the error code */
491         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
492 
493         /* Return error status */
494         status =  HAL_ERROR;
495         break;
496     }
497   }
498   else
499   {
500     /* Update the error code */
501     htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
502 
503     /* Return error status */
504     status =  HAL_ERROR;
505   }
506 
507   /* Release Lock */
508   __HAL_UNLOCK(htsc);
509   return status;
510 }
511 
512 /**
513   * @brief  Unregister an TSC Callback
514   *         TSC callback is redirected to the weak predefined callback
515   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
516   *                the configuration information for the specified TSC.
517   * @param  CallbackID ID of the callback to be unregistered
518   *         This parameter can be one of the following values:
519   *         This parameter can be one of the following values:
520   *          @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
521   *          @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
522   *          @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
523   *          @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
524   * @retval HAL status
525   */
HAL_TSC_UnRegisterCallback(TSC_HandleTypeDef * htsc,HAL_TSC_CallbackIDTypeDef CallbackID)526 HAL_StatusTypeDef HAL_TSC_UnRegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID)
527 {
528   HAL_StatusTypeDef status = HAL_OK;
529 
530   /* Process locked */
531   __HAL_LOCK(htsc);
532 
533   if (HAL_TSC_STATE_READY == htsc->State)
534   {
535     switch (CallbackID)
536     {
537       case HAL_TSC_CONV_COMPLETE_CB_ID :
538         htsc->ConvCpltCallback = HAL_TSC_ConvCpltCallback;       /* Legacy weak ConvCpltCallback      */
539         break;
540 
541       case HAL_TSC_ERROR_CB_ID :
542         htsc->ErrorCallback = HAL_TSC_ErrorCallback;               /* Legacy weak ErrorCallback        */
543         break;
544 
545       case HAL_TSC_MSPINIT_CB_ID :
546         htsc->MspInitCallback = HAL_TSC_MspInit;                   /* Legacy weak MspInit              */
547         break;
548 
549       case HAL_TSC_MSPDEINIT_CB_ID :
550         htsc->MspDeInitCallback = HAL_TSC_MspDeInit;               /* Legacy weak MspDeInit            */
551         break;
552 
553       default :
554         /* Update the error code */
555         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
556 
557         /* Return error status */
558         status =  HAL_ERROR;
559         break;
560     }
561   }
562   else if (HAL_TSC_STATE_RESET == htsc->State)
563   {
564     switch (CallbackID)
565     {
566       case HAL_TSC_MSPINIT_CB_ID :
567         htsc->MspInitCallback = HAL_TSC_MspInit;                   /* Legacy weak MspInit              */
568         break;
569 
570       case HAL_TSC_MSPDEINIT_CB_ID :
571         htsc->MspDeInitCallback = HAL_TSC_MspDeInit;               /* Legacy weak MspDeInit            */
572         break;
573 
574       default :
575         /* Update the error code */
576         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
577 
578         /* Return error status */
579         status =  HAL_ERROR;
580         break;
581     }
582   }
583   else
584   {
585     /* Update the error code */
586     htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
587 
588     /* Return error status */
589     status =  HAL_ERROR;
590   }
591 
592   /* Release Lock */
593   __HAL_UNLOCK(htsc);
594   return status;
595 }
596 
597 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
598 
599 /**
600   * @}
601   */
602 
603 /** @defgroup TSC_Exported_Functions_Group2 Input and Output operation functions
604  *  @brief    Input and Output operation functions
605  *
606 @verbatim
607  ===============================================================================
608              ##### IO Operation functions #####
609  ===============================================================================
610     [..]  This section provides functions allowing to:
611       (+) Start acquisition in polling mode.
612       (+) Start acquisition in interrupt mode.
613       (+) Stop conversion in polling mode.
614       (+) Stop conversion in interrupt mode.
615       (+) Poll for acquisition completed.
616       (+) Get group acquisition status.
617       (+) Get group acquisition value.
618 @endverbatim
619   * @{
620   */
621 
622 /**
623   * @brief  Start the acquisition.
624   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
625   *         the configuration information for the specified TSC.
626   * @retval HAL status
627   */
HAL_TSC_Start(TSC_HandleTypeDef * htsc)628 HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef *htsc)
629 {
630   /* Check the parameters */
631   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
632 
633   /* Process locked */
634   __HAL_LOCK(htsc);
635 
636   /* Change TSC state */
637   htsc->State = HAL_TSC_STATE_BUSY;
638 
639   /* Clear interrupts */
640   __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
641 
642   /* Clear flags */
643   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
644 
645   /* Set touch sensing IOs not acquired to the specified IODefaultMode */
646   if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
647   {
648     __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
649   }
650   else
651   {
652     __HAL_TSC_SET_IODEF_INFLOAT(htsc);
653   }
654 
655   /* Launch the acquisition */
656   __HAL_TSC_START_ACQ(htsc);
657 
658   /* Process unlocked */
659   __HAL_UNLOCK(htsc);
660 
661   /* Return function status */
662   return HAL_OK;
663 }
664 
665 /**
666   * @brief  Start the acquisition in interrupt mode.
667   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
668   *         the configuration information for the specified TSC.
669   * @retval HAL status.
670   */
HAL_TSC_Start_IT(TSC_HandleTypeDef * htsc)671 HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef *htsc)
672 {
673   /* Check the parameters */
674   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
675   assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
676 
677   /* Process locked */
678   __HAL_LOCK(htsc);
679 
680   /* Change TSC state */
681   htsc->State = HAL_TSC_STATE_BUSY;
682 
683   /* Enable end of acquisition interrupt */
684   __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA);
685 
686   /* Enable max count error interrupt (optional) */
687   if (htsc->Init.MaxCountInterrupt == ENABLE)
688   {
689     __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE);
690   }
691   else
692   {
693     __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE);
694   }
695 
696   /* Clear flags */
697   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
698 
699   /* Set touch sensing IOs not acquired to the specified IODefaultMode */
700   if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
701   {
702     __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
703   }
704   else
705   {
706     __HAL_TSC_SET_IODEF_INFLOAT(htsc);
707   }
708 
709   /* Launch the acquisition */
710   __HAL_TSC_START_ACQ(htsc);
711 
712   /* Process unlocked */
713   __HAL_UNLOCK(htsc);
714 
715   /* Return function status */
716   return HAL_OK;
717 }
718 
719 /**
720   * @brief  Stop the acquisition previously launched in polling mode.
721   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
722   *         the configuration information for the specified TSC.
723   * @retval HAL status
724   */
HAL_TSC_Stop(TSC_HandleTypeDef * htsc)725 HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef *htsc)
726 {
727   /* Check the parameters */
728   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
729 
730   /* Process locked */
731   __HAL_LOCK(htsc);
732 
733   /* Stop the acquisition */
734   __HAL_TSC_STOP_ACQ(htsc);
735 
736   /* Set touch sensing IOs in low power mode (output push-pull) */
737   __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
738 
739   /* Clear flags */
740   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
741 
742   /* Change TSC state */
743   htsc->State = HAL_TSC_STATE_READY;
744 
745   /* Process unlocked */
746   __HAL_UNLOCK(htsc);
747 
748   /* Return function status */
749   return HAL_OK;
750 }
751 
752 /**
753   * @brief  Stop the acquisition previously launched in interrupt mode.
754   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
755   *         the configuration information for the specified TSC.
756   * @retval HAL status
757   */
HAL_TSC_Stop_IT(TSC_HandleTypeDef * htsc)758 HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef *htsc)
759 {
760   /* Check the parameters */
761   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
762 
763   /* Process locked */
764   __HAL_LOCK(htsc);
765 
766   /* Stop the acquisition */
767   __HAL_TSC_STOP_ACQ(htsc);
768 
769   /* Set touch sensing IOs in low power mode (output push-pull) */
770   __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
771 
772   /* Disable interrupts */
773   __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
774 
775   /* Clear flags */
776   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
777 
778   /* Change TSC state */
779   htsc->State = HAL_TSC_STATE_READY;
780 
781   /* Process unlocked */
782   __HAL_UNLOCK(htsc);
783 
784   /* Return function status */
785   return HAL_OK;
786 }
787 
788 /**
789   * @brief  Start acquisition and wait until completion.
790   * @note   There is no need of a timeout parameter as the max count error is already
791   *         managed by the TSC peripheral.
792   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
793   *         the configuration information for the specified TSC.
794   * @retval HAL state
795   */
HAL_TSC_PollForAcquisition(TSC_HandleTypeDef * htsc)796 HAL_StatusTypeDef HAL_TSC_PollForAcquisition(TSC_HandleTypeDef *htsc)
797 {
798   /* Check the parameters */
799   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
800 
801   /* Process locked */
802   __HAL_LOCK(htsc);
803 
804   /* Check end of acquisition */
805   while (HAL_TSC_GetState(htsc) == HAL_TSC_STATE_BUSY)
806   {
807     /* The timeout (max count error) is managed by the TSC peripheral itself. */
808   }
809 
810   /* Process unlocked */
811   __HAL_UNLOCK(htsc);
812 
813   return HAL_OK;
814 }
815 
816 /**
817   * @brief  Get the acquisition status for a group.
818   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
819   *         the configuration information for the specified TSC.
820   * @param  gx_index Index of the group
821   * @retval Group status
822   */
HAL_TSC_GroupGetStatus(TSC_HandleTypeDef * htsc,uint32_t gx_index)823 TSC_GroupStatusTypeDef HAL_TSC_GroupGetStatus(TSC_HandleTypeDef *htsc, uint32_t gx_index)
824 {
825   /* Check the parameters */
826   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
827   assert_param(IS_TSC_GROUP_INDEX(gx_index));
828 
829   /* Return the group status */
830   return (__HAL_TSC_GET_GROUP_STATUS(htsc, gx_index));
831 }
832 
833 /**
834   * @brief  Get the acquisition measure for a group.
835   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
836   *         the configuration information for the specified TSC.
837   * @param  gx_index Index of the group
838   * @retval Acquisition measure
839   */
HAL_TSC_GroupGetValue(TSC_HandleTypeDef * htsc,uint32_t gx_index)840 uint32_t HAL_TSC_GroupGetValue(TSC_HandleTypeDef *htsc, uint32_t gx_index)
841 {
842   /* Check the parameters */
843   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
844   assert_param(IS_TSC_GROUP_INDEX(gx_index));
845 
846   /* Return the group acquisition counter */
847   return htsc->Instance->IOGXCR[gx_index];
848 }
849 
850 /**
851   * @}
852   */
853 
854 /** @defgroup TSC_Exported_Functions_Group3 Peripheral Control functions
855  *  @brief    Peripheral Control functions
856  *
857 @verbatim
858  ===============================================================================
859              ##### Peripheral Control functions #####
860  ===============================================================================
861     [..]  This section provides functions allowing to:
862       (+) Configure TSC IOs
863       (+) Discharge TSC IOs
864 @endverbatim
865   * @{
866   */
867 
868 /**
869   * @brief  Configure TSC IOs.
870   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
871   *         the configuration information for the specified TSC.
872   * @param  config Pointer to the configuration structure.
873   * @retval HAL status
874   */
HAL_TSC_IOConfig(TSC_HandleTypeDef * htsc,TSC_IOConfigTypeDef * config)875 HAL_StatusTypeDef HAL_TSC_IOConfig(TSC_HandleTypeDef *htsc, TSC_IOConfigTypeDef *config)
876 {
877   /* Check the parameters */
878   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
879   assert_param(IS_TSC_GROUP(config->ChannelIOs));
880   assert_param(IS_TSC_GROUP(config->ShieldIOs));
881   assert_param(IS_TSC_GROUP(config->SamplingIOs));
882 
883   /* Process locked */
884   __HAL_LOCK(htsc);
885 
886   /* Stop acquisition */
887   __HAL_TSC_STOP_ACQ(htsc);
888 
889   /* Disable Schmitt trigger hysteresis on all used TSC IOs */
890   htsc->Instance->IOHCR = (~(config->ChannelIOs | config->ShieldIOs | config->SamplingIOs));
891 
892   /* Set channel and shield IOs */
893   htsc->Instance->IOCCR = (config->ChannelIOs | config->ShieldIOs);
894 
895   /* Set sampling IOs */
896   htsc->Instance->IOSCR = config->SamplingIOs;
897 
898   /* Set groups to be acquired */
899   htsc->Instance->IOGCSR = TSC_extract_groups(config->ChannelIOs);
900 
901   /* Process unlocked */
902   __HAL_UNLOCK(htsc);
903 
904   /* Return function status */
905   return HAL_OK;
906 }
907 
908 /**
909   * @brief  Discharge TSC IOs.
910   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
911   *         the configuration information for the specified TSC.
912   * @param  choice This parameter can be set to ENABLE or DISABLE.
913   * @retval HAL status
914   */
HAL_TSC_IODischarge(TSC_HandleTypeDef * htsc,FunctionalState choice)915 HAL_StatusTypeDef HAL_TSC_IODischarge(TSC_HandleTypeDef *htsc, FunctionalState choice)
916 {
917   /* Check the parameters */
918   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
919 
920   /* Process locked */
921   __HAL_LOCK(htsc);
922 
923   if (choice == ENABLE)
924   {
925     __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
926   }
927   else
928   {
929     __HAL_TSC_SET_IODEF_INFLOAT(htsc);
930   }
931 
932   /* Process unlocked */
933   __HAL_UNLOCK(htsc);
934 
935   /* Return the group acquisition counter */
936   return HAL_OK;
937 }
938 
939 /**
940   * @}
941   */
942 
943 /** @defgroup TSC_Exported_Functions_Group4 Peripheral State and Errors functions
944  *  @brief   Peripheral State and Errors functions
945  *
946 @verbatim
947  ===============================================================================
948             ##### State and Errors functions #####
949  ===============================================================================
950     [..]
951     This subsection provides functions allowing to
952       (+) Get TSC state.
953 
954 @endverbatim
955   * @{
956   */
957 
958 /**
959   * @brief  Return the TSC handle state.
960   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
961   *         the configuration information for the specified TSC.
962   * @retval HAL state
963   */
HAL_TSC_GetState(TSC_HandleTypeDef * htsc)964 HAL_TSC_StateTypeDef HAL_TSC_GetState(TSC_HandleTypeDef *htsc)
965 {
966   /* Check the parameters */
967   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
968 
969   if (htsc->State == HAL_TSC_STATE_BUSY)
970   {
971     /* Check end of acquisition flag */
972     if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
973     {
974       /* Check max count error flag */
975       if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
976       {
977         /* Change TSC state */
978         htsc->State = HAL_TSC_STATE_ERROR;
979       }
980       else
981       {
982         /* Change TSC state */
983         htsc->State = HAL_TSC_STATE_READY;
984       }
985     }
986   }
987 
988   /* Return TSC state */
989   return htsc->State;
990 }
991 
992 /**
993   * @}
994   */
995 
996 /** @defgroup TSC_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
997  * @{
998  */
999 
1000 /**
1001   * @brief  Handle TSC interrupt request.
1002   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
1003   *         the configuration information for the specified TSC.
1004   * @retval None
1005   */
HAL_TSC_IRQHandler(TSC_HandleTypeDef * htsc)1006 void HAL_TSC_IRQHandler(TSC_HandleTypeDef *htsc)
1007 {
1008   /* Check the parameters */
1009   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
1010 
1011   /* Check if the end of acquisition occurred */
1012   if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
1013   {
1014     /* Clear EOA flag */
1015     __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA);
1016   }
1017 
1018   /* Check if max count error occurred */
1019   if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
1020   {
1021     /* Clear MCE flag */
1022     __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE);
1023     /* Change TSC state */
1024     htsc->State = HAL_TSC_STATE_ERROR;
1025 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
1026     htsc->ErrorCallback(htsc);
1027 #else
1028     /* Conversion completed callback */
1029     HAL_TSC_ErrorCallback(htsc);
1030 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
1031   }
1032   else
1033   {
1034     /* Change TSC state */
1035     htsc->State = HAL_TSC_STATE_READY;
1036 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
1037     htsc->ConvCpltCallback(htsc);
1038 #else
1039     /* Conversion completed callback */
1040     HAL_TSC_ConvCpltCallback(htsc);
1041 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
1042   }
1043 }
1044 
1045 /**
1046   * @brief  Acquisition completed callback in non-blocking mode.
1047   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
1048   *         the configuration information for the specified TSC.
1049   * @retval None
1050   */
HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef * htsc)1051 __weak void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef *htsc)
1052 {
1053   /* Prevent unused argument(s) compilation warning */
1054   UNUSED(htsc);
1055 
1056   /* NOTE : This function should not be modified, when the callback is needed,
1057             the HAL_TSC_ConvCpltCallback could be implemented in the user file.
1058    */
1059 }
1060 
1061 /**
1062   * @brief  Error callback in non-blocking mode.
1063   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
1064   *         the configuration information for the specified TSC.
1065   * @retval None
1066   */
HAL_TSC_ErrorCallback(TSC_HandleTypeDef * htsc)1067 __weak void HAL_TSC_ErrorCallback(TSC_HandleTypeDef *htsc)
1068 {
1069   /* Prevent unused argument(s) compilation warning */
1070   UNUSED(htsc);
1071 
1072   /* NOTE : This function should not be modified, when the callback is needed,
1073             the HAL_TSC_ErrorCallback could be implemented in the user file.
1074    */
1075 }
1076 
1077 /**
1078   * @}
1079   */
1080 
1081 /**
1082   * @}
1083   */
1084 
1085 /* Private functions ---------------------------------------------------------*/
1086 /** @defgroup TSC_Private_Functions TSC Private Functions
1087   * @{
1088   */
1089 
1090 /**
1091   * @brief  Utility function used to set the acquired groups mask.
1092   * @param  iomask Channels IOs mask
1093   * @retval Acquired groups mask
1094   */
TSC_extract_groups(uint32_t iomask)1095 static uint32_t TSC_extract_groups(uint32_t iomask)
1096 {
1097   uint32_t groups = 0UL;
1098   uint32_t idx;
1099 
1100   for (idx = 0UL; idx < (uint32_t)TSC_NB_OF_GROUPS; idx++)
1101   {
1102     if ((iomask & (0x0FUL << (idx * 4UL))) != 0UL )
1103     {
1104       groups |= (1UL << idx);
1105     }
1106   }
1107 
1108   return groups;
1109 }
1110 
1111 /**
1112   * @}
1113   */
1114 
1115 #endif /* HAL_TSC_MODULE_ENABLED */
1116 
1117 /**
1118   * @}
1119   */
1120 
1121 /**
1122   * @}
1123   */
1124 
1125 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1126