diff --git a/src/daq.cpp b/src/daq.cpp index e0a3e1c..d948c1f 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -84,7 +84,7 @@ void calibrate_current_sensors() { int vcc = VREFINT_VALUE * VREFINT_CAL / adc_value(ADC_POS_VREF_MCU); #if CONFIG_HAS_PWM_SWITCH - solar_current_offset = -adc_scaled(ADC_POS_I_SOLAR, vcc, ADC_GAIN_I_SOLAR); + solar_current_offset = -adc_scaled(ADC_POS_I_PWM, vcc, ADC_GAIN_I_PWM); #endif #if CONFIG_HAS_DCDC_CONVERTER solar_current_offset = -adc_scaled(ADC_POS_I_DCDC, vcc, ADC_GAIN_I_DCDC); @@ -99,7 +99,7 @@ void adc_update_value(unsigned int pos) // see also here: http://techteach.no/simview/lowpass_filter/doc/filter_algorithm.pdf #if CONFIG_HAS_PWM_SWITCH == 1 - if (pos == ADC_POS_V_SOLAR || pos == ADC_POS_I_SOLAR) { + if (pos == ADC_POS_V_PWM || pos == ADC_POS_I_PWM) { // only read input voltage and current when switch is on or permanently off if (pwm_switch.signal_high() || pwm_switch.active() == false) { adc_filtered[pos] += (uint32_t)adc_readings[pos] - (adc_filtered[pos] >> ADC_FILTER_CONST); @@ -154,15 +154,15 @@ void daq_update() //int vcc = 3300; // calculate lower voltage first, as it is needed for PWM terminal voltage calculation - lv_bus.voltage = adc_scaled(ADC_POS_V_BAT, vcc, ADC_GAIN_V_BAT); + lv_bus.voltage = adc_scaled(ADC_POS_V_LOW, vcc, ADC_GAIN_V_LOW); #if CONFIG_HAS_DCDC_CONVERTER - hv_bus.voltage = adc_scaled(ADC_POS_V_SOLAR, vcc, ADC_GAIN_V_SOLAR); + hv_bus.voltage = adc_scaled(ADC_POS_V_HIGH, vcc, ADC_GAIN_V_HIGH); #endif #if CONFIG_HAS_PWM_SWITCH - pwm_switch.ext_voltage = lv_bus.voltage - vcc * (ADC_OFFSET_V_SOLAR / 1000.0) - - adc_scaled(ADC_POS_V_SOLAR, vcc, ADC_GAIN_V_SOLAR); + pwm_switch.ext_voltage = lv_bus.voltage - vcc * (ADC_OFFSET_V_PWM / 1000.0) - + adc_scaled(ADC_POS_V_PWM, vcc, ADC_GAIN_V_PWM); #endif load_terminal.current = @@ -171,7 +171,7 @@ void daq_update() #if CONFIG_HAS_PWM_SWITCH // current multiplied with PWM duty cycle for PWM charger to get avg current for correct power calculation pwm_switch.current = -pwm_switch.get_duty_cycle() * ( - adc_scaled(ADC_POS_I_SOLAR, vcc, ADC_GAIN_I_SOLAR) + solar_current_offset); + adc_scaled(ADC_POS_I_PWM, vcc, ADC_GAIN_I_PWM) + solar_current_offset); lv_terminal.current = -pwm_switch.current - load_terminal.current; @@ -243,7 +243,7 @@ void high_voltage_alert() dev_stat.set_error(ERR_BAT_OVERVOLTAGE); print_error("High voltage alert, ADC reading: %d limit: %d\n", - adc_readings[ADC_POS_V_BAT], adc_alerts_upper[ADC_POS_V_BAT].limit); + adc_readings[ADC_POS_V_LOW], adc_alerts_upper[ADC_POS_V_LOW].limit); } void low_voltage_alert() @@ -252,7 +252,7 @@ void low_voltage_alert() load.stop(ERR_LOAD_VOLTAGE_DIP); print_error("Low voltage alert, ADC reading: %d limit: %d\n", - adc_readings[ADC_POS_V_BAT], adc_alerts_lower[ADC_POS_V_BAT].limit); + adc_readings[ADC_POS_V_LOW], adc_alerts_lower[ADC_POS_V_LOW].limit); } void adc_upper_alert_inhibit(int adc_pos, int timeout_ms) @@ -275,15 +275,15 @@ uint16_t adc_get_alert_limit(float scale, float limit) void daq_set_lv_alerts(float upper, float lower) { int vcc = VREFINT_VALUE * VREFINT_CAL / adc_value(ADC_POS_VREF_MCU); - float scale = ((4096* 1000) / (ADC_GAIN_V_BAT)) / vcc; + float scale = ((4096* 1000) / (ADC_GAIN_V_LOW)) / vcc; // LV side (battery) overvoltage alert - adc_alerts_upper[ADC_POS_V_BAT].limit = adc_get_alert_limit(scale, upper); - adc_alerts_upper[ADC_POS_V_BAT].callback = high_voltage_alert; + adc_alerts_upper[ADC_POS_V_LOW].limit = adc_get_alert_limit(scale, upper); + adc_alerts_upper[ADC_POS_V_LOW].callback = high_voltage_alert; // LV side (battery) undervoltage alert - adc_alerts_lower[ADC_POS_V_BAT].limit = adc_get_alert_limit(scale, lower); - adc_alerts_lower[ADC_POS_V_BAT].callback = low_voltage_alert; + adc_alerts_lower[ADC_POS_V_LOW].limit = adc_get_alert_limit(scale, lower); + adc_alerts_lower[ADC_POS_V_LOW].callback = low_voltage_alert; } #if defined(UNIT_TEST) @@ -293,8 +293,8 @@ void daq_set_lv_alerts(float upper, float lower) void prepare_adc_readings(AdcValues values) { adc_readings[ADC_POS_VREF_MCU] = (uint16_t)(1.224 / 3.3 * 4096) << 4; - adc_readings[ADC_POS_V_SOLAR] = (uint16_t)((values.solar_voltage / (ADC_GAIN_V_SOLAR)) / 3.3 * 4096) << 4; - adc_readings[ADC_POS_V_BAT] = (uint16_t)((values.battery_voltage / (ADC_GAIN_V_BAT)) / 3.3 * 4096) << 4; + adc_readings[ADC_POS_V_HIGH] = (uint16_t)((values.solar_voltage / (ADC_GAIN_V_HIGH)) / 3.3 * 4096) << 4; + adc_readings[ADC_POS_V_LOW] = (uint16_t)((values.battery_voltage / (ADC_GAIN_V_LOW)) / 3.3 * 4096) << 4; adc_readings[ADC_POS_I_DCDC] = (uint16_t)((values.dcdc_current / (ADC_GAIN_I_DCDC)) / 3.3 * 4096) << 4; adc_readings[ADC_POS_I_LOAD] = (uint16_t)((values.load_current / (ADC_GAIN_I_LOAD)) / 3.3 * 4096) << 4; } diff --git a/src/pcbs/mppt_1210_hus_0v2.h b/src/pcbs/mppt_1210_hus_0v2.h index 71ac3f0..5cc987f 100644 --- a/src/pcbs/mppt_1210_hus_0v2.h +++ b/src/pcbs/mppt_1210_hus_0v2.h @@ -81,8 +81,8 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 10000.0 -#define ADC_GAIN_V_BAT (110.0 / 10 ) // battery voltage divider 100k + 10k -#define ADC_GAIN_V_SOLAR (105.6 / 5.6) // solar voltage divider: 100k + 5.6k +#define ADC_GAIN_V_LOW (110.0 / 10 ) // battery voltage divider 100k + 10k +#define ADC_GAIN_V_HIGH (105.6 / 5.6) // solar voltage divider: 100k + 5.6k #define ADC_GAIN_I_LOAD (1000 / 4 / (1500.0 / 22.0)) // op amp gain: 150/2.2 = 68.2, resistor: 4 mOhm #define ADC_GAIN_I_DCDC (1000 / 4 / (1500.0 / 22.0)) @@ -91,8 +91,8 @@ enum { ADC_POS_TEMP_BAT, // ADC 0 ADC_POS_TEMP_FETS, // ADC 1 ADC_POS_V_REF, // ADC 5 - ADC_POS_V_BAT, // ADC 6 - ADC_POS_V_SOLAR, // ADC 7 + ADC_POS_V_LOW, // ADC 6 + ADC_POS_V_HIGH, // ADC 7 ADC_POS_I_LOAD, // ADC 8 ADC_POS_I_DCDC, // ADC 9 ADC_POS_TEMP_MCU, // ADC 16 diff --git a/src/pcbs/mppt_1210_hus_0v4.h b/src/pcbs/mppt_1210_hus_0v4.h index f9989ef..2a81c43 100644 --- a/src/pcbs/mppt_1210_hus_0v4.h +++ b/src/pcbs/mppt_1210_hus_0v4.h @@ -89,15 +89,15 @@ const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 10000.0 -#define ADC_GAIN_V_BAT (105.6 / 5.6) // both voltage dividers: 100k + 5.6k -#define ADC_GAIN_V_SOLAR (105.6 / 5.6) +#define ADC_GAIN_V_LOW (105.6 / 5.6) // both voltage dividers: 100k + 5.6k +#define ADC_GAIN_V_HIGH (105.6 / 5.6) #define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm #define ADC_GAIN_I_DCDC (1000 / 4 / 50) // position in the array written by the DMA controller enum { - ADC_POS_V_BAT, // ADC 0 (PA_0) - ADC_POS_V_SOLAR, // ADC 1 (PA_1) + ADC_POS_V_LOW, // ADC 0 (PA_0) + ADC_POS_V_HIGH, // ADC 1 (PA_1) ADC_POS_TEMP_FETS, // ADC 5 (PA_5) ADC_POS_I_LOAD, // ADC 6 (PA_6) ADC_POS_I_DCDC, // ADC 7 (PA_7) diff --git a/src/pcbs/mppt_1210_hus_0v7.h b/src/pcbs/mppt_1210_hus_0v7.h index b8aca63..f62f921 100644 --- a/src/pcbs/mppt_1210_hus_0v7.h +++ b/src/pcbs/mppt_1210_hus_0v7.h @@ -98,15 +98,15 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 10000.0 -#define ADC_GAIN_V_BAT (105.6 / 5.6) // both voltage dividers: 100k + 5.6k -#define ADC_GAIN_V_SOLAR (105.6 / 5.6) +#define ADC_GAIN_V_LOW (105.6 / 5.6) // both voltage dividers: 100k + 5.6k +#define ADC_GAIN_V_HIGH (105.6 / 5.6) #define ADC_GAIN_I_LOAD (1000.0 / 3.0 / 50.0) // amp gain: 50, resistor: 3 mOhm #define ADC_GAIN_I_DCDC (1000.0 / 3.0 / 50.0) // position in the array written by the DMA controller enum { - ADC_POS_V_BAT, // ADC 0 (PA_0) - ADC_POS_V_SOLAR, // ADC 1 (PA_1) + ADC_POS_V_LOW, // ADC 0 (PA_0) + ADC_POS_V_HIGH, // ADC 1 (PA_1) ADC_POS_I_LOAD, // ADC 5 (PA_5) ADC_POS_I_DCDC, // ADC 6 (PA_6) ADC_POS_TEMP_BAT, // ADC 7 (PA_7) diff --git a/src/pcbs/mppt_2420_hpx_0v1.h b/src/pcbs/mppt_2420_hpx_0v1.h index 953b4ca..deeb4a9 100644 --- a/src/pcbs/mppt_2420_hpx_0v1.h +++ b/src/pcbs/mppt_2420_hpx_0v1.h @@ -74,15 +74,15 @@ const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 10000.0 -#define ADC_GAIN_V_BAT (105.6 / 5.6) // both voltage dividers: 100k + 5.6k -#define ADC_GAIN_V_SOLAR (105.6 / 5.6) +#define ADC_GAIN_V_LOW (105.6 / 5.6) // both voltage dividers: 100k + 5.6k +#define ADC_GAIN_V_HIGH (105.6 / 5.6) #define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm #define ADC_GAIN_I_DCDC (1000 / 4 / 50) // position in the array written by the DMA controller enum { - ADC_POS_V_BAT, // ADC 0 (PA_0) - ADC_POS_V_SOLAR, // ADC 1 (PA_1) + ADC_POS_V_LOW, // ADC 0 (PA_0) + ADC_POS_V_HIGH, // ADC 1 (PA_1) ADC_POS_TEMP_FETS, // ADC 5 (PA_5) ADC_POS_I_DCDC, // ADC 7 (PA_7) ADC_POS_I_LOAD, // ADC 6 (PA_6) diff --git a/src/pcbs/mppt_2420_lc_0v10.h b/src/pcbs/mppt_2420_lc_0v10.h index 1dfa1be..cec876e 100644 --- a/src/pcbs/mppt_2420_lc_0v10.h +++ b/src/pcbs/mppt_2420_lc_0v10.h @@ -100,8 +100,8 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 10000.0 -#define ADC_GAIN_V_BAT (110.0 / 10 ) // battery voltage divider 100k + 10k -#define ADC_GAIN_V_SOLAR (105.6 / 5.6) // solar voltage divider: 100k + 5.6k +#define ADC_GAIN_V_LOW (110.0 / 10 ) // battery voltage divider 100k + 10k +#define ADC_GAIN_V_HIGH (105.6 / 5.6) // solar voltage divider: 100k + 5.6k #define ADC_GAIN_I_LOAD (1000 / 2 / (1500.0 / 22.0)) // op amp gain: 150/2.2 = 68.2, resistor: 2 mOhm #define ADC_GAIN_I_DCDC (1000 / 2 / (1500.0 / 22.0)) @@ -110,8 +110,8 @@ enum { ADC_POS_TEMP_BAT, // ADC 0 (PA_0) ADC_POS_TEMP_FETS, // ADC 1 (PA_1) ADC_POS_V_REF, // ADC 5 (PA_5) - ADC_POS_V_BAT, // ADC 6 (PA_6) - ADC_POS_V_SOLAR, // ADC 7 (PA_7) + ADC_POS_V_LOW, // ADC 6 (PA_6) + ADC_POS_V_HIGH, // ADC 7 (PA_7) ADC_POS_I_LOAD, // ADC 8 (PB_0) ADC_POS_I_DCDC, // ADC 9 (PB_1) ADC_POS_TEMP_MCU, // ADC 16 diff --git a/src/pcbs/pcb_stub.h b/src/pcbs/pcb_stub.h index c4d54fc..4292c06 100644 --- a/src/pcbs/pcb_stub.h +++ b/src/pcbs/pcb_stub.h @@ -72,21 +72,21 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 8200.0 -#define ADC_GAIN_V_BAT (105.6 / 5.6) // both voltage dividers: 100k + 5.6k -#define ADC_GAIN_V_SOLAR (105.6 / 5.6) +#define ADC_GAIN_V_LOW (105.6 / 5.6) // both voltage dividers: 100k + 5.6k +#define ADC_GAIN_V_HIGH (105.6 / 5.6) +#define ADC_GAIN_V_PWM (105.6 / 5.6) #define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm #define ADC_GAIN_I_DCDC (1000 / 4 / 50) +#define ADC_GAIN_I_PWM (1000 / 4 / 50) -#define ADC_OFFSET_V_SOLAR (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset - -// trick to pass unit tests for MPPT -#define ADC_POS_I_SOLAR ADC_POS_I_DCDC -#define ADC_GAIN_I_SOLAR ADC_GAIN_I_DCDC +#define ADC_OFFSET_V_PWM (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset // position in the array written by the DMA controller enum { - ADC_POS_V_BAT, // ADC 0 (PA_0) - ADC_POS_V_SOLAR, // ADC 1 (PA_1) + ADC_POS_V_LOW, // ADC 0 (PA_0) + ADC_POS_V_HIGH, // ADC 1 (PA_1) + ADC_POS_V_PWM, // ADC 3 (PA_2) + ADC_POS_I_PWM, // ADC 4 (PA_4) ADC_POS_I_LOAD, // ADC 5 (PA_5) ADC_POS_I_DCDC, // ADC 6 (PA_6) ADC_POS_TEMP_BAT, // ADC 7 (PA_7) diff --git a/src/pcbs/pwm_2420_lus.h b/src/pcbs/pwm_2420_lus.h index da77ab6..547fb9e 100644 --- a/src/pcbs/pwm_2420_lus.h +++ b/src/pcbs/pwm_2420_lus.h @@ -118,8 +118,8 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 8200.0 -#define ADC_GAIN_V_BAT (132 / 12) -#define ADC_GAIN_V_SOLAR (1 + 120/12 + 120/8.2) +#define ADC_GAIN_V_LOW (132 / 12) +#define ADC_GAIN_V_PWM (1 + 120/12 + 120/8.2) #ifdef CONFIG_BOARD_PWM_2420_LUS_0V2 // op amp gain: 68/2.2, resistor: 2 mOhm @@ -129,17 +129,17 @@ static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define ADC_GAIN_I_LOAD (1000 / 2 / (68/2.2) * (39+12+8.2) / (12+8.2)) #endif -#define ADC_GAIN_I_SOLAR (1000 / 2 / (68/2.2)) +#define ADC_GAIN_I_PWM (1000 / 2 / (68/2.2)) -#define ADC_OFFSET_V_SOLAR (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset +#define ADC_OFFSET_V_PWM (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset // position in the array written by the DMA controller enum { - ADC_POS_V_BAT, // ADC 0 (PA_0) - ADC_POS_V_SOLAR, // ADC 1 (PA_1) + ADC_POS_V_LOW, // ADC 0 (PA_0) + ADC_POS_V_PWM, // ADC 1 (PA_1) ADC_POS_I_LOAD, // ADC 5 (PA_5) - ADC_POS_I_SOLAR, // ADC 6 (PA_6) + ADC_POS_I_PWM, // ADC 6 (PA_6) ADC_POS_TEMP_BAT, // ADC 7 (PA_7) ADC_POS_VREF_MCU, // ADC 17 ADC_POS_TEMP_MCU, // ADC 18 diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 6ab3200..2361cb5 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -77,7 +77,7 @@ extern "C" void TIM3_IRQHandler(void) if ((int)TIM3->CCR4 < _pwm_resolution) { // turning the PWM switch on creates a short voltage rise, so inhibit alerts by 10 ms // at each rising edge if switch is not continuously on - adc_upper_alert_inhibit(ADC_POS_V_BAT, 10); + adc_upper_alert_inhibit(ADC_POS_V_LOW, 10); } } @@ -221,7 +221,7 @@ void PwmSwitch::control() && enable == true) { // turning the PWM switch on creates a short voltage rise, so inhibit alerts by 50 ms - adc_upper_alert_inhibit(ADC_POS_V_BAT, 50); + adc_upper_alert_inhibit(ADC_POS_V_LOW, 50); pwm_signal_start(1); print_info("PWM charger start.\n"); } diff --git a/test/tests_daq.cpp b/test/tests_daq.cpp index 899ae0a..1b4fcdc 100644 --- a/test/tests_daq.cpp +++ b/test/tests_daq.cpp @@ -29,13 +29,13 @@ void check_filtering() 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_SOLAR] = get_adc_filtered(ADC_POS_V_SOLAR); + adc_filtered_bak[ADC_POS_V_HIGH] = get_adc_filtered(ADC_POS_V_HIGH); // overwrite filtered values prepare_adc_filtered(); TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS_VREF_MCU), adc_filtered_bak[ADC_POS_VREF_MCU]); - TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS_V_SOLAR), adc_filtered_bak[ADC_POS_V_SOLAR]); + TEST_ASSERT_EQUAL(get_adc_filtered(ADC_POS_V_HIGH), adc_filtered_bak[ADC_POS_V_HIGH]); } void check_solar_terminal_readings() @@ -75,14 +75,14 @@ void adc_alert_undervoltage_triggering() battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100); daq_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min); prepare_adc_filtered(); - adc_update_value(ADC_POS_V_BAT); + adc_update_value(ADC_POS_V_LOW); // undervoltage test adcval.battery_voltage = bat_conf.voltage_absolute_min - 0.1; prepare_adc_readings(adcval); - adc_update_value(ADC_POS_V_BAT); + adc_update_value(ADC_POS_V_LOW); TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_LOAD_VOLTAGE_DIP)); - adc_update_value(ADC_POS_V_BAT); + adc_update_value(ADC_POS_V_LOW); load.control(); TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_LOAD_VOLTAGE_DIP)); TEST_ASSERT_EQUAL(false, load.pgood); @@ -104,16 +104,16 @@ void adc_alert_overvoltage_triggering() battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100); daq_set_lv_alerts(bat_conf.voltage_absolute_max, bat_conf.voltage_absolute_min); prepare_adc_filtered(); - adc_update_value(ADC_POS_V_BAT); + adc_update_value(ADC_POS_V_LOW); dcdc.state = DCDC_STATE_MPPT; // overvoltage test adcval.battery_voltage = bat_conf.voltage_absolute_max + 0.1; prepare_adc_readings(adcval); - adc_update_value(ADC_POS_V_BAT); + adc_update_value(ADC_POS_V_LOW); TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_OVERVOLTAGE)); - adc_update_value(ADC_POS_V_BAT); + adc_update_value(ADC_POS_V_LOW); TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_BAT_OVERVOLTAGE)); TEST_ASSERT_EQUAL(false, pwm_switch.active()); TEST_ASSERT_EQUAL(DCDC_STATE_OFF, dcdc.state);