|
26 | 26 | #include "periph_conf.h" |
27 | 27 | #include "periph/vbat.h" |
28 | 28 |
|
29 | | -#define SMP_MIN (0x2) /*< Sampling time for slow channels |
30 | | - (0x2 = 4.5 ADC clock cycles) */ |
| 29 | +#define ADC_SMP_MIN_VAL (0x2) /*< Sampling time for slow channels |
| 30 | + (0x2 = 4.5 ADC clock cycles) */ |
| 31 | +#define ADC_SMP_VBAT_VAL (0x5) /*< Sampling time when the VBat channel |
| 32 | + is read (0x5 = 61.5 ADC clock cycles) */ |
| 33 | + |
| 34 | +/* The sampling time width is 3 bit */ |
| 35 | +#define ADC_SMP_BIT_WIDTH (3) |
| 36 | + |
| 37 | +/* The sampling time can be specified for each channel over SMPR1 and SMPR2. |
| 38 | + This specifies the first channel that goes to SMPR2 instead of SMPR1. */ |
| 39 | +#define ADC_SMPR2_FIRST_CHAN (10) |
| 40 | + |
31 | 41 | #ifdef ADC1_COMMON |
32 | 42 | #define ADC_INSTANCE ADC1_COMMON |
33 | 43 | #else |
@@ -169,12 +179,21 @@ int adc_init(adc_t line) |
169 | 179 | dev(line)->SQR1 |= (0 & ADC_SQR1_L); |
170 | 180 | } |
171 | 181 |
|
| 182 | + /* determine the right sampling time */ |
| 183 | + uint32_t smp_time = ADC_SMP_MIN_VAL; |
| 184 | + if (IS_USED(MODULE_PERIPH_VBAT) && line == VBAT_ADC) { |
| 185 | + smp_time = ADC_SMP_VBAT_VAL; |
| 186 | + } |
| 187 | + |
172 | 188 | /* Configure sampling time for the given channel */ |
173 | 189 | if (adc_config[line].chan < 10) { |
174 | | - dev(line)->SMPR1 = (SMP_MIN << (adc_config[line].chan * 3)); |
| 190 | + dev(line)->SMPR1 = (smp_time << (adc_config[line].chan |
| 191 | + * ADC_SMP_BIT_WIDTH)); |
175 | 192 | } |
176 | 193 | else { |
177 | | - dev(line)->SMPR2 = (SMP_MIN << ((adc_config[line].chan - 10) * 3)); |
| 194 | + dev(line)->SMPR2 = (smp_time << ((adc_config[line].chan |
| 195 | + - ADC_SMPR2_FIRST_CHAN) |
| 196 | + * ADC_SMP_BIT_WIDTH)); |
178 | 197 | } |
179 | 198 |
|
180 | 199 | /* Power off and unlock device again */ |
|
0 commit comments