1 /*!
2  * @file        tsc_touchkey.c
3  *
4  * @brief       This file contains all functions to manage TouchKey sensors.
5  *
6  * @version     V1.0.1
7  *
8  * @date        2022-09-20
9  *
10  * @attention
11  *
12  *  Copyright (C) 2020-2022 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* Includes */
27 #include "tsc.h"
28 #include "tsc_touchkey.h"
29 
30 /** @addtogroup TSC_Driver_Library TSC Driver Library
31   @{
32 */
33 
34 /** @addtogroup TSC_TouchKey_Driver TSC TouchKey Driver
35   @{
36 */
37 
38 /** @defgroup TSC_TouchKey_Macros Macros
39   @{
40 */
41 
42 #if TOUCH_TOTAL_KEYS > 0
43 
44 #define FOR_MEAS                 TSC_Globals.For_Key->p_ChD->Meas
45 #define FOR_DELTA                TSC_Globals.For_Key->p_ChD->Delta
46 #define FOR_REFER                TSC_Globals.For_Key->p_ChD->Refer
47 #define FOR_REFREST              TSC_Globals.For_Key->p_ChD->RefRest
48 #define FOR_CHANNEL_DATA         TSC_Globals.For_Key->p_ChD
49 #define FOR_ACQ_STATUS           TSC_Globals.For_Key->p_ChD->Flag.AcqStatus
50 #define FOR_OBJ_STATUS           TSC_Globals.For_Key->p_ChD->Flag.ObjStatus
51 #define FOR_DATA_READY           TSC_Globals.For_Key->p_ChD->Flag.DataReady
52 
53 #define FOR_STATEID              TSC_Globals.For_Key->p_Data->StateId
54 #define FOR_CHANGE               TSC_Globals.For_Key->p_Data->Change
55 #define FOR_COUNTER_DEB          TSC_Globals.For_Key->p_Data->CounterDebounce
56 #define FOR_COUNTER_DTO          TSC_Globals.For_Key->p_Data->CounterDTO
57 #define FOR_DXSLOCK              TSC_Globals.For_Key->p_Data->DxsLock
58 
59 #define FOR_PROXIN_TH            TSC_Globals.For_Key->p_Param->ProxInTh
60 #define FOR_PROXOUT_TH           TSC_Globals.For_Key->p_Param->ProxOutTh
61 #define FOR_DETECTIN_TH          TSC_Globals.For_Key->p_Param->DetectInTh
62 #define FOR_DETECTOUT_TH         TSC_Globals.For_Key->p_Param->DetectOutTh
63 #define FOR_CALIB_TH             TSC_Globals.For_Key->p_Param->CalibTh
64 
65 #define FOR_COUNTER_DEB_CALIB    TSC_Globals.For_Key->p_Param->CounterDebCalib
66 #define FOR_COUNTER_DEB_PROX     TSC_Globals.For_Key->p_Param->CounterDebProx
67 #define FOR_COUNTER_DEB_DETECT   TSC_Globals.For_Key->p_Param->CounterDebDetect
68 #define FOR_COUNTER_DEB_RELEASE  TSC_Globals.For_Key->p_Param->CounterDebRelease
69 #define FOR_COUNTER_DEB_ERROR    TSC_Globals.For_Key->p_Param->CounterDebError
70 
71 #if TOUCH_DTO > 0
72     #define DTO_READ_TIME  {TSC_TouchKey_DTOGetTime();}
73 #else
74     #define DTO_READ_TIME
75 #endif
76 
77 #if TOUCH_COEFF_TH > 0
78     #define TEST_DELTA(OPER,TH)   (FOR_DELTA OPER (uint16_t)((uint16_t)TH << TOUCH_COEFF_TH))
79     #define TEST_DELTA_N(OPER,TH) (FOR_DELTA OPER -((uint16_t)((uint16_t)TH << TOUCH_COEFF_TH)))
80     #define TEST_DELTA_NEGATIVE   {if (FOR_DELTA < 0) {return;}}
81 #else
82     #define TEST_DELTA(OPER,TH)   (FOR_DELTA OPER TH)
83     #define TEST_DELTA_N(OPER,TH) (FOR_DELTA OPER -(TH))
84     #define TEST_DELTA_NEGATIVE
85 #endif
86 
87 /**@} end of group TSC_TouchKey_Macros */
88 
89 /** @defgroup TSC_TouchKey_Enumerations Enumerations
90   @{
91 */
92 
93 /**@} end of group TSC_TouchKey_Enumerations */
94 
95 /** @defgroup TSC_TouchKey_Structures Structures
96   @{
97 */
98 
99 /**@} end of group TSC_TouchKey_Structures */
100 
101 /** @defgroup TSC_TouchKey_Variables Variables
102   @{
103 */
104 
105 static TSC_tNum_T CalibDiv;
106 
107 /**@} end of group TSC_TouchKey_Variables */
108 
109 /** @defgroup TSC_TouchKey_Functions Functions
110   @{
111 */
112 
113 void TSC_TouchKey_DTOGetTime(void);
114 
115 /*!
116  * @brief       Config parameters with default values from configuration file
117  *
118  * @param       None
119  *
120  * @retval      None
121  */
TSC_TouchKey_Config(void)122 void TSC_TouchKey_Config(void)
123 {
124     /* Debounce counters */
125     FOR_COUNTER_DEB_CALIB   = TOUCH_DEBOUNCE_CALIB;
126 #if TOUCH_USE_PROX > 0
127     FOR_COUNTER_DEB_PROX    = TOUCH_DEBOUNCE_PROX;
128 #endif
129     FOR_COUNTER_DEB_DETECT  = TOUCH_DEBOUNCE_DETECT;
130     FOR_COUNTER_DEB_RELEASE = TOUCH_DEBOUNCE_RELEASE;
131     FOR_COUNTER_DEB_ERROR   = TOUCH_DEBOUNCE_ERROR;
132 
133     /* Thresholds */
134 #if TOUCH_USE_PROX > 0
135     FOR_PROXIN_TH    = TOUCH_KEY_PROX_IN_TH;
136     FOR_PROXOUT_TH   = TOUCH_KEY_PROX_OUT_TH;
137 #endif
138     FOR_DETECTIN_TH  = TOUCH_KEY_DETECT_IN_TH;
139     FOR_DETECTOUT_TH = TOUCH_KEY_DETECT_OUT_TH;
140     FOR_CALIB_TH     = TOUCH_KEY_CALIB_TH;
141 
142     /* Config state */
143     TSC_TouchKey_ConfigCalibrationState(TOUCH_CALIB_DELAY);
144 }
145 
146 /*!
147  * @brief       Process the State Machine
148  *
149  * @param       None
150  *
151  * @retval      None
152  */
TSC_TouchKey_Process(void)153 void TSC_TouchKey_Process(void)
154 {
155     TSC_STATEID_T stateID;
156 
157     if ((FOR_STATEID == TSC_STATEID_OFF) || (FOR_DATA_READY != 0))
158     {
159         FOR_DATA_READY = TSC_DATA_NOT_READY;
160         stateID = FOR_STATEID;
161 
162 #if TOUCH_TOTAL_TOUCHKEYS > 0
163         if (TSC_Globals.For_Obj->Type == TSC_OBJ_TOUCHKEY)
164         {
165             TSC_Globals.For_Key->p_SM[FOR_STATEID].StateFunc();
166         }
167 #endif
168 
169 #if TOUCH_TOTAL_TOUCHKEYS_B > 0
170         if (TSC_Globals.For_Obj->Type == TSC_OBJ_TOUCHKEYB)
171         {
172             TSC_Params.p_KeySta[FOR_STATEID].StateFunc();
173         }
174 #endif
175 
176         /* Check if the new state has changed */
177         if (FOR_STATEID != stateID)
178         {
179             FOR_CHANGE = TSC_STATE_CHANGED;
180         }
181         else
182         {
183             FOR_CHANGE = TSC_STATE_NOT_CHANGED;
184         }
185 
186 #if TOUCH_USE_DXS > 0
187         if (FOR_STATEID == TSC_STATEID_TOUCH)
188         {
189             FOR_STATEID = TSC_STATEID_DETECT;
190         }
191         if (FOR_STATEID != TSC_STATEID_DETECT)
192         {
193             FOR_DXSLOCK = TSC_FALSE;
194         }
195 #endif
196     }
197 }
198 
199 /*!
200  * @brief       Go in Calibration state
201  *
202  * @param       delay: Delay before calibration starts (stabilization of noise filter)
203  *
204  * @retval      None
205  */
TSC_TouchKey_ConfigCalibrationState(TSC_tCounter_T delay)206 void TSC_TouchKey_ConfigCalibrationState(TSC_tCounter_T delay)
207 {
208     FOR_STATEID = TSC_STATEID_CALIB;
209     FOR_CHANGE = TSC_STATE_CHANGED;
210     FOR_OBJ_STATUS = TSC_OBJ_STATUS_ON;
211 
212     if (TSC_Params.NumCalibSample == 4)
213     {
214         CalibDiv = 2;
215     }
216     else if (TSC_Params.NumCalibSample == 16)
217     {
218         CalibDiv = 4;
219     }
220     else
221     {
222         TSC_Params.NumCalibSample =  8;
223         CalibDiv = 3;
224     }
225     /* If a noise filter is used, the counter must be initialized to a value
226      * different from 0 in order to stabilize the filter. */
227     FOR_COUNTER_DEB = (TSC_tCounter_T)(delay + (TSC_tCounter_T)TSC_Params.NumCalibSample);
228     FOR_REFER = 0;
229 }
230 
231 /*!
232  * @brief       Go in Off state with sensor "off"
233  *
234  * @param       None
235  *
236  * @retval      None
237  */
TSC_TouchKey_ConfigOffState(void)238 void TSC_TouchKey_ConfigOffState(void)
239 {
240     FOR_STATEID = TSC_STATEID_OFF;
241     FOR_CHANGE = TSC_STATE_CHANGED;
242     FOR_OBJ_STATUS = TSC_OBJ_STATUS_OFF;
243 }
244 
245 /*!
246  * @brief       Go in Off state with sensor in "Burst mode only"
247  *
248  * @param       None
249  *
250  * @retval      None
251  */
TSC_TouchKey_ConfigBurstOnlyState(void)252 void TSC_TouchKey_ConfigBurstOnlyState(void)
253 {
254     FOR_STATEID = TSC_STATEID_OFF;
255     FOR_CHANGE = TSC_STATE_CHANGED;
256     FOR_OBJ_STATUS = TSC_OBJ_STATUS_BURST_ONLY;
257 }
258 
259 /*!
260  * @brief       Return the current state identifier
261  *
262  * @param       None
263  *
264  * @retval      State id
265  */
TSC_TouchKey_ReadStateId(void)266 TSC_STATEID_T TSC_TouchKey_ReadStateId(void)
267 {
268     return (FOR_STATEID);
269 }
270 
271 /*!
272  * @brief       Return the current state mask
273  *
274  * @param       None
275  *
276  * @retval      State mask
277  */
TSC_TouchKey_ReadStateMask(void)278 TSC_STATEMASK_T TSC_TouchKey_ReadStateMask(void)
279 {
280     TSC_STATEMASK_T state_mask = TSC_STATEMASK_UNKNOWN;
281 
282 #if TOUCH_TOTAL_TOUCHKEYS > 0
283     if (TSC_Globals.For_Obj->Type == TSC_OBJ_TOUCHKEY)
284     {
285         state_mask = TSC_Globals.For_Key->p_SM[FOR_STATEID].StateMask;
286     }
287 #endif
288 
289 #if TOUCH_TOTAL_TOUCHKEYS_B > 0
290     if (TSC_Globals.For_Obj->Type == TSC_OBJ_TOUCHKEYB)
291     {
292         state_mask = TSC_Params.p_KeySta[FOR_STATEID].StateMask;
293     }
294 #endif
295 
296     return state_mask;
297 }
298 
299 /*!
300  * @brief       Return the Change flag
301  *
302  * @param       None
303  *
304  * @retval      Change flag status
305  */
TSC_TouchKey_ReadChangeFlag(void)306 TSC_tNum_T TSC_TouchKey_ReadChangeFlag(void)
307 {
308     return (FOR_CHANGE);
309 }
310 
311 #if TOUCH_USE_PROX > 0
312 /*!
313  * @brief       Debounce Release processing (previous state = Proximity)
314  *
315  * @param       None
316  *
317  * @retval      None
318  */
TSC_TouchKey_ProcessDebReleaseProxState(void)319 void TSC_TouchKey_ProcessDebReleaseProxState(void)
320 {
321     /* Acquisition error (min or max) */
322     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
323     {
324         FOR_STATEID = TSC_STATEID_PROX;
325     }
326     else /*!< Acquisition is OK or has NOISE */
327     {
328         if (FOR_DELTA <= FOR_PROXOUT_TH)
329         {
330             if (FOR_COUNTER_DEB > 0)
331             {
332                 FOR_COUNTER_DEB--;
333             }
334             if (FOR_COUNTER_DEB == 0)
335             {
336                 FOR_STATEID = TSC_STATEID_RELEASE;
337             }
338         }
339         else
340         {
341             FOR_STATEID = TSC_STATEID_PROX;
342         }
343     }
344 }
345 #endif /*!< if TOUCH_USE_PROX > 0 */
346 
347 /*!
348  * @brief       Debounce Release processing (previous state = Detect)
349  *
350  * @param       None
351  *
352  * @retval      None
353  */
TSC_TouchKey_ProcessDebReleaseDetectState(void)354 void TSC_TouchKey_ProcessDebReleaseDetectState(void)
355 {
356     /* Acquisition error (min or max) */
357     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
358     {
359         FOR_STATEID = TSC_STATEID_DETECT;
360     }
361     else /*!< Acquisition is OK or has NOISE */
362     {
363         if TEST_DELTA( <=, FOR_DETECTOUT_TH)
364         {
365 #if TOUCH_USE_PROX > 0
366             if (FOR_DELTA > FOR_PROXOUT_TH)
367             {
368                 FOR_STATEID = TSC_STATEID_PROX;
369                 return;
370             }
371 #endif
372             if (FOR_COUNTER_DEB > 0)
373             {
374                 FOR_COUNTER_DEB--;
375             }
376             if (FOR_COUNTER_DEB == 0)
377             {
378                 FOR_STATEID = TSC_STATEID_RELEASE;
379             }
380         }
381         else
382         {
383             TEST_DELTA_NEGATIVE;
384             FOR_STATEID = TSC_STATEID_DETECT;
385         }
386     }
387 }
388 
389 /*!
390  * @brief       Debounce Release processing (previous state = Touch)
391  *              Same as Debounce Release Detect processing
392  *
393  * @param       None
394  *
395  * @retval      None
396  */
TSC_TouchKey_ProcessDebReleaseTouchState(void)397 void TSC_TouchKey_ProcessDebReleaseTouchState(void)
398 {
399     /* Acquisition error (min or max) */
400     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
401     {
402         FOR_STATEID = TSC_STATEID_TOUCH;
403     }
404     else /*!< Acquisition is OK or has NOISE */
405     {
406         if TEST_DELTA( <=, FOR_DETECTOUT_TH)
407         {
408 #if TOUCH_USE_PROX > 0
409             if (FOR_DELTA > FOR_PROXOUT_TH)
410             {
411                 FOR_STATEID = TSC_STATEID_PROX;
412                 return;
413             }
414 #endif
415             if (FOR_COUNTER_DEB > 0)
416             {
417                 FOR_COUNTER_DEB--;
418             }
419             if (FOR_COUNTER_DEB == 0)
420             {
421                 FOR_STATEID = TSC_STATEID_RELEASE;
422             }
423         }
424         else
425         {
426             TEST_DELTA_NEGATIVE;
427             FOR_STATEID = TSC_STATEID_TOUCH;
428         }
429     }
430 }
431 
432 /*!
433  * @brief       Release state processing
434  *
435  * @param       None
436  *
437  * @retval      None
438  */
TSC_TouchKey_ProcessReleaseState(void)439 void TSC_TouchKey_ProcessReleaseState(void)
440 {
441     /* Acquisition error (min or max) */
442     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
443     {
444         FOR_COUNTER_DEB = FOR_COUNTER_DEB_ERROR;
445         if (FOR_COUNTER_DEB)
446         {
447             FOR_STATEID = TSC_STATEID_DEB_ERROR_RELEASE;
448         }
449         else
450         {
451             FOR_STATEID = TSC_STATEID_ERROR;
452         }
453     }
454     else /*!< Acquisition is OK or has NOISE */
455     {
456         if TEST_DELTA( >=, FOR_DETECTIN_TH)
457         {
458             TEST_DELTA_NEGATIVE;
459             FOR_COUNTER_DEB = FOR_COUNTER_DEB_DETECT;
460             if (FOR_COUNTER_DEB)
461             {
462                 FOR_STATEID = TSC_STATEID_DEB_DETECT;
463             }
464             else
465             {
466                 FOR_STATEID = TSC_STATEID_DETECT;
467                 DTO_READ_TIME;
468             }
469             return;
470         }
471 
472 #if TOUCH_USE_PROX > 0
473         if (FOR_DELTA >= FOR_PROXIN_TH)
474         {
475             FOR_COUNTER_DEB = FOR_COUNTER_DEB_PROX;
476             if (FOR_COUNTER_DEB)
477             {
478                 FOR_STATEID = TSC_STATEID_DEB_PROX;
479             }
480             else
481             {
482                 FOR_STATEID = TSC_STATEID_PROX;
483                 DTO_READ_TIME;
484             }
485             return;
486         }
487 #endif
488 
489         /* Check delta for re-calibration
490             Warning: the threshold value is inverted in the macro */
491         if TEST_DELTA_N( <=, FOR_CALIB_TH)
492         {
493             FOR_COUNTER_DEB = FOR_COUNTER_DEB_CALIB;
494             if (FOR_COUNTER_DEB)
495             {
496                 FOR_STATEID = TSC_STATEID_DEB_CALIB;
497             }
498             else
499             {
500                 TSC_TouchKey_ConfigCalibrationState(0);
501             }
502         }
503     }
504 }
505 
506 /*!
507  * @brief       Debounce Calibration processing (previous state = Release)
508  *
509  * @param       None
510  *
511  * @retval      None
512  */
TSC_TouchKey_ProcessDebCalibrationState(void)513 void TSC_TouchKey_ProcessDebCalibrationState(void)
514 {
515     /* Acquisition error (min or max) */
516     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
517     {
518         FOR_STATEID = TSC_STATEID_RELEASE;
519     }
520     else /*!< Acquisition is OK or has NOISE */
521     {
522         /* Still below recalibration threshold
523             Warning: the threshold value is inverted in the macro */
524         if TEST_DELTA_N( >, FOR_CALIB_TH)
525         {
526             FOR_STATEID = TSC_STATEID_RELEASE;
527         }
528         else
529         {
530             if (FOR_COUNTER_DEB > 0)
531             {
532                 FOR_COUNTER_DEB--;
533             }
534             if (FOR_COUNTER_DEB == 0)
535             {
536                 TSC_TouchKey_ConfigCalibrationState(0);
537             }
538         }
539     }
540 }
541 
542 /*!
543  * @brief       Calibration state processing
544  *
545  * @param       None
546  *
547  * @retval      None
548  */
TSC_TouchKey_ProcessCalibrationState(void)549 void TSC_TouchKey_ProcessCalibrationState(void)
550 {
551     TSC_tMeas_T newMeas;
552 
553 #if TOUCH_CALIB_DELAY > 0
554     /* Noise filter stabilization time */
555     if (FOR_COUNTER_DEB > (TSC_tCounter_T)TSC_Params.NumCalibSample)
556     {
557         FOR_COUNTER_DEB--;
558         return;
559     }
560 #endif
561     /* Acquisition error (min or max) */
562     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
563     {
564         FOR_COUNTER_DEB = FOR_COUNTER_DEB_ERROR;
565         if (FOR_COUNTER_DEB)
566         {
567             FOR_STATEID = TSC_STATEID_DEB_ERROR_CALIB;
568         }
569         else
570         {
571             FOR_STATEID = TSC_STATEID_ERROR;
572         }
573     }
574     else /*!< Acquisition is OK or has NOISE */
575     {
576         /* Read the new measure or Calculate it */
577 #if TOUCH_USE_MEAS > 0
578         newMeas = FOR_MEAS;
579 #else
580         newMeas = TSC_Acq_ComputeMeas(FOR_REFER, FOR_DELTA);
581 #endif
582 
583         /* Verify the first Reference value */
584         if (FOR_COUNTER_DEB != (TSC_tCounter_T)TSC_Params.NumCalibSample)
585         {
586             FOR_REFER += newMeas;
587 
588             /* Check reference overflow */
589             if (FOR_REFER < newMeas)
590             {
591                 FOR_REFER = 0;
592                 FOR_STATEID = TSC_STATEID_ERROR;
593                 return;
594             }
595         }
596         else
597         {
598             if (TSC_Acq_TestFirstReference(FOR_CHANNEL_DATA, newMeas))
599             {
600                 FOR_REFER = newMeas;
601             }
602             else
603             {
604                 FOR_REFER = 0;
605                 return;
606             }
607         }
608 
609         if (FOR_COUNTER_DEB > 0)
610         {
611             FOR_COUNTER_DEB--;
612         }
613         if (FOR_COUNTER_DEB == 0)
614         {
615             FOR_REFER >>= CalibDiv;
616             FOR_REFREST = 0;
617             FOR_DELTA = 0;
618             FOR_STATEID = TSC_STATEID_RELEASE;
619         }
620     }
621 }
622 
623 #if TOUCH_USE_PROX > 0
624 /*!
625  * @brief       Debounce Proximity processing (previous state = Release)
626  *
627  * @param       None
628  *
629  * @retval      None
630  */
TSC_TouchKey_ProcessDebProxState(void)631 void TSC_TouchKey_ProcessDebProxState(void)
632 {
633     /* Acquisition error (min or max) */
634     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
635     {
636         FOR_STATEID = TSC_STATEID_RELEASE;
637     }
638     else /*!< Acquisition is OK or has NOISE */
639     {
640         if TEST_DELTA( >=, FOR_DETECTIN_TH)
641         {
642             TEST_DELTA_NEGATIVE;
643             FOR_COUNTER_DEB = FOR_COUNTER_DEB_DETECT;
644             if (FOR_COUNTER_DEB)
645             {
646                 FOR_STATEID = TSC_STATEID_DEB_DETECT;
647             }
648             else
649             {
650                 FOR_STATEID = TSC_STATEID_DETECT;
651                 DTO_READ_TIME;
652             }
653             return;
654         }
655 
656         if (FOR_DELTA < FOR_PROXIN_TH)
657         {
658             FOR_STATEID = TSC_STATEID_RELEASE;
659         }
660         else
661         {
662             if (FOR_COUNTER_DEB > 0)
663             {
664                 FOR_COUNTER_DEB--;
665             }
666             if (FOR_COUNTER_DEB == 0)
667             {
668                 FOR_STATEID = TSC_STATEID_PROX;
669                 DTO_READ_TIME;
670             }
671         }
672     }
673 }
674 #endif
675 
676 #if TOUCH_USE_PROX > 0
677 /*!
678  * @brief       Debounce Proximity processing (previous state = Detect)
679  *
680  * @param       None
681  *
682  * @retval      None
683  */
TSC_TouchKey_ProcessDebProxDetectState(void)684 void TSC_TouchKey_ProcessDebProxDetectState(void)
685 {
686     /* Acquisition error (min or max) */
687     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
688     {
689         FOR_STATEID = TSC_STATEID_DETECT;
690     }
691     else /*!< Acquisition is OK or has NOISE */
692     {
693         if TEST_DELTA( >, FOR_DETECTOUT_TH)
694         {
695             TEST_DELTA_NEGATIVE;
696             FOR_STATEID = TSC_STATEID_DETECT;
697             return;
698         }
699 
700         if (FOR_DELTA <= FOR_PROXOUT_TH)
701         {
702             FOR_COUNTER_DEB = FOR_COUNTER_DEB_RELEASE;
703             if (FOR_COUNTER_DEB)
704             {
705                 FOR_STATEID = TSC_STATEID_DEB_RELEASE_DETECT;
706             }
707             else
708             {
709                 FOR_STATEID = TSC_STATEID_RELEASE;
710             }
711         }
712         else
713         {
714             if (FOR_COUNTER_DEB > 0)
715             {
716                 FOR_COUNTER_DEB--;
717             }
718             if (FOR_COUNTER_DEB == 0)
719             {
720                 FOR_STATEID = TSC_STATEID_PROX;
721                 DTO_READ_TIME;
722             }
723         }
724     }
725 }
726 #endif
727 
728 #if TOUCH_USE_PROX > 0
729 /*!
730  * @brief       Debounce Proximity processing (previous state = Touch)
731  *
732  * @param       None
733  *
734  * @retval      None
735  */
TSC_TouchKey_ProcessDebProxTouchState(void)736 void TSC_TouchKey_ProcessDebProxTouchState(void)
737 {
738     /* Acquisition error (min or max) */
739     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
740     {
741         FOR_STATEID = TSC_STATEID_TOUCH;
742     }
743     else /*!< Acquisition is OK or has NOISE */
744     {
745         if TEST_DELTA( >, FOR_DETECTOUT_TH)
746         {
747             TEST_DELTA_NEGATIVE;
748             FOR_STATEID = TSC_STATEID_TOUCH;
749             return;
750         }
751 
752         if (FOR_DELTA <= FOR_PROXOUT_TH)
753         {
754             FOR_COUNTER_DEB = FOR_COUNTER_DEB_RELEASE;
755             if (FOR_COUNTER_DEB)
756             {
757                 FOR_STATEID = TSC_STATEID_DEB_RELEASE_TOUCH;
758             }
759             else
760             {
761                 FOR_STATEID = TSC_STATEID_RELEASE;
762             }
763         }
764         else
765         {
766             if (FOR_COUNTER_DEB > 0)
767             {
768                 FOR_COUNTER_DEB--;
769             }
770             if (FOR_COUNTER_DEB == 0)
771             {
772                 FOR_STATEID = TSC_STATEID_PROX;
773                 DTO_READ_TIME;
774             }
775         }
776     }
777 }
778 #endif
779 
780 #if TOUCH_USE_PROX > 0
781 /*!
782  * @brief       Proximity state processing
783  *
784  * @param       None
785  *
786  * @retval      None
787  */
TSC_TouchKey_ProcessProxState(void)788 void TSC_TouchKey_ProcessProxState(void)
789 {
790 #if TOUCH_DTO > 0
791     TSC_tTick_sec_T tick_detected;
792 #endif
793 
794     /* Acquisition error (min or max) */
795     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
796     {
797         FOR_COUNTER_DEB = FOR_COUNTER_DEB_ERROR;
798         if (FOR_COUNTER_DEB)
799         {
800             FOR_STATEID = TSC_STATEID_DEB_ERROR_PROX;
801         }
802         else
803         {
804             FOR_STATEID = TSC_STATEID_ERROR;
805         }
806     }
807     else /*!< Acquisition is OK or has NOISE */
808     {
809         if (FOR_DELTA <= FOR_PROXOUT_TH)
810         {
811             FOR_COUNTER_DEB = FOR_COUNTER_DEB_RELEASE;
812             if (FOR_COUNTER_DEB)
813             {
814                 FOR_STATEID = TSC_STATEID_DEB_RELEASE_PROX;
815             }
816             else
817             {
818                 FOR_STATEID = TSC_STATEID_RELEASE;
819             }
820             return;
821         }
822 
823         if TEST_DELTA( >=, FOR_DETECTIN_TH)
824         {
825             TEST_DELTA_NEGATIVE;
826             FOR_COUNTER_DEB = FOR_COUNTER_DEB_DETECT;
827             if (FOR_COUNTER_DEB)
828             {
829                 FOR_STATEID = TSC_STATEID_DEB_DETECT;
830             }
831             else
832             {
833                 FOR_STATEID = TSC_STATEID_DETECT;
834                 DTO_READ_TIME;
835             }
836             return;
837         }
838 
839         /* Stay in Proximity state */
840 #if TOUCH_DTO > 0
841         /* Detection Time Out (DTO) processing */
842         if ((TSC_Params.DTO > 1) && (TSC_Params.DTO < 64))
843         {
844             /* Read the detected time previously saved */
845             tick_detected = FOR_COUNTER_DTO;
846             /* Enter in calibration state if the DTO duration has elapsed */
847             if (TSC_Time_Delay_sec(TSC_Params.DTO, &tick_detected) == TSC_STATUS_OK)
848             {
849                 TSC_TouchKey_ConfigCalibrationState(0);
850             }
851         }
852 #endif
853     }
854 }
855 #endif
856 
857 /*!
858  * @brief       Debounce Detect processing (previous state = Release or Proximity)
859  *
860  * @param       None
861  *
862  * @retval      None
863  */
TSC_TouchKey_ProcessDebDetectState(void)864 void TSC_TouchKey_ProcessDebDetectState(void)
865 {
866     /* Acquisition error (min or max) */
867     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
868     {
869         FOR_STATEID = TSC_STATEID_RELEASE;
870     }
871     else /*!< Acquisition is OK or has NOISE */
872     {
873         if TEST_DELTA( <, FOR_DETECTIN_TH)
874         {
875 #if TOUCH_USE_PROX > 0
876             if (FOR_DELTA < FOR_PROXIN_TH)
877             {
878                 FOR_STATEID = TSC_STATEID_RELEASE;
879             }
880             else
881             {
882                 FOR_COUNTER_DEB = FOR_COUNTER_DEB_PROX;
883                 if (FOR_COUNTER_DEB)
884                 {
885                     FOR_STATEID = TSC_STATEID_DEB_PROX;
886                 }
887                 else
888                 {
889                     FOR_STATEID = TSC_STATEID_PROX;
890                     DTO_READ_TIME;
891                 }
892             }
893 #else
894             FOR_STATEID = TSC_STATEID_RELEASE;
895 #endif
896         }
897         else
898         {
899             TEST_DELTA_NEGATIVE;
900             if (FOR_COUNTER_DEB > 0)
901             {
902                 FOR_COUNTER_DEB--;
903             }
904             if (FOR_COUNTER_DEB == 0)
905             {
906                 FOR_STATEID = TSC_STATEID_DETECT;
907                 DTO_READ_TIME;
908             }
909         }
910     }
911 }
912 
913 /*!
914  * @brief       Detect state processing
915  *
916  * @param       None
917  *
918  * @retval      None
919  */
TSC_TouchKey_ProcessDetectState(void)920 void TSC_TouchKey_ProcessDetectState(void)
921 {
922 #if TOUCH_DTO > 0
923     TSC_tTick_sec_T tick_detected;
924 #endif
925     /* Acquisition error (min or max) */
926     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
927     {
928         FOR_COUNTER_DEB = FOR_COUNTER_DEB_ERROR;
929         if (FOR_COUNTER_DEB)
930         {
931             FOR_STATEID = TSC_STATEID_DEB_ERROR_DETECT;
932         }
933         else
934         {
935             FOR_STATEID = TSC_STATEID_ERROR;
936         }
937     }
938     else /*!< Acquisition is OK or has NOISE */
939     {
940         if TEST_DELTA( >, FOR_DETECTOUT_TH)
941         {
942             TEST_DELTA_NEGATIVE;
943 #if TOUCH_DTO > 0
944             /* Detection Time Out (DTO) processing */
945             if ((TSC_Params.DTO > 1) && (TSC_Params.DTO < 64))
946             {
947                 /* Read the detected time previously saved */
948                 tick_detected = FOR_COUNTER_DTO;
949                 /* Enter in calibration state if the DTO duration has elapsed */
950                 if (TSC_Time_Delay_sec(TSC_Params.DTO, &tick_detected) == TSC_STATUS_OK)
951                 {
952                     TSC_TouchKey_ConfigCalibrationState(0);
953                 }
954             }
955 #endif
956             return;
957         }
958 
959 #if TOUCH_USE_PROX > 0
960         if (FOR_DELTA > FOR_PROXOUT_TH)
961         {
962             FOR_COUNTER_DEB = FOR_COUNTER_DEB_PROX;
963             if (FOR_COUNTER_DEB)
964             {
965                 FOR_STATEID = TSC_STATEID_DEB_PROX_DETECT;
966             }
967             else
968             {
969                 FOR_STATEID = TSC_STATEID_PROX;
970                 DTO_READ_TIME;
971             }
972             return;
973         }
974 #endif
975 
976         FOR_COUNTER_DEB = FOR_COUNTER_DEB_RELEASE;
977         if (FOR_COUNTER_DEB)
978         {
979             FOR_STATEID = TSC_STATEID_DEB_RELEASE_DETECT;
980         }
981         else
982         {
983             FOR_STATEID = TSC_STATEID_RELEASE;
984         }
985     }
986 }
987 
988 /*!
989  * @brief       Touch state processing, Same as Detect state
990  *
991  * @param       None
992  *
993  * @retval      None
994  */
TSC_TouchKey_ProcessTouchState(void)995 void TSC_TouchKey_ProcessTouchState(void)
996 {
997 #if TOUCH_DTO > 0
998     TSC_tTick_sec_T tick_detected;
999 #endif
1000     /* Acquisition error (min or max) */
1001     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
1002     {
1003         FOR_COUNTER_DEB = FOR_COUNTER_DEB_ERROR;
1004         if (FOR_COUNTER_DEB)
1005         {
1006             FOR_STATEID = TSC_STATEID_DEB_ERROR_TOUCH;
1007         }
1008         else
1009         {
1010             FOR_STATEID = TSC_STATEID_ERROR;
1011         }
1012     }
1013     else /*!< Acquisition is OK or has NOISE */
1014     {
1015         if TEST_DELTA( >, FOR_DETECTOUT_TH)
1016         {
1017             TEST_DELTA_NEGATIVE;
1018 #if TOUCH_DTO > 0
1019             /* Detection Time Out (DTO) processing */
1020             if ((TSC_Params.DTO > 1) && (TSC_Params.DTO < 64))
1021             {
1022                 /* Read the detected time previously saved */
1023                 tick_detected = FOR_COUNTER_DTO;
1024                 /* Enter in calibration state if the DTO duration has elapsed */
1025                 if (TSC_Time_Delay_sec(TSC_Params.DTO, &tick_detected) == TSC_STATUS_OK)
1026                 {
1027                     TSC_TouchKey_ConfigCalibrationState(0);
1028                 }
1029             }
1030 #endif
1031             return;
1032         }
1033 
1034 #if TOUCH_USE_PROX > 0
1035         if (FOR_DELTA > FOR_PROXOUT_TH)
1036         {
1037             FOR_COUNTER_DEB = FOR_COUNTER_DEB_PROX;
1038             if (FOR_COUNTER_DEB)
1039             {
1040                 FOR_STATEID = TSC_STATEID_DEB_PROX_TOUCH;
1041             }
1042             else
1043             {
1044                 FOR_STATEID = TSC_STATEID_PROX;
1045                 DTO_READ_TIME;
1046             }
1047             return;
1048         }
1049 #endif
1050 
1051         FOR_COUNTER_DEB = FOR_COUNTER_DEB_RELEASE;
1052         if (FOR_COUNTER_DEB)
1053         {
1054             FOR_STATEID = TSC_STATEID_DEB_RELEASE_TOUCH;
1055         }
1056         else
1057         {
1058             FOR_STATEID = TSC_STATEID_RELEASE;
1059         }
1060     }
1061 }
1062 
1063 /*!
1064  * @brief       Debounce error state processing
1065  *
1066  * @param       None
1067  *
1068  * @retval      None
1069  */
TSC_TouchKey_ProcessDebErrorState(void)1070 void TSC_TouchKey_ProcessDebErrorState(void)
1071 {
1072     volatile TSC_STATEMASK_T mask;
1073 
1074     /* Acquisition error (min or max) */
1075     if (FOR_ACQ_STATUS & TSC_ACQ_STATUS_ERROR_MASK)
1076     {
1077         if (FOR_COUNTER_DEB > 0)
1078         {
1079             FOR_COUNTER_DEB--;
1080         }
1081         if (FOR_COUNTER_DEB == 0)
1082         {
1083             FOR_STATEID = TSC_STATEID_ERROR;
1084         }
1085     }
1086     else /*!< Acquisition is OK or has NOISE */
1087     {
1088         mask = TSC_TouchKey_ReadStateMask();
1089         mask &= (TSC_STATEMASK_T)(~(TSC_STATE_DEBOUNCE_BIT_MASK | TSC_STATE_ERROR_BIT_MASK));
1090 
1091         if (mask == TSC_STATEMASK_RELEASE)
1092         {
1093             FOR_STATEID = TSC_STATEID_RELEASE;
1094         }
1095         else if (mask == TSC_STATEMASK_PROX)
1096         {
1097             FOR_STATEID = TSC_STATEID_PROX;
1098         }
1099         else if (mask == TSC_STATEMASK_DETECT)
1100         {
1101             FOR_STATEID = TSC_STATEID_DETECT;
1102         }
1103         else if (mask == TSC_STATEMASK_TOUCH)
1104         {
1105             FOR_STATEID = TSC_STATEID_TOUCH;
1106         }
1107         else
1108         {
1109             TSC_TouchKey_ConfigCalibrationState(0);
1110         }
1111     }
1112 }
1113 
1114 /*!
1115  * @brief       Read the current time in second and affect it to the DTO counter (Private)
1116  *
1117  * @param       None
1118  *
1119  * @retval      None
1120  */
TSC_TouchKey_DTOGetTime(void)1121 void TSC_TouchKey_DTOGetTime(void)
1122 {
1123     disableInterrupts();
1124     FOR_COUNTER_DTO = (TSC_tCounter_T)TSC_Globals.Tick_sec;
1125     enableInterrupts();
1126 }
1127 #endif /*!< #if TOUCH_TOTAL_KEYS > 0 */
1128 
1129 /**@} end of group TSC_TouchKey_Functions */
1130 /**@} end of group TSC_TouchKey_Driver */
1131 /**@} end of group TSC_Driver_Library */
1132