Dedicated setup.cpp to clean up main.cpp

Also includes update of timestamp calculation in Zephyr.
This commit is contained in:
Martin Jaeger
2020-02-08 11:08:58 +01:00
parent 7940acccf4
commit 5f1d76ef74
18 changed files with 137 additions and 209 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
#endif
#include "mcu.h"
#include "main.h"
#include "setup.h"
#include "debug.h"
#include "pcb.h" // contains defines for pins
+1 -1
View File
@@ -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
+1 -3
View File
@@ -6,7 +6,7 @@
#include <stdio.h>
#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
+1 -1
View File
@@ -11,7 +11,7 @@
#include "config.h"
#include "pcb.h"
#include "main.h"
#include "setup.h"
#include "helper.h"
//----------------------------------------------------------------------------
+2 -1
View File
@@ -21,7 +21,8 @@
#include <drivers/gpio.h>
#endif
#include "main.h"
#include "setup.h"
#include "half_bridge.h"
#include "pcb.h"
#include "oled_ssd1306.h"
+1 -54
View File
@@ -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
+13 -68
View File
@@ -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);
}
}
+96
View File
@@ -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 <zephyr.h>
static inline void timestamp_inc(struct k_timer *timer_id) { timestamp++; }
void setup()
{
static struct k_timer timestamp_timer;
k_timer_init(&timestamp_timer, timestamp_inc, NULL);
k_timer_start(&timestamp_timer, K_MSEC(1000), K_MSEC(1000));
}
#endif
+12 -4
View File
@@ -9,12 +9,11 @@
#include <stdio.h>
#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();
-70
View File
@@ -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();
+1 -1
View File
@@ -9,7 +9,7 @@
#include <time.h>
#include <stdio.h>
#include "main.h"
#include "setup.h"
static void init_structs()
{
+1 -1
View File
@@ -8,7 +8,7 @@
#include "daq.h"
#include "daq_stub.h"
#include "main.h"
#include "setup.h"
#include <stdint.h>
+1 -1
View File
@@ -12,7 +12,7 @@
#include <stdio.h>
#include <math.h>
#include "main.h"
#include "setup.h"
static void init_structs_buck()
{
+1 -1
View File
@@ -10,7 +10,7 @@
#include <stdio.h>
#include <math.h>
#include "main.h"
#include "setup.h"
void reset_counters_at_start_of_day()
{
+1 -1
View File
@@ -12,7 +12,7 @@
#include <stdio.h>
#include <math.h>
#include "main.h"
#include "setup.h"
void load_init(LoadOutput *l)
{
+1 -1
View File
@@ -12,7 +12,7 @@
#include <stdio.h>
#include <math.h>
#include "main.h"
#include "setup.h"
static AdcValues adcval;
+1
View File
@@ -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)
+2
View File
@@ -32,3 +32,5 @@ CONFIG_EXT_THINGSET_SERIAL=y
# I2C OLED display in UEXT connector
#CONFIG_EXT_OLED_DISPLAY=y
CONFIG_REBOOT=y