forked from Infineon/capsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cy_capsense_filter.c
1102 lines (991 loc) · 40.1 KB
/
cy_capsense_filter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************//**
* \file cy_capsense_filter.c
* \version 2.0
*
* \brief
* This file contains the source code of all filters implementation.
*
********************************************************************************
* \copyright
* Copyright 2018-2019, Cypress Semiconductor Corporation. All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/
#include <stddef.h>
#include "cy_capsense_filter.h"
#include "cy_capsense_common.h"
#include "cy_capsense_lib.h"
#if defined(CY_IP_MXCSDV2)
/*******************************************************************************
* Local definition
*******************************************************************************/
#define CY_CAPSENSE_IIR_BL_SHIFT (8u)
#define CY_CAPSENSE_IIR_SHIFT_STANDARD (0u)
#define CY_CAPSENSE_IIR_SHIFT_PERFORMANCE (8u)
#define CY_CAPSENSE_RC_MEDIAN_SIZE (2u)
#define CY_CAPSENSE_RC_IIR_SIZE (1u)
#define CY_CAPSENSE_RC_AVG_SIZE (3u)
/* IIR filter constant */
#define CY_CAPSENSE_IIR_COEFFICIENT_K (256u)
/*******************************************************************************
* Function Name: Cy_CapSense_UpdateAllBaselines
****************************************************************************//**
*
* Updates the baseline for all the sensors in all the widgets.
*
* Baselines must be updated after sensor scan to ignore low frequency
* changes in the sensor data caused by environment changes such as
* temperature from sensor status decision.
*
* This function ignores the widget enable bit in the widget status register.
* Calling this function multiple times without a new sensor scan leads to
* unexpected behavior and should be avoided.
*
* This function is called by Cy_CapSense_ProcessAllWidgets() and
* Cy_CapSense_ProcessWidget(), hence the application program need not use this
* function if any of the above functions is already used. This function can be
* used for custom application implementation.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the update baseline operation of all the widgets:
* - CY_RET_SUCCESS - The operation is successfully completed.
* - CY_RET_BAD_DATA - The baseline processing failed.
*
*******************************************************************************/
cy_status Cy_CapSense_UpdateAllBaselines(const cy_stc_capsense_context_t * context)
{
uint32_t wdIndex;
cy_status bslnStatus = CY_RET_SUCCESS;
for(wdIndex = context->ptrCommonConfig->numWd; wdIndex-- > 0u;)
{
bslnStatus |= Cy_CapSense_UpdateWidgetBaseline(wdIndex, context);
}
return(bslnStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_UpdateWidgetBaseline
****************************************************************************//**
*
* Updates the baselines for all the sensors in a widget specified by
* the input parameter.
*
* This function performs exactly the same tasks as
* Cy_CapSense_UpdateAllBaselines() but only for a specified widget.
*
* Calling this function multiple times without a new sensor scan leads to
* unexpected behavior and should be avoided. The application program need
* not use this function if the Cy_CapSense_UpdateAllBaselines(),
* Cy_CapSense_ProcessAllWidgets() or Cy_CapSense_ProcessWidget() functions
* are already used.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the specified widget update baseline operation:
* - CY_RET_SUCCESS - The operation is successfully completed.
* - CY_RET_BAD_DATA - The baseline processing failed.
*
*******************************************************************************/
cy_status Cy_CapSense_UpdateWidgetBaseline(
uint32_t widgetId,
const cy_stc_capsense_context_t * context)
{
uint32_t snsIndex;
cy_status bslnStatus = CY_RET_SUCCESS;
for(snsIndex = context->ptrWdConfig[widgetId].numSns; snsIndex-- > 0u;)
{
bslnStatus |= Cy_CapSense_UpdateSensorBaseline(widgetId, snsIndex, context);
}
return(bslnStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_UpdateSensorBaseline
****************************************************************************//**
*
* Updates the baseline for a sensor in a widget specified by the
* input parameters.
*
* This function performs exactly the same tasks as
* Cy_CapSense_UpdateAllBaselines() and Cy_CapSense_UpdateWidgetBaseline()
* but only for a specified sensor.
*
* Calling this function multiple times without a new sensor scan leads to
* unexpected behavior and should be avoided. The application need not use
* this function if the Cy_CapSense_UpdateWidgetBaseline (),
* Cy_CapSense_UpdateAllBaselines (), Cy_CapSense_ProcessAllWidgets(),
* or Cy_CapSense_ProcessWidget() functions are already used.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param sensorId
* Specifies the ID number of the sensor within the widget. A macro for the
* sensor ID within a specified widget can be found in the cycfg_capsense.h
* file defined as CY_CAPSENSE_<WIDGET_NAME>_SNS<SENSOR_NUMBER>_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the specified sensor update baseline operation:
* - CY_RET_SUCCESS - The operation is successfully completed.
* - CY_RET_BAD_DATA - The baseline processing failed.
*
*******************************************************************************/
cy_status Cy_CapSense_UpdateSensorBaseline(
uint32_t widgetId,
uint32_t sensorId,
const cy_stc_capsense_context_t * context)
{
uint32_t result;
const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
result = Cy_CapSense_FtUpdateBaseline(ptrWdCfg->ptrWdContext, &ptrWdCfg->ptrSnsContext[sensorId], context);
return (result);
}
/*******************************************************************************
* Function Name: Cy_CapSense_FtUpdateBaseline
****************************************************************************//**
*
* Updates the baseline for a sensor specified by an input parameter.
*
* Check a matching of present baseline and its inverse duplication. If they
* match, then the function updates the baseline for a sensor specified by an input parameter.
* If they don't match, the function returns CY_CAPSENSE_TST_BSLN_DUPLICATION
* result and doesn't update the baseline.
*
* \param ptrWdContext
* The pointer to the widget context structure where all the widget parameters
* are stored.
*
* \param ptrSnsContext
* The pointer to the sensor context structure where the sensor data
* is stored.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns a status indicating whether the baseline has been updated:
* - Zero - if baseline updating was successful.
* - Non-zero - if present sensor's any channel baseline and its inversion
* doesn't match.
*
*******************************************************************************/
uint32_t Cy_CapSense_FtUpdateBaseline(
const cy_stc_capsense_widget_context_t * ptrWdContext,
cy_stc_capsense_sensor_context_t * ptrSnsContext,
const cy_stc_capsense_context_t * context)
{
uint32_t result = CY_RET_SUCCESS;
uint32_t bslnHistory;
/* Reset negative baseline counter */
if(ptrSnsContext->raw >= ptrSnsContext->bsln)
{
ptrSnsContext->negBslnRstCnt = 0u;
}
/* Reset baseline */
if (ptrSnsContext->bsln > (ptrWdContext->nNoiseTh + ptrSnsContext->raw))
{
if (ptrSnsContext->negBslnRstCnt >= ptrWdContext->lowBslnRst)
{
Cy_CapSense_FtInitializeBaseline(ptrSnsContext);
}
else
{
ptrSnsContext->negBslnRstCnt++;
}
}
else
{
/*
* Update baseline only if:
* - signal is in range between noiseThreshold and negativenoiseThreshold
* or
* - sensor Auto-reset is enabled
*/
if ((0u != context->ptrCommonConfig->swSensorAutoResetEn) ||
(ptrSnsContext->raw <= (ptrWdContext->noiseTh + ptrSnsContext->bsln)))
{
/* Get real baseline */
bslnHistory = ((uint32_t)ptrSnsContext->bsln << CY_CAPSENSE_IIR_BL_SHIFT) | ptrSnsContext->bslnExt;
/* Calculate baseline value */
bslnHistory = Cy_CapSense_FtIIR1stOrder((uint32_t)ptrSnsContext->raw << CY_CAPSENSE_IIR_BL_SHIFT, bslnHistory, (uint32_t)ptrWdContext->bslnCoeff);
/* Split baseline */
ptrSnsContext->bsln = CY_LO16(bslnHistory >> CY_CAPSENSE_IIR_BL_SHIFT);
ptrSnsContext->bslnExt = CY_LO8(bslnHistory);
}
}
return result;
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeAllBaselines
****************************************************************************//**
*
* Initializes the baselines of all the sensors of all the widgets.
*
* This function initializes baselines for all sensors and widgets in the project.
* It can also be used to re-initialize baselines at any time, however, note
* that all sensor data history information and sensor status shall be reset
* along with re-initialization of baseline.
*
* Following functions to initialize sensor and widgets and filter history
* should be called after initializing baseline for proper operation of
* the CapSense middleware:
* * Cy_CapSense_InitializeAllStatuses()
* * Cy_CapSense_InitializeAllFilters()
*
* These functions are called by the CapSense_Enable() function, hence it is
* not required to use this function if above function is used.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeAllBaselines(cy_stc_capsense_context_t * context)
{
uint32_t wdIndex;
for(wdIndex = context->ptrCommonConfig->numWd; wdIndex-- > 0u;)
{
Cy_CapSense_InitializeWidgetBaseline(wdIndex, context);
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeWidgetBaseline
****************************************************************************//**
*
* Initializes the baselines of all the sensors in a specific widget.
*
* This function initializes baselines for all sensors in a specific widget
* in the project. It can also be used to re-initialize baselines at any time,
* however, note that all sensor data history information and sensor status
* should be reset along with re-initialization of baseline.
*
* The following functions to initialize sensor and widgets and filter history
* should be called after initializing baselines for proper operation of
* middleware.
* * Cy_CapSense_InitializeWidgetStatus()
* * Cy_CapSense_InitializeWidgetFilter()
*
* These functions are called by CapSense_Enable() function, hence it is not
* required to use this function is above function is used.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeWidgetBaseline(
uint32_t widgetId,
cy_stc_capsense_context_t * context)
{
uint32_t snsIndex;
for(snsIndex = context->ptrWdConfig[widgetId].numSns; snsIndex-- > 0u;)
{
Cy_CapSense_InitializeSensorBaseline(widgetId, snsIndex, context);
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeSensorBaseline
****************************************************************************//**
*
* Initializes the baseline of a sensor in a widget specified
* by the input parameters.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param sensorId
* Specifies the ID number of the sensor within the widget. A macro for the
* sensor ID within a specified widget can be found in the cycfg_capsense.h
* file defined as CY_CAPSENSE_<WIDGET_NAME>_SNS<SENSOR_NUMBER>_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeSensorBaseline(
uint32_t widgetId,
uint32_t sensorId,
cy_stc_capsense_context_t * context)
{
uint32_t freqChIndex;
uint32_t freqChNumber;
uint32_t cxtOffset;
freqChNumber = (CY_CAPSENSE_ENABLE == context->ptrCommonConfig->mfsEn) ? 3u : 1u;
for(freqChIndex = 0u; freqChIndex < freqChNumber; freqChIndex++)
{
cxtOffset = sensorId + (freqChIndex * context->ptrCommonConfig->numSns);
Cy_CapSense_FtInitializeBaseline(&context->ptrWdConfig[widgetId].ptrSnsContext[cxtOffset]);
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_FtInitializeBaseline
****************************************************************************//**
*
* Initializes the baseline history for a sensor indicated by an input
* parameter.
*
* \param ptrSnsContext
* The pointer to the sensor context structure.
*
*******************************************************************************/
void Cy_CapSense_FtInitializeBaseline(cy_stc_capsense_sensor_context_t * ptrSnsContext)
{
ptrSnsContext->bslnExt = 0u;
ptrSnsContext->bsln = ptrSnsContext->raw;
ptrSnsContext->negBslnRstCnt = 0u;
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeAllFilters
****************************************************************************//**
*
* Initializes (or re-initializes) all the firmware filter history, except
* the baseline.
*
* Calling this function is accompanied by
* * Cy_CapSense_InitializeAllStatuses()
* * Cy_CapSense_InitializeAllBaselines()
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeAllFilters(const cy_stc_capsense_context_t * context)
{
uint32_t widgetId;
for(widgetId = context->ptrCommonConfig->numWd; widgetId-- > 0u;)
{
Cy_CapSense_InitializeWidgetFilter(widgetId, context);
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeWidgetFilter
****************************************************************************//**
*
* Initializes (or re-initializes) the raw count filter history of all
* the sensors in a widget specified by the input parameter.
*
* Calling this function is accompanied by
* - Cy_CapSense_InitializeWidgetStatus().
* - Cy_CapSense_InitializeWidgetBaseline().
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeWidgetFilter(
uint32_t widgetId,
const cy_stc_capsense_context_t * context)
{
uint32_t snsIndex;
uint32_t freqChIndex;
uint32_t freqChNumber;
uint32_t snsHistorySize;
uint32_t cxtOffset;
uint32_t historyOffset;
const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
uint32_t rawFilterCfg = ptrWdCfg->rawFilterConfig;
uint8_t * ptrHistoryLow = NULL;
uint16_t * ptrHistory;
cy_stc_capsense_sensor_context_t * ptrSnsCtx ;
cy_stc_capsense_smartsense_csd_noise_envelope_t * ptrNEHistory;
freqChNumber = (CY_CAPSENSE_ENABLE == context->ptrCommonConfig->mfsEn) ? 3u : 1u;
snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
for(freqChIndex = 0u; freqChIndex < freqChNumber; freqChIndex++)
{
for(snsIndex = 0u; snsIndex < ptrWdCfg->numSns; snsIndex++)
{
cxtOffset = snsIndex + (freqChIndex * context->ptrCommonConfig->numSns);
historyOffset = snsHistorySize * (snsIndex + (freqChIndex * context->ptrCommonConfig->numSns));
ptrSnsCtx = &ptrWdCfg->ptrSnsContext[cxtOffset];
ptrHistory = &ptrWdCfg->ptrRawFilterHistory[historyOffset];
if(CY_CAPSENSE_IIR_FILTER_PERFORMANCE == (ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_IIR_MODE_MASK))
{
ptrHistoryLow = &ptrWdCfg->ptrRawFilterHistoryLow[cxtOffset];
}
if(0u != (CY_CAPSENSE_RC_FILTER_MEDIAN_EN_MASK & rawFilterCfg))
{
Cy_CapSense_InitializeMedianInternal(ptrWdCfg, ptrSnsCtx, ptrHistory);
ptrHistory += CY_CAPSENSE_RC_MEDIAN_SIZE;
}
if(0u != (CY_CAPSENSE_RC_FILTER_IIR_EN_MASK & rawFilterCfg))
{
Cy_CapSense_InitializeIIRInternal(ptrWdCfg, ptrSnsCtx, ptrHistory, ptrHistoryLow);
ptrHistory += CY_CAPSENSE_RC_IIR_SIZE;
}
if(0u != (CY_CAPSENSE_RC_FILTER_AVERAGE_EN_MASK & rawFilterCfg))
{
Cy_CapSense_InitializeAverageInternal(ptrWdCfg, ptrSnsCtx, ptrHistory);
}
}
}
if (CY_CAPSENSE_CSD_SS_HWTH_EN == context->ptrCommonConfig->csdAutotuneEn)
{
/* Noise envelope is available for CSD widgets only */
if ((uint8_t)CY_CAPSENSE_SENSE_METHOD_CSD_E == ptrWdCfg->senseMethod)
{
ptrSnsCtx = ptrWdCfg->ptrSnsContext;
ptrNEHistory = ptrWdCfg->ptrNoiseEnvelope;
for(snsIndex = 0u; snsIndex < ptrWdCfg->numSns; snsIndex++)
{
Cy_CapSense_InitializeNoiseEnvelope_Lib_Call(ptrSnsCtx->raw,
ptrWdCfg->ptrWdContext->sigPFC,
ptrNEHistory,
context);
ptrSnsCtx++;
ptrNEHistory++;
}
}
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeIIR
****************************************************************************//**
*
* Initializes the IIR filter history.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param sensorId
* Specifies the ID number of the sensor within the widget. A macro for the
* sensor ID within a specified widget can be found in the cycfg_capsense.h
* file defined as CY_CAPSENSE_<WIDGET_NAME>_SNS<SENSOR_NUMBER>_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeIIR(
uint32_t widgetId,
uint32_t sensorId,
const cy_stc_capsense_context_t * context)
{
uint32_t snsHistorySize;
const cy_stc_capsense_widget_config_t * ptrWdCfg;
const cy_stc_capsense_sensor_context_t * ptrSnsCtx;
ptrWdCfg = &context->ptrWdConfig[widgetId];
ptrSnsCtx = &ptrWdCfg->ptrSnsContext[sensorId];
snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
Cy_CapSense_InitializeIIRInternal(ptrWdCfg,
ptrSnsCtx,
&ptrWdCfg->ptrRawFilterHistory[(sensorId * snsHistorySize) + 2uL],
&ptrWdCfg->ptrRawFilterHistoryLow[sensorId]);
}
/*******************************************************************************
* Function Name: Cy_CapSense_RunIIR
****************************************************************************//**
*
* Executes the IIR filter algorithm on a sensor indicated by an input
* parameter.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param sensorId
* Specifies the ID number of the sensor within the widget. A macro for the
* sensor ID within a specified widget can be found in the cycfg_capsense.h
* file defined as CY_CAPSENSE_<WIDGET_NAME>_SNS<SENSOR_NUMBER>_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_RunIIR(
uint32_t widgetId,
uint32_t sensorId,
const cy_stc_capsense_context_t * context)
{
uint32_t snsHistorySize;
const cy_stc_capsense_widget_config_t * ptrWdCfg;
cy_stc_capsense_sensor_context_t * ptrSnsCtx;
ptrWdCfg = &context->ptrWdConfig[widgetId];
ptrSnsCtx = &ptrWdCfg->ptrSnsContext[sensorId];
snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
Cy_CapSense_RunIIRInternal(ptrWdCfg,
ptrSnsCtx,
&ptrWdCfg->ptrRawFilterHistory[(sensorId * snsHistorySize) + 2uL],
&ptrWdCfg->ptrRawFilterHistoryLow[sensorId]);
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeIIRInternal
****************************************************************************//**
*
* Initializes the IIR filter history.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
* \param ptrSnsContext
* The pointer to the sensor context structure.
*
* \param ptrSnsRawHistory
* The pointer to the filter history of the sensor.
*
* \param ptrSnsRawHistoryLow
* The pointer to the extended filter history of the sensor.
*
*******************************************************************************/
void Cy_CapSense_InitializeIIRInternal(
const cy_stc_capsense_widget_config_t * ptrWdConfig,
const cy_stc_capsense_sensor_context_t * ptrSnsContext,
uint16_t * ptrSnsRawHistory,
uint8_t * ptrSnsRawHistoryLow)
{
ptrSnsRawHistory[0u] = ptrSnsContext->raw;
if(CY_CAPSENSE_IIR_FILTER_PERFORMANCE == (ptrWdConfig->rawFilterConfig & CY_CAPSENSE_RC_FILTER_IIR_MODE_MASK))
{
ptrSnsRawHistoryLow[0u] = 0u;
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_RunIIRInternal
****************************************************************************//**
*
* Runs the IIR filter.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
* \param ptrSnsContext
* The pointer to the sensor context structure.
*
* \param ptrSnsRawHistory
* The pointer to the filter history of the sensor.
*
* \param ptrSnsRawHistoryLow
* The pointer to the extended filter history of the sensor.
*
*******************************************************************************/
void Cy_CapSense_RunIIRInternal(
const cy_stc_capsense_widget_config_t * ptrWdConfig,
cy_stc_capsense_sensor_context_t * ptrSnsContext,
uint16_t * ptrSnsRawHistory,
uint8_t * ptrSnsRawHistoryLow)
{
uint32_t tempHistory;
uint32_t tempRaw;
if(CY_CAPSENSE_IIR_FILTER_PERFORMANCE == (ptrWdConfig->rawFilterConfig & CY_CAPSENSE_RC_FILTER_IIR_MODE_MASK))
{
tempRaw = ((uint32_t)ptrSnsContext->raw) << CY_CAPSENSE_IIR_SHIFT_PERFORMANCE;
tempHistory = ((uint32_t)(*ptrSnsRawHistory)) << CY_CAPSENSE_IIR_SHIFT_PERFORMANCE;
tempHistory |= (uint32_t)(*ptrSnsRawHistoryLow);
tempRaw = Cy_CapSense_FtIIR1stOrder(tempRaw, tempHistory, ptrWdConfig->iirCoeff);
*ptrSnsRawHistory = CY_LO16(tempRaw >> CY_CAPSENSE_IIR_SHIFT_PERFORMANCE);
*ptrSnsRawHistoryLow = CY_LO8(tempRaw);
ptrSnsContext->raw = CY_LO16(tempRaw >> CY_CAPSENSE_IIR_SHIFT_PERFORMANCE);
}
else
{
tempRaw = Cy_CapSense_FtIIR1stOrder((uint32_t)ptrSnsContext->raw, (uint32_t)(*ptrSnsRawHistory), ptrWdConfig->iirCoeff);
*ptrSnsRawHistory = CY_LO16(tempRaw);
ptrSnsContext->raw = *ptrSnsRawHistory;
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeMedian
****************************************************************************//**
*
* Initializes the median filter history.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param sensorId
* Specifies the ID number of the sensor within the widget. A macro for the
* sensor ID within a specified widget can be found in the cycfg_capsense.h
* file defined as CY_CAPSENSE_<WIDGET_NAME>_SNS<SENSOR_NUMBER>_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeMedian(
uint32_t widgetId,
uint32_t sensorId,
const cy_stc_capsense_context_t * context)
{
uint32_t snsHistorySize;
const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
cy_stc_capsense_sensor_context_t * ptrSnsCtx = &ptrWdCfg->ptrSnsContext[sensorId];
snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
Cy_CapSense_InitializeMedianInternal(ptrWdCfg,
ptrSnsCtx,
&ptrWdCfg->ptrRawFilterHistory[sensorId * snsHistorySize]);
}
/*******************************************************************************
* Function Name: Cy_CapSense_RunMedian
****************************************************************************//**
*
* Executes the median filter algorithm on a sensor raw count indicated by
* the input parameters.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param sensorId
* Specifies the ID number of the sensor within the widget. A macro for the
* sensor ID within a specified widget can be found in the cycfg_capsense.h
* file defined as CY_CAPSENSE_<WIDGET_NAME>_SNS<SENSOR_NUMBER>_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_RunMedian(
uint32_t widgetId,
uint32_t sensorId,
const cy_stc_capsense_context_t * context)
{
uint32_t snsHistorySize;
const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
cy_stc_capsense_sensor_context_t * ptrSnsCtx = &ptrWdCfg->ptrSnsContext[sensorId];
snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
Cy_CapSense_RunMedianInternal(ptrWdCfg,
ptrSnsCtx,
&ptrWdCfg->ptrRawFilterHistory[sensorId * snsHistorySize]);
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeMedianInternal
****************************************************************************//**
*
* Initializes the median filter.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
* \param ptrSnsContext
* The pointer to the sensor context structure.
*
* \param ptrSnsRawHistory
* The pointer to the filter history of the sensor.
*
*******************************************************************************/
void Cy_CapSense_InitializeMedianInternal(
const cy_stc_capsense_widget_config_t * ptrWdConfig,
const cy_stc_capsense_sensor_context_t * ptrSnsContext,
uint16_t * ptrSnsRawHistory)
{
(void)ptrWdConfig;
ptrSnsRawHistory[0u] = ptrSnsContext->raw;
ptrSnsRawHistory[1u] = ptrSnsContext->raw;
}
/*******************************************************************************
* Function Name: Cy_CapSense_RunMedianInternal
****************************************************************************//**
*
* Runs the median filter.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
* \param ptrSnsContext
* The pointer to the sensor context structure.
*
* \param ptrSnsRawHistory
* The pointer to the filter history of the sensor.
*
*******************************************************************************/
void Cy_CapSense_RunMedianInternal(
const cy_stc_capsense_widget_config_t * ptrWdConfig,
cy_stc_capsense_sensor_context_t * ptrSnsContext,
uint16_t * ptrSnsRawHistory)
{
uint32_t temp = Cy_CapSense_FtMedian(
(uint32_t)ptrSnsContext->raw,
(uint32_t)ptrSnsRawHistory[0u],
(uint32_t)ptrSnsRawHistory[1u]);
(void)ptrWdConfig;
ptrSnsRawHistory[1u] = ptrSnsRawHistory[0u];
ptrSnsRawHistory[0u] = ptrSnsContext->raw;
ptrSnsContext->raw = CY_LO16(temp);
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeAverage
****************************************************************************//**
*
* Initializes the average filter history.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param sensorId
* Specifies the ID number of the sensor within the widget. A macro for the
* sensor ID within a specified widget can be found in the cycfg_capsense.h
* file defined as CY_CAPSENSE_<WIDGET_NAME>_SNS<SENSOR_NUMBER>_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeAverage(
uint32_t widgetId,
uint32_t sensorId,
const cy_stc_capsense_context_t * context)
{
uint32_t snsHistorySize;
const cy_stc_capsense_widget_config_t * ptrWdCfg;
const cy_stc_capsense_sensor_context_t * ptrSnsCtx;
ptrWdCfg = &context->ptrWdConfig[widgetId];
ptrSnsCtx = &ptrWdCfg->ptrSnsContext[sensorId];
snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
Cy_CapSense_InitializeAverageInternal(
ptrWdCfg,
ptrSnsCtx,
&ptrWdCfg->ptrRawFilterHistory[(sensorId * snsHistorySize) + 2u + 1u]);
}
/*******************************************************************************
* Function Name: Cy_CapSense_RunAverage
****************************************************************************//**
*
* Executes the average filter algorithm on a sensor indicated by an input
* parameter.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param sensorId
* Specifies the ID number of the sensor within the widget. A macro for the
* sensor ID within a specified widget can be found in the cycfg_capsense.h
* file defined as CY_CAPSENSE_<WIDGET_NAME>_SNS<SENSOR_NUMBER>_ID.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_RunAverage(
uint32_t widgetId,
uint32_t sensorId,
const cy_stc_capsense_context_t * context)
{
uint32_t snsHistorySize;
const cy_stc_capsense_widget_config_t * ptrWdCfg;
cy_stc_capsense_sensor_context_t * ptrSnsCtx;
ptrWdCfg = &context->ptrWdConfig[widgetId];
ptrSnsCtx = &ptrWdCfg->ptrSnsContext[sensorId];
snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
Cy_CapSense_RunAverageInternal(
ptrWdCfg,
ptrSnsCtx,
&ptrWdCfg->ptrRawFilterHistory[(sensorId * snsHistorySize) + 2u + 1u]);
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeAverageInternal
****************************************************************************//**
*
* Initializes the average filter.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
* \param ptrSnsContext
* The pointer to the sensor context structure.
*
* \param ptrSnsRawHistory
* The pointer to the filter history of the sensor.
*
*******************************************************************************/
void Cy_CapSense_InitializeAverageInternal(
const cy_stc_capsense_widget_config_t * ptrWdConfig,
const cy_stc_capsense_sensor_context_t * ptrSnsContext,
uint16_t * ptrSnsRawHistory)
{
(void)ptrWdConfig;
ptrSnsRawHistory[0u] = ptrSnsContext->raw;
ptrSnsRawHistory[1u] = ptrSnsContext->raw;
ptrSnsRawHistory[2u] = ptrSnsContext->raw;
}
/*******************************************************************************
* Function Name: Cy_CapSense_RunAverageInternal
****************************************************************************//**
*
* Runs the average filter.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
* \param ptrSnsContext
* The pointer to the sensor context structure.
*
* \param ptrSnsRawHistory
* The pointer to the filter history of the sensor.
*
*******************************************************************************/
void Cy_CapSense_RunAverageInternal(
const cy_stc_capsense_widget_config_t * ptrWdConfig,
cy_stc_capsense_sensor_context_t * ptrSnsContext,
uint16_t * ptrSnsRawHistory)
{
uint32_t temp;
if(CY_CAPSENSE_AVERAGE_FILTER_LEN_4 == (ptrWdConfig->rawFilterConfig & CY_CAPSENSE_RC_FILTER_AVERAGE_MODE_MASK))
{
temp = ((uint32_t)ptrSnsContext->raw + ptrSnsRawHistory[0u] + ptrSnsRawHistory[1u] + ptrSnsRawHistory[2u]) >> 2u;
ptrSnsRawHistory[2u] = ptrSnsRawHistory[1u];
ptrSnsRawHistory[1u] = ptrSnsRawHistory[0u];
}
else
{
temp = ((uint32_t)ptrSnsContext->raw + ptrSnsRawHistory[0u]) >> 1u;
}
ptrSnsRawHistory[0u] = ptrSnsContext->raw;
ptrSnsContext->raw = CY_LO16(temp);
}
/*******************************************************************************
* Function Name: Cy_CapSense_FtRunEnabledFiltersInternal
****************************************************************************//**
*
* Runs all enabled filters.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
* \param ptrSnsContext
* The pointer to the sensor context structure.
*
* \param ptrSnsRawHistory
* The pointer to the filter history of the sensor.
*
* \param ptrSnsRawHistoryLow
* The pointer to the extended filter history of the sensor.
*
*******************************************************************************/
void Cy_CapSense_FtRunEnabledFiltersInternal(
const cy_stc_capsense_widget_config_t * ptrWdConfig,
cy_stc_capsense_sensor_context_t * ptrSnsContext,
uint16_t * ptrSnsRawHistory,
uint8_t * ptrSnsRawHistoryLow)
{
uint16_t rawFilterCfg;
rawFilterCfg = ptrWdConfig->rawFilterConfig;
if(0u != (CY_CAPSENSE_RC_FILTER_MEDIAN_EN_MASK & rawFilterCfg))
{
Cy_CapSense_RunMedianInternal(ptrWdConfig, ptrSnsContext, ptrSnsRawHistory);
ptrSnsRawHistory += CY_CAPSENSE_RC_MEDIAN_SIZE;
}
if(0u != (CY_CAPSENSE_RC_FILTER_IIR_EN_MASK & rawFilterCfg))
{
Cy_CapSense_RunIIRInternal(ptrWdConfig, ptrSnsContext, ptrSnsRawHistory, ptrSnsRawHistoryLow);
ptrSnsRawHistory += CY_CAPSENSE_RC_IIR_SIZE;
}
if(0u != (CY_CAPSENSE_RC_FILTER_AVERAGE_EN_MASK & rawFilterCfg))
{
Cy_CapSense_RunAverageInternal(ptrWdConfig, ptrSnsContext, ptrSnsRawHistory);
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_FtMedian
****************************************************************************//**