/* * Copyright (c) The Libre Solar Project Contributors * * SPDX-License-Identifier: Apache-2.0 */ #ifndef UNIT_TEST #include #include #include #include #include #include "helper.h" #include "setup.h" #include #include "bat_charger.h" // battery settings and charger state machine #include "daq.h" // ADC using DMA and conversion to measurement values #include "data_objects.h" // for access to internal data via ThingSet #include "dcdc.h" // DC/DC converter control (hardware independent) #include "device_status.h" // log data (error memory, min/max measurements, etc.) #include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc. #include "leds.h" // LED switching using charlieplexing #include "load.h" // load and USB output management #if BOARD_HAS_PWM_PORT #include "pwm_switch.h" // PWM charge controller #endif int main(void) { printf("Hardware: Libre Solar %s (%s)\n", DT_PROP(DT_PATH(pcb), type), DT_PROP(DT_PATH(pcb), version_str)); printf("Firmware: %s\n", FIRMWARE_VERSION_ID); task_wdt_init(DEVICE_DT_GET_OR_NULL(DT_NODELABEL(iwdg))); setup(); battery_conf_init(&bat_conf, CONFIG_BAT_TYPE, CONFIG_BAT_NUM_CELLS, CONFIG_BAT_CAPACITY_AH); battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values #if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR solar_terminal.init_solar(); #endif #if CONFIG_HV_TERMINAL_NANOGRID grid_terminal.init_nanogrid(); #endif // make sure loaded configuration is valid data_objects_init(); // Data Acquisition (DAQ) setup daq_setup(); #if BOARD_HAS_DCDC // requires the reference voltage, which may be calculated from initial measurements daq_set_hv_limit(DT_PROP(DT_PATH(pcb), hs_voltage_max)); #endif charger.detect_num_batteries(&bat_conf); // check if we have 24V instead of 12V system charger.init_terminal(&bat_conf); #if BOARD_HAS_LOAD_OUTPUT load.set_voltage_limits(bat_conf.load_disconnect_voltage, bat_conf.load_reconnect_voltage, bat_conf.absolute_max_voltage); #endif #if BOARD_HAS_USB_OUTPUT usb_pwr.set_voltage_limits(bat_conf.load_disconnect_voltage - 0.1f, // keep on longer than load bat_conf.load_reconnect_voltage, bat_conf.absolute_max_voltage); #endif // wait until all threads are spawned before activating the watchdog k_sleep(K_MSEC(2500)); int64_t t_start = k_uptime_get(); while (true) { // loop runs exactly once per second and includes slow control tasks and energy calculation charger.discharge_control(&bat_conf); charger.charge_control(&bat_conf); // energy + soc calculation must be called exactly once per second #if BOARD_HAS_DCDC if (dcdc.state != DCDC_CONTROL_OFF) { hv_terminal.energy_balance(); } #endif #if BOARD_HAS_PWM_PORT if (pwm_switch.active() == 1) { pwm_switch.energy_balance(); } #endif lv_terminal.energy_balance(); #if BOARD_HAS_LOAD_OUTPUT if (load.state == 1) { load.energy_balance(); } #endif dev_stat.update_energy(); dev_stat.update_min_max_values(); charger.update_soc(&bat_conf); #if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && BOARD_HAS_DCDC if (dev_stat.has_error(ERR_DCDC_HS_MOSFET_SHORT)) { dcdc.fuse_destruction(); } #endif leds_update_1s(); #if BOARD_HAS_LOAD_OUTPUT leds_update_soc(charger.soc, flags_check(&load.error_flags, ERR_LOAD_SHEDDING)); #else leds_update_soc(charger.soc, false); #endif t_start += 1000; k_sleep(K_TIMEOUT_ABS_MS(t_start)); } } void control_thread() { int wdt_channel = task_wdt_add(200, task_wdt_callback, (void *)k_current_get()); while (true) { // control loop runs at approx. 10 Hz bool charging = false; task_wdt_feed(wdt_channel); // convert ADC readings to meaningful measurement values daq_update(); // alerts should trigger only for transients, so update based on actual voltage daq_set_lv_limits(lv_terminal.bus->voltage * 1.2F, lv_terminal.bus->voltage * 0.8F); lv_terminal.update_bus_current_margins(); #if BOARD_HAS_PWM_PORT pwm_switch.control(); charging |= pwm_switch.active(); #endif #if BOARD_HAS_DCDC hv_terminal.update_bus_current_margins(); dcdc.control(); // control of DC/DC including MPPT algorithm charging |= half_bridge_is_enabled(dcdc.half_bridge); #endif leds_set_charging(charging); #if BOARD_HAS_LOAD_OUTPUT load.control(); #endif #if BOARD_HAS_USB_OUTPUT usb_pwr.control(); #endif k_sleep(K_MSEC(100)); } } // 2s delay for control thread as a safety feature: be able to re-flash before starting // priority is set to -1 (=cooperative) to have higher priority than other threads incl. main K_THREAD_DEFINE(control_thread_id, 1024, control_thread, NULL, NULL, NULL, -1, 0, 2000); #endif // UNIT_TEST