-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcy_csdadc.c
2520 lines (2281 loc) · 102 KB
/
cy_csdadc.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_csdadc.c
* \version 2.10
*
* \brief
* This file provides the ADC function implementation of the CSD HW block.
*
********************************************************************************
* \copyright
* Copyright 2018-2022, 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 "cy_device_headers.h"
#include "cy_syslib.h"
#include "cy_sysclk.h"
#include "cy_csdadc.h"
#include "cy_gpio.h"
#include "cy_csd.h"
#if (defined(CY_IP_MXCSDV2) || defined(CY_IP_M0S8CSDV2))
/*******************************************************************************
* Function Prototypes - internal functions
*******************************************************************************/
/**
* \cond SECTION_CAPSENSE_INTERNAL
* \addtogroup group_capsense_internal
* \{
*/
static void Cy_CSDADC_ClearChannels(
cy_stc_csdadc_context_t * context);
static void Cy_CSDADC_SetAdcChannel(
uint32_t chId,
uint32_t state,
const cy_stc_csdadc_context_t * context);
static void Cy_CSDADC_DsInitialize(
const cy_stc_csdadc_config_t * config,
cy_stc_csdadc_context_t * context);
static void Cy_CSDADC_Configure(
const cy_stc_csdadc_context_t * context);
static void Cy_CSDADC_SetClkDivider(
const cy_stc_csdadc_context_t * context);
static void Cy_CSDADC_StartFSM(
uint32_t measureMode,
const cy_stc_csdadc_context_t * context);
static uint8_t Cy_CSDADC_GetNextCh(
uint8_t currChId,
const cy_stc_csdadc_context_t * context);
static uint32_t Cy_CSDADC_StartAndWait(
uint32_t measureMode,
const cy_stc_csdadc_context_t * context);
/** \}
* \endcond */
/*******************************************************************************
* Local definition
*******************************************************************************/
/* Definitions for the init bit (bit 0) of the CSDADC status byte */
#define CY_CSDADC_INIT_NEEDED (0u)
#define CY_CSDADC_INIT_DONE (1u)
#define CY_CSDADC_INIT_MASK (0x01u)
/* Definitions for the conversion mode bit (bit 1) of the CSDADC status byte */
#define CY_CSDADC_CONV_MODE_BIT_POS (1u)
#define CY_CSDADC_CONV_MODE_MASK (0x02u)
/* Definitions for the busy bit (bit 2) of the CSDADC status byte */
#define CY_CSDADC_STATUS_BUSY_MASK (0x04u)
/* Definitions for the overflow bit (bit 3) of the CSDADC status byte */
#define CY_CSDADC_OVERFLOW_MASK (0x08u)
/* Definitions for the stop bits (bit 8 - 9) of the CSDADC status byte */
#define CY_CSDADC_STOP_BITS_POS (8u)
#define CY_CSDADC_STOP_BITS_MASK (0x300u)
#define CY_CSDADC_RES_8_MAX_VAL ((uint32_t)(1uL << 8u) - 1u)
#define CY_CSDADC_RES_10_MAX_VAL ((uint32_t)(1uL << 10u) - 1u)
#define CY_CSDADC_RES_8_PLUS_1_MAX_VAL (511u)
#define CY_CSDADC_RES_10_PLUS_1_MAX_VAL (2047u)
#define CY_CSDADC_CAL_WATCHDOG_CYCLES_NUM (0x0000FFFFu)
#define CY_CSDADC_CHAN_DISCONNECT (0u)
#define CY_CSDADC_CHAN_CONNECT (1u)
/* Cref common capacity in fF (can vary up to 25% for different devices) */
#define CY_CSDADC_CREF (21500u)
#define CY_CSDADC_MEGA (1000000u)
/* IdacB Leg3 LSB current in pA */
#define CY_CSDADC_IDAC_LSB (37500u)
#define CY_CSDADC_IDAC_MAX (127u)
#define CY_CSDADC_IDACB_CONFIG (0x04000080u)
#define CY_CSDADC_MIN_SNSCLK_DIVIDER (4u)
#define CY_CSDADC_MAX_SNSCLK_DIVIDER (0xFFFu)
#define CY_CSDADC_MAX_SNSCLK_CYCLES (0xFFu)
/* Default filter delay */
#define CY_CSDADC_FILTER_DELAY_DEFAULT (2u)
#define CY_CSDADC_CSD_REG_CONFIG_FILTER_DELAY_Pos (4u)
/* The reference voltage macros */
#if defined (CY_IP_MXCSDV2)
#define CY_CSDADC_VREF_IN_SRSS (800u)
#define CY_CSDADC_VREF_CALIB_BASE_1_16 (1164u)
#define CY_CSDADC_VREF_CALIB_BASE_1_60 (1600u)
#define CY_CSDADC_VREF_CALIB_BASE_2_13 (2133u)
#define CY_CSDADC_VDDA_LIMITATION_2200 (2200u)
#define CY_CSDADC_VDDA_LIMITATION_2750 (2749u)
#elif defined(CY_IP_M0S8CSDV2)
#define CY_CSDADC_VREF_IN_SRSS (1200u)
#define CY_CSDADC_VREF_CALIB_BASE_1_20 (1200u)
#define CY_CSDADC_VREF_CALIB_BASE_2_13 (2133u)
#define CY_CSDADC_VREF_CALIB_BASE_3_84 (3840u)
#define CY_CSDADC_VDDA_LIMITATION_2733 (2733u)
#define CY_CSDADC_VDDA_LIMITATION_4500 (4500u)
#endif
#define CY_CSDADC_PERCENTAGE_100 (100u)
#define CY_CSDADC_VREF_TRIM_MAX_DEVIATION (20u)
#define CY_CSDADC_VREF_GAIN_MAX (32u)
#define CY_CSDADC_VREF_VDDA_MIN_DIFF (600u)
/* CSD HW block CONFIG register definitions */
#define CY_CSDADC_CSD_REG_CONFIG_INIT (0x80001000uL)
#define CY_CSDADC_CSD_REG_CONFIG_DEFAULT (CY_CSDADC_CSD_REG_CONFIG_INIT | \
(uint32_t)((uint32_t)CY_CSDADC_FILTER_DELAY_DEFAULT << CY_CSDADC_CSD_REG_CONFIG_FILTER_DELAY_Pos))
#define CY_CSDADC_ADC_RES_OVERFLOW_MASK (0x40000000uL)
#define CY_CSDADC_ADC_RES_ABORT_MASK (0x80000000uL)
#define CY_CSDADC_ADC_RES_HSCMPPOL_MASK (0x00010000uL)
#define CY_CSDADC_ADC_RES_VALUE_MASK (0x0000FFFFuL)
/* CSD_INTR register masks */
#define CY_CSDADC_CSD_INTR_SAMPLE_MSK (0x00000001uL)
#define CY_CSDADC_CSD_INTR_INIT_MSK (0x00000002uL)
#define CY_CSDADC_CSD_INTR_ADC_RES_MSK (0x00000100uL)
#define CY_CSDADC_CSD_INTR_ALL_MSK (CY_CSDADC_CSD_INTR_SAMPLE_MSK | \
CY_CSDADC_CSD_INTR_INIT_MSK | \
CY_CSDADC_CSD_INTR_ADC_RES_MSK)
/* CSD_INTR_MASK register masks */
#define CY_CSDADC_CSD_INTR_MASK_ADC_RES_MSK (0x00000100uL)
#define CY_CSDADC_CSD_INTR_MASK_CLEAR_MSK (0x00000000uL)
/* Switch definitions */
#define CY_CSDADC_SW_HSP_DEFAULT (0x10000000uL)
#define CY_CSDADC_SW_HSN_DEFAULT (0x00100000uL)
#define CY_CSDADC_SW_SHIELD_DEFAULT (0x00000000uL)
#define CY_CSDADC_SW_SHIELD_VDDA2CSDBUSB (0x00000100uL)
#define CY_CSDADC_SW_BYP_DEFAULT (0x00110000uL)
#define CY_CSDADC_SW_CMPP_DEFAULT (0x00000000uL)
#define CY_CSDADC_SW_CMPN_DEFAULT (0x00000000uL)
/* RefGen settings */
#define CY_CSDADC_REFGEN_GAIN_SHIFT (0x00000008uL)
#define CY_CSDADC_SW_REFGEN_SGR_SRSS (0x10000000uL)
#define CY_CSDADC_REFGEN_NORM (0x00000041UL)
#define CY_CSDADC_SW_AMUBUF_NORM (0x00000000uL)
/* HSCOMP definitions */
#define CY_CSDADC_HSCMP_AZ_DEFAULT (0x80000001uL)
#define CY_CSDADC_SW_FWMOD_DEFAULT (0x01100000uL)
#define CY_CSDADC_SW_FWTANK_DEFAULT (0x01100000uL)
#define CY_CSDADC_CSD_CONFIG_DEFAULT {\
.config = CY_CSDADC_CSD_REG_CONFIG_DEFAULT,\
.spare = 0x00000000uL,\
.status = 0x00000000uL,\
.statSeq = 0x00000000uL,\
.statCnts = 0x00000000uL,\
.statHcnt = 0x00000000uL,\
.resultVal1 = 0x00000000uL,\
.resultVal2 = 0x00000000uL,\
.adcRes = 0x00000000uL,\
.intr = 0x00000000uL,\
.intrSet = 0x00000000uL,\
.intrMask = 0x00000000uL,\
.intrMasked = 0x00000000uL,\
.hscmp = 0x00000001uL,\
.ambuf = 0x00000001uL,\
.refgen = 0x00000001uL,\
.csdCmp = 0x00000000uL,\
.swRes = 0x00000000uL,\
.sensePeriod = 0x00000000uL,\
.senseDuty = 0x00000000uL,\
.swHsPosSel = 0x00000000uL,\
.swHsNegSel = 0x00000000uL,\
.swShieldSel = 0x00000000uL,\
.swAmuxbufSel = 0x00000000uL,\
.swBypSel = 0x00000000uL,\
.swCmpPosSel = 0x00000000uL,\
.swCmpNegSel = 0x00000000uL,\
.swRefgenSel = 0x00000000uL,\
.swFwModSel = 0x00000000uL,\
.swFwTankSel = 0x00000000uL,\
.swDsiSel = 0x00000000uL,\
.ioSel = 0x00000000uL,\
.seqTime = 0x00000000uL,\
.seqInitCnt = 0x00000000uL,\
.seqNormCnt = 0x00000000uL,\
.adcCtl = 0x00000000uL,\
.seqStart = 0x00000000uL,\
.idacA = 0x00000000uL,\
.idacB = 0x00000000uL,\
}
#define CY_CSDADC_MEASMODE_OFF (0u)
#define CY_CSDADC_MEASMODE_VREF (1u)
#define CY_CSDADC_MEASMODE_VREFBY2 (2u)
#define CY_CSDADC_MEASMODE_VIN (3u)
#define CY_CSDADC_ADC_CTL_MEAS_POS (16u)
#define CY_CSDADC_STATUS_FSM_MASK (0xF0u)
#define CY_CSDADC_STATUS_FSM_IDLE (0x00u)
#define CY_CSDADC_STATUS_CALIBPH1 (0x10u)
#define CY_CSDADC_STATUS_CALIBPH2 (0x20u)
#define CY_CSDADC_STATUS_CALIBPH3 (0x30u)
#define CY_CSDADC_STATUS_CONVERTING (0x40u)
#define CY_CSDADC_FSM_ABORT (0x08u)
#define CY_CSDADC_FSM_AZ0_SKIP (0x100u)
#define CY_CSDADC_FSM_AZ_SKIP_DEFAULT (CY_CSDADC_FSM_AZ0_SKIP)
#define CY_CSDADC_FSM_START (0x00000001uL)
/*******************************************************************************
* Function Name: Cy_CSDADC_Init
****************************************************************************//**
*
* Captures the CSD HW block and configures it to the default state,
* is called by the application program prior to calling any other
* function of the middleware.
*
* The function:
* * verifies input parameters
* * copies the configuration structure to the context structure
* * disconnects all input channels
* * verifies the CSD HW block state
* * locks the CSD HW block
* * writes the default configuration to the CSD HW block
* * configures the CSDADC middleware to the default state.
*
* \param config
* The pointer to the CSDADC configuration structure.
*
* \param context
* The pointer to the CSDADC context structure.
*
* \return
* The function returns the status of its operation.
* * CY_CSDADC_SUCCESS - The function performed successfully.
* * CY_CSDADC_HW_LOCKED - The CSD HW block is already in use by another CSD
* function. The CSDADC cannot be initialized
* right now. The user waits until
* the CSD HW block passes to the idle state.
* * CY_CSDADC_BAD_PARAM - The context pointer is NULL.
* The function was not performed.
*
* \funcusage
*
* \snippet csdadc/snippet/main.c snippet_Cy_CSDADC_Initialization
* The 'cy_csdadc_context' variable used as the parameter of the
* Cy_CSDADC_Init() and Cy_CSDADC_Enable() functions is declared
* on the application layer according to the example below:<br>
* \snippet csdadc/snippet/main.c snippet_csdadc_context_declaration
* The 'CSD_csdadc_config' variable used as the parameter of the
* Cy_CSDADC_Init() function is declared in the cycfg_capsense.h file if the
* Device Configurator tool is used.
* Refer to the \ref group_csdadc_configuration section for details.
* The 'CSDADC_csdadc_config' variable is declared and initialized on the
* application layer if the third party IDE is used for development.
*
* The CSDADC_ISR_cfg variable is declared by the application
* program according to the examples below:<br>
* For PSoC™ 4 devices:
* \snippet csdadc/snippet/main.c snippet_p4_adc_interrupt_source_declaration
*
* For CM0+ core of PSoC™ 6 devices:
* \snippet csdadc/snippet/main.c snippet_m0p_adc_interrupt_source_declaration
*
* For CM4 core of PSoC™ 6 devices:
* \snippet csdadc/snippet/main.c snippet_m4_adc_interrupt_source_declaration
*
* The CSDADC interrupt handler is declared by the application program
* according to the example below:
* \snippet csdadc/snippet/main.c snippet_CSDADC_Interrupt
*
* The CSDADC_HW is the pointer to the base register address of
* the CSD HW block. A macro for the pointer is in the cycfg_peripherals.h
* file defined as \<Csd_Personality_Name\>_HW. If no name is specified,
* the default name is used csd_\<Block_Number\>_csd_\<Block_Number\>_HW.
*
*******************************************************************************/
cy_en_csdadc_status_t Cy_CSDADC_Init(
const cy_stc_csdadc_config_t * config,
cy_stc_csdadc_context_t * context)
{
cy_en_csdadc_status_t result = CY_CSDADC_BAD_PARAM;
CY_ASSERT_L1(NULL != config);
CY_ASSERT_L1(NULL != context);
if ((NULL != config) && (NULL != context))
{
/* Copy the configuration structure to the context */
context->cfgCopy = *config;
/* Disconnect all CSDADC channels */
Cy_CSDADC_ClearChannels(context);
/* Capture the CSD HW block for the ADC functionality */
result = Cy_CSDADC_Restore(context);
if (CY_CSDADC_SUCCESS == result)
{
/* Wait for the CSD HW block will enter the mode */
Cy_SysLib_DelayUs((uint16_t)context->cfgCopy.csdInitTime);
/* Initialize CSDADC data structure */
Cy_CSDADC_DsInitialize(config, context);
}
else
{
result = CY_CSDADC_HW_LOCKED;
}
}
return (result);
}
/*******************************************************************************
* Function Name: Cy_CSDADC_Enable
****************************************************************************//**
*
* Initializes the CSDADC firmware modules.
*
* The Cy_CSDADC_Init() function is to be called and the CSD HW block interrupt
* is to be configured prior to calling this function.
* The following steps are performed for proper CSDADC initialization:
* * Captures the CSD HW block and initializes it to the default state.
* * Initialize the CSDADC interrupt.
* * Initialize the CSDADC firmware modules.
*
* This function is called by the application program prior to calling
* any other function of the middleware.
* The function:
* * Configures the CSD HW block to perform CSDADC conversions
* * Calibrates the CSDADC for an accurate measurement
*
* Any subsequent call of this function repeats an initialization process
* except for the data structure initialization. Therefore, changing the
* middleware configuration from the application program is possible.
* Do this by writing registers to the data structure and calling this
* function again. This is also done inside the Cy_CSDACD_WriteConfig()
* function, when configuration must be updated.
*
* \param context
* The pointer to the CSDADC context structure.
*
* \return
* Returns the status of the initialization process. If CY_RET_SUCCESS is not
* received, some of the initialization fails.
*
* \funcusage
*
* \snippet csdadc/snippet/main.c snippet_Cy_CSDADC_Initialization
* The 'cy_csdadc_context' variable used as the parameter of the
* Cy_CSDADC_Init() and Cy_CSDADC_Enable() functions is declared
* on the application layer according to the examples below:<br>
* \snippet csdadc/snippet/main.c snippet_csdadc_context_declaration
* The 'CSD_csdadc_config' variable used as the parameter of the
* Cy_CSDADC_Init() function is declared in the cycfg_capsense.h file if the
* Device Configurator tool is used.
* Refer to the \ref group_csdadc_configuration section for details.
*
* The CSDADC_ISR_cfg variable should be declared by the application
* program according to the examples below:<br>
* For PSoC™ 4 devices:
* \snippet csdadc/snippet/main.c snippet_p4_adc_interrupt_source_declaration
*
* For CM0+ core of PSoC™ 6 devices:
* \snippet csdadc/snippet/main.c snippet_m0p_adc_interrupt_source_declaration
*
* For CM4 core of PSoC™ 6 devices:
* \snippet csdadc/snippet/main.c snippet_m4_adc_interrupt_source_declaration
*
* The CSDADC interrupt handler is declared by the application program
* according to the example below:
* \snippet csdadc/snippet/main.c snippet_CSDADC_Interrupt
*
* The CSDADC_HW is the pointer to the base register address of
* the CSD HW block. A macro for the pointer is in the cycfg_peripherals.h
* file defined as \<Csd_Personality_Name\>_HW. If no name specified,
* the default name is used csd_\<Block_Number\>_csd_\<Block_Number\>_HW.
*
*******************************************************************************/
cy_en_csdadc_status_t Cy_CSDADC_Enable(cy_stc_csdadc_context_t * context)
{
cy_en_csdadc_status_t result = CY_CSDADC_BAD_PARAM;
CY_ASSERT_L1(NULL != context);
if (NULL != context)
{
/* Configure HW block */
Cy_CSDADC_Configure(context);
/* Calibrate CSDADC */
result = Cy_CSDADC_Calibrate(context);
}
return result;
}
/*******************************************************************************
* Function Name: Cy_CSDADC_DeInit
****************************************************************************//**
*
* Stops the middleware operation and releases the CSD HW block.
*
* No input voltage conversion can be executed when the middleware is stopped.
* This function should be called only when no conversion is in progress.
* I.e. Cy_CSDADC_IsEndConversion() returns a non-busy status.
*
* After it is stopped, the CSD HW block may be reconfigured by the
* application program or other middleware for any other use.
*
* When the middleware operation is stopped by the Cy_CSDADC_DeInit()
* function, a subsequent call of the Cy_CSDADC_Init() function repeats the
* initialization process. The second calling the Cy_CSDADC_Enable() function
* is not needed. However, to implement Time-multiplexed mode
* (sharing the CSD HW Block between multiple middleware), use the
* Cy_CSDADC_Save()/Cy_CSDADC_Restore() functions instead of
* the Cy_CSDADC_DeInit()/Cy_CSDADC_Init() functions.
*
* Besides releasing the CSD HW block, this function also configures all input
* channels to the default state.
*
* \param context
* The pointer to the CSDADC context structure.
*
* \return
* The function returns the status of its operation.
* * CY_CSDADC_SUCCESS - The function performed successfully.
* * CY_CSDADC_HW_LOCKED - The CSD HW block is busy with ADC conversion. The CSDADC
* can't be de-initialized right now. The user should
* wait until the CSD HW block passes to the idle
* state or use Cy_CSDADC_StopConvert().
* * CY_CSDADC_BAD_PARAM - A context pointer is equal to NULL.
* The function was not performed.
* * CY_CSDADC_HW_BUSY - A conversion is not started. The previously
* initiated conversion is in progress or
* the CSD HW block is in use by another
* application.
*
*******************************************************************************/
cy_en_csdadc_status_t Cy_CSDADC_DeInit(cy_stc_csdadc_context_t * context)
{
cy_en_csdadc_status_t result = CY_CSDADC_SUCCESS;
CY_ASSERT_L1(NULL != context);
if (NULL != context)
{
if (CY_CSDADC_SUCCESS == Cy_CSDADC_IsEndConversion(context))
{
if (CY_CSD_SUCCESS != Cy_CSD_DeInit(context->cfgCopy.base, CY_CSD_ADC_KEY, context->cfgCopy.csdCxtPtr))
{
result = CY_CSDADC_HW_LOCKED;
}
else
{
Cy_CSDADC_ClearChannels(context);
context->status = (uint16_t)CY_CSDADC_INIT_NEEDED;
}
}
else
{
result = CY_CSDADC_HW_BUSY;
}
}
else
{
result = CY_CSDADC_BAD_PARAM;
}
return (result);
}
/*******************************************************************************
* Function Name: Cy_CSDADC_WriteConfig
****************************************************************************//**
*
* Updates the CSDADC middleware with the desired configuration.
*
* This function sets the desired CSDADC middleware configuration.
* The function performs the following:
* * Verifies input parameters. If any of them is equal to NULL, the function
* does not perform further operations
* * Initializes the CSDADC context structure in the accordance with
* the specified configuration
* * Initializes the CSD HW block registers with data, passed through
* the specified configuration
* * Disconnects inputs and sets the CSD HW block to the default state for
* CSDADC operations
* * Enables CSDADC operations like the Cy_CSDADC_Enable() function. To start
* a conversion, the user should call the Cy_CSDADC_StartConvert() function.
* * Returns a status code regarding the function execution result
*
* \warning
* Call this function only in the CSD HW block idle state.
* Calling this function during a conversion will yield unpredictable
* CSD HW block behavior.
*
* \note
* This function, like the Cy_CSDADC_Enable() function, can be called only after
* the CSDADC middleware initialization. To do this, use
* the Cy_CSDADC_Init() function and the CSDADC interrupt enabling as it
* is shown in the code example for the Cy_CSDADC_Enable() function.
*
* \param config
* The pointer to the CSDADC configuration structure.
*
* \param context
* The pointer to the CSDADC context structure.
*
* \return
* The function returns the status of its operation.
* * CY_CSDADC_SUCCESS - The function performed successfully.
* * CY_CSDADC_BAD_PARAM - A context pointer or config pointer is equal to NULL.
* The function was not performed.
* * CY_CSDADC_HW_BUSY - A conversion is not started. The previously
* initiated conversion is in progress or
* the CSD HW block is in use by another
* application.
* * CY_CSDADC_CALIBRATION_FAIL - The operation watchdog is triggered.
* The calibration was not performed.
*
*******************************************************************************/
cy_en_csdadc_status_t Cy_CSDADC_WriteConfig(
const cy_stc_csdadc_config_t * config,
cy_stc_csdadc_context_t * context)
{
cy_en_csdadc_status_t result = CY_CSDADC_SUCCESS;
CY_ASSERT_L1(NULL != config);
CY_ASSERT_L1(NULL != context);
if ((NULL == config) || (NULL ==context))
{
result = CY_CSDADC_BAD_PARAM;
}
else
{
/* Copy the configuration structure to the context */
context->cfgCopy = * config;
/* Disconnect all CSDADC channels */
Cy_CSDADC_ClearChannels(context);
/* Initialize CSDADC data structure */
Cy_CSDADC_DsInitialize(config, context);
/* Configure and calibrate CSDADC */
result = Cy_CSDADC_Enable(context);
}
return (result);
}
/*******************************************************************************
* Function Name: Cy_CSDADC_RegisterCallback
****************************************************************************//**
*
* Registers a callback function, which notifies that a callback event
* occurred in the CSDADC middleware.
*
* \param callbackFunction
* The pointer to the callback function.
*
* \param context
* The pointer to the CSDADC context structure \ref cy_stc_csdadc_context_t.
*
* \return
* The function returns the status of its operation.
* * CY_CSDADC_SUCCESS - The processing performed successfully.
* * CY_CSDADC_BAD_PARAM - The input parameter is invalid.
*
*******************************************************************************/
cy_en_csdadc_status_t Cy_CSDADC_RegisterCallback(
cy_csdadc_callback_t callbackFunction,
cy_stc_csdadc_context_t * context)
{
cy_en_csdadc_status_t retVal = CY_CSDADC_SUCCESS;
if((NULL != callbackFunction) && (NULL != context))
{
context->ptrEOCCallback = callbackFunction;
}
else
{
retVal = CY_CSDADC_BAD_PARAM;
}
return(retVal);
}
/*******************************************************************************
* Function Name: Cy_CSDADC_UnRegisterCallback
****************************************************************************//**
*
* This function unregisters the CSDADC middleware callback.
*
* \param context
* The pointer to the CSDADC context structure \ref cy_stc_csdadc_context_t.
*
* \return
* The function returns the status of its operation.
* * CY_CSDADC_SUCCESS - The processing performed successfully.
* * CY_CSDADC_BAD_PARAM - The input parameter is invalid.
*
*******************************************************************************/
cy_en_csdadc_status_t Cy_CSDADC_UnRegisterCallback(
cy_stc_csdadc_context_t * context)
{
cy_en_csdadc_status_t retVal = CY_CSDADC_SUCCESS;
if(NULL != context)
{
context->ptrEOCCallback = NULL;
}
else
{
retVal = CY_CSDADC_BAD_PARAM;
}
return(retVal);
}
/*******************************************************************************
* Function Name: Cy_CSDADC_Configure
****************************************************************************//**
*
* Configures the CSD HW block to be used as an ADC.
*
* Configures the IDACB, internal switches, REFGEN, and HSCOMP. This
* function is used by the Cy_CSDADC_Restore() API to set the CSD HW block
* in the same state as before the Cy_CSDADC_Save() API was called.
*
* \param context
* The pointer to the CSDADC context structure.
*
*******************************************************************************/
static void Cy_CSDADC_Configure(const cy_stc_csdadc_context_t * context)
{
uint32_t interruptState;
CSD_Type * ptrCsdBaseAdd = context->cfgCopy.base;
/* Configure clocks */
Cy_CSDADC_SetClkDivider(context);
ptrCsdBaseAdd->SENSE_PERIOD = (uint32_t)context->snsClkDivider - 1u;
/* Configure the IDAC */
ptrCsdBaseAdd->CONFIG = CY_CSDADC_CSD_REG_CONFIG_DEFAULT;
ptrCsdBaseAdd->IDACB = CY_CSDADC_IDACB_CONFIG | context->cfgCopy.idac;
/* Configure AZ Time */
ptrCsdBaseAdd->SEQ_TIME = (uint32_t)context->azCycles - 1u;
ptrCsdBaseAdd->CSDCMP = 0u;
ptrCsdBaseAdd->SW_DSI_SEL = 0u;
ptrCsdBaseAdd->SENSE_DUTY = 0u;
ptrCsdBaseAdd->SEQ_INIT_CNT = 1u;
ptrCsdBaseAdd->SEQ_NORM_CNT = 2u;
/* Configure the block-level routing */
ptrCsdBaseAdd->SW_HS_P_SEL = CY_CSDADC_SW_HSP_DEFAULT;
ptrCsdBaseAdd->SW_HS_N_SEL = CY_CSDADC_SW_HSN_DEFAULT;
ptrCsdBaseAdd->SW_SHIELD_SEL = CY_CSDADC_SW_SHIELD_DEFAULT;
ptrCsdBaseAdd->SW_CMP_P_SEL = CY_CSDADC_SW_CMPP_DEFAULT;
ptrCsdBaseAdd->SW_CMP_N_SEL = CY_CSDADC_SW_CMPN_DEFAULT;
ptrCsdBaseAdd->SW_FW_MOD_SEL = CY_CSDADC_SW_FWMOD_DEFAULT;
ptrCsdBaseAdd->SW_FW_TANK_SEL = CY_CSDADC_SW_FWTANK_DEFAULT;
ptrCsdBaseAdd->SW_REFGEN_SEL = CY_CSDADC_SW_REFGEN_SGR_SRSS;
interruptState = Cy_SysLib_EnterCriticalSection();
ptrCsdBaseAdd->SW_BYP_SEL |= CY_CSDADC_SW_BYP_DEFAULT;
Cy_SysLib_ExitCriticalSection(interruptState);
/* Config RefGen */
ptrCsdBaseAdd->REFGEN = CY_CSDADC_REFGEN_NORM |
((uint32_t)(context->vRefGain) << CY_CSDADC_REFGEN_GAIN_SHIFT);
ptrCsdBaseAdd->SW_AMUXBUF_SEL = CY_CSDADC_SW_AMUBUF_NORM;
/* Configure HSCOMP */
ptrCsdBaseAdd->HSCMP = CY_CSDADC_HSCMP_AZ_DEFAULT;
}
/*******************************************************************************
* Function Name: Cy_CSDADC_Wakeup
****************************************************************************//**
*
*
* Resumes the middleware after CPU / System Deep Sleep.
*
* This function is used to resume the middleware operation after exiting
* CPU / System Deep Sleep. After the CSD HW block has been powered off,
* an extra delay is required to establish the correct operation of
* the CSD HW block.
*
* \param context
* The pointer to the CSDADC context structure.
*
* \return
* The function returns the status of its operation.
* * CY_CSDADC_SUCCESS - The function performed successfully.
* * CY_CSDADC_BAD_PARAM - A context pointer is equal to NULL.
* The function was not performed.
*
*******************************************************************************/
cy_en_csdadc_status_t Cy_CSDADC_Wakeup(const cy_stc_csdadc_context_t * context)
{
cy_en_csdadc_status_t result = CY_CSDADC_SUCCESS;
CY_ASSERT_L1(NULL != context);
if (NULL != context)
{
Cy_SysLib_DelayUs((uint16_t)context->cfgCopy.csdInitTime);
}
else
{
result = CY_CSDADC_BAD_PARAM;
}
return (result);
}
/*******************************************************************************
* Function Name: Cy_CSDADC_DeepSleepCallback
****************************************************************************//**
*
* Handles CPU active to CPU / System Deep Sleep power mode transitions for the CSDADC
* middleware.
*
* Do not call this function directly from the application program.
* Instead, use Cy_SysPm_CpuEnterDeepSleep() for CPU active to CPU / System Deep Sleep power mode
* transitions.
* \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 CSDADC middleware during CPU active to
* CPU / System Deep Sleep mode transitions, a callback to this function is registered
* using the Cy_SysPm_RegisterCallback() function with the 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 the input, this
* function returns CY_SYSPM_SUCCESS if no conversion is in progress. Otherwise,
* CY_SYSPM_FAIL is returned. If the CY_SYSPM_FAIL status is returned, a device
* cannot change the power mode without completing the current conversion because
* a transition to CPU / System Deep Sleep during the conversion can disrupt the middleware
* operation.
*
* For details of the SysPm types and macros, refer to the SysPm section of the
* PDL documentation
* <a href="https:/\/www.cypress.com/documentation/technical-reference-manuals/psoc-6-mcu-psoc-63-ble-architecture-technical-reference"
* title="PDL API Reference" >PDL API Reference</a>.
*
* \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 - CPU / System Deep Sleep power mode can be entered.
* * CY_SYSPM_FAIL - CPU / System Deep Sleep power mode cannot be entered.
*
*******************************************************************************/
cy_en_syspm_status_t Cy_CSDADC_DeepSleepCallback(
cy_stc_syspm_callback_params_t * callbackParams,
cy_en_syspm_callback_mode_t mode)
{
cy_en_syspm_status_t retVal = CY_SYSPM_SUCCESS;
const cy_stc_csdadc_context_t * csdadcCxt = (cy_stc_csdadc_context_t *) callbackParams->context;
if (CY_SYSPM_CHECK_READY == mode)
{ /* Actions that should be done before entering CPU / System Deep Sleep mode */
if (CY_CSD_ADC_KEY == Cy_CSD_GetLockStatus(csdadcCxt->cfgCopy.base, csdadcCxt->cfgCopy.csdCxtPtr))
{
if (CY_CSDADC_SUCCESS != Cy_CSDADC_IsEndConversion(csdadcCxt))
{
retVal = CY_SYSPM_FAIL;
}
}
}
else
{ /* Does nothing in other modes */
}
return(retVal);
}
/*******************************************************************************
* Function Name: Cy_CSDADC_Save
****************************************************************************//**
*
* Saves the state of the CSDADC MW so the functionality can be restored
* using the Cy_CSDACD_Restore() function.
*
* \warning
* The function operates only in the idle state of the CSDADC.
*
* This function, along with the Cy_CSDACD_Restore() function, is specifically
* designed for ease of use and supports time multiplexing of the CSD HW block
* among multiple middleware. When the CSD HW block is shared by two or more
* middleware, this function can be used to save the current state of
* the CSD HW block and the CSDADC middleware prior to releasing the CSD HW block
* for use by other middleware.
*
* This function performs the same tasks as the Cy_CSDADC_DeInit() function
* and is kept for the API consistency among middleware. Use the
* Cy_CSDADC_Save()/Cy_CSDADC_Restore() functions to implement
* Time-multiplexed mode instead of the Cy_CSSADC_DeInit()/Cy_CSDADC_Init()
* functions for further compatibility.
* This function:
* * Checks whether the CSDADC is in the idle state. If the CSDADC is busy,
* the function does nothing. <br>
* In the idle state:
* * Releases the CSD HW block
* * Disconnects channel input pins from analog muxbus B and configures
* them to the default state.
*
* \param context
* The pointer to the CSDADC context structure.
*
* \return
* Returns the status of the process. If CY_CSDADC_SUCCESS is not received,
* the save process fails and a retry may be required.
*
* \funcusage
*
* An example of sharing the CSD HW block with the CAPSENSE™ and CSDADC middleware.<br>
* The CapSense_ISR_cfg variable is declared by the application
* program according to the examples below:<br>
* For PSoC™ 4 devices:
* \snippet csdadc/snippet/main.c snippet_p4_capsense_interrupt_source_declaration
*
* For CM0+ core of PSoC™ 6 devices:
* \snippet csdadc/snippet/main.c snippet_m0p_capsense_interrupt_source_declaration
*
* For CM4 core of PSoC™ 6 devices:
* \snippet csdadc/snippet/main.c snippet_m4_capsense_interrupt_source_declaration
*
* The CSDADC_ISR_cfg variable is declared by the application
* program according to the examples below:<br>
* For PSoC™ 4 devices:
* \snippet csdadc/snippet/main.c snippet_p4_adc_interrupt_source_declaration
*
* For CM0+ core of PSoC™ 6 devices:
* \snippet csdadc/snippet/main.c snippet_m0p_adc_interrupt_source_declaration
*
* For CM4 core of PSoC™ 6 devices:
* \snippet csdadc/snippet/main.c snippet_m4_adc_interrupt_source_declaration
*
* Defines the CAPSENSE™ interrupt handler:
* \snippet csdadc/snippet/main.c snippet_CapSense_Interrupt
*
* Defines the CSDADC interrupt handler:
* \snippet csdadc/snippet/main.c snippet_CSDADC_Interrupt
*
* The part of the main.c FW flow:
* \snippet csdadc/snippet/main.c snippet_Cy_CSDADC_TimeMultiplex
*
*******************************************************************************/
cy_en_csdadc_status_t Cy_CSDADC_Save(cy_stc_csdadc_context_t * context)
{
cy_en_csdadc_status_t result = CY_CSDADC_BAD_PARAM;
cy_en_csd_status_t initStatus = CY_CSD_LOCKED;
CY_ASSERT_L1(NULL != context);
if (NULL != context)
{
result = CY_CSDADC_HW_BUSY;
if (CY_CSDADC_SUCCESS == Cy_CSDADC_IsEndConversion(context))
{
/* Release the CSD HW block */
initStatus = Cy_CSD_DeInit(context->cfgCopy.base, CY_CSD_ADC_KEY, context->cfgCopy.csdCxtPtr);
if (CY_CSD_SUCCESS == initStatus)
{
/* Disconnect input channels pins from analog busses */
Cy_CSDADC_ClearChannels(context);
result = CY_CSDADC_SUCCESS;
}
else if (CY_CSD_LOCKED == initStatus)
{
result = CY_CSDADC_HW_LOCKED;
}
else
{
/* Does nothing; result = CY_CSDADC_HW_BUSY */
}
}
}
return result;
}
/*******************************************************************************
* Function Name: Cy_CSDADC_GetResult
****************************************************************************//**
*
* Returns the most recent result of a specified channel as an ADC code.
*
* The function neither initiates a conversion nor converts the ADC result
* in millivolts. Instead, it returns the most recent conversion result
* on specified input as an ADC code. The valid range for result is
* from 0 to 2^ CSDADCresolution - 1.
*
* \param chId
* An ID of the input channel to read the most recent result. Acceptable values
* are between 0 and (chNum - 1).
*
* \param context
* The pointer to the CSDADC context structure.
*
* \return
* Specifies the CSDADC input channel code result between 0 and 2^resolution - 1.
* If a channel number is invalid, CY_CSDADC_MEASUREMENT_FAILED is returned
* because this function returns a number (not a status).
*
*******************************************************************************/
uint32_t Cy_CSDADC_GetResult(
uint32_t chId,
const cy_stc_csdadc_context_t * context)
{
uint32_t tmpRetVal = CY_CSDADC_MEASUREMENT_FAILED;
CY_ASSERT_L1(NULL != context);
if((NULL != context) && (chId < context->cfgCopy.numChannels))
{
tmpRetVal = context->adcResult[chId].code;
}
return tmpRetVal;
}
/*******************************************************************************
* Function Name: Cy_CSDADC_GetResultVoltage
****************************************************************************//**
*
* Returns the the most recent result of a specified channel in millivolts.
*
* The function does not initiate a conversion. Instead, it returns
* the most recent conversion result on specified input in millivolts.
*
* \param chId
* An ID of the input channel to read the most recent result. Acceptable values
* are between 0 and (chNum - 1).
*
* \param context
* The pointer to the CSDADC context structure.
*
* \return
* Specifies the CSDADC input channel result in millivolts.
* If a channel number is invalid or the pointer to the CSDADC context is
* equal to NULL, \ref CY_CSDADC_MEASUREMENT_FAILED is returned.
*
*******************************************************************************/
uint32_t Cy_CSDADC_GetResultVoltage(
uint32_t chId,
const cy_stc_csdadc_context_t * context)