From 5f1d76ef74082561dcf798558214be0db82d0410 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Sat, 8 Feb 2020 11:08:58 +0100 Subject: [PATCH] Dedicated setup.cpp to clean up main.cpp Also includes update of timestamp calculation in Zephyr. --- src/daq.cpp | 2 +- src/daq_mbed.cpp | 2 +- src/data_objects.cpp | 4 +- src/device_status.cpp | 2 +- src/ext/oled.cpp | 3 +- src/main_mbed.cpp | 55 +-------------------- src/main_zephyr.cpp | 81 +++++------------------------- src/setup.cpp | 96 ++++++++++++++++++++++++++++++++++++ src/{main.h => setup.h} | 16 ++++-- test/main.cpp | 70 -------------------------- test/tests_bat_charger.cpp | 2 +- test/tests_daq.cpp | 2 +- test/tests_dcdc.cpp | 2 +- test/tests_device_status.cpp | 2 +- test/tests_load.cpp | 2 +- test/tests_power_port.cpp | 2 +- zephyr/CMakeLists.txt | 1 + zephyr/prj.conf | 2 + 18 files changed, 137 insertions(+), 209 deletions(-) create mode 100644 src/setup.cpp rename src/{main.h => setup.h} (79%) diff --git a/src/daq.cpp b/src/daq.cpp index e7c1d98..e0a3e1c 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -14,7 +14,7 @@ #endif #include "mcu.h" -#include "main.h" +#include "setup.h" #include "debug.h" #include "pcb.h" // contains defines for pins diff --git a/src/daq_mbed.cpp b/src/daq_mbed.cpp index d1c177c..e882551 100644 --- a/src/daq_mbed.cpp +++ b/src/daq_mbed.cpp @@ -14,7 +14,7 @@ #include "mbed.h" #include "mcu.h" -#include "main.h" +#include "setup.h" #include "debug.h" #include "pcb.h" // contains defines for pins diff --git a/src/data_objects.cpp b/src/data_objects.cpp index dd409fa..126ca79 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -6,7 +6,7 @@ #include -#include "main.h" +#include "setup.h" #include "pcb.h" #include "config.h" @@ -28,8 +28,6 @@ const char* const firmware_version = "0.1"; const char* const firmware_commit = COMMIT_HASH; uint32_t device_id = CONFIG_DEVICE_ID; -extern uint32_t timestamp; - #if CONFIG_LV_TERMINAL_BATTERY #define bat_bus lv_bus #elif CONFIG_HV_TERMINAL_BATTERY diff --git a/src/device_status.cpp b/src/device_status.cpp index 8243cf2..7bd80c7 100644 --- a/src/device_status.cpp +++ b/src/device_status.cpp @@ -11,7 +11,7 @@ #include "config.h" #include "pcb.h" -#include "main.h" +#include "setup.h" #include "helper.h" //---------------------------------------------------------------------------- diff --git a/src/ext/oled.cpp b/src/ext/oled.cpp index 1f5c7b7..6478542 100644 --- a/src/ext/oled.cpp +++ b/src/ext/oled.cpp @@ -21,7 +21,8 @@ #include #endif -#include "main.h" +#include "setup.h" +#include "half_bridge.h" #include "pcb.h" #include "oled_ssd1306.h" diff --git a/src/main_mbed.cpp b/src/main_mbed.cpp index 5989720..379de0d 100644 --- a/src/main_mbed.cpp +++ b/src/main_mbed.cpp @@ -16,6 +16,7 @@ #include "thingset.h" // handles access to internal data via communication interfaces #include "pcb.h" // hardware-specific settings #include "config.h" // user-specific configuration +#include "setup.h" #include "half_bridge.h" // PWM generation for DC/DC converter #include "hardware.h" // hardware-related functions like watchdog, etc. @@ -31,60 +32,6 @@ #include "data_objects.h" // for access to internal data via ThingSet #include "bl_support.h" // Bootloader support from the application side -DcBus lv_bus; -PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT) - -#if CONFIG_HAS_DCDC_CONVERTER -DcBus hv_bus; -PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT) -PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter -#if CONFIG_HV_TERMINAL_NANOGRID -Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID); -#elif CONFIG_HV_TERMINAL_BATTERY -Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BOOST); -#else -Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK); -#endif // CONFIG_HV_TERMINAL -#endif - -#if CONFIG_HAS_PWM_SWITCH -PwmSwitch pwm_switch(&lv_bus); -#endif - -#if CONFIG_HAS_LOAD_OUTPUT -PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) -LoadOutput load(&load_terminal); -#endif - -#if CONFIG_HV_TERMINAL_SOLAR -PowerPort &solar_terminal = hv_terminal; -#elif CONFIG_LV_TERMINAL_SOLAR -PowerPort &solar_terminal = lv_terminal; -#elif CONFIG_PWM_TERMINAL_SOLAR -PowerPort &solar_terminal = pwm_switch; -#endif - -#if CONFIG_HV_TERMINAL_NANOGRID -PowerPort &grid_terminal = hv_terminal; -#endif - -#if CONFIG_LV_TERMINAL_BATTERY -PowerPort &bat_terminal = lv_terminal; -#elif CONFIG_HV_TERMINAL_BATTERY -PowerPort &bat_terminal = hv_terminal; -#endif - -Charger charger(&bat_terminal); - -BatConf bat_conf; // actual (used) battery configuration -BatConf bat_conf_user; // temporary storage where the user can write to - -DeviceStatus dev_stat; - -extern ThingSet ts; // defined in data_objects.cpp - -time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable) - Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial", 115200); /** Main function including initialization and continuous loop diff --git a/src/main_zephyr.cpp b/src/main_zephyr.cpp index 809306a..fda2dea 100644 --- a/src/main_zephyr.cpp +++ b/src/main_zephyr.cpp @@ -14,6 +14,7 @@ #include "thingset.h" // handles access to internal data via communication interfaces #include "pcb.h" // hardware-specific settings #include "config.h" // user-specific configuration +#include "setup.h" #include "half_bridge.h" // PWM generation for DC/DC converter #include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc. @@ -28,65 +29,11 @@ #include "device_status.h" // log data (error memory, min/max measurements, etc.) #include "data_objects.h" // for access to internal data via ThingSet -DcBus lv_bus; -PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT) - -#if CONFIG_HAS_DCDC_CONVERTER -DcBus hv_bus; -PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT) -PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter -#if CONFIG_HV_TERMINAL_NANOGRID -Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID); -#elif CONFIG_HV_TERMINAL_BATTERY -Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BOOST); -#else -Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK); -#endif // CONFIG_HV_TERMINAL -#endif - -#if CONFIG_HAS_PWM_SWITCH -PwmSwitch pwm_switch(&lv_bus); -#endif - -#if CONFIG_HAS_LOAD_OUTPUT -PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) -LoadOutput load(&load_terminal); -#endif - -#if CONFIG_HV_TERMINAL_SOLAR -PowerPort &solar_terminal = hv_terminal; -#elif CONFIG_LV_TERMINAL_SOLAR -PowerPort &solar_terminal = lv_terminal; -#elif CONFIG_PWM_TERMINAL_SOLAR -PowerPort &solar_terminal = pwm_switch; -#endif - -#if CONFIG_HV_TERMINAL_NANOGRID -PowerPort &grid_terminal = hv_terminal; -#endif - -#if CONFIG_LV_TERMINAL_BATTERY -PowerPort &bat_terminal = lv_terminal; -#elif CONFIG_HV_TERMINAL_BATTERY -PowerPort &bat_terminal = hv_terminal; -#endif - -Charger charger(&bat_terminal); - -BatConf bat_conf; // actual (used) battery configuration -BatConf bat_conf_user; // temporary storage where the user can write to - -DeviceStatus dev_stat; - -extern ThingSet ts; // defined in data_objects.cpp - -time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable) - void main(void) { - u32_t cnt = 0; + printf("Libre Solar Charge Controller: %s\n", CONFIG_BOARD); - printf("Booting Libre Solar Charge Controller: %s\n", CONFIG_BOARD); + setup(); battery_conf_init(&bat_conf, BAT_TYPE_GEL, // temporary!! @@ -126,15 +73,13 @@ void main(void) eeprom_update(); - cnt++; k_sleep(1000); } } - void control_thread() { - int counter = 0; + uint32_t last_call = 0; while (1) { // convert ADC readings to meaningful measurement values @@ -158,11 +103,9 @@ void control_thread() load.control(); - if (counter % CONTROL_FREQUENCY == 0) { - // called once per second (this timer is much more accurate than time(NULL) based on LSI) - // see also here: https://github.com/ARMmbed/mbed-os/issues/9065 - timestamp++; - counter = 0; + uint32_t now = k_uptime_get() / 1000; + if (now > last_call) { + last_call = now; // energy + soc calculation must be called exactly once per second #if CONFIG_HAS_DCDC_CONVERTER @@ -178,16 +121,19 @@ void control_thread() dev_stat.update_energy(); dev_stat.update_min_max_values(); charger.update_soc(&bat_conf); - } - counter++; + #if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && CONFIG_HAS_DCDC_CONVERTER + if (dev_stat.has_error(ERR_DCDC_HS_MOSFET_SHORT)) { + dcdc.fuse_destruction(); + } + #endif + } k_sleep(100); } } void ext_mgr_thread() { - u32_t cnt = 0; uint32_t last_call = 0; while (1) { uint32_t now = k_uptime_get() / 1000; @@ -196,7 +142,6 @@ void ext_mgr_thread() last_call = now; ext_mgr.process_1s(); } - cnt++; k_sleep(1); } } diff --git a/src/setup.cpp b/src/setup.cpp new file mode 100644 index 0000000..e65e312 --- /dev/null +++ b/src/setup.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2020 Martin Jäger / Libre Solar + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** @file + * + * @brief Setup of ports and other essential charge controller objects + */ + +#include "thingset.h" // handles access to internal data via communication interfaces +#include "pcb.h" // hardware-specific settings +#include "config.h" // user-specific configuration + +#include "half_bridge.h" // PWM generation for DC/DC converter +#include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc. +#include "dcdc.h" // DC/DC converter control (hardware independent) +#include "pwm_switch.h" // PWM charge controller +#include "bat_charger.h" // battery settings and charger state machine +#include "daq.h" // ADC using DMA and conversion to measurement values +#include "ext/ext.h" // communication interfaces, displays, etc. in UEXT connector +#include "eeprom.h" // external I2C EEPROM +#include "load.h" // load and USB output management +#include "leds.h" // LED switching using charlieplexing +#include "device_status.h" // log data (error memory, min/max measurements, etc.) +#include "data_objects.h" // for access to internal data via ThingSet + +DcBus lv_bus; +PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT) + +#if CONFIG_HAS_DCDC_CONVERTER +DcBus hv_bus; +PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter +#if CONFIG_HV_TERMINAL_NANOGRID +Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID); +#elif CONFIG_HV_TERMINAL_BATTERY +Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BOOST); +#else +Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK); +#endif // CONFIG_HV_TERMINAL +#endif + +#if CONFIG_HAS_PWM_SWITCH +PwmSwitch pwm_switch(&lv_bus); +#endif + +#if CONFIG_HAS_LOAD_OUTPUT +PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) +LoadOutput load(&load_terminal); +#endif + +#if CONFIG_HV_TERMINAL_SOLAR +PowerPort &solar_terminal = hv_terminal; +#elif CONFIG_LV_TERMINAL_SOLAR +PowerPort &solar_terminal = lv_terminal; +#elif CONFIG_PWM_TERMINAL_SOLAR +PowerPort &solar_terminal = pwm_switch; +#endif + +#if CONFIG_HV_TERMINAL_NANOGRID +PowerPort &grid_terminal = hv_terminal; +#endif + +#if CONFIG_LV_TERMINAL_BATTERY +PowerPort &bat_terminal = lv_terminal; +#elif CONFIG_HV_TERMINAL_BATTERY +PowerPort &bat_terminal = hv_terminal; +#endif + +Charger charger(&bat_terminal); + +BatConf bat_conf; // actual (used) battery configuration +BatConf bat_conf_user; // temporary storage where the user can write to + +DeviceStatus dev_stat; + +// current unix timestamp (independent of time(NULL), as it is user-configurable) +// uint32_t considered large enough, so we avoid 64-bit math (overflow in year 2106) +uint32_t timestamp; + +#ifdef __ZEPHYR__ + +#include + +static inline void timestamp_inc(struct k_timer *timer_id) { timestamp++; } + +void setup() +{ + static struct k_timer timestamp_timer; + k_timer_init(×tamp_timer, timestamp_inc, NULL); + k_timer_start(×tamp_timer, K_MSEC(1000), K_MSEC(1000)); +} + +#endif diff --git a/src/main.h b/src/setup.h similarity index 79% rename from src/main.h rename to src/setup.h index 1c5a1f5..913e851 100644 --- a/src/main.h +++ b/src/setup.h @@ -9,12 +9,11 @@ #include #include "bat_charger.h" -#include "power_port.h" #include "device_status.h" -#include "pwm_switch.h" -#include "half_bridge.h" #include "load.h" -#include "pcb.h" +#include "power_port.h" +#include "pwm_switch.h" +#include "thingset.h" extern DcBus lv_bus; extern PowerPort lv_terminal; @@ -41,3 +40,12 @@ extern BatConf bat_conf; extern BatConf bat_conf_user; extern LoadOutput load; + +extern ThingSet ts; // defined in data_objects.cpp + +extern uint32_t timestamp; + +/** + * Perform some device setup tasks (currently only used in Zephyr) + */ +void setup(); diff --git a/test/main.cpp b/test/main.cpp index b38718a..999033b 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -4,78 +4,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "thingset.h" // handles access to internal data via communication interfaces -#include "pcb.h" // hardware-specific settings -#include "config.h" // user-specific configuration - -#include "half_bridge.h" // PWM generation for DC/DC converter -#include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc. -#include "dcdc.h" // DC/DC converter control (hardware independent) -#include "pwm_switch.h" // PWM charge controller -#include "bat_charger.h" // battery settings and charger state machine -#include "daq.h" // ADC using DMA and conversion to measurement values -#include "eeprom.h" // external I2C EEPROM -#include "load.h" // load and USB output management -#include "leds.h" // LED switching using charlieplexing -#include "device_status.h" // device-level data (error memory, min/max measurements, etc.) -#include "data_objects.h" // for access to internal data via ThingSet - #include "tests.h" -DcBus lv_bus; -PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT) - -#if CONFIG_HAS_DCDC_CONVERTER -DcBus hv_bus; -PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT) -PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter -#if CONFIG_HV_TERMINAL_NANOGRID -Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_NANOGRID); -#elif CONFIG_HV_TERMINAL_BATTERY -Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BOOST); -#else -Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK); -#endif // CONFIG_HV_TERMINAL -#endif - -#if CONFIG_HAS_PWM_SWITCH -PwmSwitch pwm_switch(&lv_bus); -#endif - -#if CONFIG_HAS_LOAD_OUTPUT -PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) -LoadOutput load(&load_terminal); -#endif - -#if CONFIG_HV_TERMINAL_SOLAR -PowerPort &solar_terminal = hv_terminal; -#elif CONFIG_LV_TERMINAL_SOLAR -PowerPort &solar_terminal = lv_terminal; -#elif CONFIG_PWM_TERMINAL_SOLAR -PowerPort &solar_terminal = pwm_switch; -#endif - -#if CONFIG_HV_TERMINAL_NANOGRID -PowerPort &grid_terminal = hv_terminal; -#endif - -#if CONFIG_LV_TERMINAL_BATTERY -PowerPort &bat_terminal = lv_terminal; -#elif CONFIG_HV_TERMINAL_BATTERY -PowerPort &bat_terminal = hv_terminal; -#endif - -Charger charger(&lv_terminal); - -BatConf bat_conf; // actual (used) battery configuration -BatConf bat_conf_user; // temporary storage where the user can write to - -DeviceStatus dev_stat; - -extern ThingSet ts; // defined in data_objects.cpp - -time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable) - int main() { daq_tests(); diff --git a/test/tests_bat_charger.cpp b/test/tests_bat_charger.cpp index 7aad6fd..97964c7 100644 --- a/test/tests_bat_charger.cpp +++ b/test/tests_bat_charger.cpp @@ -9,7 +9,7 @@ #include #include -#include "main.h" +#include "setup.h" static void init_structs() { diff --git a/test/tests_daq.cpp b/test/tests_daq.cpp index 7ed9a6b..899ae0a 100644 --- a/test/tests_daq.cpp +++ b/test/tests_daq.cpp @@ -8,7 +8,7 @@ #include "daq.h" #include "daq_stub.h" -#include "main.h" +#include "setup.h" #include diff --git a/test/tests_dcdc.cpp b/test/tests_dcdc.cpp index c1dff39..ffc1c7c 100644 --- a/test/tests_dcdc.cpp +++ b/test/tests_dcdc.cpp @@ -12,7 +12,7 @@ #include #include -#include "main.h" +#include "setup.h" static void init_structs_buck() { diff --git a/test/tests_device_status.cpp b/test/tests_device_status.cpp index 0f8b36d..c3aa092 100644 --- a/test/tests_device_status.cpp +++ b/test/tests_device_status.cpp @@ -10,7 +10,7 @@ #include #include -#include "main.h" +#include "setup.h" void reset_counters_at_start_of_day() { diff --git a/test/tests_load.cpp b/test/tests_load.cpp index 82644ef..65629d2 100644 --- a/test/tests_load.cpp +++ b/test/tests_load.cpp @@ -12,7 +12,7 @@ #include #include -#include "main.h" +#include "setup.h" void load_init(LoadOutput *l) { diff --git a/test/tests_power_port.cpp b/test/tests_power_port.cpp index 51248b7..ef15521 100644 --- a/test/tests_power_port.cpp +++ b/test/tests_power_port.cpp @@ -12,7 +12,7 @@ #include #include -#include "main.h" +#include "setup.h" static AdcValues adcval; diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index e03eb56..7fd9e48 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -28,6 +28,7 @@ target_sources(app PRIVATE ../src/load_drv.cpp) target_sources(app PRIVATE ../src/main_zephyr.cpp) target_sources(app PRIVATE ../src/power_port.cpp) target_sources(app PRIVATE ../src/pwm_switch.cpp) +target_sources(app PRIVATE ../src/setup.cpp) target_sources(app PRIVATE ../src/ext/ext.cpp) target_sources(app PRIVATE ../src/ext/oled.cpp) target_sources(app PRIVATE ../src/ext/serial.cpp) diff --git a/zephyr/prj.conf b/zephyr/prj.conf index 5b7e247..16d7342 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -32,3 +32,5 @@ CONFIG_EXT_THINGSET_SERIAL=y # I2C OLED display in UEXT connector #CONFIG_EXT_OLED_DISPLAY=y + +CONFIG_REBOOT=y