forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc_stm32wb0.c
1251 lines (1072 loc) · 36.3 KB
/
adc_stm32wb0.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) 2024 STMicroelectronics
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Terminology used in this file:
* - sampling: a single analog-to-digital conversion performed by the ADC
* - sequence: one or more sampling(s) performed one after the other by the
* ADC after a single programmation. This is the meaning used in the
* STM32WB0 ADC documentation.
* - round: all ADC operations needed to read all channels in the adc_sequence passed
* to adc_read. Zephyr calls this a "sampling", but we use the term "round" to
* prevent confusion with STM32 terminology. A single round may require multiple
* sequences to be performed by the ADC to be completed, due to hardware limitations.
*
* When Zephyr's "sequence" feature is used, the same round is repeated multiple times.
*
* - idle mode: clock & ADC configuration that minimizes power consumption
* - Only the ADC digital domain clock is turned on:
* - ADC is powered off (CTRL.ADC_CTRL_ADC_ON_OFF = 0)
* - ADC analog domain clock is turned off
* - If applicable:
* - ADC LDO is disabled
* - ADC I/O Booster clock is turned off
* - ADC I/O Booster is disabled
* - ADC-SMPS clock synchronization is disabled
*/
#define DT_DRV_COMPAT st_stm32wb0_adc
#include <errno.h>
#include <stdbool.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/policy.h>
#include <zephyr/sys/check.h>
#include <zephyr/sys/util_macro.h>
#include <zephyr/sys/math_extras.h>
#include <soc.h>
#include <stm32_ll_adc.h>
#include <stm32_ll_utils.h>
#ifdef CONFIG_ADC_STM32_DMA
#include <zephyr/drivers/dma/dma_stm32.h>
#include <zephyr/drivers/dma.h>
#include <zephyr/toolchain.h>
#include <stm32_ll_dma.h>
#endif
#define ADC_CONTEXT_USES_KERNEL_TIMER
#define ADC_CONTEXT_ENABLE_ON_COMPLETE
#include "adc_context.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(adc_stm32wb0, CONFIG_ADC_LOG_LEVEL);
/**
* Driver private definitions & assertions
*/
#define ADC_INSTANCE 0
#define ADC_NODE DT_DRV_INST(ADC_INSTANCE)
#define ADC_USE_IO_BOOSTER DT_PROP_OR(ADC_NODE, io_booster, 0)
#define LL_ADC_EXTERNAL_CHANNEL_NUM 12 /* must be a plain constant for LISTIFY */
#define LL_ADC_EXTERNAL_CHANNEL_MAX (LL_ADC_CHANNEL_VINP3_VINM3 + 1U)
#define LL_ADC_CHANNEL_MAX (LL_ADC_CHANNEL_TEMPSENSOR + 1U)
#define LL_ADC_VIN_RANGE_INVALID ((uint8_t)0xFFU)
#define NUM_CALIBRATION_POINTS 4 /* 4 calibration point registers (COMP_[0-3]) */
#if !defined(ADC_CONF_SAMPLE_RATE_MSB)
# define NUM_ADC_SAMPLE_RATES 4 /* SAMPLE_RATE on 2 bits */
#else
# define NUM_ADC_SAMPLE_RATES 32 /* SAMPLE_RATE on 5 bits */
#endif
/* The STM32WB0 has a 12-bit ADC, but the resolution can be
* enhanced to 16-bit by oversampling (using the downsampler)
*/
#define ADC_MIN_RESOLUTION 12
#define ADC_MAX_RESOLUTION 16
/* ADC channel type definitions are not provided by LL as
* it uses per-type functions instead. Bring our own.
*/
#define ADC_CHANNEL_TYPE_SINGLE_NEG (0x00U) /* Single-ended, positive */
#define ADC_CHANNEL_TYPE_SINGLE_POS (0x01U) /* Single-ended, negative */
#define ADC_CHANNEL_TYPE_DIFF (0x02U) /* Differential */
#define ADC_CHANNEL_TYPE_INVALID (0xFFU) /* Invalid */
/** See RM0505 §6.2.1 "System clock details" */
BUILD_ASSERT(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= (8 * 1000 * 1000),
"STM32WB0: system clock frequency must be at least 8MHz to use ADC");
/**
* Driver private structures
*/
struct adc_stm32wb0_data {
struct adc_context ctx;
const struct device *const dev;
/**
* Bitmask of all channels requested as part of this round
* but not sampled yet.
*/
uint32_t unsampled_channels;
/**
* Pointer in output buffer where the first data sample of the round
* is stored. This is used to reload next_sample_ptr when the user
* callback asks to repeat a round.
*/
uint16_t *round_buf_pointer;
/**
* Pointer in output buffer where the next data sample from ADC should
* be stored.
*/
uint16_t *next_sample_ptr;
#if defined(CONFIG_ADC_STM32_DMA)
/** Size of the sequence currently scheduled and executing */
size_t sequence_length;
struct dma_config dmac_config;
struct dma_block_config dma_block_config;
#endif
/** Channels configuration */
struct adc_stm32wb0_channel_config {
/** Vinput range selection */
uint8_t vinput_range;
} channel_config[LL_ADC_CHANNEL_MAX];
};
struct adc_stm32wb0_config {
ADC_TypeDef *reg;
const struct pinctrl_dev_config *pinctrl_cfg;
/** ADC digital domain clock */
struct stm32_pclken dig_clk;
/** ADC analog domain clock */
struct stm32_pclken ana_clk;
#if defined(CONFIG_ADC_STM32_DMA)
const struct device *dmac;
uint32_t dma_channel;
#endif
};
/**
* Driver private utility functions
*/
/**
* In STM32CubeWB0 v1.0.0, the LL_GetPackageType is buggy and returns wrong values.
* This bug is reported in the ST internal bugtracker under reference 185295.
* For now, implement the function ourselves.
*/
static inline uint32_t ll_get_package_type(void)
{
return sys_read32(PACKAGE_BASE);
}
static inline struct adc_stm32wb0_data *drv_data_from_adc_ctx(struct adc_context *adc_ctx)
{
return CONTAINER_OF(adc_ctx, struct adc_stm32wb0_data, ctx);
}
static inline uint8_t vinput_range_from_adc_ref(uint32_t reference)
{
switch (reference) {
case ADC_REF_INTERNAL:
case ADC_REF_VDD_1:
return LL_ADC_VIN_RANGE_3V6;
case ADC_REF_VDD_1_2:
return LL_ADC_VIN_RANGE_2V4;
case ADC_REF_VDD_1_3:
return LL_ADC_VIN_RANGE_1V2;
default:
return LL_ADC_VIN_RANGE_INVALID;
}
}
static inline uint32_t ds_width_from_adc_res(uint32_t resolution)
{
/*
* 12 -> 0 (LL_ADC_DS_DATA_WIDTH_12_BIT)
* 13 -> 1 (LL_ADC_DS_DATA_WIDTH_13_BIT)
* 14 -> 2 (LL_ADC_DS_DATA_WIDTH_14_BIT)
* 15 -> 3 (LL_ADC_DS_DATA_WIDTH_15_BIT)
* 16 -> 4 (LL_ADC_DS_DATA_WIDTH_16_BIT)
*/
return resolution - 12;
}
static inline uint8_t get_channel_type(uint32_t channel)
{
switch (channel) {
case LL_ADC_CHANNEL_VINM0:
case LL_ADC_CHANNEL_VINM1:
case LL_ADC_CHANNEL_VINM2:
case LL_ADC_CHANNEL_VINM3:
case LL_ADC_CHANNEL_VBAT:
return ADC_CHANNEL_TYPE_SINGLE_NEG;
case LL_ADC_CHANNEL_VINP0:
case LL_ADC_CHANNEL_VINP1:
case LL_ADC_CHANNEL_VINP2:
case LL_ADC_CHANNEL_VINP3:
case LL_ADC_CHANNEL_TEMPSENSOR:
return ADC_CHANNEL_TYPE_SINGLE_POS;
case LL_ADC_CHANNEL_VINP0_VINM0:
case LL_ADC_CHANNEL_VINP1_VINM1:
case LL_ADC_CHANNEL_VINP2_VINM2:
case LL_ADC_CHANNEL_VINP3_VINM3:
return ADC_CHANNEL_TYPE_DIFF;
default:
__ASSERT_NO_MSG(0);
return ADC_CHANNEL_TYPE_INVALID;
}
}
/**
* @brief Checks all fields of the adc_sequence and asserts they are
* valid and all configuration options are supported by the driver.
*
* @param sequence adc_sequence to validate
* @return 0 if the adc_sequence is valid, negative value otherwise
*/
static int validate_adc_sequence(const struct adc_sequence *sequence)
{
const size_t round_size = sizeof(uint16_t) * POPCOUNT(sequence->channels);
size_t needed_buf_size;
if (sequence->channels == 0 ||
(sequence->channels & ~BIT_MASK(LL_ADC_CHANNEL_MAX)) != 0) {
LOG_ERR("invalid channels selection");
return -EINVAL;
}
CHECKIF(!sequence->buffer) {
LOG_ERR("storage buffer pointer is NULL");
return -EINVAL;
}
if (!IN_RANGE(sequence->resolution, ADC_MIN_RESOLUTION, ADC_MAX_RESOLUTION)) {
LOG_ERR("invalid resolution %u (must be between %u and %u)",
sequence->resolution, ADC_MIN_RESOLUTION, ADC_MAX_RESOLUTION);
return -EINVAL;
}
/* N.B.: LL define is in the same log2(x) format as the Zephyr variable */
if (sequence->oversampling > LL_ADC_DS_RATIO_128) {
LOG_ERR("oversampling unsupported by hardware (max: %lu)", LL_ADC_DS_RATIO_128);
return -ENOTSUP;
}
if (sequence->options) {
const size_t samplings = (size_t)sequence->options->extra_samplings + 1;
if (size_mul_overflow(round_size, samplings, &needed_buf_size)) {
return -ENOMEM;
}
} else {
needed_buf_size = round_size;
}
if (needed_buf_size > sequence->buffer_size) {
return -ENOMEM;
}
return 0;
}
/**
* @brief Set which channel is sampled during a given conversion of the sequence.
*
* @param ADCx ADC registers pointer
* @param Conversion Target conversion index (0~15)
* @param Channel Channel to sample during specified conversion
*
* @note This function is a more convenient implementation of LL_ADC_SetSequencerRanks
*/
static inline void ll_adc_set_conversion_channel(ADC_TypeDef *ADCx,
uint32_t Conversion, uint32_t Channel)
{
/**
* There are two registers to control the sequencer:
* - SEQ_1 holds channel selection for conversions 0~7
* - SEQ_2 holds channel selection for conversions 8~15
*
* Notice that all conversions in SEQ_2 have 3rd bit set,
* whereas all conversions in SEQ_1 have 3rd bit clear.
*
* In a SEQ_x register, each channel occupies 4 bits, so the
* field for conversion N is at bit offset (4 * (N % 7)).
*/
const uint32_t reg = (Conversion & 8) ? 1 : 0;
const uint32_t shift = 4 * (Conversion & 7);
MODIFY_REG((&ADCx->SEQ_1)[reg], ADC_SEQ_1_SEQ0 << shift, Channel << shift);
}
/**
* @brief Set the calibration point to use for a chosen channel type and Vinput range.
*
* @param ADCx ADC registers pointer
* @param Type Channel type
* @param Range Channel Vinput range
* @param Point Calibration point to use
*
* @note This is a generic version of the LL_ADC_SetCalibPointFor* functions.
*/
static inline void ll_adc_set_calib_point_for_any(ADC_TypeDef *ADCx, uint32_t Type,
uint32_t Range, uint32_t Point)
{
__ASSERT(Range == LL_ADC_VIN_RANGE_1V2
|| Range == LL_ADC_VIN_RANGE_2V4
|| Range == LL_ADC_VIN_RANGE_3V6, "Range is not valid");
__ASSERT(Type == ADC_CHANNEL_TYPE_SINGLE_NEG
|| Type == ADC_CHANNEL_TYPE_SINGLE_POS
|| Type == ADC_CHANNEL_TYPE_DIFF, "Type is not valid");
__ASSERT(Point == LL_ADC_CALIB_POINT_1
|| Point == LL_ADC_CALIB_POINT_2
|| Point == LL_ADC_CALIB_POINT_3
|| Point == LL_ADC_CALIB_POINT_4, "Point is not valid");
/* Register organization is as follows:
*
* - Group for 1.2V Vinput range
* - Group for 2.4V Vinput range
* - Group for 3.6V Vinput range
*
* where Group is organized as:
* - Select for Single Negative mode
* - Select for Single Positive mode
* - Select for Differential mode
*
* Each select is 2 bits, and each group is thus 6 bits.
*/
uint32_t type_shift, group_shift;
switch (Type) {
case ADC_CHANNEL_TYPE_SINGLE_NEG:
type_shift = 0 * 2;
break;
case ADC_CHANNEL_TYPE_SINGLE_POS:
type_shift = 1 * 2;
break;
case ADC_CHANNEL_TYPE_DIFF:
type_shift = 2 * 2;
break;
default:
CODE_UNREACHABLE;
}
switch (Range) {
case LL_ADC_VIN_RANGE_1V2:
group_shift = 0 * 6;
break;
case LL_ADC_VIN_RANGE_2V4:
group_shift = 1 * 6;
break;
case LL_ADC_VIN_RANGE_3V6:
group_shift = 2 * 6;
break;
default:
CODE_UNREACHABLE;
}
const uint32_t shift = (group_shift + type_shift);
MODIFY_REG(ADCx->COMP_SEL, (ADC_COMP_SEL_OFFSET_GAIN0 << shift), (Point << shift));
}
static void adc_acquire_pm_locks(void)
{
pm_policy_state_lock_get(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
if (IS_ENABLED(CONFIG_PM_S2RAM)) {
pm_policy_state_lock_get(PM_STATE_SUSPEND_TO_RAM, PM_ALL_SUBSTATES);
}
}
static void adc_release_pm_locks(void)
{
pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
if (IS_ENABLED(CONFIG_PM_S2RAM)) {
pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_RAM, PM_ALL_SUBSTATES);
}
}
/**
* Driver private functions
*/
static void configure_tempsensor_calib_point(ADC_TypeDef *adc, uint32_t calib_point)
{
uint16_t gain;
#if defined(CONFIG_SOC_STM32WB09XX) || defined(CONFIG_SOC_STM32WB05XX)
/** RM0505/RM0529 §12.2.1 "Temperature sensor subsystem" */
gain = 0xFFF;
#else
/** RM0530 §12.2.2 "Temperature sensor subsystem" */
gain = LL_ADC_GET_CALIB_GAIN_FOR_VINPX_1V2();
#endif /* CONFIG_SOC_STM32WB09XX | CONFIG_SOC_STM32WB05XX */
LL_ADC_ConfigureCalibPoint(adc, calib_point, gain, 0x0);
}
/**
* @brief Obtain calibration data for specified channel type and Vinput range
* from engineering flash, and write it to specified calibration point
*
* @param ADCx ADC registers pointer
* @param Point Calibration point to configure
* @param Type Target channel type
* @param Range Target channel Vinput range
*/
static void configure_calib_point_from_flash(ADC_TypeDef *ADCx, uint32_t Point,
uint32_t Type, uint32_t Range)
{
int8_t offset = 0;
uint16_t gain = 0;
switch (Range) {
case LL_ADC_VIN_RANGE_1V2:
switch (Type) {
case ADC_CHANNEL_TYPE_SINGLE_POS:
gain = LL_ADC_GET_CALIB_GAIN_FOR_VINPX_1V2();
offset = LL_ADC_GET_CALIB_OFFSET_FOR_VINPX_1V2();
break;
case ADC_CHANNEL_TYPE_SINGLE_NEG:
gain = LL_ADC_GET_CALIB_GAIN_FOR_VINMX_1V2();
offset = LL_ADC_GET_CALIB_OFFSET_FOR_VINMX_1V2();
break;
case ADC_CHANNEL_TYPE_DIFF:
gain = LL_ADC_GET_CALIB_GAIN_FOR_VINDIFF_1V2();
offset = LL_ADC_GET_CALIB_OFFSET_FOR_VINDIFF_1V2();
break;
}
break;
case LL_ADC_VIN_RANGE_2V4:
switch (Type) {
case ADC_CHANNEL_TYPE_SINGLE_POS:
gain = LL_ADC_GET_CALIB_GAIN_FOR_VINPX_2V4();
offset = LL_ADC_GET_CALIB_OFFSET_FOR_VINPX_2V4();
break;
case ADC_CHANNEL_TYPE_SINGLE_NEG:
gain = LL_ADC_GET_CALIB_GAIN_FOR_VINMX_2V4();
offset = LL_ADC_GET_CALIB_OFFSET_FOR_VINMX_2V4();
break;
case ADC_CHANNEL_TYPE_DIFF:
gain = LL_ADC_GET_CALIB_GAIN_FOR_VINDIFF_2V4();
offset = LL_ADC_GET_CALIB_OFFSET_FOR_VINDIFF_2V4();
break;
}
break;
case LL_ADC_VIN_RANGE_3V6:
switch (Type) {
case ADC_CHANNEL_TYPE_SINGLE_POS:
gain = LL_ADC_GET_CALIB_GAIN_FOR_VINPX_3V6();
offset = LL_ADC_GET_CALIB_OFFSET_FOR_VINPX_3V6();
break;
case ADC_CHANNEL_TYPE_SINGLE_NEG:
gain = LL_ADC_GET_CALIB_GAIN_FOR_VINMX_3V6();
offset = LL_ADC_GET_CALIB_OFFSET_FOR_VINMX_3V6();
break;
case ADC_CHANNEL_TYPE_DIFF:
gain = LL_ADC_GET_CALIB_GAIN_FOR_VINDIFF_3V6();
offset = LL_ADC_GET_CALIB_OFFSET_FOR_VINDIFF_3V6();
break;
}
break;
}
LL_ADC_ConfigureCalibPoint(ADCx, Point, gain, offset);
}
static void adc_enter_idle_mode(ADC_TypeDef *adc, const struct stm32_pclken *ana_clk)
{
const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
int err;
/* Power down the ADC */
LL_ADC_Disable(adc);
#if SMPS_MODE != STM32WB0_SMPS_MODE_OFF
/* Disable SMPS synchronization */
LL_ADC_SMPSSyncDisable(adc);
#endif /* SMPS_MODE != STM32WB0_SMPS_MODE_OFF */
#if ADC_USE_IO_BOOSTER
/* Disable ADC I/O booster */
LL_RCC_IOBOOST_Disable();
# if defined(RCC_CFGR_IOBOOSTCLKEN)
/* Disable ADC I/O Booster clock if present */
LL_RCC_IOBOOSTCLK_Disable();
# endif
#endif /* ADC_USE_IO_BOOSTER */
#if defined(ADC_CTRL_ADC_LDO_ENA)
/* Disable ADC voltage regulator */
LL_ADC_DisableInternalRegulator(adc);
#endif /* ADC_CTRL_ADC_LDO_ENA */
/* Turn off ADC analog domain clock */
err = clock_control_off(clk, (clock_control_subsys_t)ana_clk);
if (err < 0) {
LOG_WRN("failed to turn off ADC analog clock (%d)", err);
}
/* Release power management locks */
adc_release_pm_locks();
}
static int adc_exit_idle_mode(ADC_TypeDef *adc, const struct stm32_pclken *ana_clk)
{
const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
int err;
/* Acquire power management locks */
adc_acquire_pm_locks();
/* Turn on ADC analog domain clock */
err = clock_control_on(clk,
(clock_control_subsys_t)ana_clk);
if (err < 0) {
LOG_ERR("failed to turn on ADC analog clock: %d", err);
adc_release_pm_locks();
return err;
}
#if defined(ADC_CTRL_ADC_LDO_ENA)
/* RM0479 §12.6.3: bit ADC_LDO_ENA must not be set on QFN32 packages.
* Using an equality check with supported package types ensures that
* we never accidentally set the bit on an unsupported MCU.
*/
const uint32_t package_type = ll_get_package_type();
if (package_type == LL_UTILS_PACKAGETYPE_QFN48
|| package_type == LL_UTILS_PACKAGETYPE_CSP49) {
LL_ADC_EnableInternalRegulator(adc);
}
#endif /* ADC_CTRL_ADC_LDO_ENA */
#if ADC_USE_IO_BOOSTER
# if defined(RCC_CFGR_IOBOOSTCLKEN)
/* Enable ADC I/O Booster clock if needed by hardware */
LL_RCC_IOBOOSTCLK_Enable();
# endif
/* Enable ADC I/O Booster */
LL_RCC_IOBOOST_Enable();
#endif /* ADC_USE_IO_BOOSTER*/
#if SMPS_MODE != STM32WB0_SMPS_MODE_OFF
/* RM0505 §6.2.2 "Peripherals clock details":
* To avoid SNR degradation of the ADC,
* SMPS and ADC clocks must be synchronous.
*/
LL_ADC_SMPSSyncEnable(adc);
#endif /* SMPS_MODE != STM32WB0_SMPS_MODE_OFF */
/* Power up the ADC */
LL_ADC_Enable(adc);
return err;
}
/**
* @brief Schedule as many samplings as possible in a sequence
* then start the ADC conversion.
*/
static void schedule_and_start_adc_sequence(ADC_TypeDef *adc, struct adc_stm32wb0_data *data)
{
uint32_t remaining_unsampled = data->unsampled_channels;
uint32_t allocated_calib_points = 0;
uint32_t sequence_length = 0;
bool temp_sensor_scheduled = false;
/**
* These tables are used to keep track of which calibration
* point registers are used for what type of acquisition, in
* order to share the same calibration point for different
* channels if they use compatible configurations.
*
* Initialize only the first table with invalid values; since
* both tables are updated at the same time, this is sufficient
* to know when to stop programming calibration points.
*/
uint8_t calib_pt_ch_type[NUM_CALIBRATION_POINTS] = {
ADC_CHANNEL_TYPE_INVALID, ADC_CHANNEL_TYPE_INVALID,
ADC_CHANNEL_TYPE_INVALID, ADC_CHANNEL_TYPE_INVALID
};
uint8_t calib_pt_vin_range[NUM_CALIBRATION_POINTS];
/* Schedule as many channels as possible for sampling */
for (uint32_t channel = 0;
channel < LL_ADC_CHANNEL_MAX && remaining_unsampled != 0U;
channel++) {
const uint32_t ch_bit = BIT(channel);
if ((remaining_unsampled & ch_bit) == 0) {
continue;
}
/* Get channel information */
const uint8_t ch_type = get_channel_type(channel);
const uint8_t ch_vin_range = data->channel_config[channel].vinput_range;
/* Attempt to find a compatible calibration point */
uint32_t calib_pt = 0;
for (; calib_pt < allocated_calib_points; calib_pt++) {
if (calib_pt_ch_type[calib_pt] == ch_type
&& calib_pt_vin_range[calib_pt] == ch_vin_range) {
break;
}
}
if (calib_pt == allocated_calib_points) {
/* No compatible calibration point found.
* If an unallocated calibration point remains, use it.
* Otherwise, this channel cannot be scheduled; since we must
* perform samplings in order, exit the scheduling loop.
*/
if (allocated_calib_points < NUM_CALIBRATION_POINTS) {
allocated_calib_points++;
} else {
/* Exit scheduling loop */
break;
}
}
if (channel == LL_ADC_CHANNEL_TEMPSENSOR) {
if (calib_pt_ch_type[calib_pt] == ADC_CHANNEL_TYPE_INVALID) {
/**
* Temperature sensor is a special channel: it must be sampled
* with special gain/offset instead of the calibration values found
* in engineering flash. For this reason, it must NOT be scheduled
* with any other 1.2V Vinput range, single-ended positive channel.
*
* If this check succeeds, then no such channel is scheduled, and we
* can add the temperature sensor to this sequence. We're sure there
* won't be any conflict because the temperature sensor is the last
* channel. Otherwise, a channel with 1.2V Vinput range has been
* scheduled and we must delay the temperature sensor measurement to
* another sequence.
*/
temp_sensor_scheduled = true;
} else {
/* Exit scheduling loop before scheduling temperature sensor */
break;
}
}
/* Ensure calibration point tables are updated.
* This is unneeded if the entry was already filled up,
* but cheaper than checking for duplicate work.
*/
calib_pt_ch_type[calib_pt] = ch_type;
calib_pt_vin_range[calib_pt] = ch_vin_range;
/* Remove channel from unscheduled list */
remaining_unsampled &= ~ch_bit;
/* Add channel to sequence */
ll_adc_set_conversion_channel(adc, sequence_length, channel);
sequence_length++;
/* Select the calibration point to use for channel */
ll_adc_set_calib_point_for_any(adc, ch_type, ch_vin_range, calib_pt);
/* Configure the channel Vinput range selection.
* This must not be done for internal channels, which
* use a hardwired Vinput range selection instead.
*/
if (channel < LL_ADC_EXTERNAL_CHANNEL_MAX) {
LL_ADC_SetChannelVoltageRange(adc, channel, ch_vin_range);
}
#if !defined(CONFIG_ADC_STM32_DMA)
/* If DMA is not enabled, only schedule one channel at a time.
* Otherwise, the ADC will overflow and everything will break.
*/
__ASSERT_NO_MSG(sequence_length == 1);
break;
#endif
}
/* Configure all (used) calibration points */
for (int i = 0; i < NUM_CALIBRATION_POINTS; i++) {
uint8_t type = calib_pt_ch_type[i];
uint8_t range = calib_pt_vin_range[i];
if (type == ADC_CHANNEL_TYPE_INVALID) {
break;
} else if ((type == ADC_CHANNEL_TYPE_SINGLE_POS)
&& (range == LL_ADC_VIN_RANGE_1V2)
&& temp_sensor_scheduled) {
/* Configure special calibration point for temperature sensor */
configure_tempsensor_calib_point(adc, i);
} else {
configure_calib_point_from_flash(adc, i, type, range);
}
}
__ASSERT_NO_MSG(sequence_length > 0);
/* Now that scheduling is done, we can set the sequence length */
LL_ADC_SetSequenceLength(adc, sequence_length);
/* Save unsampled channels (if any) for next sequence */
data->unsampled_channels = remaining_unsampled;
#if defined(CONFIG_ADC_STM32_DMA)
const struct adc_stm32wb0_config *config = data->dev->config;
int err;
/* Save sequence length in driver data for later usage */
data->sequence_length = sequence_length;
/* Prepare the DMA controller for ADC->memory transfers */
data->dma_block_config.source_address = (uint32_t)&adc->DS_DATAOUT;
data->dma_block_config.dest_address = (uint32_t)data->next_sample_ptr;
data->dma_block_config.block_size = data->sequence_length * sizeof(uint16_t);
err = dma_config(config->dmac, config->dma_channel, &data->dmac_config);
if (err < 0) {
LOG_ERR("%s: FAIL - dma_config returns %d", __func__, err);
adc_context_complete(&data->ctx, err);
return;
}
err = dma_start(config->dmac, config->dma_channel);
if (err < 0) {
LOG_ERR("%s: FAIL - dma_start returns %d", __func__, err);
adc_context_complete(&data->ctx, err);
return;
}
#endif
/* Start conversion sequence */
LL_ADC_StartConversion(adc);
}
static inline void handle_end_of_sequence(ADC_TypeDef *adc, struct adc_stm32wb0_data *data)
{
if (data->unsampled_channels != 0) {
/* Some channels requested for this round have
* not been sampled yet. Schedule and start another
* acquisition sequence.
*/
schedule_and_start_adc_sequence(adc, data);
} else {
/* All channels sampled: round is complete. */
adc_context_on_sampling_done(&data->ctx, data->dev);
}
}
static int initiate_read_operation(const struct device *dev,
const struct adc_sequence *sequence)
{
const struct adc_stm32wb0_config *config = dev->config;
struct adc_stm32wb0_data *data = dev->data;
ADC_TypeDef *adc = (ADC_TypeDef *)config->reg;
int err;
err = validate_adc_sequence(sequence);
if (err < 0) {
return err;
}
/* Take ADC out of idle mode before getting to work */
err = adc_exit_idle_mode(adc, &config->ana_clk);
if (err < 0) {
return err;
}
/* Initialize output pointers to first byte of user buffer */
data->next_sample_ptr = data->round_buf_pointer = sequence->buffer;
/* Configure resolution */
LL_ADC_SetDSDataOutputWidth(adc, ds_width_from_adc_res(sequence->resolution));
/* Configure oversampling */
LL_ADC_SetDSDataOutputRatio(adc, sequence->oversampling);
/* Start reading using the ADC */
adc_context_start_read(&data->ctx, sequence);
return 0;
}
#if !defined(CONFIG_ADC_STM32_DMA)
void adc_stm32wb0_isr(const struct device *dev)
{
const struct adc_stm32wb0_config *config = dev->config;
struct adc_stm32wb0_data *data = dev->data;
ADC_TypeDef *adc = config->reg;
/* Down sampler output data available */
if (LL_ADC_IsActiveFlag_EODS(adc)) {
/* Clear pending interrupt flag */
LL_ADC_ClearFlag_EODS(adc);
/* Write ADC data to output buffer and update pointer */
*data->next_sample_ptr++ = LL_ADC_DSGetOutputData(adc);
}
/* Down sampler overflow detected - return error */
if (LL_ADC_IsActiveFlag_OVRDS(adc)) {
LL_ADC_ClearFlag_OVRDS(adc);
LOG_ERR("ADC overflow\n");
adc_context_complete(&data->ctx, -EIO);
return;
}
if (!LL_ADC_IsActiveFlag_EOS(adc)) {
/* ADC sequence not finished yet */
return;
}
/* Clear pending interrupt flag */
LL_ADC_ClearFlag_EOS(adc);
/* Execute end-of-sequence logic */
handle_end_of_sequence(adc, data);
}
#else /* CONFIG_ADC_STM32_DMA */
static void adc_stm32wb0_dma_callback(const struct device *dma, void *user_data,
uint32_t dma_channel, int dma_status)
{
struct adc_stm32wb0_data *data = user_data;
const struct device *dev = data->dev;
const struct adc_stm32wb0_config *config = dev->config;
ADC_TypeDef *adc = config->reg;
int err;
/* N.B.: some of this code is borrowed from existing ADC driver,
* but may be not applicable to STM32WB0 series' ADC.
*/
if (dma_channel == config->dma_channel) {
if (LL_ADC_IsActiveFlag_OVRDS(adc) || (dma_status >= 0)) {
/* Sequence finished - update driver data accordingly */
data->next_sample_ptr += data->sequence_length;
/* Stop the DMA controller */
err = dma_stop(config->dmac, config->dma_channel);
LOG_DBG("%s: dma_stop returns %d", __func__, err);
LL_ADC_ClearFlag_OVRDS(adc);
/* Execute the common end-of-sequence logic */
handle_end_of_sequence(adc, data);
} else { /* dma_status < 0 */
LOG_ERR("%s: dma error %d", __func__, dma_status);
LL_ADC_StopConversion(adc);
err = dma_stop(config->dmac, config->dma_channel);
LOG_DBG("dma_stop returns %d", err);
adc_context_complete(&data->ctx, dma_status);
}
} else {
LOG_DBG("dma_channel 0x%08X != config->dma_channel 0x%08X",
dma_channel, config->dma_channel);
}
}
#endif /* !CONFIG_ADC_STM32_DMA */
/**
* adc_context API implementation
*/
static void adc_context_start_sampling(struct adc_context *ctx)
{
struct adc_stm32wb0_data *data = drv_data_from_adc_ctx(ctx);
const struct adc_stm32wb0_config *config = data->dev->config;
/* Mark all channels of this round as unsampled */
data->unsampled_channels = data->ctx.sequence.channels;
/* Schedule and start first sequence of this round */
schedule_and_start_adc_sequence(config->reg, data);
}
static void adc_context_update_buffer_pointer(
struct adc_context *ctx, bool repeat_sampling)
{
struct adc_stm32wb0_data *data = drv_data_from_adc_ctx(ctx);
if (repeat_sampling) {
/* Roll back output pointer to address of first sample in round */
data->next_sample_ptr = data->round_buf_pointer;
} else /* a new round is starting: */ {
/* Save address of first sample in round in case we have to repeat it */
data->round_buf_pointer = data->next_sample_ptr;
}
}
static void adc_context_on_complete(struct adc_context *ctx, int status)
{
struct adc_stm32wb0_data *data = drv_data_from_adc_ctx(ctx);
const struct adc_stm32wb0_config *config = data->dev->config;
ARG_UNUSED(status);
/**
* All ADC operations are complete.
* Save power by placing ADC in idle mode.
*/
adc_enter_idle_mode(config->reg, &config->ana_clk);
/* Prevent data corruption if something goes wrong. */
data->next_sample_ptr = NULL;
}
/**
* Driver subsystem API implementation
*/
int adc_stm32wb0_channel_setup(const struct device *dev,
const struct adc_channel_cfg *channel_cfg)
{
CHECKIF(dev == NULL) { return -ENODEV; }
CHECKIF(channel_cfg == NULL) { return -EINVAL; }
const bool is_diff_channel =
(channel_cfg->channel_id == LL_ADC_CHANNEL_VINP0_VINM0
|| channel_cfg->channel_id == LL_ADC_CHANNEL_VINP1_VINM1
|| channel_cfg->channel_id == LL_ADC_CHANNEL_VINP2_VINM2
|| channel_cfg->channel_id == LL_ADC_CHANNEL_VINP3_VINM3);
const uint8_t vin_range = vinput_range_from_adc_ref(channel_cfg->reference);
const uint32_t channel_id = channel_cfg->channel_id;
struct adc_stm32wb0_data *data = dev->data;
int res;
/* Forbid reconfiguration while operation in progress */
res = k_sem_take(&data->ctx.lock, K_NO_WAIT);
if (res < 0) {
return res;
}
/* Validate channel configuration parameters */
if (channel_cfg->gain != ADC_GAIN_1) {
LOG_ERR("gain unsupported by hardware");
res = -ENOTSUP;
goto unlock_and_return;
}
if (vin_range == LL_ADC_VIN_RANGE_INVALID) {
LOG_ERR("invalid channel voltage reference");
res = -EINVAL;
goto unlock_and_return;
}
if (channel_id >= LL_ADC_CHANNEL_MAX) {
LOG_ERR("invalid channel id %d", channel_cfg->channel_id);
res = -EINVAL;
goto unlock_and_return;
} else if (is_diff_channel != channel_cfg->differential) {
/* channel_cfg->differential flag does not match
* with the selected channel's type
*/
LOG_ERR("differential flag does not match channel type");
res = -EINVAL;
goto unlock_and_return;
}
if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
LOG_ERR("acquisition time unsupported by hardware");
res = -ENOTSUP;
goto unlock_and_return;
}
/* Verify that the correct reference is selected for special channels */
if (channel_id == LL_ADC_CHANNEL_VBAT && vin_range != LL_ADC_VIN_RANGE_3V6) {
LOG_ERR("invalid reference for Vbat channel");
res = -EINVAL;
goto unlock_and_return;
} else if (channel_id == LL_ADC_CHANNEL_TEMPSENSOR && vin_range != LL_ADC_VIN_RANGE_1V2) {
LOG_ERR("invalid reference for temperature sensor channel");
res = -EINVAL;
goto unlock_and_return;
}
/* Save the channel configuration in driver data.
* Note that the only configuration option available
* is the ADC channel reference (= Vinput range).
*/