-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcy_capsense_sensing_v3.c
3217 lines (2945 loc) · 130 KB
/
cy_capsense_sensing_v3.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_sensing_v3.c
* \version 3.0
*
* \brief
* This file contains the source of functions common for different scanning
* modes.
*
********************************************************************************
* \copyright
* Copyright 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 <string.h>
#include "cy_syslib.h"
#include "cy_sysclk.h"
#include "cy_gpio.h"
#include "cy_device_headers.h"
#include "cy_capsense_sensing_v3.h"
#include "cy_capsense_generator_v3.h"
#include "cy_capsense_common.h"
#include "cy_capsense_structure.h"
#include "cy_capsense_processing.h"
#if (CY_CAPSENSE_PLATFORM_BLOCK_MSCV3)
#include "cy_msc.h"
#include "cy_dmac.h"
#endif
#if (defined(CY_IP_M0S8MSCV3))
/*******************************************************************************
* Local definition
*******************************************************************************/
#define CY_CAPSENSE_CSD_CCOMP_CALC_DIV (4u * CY_CAPSENSE_PERCENTAGE_100)
#define CY_CAPSENSE_CSX_CCOMP_CALC_DIV (8u * CY_CAPSENSE_PERCENTAGE_100)
/*******************************************************************************
* Constants
*******************************************************************************/
const cy_stc_msc_base_config_t cy_capsense_mscCfg = CY_CAPSENSE_MSC_CONFIG_DEFAULT;
/******************************************************************************/
/** \cond SECTION_CAPSENSE_INTERNAL */
/** \addtogroup group_capsense_internal *//** \{ */
/******************************************************************************/
static void Cy_CapSense_SetModClkDivider(
const cy_stc_capsense_context_t * context);
/** \} \endcond */
/*******************************************************************************
* Function Name: Cy_CapSense_ScanAllSlots
****************************************************************************//**
*
* Initiates the non-blocking scan of all slots. Scanning is
* initiated only if no scan is in progress. Scan finishing can be
* checked by the Cy_CapSense_IsBusy() function.
*
* This function initiates a scan only for the first slot for all channels
* and then exits. Scans for the remaining slots in the Interrupt-driven scan mode
* are initiated
* in the interrupt service routine (part of middleware) trigged at the end
* of each scan completion for each channel. If the syncMode field in the
* cy_stc_capsense_common_config_t structure is set to CY_CAPSENSE_SYNC_MODE_OFF,
* then the next slot scan for the channel with the fired interrupt,
* will start regardless of the another channel readiness for the next scan.
* If the syncMode field is set to CY_CAPSENSE_SYNC_INTERNAL (for single-chip projects)
* or to CY_CAPSENSE_SYNC_EXTERNAL (for multi-chip projects),
* then the next slot scan for the channel with the fired interrupt,
* will start in lockstep with another channels after they all are ready
* for the next scan (the next scan configuration is loaded into the channel MSC HW block).
* Scans for the remaining slots in CS-DMA scan mode are initiated
* by DMAC trigged at the end
* of each scan completion for each channel. The channel scan synchronization is
* performed as in Interrupt-driven scan mode. After all slots are scanned,
* the FRAME interrupt is fired and the interrupt service routine (part of middleware)
* updates the busy status.
*
* The status of the current scan should be
* checked using the Cy_CapSense_IsBusy() function, where there are separate busy bits for each
* channel and the application program waits until all scans are finished
* prior to starting a next scan by using this function.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The operation is performed successfully.
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid.
* - CY_CAPSENSE_STATUS_HW_BUSY - The HW is busy with the previous scan.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_ScanAllSlots(
cy_stc_capsense_context_t * context)
{
cy_capsense_status_t capStatus = CY_CAPSENSE_STATUS_BAD_PARAM;
if (NULL != context)
{
capStatus = Cy_CapSense_ScanSlots(0u, context->ptrCommonConfig->numSlots, context);
}
return (capStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_ScanSlots
****************************************************************************//**
*
* Initiates the non-blocking scan of specified slots. Scanning is
* initiated only if no scan is in progress. Scan finishing can be
* checked by the Cy_CapSense_IsBusy() function.
*
* This function initiates a scan only for the first specified slot for all channels
* and then exits. Scans for the remaining slots in the interrupt-driven scan mode
* are initiated
* in the interrupt service routine (part of middleware) trigged at the end
* of each scan completion for each channel. If the syncMode field in the
* cy_stc_capsense_common_config_t structure is set to CY_CAPSENSE_SYNC_MODE_OFF,
* then the next slot scan for the channel with the fired interrupt,
* will start regardless of the another channel readiness for the next scan.
* If the syncMode field is set to CY_CAPSENSE_SYNC_INTERNAL (for single-chip projects)
* or to CY_CAPSENSE_SYNC_EXTERNAL (for multi-chip projects),
* then the next slot scan for the channel with the fired interrupt,
* will start in lockstep with another channels after they all are ready
* for the next scan.
* The scan for the remaining slots in CS-DMA scan mode are initiated
* by DMAC trigged at the end
* of each scan completion for each channel. The channel scan synchronization is
* performed as in Interrupt-driven scan mode. After all slots are scanned,
* the FRAME interrupt is fired and the interrupt service routine (part of middleware)
* updates the busy status.
*
* \param startSlotId
* The slot ID scan will be started from.
*
* \param numberSlots
* The number of slots will be scanned.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The operation is performed successfully.
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid.
* - CY_CAPSENSE_STATUS_HW_BUSY - The HW is busy with the previous scan.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_ScanSlots(
uint32_t startSlotId,
uint32_t numberSlots,
cy_stc_capsense_context_t * context)
{
uint32_t wdIndex;
uint32_t snsIndex;
uint32_t slotValue;
uint32_t curChIndex;
uint32_t curSlotIndex;
const cy_stc_capsense_common_config_t * ptrCommonCfg = context->ptrCommonConfig;
uint32_t * ptrSensorFrame;
uint32_t scanSlotIndexValid;
uint32_t lastSlot = startSlotId + numberSlots - 1u;
uint32_t sensorFrame[CY_MSC_6_SNS_REGS] = {0u, 0u, 0u, 0u, 0u, 0u};
cy_capsense_status_t capStatus = CY_CAPSENSE_STATUS_BAD_PARAM;
if ((NULL != context) && (0u != numberSlots))
{
if (ptrCommonCfg->numSlots > lastSlot)
{
if (CY_CAPSENSE_NOT_BUSY != (context->ptrCommonContext->status & CY_CAPSENSE_BUSY_ALL_CH_MASK))
{
/* Previous widget is being scanned. Return error. */
capStatus = CY_CAPSENSE_STATUS_HW_BUSY;
}
else
{
context->ptrInternalContext->slotIndex = (uint16_t)startSlotId;
context->ptrInternalContext->currentSlotIndex = (uint16_t)startSlotId;
context->ptrInternalContext->endSlotIndex = (uint16_t)lastSlot;
for (curChIndex = 0u; curChIndex < ptrCommonCfg->numChannels; curChIndex++)
{
context->ptrActiveScanSns[curChIndex].currentChannelSlotIndex = startSlotId;
}
/* Configure DMA resources for each channel */
if (CY_CAPSENSE_SCAN_MODE_DMA_DRIVEN == ptrCommonCfg->scanningMode)
{
for (curChIndex = 0u; curChIndex < ptrCommonCfg->numChannels; curChIndex++)
{
Cy_CapSense_ConfigureDmaResource(curChIndex, context);
}
}
if (CY_CAPSENSE_SCAN_MODE_INT_DRIVEN == ptrCommonCfg->scanningMode)
{
/* Initiates the frame start for each channel in interrupt driven scan mode */
for (curChIndex = 0u; curChIndex < ptrCommonCfg->numChannels; curChIndex++)
{
Cy_MSC_WriteReg(ptrCommonCfg->ptrMscChConfig[curChIndex].ptrMscBase,
CY_MSC_REG_OFFSET_FRAME_CMD, MSC_FRAME_CMD_START_FRAME_Msk);
}
}
/* Initialize all enabled MSC channels for scan */
for (curChIndex = 0u; curChIndex < ptrCommonCfg->numChannels; curChIndex++)
{
Cy_CapSense_SetBusyFlags(curChIndex, context);
curSlotIndex = startSlotId + (curChIndex + ptrCommonCfg->channelOffset) * ptrCommonCfg->numSlots;
slotValue = context->ptrScanSlots[curSlotIndex].wdId;
if(CY_CAPSENSE_SLOT_EMPTY == slotValue)
{
/* CY_ID#XXXX */
scanSlotIndexValid = curSlotIndex;
if(ptrCommonCfg->numChannels > 1u)
{
if(curSlotIndex < ptrCommonCfg->numSlots)
{
scanSlotIndexValid += ptrCommonCfg->numSlots;
}
else
{
scanSlotIndexValid -= ptrCommonCfg->numSlots;
}
}
else
{
scanSlotIndexValid = curSlotIndex;
}
}
else if (CY_CAPSENSE_SLOT_SHIELD_ONLY <= slotValue)
{
scanSlotIndexValid = (context->ptrScanSlots[curSlotIndex].snsId * ptrCommonCfg->numSlots) +
(curSlotIndex % ptrCommonCfg->numSlots);
}
else
{
scanSlotIndexValid = curSlotIndex;
}
/* Initializes for each channel the active sensor structure for the current sensor */
wdIndex = context->ptrScanSlots[scanSlotIndexValid].wdId;
snsIndex = context->ptrScanSlots[scanSlotIndexValid].snsId;
Cy_CapSense_InitActivePtr(curChIndex, wdIndex, snsIndex, context);
if (CY_CAPSENSE_AMUX_SENSOR_CONNECTION_METHOD == ptrCommonCfg->sensorConnection)
{
Cy_CapSense_ConfigureAnalogMuxResource(curChIndex, context);
/* Getting the sensor frame configuration */
ptrSensorFrame = &sensorFrame[0u];
Cy_CapSense_GenerateSensorConfig(curChIndex, curSlotIndex, ptrSensorFrame, context);
/* Configure the last slot */
if (1u == numberSlots)
{
sensorFrame[CY_CAPSENSE_SNS_CTL_INDEX] |= MSC_SNS_CTL_LAST_Msk;
}
if (CY_MSC_5_SNS_REGS == context->ptrInternalContext->snsConfigSize)
{
ptrSensorFrame++;
}
}
else
{
/* Configure the last slot for each channel */
context->ptrSensorFrameContext[(lastSlot + 1u +
(curChIndex + ptrCommonCfg->channelOffset) * ptrCommonCfg->numSlots) *
context->ptrInternalContext->snsConfigSize - 1u] |= MSC_SNS_CTL_LAST_Msk;
ptrSensorFrame = &context->ptrSensorFrameContext[(startSlotId +
(curChIndex + ptrCommonCfg->channelOffset) * ptrCommonCfg->numSlots) *
context->ptrInternalContext->snsConfigSize];
}
if ((NULL != context->ptrInternalContext->ptrSSCallback) &&
((curChIndex + ptrCommonCfg->channelOffset) == (ptrCommonCfg->numChannels - 1u)))
{
context->ptrInternalContext->ptrSSCallback((cy_stc_active_scan_sns_t *)&context->ptrActiveScanSns[0u]);
}
if (CY_CAPSENSE_SCAN_MODE_INT_DRIVEN == ptrCommonCfg->scanningMode)
{
Cy_MSC_ConfigureScan(ptrCommonCfg->ptrMscChConfig[curChIndex].ptrMscBase,
context->ptrInternalContext->snsConfigSize,
ptrSensorFrame);
}
}
if (CY_CAPSENSE_SCAN_MODE_DMA_DRIVEN == ptrCommonCfg->scanningMode)
{
/* Initiates the frame start for each channel in DMA driven scan mode */
for (curChIndex = 0u; curChIndex < ptrCommonCfg->numChannels; curChIndex++)
{
Cy_MSC_WriteReg(ptrCommonCfg->ptrMscChConfig[curChIndex].ptrMscBase,
CY_MSC_REG_OFFSET_FRAME_CMD, MSC_FRAME_CMD_START_FRAME_Msk);
}
}
capStatus = CY_CAPSENSE_STATUS_SUCCESS;
}
}
}
return (capStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_ScanAllWidgets
****************************************************************************//**
*
* Initiates the non-blocking scan for all widgets/sensors. Scanning is
* initiated only if no scan is in progress. Scan finishing can be
* checked by the Cy_CapSense_IsBusy() function.
*
* The function is the wrapper for the Cy_CapSense_ScanAllSlots() function
* to provide the backward compatibility.
*
* \note
* The function operates only in single-channel projects for a while.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The operation is performed successfully.
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid.
* - CY_CAPSENSE_STATUS_HW_BUSY - The HW is busy with the previous scan.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_ScanAllWidgets(cy_stc_capsense_context_t * context)
{
cy_capsense_status_t capStatus = CY_CAPSENSE_STATUS_BAD_PARAM;
if (NULL != context)
{
if (1u >= context->ptrCommonConfig->numChannels)
{
capStatus = Cy_CapSense_ScanSlots(0u, context->ptrCommonConfig->numSlots, context);
}
}
return (capStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_ScanWidget
****************************************************************************//**
*
* Initiates the scanning of all sensors in the widget. Scanning is
* initiated only if no scan is in progress. Scan finishing can be
* checked by the Cy_CapSense_IsBusy() function.
*
* The function uses the Cy_CapSense_ScanSlots() function with the parameters of
* startSlotId and numberSlots retrieved from the firstSlotId and numSlots
* fields of the cy_stc_capsense_widget_config_t structure.
*
* \note
* The function operates only in single-channel projects for a while.
* \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 operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The operation is performed successfully.
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid.
* - CY_CAPSENSE_STATUS_HW_BUSY - The HW is busy with the previous scan.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_ScanWidget(
uint32_t widgetId,
cy_stc_capsense_context_t * context)
{
cy_capsense_status_t capStatus = CY_CAPSENSE_STATUS_BAD_PARAM;
if (NULL != context)
{
if (1u >= context->ptrCommonConfig->numChannels)
{
if (widgetId < context->ptrCommonConfig->numWd)
{
capStatus = Cy_CapSense_ScanSlots(context->ptrWdConfig[widgetId].firstSlotId,
context->ptrWdConfig[widgetId].numSlots, context);
}
}
}
return (capStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_ScanSensor
****************************************************************************//**
*
* Initiates the scanning of the selected sensor in the widget. Scanning is
* initiated only if no scan is in progress. Scan finishing can be
* checked by the Cy_CapSense_IsBusy() function.
*
* \note
* The function is not available for a while.
* \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 operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The operation is performed successfully.
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid.
* - CY_CAPSENSE_STATUS_HW_BUSY - The HW is busy with the previous scan.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_ScanSensor(
uint32_t widgetId,
uint32_t sensorId,
cy_stc_capsense_context_t * context)
{
cy_capsense_status_t capStatus = CY_CAPSENSE_STATUS_BAD_PARAM;
if (NULL != context)
{
if (1u >= context->ptrCommonConfig->numChannels)
{
if (widgetId < context->ptrCommonConfig->numWd)
{
if (sensorId < context->ptrWdConfig[widgetId].numSns)
{
capStatus = Cy_CapSense_ScanSlots((sensorId + context->ptrWdConfig->firstSlotId), 1u, context);
}
}
}
}
return (capStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CalibrateAllSlots
****************************************************************************//**
*
* Executes the CapDAC calibration for all the sensors in all widgets in
* the project to defined target values.
*
* This function detects the sensing method used by each widget and performs
* a successive approximation search algorithm to find the appropriate reference
* and compensation CapDAC (if enabled) values for all sensors to make
* sensor raw counts closest to the defined value levels.
*
* This function can be used only if the Enable CapDAC auto-calibration parameter
* is enabled for CSD and/or CSX widgets.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The operation is performed successfully.
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid.
* - CY_CAPSENSE_STATUS_CALIBRATION_FAIL - The calibration failed if software
* watchdog timeout occurred
* during any calibration scan,
* the scan was not completed, or
* resulted raw counts
* are outside the limits.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_CalibrateAllSlots(
cy_stc_capsense_context_t * context)
{
cy_capsense_status_t calibStatus = CY_CAPSENSE_STATUS_BAD_PARAM;
uint32_t curSlotIndex;
uint32_t curWdIndex;
uint32_t csdTarget;
uint32_t csxTarget;
if ((NULL != context) && (0u != context->ptrCommonConfig->numSlots))
{
calibStatus = CY_CAPSENSE_STATUS_SUCCESS;
if (((context->ptrCommonConfig->csdEn == CY_CAPSENSE_ENABLE) &&
(context->ptrCommonConfig->csdCdacAutocalEn == CY_CAPSENSE_ENABLE)) ||
((context->ptrCommonConfig->csxEn == CY_CAPSENSE_ENABLE) &&
(context->ptrCommonConfig->csxCdacAutocalEn == CY_CAPSENSE_ENABLE)))
{
context->ptrCommonContext->status |= CY_CAPSENSE_BUSY_CALIBRATION;
csdTarget = context->ptrCommonConfig->csdRawTarget;
csxTarget = context->ptrCommonConfig->csxRawTarget;
for (curSlotIndex = 0u; curSlotIndex < context->ptrCommonConfig->numSlots; curSlotIndex++)
{
/* Calibrate all sensors in slot */
calibStatus = Cy_CapSense_CalibrateSlot(curSlotIndex, csdTarget, csxTarget, context);
if (CY_CAPSENSE_STATUS_SUCCESS != calibStatus)
{
calibStatus = CY_CAPSENSE_STATUS_CALIBRATION_FAIL;
break;
}
}
if (CY_CAPSENSE_STATUS_SUCCESS == calibStatus)
{
context->ptrCommonContext->status |= CY_CAPSENSE_BUSY_VERIFY_CALIBRATION;
for (curWdIndex = 0u; curWdIndex < context->ptrCommonConfig->numWd; curWdIndex++)
{
/* Normalize all widgets */
calibStatus |= Cy_CapSense_NormalizeCdac(curWdIndex, context);;
}
if ((context->ptrCommonConfig->csdCdacCompEn == CY_CAPSENSE_ENABLE) ||
(context->ptrCommonConfig->csxCdacCompEn == CY_CAPSENSE_ENABLE))
{
for (curSlotIndex = 0u; curSlotIndex < context->ptrCommonConfig->numSlots; curSlotIndex++)
{
/* Calibrate all sensors in slot */
calibStatus = Cy_CapSense_CalibrateSlot(curSlotIndex, csdTarget, csxTarget, context);
if (CY_CAPSENSE_STATUS_SUCCESS != calibStatus)
{
calibStatus = CY_CAPSENSE_STATUS_CALIBRATION_FAIL;
break;
}
}
}
for (curWdIndex = 0u; curWdIndex < context->ptrCommonConfig->numWd; curWdIndex++)
{
/* Check calibration result */
calibStatus |= Cy_CapSense_VerifyCalibration(curWdIndex, csdTarget, csxTarget, context);
}
if (CY_CAPSENSE_STATUS_SUCCESS != calibStatus)
{
calibStatus = CY_CAPSENSE_STATUS_CALIBRATION_FAIL;
}
context->ptrCommonContext->status &= (uint32_t)~CY_CAPSENSE_BUSY_VERIFY_CALIBRATION;
}
context->ptrCommonContext->status &= (uint32_t)~CY_CAPSENSE_BUSY_CALIBRATION;
}
}
return (calibStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CalibrateAllWidgets
****************************************************************************//**
*
* Calibrates CapDACs for all widgets.
*
* The function is the wrapper for the Cy_CapSense_CalibrateAllSlots() function
* to provide the backward compatibility.
*
* \note
* The function operates only in single-channel projects for a while.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The operation is performed successfully.
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid.
* - CY_CAPSENSE_STATUS_CALIBRATION_FAIL - The calibration failed if software
* watchdog timeout occurred
* during any calibration scan,
* the scan was not completed, or
* resulted raw counts
* are outside the limits.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_CalibrateAllWidgets(
cy_stc_capsense_context_t * context)
{
cy_capsense_status_t calibStatus = CY_CAPSENSE_STATUS_BAD_PARAM;
if (NULL != context)
{
if (1u >= context->ptrCommonConfig->numChannels)
{
calibStatus = Cy_CapSense_CalibrateAllSlots(context);
}
}
return (calibStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_CalibrateWidget
****************************************************************************//**
*
* Executes the CapDAC calibration for all the sensors in the specified widget
* to the default target value.
*
* This function performs exactly the same tasks as
* Cy_CapSense_CalibrateAllWidgets(), but only for a specified widget.
*
* \note
* The function operates only in single-channel projects for a while.
*
* \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 operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The operation is performed successfully.
* - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid.
* - CY_CAPSENSE_STATUS_CALIBRATION_FAIL - The calibration failed if software
* watchdog timeout occurred
* during any calibration scan,
* the scan was not completed, or
* resulted raw counts
* are outside the limits.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_CalibrateWidget(
uint32_t widgetId,
cy_stc_capsense_context_t * context)
{
cy_capsense_status_t calibStatus = CY_CAPSENSE_STATUS_BAD_PARAM;
uint32_t curSlotIndex;
uint32_t csdTarget;
uint32_t csxTarget;
uint32_t firstSlotIdx;
uint32_t slotsNum;
if (NULL != context)
{
if (1u == context->ptrCommonConfig->numChannels)
{
if (widgetId < context->ptrCommonConfig->numWd)
{
/* Add check for AutocalEn at this widget */
context->ptrCommonContext->status |= CY_CAPSENSE_BUSY_CALIBRATION;
csdTarget = context->ptrCommonConfig->csdRawTarget;
csxTarget = context->ptrCommonConfig->csxRawTarget;
firstSlotIdx = context->ptrWdConfig[widgetId].firstSlotId;
slotsNum = firstSlotIdx + context->ptrWdConfig[widgetId].numSlots;
for (curSlotIndex = firstSlotIdx; curSlotIndex < slotsNum; curSlotIndex++)
{
/* Calibrate all sensors in slot */
calibStatus = Cy_CapSense_CalibrateSlot(curSlotIndex, csdTarget, csxTarget, context);
if (CY_CAPSENSE_STATUS_SUCCESS != calibStatus)
{
break;
}
}
/* Normalize all widgets */
calibStatus |= Cy_CapSense_NormalizeCdac(widgetId, context);
context->ptrCommonContext->status &= (uint32_t)~CY_CAPSENSE_BUSY_CALIBRATION;
/* Check calibration result */
calibStatus |= Cy_CapSense_VerifyCalibration(widgetId, csdTarget, csxTarget, context);
}
}
}
return (calibStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_WatchdogCyclesNum
****************************************************************************//**
*
* Converts the specified time into number of CPU cycles.
*
* \param desiredTimeUs
* The time (delay) specified in us.
*
* \param cpuFreqMHz
* The CPU frequency in MHz.
*
* \param cyclesPerLoop
* The number of cycles per a loop.
*
*******************************************************************************/
uint32_t Cy_CapSense_WatchdogCyclesNum(
uint32_t desiredTimeUs,
uint32_t cpuFreqMHz,
uint32_t cyclesPerLoop)
{
uint32_t retVal;
if(0uL != cyclesPerLoop)
{
retVal = (desiredTimeUs * cpuFreqMHz) / cyclesPerLoop;
}
else
{
retVal = 0xFFFFFFFFuL;
}
return(retVal);
}
/*******************************************************************************
* Function Name: Cy_CapSense_SsInitialize
****************************************************************************//**
*
* Performs the hardware and firmware initialization required for proper operation
* of the CapSense middleware. This function is called from
* the Cy_CapSense_Init() prior to calling any other function of the middleware.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \return status
* Returns the status of the operation:
* - CY_CAPSENSE_STATUS_SUCCESS - The operation is performed successfully.
*
*******************************************************************************/
cy_capsense_status_t Cy_CapSense_SsInitialize(cy_stc_capsense_context_t * context)
{
uint32_t curChIndex;
cy_en_msc_status_t mscStatus;
cy_capsense_status_t capStatus = CY_CAPSENSE_STATUS_BAD_PARAM;
const cy_stc_capsense_common_config_t * ptrCommonCfg = context->ptrCommonConfig;
const cy_stc_capsense_widget_config_t * ptrWdCfg;
uint32_t i;
/* Configure the modulator clock and then start it */
Cy_CapSense_SetModClkDivider(context);
Cy_CapSense_SetIOsInDefaultState(context);
Cy_CapSense_SetShieldPinsInDefaultState(context);
/* Configuring the CIC filter BIT_FORMAT to UNSIGNED */
context->ptrInternalContext->filterBitFormat = 0u;
/* Initialize all enabled MSC channels for scan */
for (curChIndex = 0u; curChIndex < context->ptrCommonConfig->numChannels; curChIndex++)
{
/* Generate base frame configurations for all enabled MSC channels */
capStatus = Cy_CapSense_GenerateBaseConfig(curChIndex, context);
if (CY_CAPSENSE_STATUS_SUCCESS != capStatus)
{
break;
}
/* Reset the sense method of all channels */
context->ptrActiveScanSns[curChIndex].currentSenseMethod = CY_CAPSENSE_SENSING_METHOD_UNDEFINED;
Cy_CapSense_SetCmodInDefaultState(curChIndex, context);
/* Generates sensor frame configuration */
if (CY_CAPSENSE_CTRLMUX_SENSOR_CONNECTION_METHOD == ptrCommonCfg->sensorConnection)
{
Cy_CapSense_GenerateAllSensorConfig(curChIndex,
&context->ptrSensorFrameContext[(context->ptrCommonConfig->numSlots *
(curChIndex + ptrCommonCfg->channelOffset)) *
context->ptrInternalContext->snsConfigSize], context);
}
}
/* Assign the ISR for scan */
context->ptrInternalContext->ptrISRCallback = &Cy_CapSense_ScanISR;
/* Configure non-CapSense HW Resources */
if (CY_CAPSENSE_SCAN_MODE_DMA_DRIVEN == ptrCommonCfg->scanningMode)
{
Cy_CapSense_InitializeDmaResource(context);
}
else if (CY_CAPSENSE_AMUX_SENSOR_CONNECTION_METHOD == ptrCommonCfg->sensorConnection)
{
context->ptrInternalContext->csdInactiveSnsHsiom = HSIOM_SEL_GPIO;
switch (ptrCommonCfg->csdInactiveSnsConnection)
{
case CY_CAPSENSE_SNS_CONNECTION_HIGHZ:
context->ptrInternalContext->csdInactiveSnsDm = CY_GPIO_DM_ANALOG;
break;
case CY_CAPSENSE_SNS_CONNECTION_SHIELD:
context->ptrInternalContext->csdInactiveSnsHsiom = HSIOM_SEL_CSD_SHIELD;
if (CY_CAPSENSE_SHIELD_ACTIVE == context->ptrCommonConfig->csdShieldMode)
{
context->ptrInternalContext->csdInactiveSnsDm = CY_GPIO_DM_ANALOG;
}
else
{
context->ptrInternalContext->csdInactiveSnsDm = CY_GPIO_DM_STRONG;
}
break;
default:
/* CY_CAPSENSE_SNS_CONNECTION_GROUND */
context->ptrInternalContext->csdInactiveSnsDm = CY_GPIO_DM_STRONG_IN_OFF;
break;
}
}
else
{
/* Interrupt Driven mode has nothing specific */
}
/* Find maximum raw count for each widget */
ptrWdCfg = &context->ptrWdConfig[0u];
for (i = 0u; i < context->ptrCommonConfig->numWd; i++)
{
if (CY_CAPSENSE_CIC2_FILTER == context->ptrCommonConfig->cicFilterMode)
{
if(CY_CAPSENSE_SCAN_MODE_DMA_DRIVEN == context->ptrCommonConfig->scanningMode)
{
ptrWdCfg->ptrWdContext->maxRawCount = ptrWdCfg->ptrWdContext->cicRate;
ptrWdCfg->ptrWdContext->maxRawCount *= ptrWdCfg->ptrWdContext->cicRate;
if(0u != context->ptrInternalContext->filterBitFormat)
{
ptrWdCfg->ptrWdContext->maxRawCount <<= 1u;
}
ptrWdCfg->ptrWdContext->sigPFC = ptrWdCfg->ptrWdContext->maxRawCount;
}
else
{
ptrWdCfg->ptrWdContext->maxRawCount = Cy_CapSense_GetMaxRawCIC2(
ptrWdCfg->numChopCycles,
ptrWdCfg->ptrWdContext->numSubConversions,
ptrWdCfg->ptrWdContext->snsClk,
ptrWdCfg->ptrWdContext->cicRate);
if((CY_CAPSENSE_CSD_GROUP == ptrWdCfg->senseGroup) &&
((CY_CAPSENSE_WD_MATRIX_BUTTON_E == ptrWdCfg->wdType) ||
(CY_CAPSENSE_WD_TOUCHPAD_E == ptrWdCfg->wdType)))
{
ptrWdCfg->ptrWdContext->sigPFC = Cy_CapSense_GetMaxRawCIC2(
ptrWdCfg->numChopCycles,
ptrWdCfg->ptrWdContext->numSubConversions,
ptrWdCfg->ptrWdContext->rowSnsClk,
ptrWdCfg->ptrWdContext->cicRate);
}
}
}
else
{
ptrWdCfg->ptrWdContext->maxRawCount = Cy_CapSense_GetMaxRawCIC1(
ptrWdCfg->numChopCycles,
ptrWdCfg->ptrWdContext->numSubConversions,
ptrWdCfg->ptrWdContext->snsClk,
context->ptrCommonConfig->numEpiCycles);
if((CY_CAPSENSE_CSD_GROUP == ptrWdCfg->senseGroup) &&
((CY_CAPSENSE_WD_MATRIX_BUTTON_E == ptrWdCfg->wdType) ||
(CY_CAPSENSE_WD_TOUCHPAD_E == ptrWdCfg->wdType)))
{
ptrWdCfg->ptrWdContext->sigPFC = Cy_CapSense_GetMaxRawCIC1(
ptrWdCfg->numChopCycles,
ptrWdCfg->ptrWdContext->numSubConversions,
ptrWdCfg->ptrWdContext->rowSnsClk,
context->ptrCommonConfig->numEpiCycles);
}
}
ptrWdCfg++;
}
/* Call user's callback function if it is registered */
if (NULL != context->ptrInternalContext->ptrEODsInitCallback)
{
context->ptrInternalContext->ptrEODsInitCallback(context);
}
if (CY_CAPSENSE_STATUS_SUCCESS == capStatus)
{
for (curChIndex = 0u; curChIndex < context->ptrCommonConfig->numChannels; curChIndex++)
{
/* MSCv3 IP Block Base Register Configuration */
mscStatus = Cy_MSC_Configure(ptrCommonCfg->ptrMscChConfig[curChIndex].ptrMscBase,
&context->ptrBaseFrameContext[curChIndex],
CY_MSC_CAPSENSE_KEY,
ptrCommonCfg->ptrMscChConfig[curChIndex].ptrMscContext);
if (CY_MSC_SUCCESS != mscStatus)
{
capStatus = CY_CAPSENSE_STATUS_HW_BUSY;
break;
}
/* Clear all pending interrupts of the MSC HW block */
Cy_MSC_WriteReg(ptrCommonCfg->ptrMscChConfig[curChIndex].ptrMscBase, CY_MSC_REG_OFFSET_INTR,
CY_CAPSENSE_MSC_INTR_ALL_MSK);
(void)Cy_MSC_ReadReg(ptrCommonCfg->ptrMscChConfig[curChIndex].ptrMscBase, CY_MSC_REG_OFFSET_INTR);
}
}
return (capStatus);
}
/*******************************************************************************
* Function Name: Cy_CapSense_InterruptHandler
****************************************************************************//**
*
* Implements interrupt service routine for CapSense Middleware.
*
* The CSD / MSC HW block generates an interrupt at end of every sensor scan.
* The CapSense middleware uses this interrupt to implement a
* non-blocking sensor scan method, in which only the first sensor scan is
* initiated by the application program and subsequent sensor scans are
* initiated in the interrupt service routine as soon as the current scan
* is completed. The above stated interrupt service routine is implemented
* as a part of the CapSense middleware.
*
* The CapSense middleware does not initialize or modify the priority
* of interrupts. For the operation of middleware, the application program
* must configure MSC interrupt and assign interrupt vector to
* the Cy_CapSense_InterruptHandler() function. Refer to function
* usage example for details.
*
* Update doc including snippets for CSD & MSC around the MW.
*
* \param base
* The pointer to the base register address of the CSD HW block. A macro for
* the pointer can be found in cycfg_peripherals.h file defined as
* \<Csd_Personality_Name\>_HW. If no name is specified then the default name
* is used csd_\<Block_Number\>_csd_\<Block_Number\>_HW.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
* \note
* The calls of the Start Sample and End Of Scan callbacks
* (see the \ref group_capsense_callbacks section for details) are the part of the
* Cy_CapSense_InterruptHandler() routine and they lengthen its execution. These
* callbacks will lengthen the CSD ISR execution in case of a direct call of the
* Cy_CapSense_InterruptHandler() function from a CSD ISR.
*
* \funcusage
*
* An example of the ISR initialization:
*
* The CapSense_ISR_cfg variable should be declared by the application
* program according to the examples below:<br>
* For Core CM0+:
* \snippet capsense/snippet/main.c snippet_m0p_capsense_interrupt_source_declaration
*
* The CapSense interrupt handler should be defined by the application program
* according to the example below:
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_IntHandler
*
* Then, the application program configures and enables the interrupt
* for each MSC HW block between calls of the Cy_CapSense_Init() and
* Cy_CapSense_Enable() functions:
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_Initialization
*
* 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 name msc_\<Block_Number\>_msc_\<Block_Number\>_HW is used.
*
*******************************************************************************/
void Cy_CapSense_InterruptHandler(const MSC_Type * base, cy_stc_capsense_context_t * context)
{
(void)base;
context->ptrInternalContext->ptrISRCallback((void *)context);
}
/*******************************************************************************
* Function Name: Cy_CapSense_ConnectSensor
****************************************************************************//**
*
* Connects a sensor to the specified channel MSC HW block.
*
* The function checks the widget type and performs connection of all sensor
* electrodes (sns for CSD widgets and Tx/Rx for CSX ones) including ganged.
* The sensor and the current slot are specified in the cy_stc_active_scan_sns_t structure.
*
* \param chIndex
* The channel index.
*
* \param context
* The pointer to the CapSense context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/