Refactoring of files and function names
- Files for interfaces connected via UEXT are stored in separate uext/ directory - UEXT init and process function names independent of connected module - LED function names adjusted to start with leds_ - main.cpp structure simplified
This commit is contained in:
+78
-135
@@ -18,9 +18,9 @@
|
||||
|
||||
#include "mbed.h"
|
||||
|
||||
#include "thingset.h"
|
||||
#include "pcb.h"
|
||||
#include "config.h"
|
||||
#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.
|
||||
@@ -29,15 +29,13 @@
|
||||
#include "battery.h" // battery settings
|
||||
#include "charger.h" // charger state machine
|
||||
#include "adc_dma.h" // ADC using DMA and conversion to measurement values
|
||||
#include "interface.h" // communication interfaces, displays, etc.
|
||||
#include "uext.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"
|
||||
#include "log.h"
|
||||
#include "data_objects.h"
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// global variables
|
||||
#include "leds.h" // LED switching using charlieplexing
|
||||
#include "log.h" // log data (error memory, min/max measurements, etc.)
|
||||
#include "data_objects.h" // for access to internal data via ThingSet
|
||||
#include "thingset_serial.h" // UART or USB serial communication
|
||||
|
||||
Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial");
|
||||
|
||||
@@ -45,7 +43,7 @@ dcdc_t dcdc = {};
|
||||
power_port_t hs_port = {}; // high-side (solar for typical MPPT)
|
||||
power_port_t ls_port = {}; // low-side (battery for typical MPPT)
|
||||
power_port_t *bat_port = NULL;
|
||||
pwm_switch_t pwm_switch = {}; // only for PWM charger
|
||||
pwm_switch_t pwm_switch = {}; // only necessary for PWM charger
|
||||
battery_conf_t bat_conf; // actual (used) battery configuration
|
||||
battery_conf_t bat_conf_user; // temporary storage where the user can write to
|
||||
battery_state_t bat_state; // battery state information
|
||||
@@ -55,17 +53,11 @@ extern ThingSet ts; // defined in data_objects.cpp
|
||||
|
||||
time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable)
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// function prototypes
|
||||
|
||||
void setup();
|
||||
|
||||
void interfaces_init();
|
||||
void interfaces_process_asap(); // called each time the main loop is in idle
|
||||
void interfaces_process_1s(); // called every second
|
||||
|
||||
|
||||
void system_control() // called by control timer (see hardware.cpp)
|
||||
/** High priority function for DC/DC control and safety functions
|
||||
*
|
||||
* Called by control timer with 10 Hz frequency (see hardware.cpp).
|
||||
*/
|
||||
void system_control()
|
||||
{
|
||||
static int counter = 0;
|
||||
|
||||
@@ -96,121 +88,13 @@ void system_control() // called by control timer (see hardware.cpp)
|
||||
counter++;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/** Main function including initialization and continuous loop
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
setup();
|
||||
wait(2); // safety feature: be able to re-flash before starting
|
||||
|
||||
interfaces_init();
|
||||
init_watchdog(10); // 10s should be enough for communication ports
|
||||
|
||||
switch(dcdc.mode)
|
||||
{
|
||||
case MODE_NANOGRID:
|
||||
power_port_init_nanogrid(&hs_port);
|
||||
power_port_init_bat(&ls_port, &bat_conf);
|
||||
bat_port = &ls_port;
|
||||
break;
|
||||
case MODE_MPPT_BUCK: // typical MPPT charge controller operation
|
||||
power_port_init_solar(&hs_port);
|
||||
power_port_init_bat(&ls_port, &bat_conf);
|
||||
bat_port = &ls_port;
|
||||
break;
|
||||
case MODE_MPPT_BOOST: // for charging of e-bike battery via solar panel
|
||||
power_port_init_solar(&ls_port);
|
||||
power_port_init_bat(&hs_port, &bat_conf);
|
||||
bat_port = &hs_port;
|
||||
break;
|
||||
}
|
||||
control_timer_start(CONTROL_FREQUENCY);
|
||||
|
||||
time_t last_call = timestamp;
|
||||
|
||||
// the main loop is suitable for slow tasks like communication (even blocking wait allowed)
|
||||
while(1) {
|
||||
|
||||
interfaces_process_asap();
|
||||
|
||||
update_soc_led(bat_state.soc);
|
||||
|
||||
time_t now = timestamp;
|
||||
if (now >= last_call + 1 || now < last_call) { // called once per second (or slower if blocking wait occured somewhere)
|
||||
|
||||
//printf("Still alive... time: %d, mode: %d\n", (int)time(NULL), dcdc.mode);
|
||||
|
||||
charger_state_machine(bat_port, &bat_conf, &bat_state, bat_port->voltage, bat_port->current);
|
||||
|
||||
load_state_machine(&load, ls_port.input_allowed);
|
||||
|
||||
eeprom_update();
|
||||
interfaces_process_1s();
|
||||
toggle_led_blink();
|
||||
|
||||
last_call = now;
|
||||
}
|
||||
feed_the_dog();
|
||||
sleep(); // wake-up by timer interrupts
|
||||
}
|
||||
}
|
||||
|
||||
void interfaces_init()
|
||||
{
|
||||
uart_serial_init(&serial);
|
||||
//usb_serial_init();
|
||||
|
||||
#ifdef GSM_ENABLED
|
||||
gsm_init();
|
||||
#endif
|
||||
|
||||
#ifdef LORA_ENABLED
|
||||
lora_init();
|
||||
#endif
|
||||
|
||||
#ifdef WIFI_ENABLED
|
||||
wifi_init();
|
||||
#endif
|
||||
}
|
||||
|
||||
void interfaces_process_asap()
|
||||
{
|
||||
uart_serial_process();
|
||||
usb_serial_process();
|
||||
update_rxtx_led();
|
||||
}
|
||||
|
||||
void interfaces_process_1s()
|
||||
{
|
||||
#ifdef OLED_ENABLED
|
||||
oled_output(&dcdc, &hs_port, &ls_port, &bat_state, &load);
|
||||
#endif
|
||||
|
||||
#ifdef GSM_ENABLED
|
||||
gsm_process();
|
||||
#endif
|
||||
|
||||
#ifdef LORA_ENABLED
|
||||
lora_process();
|
||||
#endif
|
||||
|
||||
#ifdef WIFI_ENABLED
|
||||
wifi_process();
|
||||
#endif
|
||||
|
||||
// will only actually publish the messages if enabled via ThingSet
|
||||
uart_serial_pub();
|
||||
usb_serial_pub();
|
||||
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void setup()
|
||||
{
|
||||
serial.baud(115200);
|
||||
|
||||
leds_init();
|
||||
led_timer_start(NUM_LEDS * 60); // 60 Hz
|
||||
|
||||
battery_conf_init(&bat_conf, BATTERY_TYPE, BATTERY_NUM_CELLS, BATTERY_CAPACITY);
|
||||
battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values
|
||||
@@ -225,18 +109,77 @@ void setup()
|
||||
half_bridge_init(PWM_FREQUENCY, 300, 12 / dcdc.hs_voltage_max, 0.97); // lower duty limit might have to be adjusted dynamically depending on LS voltage
|
||||
#endif
|
||||
|
||||
// Configuration from EEPROM
|
||||
data_objects_read_eeprom();
|
||||
ts.set_conf_callback(data_objects_update_conf); // after each configuration change, data should be written back to EEPROM
|
||||
|
||||
// ADC, DMA and sensor calibration
|
||||
adc_setup();
|
||||
dma_setup();
|
||||
|
||||
adc_timer_start(1000); // 1 kHz
|
||||
|
||||
wait(0.5); // wait for ADC to collect some measurement values
|
||||
|
||||
update_measurements(&dcdc, &bat_state, &load, &hs_port, &ls_port);
|
||||
calibrate_current_sensors(&dcdc, &load);
|
||||
|
||||
// Communication interfaces
|
||||
uart_serial_init(&serial);
|
||||
uext_init();
|
||||
|
||||
init_watchdog(10); // 10s should be enough for communication ports
|
||||
|
||||
// Setup of DC/DC power stage
|
||||
switch(dcdc.mode) {
|
||||
case MODE_NANOGRID:
|
||||
power_port_init_nanogrid(&hs_port);
|
||||
power_port_init_bat(&ls_port, &bat_conf);
|
||||
bat_port = &ls_port;
|
||||
break;
|
||||
case MODE_MPPT_BUCK: // typical MPPT charge controller operation
|
||||
power_port_init_solar(&hs_port);
|
||||
power_port_init_bat(&ls_port, &bat_conf);
|
||||
bat_port = &ls_port;
|
||||
break;
|
||||
case MODE_MPPT_BOOST: // for charging of e-bike battery via solar panel
|
||||
power_port_init_solar(&ls_port);
|
||||
power_port_init_bat(&hs_port, &bat_conf);
|
||||
bat_port = &hs_port;
|
||||
break;
|
||||
}
|
||||
|
||||
wait(2); // safety feature: be able to re-flash before starting
|
||||
control_timer_start(CONTROL_FREQUENCY);
|
||||
|
||||
// the main loop is suitable for slow tasks like communication (even blocking wait allowed)
|
||||
time_t last_call = timestamp;
|
||||
while (1) {
|
||||
|
||||
thingset_serial_process_asap();
|
||||
uext_process_asap();
|
||||
|
||||
leds_update_rxtx();
|
||||
|
||||
time_t now = timestamp;
|
||||
if (now >= last_call + 1 || now < last_call) { // called once per second (or slower if blocking wait occured somewhere)
|
||||
|
||||
//printf("Still alive... time: %d, mode: %d\n", (int)time(NULL), dcdc.mode);
|
||||
|
||||
charger_state_machine(bat_port, &bat_conf, &bat_state, bat_port->voltage, bat_port->current);
|
||||
|
||||
load_state_machine(&load, ls_port.input_allowed);
|
||||
|
||||
eeprom_update();
|
||||
|
||||
leds_update_soc(bat_state.soc);
|
||||
leds_toggle_blink();
|
||||
|
||||
uext_process_1s();
|
||||
thingset_serial_process_1s();
|
||||
|
||||
last_call = now;
|
||||
}
|
||||
feed_the_dog();
|
||||
sleep(); // wake-up by timer interrupts
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user