forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc_ads114s0x.c
1465 lines (1262 loc) · 53.3 KB
/
adc_ads114s0x.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
/*
* Copyright (c) 2023 SILA Embedded Solutions GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/adc/ads114s0x.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/dt-bindings/adc/ads114s0x_adc.h>
#include <zephyr/logging/log.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>
#define ADC_CONTEXT_USES_KERNEL_TIMER 1
#define ADC_CONTEXT_WAIT_FOR_COMPLETION_TIMEOUT \
K_MSEC(CONFIG_ADC_ADS114S0X_WAIT_FOR_COMPLETION_TIMEOUT_MS)
#include "adc_context.h"
LOG_MODULE_REGISTER(ads114s0x, CONFIG_ADC_LOG_LEVEL);
#define ADS114S0X_CLK_FREQ_IN_KHZ 4096
#define ADS114S0X_RESET_LOW_TIME_IN_CLOCK_CYCLES 4
#define ADS114S0X_START_SYNC_PULSE_DURATION_IN_CLOCK_CYCLES 4
#define ADS114S0X_SETUP_TIME_IN_CLOCK_CYCLES 32
#define ADS114S0X_INPUT_SELECTION_AINCOM 12
#define ADS114S0X_RESOLUTION 16
#define ADS114S0X_REF_INTERNAL 2500
#define ADS114S0X_GPIO_MAX 3
#define ADS114S0X_POWER_ON_RESET_TIME_IN_US 2200
/* Not mentioned in the datasheet, but instead determined experimentally. */
#define ADS114S0X_RESET_DELAY_TIME_SAFETY_MARGIN_IN_US 1000
#define ADS114S0X_RESET_DELAY_TIME_IN_US \
(4096 * 1000 / ADS114S0X_CLK_FREQ_IN_KHZ + ADS114S0X_RESET_DELAY_TIME_SAFETY_MARGIN_IN_US)
#define ADS114S0X_RESET_LOW_TIME_IN_US \
(ADS114S0X_RESET_LOW_TIME_IN_CLOCK_CYCLES * 1000 / ADS114S0X_CLK_FREQ_IN_KHZ)
#define ADS114S0X_START_SYNC_PULSE_DURATION_IN_US \
(ADS114S0X_START_SYNC_PULSE_DURATION_IN_CLOCK_CYCLES * 1000 / ADS114S0X_CLK_FREQ_IN_KHZ)
#define ADS114S0X_SETUP_TIME_IN_US \
(ADS114S0X_SETUP_TIME_IN_CLOCK_CYCLES * 1000 / ADS114S0X_CLK_FREQ_IN_KHZ)
enum ads114s0x_command {
ADS114S0X_COMMAND_NOP = 0x00,
ADS114S0X_COMMAND_WAKEUP = 0x02,
ADS114S0X_COMMAND_POWERDOWN = 0x04,
ADS114S0X_COMMAND_RESET = 0x06,
ADS114S0X_COMMAND_START = 0x08,
ADS114S0X_COMMAND_STOP = 0x0A,
ADS114S0X_COMMAND_SYOCAL = 0x16,
ADS114S0X_COMMAND_SYGCAL = 0x17,
ADS114S0X_COMMAND_SFOCAL = 0x19,
ADS114S0X_COMMAND_RDATA = 0x12,
ADS114S0X_COMMAND_RREG = 0x20,
ADS114S0X_COMMAND_WREG = 0x40,
};
enum ads114s0x_register {
ADS114S0X_REGISTER_ID = 0x00,
ADS114S0X_REGISTER_STATUS = 0x01,
ADS114S0X_REGISTER_INPMUX = 0x02,
ADS114S0X_REGISTER_PGA = 0x03,
ADS114S0X_REGISTER_DATARATE = 0x04,
ADS114S0X_REGISTER_REF = 0x05,
ADS114S0X_REGISTER_IDACMAG = 0x06,
ADS114S0X_REGISTER_IDACMUX = 0x07,
ADS114S0X_REGISTER_VBIAS = 0x08,
ADS114S0X_REGISTER_SYS = 0x09,
ADS114S0X_REGISTER_OFCAL0 = 0x0B,
ADS114S0X_REGISTER_OFCAL1 = 0x0C,
ADS114S0X_REGISTER_FSCAL0 = 0x0E,
ADS114S0X_REGISTER_FSCAL1 = 0x0F,
ADS114S0X_REGISTER_GPIODAT = 0x10,
ADS114S0X_REGISTER_GPIOCON = 0x11,
};
#define ADS114S0X_REGISTER_GET_VALUE(value, pos, length) \
FIELD_GET(GENMASK(pos + length - 1, pos), value)
#define ADS114S0X_REGISTER_SET_VALUE(target, value, pos, length) \
target &= ~GENMASK(pos + length - 1, pos); \
target |= FIELD_PREP(GENMASK(pos + length - 1, pos), value)
#define ADS114S0X_REGISTER_ID_DEV_ID_LENGTH 3
#define ADS114S0X_REGISTER_ID_DEV_ID_POS 0
#define ADS114S0X_REGISTER_ID_DEV_ID_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_ID_DEV_ID_POS, \
ADS114S0X_REGISTER_ID_DEV_ID_LENGTH)
#define ADS114S0X_REGISTER_ID_DEV_ID_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_ID_DEV_ID_POS, \
ADS114S0X_REGISTER_ID_DEV_ID_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_POR_LENGTH 1
#define ADS114S0X_REGISTER_STATUS_FL_POR_POS 7
#define ADS114S0X_REGISTER_STATUS_FL_POR_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_STATUS_FL_POR_POS, \
ADS114S0X_REGISTER_STATUS_FL_POR_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_POR_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_STATUS_FL_POR_POS, \
ADS114S0X_REGISTER_STATUS_FL_POR_LENGTH)
#define ADS114S0X_REGISTER_STATUS_NOT_RDY_LENGTH 1
#define ADS114S0X_REGISTER_STATUS_NOT_RDY_POS 6
#define ADS114S0X_REGISTER_STATUS_NOT_RDY_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_STATUS_NOT_RDY_POS, \
ADS114S0X_REGISTER_STATUS_NOT_RDY_LENGTH)
#define ADS114S0X_REGISTER_STATUS_NOT_RDY_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_STATUS_NOT_RDY_POS, \
ADS114S0X_REGISTER_STATUS_NOT_RDY_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_P_RAILP_LENGTH 1
#define ADS114S0X_REGISTER_STATUS_FL_P_RAILP_POS 5
#define ADS114S0X_REGISTER_STATUS_FL_P_RAILP_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_STATUS_FL_P_RAILP_POS, \
ADS114S0X_REGISTER_STATUS_FL_P_RAILP_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_P_RAILP_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_STATUS_FL_P_RAILP_POS, \
ADS114S0X_REGISTER_STATUS_FL_P_RAILP_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_P_RAILN_LENGTH 1
#define ADS114S0X_REGISTER_STATUS_FL_P_RAILN_POS 4
#define ADS114S0X_REGISTER_STATUS_FL_P_RAILN_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_STATUS_FL_P_RAILN_POS, \
ADS114S0X_REGISTER_STATUS_FL_P_RAILN_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_P_RAILN_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_STATUS_FL_P_RAILN_POS, \
ADS114S0X_REGISTER_STATUS_FL_P_RAILN_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_N_RAILP_LENGTH 1
#define ADS114S0X_REGISTER_STATUS_FL_N_RAILP_POS 3
#define ADS114S0X_REGISTER_STATUS_FL_N_RAILP_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_STATUS_FL_N_RAILP_POS, \
ADS114S0X_REGISTER_STATUS_FL_N_RAILP_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_N_RAILP_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_STATUS_FL_N_RAILP_POS, \
ADS114S0X_REGISTER_STATUS_FL_N_RAILP_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_N_RAILN_LENGTH 1
#define ADS114S0X_REGISTER_STATUS_FL_N_RAILN_POS 2
#define ADS114S0X_REGISTER_STATUS_FL_N_RAILN_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_STATUS_FL_N_RAILN_POS, \
ADS114S0X_REGISTER_STATUS_FL_N_RAILN_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_N_RAILN_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_STATUS_FL_N_RAILN_POS, \
ADS114S0X_REGISTER_STATUS_FL_N_RAILN_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_REF_L1_LENGTH 1
#define ADS114S0X_REGISTER_STATUS_FL_REF_L1_POS 1
#define ADS114S0X_REGISTER_STATUS_FL_REF_L1_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_STATUS_FL_REF_L1_POS, \
ADS114S0X_REGISTER_STATUS_FL_REF_L1_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_REF_L1_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_STATUS_FL_REF_L1_POS, \
ADS114S0X_REGISTER_STATUS_FL_REF_L1_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_REF_L0_LENGTH 1
#define ADS114S0X_REGISTER_STATUS_FL_REF_L0_POS 0
#define ADS114S0X_REGISTER_STATUS_FL_REF_L0_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_STATUS_FL_REF_L0_POS, \
ADS114S0X_REGISTER_STATUS_FL_REF_L0_LENGTH)
#define ADS114S0X_REGISTER_STATUS_FL_REF_L0_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_STATUS_FL_REF_L0_POS, \
ADS114S0X_REGISTER_STATUS_FL_REF_L0_LENGTH)
#define ADS114S0X_REGISTER_INPMUX_MUXP_LENGTH 4
#define ADS114S0X_REGISTER_INPMUX_MUXP_POS 4
#define ADS114S0X_REGISTER_INPMUX_MUXP_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_INPMUX_MUXP_POS, \
ADS114S0X_REGISTER_INPMUX_MUXP_LENGTH)
#define ADS114S0X_REGISTER_INPMUX_MUXP_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_INPMUX_MUXP_POS, \
ADS114S0X_REGISTER_INPMUX_MUXP_LENGTH)
#define ADS114S0X_REGISTER_INPMUX_MUXN_LENGTH 4
#define ADS114S0X_REGISTER_INPMUX_MUXN_POS 0
#define ADS114S0X_REGISTER_INPMUX_MUXN_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_INPMUX_MUXN_POS, \
ADS114S0X_REGISTER_INPMUX_MUXN_LENGTH)
#define ADS114S0X_REGISTER_INPMUX_MUXN_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_INPMUX_MUXN_POS, \
ADS114S0X_REGISTER_INPMUX_MUXN_LENGTH)
#define ADS114S0X_REGISTER_PGA_DELAY_LENGTH 3
#define ADS114S0X_REGISTER_PGA_DELAY_POS 5
#define ADS114S0X_REGISTER_PGA_DELAY_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_PGA_DELAY_POS, \
ADS114S0X_REGISTER_PGA_DELAY_LENGTH)
#define ADS114S0X_REGISTER_PGA_DELAY_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_PGA_DELAY_POS, \
ADS114S0X_REGISTER_PGA_DELAY_LENGTH)
#define ADS114S0X_REGISTER_PGA_PGA_EN_LENGTH 2
#define ADS114S0X_REGISTER_PGA_PGA_EN_POS 3
#define ADS114S0X_REGISTER_PGA_PGA_EN_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_PGA_PGA_EN_POS, \
ADS114S0X_REGISTER_PGA_PGA_EN_LENGTH)
#define ADS114S0X_REGISTER_PGA_PGA_EN_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_PGA_PGA_EN_POS, \
ADS114S0X_REGISTER_PGA_PGA_EN_LENGTH)
#define ADS114S0X_REGISTER_PGA_GAIN_LENGTH 3
#define ADS114S0X_REGISTER_PGA_GAIN_POS 0
#define ADS114S0X_REGISTER_PGA_GAIN_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_PGA_GAIN_POS, \
ADS114S0X_REGISTER_PGA_GAIN_LENGTH)
#define ADS114S0X_REGISTER_PGA_GAIN_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_PGA_GAIN_POS, \
ADS114S0X_REGISTER_PGA_GAIN_LENGTH)
#define ADS114S0X_REGISTER_DATARATE_G_CHOP_LENGTH 1
#define ADS114S0X_REGISTER_DATARATE_G_CHOP_POS 7
#define ADS114S0X_REGISTER_DATARATE_G_CHOP_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_DATARATE_G_CHOP_POS, \
ADS114S0X_REGISTER_DATARATE_G_CHOP_LENGTH)
#define ADS114S0X_REGISTER_DATARATE_G_CHOP_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_DATARATE_G_CHOP_POS, \
ADS114S0X_REGISTER_DATARATE_G_CHOP_LENGTH)
#define ADS114S0X_REGISTER_DATARATE_CLK_LENGTH 1
#define ADS114S0X_REGISTER_DATARATE_CLK_POS 6
#define ADS114S0X_REGISTER_DATARATE_CLK_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_DATARATE_CLK_POS, \
ADS114S0X_REGISTER_DATARATE_CLK_LENGTH)
#define ADS114S0X_REGISTER_DATARATE_CLK_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_DATARATE_CLK_POS, \
ADS114S0X_REGISTER_DATARATE_CLK_LENGTH)
#define ADS114S0X_REGISTER_DATARATE_MODE_LENGTH 1
#define ADS114S0X_REGISTER_DATARATE_MODE_POS 5
#define ADS114S0X_REGISTER_DATARATE_MODE_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_DATARATE_MODE_POS, \
ADS114S0X_REGISTER_DATARATE_MODE_LENGTH)
#define ADS114S0X_REGISTER_DATARATE_MODE_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_DATARATE_MODE_POS, \
ADS114S0X_REGISTER_DATARATE_MODE_LENGTH)
#define ADS114S0X_REGISTER_DATARATE_FILTER_LENGTH 1
#define ADS114S0X_REGISTER_DATARATE_FILTER_POS 4
#define ADS114S0X_REGISTER_DATARATE_FILTER_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_DATARATE_FILTER_POS, \
ADS114S0X_REGISTER_DATARATE_FILTER_LENGTH)
#define ADS114S0X_REGISTER_DATARATE_FILTER_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_DATARATE_FILTER_POS, \
ADS114S0X_REGISTER_DATARATE_FILTER_LENGTH)
#define ADS114S0X_REGISTER_DATARATE_DR_LENGTH 4
#define ADS114S0X_REGISTER_DATARATE_DR_POS 0
#define ADS114S0X_REGISTER_DATARATE_DR_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_DATARATE_DR_POS, \
ADS114S0X_REGISTER_DATARATE_DR_LENGTH)
#define ADS114S0X_REGISTER_DATARATE_DR_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_DATARATE_DR_POS, \
ADS114S0X_REGISTER_DATARATE_DR_LENGTH)
#define ADS114S0X_REGISTER_REF_FL_REF_EN_LENGTH 2
#define ADS114S0X_REGISTER_REF_FL_REF_EN_POS 6
#define ADS114S0X_REGISTER_REF_FL_REF_EN_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_REF_FL_REF_EN_POS, \
ADS114S0X_REGISTER_REF_FL_REF_EN_LENGTH)
#define ADS114S0X_REGISTER_REF_FL_REF_EN_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_REF_FL_REF_EN_POS, \
ADS114S0X_REGISTER_REF_FL_REF_EN_LENGTH)
#define ADS114S0X_REGISTER_REF_NOT_REFP_BUF_LENGTH 1
#define ADS114S0X_REGISTER_REF_NOT_REFP_BUF_POS 5
#define ADS114S0X_REGISTER_REF_NOT_REFP_BUF_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_REF_NOT_REFP_BUF_POS, \
ADS114S0X_REGISTER_REF_NOT_REFP_BUF_LENGTH)
#define ADS114S0X_REGISTER_REF_NOT_REFP_BUF_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_REF_NOT_REFP_BUF_POS, \
ADS114S0X_REGISTER_REF_NOT_REFP_BUF_LENGTH)
#define ADS114S0X_REGISTER_REF_NOT_REFN_BUF_LENGTH 1
#define ADS114S0X_REGISTER_REF_NOT_REFN_BUF_POS 4
#define ADS114S0X_REGISTER_REF_NOT_REFN_BUF_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_REF_NOT_REFN_BUF_POS, \
ADS114S0X_REGISTER_REF_NOT_REFN_BUF_LENGTH)
#define ADS114S0X_REGISTER_REF_NOT_REFN_BUF_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_REF_NOT_REFN_BUF_POS, \
ADS114S0X_REGISTER_REF_NOT_REFN_BUF_LENGTH)
#define ADS114S0X_REGISTER_REF_REFSEL_LENGTH 2
#define ADS114S0X_REGISTER_REF_REFSEL_POS 2
#define ADS114S0X_REGISTER_REF_REFSEL_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_REF_REFSEL_POS, \
ADS114S0X_REGISTER_REF_REFSEL_LENGTH)
#define ADS114S0X_REGISTER_REF_REFSEL_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_REF_REFSEL_POS, \
ADS114S0X_REGISTER_REF_REFSEL_LENGTH)
#define ADS114S0X_REGISTER_REF_REFCON_LENGTH 2
#define ADS114S0X_REGISTER_REF_REFCON_POS 0
#define ADS114S0X_REGISTER_REF_REFCON_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_REF_REFCON_POS, \
ADS114S0X_REGISTER_REF_REFCON_LENGTH)
#define ADS114S0X_REGISTER_REF_REFCON_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_REF_REFCON_POS, \
ADS114S0X_REGISTER_REF_REFCON_LENGTH)
#define ADS114S0X_REGISTER_IDACMAG_FL_RAIL_EN_LENGTH 1
#define ADS114S0X_REGISTER_IDACMAG_FL_RAIL_EN_POS 7
#define ADS114S0X_REGISTER_IDACMAG_FL_RAIL_EN_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_IDACMAG_FL_RAIL_EN_POS, \
ADS114S0X_REGISTER_IDACMAG_FL_RAIL_EN_LENGTH)
#define ADS114S0X_REGISTER_IDACMAG_FL_RAIL_EN_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_IDACMAG_FL_RAIL_EN_POS, \
ADS114S0X_REGISTER_IDACMAG_FL_RAIL_EN_LENGTH)
#define ADS114S0X_REGISTER_IDACMAG_PSW_LENGTH 1
#define ADS114S0X_REGISTER_IDACMAG_PSW_POS 6
#define ADS114S0X_REGISTER_IDACMAG_PSW_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_IDACMAG_PSW_POS, \
ADS114S0X_REGISTER_IDACMAG_PSW_LENGTH)
#define ADS114S0X_REGISTER_IDACMAG_PSW_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_IDACMAG_PSW_POS, \
ADS114S0X_REGISTER_IDACMAG_PSW_LENGTH)
#define ADS114S0X_REGISTER_IDACMAG_IMAG_LENGTH 4
#define ADS114S0X_REGISTER_IDACMAG_IMAG_POS 0
#define ADS114S0X_REGISTER_IDACMAG_IMAG_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_IDACMAG_IMAG_POS, \
ADS114S0X_REGISTER_IDACMAG_IMAG_LENGTH)
#define ADS114S0X_REGISTER_IDACMAG_IMAG_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_IDACMAG_IMAG_POS, \
ADS114S0X_REGISTER_IDACMAG_IMAG_LENGTH)
#define ADS114S0X_REGISTER_IDACMUX_I2MUX_LENGTH 4
#define ADS114S0X_REGISTER_IDACMUX_I2MUX_POS 4
#define ADS114S0X_REGISTER_IDACMUX_I2MUX_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_IDACMUX_I2MUX_POS, \
ADS114S0X_REGISTER_IDACMUX_I2MUX_LENGTH)
#define ADS114S0X_REGISTER_IDACMUX_I2MUX_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_IDACMUX_I2MUX_POS, \
ADS114S0X_REGISTER_IDACMUX_I2MUX_LENGTH)
#define ADS114S0X_REGISTER_IDACMUX_I1MUX_LENGTH 4
#define ADS114S0X_REGISTER_IDACMUX_I1MUX_POS 0
#define ADS114S0X_REGISTER_IDACMUX_I1MUX_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_IDACMUX_I1MUX_POS, \
ADS114S0X_REGISTER_IDACMUX_I1MUX_LENGTH)
#define ADS114S0X_REGISTER_IDACMUX_I1MUX_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_IDACMUX_I1MUX_POS, \
ADS114S0X_REGISTER_IDACMUX_I1MUX_LENGTH)
#define ADS114S0X_REGISTER_GPIODAT_DIR_LENGTH 4
#define ADS114S0X_REGISTER_GPIODAT_DIR_POS 4
#define ADS114S0X_REGISTER_GPIODAT_DIR_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_GPIODAT_DIR_POS, \
ADS114S0X_REGISTER_GPIODAT_DIR_LENGTH)
#define ADS114S0X_REGISTER_GPIODAT_DIR_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_GPIODAT_DIR_POS, \
ADS114S0X_REGISTER_GPIODAT_DIR_LENGTH)
#define ADS114S0X_REGISTER_GPIODAT_DAT_LENGTH 4
#define ADS114S0X_REGISTER_GPIODAT_DAT_POS 0
#define ADS114S0X_REGISTER_GPIODAT_DAT_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_GPIODAT_DAT_POS, \
ADS114S0X_REGISTER_GPIODAT_DAT_LENGTH)
#define ADS114S0X_REGISTER_GPIODAT_DAT_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_GPIODAT_DAT_POS, \
ADS114S0X_REGISTER_GPIODAT_DAT_LENGTH)
#define ADS114S0X_REGISTER_GPIOCON_CON_LENGTH 4
#define ADS114S0X_REGISTER_GPIOCON_CON_POS 0
#define ADS114S0X_REGISTER_GPIOCON_CON_GET(value) \
ADS114S0X_REGISTER_GET_VALUE(value, ADS114S0X_REGISTER_GPIOCON_CON_POS, \
ADS114S0X_REGISTER_GPIOCON_CON_LENGTH)
#define ADS114S0X_REGISTER_GPIOCON_CON_SET(target, value) \
ADS114S0X_REGISTER_SET_VALUE(target, value, ADS114S0X_REGISTER_GPIOCON_CON_POS, \
ADS114S0X_REGISTER_GPIOCON_CON_LENGTH)
/*
* - AIN0 as positive input
* - AIN1 as negative input
*/
#define ADS114S0X_REGISTER_INPMUX_SET_DEFAULTS(target) \
ADS114S0X_REGISTER_INPMUX_MUXP_SET(target, 0b0000); \
ADS114S0X_REGISTER_INPMUX_MUXN_SET(target, 0b0001)
/*
* - disable reference monitor
* - enable positive reference buffer
* - disable negative reference buffer
* - use internal reference
* - enable internal voltage reference
*/
#define ADS114S0X_REGISTER_REF_SET_DEFAULTS(target) \
ADS114S0X_REGISTER_REF_FL_REF_EN_SET(target, 0b00); \
ADS114S0X_REGISTER_REF_NOT_REFP_BUF_SET(target, 0b0); \
ADS114S0X_REGISTER_REF_NOT_REFN_BUF_SET(target, 0b1); \
ADS114S0X_REGISTER_REF_REFSEL_SET(target, 0b10); \
ADS114S0X_REGISTER_REF_REFCON_SET(target, 0b01)
/*
* - disable global chop
* - use internal oscillator
* - single shot conversion mode
* - low latency filter
* - 20 samples per second
*/
#define ADS114S0X_REGISTER_DATARATE_SET_DEFAULTS(target) \
ADS114S0X_REGISTER_DATARATE_G_CHOP_SET(target, 0b0); \
ADS114S0X_REGISTER_DATARATE_CLK_SET(target, 0b0); \
ADS114S0X_REGISTER_DATARATE_MODE_SET(target, 0b1); \
ADS114S0X_REGISTER_DATARATE_FILTER_SET(target, 0b1); \
ADS114S0X_REGISTER_DATARATE_DR_SET(target, 0b0100)
/*
* - delay of 14*t_mod
* - disable gain
* - gain 1
*/
#define ADS114S0X_REGISTER_PGA_SET_DEFAULTS(target) \
ADS114S0X_REGISTER_PGA_DELAY_SET(target, 0b000); \
ADS114S0X_REGISTER_PGA_PGA_EN_SET(target, 0b00); \
ADS114S0X_REGISTER_PGA_GAIN_SET(target, 0b000)
/*
* - disable PGA output rail flag
* - low-side power switch
* - IDAC off
*/
#define ADS114S0X_REGISTER_IDACMAG_SET_DEFAULTS(target) \
ADS114S0X_REGISTER_IDACMAG_FL_RAIL_EN_SET(target, 0b0); \
ADS114S0X_REGISTER_IDACMAG_PSW_SET(target, 0b0); \
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(target, 0b0000)
/*
* - disconnect IDAC1
* - disconnect IDAC2
*/
#define ADS114S0X_REGISTER_IDACMUX_SET_DEFAULTS(target) \
ADS114S0X_REGISTER_IDACMUX_I1MUX_SET(target, 0b1111); \
ADS114S0X_REGISTER_IDACMUX_I2MUX_SET(target, 0b1111)
struct ads114s0x_config {
struct spi_dt_spec bus;
#if CONFIG_ADC_ASYNC
k_thread_stack_t *stack;
#endif
const struct gpio_dt_spec gpio_reset;
const struct gpio_dt_spec gpio_data_ready;
const struct gpio_dt_spec gpio_start_sync;
int idac_current;
};
struct ads114s0x_data {
struct adc_context ctx;
#if CONFIG_ADC_ASYNC
struct k_thread thread;
#endif /* CONFIG_ADC_ASYNC */
struct gpio_callback callback_data_ready;
struct k_sem data_ready_signal;
struct k_sem acquire_signal;
int16_t *buffer;
int16_t *buffer_ptr;
#if CONFIG_ADC_ADS114S0X_GPIO
struct k_mutex gpio_lock;
uint8_t gpio_enabled; /* one bit per GPIO, 1 = enabled */
uint8_t gpio_direction; /* one bit per GPIO, 1 = input */
uint8_t gpio_value; /* one bit per GPIO, 1 = high */
#endif /* CONFIG_ADC_ADS114S0X_GPIO */
};
static void ads114s0x_data_ready_handler(const struct device *dev, struct gpio_callback *gpio_cb,
uint32_t pins)
{
ARG_UNUSED(dev);
ARG_UNUSED(pins);
struct ads114s0x_data *data =
CONTAINER_OF(gpio_cb, struct ads114s0x_data, callback_data_ready);
k_sem_give(&data->data_ready_signal);
}
static int ads114s0x_read_register(const struct spi_dt_spec *bus,
enum ads114s0x_register register_address, uint8_t *value)
{
uint8_t buffer_tx[3];
uint8_t buffer_rx[ARRAY_SIZE(buffer_tx)];
const struct spi_buf tx_buf[] = {{
.buf = buffer_tx,
.len = ARRAY_SIZE(buffer_tx),
}};
const struct spi_buf rx_buf[] = {{
.buf = buffer_rx,
.len = ARRAY_SIZE(buffer_rx),
}};
const struct spi_buf_set tx = {
.buffers = tx_buf,
.count = ARRAY_SIZE(tx_buf),
};
const struct spi_buf_set rx = {
.buffers = rx_buf,
.count = ARRAY_SIZE(rx_buf),
};
buffer_tx[0] = ((uint8_t)ADS114S0X_COMMAND_RREG) | ((uint8_t)register_address);
/* read one register */
buffer_tx[1] = 0x00;
int result = spi_transceive_dt(bus, &tx, &rx);
if (result != 0) {
LOG_ERR("spi_transceive failed with error %i", result);
return result;
}
*value = buffer_rx[2];
LOG_DBG("read from register 0x%02X value 0x%02X", register_address, *value);
return 0;
}
static int ads114s0x_write_register(const struct spi_dt_spec *bus,
enum ads114s0x_register register_address, uint8_t value)
{
uint8_t buffer_tx[3];
const struct spi_buf tx_buf[] = {{
.buf = buffer_tx,
.len = ARRAY_SIZE(buffer_tx),
}};
const struct spi_buf_set tx = {
.buffers = tx_buf,
.count = ARRAY_SIZE(tx_buf),
};
buffer_tx[0] = ((uint8_t)ADS114S0X_COMMAND_WREG) | ((uint8_t)register_address);
/* write one register */
buffer_tx[1] = 0x00;
buffer_tx[2] = value;
LOG_DBG("writing to register 0x%02X value 0x%02X", register_address, value);
int result = spi_write_dt(bus, &tx);
if (result != 0) {
LOG_ERR("spi_write failed with error %i", result);
return result;
}
return 0;
}
static int ads114s0x_write_multiple_registers(const struct spi_dt_spec *bus,
enum ads114s0x_register *register_addresses,
uint8_t *values, size_t count)
{
uint8_t buffer_tx[2];
const struct spi_buf tx_buf[] = {
{
.buf = buffer_tx,
.len = ARRAY_SIZE(buffer_tx),
},
{
.buf = values,
.len = count,
},
};
const struct spi_buf_set tx = {
.buffers = tx_buf,
.count = ARRAY_SIZE(tx_buf),
};
if (count == 0) {
LOG_WRN("ignoring the command to write 0 registers");
return -EINVAL;
}
buffer_tx[0] = ((uint8_t)ADS114S0X_COMMAND_WREG) | ((uint8_t)register_addresses[0]);
buffer_tx[1] = count - 1;
LOG_HEXDUMP_DBG(register_addresses, count, "writing to registers");
LOG_HEXDUMP_DBG(values, count, "values");
/* ensure that the register addresses are in the correct order */
for (size_t i = 1; i < count; ++i) {
__ASSERT(register_addresses[i - 1] + 1 == register_addresses[i],
"register addresses are not consecutive");
}
int result = spi_write_dt(bus, &tx);
if (result != 0) {
LOG_ERR("spi_write failed with error %i", result);
return result;
}
return 0;
}
static int ads114s0x_send_command(const struct spi_dt_spec *bus, enum ads114s0x_command command)
{
uint8_t buffer_tx[1];
const struct spi_buf tx_buf[] = {{
.buf = buffer_tx,
.len = ARRAY_SIZE(buffer_tx),
}};
const struct spi_buf_set tx = {
.buffers = tx_buf,
.count = ARRAY_SIZE(tx_buf),
};
buffer_tx[0] = (uint8_t)command;
LOG_DBG("sending command 0x%02X", command);
int result = spi_write_dt(bus, &tx);
if (result != 0) {
LOG_ERR("spi_write failed with error %i", result);
return result;
}
return 0;
}
static int ads114s0x_channel_setup(const struct device *dev,
const struct adc_channel_cfg *channel_cfg)
{
const struct ads114s0x_config *config = dev->config;
uint8_t input_mux = 0;
uint8_t reference_control = 0;
uint8_t data_rate = 0;
uint8_t gain = 0;
uint8_t idac_magnitude = 0;
uint8_t idac_mux = 0;
uint8_t pin_selections[4];
size_t pin_selections_size;
int result;
enum ads114s0x_register register_addresses[6];
uint8_t values[ARRAY_SIZE(register_addresses)];
uint16_t acquisition_time_value = ADC_ACQ_TIME_VALUE(channel_cfg->acquisition_time);
uint16_t acquisition_time_unit = ADC_ACQ_TIME_UNIT(channel_cfg->acquisition_time);
ADS114S0X_REGISTER_INPMUX_SET_DEFAULTS(gain);
ADS114S0X_REGISTER_REF_SET_DEFAULTS(reference_control);
ADS114S0X_REGISTER_DATARATE_SET_DEFAULTS(data_rate);
ADS114S0X_REGISTER_PGA_SET_DEFAULTS(gain);
ADS114S0X_REGISTER_IDACMAG_SET_DEFAULTS(idac_magnitude);
ADS114S0X_REGISTER_IDACMUX_SET_DEFAULTS(idac_mux);
if (channel_cfg->channel_id != 0) {
LOG_ERR("only one channel is supported");
return -EINVAL;
}
/* The ADS114 uses samples per seconds units with the lowest being 2.5SPS
* and with acquisition_time only having 14b for time, this will not fit
* within here for microsecond units. Use Tick units and allow the user to
* specify the ODR directly.
*/
if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT &&
acquisition_time_unit != ADC_ACQ_TIME_TICKS) {
LOG_ERR("invalid acquisition time %i", channel_cfg->acquisition_time);
return -EINVAL;
}
if (channel_cfg->acquisition_time == ADC_ACQ_TIME_DEFAULT) {
ADS114S0X_REGISTER_DATARATE_DR_SET(data_rate, ADS114S0X_CONFIG_DR_20);
} else {
ADS114S0X_REGISTER_DATARATE_DR_SET(data_rate, acquisition_time_value);
}
switch (channel_cfg->reference) {
case ADC_REF_INTERNAL:
/* disable negative reference buffer */
ADS114S0X_REGISTER_REF_NOT_REFN_BUF_SET(reference_control, 0b1);
/* disable positive reference buffer */
ADS114S0X_REGISTER_REF_NOT_REFP_BUF_SET(reference_control, 0b1);
/* use internal reference */
ADS114S0X_REGISTER_REF_REFSEL_SET(reference_control, 0b10);
break;
case ADC_REF_EXTERNAL0:
/* enable negative reference buffer */
ADS114S0X_REGISTER_REF_NOT_REFN_BUF_SET(reference_control, 0b0);
/* enable positive reference buffer */
ADS114S0X_REGISTER_REF_NOT_REFP_BUF_SET(reference_control, 0b0);
/* use external reference 0*/
ADS114S0X_REGISTER_REF_REFSEL_SET(reference_control, 0b00);
break;
case ADC_REF_EXTERNAL1:
/* enable negative reference buffer */
ADS114S0X_REGISTER_REF_NOT_REFN_BUF_SET(reference_control, 0b0);
/* enable positive reference buffer */
ADS114S0X_REGISTER_REF_NOT_REFP_BUF_SET(reference_control, 0b0);
/* use external reference 0*/
ADS114S0X_REGISTER_REF_REFSEL_SET(reference_control, 0b01);
break;
default:
LOG_ERR("reference %i is not supported", channel_cfg->reference);
return -EINVAL;
}
if (channel_cfg->differential) {
if (channel_cfg->input_positive >= ADS114S0X_INPUT_SELECTION_AINCOM) {
LOG_ERR("positive channel input %i is invalid",
channel_cfg->input_positive);
return -EINVAL;
}
if (channel_cfg->input_negative >= ADS114S0X_INPUT_SELECTION_AINCOM) {
LOG_ERR("negative channel input %i is invalid",
channel_cfg->input_negative);
return -EINVAL;
}
if (channel_cfg->input_positive == channel_cfg->input_negative) {
LOG_ERR("negative and positive channel inputs must be different");
return -EINVAL;
}
ADS114S0X_REGISTER_INPMUX_MUXP_SET(input_mux, channel_cfg->input_positive);
ADS114S0X_REGISTER_INPMUX_MUXN_SET(input_mux, channel_cfg->input_negative);
pin_selections[0] = channel_cfg->input_positive;
pin_selections[1] = channel_cfg->input_negative;
} else {
if (channel_cfg->input_positive >= ADS114S0X_INPUT_SELECTION_AINCOM) {
LOG_ERR("channel input %i is invalid", channel_cfg->input_positive);
return -EINVAL;
}
ADS114S0X_REGISTER_INPMUX_MUXP_SET(input_mux, channel_cfg->input_positive);
ADS114S0X_REGISTER_INPMUX_MUXN_SET(input_mux, ADS114S0X_INPUT_SELECTION_AINCOM);
pin_selections[0] = channel_cfg->input_positive;
pin_selections[1] = ADS114S0X_INPUT_SELECTION_AINCOM;
}
switch (channel_cfg->gain) {
case ADC_GAIN_1:
/* set gain value */
ADS114S0X_REGISTER_PGA_GAIN_SET(gain, 0b000);
break;
case ADC_GAIN_2:
ADS114S0X_REGISTER_PGA_GAIN_SET(gain, 0b001);
break;
case ADC_GAIN_4:
ADS114S0X_REGISTER_PGA_GAIN_SET(gain, 0b010);
break;
case ADC_GAIN_8:
ADS114S0X_REGISTER_PGA_GAIN_SET(gain, 0b011);
break;
case ADC_GAIN_16:
ADS114S0X_REGISTER_PGA_GAIN_SET(gain, 0b100);
break;
case ADC_GAIN_32:
ADS114S0X_REGISTER_PGA_GAIN_SET(gain, 0b101);
break;
case ADC_GAIN_64:
ADS114S0X_REGISTER_PGA_GAIN_SET(gain, 0b110);
break;
case ADC_GAIN_128:
ADS114S0X_REGISTER_PGA_GAIN_SET(gain, 0b111);
break;
default:
LOG_ERR("gain value %i not supported", channel_cfg->gain);
return -EINVAL;
}
if (channel_cfg->gain != ADC_GAIN_1) {
/* enable gain */
ADS114S0X_REGISTER_PGA_PGA_EN_SET(gain, 0b01);
}
switch (config->idac_current) {
case 0:
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(idac_magnitude, 0b0000);
break;
case 10:
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(idac_magnitude, 0b0001);
break;
case 50:
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(idac_magnitude, 0b0010);
break;
case 100:
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(idac_magnitude, 0b0011);
break;
case 250:
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(idac_magnitude, 0b0100);
break;
case 500:
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(idac_magnitude, 0b0101);
break;
case 750:
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(idac_magnitude, 0b0110);
break;
case 1000:
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(idac_magnitude, 0b0111);
break;
case 1500:
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(idac_magnitude, 0b1000);
break;
case 2000:
ADS114S0X_REGISTER_IDACMAG_IMAG_SET(idac_magnitude, 0b1001);
break;
default:
LOG_ERR("IDAC magnitude %i not supported", config->idac_current);
return -EINVAL;
}
if (channel_cfg->current_source_pin_set) {
if (channel_cfg->current_source_pin[0] > 0b1111) {
LOG_ERR("invalid selection %i for I1MUX",
channel_cfg->current_source_pin[0]);
return -EINVAL;
}
if (channel_cfg->current_source_pin[1] > 0b1111) {
LOG_ERR("invalid selection %i for I2MUX",
channel_cfg->current_source_pin[1]);
return -EINVAL;
}
ADS114S0X_REGISTER_IDACMUX_I1MUX_SET(idac_mux, channel_cfg->current_source_pin[0]);
ADS114S0X_REGISTER_IDACMUX_I2MUX_SET(idac_mux, channel_cfg->current_source_pin[1]);
pin_selections[2] = channel_cfg->current_source_pin[0];
pin_selections[3] = channel_cfg->current_source_pin[1];
pin_selections_size = 4;
} else {
pin_selections_size = 2;
}
for (size_t i = 0; i < pin_selections_size; ++i) {
if (pin_selections[i] > ADS114S0X_INPUT_SELECTION_AINCOM) {
continue;
}
for (size_t j = i + 1; j < pin_selections_size; ++j) {
if (pin_selections[j] > ADS114S0X_INPUT_SELECTION_AINCOM) {
continue;
}
if (pin_selections[i] == pin_selections[j]) {
LOG_ERR("pins for inputs and current sources must be different");
return -EINVAL;
}
}
}
register_addresses[0] = ADS114S0X_REGISTER_INPMUX;
register_addresses[1] = ADS114S0X_REGISTER_PGA;
register_addresses[2] = ADS114S0X_REGISTER_DATARATE;
register_addresses[3] = ADS114S0X_REGISTER_REF;
register_addresses[4] = ADS114S0X_REGISTER_IDACMAG;
register_addresses[5] = ADS114S0X_REGISTER_IDACMUX;
BUILD_ASSERT(ARRAY_SIZE(register_addresses) == 6);
values[0] = input_mux;
values[1] = gain;
values[2] = data_rate;
values[3] = reference_control;
values[4] = idac_magnitude;
values[5] = idac_mux;
BUILD_ASSERT(ARRAY_SIZE(values) == 6);
result = ads114s0x_write_multiple_registers(&config->bus, register_addresses, values,
ARRAY_SIZE(values));
if (result != 0) {
LOG_ERR("unable to configure registers");
return result;
}
return 0;
}
static int ads114s0x_validate_buffer_size(const struct adc_sequence *sequence)
{
size_t needed = sizeof(int16_t);
if (sequence->options) {
needed *= (1 + sequence->options->extra_samplings);
}
if (sequence->buffer_size < needed) {
return -ENOMEM;
}
return 0;
}
static int ads114s0x_validate_sequence(const struct device *dev,
const struct adc_sequence *sequence)
{
if (sequence->resolution != ADS114S0X_RESOLUTION) {
LOG_ERR("invalid resolution");
return -EINVAL;
}
if (sequence->channels != BIT(0)) {
LOG_ERR("invalid channel");
return -EINVAL;
}
if (sequence->oversampling) {
LOG_ERR("oversampling is not supported");
return -EINVAL;
}
return ads114s0x_validate_buffer_size(sequence);
}
static void adc_context_update_buffer_pointer(struct adc_context *ctx, bool repeat_sampling)
{
struct ads114s0x_data *data = CONTAINER_OF(ctx, struct ads114s0x_data, ctx);
if (repeat_sampling) {
data->buffer = data->buffer_ptr;
}
}
static void adc_context_start_sampling(struct adc_context *ctx)
{
struct ads114s0x_data *data = CONTAINER_OF(ctx, struct ads114s0x_data, ctx);
data->buffer_ptr = data->buffer;
k_sem_give(&data->acquire_signal);
}
static int ads114s0x_adc_start_read(const struct device *dev, const struct adc_sequence *sequence,
bool wait)
{
int result;
struct ads114s0x_data *data = dev->data;
result = ads114s0x_validate_sequence(dev, sequence);
if (result != 0) {
LOG_ERR("sequence validation failed");
return result;
}
data->buffer = sequence->buffer;
adc_context_start_read(&data->ctx, sequence);
if (wait) {
result = adc_context_wait_for_completion(&data->ctx);
}
return result;
}
static int ads114s0x_send_start_read(const struct device *dev)
{
const struct ads114s0x_config *config = dev->config;
int result;
if (config->gpio_start_sync.port == 0) {
result = ads114s0x_send_command(&config->bus, ADS114S0X_COMMAND_START);
if (result != 0) {
LOG_ERR("unable to send START/SYNC command");
return result;
}
} else {
result = gpio_pin_set_dt(&config->gpio_start_sync, 1);
if (result != 0) {
LOG_ERR("unable to start ADC operation");
return result;
}
k_sleep(K_USEC(ADS114S0X_START_SYNC_PULSE_DURATION_IN_US +
ADS114S0X_SETUP_TIME_IN_US));
result = gpio_pin_set_dt(&config->gpio_start_sync, 0);
if (result != 0) {
LOG_ERR("unable to start ADC operation");
return result;
}
}
return 0;
}
static int ads114s0x_wait_data_ready(const struct device *dev)
{
struct ads114s0x_data *data = dev->data;
return k_sem_take(&data->data_ready_signal, ADC_CONTEXT_WAIT_FOR_COMPLETION_TIMEOUT);
}
static int ads114s0x_read_sample(const struct device *dev, uint16_t *buffer)
{
const struct ads114s0x_config *config = dev->config;
uint8_t buffer_tx[3];
uint8_t buffer_rx[ARRAY_SIZE(buffer_tx)];
const struct spi_buf tx_buf[] = {{
.buf = buffer_tx,
.len = ARRAY_SIZE(buffer_tx),
}};
const struct spi_buf rx_buf[] = {{
.buf = buffer_rx,
.len = ARRAY_SIZE(buffer_rx),
}};
const struct spi_buf_set tx = {
.buffers = tx_buf,
.count = ARRAY_SIZE(tx_buf),
};
const struct spi_buf_set rx = {
.buffers = rx_buf,
.count = ARRAY_SIZE(rx_buf),
};
buffer_tx[0] = (uint8_t)ADS114S0X_COMMAND_RDATA;
int result = spi_transceive_dt(&config->bus, &tx, &rx);
if (result != 0) {
LOG_ERR("spi_transceive failed with error %i", result);
return result;
}
*buffer = sys_get_be16(buffer_rx + 1);
return 0;
}
static int ads114s0x_adc_perform_read(const struct device *dev)
{
int result;
struct ads114s0x_data *data = dev->data;
k_sem_take(&data->acquire_signal, K_FOREVER);
result = ads114s0x_send_start_read(dev);
if (result != 0) {
LOG_ERR("unable to start ADC conversion");
adc_context_complete(&data->ctx, result);
return result;
}
result = ads114s0x_wait_data_ready(dev);