-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcy_capsense_control.c
1470 lines (1346 loc) · 59 KB
/
cy_capsense_control.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_control.c
* \version 3.0
*
* \brief
* This file provides the source code to the Control module functions.
*
********************************************************************************
* \copyright
* Copyright 2018-2020, 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_syslib.h"
#include "cy_syspm.h"
#include "cy_capsense_common.h"
#include "cy_capsense_structure.h"
#include "cy_capsense_control.h"
#include "cy_capsense_processing.h"
#include "cy_capsense_filter.h"
#include "cy_capsense_tuner.h"
#include "cy_capsense_selftest.h"
#if (CY_CAPSENSE_PLATFORM_BLOCK_CSDV2)
#include "cy_capsense_sensing_v2.h"
#include "cy_csd.h"
#else /* (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3) */
#include "cy_capsense_sensing_v3.h"
#include "cy_msc.h"
#endif
#if (defined(CY_IP_MXCSDV2) || defined(CY_IP_M0S8CSDV2) || defined(CY_IP_M0S8MSCV3))
/*******************************************************************************
* Local definition
*******************************************************************************/
#define CY_CAPSENSE_PUMP_VDDA_VOLTAGE_MV (3800u)
/*******************************************************************************
* Function Name: Cy_CapSense_Init
****************************************************************************//**
*
* Captures HW blocks (one or more) for CapSense operations and configures them
* to the default state. Call this function with the application program
* prior to calling any other function of the middleware.
*
* The following tasks are executed:
* 1. Capturing not used HW blocks. If any
* of HW block is
* already in use, then the function returns the fail status, and
* the application program should perform corresponding actions. For example, releasing
* the HW block captured by another middleware.
* 2. If the HW block has been captured successfully, this function configures it
* to the default state.
*
* After the middleware is configured using the Cy_CapSense_Init() function,
* the application program configures and enables the HW block interrupt(s),
* and then call of the Cy_CapSense_Enable() function to complete the
* middleware initialization process.
* See the function usage example below for more details.
*
* When the middleware operation is stopped by the Cy_CapSense_DeInit()
* function, subsequent call of the Cy_CapSense_Init() function repeats
* initialization process and it is not needed to call the Cy_CapSense_Enable()
* function second time. However, to implement time-multiplexed mode
* (sharing the HW block(s) between multiple middleware)
* the Cy_CapSense_Save() and Cy_CapSense_Restore() functions should be used
* instead of the Cy_CapSense_DeInit() and Cy_CapSense_Init() functions for
* further compatibility.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t
* generated by the CapSense Configurator tool. The structure contains both, CapSense
* configuration and internal data and it is used during whole CapSense operation.
*
* \return
* Returns the status of the initialization process. If CY_CAPSENSE_STATUS_SUCCESS is not
* received, some of the initialization fails, the middleware may not operate
* as expected, and repeating of initialization is required.
*
* \funcusage
*
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_Initialization
* The 'cy_capsense_context' variable that is used as the parameter of the
* Cy_CapSense_Init() and Cy_CapSense_Enable() functions is declared in the
* cycfg_capsense.h file.
*
* The CapSense_ISR_cfg variable should be declared by the application
* program according to the examples below:<br>
* For PSoC4 CPU or for PSoC6 CM0+ core:
* \snippet capsense/snippet/main.c snippet_m0p_capsense_interrupt_source_declaration
*
* The CapSense interrupt handler should be declared by the application program
* according to the example below:
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_IntHandler
*
* The CY_MSC<X>_HW is the pointer to the base register address of
* the MSC_X HW block. A macro for the pointer is in the cycfg_peripherals.h
* file defined as \<Msc_Personality_Name\>_HW. If no name is specified,
* the default msc_\<Block_Number\>_msc_\<Block_Number\>_HW is used.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_Init(cy_stc_capsense_context_t * context)
{
cy_capsense_status_t result = CY_CAPSENSE_STATUS_BAD_PARAM;
if (NULL != context)
{
result = Cy_CapSense_Restore(context);
/* The time interval is required for settling analog part of the HW block. */
Cy_SysLib_DelayUs(CY_CAPSENSE_ANALOG_SETTLING_TIME_US);
#if (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3)
context->ptrInternalContext->ptrSSCallback = NULL;
context->ptrInternalContext->ptrEOSCallback = NULL;
context->ptrInternalContext->ptrTunerReceiveCallback = NULL;
context->ptrInternalContext->ptrTunerSendCallback = NULL;
context->ptrInternalContext->ptrEODsInitCallback = NULL;
#endif
}
return result;
}
/*******************************************************************************
* Function Name: Cy_CapSense_Enable
****************************************************************************//**
*
* Initializes the CapSense firmware modules.
*
* Call the Cy_CapSense_Init() function and configure MSC HW block interrupts
* prior to calling this function.
* See the function usage example below for details on usage.
*
* The following are executed as part of the function:
* 1. Check CapSense configuration integrity.
* 2. Pre-calculate of internal register values to speed up operation.
* 3. Configure the MSC HW block(s) to perform capacitive sensing operation.
* 5. Calibrate the sensors and find the optimal values for CDACs of each
* widget/sensor, if the Enable CDAC auto-calibration is enabled in the
* CSD Setting or CSX Setting tabs.
* 5. Perform scanning for all the sensors and initialize the baseline history.
* 6. If the firmware filters are enabled in the Advanced General tab, the
* filter histories are also initialized.
*
* Any subsequent call of this function repeats initialization process.
* Therefore, it is possible to change the middleware configuration
* from the application program by writing registers to the data structure
* and calling this function again.
*
* The repeated call of this function is also done inside the
* Cy_CapSense_RunTuner() function when a restart command is received.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the initialization process. If CY_CAPSENSE_STATUS_SUCCESS is not
* received, some of the initialization fails.
*
* \funcusage
*
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_Initialization
* The 'cy_capsense_context' variable that is used as the parameter of the
* Cy_CapSense_Init() and Cy_CapSense_Enable() functions is declared in the
* cycfg_capsense.h file.
*
* The CapSense_ISR_cfg variable should be declared by the application
* program according to the examples below:<br>
* For CM0+ core:
* \snippet capsense/snippet/main.c snippet_m0p_capsense_interrupt_source_declaration
*
* For CM4 core:
* \snippet capsense/snippet/main.c snippet_m4_capsense_interrupt_source_declaration
*
* The CapSense interrupt handler should be declared by the application program
* according to the example below:
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_IntHandler
*
* The CY_MSC<X>_HW is the pointer to the base register address of
* the MSC_X HW block. A macro for the pointer is in the cycfg_peripherals.h
* file defined as \<Msc_Personality_Name\>_HW. If no name is specified,
* the default msc_\<Block_Number\>_msc_\<Block_Number\>_HW is used.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_Enable(cy_stc_capsense_context_t * context)
{
cy_capsense_status_t result;
uint32_t cpuFreqMHz;
uint32_t watchdogCounter;
#if (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3)
uint32_t widgetId;
#endif /* (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3) */
/* Approximate duration of Wait For Init loop */
const uint32_t isBusyLoopDuration = 5uL;
/* Wait For Init watchdog timeout in microseconds */
const uint32_t isBusyWatchdogTimeUs = 1000000uL;
/* Initialize CapSense modules */
result = Cy_CapSense_Initialize(context);
if(CY_CAPSENSE_STATUS_SUCCESS == result)
{
#if (CY_CAPSENSE_PLATFORM_BLOCK_CSDV2)
if (CY_CAPSENSE_CSD_SS_DIS != context->ptrCommonConfig->csdAutotuneEn)
{
result = Cy_CapSense_SsAutoTune_Call(context);
}
if ((CY_CAPSENSE_ENABLE == context->ptrCommonConfig->csdEn) &&
(CY_CAPSENSE_CSD_SS_DIS == context->ptrCommonConfig->csdAutotuneEn) &&
(CY_CAPSENSE_ENABLE == context->ptrCommonConfig->csdIdacAutocalEn))
{
result |= Cy_CapSense_CalibrateAllCsdWidgets_Call(context);
}
if((CY_CAPSENSE_ENABLE == context->ptrCommonConfig->csxEn) &&
(CY_CAPSENSE_ENABLE == context->ptrCommonConfig->csxIdacAutocalEn))
{
result |= Cy_CapSense_CalibrateAllCsxWidgets_Call(context);
}
#else /* (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3) */
result |= Cy_CapSense_CalibrateAllSlots(context);
#endif
#if (CY_CAPSENSE_PLATFORM_BLOCK_CSDV2)
result |= Cy_CapSense_ScanAllWidgets(context);
#else /* (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3) */
result |= Cy_CapSense_ScanAllSlots(context);
#endif
/* Init Watchdog Counter to prevent a hang */
cpuFreqMHz = context->ptrCommonConfig->cpuClkHz / CY_CAPSENSE_CONVERSION_MEGA;
watchdogCounter = Cy_CapSense_WatchdogCyclesNum(isBusyWatchdogTimeUs, cpuFreqMHz, isBusyLoopDuration);
while(CY_CAPSENSE_NOT_BUSY != Cy_CapSense_IsBusy(context))
{
if(0uL == watchdogCounter)
{
break;
}
watchdogCounter--;
}
#if (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3)
Cy_CapSense_PreProcessAllRaw(context);
#endif
#if (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3)
for (widgetId = 0u; widgetId < context->ptrCommonConfig->numWd; widgetId++)
{
if (CY_CAPSENSE_MPTX_MIN_ORDER <= context->ptrWdConfig[widgetId].mptxOrder)
{
result |= Cy_CapSense_ProcessWidgetMptxDeconvolution(widgetId, context);
}
}
#endif /* (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3) */
}
Cy_CapSense_InitializeAllFilters_Call(context);
Cy_CapSense_InitializeAllBaselines(context);
return result;
}
/*******************************************************************************
* Function Name: Cy_CapSense_Initialize
****************************************************************************//**
*
* Initializes all sub-modules of the CapSense middleware.
*
* The initialization includes:
* - Data Structure - set the default middleware parameters based
* on configuration.
* - Data Processing - resets the status all widgets.
* - Tuner - resets the tuner communication state.
* - Sensing - prepares the CapSense HW blocks for operation.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Return CY_CAPSENSE_STATUS_SUCCESS if the initialization was successful.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_Initialize(cy_stc_capsense_context_t * context)
{
cy_capsense_status_t result;
result = Cy_CapSense_CheckConfigIntegrity(context);
if (CY_CAPSENSE_STATUS_SUCCESS == result)
{
Cy_CapSense_InitializeAllStatuses(context);
result = Cy_CapSense_SsInitialize(context);
/* The Tuner and the BIST are initialized only once */
if (CY_CAPSENSE_INIT_NEEDED == context->ptrCommonContext->initDone)
{
Cy_CapSense_TuInitialize(context);
#if (CY_CAPSENSE_PLATFORM_BLOCK_CSDV2)
if (CY_CAPSENSE_ENABLE == context->ptrCommonConfig->bistEn)
{
Cy_CapSense_BistDsInitialize_Call(context);
}
#endif
context->ptrCommonContext->initDone = CY_CAPSENSE_INIT_DONE;
}
}
return (result);
}
/*******************************************************************************
* Function Name: Cy_CapSense_DeInit
****************************************************************************//**
*
* Stops the middleware operation and releases the CapSense captured HW blocks.
*
* No sensor scanning can be executed when the middleware is stopped.
* This function should be called only when no scanning is in progress.
* I.e. Cy_CapSense_IsBusy() returns a non-busy status.
*
* After the middleware stops, the MSC HW block(s) may be reconfigured with the
* application program or other middleware for any other usage.
*
* When the middleware operation is stopped by the Cy_CapSense_DeInit()
* function, subsequent call of the Cy_CapSense_Init() function repeats
* initialization process and it is not needed to call the Cy_CapSense_Enable()
* function second time. However, to implement time-multiplexed mode
* (sharing the MSC HW block(s) between multiple middleware)
* the Cy_CapSense_Save() and Cy_CapSense_Restore() functions should be used
* instead of the Cy_CapSense_DeInit() and Cy_CapSense_Init() functions for
* further compatibility.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the stop process. If CY_CAPSENSE_STATUS_SUCCESS is not received,
* the stop process fails and retries may be required.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_DeInit(cy_stc_capsense_context_t * context)
{
cy_capsense_status_t result;
result = Cy_CapSense_Save(context);
if (CY_CAPSENSE_STATUS_SUCCESS != result)
{
result = CY_CAPSENSE_STATUS_BAD_DATA;
}
else
{
context->ptrCommonContext->initDone = CY_CAPSENSE_INIT_NEEDED;
}
return (result);
}
/*******************************************************************************
* Function Name: Cy_CapSense_ProcessAllWidgets
****************************************************************************//**
*
* Performs full data processing of all enabled widgets.
*
* This function performs all data processes for all enabled widgets and
* sensors in the middleware to produce meaningful status output from widgets
* and sensors. The following tasks are executed as part of processing all the
* widgets:
* 1. Apply raw count filters to the raw counts, if they are enabled.
* 2. Update the thresholds if the SmartSense Full Auto-Tuning is enabled.
* 3. Update the baselines and difference counts for all the sensors.
* 4. Update the sensor and widget output status. Updates on/off status for
* buttons and proximity widgets, centroid/position for
* the sliders and the X/Y position for the touchpads.
*
* This function is called by the application program only after all the enabled
* widgets (and sensors) in the middleware are scanned. Calling this function
* multiple times without sensor scanning causes unexpected behavior.
*
* The disabled widgets are not processed by this function.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the processing operation. If CY_CAPSENSE_STATUS_SUCCESS is not received,
* the processing fails and retries may be required.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_ProcessAllWidgets(cy_stc_capsense_context_t * context)
{
uint32_t wdIndex;
cy_capsense_status_t result = CY_CAPSENSE_STATUS_SUCCESS;
for (wdIndex = context->ptrCommonConfig->numWd; wdIndex-- > 0u;)
{
result |= Cy_CapSense_ProcessWidget(wdIndex, context);
}
return result;
}
/*******************************************************************************
* Function Name: Cy_CapSense_ProcessWidget
****************************************************************************//**
*
* Performs full data processing of the specified widget if it is enabled.
*
* This function performs exactly the same tasks as
* Cy_CapSense_ProcessAllWidgets(), but only for a specified widget. This
* function can be used along with the Cy_CapSense_SetupWidget() and
* Cy_CapSense_Scan() functions to scan and process data for a specific
* widget. This function is called only after all the sensors in the
* widgets are scanned. A disabled widget is not processed by this function.
*
* A pipeline scan method (i.e. during scanning of a current widget (N),
* perform processing of the previously scanned widget (N-1)) can be
* implemented using this function and it may reduce the total execution time,
* increase the refresh rate, and decrease the average power consumption.
* See the function usage example below for details on usage.
*
* \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 widget processing:
* - CY_CAPSENSE_STATUS_SUCCESS - The operation is successfully completed
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid
* - CY_CAPSENSE_STATUS_INVALID_STATE - The specified widget is disabled
* - CY_CAPSENSE_STATUS_BAD_DATA - The processing is failed
*
* \funcusage
*
* An example of pipeline implementation:
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_ProcessWidget
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_ProcessWidget(
uint32_t widgetId,
cy_stc_capsense_context_t * context)
{
cy_capsense_status_t result = CY_CAPSENSE_STATUS_BAD_PARAM;
const cy_stc_capsense_widget_config_t * ptrWdCfg;
/* Check parameter validity */
if (widgetId < context->ptrCommonConfig->numWd)
{
ptrWdCfg = &context->ptrWdConfig[widgetId];
#if (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3)
Cy_CapSense_PreProcessWidgetRaw(widgetId, context);
#endif
/* Check widget enable status */
if (0u == (ptrWdCfg->ptrWdContext->status & CY_CAPSENSE_WD_DISABLE_MASK))
{
switch(ptrWdCfg->senseGroup)
{
case CY_CAPSENSE_CSD_GROUP:
result = Cy_CapSense_DpProcessCsdWidgetRawCounts_Call(ptrWdCfg, context);
Cy_CapSense_DpProcessCsdWidgetStatus_Call(ptrWdCfg, context);
break;
case CY_CAPSENSE_CSX_GROUP:
#if (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3)
if (CY_CAPSENSE_MPTX_MIN_ORDER <= ptrWdCfg->mptxOrder)
{
result |= Cy_CapSense_ProcessWidgetMptxDeconvolution(widgetId, context);
}
#endif /* CY_CAPSENSE_PLATFORM_BLOCK_MSCV3 */
result = Cy_CapSense_DpProcessCsxWidgetRawCounts_Call(ptrWdCfg, context);
Cy_CapSense_DpProcessCsxWidgetStatus_Call(ptrWdCfg, context);
break;
default:
CY_ASSERT(0 != 0);
break;
}
}
else
{
result = CY_CAPSENSE_STATUS_INVALID_STATE;
}
}
return result;
}
/*******************************************************************************
* Function Name: Cy_CapSense_ProcessWidgetExt
****************************************************************************//**
*
* Performs customized data processing on the selected widget.
*
* This function performs customized data processing specified by the mode
* parameter on a widget. This function can be used with any of the
* available scan functions. This function should be called only after all
* the sensors in the specified widget are scanned. Calling this function
* multiple times with the same mode without new sensor scan causes
* unexpected behavior. This function ignores the value of the
* wdgtEnable register.
*
* The CY_CAPSENSE_PROCESS_CALC_NOISE and CY_CAPSENSE_PROCESS_THRESHOLDS masks
* for mode parameter are supported only when SmartSense is enabled for
* CSD widgets.
*
* The execution order of processing tasks starts from LSB to MSB of the
* mode parameter. To implement a different order of execution, call this
* function multiple times with the required mode parameter.
*
* For more details, refer to function usage example below.
*
* \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 mode
* Specifies the type of widget processing to be executed for the
* specified widget:
* 1. Bits [31..6] - Reserved.
* 2. Bits [5..0] - CY_CAPSENSE_PROCESS_ALL - Execute all of the below tasks.
* 3. Bit [5] - CY_CAPSENSE_PROCESS_STATUS - Update the status
* (on/off, centroid position).
* 4. Bit [4] - CY_CAPSENSE_PROCESS_THRESHOLDS - Update the thresholds
* (only in CSD auto-tuning mode).
* 5. Bit [3] - CY_CAPSENSE_PROCESS_CALC_NOISE - Calculate the noise
* (only in CSD auto-tuning mode).
* 6. Bit [2] - CY_CAPSENSE_PROCESS_DIFFCOUNTS - Update the difference
* counts of each sensor.
* 7. Bit [1] - CY_CAPSENSE_PROCESS_BASELINE - Update the baselines
* for all sensor.
* 8. Bit [0] - CY_CAPSENSE_PROCESS_FILTER - Run the firmware filters
* on sensor rawcounts.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the widget processing operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The processing is successfully performed.
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid.
* - CY_CAPSENSE_STATUS_BAD_DATA - The processing failed.
*
* \funcusage
*
* An example of customized data processing, changed processing order:
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_ProcessWidgetExt
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_ProcessWidgetExt(
uint32_t widgetId,
uint32_t mode,
cy_stc_capsense_context_t * context)
{
uint32_t snsIndex;
cy_capsense_status_t result = CY_CAPSENSE_STATUS_BAD_PARAM;
uint32_t snsHistorySize;
uint32_t freqChIndex;
uint32_t freqChNumber;
uint16_t * ptrHistoryCh;
uint16_t * ptrHistorySns;
uint8_t * ptrHistoryLowCh = NULL;
uint8_t * ptrHistoryLowSns;
cy_stc_capsense_sensor_context_t * ptrSnsCxtCh;
cy_stc_capsense_sensor_context_t * ptrSnsCxtSns;
const cy_stc_capsense_widget_config_t * ptrWdCfg;
uint16_t * ptrBslnInvCh;
uint16_t * ptrBslnInvSns;
/* Check parameter validity */
if (widgetId < context->ptrCommonConfig->numWd)
{
ptrWdCfg = &context->ptrWdConfig[widgetId];
#if (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3)
Cy_CapSense_PreProcessWidgetRaw(widgetId, context);
#endif
snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
freqChNumber = (CY_CAPSENSE_ENABLE == context->ptrCommonConfig->mfsEn) ? 3u : 1u;
ptrSnsCxtCh = &ptrWdCfg->ptrSnsContext[0u];
ptrHistoryCh = &ptrWdCfg->ptrRawFilterHistory[0u];
ptrBslnInvCh = ptrWdCfg->ptrBslnInv;
if(CY_CAPSENSE_IIR_FILTER_PERFORMANCE == (ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_IIR_MODE_MASK))
{
ptrHistoryLowCh = &ptrWdCfg->ptrRawFilterHistoryLow[0u];
}
switch(ptrWdCfg->senseGroup)
{
case CY_CAPSENSE_CSD_GROUP:
/* Run the desired processing for the all CSD widget sensors */
for(freqChIndex = 0u; freqChIndex < freqChNumber; freqChIndex++)
{
ptrSnsCxtSns = ptrSnsCxtCh;
ptrHistorySns = ptrHistoryCh;
ptrHistoryLowSns = ptrHistoryLowCh;
ptrBslnInvSns = ptrBslnInvCh;
for (snsIndex = 0uL; snsIndex < ptrWdCfg->numSns; snsIndex++)
{
result = Cy_CapSense_DpProcessCsdSensorRawCountsExt(ptrWdCfg,
ptrSnsCxtSns,
ptrHistorySns,
ptrHistoryLowSns,
mode,
ptrBslnInvSns,
context);
ptrSnsCxtSns++;
ptrBslnInvSns++;
ptrHistorySns += snsHistorySize;
if(NULL != ptrHistoryLowSns)
{
ptrHistoryLowSns++;
}
}
ptrSnsCxtCh += context->ptrCommonConfig->numSns;
ptrBslnInvCh += context->ptrCommonConfig->numSns;
ptrHistoryCh += context->ptrCommonConfig->numSns * snsHistorySize;
if(NULL != ptrHistoryLowCh)
{
ptrHistoryLowCh += context->ptrCommonConfig->numSns;
}
}
if(CY_CAPSENSE_ENABLE == context->ptrCommonConfig->mfsEn)
{
ptrSnsCxtSns = ptrWdCfg->ptrSnsContext;
for (snsIndex = ptrWdCfg->numSns; snsIndex-- > 0u;)
{
Cy_CapSense_RunMfsFiltering(ptrSnsCxtSns, context);
ptrSnsCxtSns++;
}
}
if (0u != (mode & CY_CAPSENSE_PROCESS_STATUS))
{
Cy_CapSense_DpProcessCsdWidgetStatus_Call(ptrWdCfg, context);
}
break;
case CY_CAPSENSE_CSX_GROUP:
#if (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3)
if (CY_CAPSENSE_MPTX_MIN_ORDER <= context->ptrWdConfig[widgetId].mptxOrder)
{
result |= Cy_CapSense_ProcessWidgetMptxDeconvolution(widgetId, context);
}
#endif /* CY_CAPSENSE_PLATFORM_BLOCK_MSCV3 */
/* Run the desired processing for the all CSX widget sensors */
for(freqChIndex = 0u; freqChIndex < freqChNumber; freqChIndex++)
{
ptrSnsCxtSns = ptrSnsCxtCh;
ptrHistorySns = ptrHistoryCh;
ptrHistoryLowSns = ptrHistoryLowCh;
ptrBslnInvSns = ptrBslnInvCh;
for (snsIndex = 0uL; snsIndex < ptrWdCfg->numSns; snsIndex++)
{
result = Cy_CapSense_DpProcessCsxSensorRawCountsExt(ptrWdCfg,
ptrSnsCxtSns,
ptrHistorySns,
ptrHistoryLowSns,
mode,
ptrBslnInvSns,
context);
ptrSnsCxtSns++;
ptrBslnInvSns++;
ptrHistorySns += snsHistorySize;
if(NULL != ptrHistoryLowSns)
{
ptrHistoryLowSns++;
}
}
ptrSnsCxtCh += context->ptrCommonConfig->numSns;
ptrBslnInvCh += context->ptrCommonConfig->numSns;
ptrHistoryCh += context->ptrCommonConfig->numSns * snsHistorySize;
if(NULL != ptrHistoryLowCh)
{
ptrHistoryLowCh += context->ptrCommonConfig->numSns;
}
}
if(CY_CAPSENSE_ENABLE == context->ptrCommonConfig->mfsEn)
{
ptrSnsCxtSns = ptrWdCfg->ptrSnsContext;
for (snsIndex = ptrWdCfg->numSns; snsIndex-- > 0u;)
{
Cy_CapSense_RunMfsFiltering(ptrSnsCxtSns, context);
ptrSnsCxtSns++;
}
}
if (0u != (mode & CY_CAPSENSE_PROCESS_STATUS))
{
Cy_CapSense_DpProcessCsxWidgetStatus_Call(ptrWdCfg, context);
}
break;
default:
CY_ASSERT(0 != 0);
break;
}
}
return result;
}
/*******************************************************************************
* Function Name: Cy_CapSense_ProcessSensorExt
****************************************************************************//**
*
* Performs customized data processing on the selected sensor.
*
* This function performs customized data processing specified by the mode
* parameter on a sensor. This function performs the exact same task
* of the Cy_CapSense_ProcessWidgetExt() function but only on the specified
* sensor instead of all sensors in the widget.
*
* The pipeline scan method (i.e. during scanning of a sensor, processing
* of a previously scanned sensor is performed) can be implemented using this
* function and it may reduce the total scan/process time, increase the refresh
* rate, and decrease the power consumption. For more details, refer to
* function usage example below.
*
* \note
* This function does not perform raw count deconvolition for widgets with
* multi-phase Tx. The Cy_CapSense_ProcessWidgetMptxDeconvolution() function
* must be called manually from application layer before data processing perfomed
* by this function.
*
* \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 mode
* Specifies the type of the sensor processing that must be executed for the
* specified sensor:
* 1. Bits [31..5] - Reserved
* 2. Bits [4..0] - CY_CAPSENSE_PROCESS_ALL - Executes all the tasks
* 3. Bit [4] - CY_CAPSENSE_PROCESS_THRESHOLDS - Updates the thresholds
* (only in auto-tuning mode)
* 4. Bit [3] - CY_CAPSENSE_PROCESS_CALC_NOISE - Calculates the noise
* (only in auto-tuning mode)
* 5. Bit [2] - CY_CAPSENSE_PROCESS_DIFFCOUNTS - Updates the diff count
* 6. Bit [1] - CY_CAPSENSE_PROCESS_BASELINE - Updates the baseline
* 7. Bit [0] - CY_CAPSENSE_PROCESS_FILTER - Runs the firmware filters
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the sensor process operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The processing is successfully performed.
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid.
* - CY_CAPSENSE_STATUS_BAD_DATA - The processing failed.
*
* \funcusage
*
* An example demonstrates pipeline implementation of sensor scanning and
* processing:
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_ProcessSensorExt
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_ProcessSensorExt(
uint32_t widgetId,
uint32_t sensorId,
uint32_t mode,
const cy_stc_capsense_context_t * context)
{
cy_capsense_status_t result = CY_CAPSENSE_STATUS_BAD_PARAM;
cy_capsense_status_t resultTmp;
uint32_t freqChIndex;
uint32_t freqChNumber;
uint32_t snsHistorySize;
uint32_t cxtOffset;
uint32_t historyOffset;
uint16_t * ptrHistory;
uint8_t * ptrHistoryLow = NULL;
const cy_stc_capsense_widget_config_t * ptrWdCfg;
cy_stc_capsense_sensor_context_t * ptrSnsCxt;
uint16_t * ptrSnsBslnInv;
if (widgetId < context->ptrCommonConfig->numWd)
{
ptrWdCfg = &context->ptrWdConfig[widgetId];
if (sensorId < ptrWdCfg->numSns)
{
#if (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3)
Cy_CapSense_PreProcessSnsRaw(widgetId, sensorId, context);
#endif
snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
freqChNumber = (CY_CAPSENSE_ENABLE == context->ptrCommonConfig->mfsEn) ? 3u : 1u;
ptrHistory = &ptrWdCfg->ptrRawFilterHistory[sensorId * snsHistorySize];
if((uint32_t)CY_CAPSENSE_IIR_FILTER_PERFORMANCE == ((uint32_t)ptrWdCfg->rawFilterConfig &
(uint32_t)CY_CAPSENSE_RC_FILTER_IIR_MODE_MASK))
{
ptrHistoryLow = &ptrWdCfg->ptrRawFilterHistoryLow[sensorId];
}
ptrSnsCxt = &ptrWdCfg->ptrSnsContext[sensorId];
ptrSnsBslnInv = &ptrWdCfg->ptrBslnInv[sensorId];
cxtOffset = context->ptrCommonConfig->numSns;
historyOffset = snsHistorySize * context->ptrCommonConfig->numSns;
result = CY_CAPSENSE_STATUS_SUCCESS;
switch(ptrWdCfg->senseGroup)
{
case CY_CAPSENSE_CSD_GROUP:
for(freqChIndex = 0u; freqChIndex < freqChNumber; freqChIndex++)
{
resultTmp = Cy_CapSense_DpProcessCsdSensorRawCountsExt(ptrWdCfg, ptrSnsCxt,
ptrHistory, ptrHistoryLow,
mode, ptrSnsBslnInv, context);
if (CY_CAPSENSE_STATUS_SUCCESS != resultTmp)
{
result = CY_CAPSENSE_STATUS_BAD_DATA;
}
ptrSnsCxt += cxtOffset;
ptrHistory += historyOffset;
ptrHistoryLow += cxtOffset;
ptrSnsBslnInv += cxtOffset;
}
break;
case CY_CAPSENSE_CSX_GROUP:
for(freqChIndex = 0u; freqChIndex < freqChNumber; freqChIndex++)
{
resultTmp = Cy_CapSense_DpProcessCsxSensorRawCountsExt(ptrWdCfg, ptrSnsCxt,
ptrHistory, ptrHistoryLow,
mode, ptrSnsBslnInv, context);
if (CY_CAPSENSE_STATUS_SUCCESS != resultTmp)
{
result = CY_CAPSENSE_STATUS_BAD_DATA;
}
ptrSnsCxt += cxtOffset;
ptrHistory += historyOffset;
ptrHistoryLow += cxtOffset;
ptrSnsBslnInv += cxtOffset;
}
break;
default:
CY_ASSERT(0 != 0);
break;
}
if(CY_CAPSENSE_ENABLE == context->ptrCommonConfig->mfsEn)
{
ptrSnsCxt = ptrWdCfg->ptrSnsContext;
Cy_CapSense_RunMfsFiltering(ptrSnsCxt, context);
}
}
}
return result;
}
/*******************************************************************************
* Function Name: Cy_CapSense_Wakeup
****************************************************************************//**
*
* Resumes the middleware after System Deep Sleep.
*
* This function is used to resume the middleware operation after exiting
* System Deep Sleep. After the MSC HW block is powered off,
* an extra delay is required to establish the correct operation of
* the MSC HW block.
*
* This function is called by the Cy_CapSense_DeepSleepCallback() function after
* exiting System Deep Sleep if the CapSense Deep Sleep callback is registered.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_Wakeup(const cy_stc_capsense_context_t * context)
{
Cy_SysLib_DelayUs((uint16_t)context->ptrCommonConfig->analogWakeupDelay);
}
/*******************************************************************************
* Function Name: Cy_CapSense_DeepSleepCallback
****************************************************************************//**
*
* Handles CPU active to System Deep Sleep power mode transition for the CapSense
* middleware.
*
* Calling this function directly from the application program is not recommended.
* Instead, Cy_SysPm_CpuEnterDeepSleep() should be used for the CPU active to System Deep Sleep
* power mode transition of the device.
* \note
* After the CPU Deep Sleep transition, the device automatically goes
* to System Deep Sleep if all conditions are fulfilled: another core is
* in CPU Deep Sleep, all the peripherals are ready to System Deep Sleep, etc.
* (see details in the device TRM).
*
* For proper operation of the CapSense middleware during the CPU active to
* System Deep Sleep mode transition, a callback to this function should be registered
* using the Cy_SysPm_RegisterCallback() function with CY_SYSPM_DEEPSLEEP
* type. After the callback is registered, this function is called by the
* Cy_SysPm_CpuEnterDeepSleep() function to prepare the middleware to the device
* power mode transition.
*
* When this function is called with CY_SYSPM_CHECK_READY as an input, this
* function returns CY_SYSPM_SUCCESS if no scanning is in progress or not
* a single HW block is captured by the CapSense middleware. Otherwise
* CY_SYSPM_FAIL is returned. If CY_SYSPM_FAIL status is returned, a device
* cannot change the power mode without completing the current scan as
* a transition to System Deep Sleep during the scan can disrupt the middleware
* operation.
*
* When this function is called with CY_SYSPM_AFTER_TRANSITION as an input, then
* the Cy_CapSense_Wakeup() function is called to resume the middleware
* operation after exiting System Deep Sleep. If there are no CapSense captured
* HW blocks the Cy_CapSense_Wakeup() function calling is omitted and restoring
* CapSense immediately after Deep Sleep without the wake-up delay can lead to
* unpredictable behavior.
*
* For details of SysPm types and macros refer to the SysPm section of the
* PDL documentation.
*
* \param callbackParams
* Refer to the description of the cy_stc_syspm_callback_params_t type in the
* Peripheral Driver Library documentation.
*
* \param mode
* Specifies mode cy_en_syspm_callback_mode_t.
*
* \return
* Returns the status cy_en_syspm_status_t of the operation requested
* by the mode parameter:
* - CY_SYSPM_SUCCESS - System Deep Sleep power mode can be entered.
* - CY_SYSPM_FAIL - System Deep Sleep power mode cannot be entered.
*
*******************************************************************************/
cy_en_syspm_status_t Cy_CapSense_DeepSleepCallback(
cy_stc_syspm_callback_params_t * callbackParams,
cy_en_syspm_callback_mode_t mode)
{
cy_en_syspm_status_t retVal = CY_SYSPM_SUCCESS;
cy_stc_capsense_context_t * capsenseCxt = (cy_stc_capsense_context_t *)callbackParams->context;
#if (CY_CAPSENSE_PLATFORM_BLOCK_CSDV2)
cy_en_csd_key_t mwKey;
switch(mode)
{
case CY_SYSPM_CHECK_READY:
mwKey = Cy_CSD_GetLockStatus(capsenseCxt->ptrCommonConfig->ptrCsdBase, capsenseCxt->ptrCommonConfig->ptrCsdContext);
if (CY_CSD_CAPSENSE_KEY == mwKey)
{
if (CY_CAPSENSE_NOT_BUSY != Cy_CapSense_IsBusy(capsenseCxt))
{
retVal = CY_SYSPM_FAIL;
}
}
break;
case CY_SYSPM_AFTER_TRANSITION:
mwKey = Cy_CSD_GetLockStatus(capsenseCxt->ptrCommonConfig->ptrCsdBase, capsenseCxt->ptrCommonConfig->ptrCsdContext);
if (CY_CSD_CAPSENSE_KEY == mwKey)
{
Cy_CapSense_Wakeup(capsenseCxt);
}
break;
default:
break;
}
#else /* (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3) */
uint32_t curChIndex;