app: use DAQ driver
Replace the register-level ADC, DAC and DMA handling in daq_driver.c by the new DAQ driver. The application only maps the measurements to the power ports and DC buses and handles the alerts. The factory calibration values are removed from mcu.h, as they are provided by the driver now. All boards are migrated to the new libresolar,stm32-daq compatible. The unit tests use the emulated DAQ. The high-side overvoltage limit is now set after the DAQ setup. Before, the limit was calculated before any reading of the internal reference voltage was available, so that the alert never triggered on boards without fixed reference voltage (STM32F0 and STM32L0). Note for out-of-tree boards: The adc-inputs node must use the compatible libresolar,stm32-daq instead of adc-inputs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
82c5d1542d
commit
55f1c86523
@@ -21,6 +21,7 @@ zephyr_include_directories(../../app/src)
|
||||
zephyr_include_directories(../../../modules/thingset/src)
|
||||
|
||||
target_sources(app PRIVATE
|
||||
src/daq_stub.cpp
|
||||
src/tests_bat_charger.cpp
|
||||
src/tests_daq.cpp
|
||||
src/tests_dcdc.cpp
|
||||
|
||||
@@ -69,7 +69,7 @@
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
compatible = "libresolar,daq-emul";
|
||||
|
||||
v-low {
|
||||
io-channels = <&adc1 12>;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "daq_stub.h"
|
||||
|
||||
#include <libresolar/drivers/daq_emul.h>
|
||||
|
||||
#include "daq.h"
|
||||
|
||||
void prepare_adc_readings(AdcValues values)
|
||||
{
|
||||
daq_emul_set_value(DAQ_DEV, DAQ_CH(v_high), values.solar_voltage);
|
||||
daq_emul_set_value(DAQ_DEV, DAQ_CH(v_low), values.battery_voltage);
|
||||
daq_emul_set_value(DAQ_DEV, DAQ_CH(i_dcdc), values.dcdc_current);
|
||||
daq_emul_set_value(DAQ_DEV, DAQ_CH(i_load), values.load_current);
|
||||
}
|
||||
|
||||
void prepare_adc_filtered()
|
||||
{
|
||||
daq_emul_preset_filtered(DAQ_DEV);
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
#ifndef DAQ_STUB_H_
|
||||
#define DAQ_STUB_H_
|
||||
|
||||
/** Data to fill adc_filtered array during unit-tests
|
||||
/** Data to fill readings of the emulated DAQ during unit-tests
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
@@ -19,11 +19,10 @@ typedef struct
|
||||
float internal_temperature;
|
||||
} AdcValues;
|
||||
|
||||
/** Set the readings of the emulated DAQ for the next samples */
|
||||
void prepare_adc_readings(AdcValues values);
|
||||
|
||||
/** Set the filtered values of the emulated DAQ to the current readings */
|
||||
void prepare_adc_filtered();
|
||||
void clear_adc_filtered();
|
||||
uint32_t get_adc_filtered(uint32_t channel);
|
||||
uint16_t adc_raw_clamp(float scale, float limit);
|
||||
|
||||
#endif
|
||||
|
||||
+11
-68
@@ -4,6 +4,8 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <libresolar/drivers/daq_emul.h>
|
||||
|
||||
#include "daq.h"
|
||||
#include "daq_stub.h"
|
||||
#include "helper.h"
|
||||
@@ -14,58 +16,6 @@
|
||||
|
||||
static AdcValues adcval;
|
||||
|
||||
ZTEST(daq, test_adc_voltage_to_raw)
|
||||
{
|
||||
int32_t raw;
|
||||
|
||||
raw = adc_voltage_to_raw(0.0, 3300);
|
||||
zassert_equal(raw, 0);
|
||||
|
||||
raw = adc_voltage_to_raw(3.3, 3300);
|
||||
zassert_equal(raw, 65535);
|
||||
|
||||
raw = adc_voltage_to_raw(1.65, 3300);
|
||||
zassert_equal(raw, 32767);
|
||||
}
|
||||
|
||||
ZTEST(daq, test_adc_raw_to_voltage)
|
||||
{
|
||||
float voltage;
|
||||
|
||||
voltage = adc_raw_to_voltage(0, 3300);
|
||||
assert_float_equal(voltage, 0.0);
|
||||
|
||||
voltage = adc_raw_to_voltage(65535, 3300);
|
||||
zassert_within(voltage, 3.3F, 0.01F);
|
||||
|
||||
voltage = adc_raw_to_voltage(32767, 3300);
|
||||
zassert_within(voltage, 1.65F, 0.01F);
|
||||
}
|
||||
|
||||
// testing only for 2 values
|
||||
ZTEST(daq, test_check_filtering)
|
||||
{
|
||||
// reset values
|
||||
clear_adc_filtered();
|
||||
|
||||
// collect 1000 samples to update filtered values
|
||||
for (int s = 0; s < 1000; s++) {
|
||||
for (int i = 0; i < NUM_ADC_CH; i++) {
|
||||
adc_update_value(i);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t adc_filtered_bak[NUM_ADC_CH];
|
||||
adc_filtered_bak[ADC_POS(vref_mcu)] = get_adc_filtered(ADC_POS(vref_mcu));
|
||||
adc_filtered_bak[ADC_POS(v_high)] = get_adc_filtered(ADC_POS(v_high));
|
||||
|
||||
// overwrite filtered values
|
||||
prepare_adc_filtered();
|
||||
|
||||
zassert_equal(adc_filtered_bak[ADC_POS(vref_mcu)], get_adc_filtered(ADC_POS(vref_mcu)));
|
||||
zassert_equal(adc_filtered_bak[ADC_POS(v_high)], get_adc_filtered(ADC_POS(v_high)));
|
||||
}
|
||||
|
||||
ZTEST(daq, test_check_solar_terminal_readings)
|
||||
{
|
||||
assert_float_equal(round(hv_terminal.bus->voltage * 10) / 10, adcval.solar_voltage);
|
||||
@@ -97,14 +47,14 @@ ZTEST(daq, test_adc_alert_lv_undervoltage_triggering)
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
|
||||
daq_set_lv_limits(bat_conf.absolute_max_voltage, bat_conf.absolute_min_voltage);
|
||||
prepare_adc_filtered();
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_low));
|
||||
|
||||
// undervoltage test
|
||||
adcval.battery_voltage = bat_conf.absolute_min_voltage - 0.1f;
|
||||
prepare_adc_readings(adcval);
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_low));
|
||||
zassert_equal(dev_stat.has_error(ERR_LOAD_VOLTAGE_DIP), false);
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_low));
|
||||
load.control();
|
||||
zassert_equal(flags_check(&load.error_flags, ERR_LOAD_VOLTAGE_DIP), true);
|
||||
zassert_equal(load.state, LOAD_STATE_OFF);
|
||||
@@ -125,16 +75,16 @@ ZTEST(daq, test_adc_alert_lv_overvoltage_triggering)
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
|
||||
daq_set_lv_limits(bat_conf.absolute_max_voltage, bat_conf.absolute_min_voltage);
|
||||
prepare_adc_filtered();
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_low));
|
||||
|
||||
dcdc.state = DCDC_CONTROL_MPPT;
|
||||
|
||||
// overvoltage test
|
||||
adcval.battery_voltage = bat_conf.absolute_max_voltage + 0.1f;
|
||||
prepare_adc_readings(adcval);
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_low));
|
||||
zassert_equal(dev_stat.has_error(ERR_BAT_OVERVOLTAGE), false);
|
||||
adc_update_value(ADC_POS(v_low));
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_low));
|
||||
zassert_equal(dev_stat.has_error(ERR_BAT_OVERVOLTAGE), true);
|
||||
// zassert_equal(pwm_switch.active(), false);
|
||||
zassert_equal(dcdc.state, DCDC_CONTROL_OFF);
|
||||
@@ -155,25 +105,18 @@ ZTEST(daq, test_adc_alert_hv_overvoltage_triggering)
|
||||
dev_stat.clear_error(ERR_ANY_ERROR);
|
||||
daq_set_hv_limit(DT_PROP(DT_PATH(pcb), hs_voltage_max));
|
||||
prepare_adc_filtered();
|
||||
adc_update_value(ADC_POS(v_high));
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_high));
|
||||
|
||||
// overvoltage test
|
||||
adcval.solar_voltage = 85.0F;
|
||||
prepare_adc_readings(adcval);
|
||||
adc_update_value(ADC_POS(v_high));
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_high));
|
||||
zassert_equal(dev_stat.has_error(ERR_DCDC_HS_OVERVOLTAGE), false);
|
||||
adc_update_value(ADC_POS(v_high));
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_high));
|
||||
zassert_equal(dev_stat.has_error(ERR_DCDC_HS_OVERVOLTAGE), true);
|
||||
zassert_equal(dcdc.state, DCDC_CONTROL_OFF);
|
||||
}
|
||||
|
||||
ZTEST(daq, test_adc_alert_overflow_prevention)
|
||||
{
|
||||
// try to set an alert that overflows the 12-bit ADC resolution
|
||||
uint16_t limit = adc_raw_clamp(1, UINT16_MAX + 1);
|
||||
zassert_equal(limit, UINT16_MAX);
|
||||
}
|
||||
|
||||
/** Data acquisition tests
|
||||
*
|
||||
* Purpose: Check if raw data from 2 voltage and 2 current measurements are converted
|
||||
|
||||
Reference in New Issue
Block a user