-
Notifications
You must be signed in to change notification settings - Fork 4
/
cy_capsense_csd_v2.c
2144 lines (1889 loc) · 87 KB
/
cy_capsense_csd_v2.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_csd_v2.c
* \version 5.0
*
* \brief
* This file defines the data structure global variables and provides
* implementation for the low-level functions of the CSD part of
* the Sensing module. The file contains the functions used for the CSD HW block
* initialization, calibration, and scanning.
*
********************************************************************************
* \copyright
* Copyright 2018-2024, Cypress Semiconductor Corporation (an Infineon company)
* or an affiliate of 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_device_headers.h"
#include "cy_sysclk.h"
#include "cy_syslib.h"
#include "cy_gpio.h"
#include "cy_csd.h"
#include "cycfg_capsense_defines.h"
#include "cy_capsense_common.h"
#include "cy_capsense_structure.h"
#include "cy_capsense_sensing_v2.h"
#include "cy_capsense_csd_v2.h"
#if (defined(CY_IP_MXCSDV2) || defined(CY_IP_M0S8CSDV2))
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN)
/*******************************************************************************
* Local definition
*******************************************************************************/
/* CY_ID285392, CY_ID515 */
#define CY_CAPSENSE_FILTER_DELAY_MAX (CY_CAPSENSE_CSD_CONFIG_FILTER_DELAY_4_CYCLES)
#define CY_CAPSENSE_EXTRA_COUNTS_MAX (CY_CAPSENSE_FILTER_DELAY_MAX + 5u + 20u)
#define CY_CAPSENSE_16_BIT_RESOLUTION (16uL)
#define CY_CAPSENSE_CSD_PRECHARGE_MAX_TIME_US (6000u)
#define CY_CAPSENSE_CSD_WATCHDOG_MARGIN_COEFF (3u)
#define CY_CAPSENSE_CSD_PRECHARGE_WATCHDOG_TIME_US (CY_CAPSENSE_CSD_PRECHARGE_MAX_TIME_US *\
CY_CAPSENSE_CSD_WATCHDOG_MARGIN_COEFF)
/*******************************************************************************
* Static Function Prototypes
*******************************************************************************/
/******************************************************************************/
/** \cond SECTION_CAPSENSE_INTERNAL */
/** \addtogroup group_capsense_internal *//** \{ */
/******************************************************************************/
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN)
__STATIC_INLINE void Cy_CapSense_CSDChangeClkFreq(uint32_t channelIndex, cy_stc_capsense_context_t * context);
#endif
static void Cy_CapSense_CSDInitNextSnsScan(cy_stc_capsense_context_t * context);
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN)
static void Cy_CapSense_CSDInitNextChScan(cy_stc_capsense_context_t * context);
#endif
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_SHIELD_EN)
static void Cy_CapSense_CSDEnableShieldElectrodes(const cy_stc_capsense_context_t * context);
#endif
static void Cy_CapSense_CSDTriggerScan(cy_stc_capsense_context_t * context);
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_CALIBRATION_EN)
static void Cy_CapSense_CSDCalibrate(
uint32_t widgetId, uint32_t target,
cy_stc_capsense_context_t * context);
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_IDAC_AUTO_GAIN_EN)
static uint32_t Cy_CapSense_CSDSwitchIdacGain(cy_stc_capsense_context_t * context);
#endif
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_IDAC_COMP_EN)
static void Cy_CapSense_CSDNormalizeIdac(
cy_stc_capsense_widget_config_t const * ptrWdConfig,
uint32_t target,
const cy_stc_capsense_context_t * context);
#endif
#endif
/** \} \endcond */
/*******************************************************************************
* Function Name: Cy_CapSense_CSDDisableMode
****************************************************************************//**
*
* This function disables CSD mode.
*
* To disable CSD mode the following tasks are performed:
* 1. Disconnect Cmod from AMUXBUS-A.
* 2. Disconnect previous CSX electrode if it has been connected.
* 3. Disconnect Csh from AMUXBUS-B.
* 4. Disable Shield Electrodes.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDDisableMode(cy_stc_capsense_context_t * context)
{
/* Cmod pin to default state */
Cy_CapSense_SsConfigPinRegisters(context->ptrCommonConfig->portCmod, (uint32_t)context->ptrCommonConfig->pinCmod, CY_CAPSENSE_DM_GPIO_ANALOG, CY_CAPSENSE_HSIOM_SEL_GPIO);
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_SHIELD_EN)
/* Disconnect shields */
Cy_CapSense_CSDDisableShieldElectrodes(context);
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_SHIELD_CAP_EN)
/* Csh pin to default state */
Cy_CapSense_SsConfigPinRegisters(context->ptrCommonConfig->portCsh, (uint32_t)context->ptrCommonConfig->pinCsh, CY_CAPSENSE_DM_GPIO_ANALOG, CY_CAPSENSE_HSIOM_SEL_GPIO);
#endif
#endif
/* Disconnect IDACA, IDACB and RefGen input */
context->ptrCommonConfig->ptrCsdBase->SW_REFGEN_SEL = 0u;
/* Disconnect previous CSD sensor if it has been connected */
Cy_CapSense_CSDSnsStateCheck(context);
}
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_SHIELD_EN)
/*******************************************************************************
* Function Name: Cy_CapSense_CSDEnableShieldElectrodes
****************************************************************************//**
*
* This internal function initializes Shield Electrodes.
*
* The function sets the bit in the HSIOM register which enables the shield electrode
* functionality on the pin. The port and pin configurations are stored in
* the cy_capsense_shieldIoList structure.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
static void Cy_CapSense_CSDEnableShieldElectrodes(const cy_stc_capsense_context_t * context)
{
uint32_t shieldIndex;
const cy_stc_capsense_pin_config_t * ptrShieldPin = context->ptrShieldPinConfig;
for (shieldIndex = 0u; shieldIndex < context->ptrCommonConfig->csdShieldNumPin; shieldIndex++)
{
Cy_CapSense_SsConfigPinRegisters(ptrShieldPin->pcPtr, (uint32_t)ptrShieldPin->pinNumber, CY_CAPSENSE_DM_SHIELD, CY_CAPSENSE_HSIOM_SEL_CSD_SHIELD);
ptrShieldPin++;
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDDisableShieldElectrodes
****************************************************************************//**
*
* This internal function disables Shield Electrodes.
*
* The function resets the bit in the HSIOM register which disables the shield
* electrode functionality on the pin. The port and pin configurations are
* stored in the cy_capsense_shieldIoList structure.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDDisableShieldElectrodes(const cy_stc_capsense_context_t * context)
{
uint32_t shieldIndex;
const cy_stc_capsense_pin_config_t * ptrShieldPin = context->ptrShieldPinConfig;
for (shieldIndex = 0u; shieldIndex < context->ptrCommonConfig->csdShieldNumPin; shieldIndex++)
{
Cy_CapSense_SsConfigPinRegisters(ptrShieldPin->pcPtr, (uint32_t)ptrShieldPin->pinNumber, CY_CAPSENSE_DM_GPIO_STRONG_IN_OFF, CY_CAPSENSE_HSIOM_SEL_GPIO);
Cy_GPIO_Clr(ptrShieldPin->pcPtr, (uint32_t)ptrShieldPin->pinNumber);
ptrShieldPin++;
}
}
#endif
/*******************************************************************************
* Function Name: Cy_CapSense_CSDInitialize
****************************************************************************//**
*
* This function initializes the CSD module.
*
* The function performs the following steps:
* 1) Sets GPIO output to "0" for all sensor pins;
* 2) Connects CMOD to AMUXBUS-A and to CSDBUS-A;
* 3) Connects CMOD to (sense path) to CSDCOMP;
* 4) Connects Csh_tank to AMUXBUS-B and to CSDBUS-B;
* 5) Connects VREF to REFGEN;
* 6) Configures REFGEN and sets the reference voltage;
* 7) Connects VREF to CSDCOMP and HSCOMP;
* 8) Configures IDAC and connect to CSDBUS-A (to drive CMOD);
* 9) Configures ModClk;
* 10) Configure SnsClk source;
* 11) Sets other CSD configurations (Csd Auto Zero time,
* Sample Init period, interrupts,
* CMOD and Csh_tank/shield initialization switch resistance).
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDInitialize(cy_stc_capsense_context_t * context)
{
uint32_t interruptState;
/* Set all the sensors to inactive state */
Cy_CapSense_CSDClearSensors(context);
Cy_CapSense_DischargeExtCapacitors(context);
/* Initialize the unused CSD IP registers to default state */
context->ptrCommonConfig->ptrCsdBase->HSCMP = 0u;
context->ptrCommonConfig->ptrCsdBase->SEQ_TIME = 0u;
context->ptrCommonConfig->ptrCsdBase->SENSE_DUTY = CY_CAPSENSE_CSD_SENSE_DUTY_CFG;
#if (CY_CAPSENSE_PSOC6_FOURTH_GEN)
context->ptrCommonConfig->ptrCsdBase->IO_SEL = context->ptrInternalContext->csdRegIoSel;
#endif
context->ptrCommonConfig->ptrCsdBase->INTR_SET = CY_CAPSENSE_DEFAULT_CSD_INTR_SET;
(void)context->ptrCommonConfig->ptrCsdBase->INTR_SET;
context->ptrCommonConfig->ptrCsdBase->SW_DSI_SEL = context->ptrInternalContext->csdRegSwDsiSel;
context->ptrCommonConfig->ptrCsdBase->ADC_CTL = CY_CAPSENSE_DEFAULT_CSD_ADC_CTL;
context->ptrCommonConfig->ptrCsdBase->SW_SHIELD_SEL = 0u;
context->ptrCommonConfig->ptrCsdBase->SW_FW_MOD_SEL = 0u;
context->ptrCommonConfig->ptrCsdBase->SW_FW_TANK_SEL = 0u;
context->ptrCommonConfig->ptrCsdBase->SW_AMUXBUF_SEL = context->ptrInternalContext->csdRegSwAmuxbufSel;
context->ptrCommonConfig->ptrCsdBase->SW_HS_P_SEL = context->ptrInternalContext->csdRegSwHsPSelScan;
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_SHIELD_EN)
/* Connect shields to AMUX-B bus (config HSIOM regs) */
Cy_CapSense_CSDEnableShieldElectrodes(context);
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_SHIELD_CAP_EN)
Cy_CapSense_SsConfigPinRegisters(context->ptrCommonConfig->portCsh, (uint32_t)context->ptrCommonConfig->pinCsh, CY_CAPSENSE_DM_GPIO_ANALOG, CY_CAPSENSE_HSIOM_SEL_AMUXB);
#endif
#endif
/* Connection AMUXBUS-A to CSDBUS-A / AMUXBUS-B to CSDBUS-B */
context->ptrCommonConfig->ptrCsdBase->SW_BYP_SEL = context->ptrInternalContext->csdRegSwBypSel;
/* Connect CMOD to AMUXBUS-A */
interruptState = Cy_SysLib_EnterCriticalSection();
Cy_GPIO_SetDrivemode((GPIO_PRT_Type*)context->ptrCommonConfig->portCmod, (uint32_t)context->ptrCommonConfig->pinCmod, CY_CAPSENSE_DM_GPIO_ANALOG);
Cy_GPIO_SetHSIOM((GPIO_PRT_Type*)context->ptrCommonConfig->portCmod, (uint32_t)context->ptrCommonConfig->pinCmod, CY_CAPSENSE_HSIOM_SEL_AMUXA);
Cy_SysLib_ExitCriticalSection(interruptState);
/* Select CMOD and Csh_tank/shield initialization switch resistance */
context->ptrCommonConfig->ptrCsdBase->SW_RES = context->ptrInternalContext->csdRegSwResScan;
/* Set the number of dummy fine initialization cycles */
context->ptrCommonConfig->ptrCsdBase->SEQ_INIT_CNT = (uint32_t)context->ptrCommonConfig->csdFineInitTime;
/* Connect CMOD to (sense path) to CSDCOMP */
context->ptrCommonConfig->ptrCsdBase->SW_CMP_P_SEL = context->ptrInternalContext->csdRegSwCmpPSel;
/* Configure VREF */
context->ptrCommonConfig->ptrCsdBase->SW_REFGEN_SEL = context->ptrInternalContext->csdRegSwRefGenSel;
context->ptrCommonConfig->ptrCsdBase->AMBUF = context->ptrInternalContext->csdRegAmuxbufInit;
/* Connect VREFHI to HSCOMP */
context->ptrCommonConfig->ptrCsdBase->SW_HS_N_SEL = CY_CAPSENSE_CSD_SW_HS_N_SEL_SW_HCRH_STATIC_CLOSE;
context->ptrCommonConfig->ptrCsdBase->SW_CMP_N_SEL = context->ptrInternalContext->csdRegSwCmpNSel;
context->ptrCommonConfig->ptrCsdBase->REFGEN = context->ptrInternalContext->csdRegRefgen;
/* Configure IDACs mode */
context->ptrCommonConfig->ptrCsdBase->IDACA = context->ptrInternalContext->csdIdacAConfig;
context->ptrCommonConfig->ptrCsdBase->IDACB = context->ptrInternalContext->csdIdacBConfig;
context->ptrCommonConfig->ptrCsdBase->CONFIG = context->ptrInternalContext->csdRegConfig;
/* Configure ModClk */
Cy_CapSense_SetClkDivider((uint32_t)context->ptrCommonContext->modCsdClk - 1u, context);
/* Set all sensor states to the defined ISC state */
Cy_CapSense_SetIOsInDesiredState(context->ptrInternalContext->csdInactiveSnsDm, 0u,
context->ptrInternalContext->csdInactiveSnsHsiom, context);
/* Setup ISR handler to single-sensor scan function */
context->ptrInternalContext->ptrISRCallback = &Cy_CapSense_CSDScanISR;
context->ptrActiveScanSns->mfsChannelIndex = 0u;
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDSnsStateCheck
****************************************************************************//**
*
* Checks if electrodes were previously connected using
* Cy_CapSense_CSDSetupWidgetExt() and if yes disconnects them.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDSnsStateCheck(cy_stc_capsense_context_t * context)
{
if (CY_CAPSENSE_SNS_CONNECTED == context->ptrActiveScanSns->connectedSnsState)
{
Cy_CapSense_CSDDisconnectSnsExt(context);
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDSetUpIdacs
****************************************************************************//**
*
* This function writes both IDACs available in CSD method into corresponding
* the CSD HW block registers.
*
* Modulator IDAC is taken from widget context structure and Compensation
* IDAC is taken from sensor context structure.
* The cy_stc_capsense_active_scan_sns_t structure must be updated to the current
* widget/sensor by Cy_CapSense_InitActivePtr() before calling this function.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDSetUpIdacs(cy_stc_capsense_context_t * context)
{
uint32_t regValue;
const cy_stc_capsense_active_scan_sns_t * ptrActive = context->ptrActiveScanSns;
const uint32_t idacaBitsToWrite = CSD_IDACA_VAL_Msk |
CSD_IDACA_RANGE_Msk |
CSD_IDACA_LEG1_EN_Msk |
CSD_IDACA_LEG2_EN_Msk;
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_IDAC_COMP_EN)
const uint32_t idacbBitsToWrite = CSD_IDACB_VAL_Msk |
CSD_IDACB_RANGE_Msk |
CSD_IDACB_LEG1_EN_Msk |
CSD_IDACB_LEG2_EN_Msk |
CSD_IDACB_LEG3_EN_Msk;
#endif
/* IDAC A Code */
if (ptrActive->ptrWdConfig->numCols > ptrActive->sensorIndex)
{
regValue = ptrActive->ptrWdContext->idacMod[ptrActive->mfsChannelIndex];
}
else
{
regValue = ptrActive->ptrWdContext->rowIdacMod[ptrActive->mfsChannelIndex];
}
regValue |= context->ptrCommonConfig->idacGainTable[ptrActive->ptrWdContext->idacGainIndex].gainReg;
context->ptrCommonConfig->ptrCsdBase->IDACA =
(context->ptrCommonConfig->ptrCsdBase->IDACA & ~idacaBitsToWrite) |
(regValue & idacaBitsToWrite);
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_IDAC_COMP_EN)
/* IDAC B Code */
regValue = ptrActive->ptrSnsContext->idacComp;
regValue |= context->ptrCommonConfig->idacGainTable[ptrActive->ptrWdContext->idacGainIndex].gainReg;
context->ptrCommonConfig->ptrCsdBase->IDACB =
(context->ptrCommonConfig->ptrCsdBase->IDACB & ~idacbBitsToWrite) |
(regValue & idacbBitsToWrite);
#endif
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDGetNumberOfConversions
****************************************************************************//**
*
* This function calculate number of sub-conversions.
*
* \param snsClkDivider
* The divider value of the sense clock.
*
* \param resolution
* The widget resolution.
*
* \param snsClkSrc
* The widget Sense Clock Source.
*
* \return
* Returns the number of sub-conversions.
*
*******************************************************************************/
uint32_t Cy_CapSense_CSDGetNumberOfConversions(uint32_t snsClkDivider, uint32_t resolution, uint32_t snsClkSrc)
{
/* CY_ID304273 */
uint32_t conversionsNum = 1uL << resolution;
uint32_t extraCounts = 0u;
uint32_t snsClkDividerLocal = snsClkDivider;
if (0u == snsClkDividerLocal)
{
snsClkDividerLocal = 1u;
}
if ((CY_CAPSENSE_CLK_SOURCE_PRS8 == snsClkSrc) || (CY_CAPSENSE_CLK_SOURCE_PRS12 == snsClkSrc))
{
snsClkDividerLocal <<= 1u;
}
if (CY_CAPSENSE_16_BIT_RESOLUTION <= resolution)
{
/* CY_ID285392 */
extraCounts = CY_CAPSENSE_EXTRA_COUNTS_MAX;
}
conversionsNum = (conversionsNum - extraCounts) / snsClkDividerLocal;
return((conversionsNum > 0u) ? (conversionsNum) : 1u);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDConfigClock
****************************************************************************//**
*
* This function configure sense clock for different modes.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDConfigClock(cy_stc_capsense_context_t * context)
{
const cy_stc_capsense_widget_context_t * ptrWdCxt = context->ptrActiveScanSns->ptrWdContext;
uint32_t snsClkDivider;
uint32_t snsClkSrc = (uint32_t)ptrWdCxt->snsClkSource & ((uint32_t)~(uint32_t)CY_CAPSENSE_CLK_SOURCE_AUTO_MASK);
uint32_t regValue = 0u;
/* Getting row clock divider for matrix buttons or touchpad widgets */
if (context->ptrActiveScanSns->ptrWdConfig->numCols <= context->ptrActiveScanSns->sensorIndex)
{
snsClkDivider = ptrWdCxt->rowSnsClk;
}
else
{
snsClkDivider = ptrWdCxt->snsClk;
}
/* Configuring PRS SEL_BIT and decreasing divider in case of PRS */
if ((CY_CAPSENSE_CLK_SOURCE_PRS8 == snsClkSrc) || (CY_CAPSENSE_CLK_SOURCE_PRS12 == snsClkSrc))
{
regValue = CY_CAPSENSE_CSD_SENSE_PERIOD_SEL_LFSR_MSB_MSK;
snsClkDivider >>= 1u;
}
/* Check divider value */
if (0u == snsClkDivider)
{
snsClkDivider = 1u;
}
regValue |= ((snsClkSrc << CY_CAPSENSE_CSD_SENSE_PERIOD_LFSR_SIZE_POS) | (snsClkDivider - 1u));
/* Update reg value with divider and configuration */
context->ptrCommonConfig->ptrCsdBase->SENSE_PERIOD = regValue;
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDCalculateScanDuration
****************************************************************************//**
*
* Calculates Scan Duration which is defined by scan resolution
*
* The function calculates the number of conversions and updates
* SEQ_NORM_CNT register. The number of conversions depends on resolution and
* snsClk divider.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDCalculateScanDuration(cy_stc_capsense_context_t * context)
{
uint32_t subConv;
const cy_stc_capsense_widget_context_t * ptrWdCxt = context->ptrActiveScanSns->ptrWdContext;
uint32_t divider = ptrWdCxt->snsClk;
if (context->ptrActiveScanSns->ptrWdConfig->numCols <= context->ptrActiveScanSns->sensorIndex)
{
divider = ptrWdCxt->rowSnsClk;
}
subConv = Cy_CapSense_CSDGetNumberOfConversions(divider, (uint32_t)ptrWdCxt->resolution,
((uint32_t)ptrWdCxt->snsClkSource & ((uint32_t)~(uint32_t)CY_CAPSENSE_CLK_SOURCE_AUTO_MASK)));
/* Write number of sub-conversions into the CSD HW block register */
context->ptrCommonConfig->ptrCsdBase->SEQ_NORM_CNT = (subConv & CY_CAPSENSE_CSD_SEQ_NORM_CNT_CONV_CNT_MSK);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDSetupWidget
****************************************************************************//**
*
* Performs the initialization required to scan the specified CSD widget.
*
* \deprecated This function is obsolete and kept for backward compatibility only.
* The Cy_CapSense_SetupWidget() function should be used instead.
*
* This function prepares the middleware to scan all the sensors in the
* specified CSD widget by executing the following tasks:
* 1. Configure the CSD HW block if it is not configured to perform the
* CSD sensing method used by the specified widget.
* 2. Initialize the CSD HW block with specific sensing configuration (e.g.
* sensor clock, scan resolution) used by the widget.
* 3. Disconnect all previously connected electrodes, if the electrodes
* connected by the Cy_CapSense_CSDSetupWidgetExt(),
* Cy_CapSense_CSXSetupWidgetExt() functions are not disconnected.
*
* This function does not start sensor scanning. The Cy_CapSense_CSDScan()
* function must be called to start the scan sensors in the widget. If this
* function is called more than once, it does not break the middleware
* operation, but only the last initialized widget is in effect.
* Calling this function directly from the application program is not
* recommended. This function is used to implement only the user's specific
* use cases (for example faster execution).
*
* The status of a sensor scan must be checked using the Cy_CapSense_IsBusy()
* function prior to starting a next scan or setting up another widget.
*
* \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_CSDSetupWidget(uint32_t widgetId, cy_stc_capsense_context_t * context)
{
#if ((CY_CAPSENSE_DISABLE == CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN) &&\
((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN) ||\
(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN)))
const cy_stc_capsense_widget_config_t * ptrWdCfg;
#endif
/* Setup new scanning mode */
(void)Cy_CapSense_SwitchSensingMode(CY_CAPSENSE_CSD_GROUP, context);
/* Disconnect previous sensors if they have been connected */
Cy_CapSense_CSDSnsStateCheck(context);
/* Save widget Id to have assess to it after scanning */
Cy_CapSense_InitActivePtr(widgetId, 0u, context);
/*
* Configuring the Sense Clock and the Scan Duration if the
* MFS feature is disabled, and the one-dimension widget will be scanned.
* If the MFS feature is enabled, or the two-dimension widget will be scanned,
* the Sense Clock and the Scan Duration will be configured for each call
* of the Cy_CapSense_CSDScan() function.
*/
#if (CY_CAPSENSE_DISABLE == CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN)
#if ((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN) ||\
(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN))
ptrWdCfg = context->ptrActiveScanSns->ptrWdConfig;
if (((uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E != ptrWdCfg->wdType) &&
((uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E != ptrWdCfg->wdType))
{
Cy_CapSense_CSDCalculateScanDuration(context);
Cy_CapSense_CSDConfigClock(context);
}
#else
Cy_CapSense_CSDCalculateScanDuration(context);
Cy_CapSense_CSDConfigClock(context);
#endif
#endif
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDSetupWidgetExt
****************************************************************************//**
*
* Performs extended initialization required to scan a specified sensor in
* a widget using CSD sensing method.
*
* \deprecated This function is obsolete and kept for backward compatibility only.
* The Cy_CapSense_SetupWidgetExt() function should be used instead.
*
* This function performs the same tasks of Cy_CapSense_CSDSetupWidget() and
* also connects and configures specified sensor for scan. Hence this
* function, along with Cy_CapSense_CSDScanExt() function, can be used to
* scan a specific sensor in the widget.
*
* This function should be called for widget that is configured to use
* CSD sensing method, using this function on a non-CSD sensing widget
* would cause unexpected result.
*
* This function requires using the Cy_CapSense_CSDScanExt() function to
* initiate a scan.
*
* Calling this function directly from the application layer is not
* recommended. This function is used to implement only the user's
* specific use cases (for example faster execution).
*
* \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_CSDSetupWidgetExt(uint32_t widgetId, uint32_t sensorId, cy_stc_capsense_context_t * context)
{
/* Switch Mode if needed */
(void)Cy_CapSense_SwitchSensingMode(CY_CAPSENSE_CSD_GROUP, context);
/* Disconnect previous sensors if they have been connected */
Cy_CapSense_CSDSnsStateCheck(context);
Cy_CapSense_InitActivePtr(widgetId, sensorId, context);
/*
* Set up of the Sense Clock, the Scan Duration and the IDACs
* if the MFS feature is disabled.
* If the MFS feature is enabled, the Sense Clock, the
* Scan Duration and the IDACs will be configured with the
* MFS channel #0 settings each call of the Cy_CapSense_CSDScan()
* function.
*/
#if (CY_CAPSENSE_DISABLE == CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN)
Cy_CapSense_CSDSetUpIdacs(context);
Cy_CapSense_CSDCalculateScanDuration(context);
Cy_CapSense_CSDConfigClock(context);
#endif
/* Setup new widget/sensor */
Cy_CapSense_CSDConnectSnsExt(context);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDStartSample
****************************************************************************//**
*
* Starts the CSD conversion.
*
* This function assumes that the CSD HW block is already set up using
* the Cy_CapSense_CSDSetupWidget() and the sensor port-pin is connected to the CSD
* block using Cy_CapSense_CSDConnectSns().
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDStartSample(cy_stc_capsense_context_t * context)
{
/* Force the LFSR BITS to default before callback, so they can be changed */
context->ptrCommonConfig->ptrCsdBase->SENSE_PERIOD |= CY_CAPSENSE_CSD_SENSE_PERIOD_LFSR_BITS_MSK;
if (NULL != context->ptrInternalContext->ptrSSCallback)
{
context->ptrInternalContext->ptrSSCallback(context->ptrActiveScanSns);
}
/* Precharging Cmod and Csh */
Cy_CapSense_CSDCmodPrecharge(context);
/* Trigger Scan */
Cy_CapSense_CSDTriggerScan(context);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDScanExt
****************************************************************************//**
*
* Starts the CSD conversion on the preconfigured sensor.
*
* \deprecated This function is obsolete and kept for backward compatibility only.
* The Cy_CapSense_ScanExt() function should be used instead.
*
* This function performs scanning of a specific sensor in the widget
* previously initialized using the Cy_CapSense_CSDSetupWidgetExt() function.
*
* This function is called when no scanning is in progress.
* I.e. Cy_CapSense_IsBusy() returns a non-busy status and the widget must
* be preconfigured using Cy_CapSense_CSDSetupWidgetExt() function prior
* to calling this function. Calling this function directly from
* the application program is not recommended. This function is used to
* implement only the user's specific use cases (for example faster execution).
*
* This function does not disconnect sensor GPIOs from CSD HW block at the
* end of a scan, therefore making subsequent scan of the same sensor is faster.
* If sensor needs to be disconnected after the scan,
* Cy_CapSense_CSDDisconnectSns() function can be used.
*
* Calling Cy_CapSense_SetupWidget(), Cy_CapSense_CSDSetupWidget(),
* Cy_CapSense_ScanAllWidgets(), or if Cy_CapSense_RunTuner() returns
* CY_CAPSENSE_STATUS_RESTART_DONE status invalidated initialization
* made by this function.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDScanExt(cy_stc_capsense_context_t * context)
{
cy_stc_capsense_active_scan_sns_t * ptrActive = context->ptrActiveScanSns;
/* Set MFS channel index to 0 */
ptrActive->mfsChannelIndex = 0u;
/* Initialize the Active Context pointer with the CH0 context */
ptrActive->ptrSnsContext = &ptrActive->ptrWdConfig->ptrSnsContext[ptrActive->sensorIndex];
/* Set Start of sensor scan flag */
Cy_CapSense_SetBusyFlags(context);
/* Set scope flag */
ptrActive->scanScope = CY_CAPSENSE_SCAN_SCOPE_SNGL_SNS;
/*
* Configuring of the Sense Clock, the Scan Duration and
* the IDACs with the MFS channel #0 settings if the MFS
* feature is enabled.
* If the MFS feature is disabled, the Sense Clock, the
* Scan Duration and the IDACs are configured only once
* in scope of the Cy_CapSense_CSDSetupWidgetExt() function.
*/
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN)
Cy_CapSense_CSDSetUpIdacs(context);
Cy_CapSense_CSDCalculateScanDuration(context);
Cy_CapSense_CSDConfigClock(context);
#endif
/* Initiate scan */
Cy_CapSense_CSDStartSample(context);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDScan
****************************************************************************//**
*
* This function initiates a scan for the sensors of the widget initialized
* by the Cy_CapSense_CSDSetupWidget() function.
*
* \deprecated This function is obsolete and kept for backward compatibility only.
* The Cy_CapSense_Scan() function should be used instead.
*
* This function does the following tasks:
* 1. Connects the first sensor of the widget.
* 2. Configures the IDAC value.
* 3. Starts scanning for the first sensor in the widget.
*
* This function is called by the Cy_CapSense_Scan() if the given
* widget uses the CSD sensing method.
*
* Calling this function directly from the application program is not
* recommended. This function is used to implement only the user's specific
* use cases (for example faster execution).
*
* This function is called when no scanning is in progress. I.e.
* Cy_CapSense_IsBusy() returns a non-busy status and the widget must be
* preconfigured using the Cy_CapSense_CSDSetupWidget() function prior
* to calling this function.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDScan(cy_stc_capsense_context_t * context)
{
#if ((CY_CAPSENSE_DISABLE == CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN) &&\
((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN) ||\
(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN)))
const cy_stc_capsense_widget_config_t * ptrWdCfg;
#endif
Cy_CapSense_InitActivePtrSns(0u, context);
/* Setup Idac Value */
Cy_CapSense_CSDSetUpIdacs(context);
/*
* Configuring the Sense Clock and the Scan Duration with the settings of the
* widget's Row, if the two-dimension widget is scanned and with the MFS channel #0
* settings, if the MFS feature is enabled.
* If the MFS feature is disabled and the one-dimension widget is scanned,
* the Sense Clock and the Scan Duration will be configured only once in the scope
* of the Cy_CapSense_CSDSetupWidget() function.
*/
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN)
Cy_CapSense_CSDCalculateScanDuration(context);
Cy_CapSense_CSDConfigClock(context);
#elif ((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN) ||\
(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN))
ptrWdCfg = context->ptrActiveScanSns->ptrWdConfig;
if (((uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E == ptrWdCfg->wdType) ||
((uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E == ptrWdCfg->wdType))
{
Cy_CapSense_CSDCalculateScanDuration(context);
Cy_CapSense_CSDConfigClock(context);
}
#endif
Cy_CapSense_CSDConnectSnsExt(context);
context->ptrActiveScanSns->scanScope |= CY_CAPSENSE_SCAN_SCOPE_ALL_SNS_MASK;
/* Set Start of sensor scan flag */
Cy_CapSense_SetBusyFlags(context);
/* Initiate scan */
Cy_CapSense_CSDStartSample(context);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDConnectSnsExt
****************************************************************************//**
*
* Connects a ganged sensor port-pin to the CSD HW block via the AMUX bus.
*
* The function gets the IO configuration registers addresses, their shifts and
* masks from the cy_stc_capsense_pin_config_t object. Based on this data, it
* updates the HSIOM and PC registers.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDConnectSnsExt(cy_stc_capsense_context_t * context)
{
cy_stc_capsense_active_scan_sns_t * ptrActive = context->ptrActiveScanSns;
const cy_stc_capsense_electrode_config_t * ptrActiveSns = ptrActive->ptrEltdConfig;
const cy_stc_capsense_pin_config_t * ptrActivePin = ptrActiveSns->ptrPin;
uint32_t i;
/* Connect all pins of current sensors */
for (i = 0u; i < ptrActiveSns->numPins; i++)
{
Cy_CapSense_CSDConnectSns(ptrActivePin, context);
ptrActivePin++;
}
ptrActive->connectedSnsState = CY_CAPSENSE_SNS_CONNECTED;
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDDisconnectSnsExt
****************************************************************************//**
*
* Disconnects a ganged sensor port-pin from the CSD HW block and AMUX bus.
* Sets the default state of the un-scanned sensor.
*
* The function gets the IO configuration registers addresses, their shifts and
* masks from the cy_stc_capsense_pin_config_t object. Based on this data and
* the Inactive sensor connection parameter, it updates the HSIOM, PC, and DR registers.
* The HSIOM register is updated only when the Inactive sensor connection parameter
* is set to Shield.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDDisconnectSnsExt(cy_stc_capsense_context_t * context)
{
cy_stc_capsense_active_scan_sns_t * ptrActive = context->ptrActiveScanSns;
const cy_stc_capsense_electrode_config_t * ptrActiveSns = ptrActive->ptrEltdConfig;
const cy_stc_capsense_pin_config_t * ptrActivePin = ptrActiveSns->ptrPin;
uint32_t i;
/* Disconnect all pins of current sensors */
for (i = 0u; i < ptrActiveSns->numPins; i++)
{
Cy_CapSense_CSDDisconnectSns(ptrActivePin, context);
ptrActivePin++;
}
ptrActive->connectedSnsState = CY_CAPSENSE_SNS_DISCONNECTED;
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDConnectSns
****************************************************************************//**
*
* Connects port pin to the CSD HW block using AMUX bus.
*
* \deprecated This function is obsolete and kept for backward compatibility only.
* The Cy_CapSense_SetPinState() function should be used instead.
*
* This function can be used to customize the default sensor connection
* by connecting one or more pins to an existing sensor prior to initiating
* scan of the sensor.
*
* The function ignores whether the sensor is a ganged sensor and
* connects only a specified port pin to the CSD HW block. This function can
* only use GPIOs that are already assigned to CAPSENSE™ middleware.
*
* The functions that perform a setup and scan of a sensor/widget do not
* take into account changes in the design made by
* the Cy_CapSense_CSDConnectSns() function, hence all GPIOs connected
* using this function must be disconnected using
* the Cy_CapSense_CSDDisconnectSns() function prior to initializing
* new widgets. Use this function in StartSample
* callback (see the \ref group_capsense_callbacks section for details)
* or with low-level functions that perform a single-sensor scanning.
*
* Scanning should be completed before calling this function.
*
* \param snsAddrPtr
* Specifies the pointer to the cy_stc_capsense_pin_config_t object belonging to
* a sensor that is to be connected to the CSD HW block.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDConnectSns(
const cy_stc_capsense_pin_config_t * snsAddrPtr,
const cy_stc_capsense_context_t * context)
{
(void)context;
Cy_CapSense_SsConfigPinRegisters(snsAddrPtr->pcPtr, (uint32_t)snsAddrPtr->pinNumber, CY_CAPSENSE_CSD_SCAN_PIN_DM, CY_CAPSENSE_HSIOM_SEL_CSD_SENSE);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDDisconnectSns
****************************************************************************//**
*
* Disconnects port pin from the CSD HW block by disconnecting it from AMUX bus.
*
* \deprecated This function is obsolete and kept for backward compatibility only.
* The Cy_CapSense_SetPinState() function should be used instead.
*
* This function can be used to disconnect a sensor connected
* using the Cy_CapSense_CSDConnectSns() function. In addition, this
* function can be used to customize a default sensor connection by
* disconnecting one or more already connected sensors prior to
* initiating a scan of the sensor.
*
* This function works identically to the Cy_CapSense_CSDConnectSns() function
* except it disconnects the specified port pin used by the sensor.
*
* Scanning should be completed before calling this function.
*
* \param snsAddrPtr
* Specifies the pointer to the cy_stc_capsense_pin_config_t object belonging to
* a sensor that should be disconnected from the CSD HW block.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDDisconnectSns(
const cy_stc_capsense_pin_config_t * snsAddrPtr,
const cy_stc_capsense_context_t * context)
{
Cy_CapSense_SsConfigPinRegisters(snsAddrPtr->pcPtr, (uint32_t)snsAddrPtr->pinNumber,
context->ptrInternalContext->csdInactiveSnsDm,
context->ptrInternalContext->csdInactiveSnsHsiom);
Cy_GPIO_Clr(snsAddrPtr->pcPtr, (uint32_t)snsAddrPtr->pinNumber);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CSDClearSensors
****************************************************************************//**
*
* Resets all the CSD sensors to the non-sampling state by sequentially
* disconnecting all the sensors from the Analog MUX bus and putting them to
* an inactive state.
*
* The function goes through all the widgets and updates appropriate bits in
* the IO HSIOM, PC and DR registers depending on the Inactive sensor connection
* parameter. DR register bits are set to zero when the Inactive sensor
* connection is Ground or Hi-Z.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_CSDClearSensors(const cy_stc_capsense_context_t * context)
{