Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit 10b28c9

Browse files
committed
blackpill-f4: split adc conversions
* Power-off ADC after use (only used upon target scan) * Prescale ADC1 clock (APB2) by 4 * Refactor regular sequence of 2 channels into two launches
1 parent cb00a54 commit 10b28c9

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src/platforms/common/blackpill-f4/blackpill-f4.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,33 @@ static void adc_init(void)
164164
rcc_periph_clock_enable(RCC_ADC1);
165165
gpio_mode_setup(VTREF_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, VTREF_PIN);
166166
adc_power_off(ADC1);
167-
adc_disable_scan_mode(ADC1);
168-
adc_set_resolution(ADC1, ADC_CR1_RES_12BIT);
167+
/* Pclk2=Hclk=96 MHz, use ADCclk=24 MHz to stay below 36 per datasheet. Then 480 cycles take 20us. */
168+
adc_set_clk_prescale(ADC_CCR_ADCPRE_BY4);
169169
adc_set_sample_time(ADC1, VTREF_CHANNEL, ADC_SMPR_SMP_480CYC);
170170
adc_set_sample_time(ADC1, ADC_CHANNEL_VREF, ADC_SMPR_SMP_480CYC);
171171
adc_enable_temperature_sensor();
172-
adc_power_on(ADC1);
173172
}
174173

175174
static uint16_t platform_adc_read(void)
176175
{
177-
const uint8_t channels[] = {ADC_CHANNEL_VREF, VTREF_CHANNEL};
178-
adc_set_regular_sequence(ADC1, ARRAY_LENGTH(channels), channels);
176+
const uint8_t channel17 = ADC_CHANNEL_VREF;
177+
const uint8_t channel_vtref = VTREF_CHANNEL;
178+
adc_power_on(ADC1);
179+
adc_set_regular_sequence(ADC1, 1, &channel17);
179180
adc_start_conversion_regular(ADC1);
180181
while (!adc_eoc(ADC1))
181182
continue;
182183
const uint16_t vrefint_sample = adc_read_regular(ADC1);
184+
adc_set_regular_sequence(ADC1, 1, &channel_vtref);
185+
adc_start_conversion_regular(ADC1);
186+
while (!adc_eoc(ADC1))
187+
continue;
183188
const uint16_t value = adc_read_regular(ADC1);
184189
/* Vrefint = 1.21V typ, Vdda = 3.3V, expected code of 1501 */
185190
const uint16_t vrefint_expected = 1210U * 4095U / 3300U;
186191
const uint16_t value_adj = value * vrefint_expected / vrefint_sample;
187-
DEBUG_INFO("%s: Vrefint=%u, VTref=%u, returning %u", __func__, vrefint_sample, value, value_adj);
192+
DEBUG_INFO("%s: Vrefint=%u, VTref=%u, returning %u\n", __func__, vrefint_sample, value, value_adj);
193+
adc_power_off(ADC1);
188194
return value_adj;
189195
}
190196

0 commit comments

Comments
 (0)