application: add ADC to measure target voltage

This commit is contained in:
King Kévin 2021-03-24 00:39:56 +01:00
parent da626f7780
commit d55dbc34b2
1 changed files with 45 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#include <libopencm3/stm32/dbgmcu.h> // debug utilities
#include <libopencm3/stm32/desig.h> // design utilities
#include <libopencm3/stm32/flash.h> // flash utilities
#include <libopencm3/stm32/adc.h> // ADC utilities
/* own libraries */
#include "global.h" // board definitions
@ -49,6 +50,10 @@ static volatile bool second_flag = false; /**< flag set when a second passed */
/** number of seconds since boot */
static uint32_t boot_time = 0;
#define TARGET_CHANNEL 6 /**< PA6/ADC1_IN6 used to measure target voltage */
#define SIGNAL_CHANNEL 1 /**< PA1/ADC1_IN1 used to measure signal voltage */
const uint8_t adc_channels[] = {ADC_CHANNEL17, ADC_CHANNEL(TARGET_CHANNEL), ADC_CHANNEL(SIGNAL_CHANNEL)}; /**< voltages to convert (channel 17 = internal voltage reference) */
size_t putc(char c)
{
size_t length = 0; // number of characters printed
@ -65,6 +70,26 @@ size_t putc(char c)
return length; // return number of characters printed
}
/** measure target and signal voltages
* @return voltages of channels
*/
static float* measure_voltages(void)
{
static float voltages[LENGTH(adc_channels)]; // to store and return the voltages
// read lid temperature using ADC
ADC_SR(ADC1) = 0; // reset flags
uint16_t adc_values[LENGTH(adc_channels)];
for (uint8_t i = 0; i < LENGTH(adc_channels); i++) {
adc_start_conversion_regular(ADC1); // start conversion (using trigger)
while (!adc_eoc(ADC1)); // wait until conversion finished
adc_values[i] = adc_read_regular(ADC1); // read voltage value (clears flag)
voltages[i] = adc_values[i] * 1.21 / adc_values[0]; // use 1.21 V internal voltage reference to get ADC voltage
}
voltages[1] *= 2.0; // the is a /2 voltage divider for target voltage
return voltages;
}
// menu commands
/** display available commands
@ -440,6 +465,26 @@ void main(void)
// important: do not re-enable backup_domain_write_protect, since this will prevent clearing flags (but RTC registers do not need to be unlocked)
puts("OK\n");
puts("setup ADC to measure voltages: ");
rcc_periph_clock_enable(RCC_ADC1); // enable clock for ADC domain
adc_power_off(ADC1); // switch off ADC while configuring it
adc_set_right_aligned(ADC1); // ensure it is right aligned to get the actual value in the 16-bit register
adc_enable_scan_mode(ADC1); // use scan mode do be able to go to next discontinuous subgroup of the regular sequence
adc_enable_discontinuous_mode_regular(ADC1, 1); // use discontinuous mode (to go through all channels of the group, one after another)
adc_set_single_conversion_mode(ADC1); // ensure continuous mode is not used (that's not the same as discontinuous)
adc_eoc_after_each(ADC1); // set EOC after each conversion instead of each group
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28CYC); // use at least 15 cycles to be able to sample at 12-bit resolution
adc_set_regular_sequence(ADC1, LENGTH(adc_channels), (uint8_t*)adc_channels); // set channel to convert
adc_enable_temperature_sensor(); // enable internal voltage reference
adc_power_on(ADC1); // switch on ADC
sleep_us(3); // wait t_stab for the ADC to stabilize
rcc_periph_clock_enable(RCC_ADC1_IN(TARGET_CHANNEL)); // enable clock for GPIO domain for target voltage channel
gpio_mode_setup(ADC1_IN_PORT(TARGET_CHANNEL), GPIO_MODE_ANALOG, GPIO_PUPD_NONE, ADC1_IN_PIN(TARGET_CHANNEL)); // set target voltage channel as analog input for the ADC
rcc_periph_clock_enable(RCC_ADC1_IN(SIGNAL_CHANNEL)); // enable clock for GPIO domain for signal channel
gpio_mode_setup(ADC1_IN_PORT(SIGNAL_CHANNEL), GPIO_MODE_ANALOG, GPIO_PUPD_NONE, ADC1_IN_PIN(SIGNAL_CHANNEL)); // set signal channel as analog input for the ADC
measure_voltages(); // try to measure voltages
puts("OK\n");
// setup terminal
terminal_prefix = ""; // set default prefix
terminal_process = &process_command; // set central function to process commands