From b8fe6d7023e02936e37987d568986d5efca7b114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Wed, 8 Jul 2020 10:30:53 +0200 Subject: [PATCH] Fix some static code analysis warnings --- README.md | 13 +++-- src/bat_charger.cpp | 132 ++++++++++++++++++++++-------------------- src/bat_charger.h | 4 +- src/daq.cpp | 2 - src/daq_driver.c | 2 + src/data_nodes.cpp | 3 +- src/dcdc.h | 4 +- src/device_status.cpp | 4 +- src/ext/ext.cpp | 4 +- src/leds.cpp | 6 +- src/main.cpp | 8 +-- src/power_port.cpp | 6 +- src/power_port.h | 14 ++--- src/setup.cpp | 6 +- 14 files changed, 110 insertions(+), 98 deletions(-) diff --git a/README.md b/README.md index 8dc8609..e5e435e 100644 --- a/README.md +++ b/README.md @@ -134,24 +134,25 @@ The documentation auto-generated by Doxygen can be found [here](https://libre.so ## Conformance testing We are using Travis CI to perform several checks for each commit or pull-request. In order to run -the same tests locally (recommended before each pull-request) use the script `check.sh` or ` check.bat`: +the same tests locally (recommended before pushing to the server) use the script `check.sh` or ` check.bat`: bash check.sh # Linux cmd check.bat # Windows ### Unit-tests -In order to run the unit tests, you need a PlatformIO Plus account. Run tests with the following command: +Core functions of the firmware are covered by unit-tests (located in test directory). Run the tests with the following command: - platformio test -e unit-test-native + platformio test -e unit_test ### Static code analysis PlatformIO integrates cppcheck and clangtidy. The following command is an example to run the checks for MPPT 1210 HUS charge controller code: - platformio check -e mppt-1210-hus-v0.7 + platformio check -e mppt_1210_hus ## Additional firmware documentation (docs folder) -- [MPPT charger firmware details](docs/firmware.md) -- [Charger state machine](docs/charger.md) +- [Charge controller basic concepts](docs/charger-concepts.md) + +- [Firmware module overview](docs/module-overview.md) diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index e88c53f..47b7ad7 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -30,15 +30,15 @@ void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capa bat->nominal_capacity = nominal_capacity; // 1C should be safe for all batteries - bat->charge_current_max = bat->nominal_capacity; + bat->charge_current_max = bat->nominal_capacity; bat->discharge_current_max = bat->nominal_capacity; bat->time_limit_recharge = 60; // sec - bat->topping_duration = 120*60; // sec + bat->topping_duration = 120*60; // sec - bat->charge_temp_max = 50; - bat->charge_temp_min = -10; - bat->discharge_temp_max = 50; + bat->charge_temp_max = 50; + bat->charge_temp_min = -10; + bat->discharge_temp_max = 50; bat->discharge_temp_min = -10; switch (type) @@ -46,91 +46,100 @@ void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capa case BAT_TYPE_FLOODED: case BAT_TYPE_AGM: case BAT_TYPE_GEL: - bat->voltage_absolute_max = num_cells * 2.45; - bat->topping_voltage = num_cells * 2.4; - bat->voltage_recharge = num_cells * 2.3; + bat->voltage_absolute_max = static_cast(num_cells) * 2.45F; + bat->topping_voltage = static_cast(num_cells) * 2.4F; + bat->voltage_recharge = static_cast(num_cells) * 2.3F; // Cell-level thresholds based on EN 62509:2011 (both thresholds current-compensated) - bat->voltage_load_disconnect = num_cells * 1.95; - bat->voltage_load_reconnect = num_cells * 2.10; + bat->voltage_load_disconnect = static_cast(num_cells) * 1.95F; + bat->voltage_load_reconnect = static_cast(num_cells) * 2.10F; // assumption: Battery selection matching charge controller - bat->internal_resistance = num_cells * (1.95 - 1.80) / DISCHARGE_CURRENT_MAX; + bat->internal_resistance = static_cast(num_cells) * (1.95F - 1.80F) / + DISCHARGE_CURRENT_MAX; - bat->voltage_absolute_min = num_cells * 1.6; + bat->voltage_absolute_min = static_cast(num_cells) * 1.6F; // Voltages during idle (no charging/discharging current) - bat->ocv_full = num_cells * ((type == BAT_TYPE_FLOODED) ? 2.10 : 2.15); - bat->ocv_empty = num_cells * 1.90; + bat->ocv_full = static_cast(num_cells) * + ((type == BAT_TYPE_FLOODED) ? 2.10F : 2.15F); + bat->ocv_empty = static_cast(num_cells) * 1.90F; // https://batteryuniversity.com/learn/article/charging_the_lead_acid_battery - bat->topping_current_cutoff = bat->nominal_capacity * 0.04; // 3-5 % of C/1 + bat->topping_current_cutoff = bat->nominal_capacity * 0.04F; // 3-5 % of C/1 - bat->trickle_enabled = true; - bat->trickle_recharge_time = 30*60; + bat->trickle_enabled = true; + bat->trickle_recharge_time = 30*60; // Values as suggested in EN 62509:2011 - bat->trickle_voltage = num_cells * ((type == BAT_TYPE_FLOODED) ? 2.35 : 2.3); + bat->trickle_voltage = static_cast(num_cells) * + ((type == BAT_TYPE_FLOODED) ? 2.35F : 2.3F); // Enable for flooded batteries only, according to // https://discoverbattery.com/battery-101/equalizing-flooded-batteries-only - bat->equalization_enabled = false; + bat->equalization_enabled = false; // Values as suggested in EN 62509:2011 - bat->equalization_voltage = num_cells * ((type == BAT_TYPE_FLOODED) ? 2.50 : 2.45); - bat->equalization_duration = 60*60; - bat->equalization_current_limit = (1.0 / 7.0) * bat->nominal_capacity; + bat->equalization_voltage = static_cast(num_cells) * + ((type == BAT_TYPE_FLOODED) ? 2.50F : 2.45F); + bat->equalization_duration = 60*60; + bat->equalization_current_limit = (1.0F / 7.0F) * bat->nominal_capacity; bat->equalization_trigger_days = 60; bat->equalization_trigger_deep_cycles = 10; - bat->temperature_compensation = -0.003; // -3 mV/°C/cell + bat->temperature_compensation = -0.003F; // -3 mV/°C/cell break; case BAT_TYPE_LFP: - bat->voltage_absolute_max = num_cells * 3.60; - bat->topping_voltage = num_cells * 3.55; // CV voltage - bat->voltage_recharge = num_cells * 3.35; + bat->voltage_absolute_max = static_cast(num_cells) * 3.60F; + bat->topping_voltage = static_cast(num_cells) * 3.55F; // CV voltage + bat->voltage_recharge = static_cast(num_cells) * 3.35F; - bat->voltage_load_disconnect = num_cells * 3.0; - bat->voltage_load_reconnect = num_cells * 3.15; + bat->voltage_load_disconnect = static_cast(num_cells) * 3.00F; + bat->voltage_load_reconnect = static_cast(num_cells) * 3.15F; // 5% voltage drop at max current - bat->internal_resistance = bat->voltage_load_disconnect * 0.05 / DISCHARGE_CURRENT_MAX; - bat->voltage_absolute_min = num_cells * 2.0; + bat->internal_resistance = bat->voltage_load_disconnect * 0.05F / + DISCHARGE_CURRENT_MAX; + bat->voltage_absolute_min = static_cast(num_cells) * 2.0F; - bat->ocv_full = num_cells * 3.4; // will give really nonlinear SOC calculation - bat->ocv_empty = num_cells * 3.0; // because of flat OCV of LFP cells... + // will give really nonlinear SOC calculation because of flat OCV of LFP cells... + bat->ocv_full = static_cast(num_cells) * 3.4F; + bat->ocv_empty = static_cast(num_cells) * 3.0F; // C/10 cut-off at end of CV phase by default bat->topping_current_cutoff = bat->nominal_capacity / 10; bat->trickle_enabled = false; bat->equalization_enabled = false; - bat->temperature_compensation = 0.0; + bat->temperature_compensation = 0; bat->charge_temp_min = 0; break; case BAT_TYPE_NMC: case BAT_TYPE_NMC_HV: - bat->topping_voltage = num_cells * ((type == BAT_TYPE_NMC_HV) ? 4.35 : 4.20); - bat->voltage_absolute_max = bat->topping_voltage + num_cells * 0.05; - bat->voltage_recharge = num_cells * 3.9; + bat->topping_voltage = static_cast(num_cells) * + ((type == BAT_TYPE_NMC_HV) ? 4.35F : 4.20F); + bat->voltage_absolute_max = bat->topping_voltage + + static_cast(num_cells) * 0.05F; + bat->voltage_recharge = static_cast(num_cells) * 3.9F; - bat->voltage_load_disconnect = num_cells * 3.3; - bat->voltage_load_reconnect = num_cells * 3.6; + bat->voltage_load_disconnect = static_cast(num_cells) * 3.3F; + bat->voltage_load_reconnect = static_cast(num_cells) * 3.6F; // 5% voltage drop at max current - bat->internal_resistance = bat->voltage_load_disconnect * 0.05 / DISCHARGE_CURRENT_MAX; + bat->internal_resistance = bat->voltage_load_disconnect * 0.05F / + DISCHARGE_CURRENT_MAX; - bat->voltage_absolute_min = num_cells * 2.5; + bat->voltage_absolute_min = static_cast(num_cells) * 2.5F; - bat->ocv_full = num_cells * 4.0; - bat->ocv_empty = num_cells * 3.0; + bat->ocv_full = static_cast(num_cells) * 4.0F; + bat->ocv_empty = static_cast(num_cells) * 3.0F; // C/10 cut-off at end of CV phase by default - bat->topping_current_cutoff = bat->nominal_capacity / 10; + bat->topping_current_cutoff = bat->nominal_capacity / 10; bat->trickle_enabled = false; bat->equalization_enabled = false; - bat->temperature_compensation = 0.0; + bat->temperature_compensation = 0; bat->charge_temp_min = 0; break; @@ -161,8 +170,8 @@ bool battery_conf_check(BatConf *bat_conf) ); */ - return - (bat_conf->voltage_load_reconnect > (bat_conf->voltage_load_disconnect + 0.4) && + return ( + bat_conf->voltage_load_reconnect > (bat_conf->voltage_load_disconnect + 0.4) && bat_conf->voltage_recharge < (bat_conf->topping_voltage - 0.4) && bat_conf->voltage_recharge > (bat_conf->voltage_load_disconnect + 1) && bat_conf->voltage_load_disconnect > (bat_conf->voltage_absolute_min + 0.4) && @@ -177,7 +186,7 @@ bool battery_conf_check(BatConf *bat_conf) (bat_conf->trickle_enabled == false || (bat_conf->trickle_voltage < bat_conf->topping_voltage && bat_conf->trickle_voltage > bat_conf->voltage_load_disconnect)) - ); + ); } void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *charger) @@ -221,7 +230,7 @@ void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *char bool battery_conf_changed(BatConf *a, BatConf *b) { - if ( + return ( a->topping_voltage != b->topping_voltage || a->voltage_recharge != b->voltage_recharge || a->voltage_load_reconnect != b->voltage_load_reconnect || @@ -241,15 +250,10 @@ bool battery_conf_changed(BatConf *a, BatConf *b) a->temperature_compensation != b->temperature_compensation || a->internal_resistance != b->internal_resistance || a->wire_resistance != b->wire_resistance - ) { - return true; - } - else { - return false; - } + ); } -void Charger::detect_num_batteries(BatConf *bat) +void Charger::detect_num_batteries(BatConf *bat) const { if (port->bus->voltage > bat->voltage_absolute_min * 2 && port->bus->voltage < bat->voltage_absolute_max * 2) @@ -288,7 +292,7 @@ void Charger::update_soc(BatConf *bat_conf) soc = soc_filtered / 100; } - discharged_Ah += -port->current / 3600.0; // charged current is positive: change sign + discharged_Ah += -port->current / 3600.0F; // charged current is positive: change sign } void Charger::enter_state(int next_state) @@ -315,8 +319,8 @@ void Charger::discharge_control(BatConf *bat_conf) else { // slowly adapt new measurements with low-pass filter usable_capacity = - 0.8 * usable_capacity + - 0.2 * discharged_Ah; + 0.8F * usable_capacity + + 0.2F * discharged_Ah; } // simple SOH estimation @@ -353,7 +357,7 @@ void Charger::discharge_control(BatConf *bat_conf) // discharging currently not allowed. should we allow it? if (port->bus->voltage >= - port->bus->src_control_voltage(bat_conf->voltage_absolute_min + 0.1)) + port->bus->src_control_voltage(bat_conf->voltage_absolute_min + 0.1F)) { dev_stat.clear_error(ERR_BAT_UNDERVOLTAGE); } @@ -391,7 +395,7 @@ void Charger::charge_control(BatConf *bat_conf) } if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE) && - port->bus->voltage < (bat_conf->voltage_absolute_max - 0.5) * port->bus->series_multiplier) + port->bus->voltage < (bat_conf->voltage_absolute_max - 0.5F) * port->bus->series_multiplier) { dev_stat.clear_error(ERR_BAT_OVERVOLTAGE); } @@ -523,7 +527,7 @@ void Charger::charge_control(BatConf *bat_conf) } } -void Charger::init_terminal(BatConf *bat) +void Charger::init_terminal(BatConf *bat) const { port->bus->sink_voltage_bound = bat->topping_voltage; port->bus->src_voltage_bound = bat->voltage_load_disconnect; @@ -537,12 +541,14 @@ void Charger::init_terminal(BatConf *bat) * droop_res is multiplied with number of series connected batteries to calculate control * voltage, so we need to divide by number of batteries here for correction */ - port->bus->sink_droop_res = -bat->wire_resistance / port->bus->series_multiplier; + port->bus->sink_droop_res = -bat->wire_resistance / + static_cast(port->bus->series_multiplier); /* * In discharging direction also include battery internal resistance for current-compensation * of voltage setpoints */ - port->bus->src_droop_res = -bat->wire_resistance / port->bus->series_multiplier + port->bus->src_droop_res = -bat->wire_resistance / + static_cast(port->bus->series_multiplier) - bat->internal_resistance; } diff --git a/src/bat_charger.h b/src/bat_charger.h index 9fd4923..5aeccdb 100644 --- a/src/bat_charger.h +++ b/src/bat_charger.h @@ -362,7 +362,7 @@ public: /** * Detect if two batteries are connected in series (12V/24V auto-detection) */ - void detect_num_batteries(BatConf *bat); + void detect_num_batteries(BatConf *bat) const; /** * Discharging control update (for load output), should be called once per second @@ -386,7 +386,7 @@ public: * * @param bat Configuration to be used for terminal setpoints */ - void init_terminal(BatConf *bat); + void init_terminal(BatConf *bat) const; private: void enter_state(int next_state); diff --git a/src/daq.cpp b/src/daq.cpp index 1cc867a..f599b00 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -32,8 +32,6 @@ static volatile uint32_t adc_filtered[NUM_ADC_CH] = {}; static volatile AdcAlert adc_alerts_upper[NUM_ADC_CH] = {}; static volatile AdcAlert adc_alerts_lower[NUM_ADC_CH] = {}; -extern DeviceStatus dev_stat; - /** * Average value for ADC channel * diff --git a/src/daq_driver.c b/src/daq_driver.c index a7d4392..991a611 100644 --- a/src/daq_driver.c +++ b/src/daq_driver.c @@ -268,6 +268,8 @@ static inline void adc_trigger_conversion(struct k_timer *timer_id) static void DMA1_Channel1_IRQHandler(void *args) { + ARG_UNUSED(args); + if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) // Test if transfer completed on DMA channel 1 { for (unsigned int i = 0; i < num_adc1_ch; i++) { diff --git a/src/data_nodes.cpp b/src/data_nodes.cpp index 15b5126..4c03963 100644 --- a/src/data_nodes.cpp +++ b/src/data_nodes.cpp @@ -480,8 +480,9 @@ void data_nodes_update_conf() // TODO: check also for changes in Load/USB EnDefault changed = true; // temporary hack - if (changed) + if (changed) { eeprom_store_data(); + } } void data_nodes_init() diff --git a/src/dcdc.h b/src/dcdc.h index b79b714..a6fa08f 100644 --- a/src/dcdc.h +++ b/src/dcdc.h @@ -110,8 +110,8 @@ public: int32_t power_good_timestamp; ///< Last time the DC/DC reached above minimum output power // maximum allowed values - float ls_current_max; ///< Maximum low-side (inductor) current - float hs_voltage_max; ///< Maximum high-side voltage + float ls_current_max = 0; ///< Maximum low-side (inductor) current + float hs_voltage_max = 0; ///< Maximum high-side voltage float ls_voltage_max; ///< Maximum low-side voltage float ls_voltage_min; ///< Minimum low-side voltage, e.g. for driver supply float output_power_min; ///< Minimum output power (if lower, DC/DC is switched off) diff --git a/src/device_status.cpp b/src/device_status.cpp index 5e5c8dc..b352597 100644 --- a/src/device_status.cpp +++ b/src/device_status.cpp @@ -134,11 +134,11 @@ void DeviceStatus::update_min_max_values() } #endif - if (charger.bat_temperature > bat_temp_max) { + if (charger.bat_temperature > static_cast(bat_temp_max)) { bat_temp_max = charger.bat_temperature; } - if (internal_temp > int_temp_max) { + if (internal_temp > static_cast(int_temp_max)) { int_temp_max = internal_temp; } } diff --git a/src/ext/ext.cpp b/src/ext/ext.cpp index ea94942..59430c6 100644 --- a/src/ext/ext.cpp +++ b/src/ext/ext.cpp @@ -11,13 +11,13 @@ // necessary for PlatformIO debug session void std::__throw_bad_alloc() { - while (1) {} + while (true) {} } // necessary to compile Zephyr v2.2 void std::__throw_length_error(char const*) { - while (1) {} + while (true) {} } // we use code to self register objects at construction time. diff --git a/src/leds.cpp b/src/leds.cpp index 62c0d2a..e3c48a1 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -10,7 +10,7 @@ static int led_states[NUM_LEDS]; // must be one of enum LedState static int trigger_timeout[NUM_LEDS]; // seconds until trigger should end -static int blink_state = 1; // global state of all blinking LEDs +static bool blink_state = true; // global state of all blinking LEDs static bool charging = false; @@ -26,7 +26,7 @@ void leds_update_thread() { int led_count = 0; int flicker_count = 0; - bool flicker_state = 1; + bool flicker_state = true; struct device *led_devs[NUM_LED_PINS]; int wdt_channel = sw_watchdog_register(1000); @@ -37,7 +37,7 @@ void leds_update_thread() leds_init(); - while (1) { + while (true) { sw_watchdog_feed(wdt_channel); diff --git a/src/main.cpp b/src/main.cpp index 0818641..0549bf5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,7 +76,7 @@ void main(void) watchdog_start(); sw_watchdog_start(); - while (1) { + while (true) { charger.discharge_control(&bat_conf); charger.charge_control(&bat_conf); @@ -99,7 +99,7 @@ void control_thread() uint32_t last_call = 0; int wdt_channel = sw_watchdog_register(200); - while (1) { + while (true) { bool charging = false; @@ -109,7 +109,7 @@ void control_thread() daq_update(); // alerts should trigger only for transients, so update based on actual voltage - daq_set_lv_alerts(lv_terminal.bus->voltage * 1.2, lv_terminal.bus->voltage * 0.8); + daq_set_lv_alerts(lv_terminal.bus->voltage * 1.2F, lv_terminal.bus->voltage * 0.8F); lv_terminal.update_bus_current_margins(); @@ -181,7 +181,7 @@ void ext_mgr_thread() // quite long watchdog timeout as we might be dealing with slow communication (e.g. modems // using AT commands via serial interface) - while (1) { + while (true) { sw_watchdog_feed(wdt_channel); diff --git a/src/power_port.cpp b/src/power_port.cpp index b12377a..6e53470 100644 --- a/src/power_port.cpp +++ b/src/power_port.cpp @@ -30,14 +30,14 @@ void PowerPort::energy_balance() { // remark: timespan = 1s, so no multiplication with time necessary for energy calculation if (current >= 0.0F) { - pos_energy_Wh += bus->voltage * current / 3600.0; + pos_energy_Wh += bus->voltage * current / 3600.0F; } else { - neg_energy_Wh -= bus->voltage * current / 3600.0; + neg_energy_Wh -= bus->voltage * current / 3600.0F; } } -void PowerPort::update_bus_current_margins() +void PowerPort::update_bus_current_margins() const { // charging direction of battery bus->sink_current_margin = pos_current_limit - current; diff --git a/src/power_port.h b/src/power_port.h index d42a324..9b3c17f 100644 --- a/src/power_port.h +++ b/src/power_port.h @@ -176,32 +176,32 @@ public: /** * Measured current through this port (positive sign = current into the external device) */ - float current; + float current = 0; /** * Product of port current and bus voltage */ - float power; + float power = 0; /** * Maximum positive current (valid values >= 0.0) */ - float pos_current_limit; + float pos_current_limit = 0; /** * Maximum negative current (valid values <= 0.0) */ - float neg_current_limit; + float neg_current_limit = 0; /** * Cumulated energy in positive current direction since last counter reset (Wh) */ - float pos_energy_Wh; + float pos_energy_Wh = 0; /** * Cumulated energy in negative current direction since last counter reset (Wh) */ - float neg_energy_Wh; + float neg_energy_Wh = 0; /** * Constructor assigning the port to a DC bus @@ -241,7 +241,7 @@ public: * This function has to be called using the port defining the bus control targets, i.e. * the battery, the solar panel or the DC grid. */ - void update_bus_current_margins(); + void update_bus_current_margins() const; }; #endif /* POWER_PORT_H */ diff --git a/src/setup.cpp b/src/setup.cpp index ac1fe5b..1c1cee5 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -88,7 +88,11 @@ uint32_t timestamp; #include -static inline void timestamp_inc(struct k_timer *timer_id) { timestamp++; } +static inline void timestamp_inc(struct k_timer *timer_id) +{ + ARG_UNUSED(timer_id); + timestamp++; +} void setup() {