Compare commits
45
Commits
@@ -0,0 +1,3 @@
|
||||
CompileFlags:
|
||||
CompilationDatabase: "./build"
|
||||
Add: ["-D__ARM_ARCH_PROFILE='M'"]
|
||||
@@ -35,20 +35,15 @@ jobs:
|
||||
- name: Coding style check
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y clang-format-15 colordiff
|
||||
sudo apt-get install -y colordiff
|
||||
pip install clang-format==23.1.*
|
||||
git status
|
||||
bash scripts/check-style.sh
|
||||
|
||||
- name: Build firmware
|
||||
run: |
|
||||
west build -b mppt_1210_hus app -p
|
||||
west build -b mppt_2420_hc app -p
|
||||
west build -b mppt_2420_hpx app -p
|
||||
west build -b mppt_2420_lc app -p
|
||||
west build -b mppt_2420_rc app -p
|
||||
west build -b nucleo_g474re app -p
|
||||
west build -b pwm_2420_lus app -p
|
||||
west twister -T app --integration --inline-logs
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
west build -b native_sim -t run tests -p
|
||||
west twister -T tests --integration --inline-logs
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
# folders generated by west
|
||||
build
|
||||
twister-out*
|
||||
|
||||
# editors
|
||||
.vscode/*
|
||||
|
||||
Vendored
-1
@@ -7,7 +7,6 @@
|
||||
"${workspaceFolder}/app/build/zephyr/include/generated",
|
||||
"${workspaceFolder}/../modules/hal/cmsis/CMSIS/Core/Include",
|
||||
"${workspaceFolder}/../modules/hal/stm32/stm32cube/stm32g0xx/soc",
|
||||
"${workspaceFolder}/../modules/oled-display/src",
|
||||
"${workspaceFolder}/../modules/thingset/src",
|
||||
"${workspaceFolder}/../modules/unity/src",
|
||||
"${workspaceFolder}/../zephyr/include",
|
||||
|
||||
@@ -2,3 +2,8 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_include_directories(include)
|
||||
|
||||
# scan headers for __subsystem driver API structs (required for DEVICE_API)
|
||||
zephyr_syscall_include_directories(include)
|
||||
|
||||
add_subdirectory(drivers)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
rsource "drivers/Kconfig"
|
||||
|
||||
+49
-29
@@ -16,19 +16,29 @@ config CONTROL_FREQUENCY
|
||||
Frequencies higher than 10 Hz caused issues with MPPT control during lab test with
|
||||
PV simulator. Might be different with actual solar panel.
|
||||
|
||||
config LIBRE_SOLAR_TYPE_ID
|
||||
config DCDC_DUTY_STEP_SIZE
|
||||
int
|
||||
default 3 if SOC_SERIES_STM32G4X
|
||||
default 1
|
||||
help
|
||||
Numeric Libre Solar hardware type ID
|
||||
Duty cycle change per control step of the DC/DC MPPT algorithm in timer counts of the
|
||||
half bridge.
|
||||
|
||||
Larger steps are used for faster microcontrollers, as their half-bridge timers have a
|
||||
higher resolution.
|
||||
|
||||
config HS_MOSFET_FAIL_SAFE_PROTECTION
|
||||
bool
|
||||
default y
|
||||
depends on $(dt_node_bool_prop,/pcb,ls-fuse)
|
||||
help
|
||||
If enabled, the DC/DC control function will will detect if the high-side MOSFET has failed
|
||||
If enabled, the DC/DC control function will detect if the high-side MOSFET has failed
|
||||
short and trip the fuse by switching the low-side MOSFET continuously on. This will prevent
|
||||
accidental overcharging of the battery.
|
||||
|
||||
Only available if the board has a fuse at the low-side terminal (ls-fuse property of the
|
||||
pcb devicetree node).
|
||||
|
||||
#
|
||||
# Visible (user-configurable) Kconfig symbols
|
||||
#
|
||||
@@ -37,7 +47,7 @@ menu "Charge Controller Setup"
|
||||
|
||||
choice
|
||||
prompt "High-voltage terminal"
|
||||
depends on $(dt_compat_enabled,half-bridge)
|
||||
depends on $(dt_nodelabel_enabled,halfbridge)
|
||||
help
|
||||
Select the device connected to the high-voltage
|
||||
side terminal of the DC/DC converter.
|
||||
@@ -74,7 +84,7 @@ endchoice
|
||||
|
||||
choice
|
||||
prompt "PWM terminal"
|
||||
depends on $(dt_node_bool_prop,/outputs/pwm_switch,kconfig-flag)
|
||||
depends on $(dt_nodelabel_enabled,pwm_switch)
|
||||
help
|
||||
Select the device connected to the PWM switch (currently
|
||||
only solar panel possible)
|
||||
@@ -295,7 +305,7 @@ endmenu # Battery default settings
|
||||
|
||||
|
||||
menu "Load output settings"
|
||||
depends on $(dt_node_bool_prop,/outputs/load,kconfig-flag)
|
||||
depends on $(dt_nodelabel_enabled,load_switch)
|
||||
|
||||
config LOAD_OC_RECOVERY_DELAY
|
||||
int "Overcurrent recovery delay (s)"
|
||||
@@ -315,15 +325,32 @@ endmenu # Charge controller setup
|
||||
|
||||
menu "UEXT port"
|
||||
|
||||
config UEXT_OLED_DISPLAY
|
||||
select I2C
|
||||
bool "OLED display"
|
||||
DT_CHOSEN_Z_DISPLAY := zephyr,display
|
||||
|
||||
config UEXT_OLED_BRIGHTNESS
|
||||
depends on UEXT_OLED_DISPLAY
|
||||
int "OLED display brightness"
|
||||
range 1 255
|
||||
default 1
|
||||
config UEXT_OLED_DISPLAY
|
||||
bool "OLED display"
|
||||
depends on $(dt_chosen_enabled,$(DT_CHOSEN_Z_DISPLAY))
|
||||
default y if SHIELD_UEXT_OLED
|
||||
select DISPLAY
|
||||
select CHARACTER_FRAMEBUFFER
|
||||
help
|
||||
Show live data on a SSD1306 128x64 OLED display connected to the UEXT
|
||||
port. The display is configured via the uext_oled shield:
|
||||
|
||||
west build -b <board> app -- -DSHIELD=uext_oled
|
||||
|
||||
The brightness can be changed via CONFIG_SSD1306_DEFAULT_CONTRAST.
|
||||
|
||||
config HEAP_MEM_POOL_ADD_SIZE_UEXT_OLED
|
||||
int
|
||||
default 1280 if UEXT_OLED_DISPLAY
|
||||
default 0
|
||||
help
|
||||
Frame buffer allocated by the character framebuffer subsystem (1024 bytes) plus some
|
||||
margin for the heap management overhead.
|
||||
|
||||
config CHARACTER_FRAMEBUFFER_USE_DEFAULT_FONTS
|
||||
default n if UEXT_OLED_DISPLAY
|
||||
|
||||
endmenu # UEXT port
|
||||
|
||||
@@ -336,13 +363,6 @@ config CAN_LOG_LEVEL
|
||||
help
|
||||
Log level for the CAN module (if included). 0=off, 1=ERR, 2=WRN, 3=INF, 4=DBG
|
||||
|
||||
config DAQ_LOG_LEVEL
|
||||
int "Log level for the DAQ module"
|
||||
default 2
|
||||
depends on LOG
|
||||
help
|
||||
Log level for the DAQ module. 0=off, 1=ERR, 2=WRN, 3=INF, 4=DBG
|
||||
|
||||
config DATA_OBJECTS_LOG_LEVEL
|
||||
int "Log level for the data objects module"
|
||||
default 2
|
||||
@@ -364,13 +384,6 @@ config DCDC_LOG_LEVEL
|
||||
help
|
||||
Log level for the DCDC module (if included). 0=off, 1=ERR, 2=WRN, 3=INF, 4=DBG
|
||||
|
||||
config HW_LOG_LEVEL
|
||||
int "Log level for the hardware module"
|
||||
default 2
|
||||
depends on LOG
|
||||
help
|
||||
Log level for the hardware module. 0=off, 1=ERR, 2=WRN, 3=INF, 4=DBG
|
||||
|
||||
config BAT_LOG_LEVEL
|
||||
int "Log level for the battery module"
|
||||
default 2
|
||||
@@ -378,6 +391,13 @@ config BAT_LOG_LEVEL
|
||||
help
|
||||
Log level for the battery module. 0=off, 1=ERR, 2=WRN, 3=INF, 4=DBG
|
||||
|
||||
config LOAD_LOG_LEVEL
|
||||
int "Log level for the load output module (if included)"
|
||||
default 2
|
||||
depends on LOG
|
||||
help
|
||||
Log level for the load output module (if included). 0=off, 1=ERR, 2=WRN, 3=INF, 4=DBG
|
||||
|
||||
config PWM_LOG_LEVEL
|
||||
int "Log level for the PWM module (if included)"
|
||||
default 2
|
||||
@@ -406,7 +426,7 @@ config CUSTOM_DATA_OBJECTS_FILE
|
||||
|
||||
config CUSTOM_DCDC_CONTROLLER
|
||||
bool "Custom implementation of DC/DC controller"
|
||||
depends on $(dt_compat_enabled,half-bridge)
|
||||
depends on $(dt_nodelabel_enabled,halfbridge)
|
||||
default n
|
||||
help
|
||||
Use a custom implementation instead of the default perturb & observe
|
||||
|
||||
@@ -52,7 +52,7 @@
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
compatible = "libresolar,stm32-daq";
|
||||
|
||||
v-low {
|
||||
io-channels = <&adc1 12>;
|
||||
@@ -105,7 +105,8 @@
|
||||
hrtim1: timers@40016800 {
|
||||
compatible = "st,stm32-timers";
|
||||
reg = <0x40016800 0x400>;
|
||||
clocks = <&rcc STM32_CLOCK(APB2, 26)>;
|
||||
clocks = <&rcc STM32_CLOCK(APB2, 26)>,
|
||||
<&rcc STM32_SRC_TIMPCLK2 NO_SEL>;
|
||||
resets = <&rctl STM32_RESET(APB2, 26)>;
|
||||
interrupts = <67 0>, <68 0>, <69 0>, <70 0>,
|
||||
<71 0>, <72 0>, <73 0>, <74 0>;
|
||||
@@ -121,11 +122,12 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-hrtim-half-bridge";
|
||||
pinctrl-0 = <&hrtim1_cha1_pa8 &hrtim1_cha2_pa9>;
|
||||
pinctrl-names = "default";
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
adc-trigger;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -135,11 +137,13 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-advtim-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
pinctrl-names = "default";
|
||||
channel = <1>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
adc-trigger;
|
||||
};
|
||||
};
|
||||
*/
|
||||
|
||||
+4
-2
@@ -18,6 +18,9 @@ CONFIG_HWINFO=y
|
||||
|
||||
CONFIG_CRC=y
|
||||
|
||||
# NTC temperature sensors
|
||||
CONFIG_SENSOR=y
|
||||
|
||||
CONFIG_CONSOLE_SUBSYS=y
|
||||
CONFIG_CONSOLE_GETCHAR=y
|
||||
|
||||
@@ -38,8 +41,7 @@ CONFIG_THINGSET_SDK_THREAD_PRIORITY=6
|
||||
# Uncomment to disable regular data publication on ThingSet serial at startup
|
||||
#CONFIG_THINGSET_REPORTING_LIVE_ENABLE_PRESET=n
|
||||
|
||||
# Uncomment to enable I2C OLED display in UEXT connector
|
||||
#CONFIG_UEXT_OLED_DISPLAY=y
|
||||
# I2C OLED display in UEXT connector is enabled by building with -DSHIELD=uext_oled
|
||||
|
||||
# Select initial battery configuration: GEL (default), FLOODED, AGM, LFP, NMC or NMC_HV
|
||||
#CONFIG_BAT_TYPE_???=y
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
sample:
|
||||
name: Libre Solar charge controller firmware
|
||||
common:
|
||||
build_only: true
|
||||
tags:
|
||||
- charge_controller
|
||||
tests:
|
||||
charge_controller.firmware:
|
||||
platform_allow: &boards
|
||||
- mppt_1210_hus@0.4.0
|
||||
- mppt_1210_hus@0.7.0
|
||||
- mppt_2420_hc@0.1.0
|
||||
- mppt_2420_hc@0.2.0
|
||||
- mppt_2420_hpx
|
||||
- mppt_2420_lc
|
||||
- mppt_2420_rc
|
||||
- nucleo_g474re
|
||||
- pwm_2420_lus@0.2.0
|
||||
- pwm_2420_lus@0.3.0
|
||||
- spinvert_boost_4820
|
||||
integration_platforms: *boards
|
||||
charge_controller.firmware.uext_oled:
|
||||
extra_args: SHIELD=uext_oled
|
||||
# mppt_2420_lc and nucleo_g474re excluded: not enough RAM / no UEXT port
|
||||
platform_allow: &uext_oled_boards
|
||||
- mppt_1210_hus@0.7.0
|
||||
- mppt_2420_hc@0.2.0
|
||||
- mppt_2420_hpx
|
||||
- mppt_2420_rc
|
||||
- pwm_2420_lus@0.3.0
|
||||
integration_platforms: *uext_oled_boards
|
||||
@@ -5,22 +5,19 @@ zephyr_include_directories(.)
|
||||
target_sources(app PRIVATE
|
||||
bat_charger.cpp
|
||||
daq.cpp
|
||||
daq_driver.c
|
||||
device_status.cpp
|
||||
dcdc.cpp
|
||||
half_bridge.c
|
||||
dcdc.c
|
||||
hardware.cpp
|
||||
oled.cpp
|
||||
leds.cpp
|
||||
load.cpp
|
||||
load_driver.c
|
||||
main.cpp
|
||||
power_port.cpp
|
||||
pwm_switch_driver.c
|
||||
power_port.c
|
||||
pwm_switch.cpp
|
||||
setup.cpp
|
||||
)
|
||||
|
||||
target_sources_ifdef(CONFIG_UEXT_OLED_DISPLAY app PRIVATE oled.cpp oled_font.c)
|
||||
|
||||
if(${CONFIG_CUSTOM_DATA_OBJECTS_FILE})
|
||||
target_sources(app PRIVATE data_objects_custom.cpp)
|
||||
else()
|
||||
|
||||
+27
-24
@@ -18,13 +18,14 @@ LOG_MODULE_REGISTER(bat_charger, CONFIG_BAT_LOG_LEVEL);
|
||||
#include "board.h"
|
||||
#include "device_status.h"
|
||||
#include "helper.h"
|
||||
#include "load.h"
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
extern struct device_status dev_stat;
|
||||
extern LoadOutput load;
|
||||
|
||||
// DISCHARGE_CURRENT_MAX used to estimate current-compensation of load disconnect voltage
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
#define DISCHARGE_CURRENT_MAX DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max)
|
||||
#define DISCHARGE_CURRENT_MAX DT_PROP(LOAD_SWITCH_NODE, current_max)
|
||||
#else
|
||||
#define DISCHARGE_CURRENT_MAX DT_PROP(DT_PATH(pcb), dcdc_current_max)
|
||||
#endif
|
||||
@@ -435,37 +436,39 @@ void Charger::discharge_control(BatConf *bat_conf)
|
||||
// This limit should normally never be reached, as the load output settings should be
|
||||
// higher. The flag can be used to trigger actions of last resort, e.g. deep-sleep
|
||||
// of the charge controller itself.
|
||||
if (port->bus->voltage < port->bus->src_control_voltage(bat_conf->absolute_min_voltage)) {
|
||||
if (port->bus->voltage
|
||||
< dc_bus_src_control_voltage(port->bus, bat_conf->absolute_min_voltage))
|
||||
{
|
||||
port->neg_current_limit = 0;
|
||||
dev_stat.set_error(ERR_BAT_UNDERVOLTAGE);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_UNDERVOLTAGE);
|
||||
}
|
||||
|
||||
if (bat_temperature > bat_conf->discharge_temp_max) {
|
||||
port->neg_current_limit = 0;
|
||||
dev_stat.set_error(ERR_BAT_DIS_OVERTEMP);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_DIS_OVERTEMP);
|
||||
}
|
||||
else if (bat_temperature < bat_conf->discharge_temp_min) {
|
||||
port->neg_current_limit = 0;
|
||||
dev_stat.set_error(ERR_BAT_DIS_UNDERTEMP);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_DIS_UNDERTEMP);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// discharging currently not allowed. should we allow it?
|
||||
|
||||
if (port->bus->voltage
|
||||
>= port->bus->src_control_voltage(bat_conf->absolute_min_voltage + 0.1F))
|
||||
>= dc_bus_src_control_voltage(port->bus, bat_conf->absolute_min_voltage + 0.1F))
|
||||
{
|
||||
dev_stat.clear_error(ERR_BAT_UNDERVOLTAGE);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_UNDERVOLTAGE);
|
||||
}
|
||||
|
||||
if (bat_temperature < bat_conf->discharge_temp_max - 1
|
||||
&& bat_temperature > bat_conf->discharge_temp_min + 1)
|
||||
{
|
||||
dev_stat.clear_error(ERR_BAT_DIS_OVERTEMP | ERR_BAT_DIS_UNDERTEMP);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_DIS_OVERTEMP | ERR_BAT_DIS_UNDERTEMP);
|
||||
}
|
||||
|
||||
if (!dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_DIS_OVERTEMP
|
||||
| ERR_BAT_DIS_UNDERTEMP))
|
||||
if (!device_status_has_error(&dev_stat, ERR_BAT_UNDERVOLTAGE | ERR_BAT_DIS_OVERTEMP
|
||||
| ERR_BAT_DIS_UNDERTEMP))
|
||||
{
|
||||
// discharge current is stored as absolute value in bat_conf, but defined
|
||||
// as negative current for power port
|
||||
@@ -480,20 +483,20 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
// check battery temperature for charging direction
|
||||
if (bat_temperature > bat_conf->charge_temp_max) {
|
||||
port->pos_current_limit = 0;
|
||||
dev_stat.set_error(ERR_BAT_CHG_OVERTEMP);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_CHG_OVERTEMP);
|
||||
enter_state(CHG_STATE_IDLE);
|
||||
}
|
||||
else if (bat_temperature < bat_conf->charge_temp_min) {
|
||||
port->pos_current_limit = 0;
|
||||
dev_stat.set_error(ERR_BAT_CHG_UNDERTEMP);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_CHG_UNDERTEMP);
|
||||
enter_state(CHG_STATE_IDLE);
|
||||
}
|
||||
|
||||
if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE)
|
||||
if (device_status_has_error(&dev_stat, ERR_BAT_OVERVOLTAGE)
|
||||
&& port->bus->voltage
|
||||
< (bat_conf->absolute_max_voltage - 0.5F) * port->bus->series_multiplier)
|
||||
{
|
||||
dev_stat.clear_error(ERR_BAT_OVERVOLTAGE);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_OVERVOLTAGE);
|
||||
}
|
||||
|
||||
if (state != CHG_STATE_FOLLOWER && (uptime() - time_last_ctrl_msg) <= 1) {
|
||||
@@ -506,9 +509,9 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
if ((time_state_changed == CHARGER_TIME_NEVER
|
||||
|| ((uptime() - time_state_changed) > bat_conf->time_limit_recharge
|
||||
&& port->bus->voltage
|
||||
< port->bus->sink_control_voltage(bat_conf->recharge_voltage)))
|
||||
< dc_bus_sink_control_voltage(port->bus, bat_conf->recharge_voltage)))
|
||||
&& port->bus->voltage
|
||||
> port->bus->sink_control_voltage(bat_conf->absolute_min_voltage)
|
||||
> dc_bus_sink_control_voltage(port->bus, bat_conf->absolute_min_voltage)
|
||||
&& bat_temperature < bat_conf->charge_temp_max - 1
|
||||
&& bat_temperature > bat_conf->charge_temp_min + 1)
|
||||
{
|
||||
@@ -518,9 +521,9 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
port->pos_current_limit = bat_conf->charge_current_max;
|
||||
target_current_control = port->pos_current_limit;
|
||||
full = false;
|
||||
dev_stat.clear_error(ERR_BAT_CHG_OVERTEMP);
|
||||
dev_stat.clear_error(ERR_BAT_CHG_UNDERTEMP);
|
||||
dev_stat.clear_error(ERR_BAT_OVERVOLTAGE);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_CHG_OVERTEMP);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_CHG_UNDERTEMP);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_OVERVOLTAGE);
|
||||
enter_state(CHG_STATE_BULK);
|
||||
}
|
||||
break;
|
||||
@@ -531,7 +534,7 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
bat_conf->topping_voltage
|
||||
+ bat_conf->temperature_compensation * (bat_temperature - 25);
|
||||
|
||||
if (port->bus->voltage > port->bus->sink_control_voltage()) {
|
||||
if (port->bus->voltage > dc_bus_sink_control_voltage(port->bus, 0)) {
|
||||
target_voltage_timer = 0;
|
||||
enter_state(CHG_STATE_TOPPING);
|
||||
}
|
||||
@@ -546,7 +549,7 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
// power sharing: multiple devices in parallel supply the same current
|
||||
target_current_control = port->current_filtered;
|
||||
|
||||
if (port->bus->voltage_filtered >= port->bus->sink_control_voltage() - 0.05F) {
|
||||
if (port->bus->voltage_filtered >= dc_bus_sink_control_voltage(port->bus, 0) - 0.05F) {
|
||||
// battery is full if topping target voltage is still reached (i.e. sufficient
|
||||
// solar power available) and time limit or cut-off current reached
|
||||
if (port->current_filtered < bat_conf->topping_cutoff_current
|
||||
@@ -597,13 +600,13 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
|
||||
target_current_control = port->current_filtered;
|
||||
|
||||
if (port->bus->voltage >= port->bus->sink_control_voltage()) {
|
||||
if (port->bus->voltage >= dc_bus_sink_control_voltage(port->bus, 0)) {
|
||||
time_target_voltage_reached = uptime();
|
||||
}
|
||||
|
||||
if ((uptime() - time_target_voltage_reached > bat_conf->float_recharge_time)
|
||||
&& port->bus->voltage_filtered
|
||||
< port->bus->sink_control_voltage(bat_conf->recharge_voltage))
|
||||
< dc_bus_sink_control_voltage(port->bus, bat_conf->recharge_voltage))
|
||||
{
|
||||
// the battery was discharged: float voltage could not be reached anymore
|
||||
port->pos_current_limit = bat_conf->charge_current_max;
|
||||
|
||||
@@ -341,9 +341,9 @@ enum ChargerState
|
||||
class Charger
|
||||
{
|
||||
public:
|
||||
Charger(PowerPort *pwr_port) : port(pwr_port){};
|
||||
Charger(struct power_port *pwr_port) : port(pwr_port) {};
|
||||
|
||||
PowerPort *port;
|
||||
struct power_port *port;
|
||||
|
||||
/**
|
||||
* Current charger state (see enum ChargerState)
|
||||
|
||||
+12
-6
@@ -15,11 +15,17 @@
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#define BOARD_HAS_DCDC DT_HAS_COMPAT_STATUS_OKAY(half_bridge)
|
||||
#define BOARD_HAS_PWM_PORT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch))
|
||||
#define BOARD_HAS_LOAD_OUTPUT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load))
|
||||
#define BOARD_HAS_USB_OUTPUT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr))
|
||||
#define BOARD_HAS_TEMP_FETS DT_NODE_EXISTS(DT_CHILD(DT_PATH(adc_inputs), temp_fets))
|
||||
#define BOARD_HAS_TEMP_BAT DT_NODE_EXISTS(DT_CHILD(DT_PATH(adc_inputs), temp_bat))
|
||||
#define HALF_BRIDGE_NODE DT_NODELABEL(halfbridge)
|
||||
#define LOAD_SWITCH_NODE DT_NODELABEL(load_switch)
|
||||
#define USB_SWITCH_NODE DT_NODELABEL(usb_switch)
|
||||
#define PWM_SWITCH_NODE DT_NODELABEL(pwm_switch)
|
||||
#define NTC_FETS_NODE DT_NODELABEL(ntc_fets)
|
||||
#define NTC_BAT_NODE DT_NODELABEL(ntc_bat)
|
||||
#define BOARD_HAS_DCDC DT_NODE_HAS_STATUS_OKAY(HALF_BRIDGE_NODE)
|
||||
#define BOARD_HAS_PWM_PORT DT_NODE_HAS_STATUS_OKAY(PWM_SWITCH_NODE)
|
||||
#define BOARD_HAS_LOAD_OUTPUT DT_NODE_HAS_STATUS_OKAY(LOAD_SWITCH_NODE)
|
||||
#define BOARD_HAS_USB_OUTPUT DT_NODE_HAS_STATUS_OKAY(USB_SWITCH_NODE)
|
||||
#define BOARD_HAS_TEMP_FETS DT_NODE_HAS_STATUS_OKAY(NTC_FETS_NODE)
|
||||
#define BOARD_HAS_TEMP_BAT DT_NODE_HAS_STATUS_OKAY(NTC_BAT_NODE)
|
||||
|
||||
#endif /* BOARD_H_ */
|
||||
|
||||
+103
-229
@@ -6,19 +6,16 @@
|
||||
|
||||
#include "daq.h"
|
||||
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(daq, CONFIG_DAQ_LOG_LEVEL);
|
||||
LOG_MODULE_REGISTER(daq_app, CONFIG_DAQ_LOG_LEVEL);
|
||||
|
||||
#include <assert.h>
|
||||
#include <math.h> // log for thermistor calculation
|
||||
|
||||
#include "mcu.h"
|
||||
#include "setup.h"
|
||||
|
||||
// typical value for Semitec 103AT-5 thermistor: 3435
|
||||
#define NTC_BETA_VALUE 3435
|
||||
// temperatures below this value are considered as invalid (e.g. sensor not connected)
|
||||
#define NTC_TEMP_MIN (-50.0F)
|
||||
|
||||
// filter parameter c for additional battery voltage and current low-pass filter
|
||||
// c = dt / (tau + dt) = 0.1s / (10s + 0.1s)
|
||||
@@ -27,148 +24,88 @@ LOG_MODULE_REGISTER(daq, CONFIG_DAQ_LOG_LEVEL);
|
||||
#define LV_TERMINAL_CURRENT_FILTER_CONST 0.0099F
|
||||
#define PWM_CURRENT_FILTER_CONST 0.0625F // 1.5 seconds
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
static uint16_t dcdc_current_offset_raw;
|
||||
#endif
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
static uint16_t pwm_current_offset_raw;
|
||||
#endif
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
static uint16_t load_current_offset_raw;
|
||||
#endif
|
||||
|
||||
// 16-bit ADC raw readings (actually left-aligned 12-bit, i.e. left-shifted by 4 bits)
|
||||
volatile uint16_t adc_readings[NUM_ADC_CH] = {};
|
||||
|
||||
// filtered raw readings left-shifted by additional adc_filter_const[channel] bits
|
||||
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] = {};
|
||||
|
||||
/*
|
||||
* Channel-specific ADC filter constant from devicetree
|
||||
*
|
||||
* multiplier = 1/(2^adc_filter_const[channel])
|
||||
*/
|
||||
#define ADC_FILTER_CONST(node_id) DT_PROP(node_id, filter_const),
|
||||
static const uint8_t adc_filter_const[NUM_ADC_CH] = { DT_FOREACH_CHILD(DT_PATH(adc_inputs),
|
||||
ADC_FILTER_CONST) };
|
||||
static const struct device *const daq = DAQ_DEV;
|
||||
|
||||
#if BOARD_HAS_TEMP_BAT || BOARD_HAS_TEMP_FETS
|
||||
/**
|
||||
* Average value for ADC channel
|
||||
* Read temperature from an NTC sensor
|
||||
*
|
||||
* @param channel valid ADC channel position using ADC_POS() macro
|
||||
* @returns 0 on success or negative errno if the temperature is not available or implausible
|
||||
*/
|
||||
static inline uint32_t adc_raw_filtered(uint32_t channel)
|
||||
static int ntc_temp(const struct device *sensor, float *temp)
|
||||
{
|
||||
return adc_filtered[channel] >> adc_filter_const[channel];
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured current/voltage for ADC channel after average and scaling
|
||||
*
|
||||
* @param channel valid ADC channel position using ADC_POS() macro
|
||||
* @param vref reference voltage in millivolts
|
||||
* @param offset offset added to raw ADC value before applying gain
|
||||
*
|
||||
* @return scaled final value in volts/amps
|
||||
*/
|
||||
static inline float adc_scaled(uint32_t channel, int32_t vref, const float gain, int32_t offset = 0)
|
||||
{
|
||||
return adc_raw_to_voltage(adc_raw_filtered(channel) - offset, vref) * gain;
|
||||
}
|
||||
|
||||
static inline float ntc_temp(uint32_t channel, int32_t vref, float ntc_series_resistor)
|
||||
{
|
||||
/** \todo Improved (faster) temperature calculation:
|
||||
https://www.embeddedrelated.com/showarticle/91.php
|
||||
*/
|
||||
|
||||
// voltage read by ADC (mV)
|
||||
float v_temp = adc_raw_to_voltage(adc_raw_filtered(channel), vref) * 1000;
|
||||
|
||||
// resistance of NTC (Ohm)
|
||||
float rts = ntc_series_resistor * v_temp / (vref - v_temp);
|
||||
|
||||
return 1.0f / (1.0f / (273.15f + 25.0f) + 1.0f / (float)NTC_BETA_VALUE * logf(rts / 10000.0f))
|
||||
- 273.15f; // °C
|
||||
struct sensor_value val;
|
||||
int err;
|
||||
|
||||
err = sensor_sample_fetch(sensor);
|
||||
if (err != 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &val);
|
||||
if (err != 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
*temp = sensor_value_to_float(&val);
|
||||
|
||||
return *temp > NTC_TEMP_MIN ? 0 : -ENODATA;
|
||||
}
|
||||
#endif
|
||||
|
||||
void calibrate_current_sensors()
|
||||
{
|
||||
#if BOARD_HAS_DCDC
|
||||
dcdc_current_offset_raw = adc_raw_filtered(ADC_POS(i_dcdc));
|
||||
daq_calibrate_offset(daq, DAQ_CH(i_dcdc));
|
||||
#endif
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
pwm_current_offset_raw = adc_raw_filtered(ADC_POS(i_pwm));
|
||||
daq_calibrate_offset(daq, DAQ_CH(i_pwm));
|
||||
#endif
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load_current_offset_raw = adc_raw_filtered(ADC_POS(i_load));
|
||||
daq_calibrate_offset(daq, DAQ_CH(i_load));
|
||||
#endif
|
||||
}
|
||||
|
||||
void adc_update_value(unsigned int pos)
|
||||
{
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
static bool pwm_sample_filter(const struct device *dev, int channel)
|
||||
{
|
||||
// only read input voltage and current when switch is on or permanently off
|
||||
if ((pos != ADC_POS(v_pwm) && pos != ADC_POS(i_pwm)) || pwm_switch.signal_high()
|
||||
|| pwm_switch.active() == false)
|
||||
return pwm_switch.signal_high() || pwm_switch.active() == false;
|
||||
}
|
||||
#endif
|
||||
{
|
||||
/*
|
||||
* Low-pass filtering of ADC raw readings
|
||||
* (see also: http://techteach.no/simview/lowpass_filter/doc/filter_algorithm.pdf)
|
||||
*
|
||||
* y(n) = c * x(n) + (1 - c) * y(n-1) with filter constant c = 1/(2^adc_filter_const[pos])
|
||||
*
|
||||
* Remarks regarding below implementation optimized for efficiency:
|
||||
*
|
||||
* 1. adc_readings has different fixed-point math format, so that it would have to be
|
||||
* aligned with << adc_filter_const[pos] before calculation. Not aligning it is
|
||||
* equivalent to multiplication with c:
|
||||
* adc_readings[pos] == c * (adc_readings[pos] << adc_filter_const[pos])
|
||||
*
|
||||
* 2. c * adc_filtered[pos] == adc_filtered[pos] >> adc_filter_const[pos]
|
||||
*/
|
||||
adc_filtered[pos] = (uint32_t)adc_readings[pos] + adc_filtered[pos]
|
||||
- (adc_filtered[pos] >> adc_filter_const[pos]);
|
||||
|
||||
#ifdef CONFIG_CUSTOM_DCDC_CONTROLLER
|
||||
static void dcdc_sample_callback(const struct device *dev)
|
||||
{
|
||||
dcdc_low_level_controller();
|
||||
}
|
||||
#endif
|
||||
|
||||
void daq_setup()
|
||||
{
|
||||
if (!device_is_ready(daq)) {
|
||||
LOG_ERR("DAQ device not ready");
|
||||
return;
|
||||
}
|
||||
|
||||
// check upper alerts
|
||||
adc_alerts_upper[pos].debounce_ms++;
|
||||
if (adc_alerts_upper[pos].callback != NULL && adc_readings[pos] >= adc_alerts_upper[pos].limit)
|
||||
{
|
||||
if (adc_alerts_upper[pos].debounce_ms > 1) {
|
||||
// create function pointer and call function
|
||||
adc_alerts_upper[pos].callback();
|
||||
}
|
||||
}
|
||||
else if (adc_alerts_upper[pos].debounce_ms > 0) {
|
||||
// reset debounce ms counter only if already close to triggering to allow setting negative
|
||||
// values to specify a one-time inhibit delay
|
||||
adc_alerts_upper[pos].debounce_ms = 0;
|
||||
}
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
daq_set_sample_filter(daq, pwm_sample_filter, BIT(DAQ_CH(v_pwm)) | BIT(DAQ_CH(i_pwm)));
|
||||
#endif
|
||||
|
||||
// same for lower alerts
|
||||
adc_alerts_lower[pos].debounce_ms++;
|
||||
if (adc_alerts_lower[pos].callback != NULL && adc_readings[pos] <= adc_alerts_lower[pos].limit)
|
||||
{
|
||||
if (adc_alerts_lower[pos].debounce_ms > 1) {
|
||||
adc_alerts_lower[pos].callback();
|
||||
}
|
||||
}
|
||||
else if (adc_alerts_lower[pos].debounce_ms > 0) {
|
||||
adc_alerts_lower[pos].debounce_ms = 0;
|
||||
}
|
||||
#ifdef CONFIG_CUSTOM_DCDC_CONTROLLER
|
||||
daq_set_sample_callback(daq, dcdc_sample_callback);
|
||||
#endif
|
||||
|
||||
k_sleep(K_MSEC(500)); // wait for ADC to collect some measurement values
|
||||
daq_update();
|
||||
calibrate_current_sensors();
|
||||
daq_update();
|
||||
}
|
||||
|
||||
void daq_update()
|
||||
{
|
||||
int vref = VREF;
|
||||
|
||||
// calculate lower voltage first, as it is needed for PWM terminal voltage calculation
|
||||
lv_bus.voltage = adc_scaled(ADC_POS(v_low), vref, ADC_GAIN(v_low));
|
||||
lv_bus.voltage = daq_get_value(daq, DAQ_CH(v_low));
|
||||
|
||||
if (lv_bus.voltage_filtered != 0.0F) {
|
||||
lv_bus.voltage_filtered = LV_BUS_VOLTAGE_FILTER_CONST * lv_bus.voltage
|
||||
@@ -180,7 +117,7 @@ void daq_update()
|
||||
}
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
hv_bus.voltage = adc_scaled(ADC_POS(v_high), vref, ADC_GAIN(v_high));
|
||||
hv_bus.voltage = daq_get_value(daq, DAQ_CH(v_high));
|
||||
|
||||
if (hv_bus.voltage_filtered != 0.0F) {
|
||||
hv_bus.voltage_filtered = HV_BUS_VOLTAGE_FILTER_CONST * hv_bus.voltage
|
||||
@@ -193,12 +130,11 @@ void daq_update()
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
pwm_switch.ext_voltage =
|
||||
lv_bus.voltage - adc_scaled(ADC_POS(v_pwm), vref, ADC_GAIN(v_pwm), ADC_OFFSET(v_pwm));
|
||||
pwm_switch.ext_voltage = lv_bus.voltage - daq_get_value(daq, DAQ_CH(v_pwm));
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load.current = adc_scaled(ADC_POS(i_load), vref, ADC_GAIN(i_load), load_current_offset_raw);
|
||||
load.current = daq_get_value(daq, DAQ_CH(i_load));
|
||||
float load_current = load.current;
|
||||
#else
|
||||
float load_current = 0; // value used below, so we still need to define the variable
|
||||
@@ -209,9 +145,7 @@ void daq_update()
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
// current multiplied with PWM duty cycle for PWM charger to get avg current for correct power
|
||||
// calculation
|
||||
pwm_switch.current =
|
||||
-pwm_switch.get_duty_cycle()
|
||||
* adc_scaled(ADC_POS(i_pwm), vref, ADC_GAIN(i_pwm), pwm_current_offset_raw);
|
||||
pwm_switch.current = -pwm_switch.get_duty_cycle() * daq_get_value(daq, DAQ_CH(i_pwm));
|
||||
pwm_switch.current_filtered = PWM_CURRENT_FILTER_CONST * pwm_switch.current
|
||||
+ (1.0F - PWM_CURRENT_FILTER_CONST) * pwm_switch.current_filtered;
|
||||
|
||||
@@ -221,8 +155,7 @@ void daq_update()
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
dcdc.inductor_current =
|
||||
adc_scaled(ADC_POS(i_dcdc), vref, ADC_GAIN(i_dcdc), dcdc_current_offset_raw);
|
||||
dcdc.inductor_current = daq_get_value(daq, DAQ_CH(i_dcdc));
|
||||
|
||||
lv_terminal_current += dcdc.inductor_current;
|
||||
|
||||
@@ -244,10 +177,9 @@ void daq_update()
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_TEMP_BAT
|
||||
// battery temperature calculation
|
||||
float bat_temp = ntc_temp(ADC_POS(temp_bat), vref, ADC_GAIN(temp_bat));
|
||||
float bat_temp;
|
||||
|
||||
if (bat_temp > -50) {
|
||||
if (ntc_temp(DEVICE_DT_GET(NTC_BAT_NODE), &bat_temp) == 0) {
|
||||
// external sensor connected: take measured value
|
||||
charger.bat_temperature = bat_temp;
|
||||
charger.ext_temp_sensor = true;
|
||||
@@ -260,30 +192,34 @@ void daq_update()
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_TEMP_FETS
|
||||
dcdc.temp_mosfets = ntc_temp(ADC_POS(temp_fets), vref, ADC_GAIN(temp_fets));
|
||||
float fets_temp;
|
||||
|
||||
if (ntc_temp(DEVICE_DT_GET(NTC_FETS_NODE), &fets_temp) == 0) {
|
||||
dcdc.temp_mosfets = fets_temp;
|
||||
}
|
||||
#endif
|
||||
|
||||
// internal MCU temperature (calibrated using 12-bit right-aligned readings)
|
||||
uint16_t adcval = (adc_raw_filtered(ADC_POS(temp_mcu)) >> 4) * vref / VREFINT_VALUE;
|
||||
dev_stat.internal_temp = (TSENSE_CAL2_VALUE - TSENSE_CAL1_VALUE) / (TSENSE_CAL2 - TSENSE_CAL1)
|
||||
* (adcval - TSENSE_CAL1)
|
||||
+ TSENSE_CAL1_VALUE;
|
||||
float internal_temp;
|
||||
if (daq_get_internal_temp(daq, &internal_temp) == 0) {
|
||||
dev_stat.internal_temp = internal_temp;
|
||||
}
|
||||
|
||||
if (dev_stat.internal_temp > 80) {
|
||||
dev_stat.set_error(ERR_INT_OVERTEMP);
|
||||
device_status_set_error(&dev_stat, ERR_INT_OVERTEMP);
|
||||
}
|
||||
else if (dev_stat.internal_temp < 70 && (dev_stat.has_error(ERR_INT_OVERTEMP))) {
|
||||
else if (dev_stat.internal_temp < 70 && (device_status_has_error(&dev_stat, ERR_INT_OVERTEMP)))
|
||||
{
|
||||
// remove error flag with hysteresis of 10°C
|
||||
dev_stat.clear_error(ERR_INT_OVERTEMP);
|
||||
device_status_clear_error(&dev_stat, ERR_INT_OVERTEMP);
|
||||
}
|
||||
// else: keep previous setting
|
||||
}
|
||||
|
||||
void lv_overvoltage_alert()
|
||||
static void lv_overvoltage_alert(const struct device *dev, int channel)
|
||||
{
|
||||
// disable any sort of input
|
||||
#if BOARD_HAS_DCDC
|
||||
dcdc.stop();
|
||||
dcdc_stop(&dcdc);
|
||||
#endif
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
pwm_switch.stop();
|
||||
@@ -291,112 +227,50 @@ void lv_overvoltage_alert()
|
||||
// do not use enter_state function, as we don't want to wait entire recharge delay
|
||||
charger.state = CHG_STATE_IDLE;
|
||||
|
||||
dev_stat.set_error(ERR_BAT_OVERVOLTAGE);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_OVERVOLTAGE);
|
||||
|
||||
LOG_ERR("Low-side overvoltage alert, ADC reading: %d limit: %d\n", adc_readings[ADC_POS(v_low)],
|
||||
adc_alerts_upper[ADC_POS(v_low)].limit);
|
||||
LOG_ERR("Low-side overvoltage alert");
|
||||
}
|
||||
|
||||
void lv_undervoltage_alert()
|
||||
static void lv_undervoltage_alert(const struct device *dev, int channel)
|
||||
{
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
// the battery undervoltage must have been caused by a load current peak
|
||||
load.stop(ERR_LOAD_VOLTAGE_DIP);
|
||||
#endif
|
||||
|
||||
LOG_ERR("Low-side undervoltage alert, ADC reading: %d limit: %d\n",
|
||||
adc_readings[ADC_POS(v_low)], adc_alerts_lower[ADC_POS(v_low)].limit);
|
||||
}
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
void hv_overvoltage_alert()
|
||||
{
|
||||
dcdc.stop();
|
||||
|
||||
// do not use enter_state function, as we don't want to wait entire recharge delay
|
||||
charger.state = CHG_STATE_IDLE;
|
||||
|
||||
dev_stat.set_error(ERR_DCDC_HS_OVERVOLTAGE);
|
||||
|
||||
LOG_ERR("High-side overvoltage alert, ADC reading: %d limit: %d\n",
|
||||
adc_readings[ADC_POS(v_high)], adc_alerts_upper[ADC_POS(v_high)].limit);
|
||||
}
|
||||
#endif
|
||||
|
||||
void adc_upper_alert_inhibit(int adc_pos, int timeout_ms)
|
||||
{
|
||||
// set negative value so that we get a final debouncing of this timeout + the original
|
||||
// delay in the alert function (currently only waiting for 2 samples = 2 ms)
|
||||
adc_alerts_upper[adc_pos].debounce_ms = -timeout_ms;
|
||||
}
|
||||
|
||||
uint16_t adc_raw_clamp(float scale, float limit)
|
||||
{
|
||||
// even if we have a higher voltage limit, we must limit it
|
||||
// to the max value the ADC will be able to deliver
|
||||
const float limit_scaled = limit * scale;
|
||||
return limit_scaled > (float)UINT16_MAX ? UINT16_MAX : (uint16_t)(limit_scaled);
|
||||
LOG_ERR("Low-side undervoltage alert");
|
||||
}
|
||||
|
||||
void daq_set_lv_limits(float lv_overvoltage, float lv_undervoltage)
|
||||
{
|
||||
float scale = (((4096 << 4) * 1000) / ADC_GAIN(v_low)) / (float)VREF;
|
||||
|
||||
// LV side (battery) overvoltage alert
|
||||
adc_alerts_upper[ADC_POS(v_low)].limit = adc_raw_clamp(scale, lv_overvoltage);
|
||||
adc_alerts_upper[ADC_POS(v_low)].callback = lv_overvoltage_alert;
|
||||
daq_set_alert(daq, DAQ_CH(v_low), DAQ_ALERT_UPPER, lv_overvoltage, lv_overvoltage_alert);
|
||||
|
||||
// LV side (battery) undervoltage alert
|
||||
adc_alerts_lower[ADC_POS(v_low)].limit = adc_raw_clamp(scale, lv_undervoltage);
|
||||
adc_alerts_lower[ADC_POS(v_low)].callback = lv_undervoltage_alert;
|
||||
daq_set_alert(daq, DAQ_CH(v_low), DAQ_ALERT_LOWER, lv_undervoltage, lv_undervoltage_alert);
|
||||
}
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
static void hv_overvoltage_alert(const struct device *dev, int channel)
|
||||
{
|
||||
dcdc_stop(&dcdc);
|
||||
|
||||
// do not use enter_state function, as we don't want to wait entire recharge delay
|
||||
charger.state = CHG_STATE_IDLE;
|
||||
|
||||
device_status_set_error(&dev_stat, ERR_DCDC_HS_OVERVOLTAGE);
|
||||
|
||||
LOG_ERR("High-side overvoltage alert");
|
||||
}
|
||||
|
||||
void daq_set_hv_limit(float hv_overvoltage)
|
||||
{
|
||||
float scale = (((4096 << 4) * 1000) / ADC_GAIN(v_high)) / (float)VREF;
|
||||
|
||||
// HV side (solar/grid) overvoltage alert
|
||||
adc_alerts_upper[ADC_POS(v_high)].limit = adc_raw_clamp(scale, hv_overvoltage);
|
||||
adc_alerts_upper[ADC_POS(v_high)].callback = hv_overvoltage_alert;
|
||||
int err =
|
||||
daq_set_alert(daq, DAQ_CH(v_high), DAQ_ALERT_UPPER, hv_overvoltage, hv_overvoltage_alert);
|
||||
if (err != 0) {
|
||||
LOG_ERR("Failed to set high-side overvoltage alert: %d", err);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(CONFIG_SOC_FAMILY_STM32)
|
||||
|
||||
#include "daq_stub.h"
|
||||
|
||||
void prepare_adc_readings(AdcValues values)
|
||||
{
|
||||
adc_readings[ADC_POS(vref_mcu)] = (uint16_t)(1.224 / 3.3 * 4096) << 4;
|
||||
adc_readings[ADC_POS(v_high)] =
|
||||
(uint16_t)((values.solar_voltage / ADC_GAIN(v_high)) / 3.3f * 4096) << 4;
|
||||
adc_readings[ADC_POS(v_low)] =
|
||||
(uint16_t)((values.battery_voltage / ADC_GAIN(v_low)) / 3.3f * 4096) << 4;
|
||||
adc_readings[ADC_POS(i_dcdc)] =
|
||||
(uint16_t)((values.dcdc_current / ADC_GAIN(i_dcdc)) / 3.3f * 4096) << 4;
|
||||
adc_readings[ADC_POS(i_load)] =
|
||||
(uint16_t)((values.load_current / ADC_GAIN(i_load)) / 3.3f * 4096) << 4;
|
||||
}
|
||||
|
||||
void prepare_adc_filtered()
|
||||
{
|
||||
// initialize also filtered values
|
||||
for (int i = 0; i < NUM_ADC_CH; i++) {
|
||||
adc_filtered[i] = adc_readings[i] << adc_filter_const[i];
|
||||
}
|
||||
}
|
||||
|
||||
void clear_adc_filtered()
|
||||
{
|
||||
// initialize also filtered values
|
||||
for (int i = 0; i < NUM_ADC_CH; i++) {
|
||||
adc_filtered[i] = 0;
|
||||
}
|
||||
}
|
||||
uint32_t get_adc_filtered(uint32_t channel)
|
||||
{
|
||||
return adc_raw_filtered(channel);
|
||||
}
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
|
||||
+17
-96
@@ -8,101 +8,35 @@
|
||||
* @file
|
||||
* @brief Data Acquisition (DAQ)
|
||||
*
|
||||
* Measurements are taken using ADC with DMA. The DAC is used to generate reference voltages
|
||||
* for bi-directional current measurement. Data is stored in structs, which are afterwards
|
||||
* used by the control algorithms
|
||||
*
|
||||
* All calculations assume a 16-bit ADC, even though the STM32 ADCs natively support 12-bit.
|
||||
* Using 16-bit for calculation allows to increase resolution by oversampling. Left-aligned
|
||||
* 12-bit ADC readings can be treated as 16-bit ADC readings.
|
||||
* Measurements are taken by the DAQ driver. This module converts them to the measurement values
|
||||
* stored in the structs of the power ports and DC buses, which are afterwards used by the control
|
||||
* algorithms.
|
||||
*/
|
||||
|
||||
#ifndef DAQ_H_
|
||||
#define DAQ_H_
|
||||
|
||||
#include <libresolar/drivers/daq.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
/** DAQ devicetree node */
|
||||
#define DAQ_NODE DT_PATH(adc_inputs)
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define ADC_SCALE_FLOAT 65536.0F // 16-bit full scale
|
||||
|
||||
#ifdef CONFIG_SOC_SERIES_STM32G4X
|
||||
// Using internal reference buffer at VREF+ pin, set to 2048 mV
|
||||
#define VREF (2048)
|
||||
#elif defined(UNIT_TEST)
|
||||
#define VREF (3300)
|
||||
#else
|
||||
// internal STM reference voltage (calibrated using 12-bit right-aligned readings)
|
||||
#define VREF (VREFINT_VALUE * VREFINT_CAL / (adc_raw_filtered(ADC_POS(vref_mcu)) >> 4))
|
||||
#endif
|
||||
|
||||
#define ADC_GAIN(name) \
|
||||
((float)DT_PROP(DT_CHILD(DT_PATH(adc_inputs), name), multiplier) \
|
||||
/ DT_PROP(DT_CHILD(DT_PATH(adc_inputs), name), divider))
|
||||
|
||||
#define ADC_OFFSET(name) (DT_PROP(DT_CHILD(DT_PATH(adc_inputs), name), offset))
|
||||
|
||||
/*
|
||||
* Find out the position in the ADC reading array for a channel identified by its Devicetree node
|
||||
*/
|
||||
#define ADC_POS(node) DT_N_S_adc_inputs_S_##node##_ADC_POS
|
||||
|
||||
/*
|
||||
* Creates a unique name for below enum
|
||||
*/
|
||||
#define ADC_ENUM(node) node##_ADC_POS,
|
||||
|
||||
/*
|
||||
* Enum for numbering of ADC channels as they are written by the DMA controller
|
||||
*
|
||||
* The channels must be specified in ascending order in the board.dts file.
|
||||
*/
|
||||
// cppcheck-suppress syntaxError
|
||||
enum
|
||||
{
|
||||
DT_FOREACH_CHILD(DT_PATH(adc_inputs), ADC_ENUM)
|
||||
NUM_ADC_CH // trick to get the number of elements
|
||||
};
|
||||
/** DAQ device */
|
||||
#define DAQ_DEV DEVICE_DT_GET(DAQ_NODE)
|
||||
|
||||
/**
|
||||
* Struct to define upper and lower limit alerts for any ADC channel
|
||||
* Get the channel index of a DAQ input
|
||||
*
|
||||
* @param name Lower-case-and-underscores name of the child node of the DAQ node, e.g. v_low
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
void (*callback)(); ///< Function to be called when limits are exceeded
|
||||
uint16_t limit; ///< ADC reading for lower limit
|
||||
int16_t debounce_ms; ///< Milliseconds delay for triggering alert
|
||||
} AdcAlert;
|
||||
|
||||
/**
|
||||
* Convert 16-bit raw ADC reading to voltage
|
||||
*
|
||||
* @param raw 16-bit ADC reading
|
||||
* @param vref_mV Reference voltage in millivolts
|
||||
*
|
||||
* @return Voltage in volts
|
||||
*/
|
||||
static inline float adc_raw_to_voltage(int32_t raw, int32_t vref_mV)
|
||||
{
|
||||
return (raw * vref_mV) / (ADC_SCALE_FLOAT * 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert voltage to 16-bit raw ADC reading
|
||||
*
|
||||
* @param voltage Voltage in volts
|
||||
* @param vref_mV Reference voltage in millivolts
|
||||
*
|
||||
* @return 16-bit ADC reading
|
||||
*/
|
||||
static inline int32_t adc_voltage_to_raw(float voltage, int32_t vref_mV)
|
||||
{
|
||||
return voltage / vref_mV * ADC_SCALE_FLOAT * 1000;
|
||||
}
|
||||
#define DAQ_CH(name) DAQ_CHANNEL(DAQ_NODE, name)
|
||||
|
||||
/**
|
||||
* Set offset vs. actual measured value, i.e. sets zero current point.
|
||||
@@ -117,15 +51,10 @@ void calibrate_current_sensors(void);
|
||||
void daq_update(void);
|
||||
|
||||
/**
|
||||
* Initializes ADC, DAC and DMA
|
||||
* Waits for initial measurements and calibrates the current sensors
|
||||
*/
|
||||
void daq_setup(void);
|
||||
|
||||
/**
|
||||
* Read, filter and check raw ADC readings stored by DMA controller
|
||||
*/
|
||||
void adc_update_value(unsigned int pos);
|
||||
|
||||
/**
|
||||
* Set lv side (battery) voltage limits where an alert should be triggered
|
||||
*
|
||||
@@ -141,14 +70,6 @@ void daq_set_lv_limits(float lv_overvoltage, float lv_undervoltage);
|
||||
*/
|
||||
void daq_set_hv_limit(float hv_overvoltage);
|
||||
|
||||
/**
|
||||
* Add an inhibit delay to the alerts to disable it temporarily
|
||||
*
|
||||
* @param adc_pos The position of the ADC measurement channel
|
||||
* @param timeout_ms Timeout in milliseconds
|
||||
*/
|
||||
void adc_upper_alert_inhibit(int adc_pos, int timeout_ms);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,368 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "daq.h"
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include <stm32_ll_adc.h>
|
||||
#include <stm32_ll_bus.h>
|
||||
#include <stm32_ll_dac.h>
|
||||
#include <stm32_ll_dma.h>
|
||||
#include <stm32_ll_system.h>
|
||||
|
||||
#include "dcdc.h" // for low-level control function called by DMA
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
|
||||
// automatic channel selection based on devicetree settings
|
||||
#define ADC_CHSEL_FN(node_id) (1UL << DT_PHA(node_id, io_channels, input)) |
|
||||
#define ADC_CHSEL (DT_FOREACH_CHILD(DT_PATH(adc_inputs), ADC_CHSEL_FN) 0)
|
||||
|
||||
int num_adc1_ch = NUM_ADC_CH;
|
||||
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
|
||||
// create array with selected channel for each ADC input
|
||||
#define ADC_CH_NUM_(node_id) DT_PHA(node_id, io_channels, input),
|
||||
static const uint32_t adc_ch_numbers[] = { DT_FOREACH_CHILD(DT_PATH(adc_inputs), ADC_CH_NUM_) };
|
||||
|
||||
// create array with ADC instance register address for each ADC input
|
||||
#define ADC_REG_(node_id) DT_REG_ADDR_BY_IDX(DT_PHANDLE(node_id, io_channels), 0),
|
||||
static const uint32_t adc_registers[] = { DT_FOREACH_CHILD(DT_PATH(adc_inputs), ADC_REG_) };
|
||||
|
||||
// number of channels per ADC peripheral will be calculated in adc_init function
|
||||
int num_adc1_ch = 0;
|
||||
int num_adc2_ch = 0;
|
||||
|
||||
// Depending on the channel, not just a single bit has to be set in the right location. So we need
|
||||
// a map to get correct bit settings for each channel.
|
||||
static const uint32_t table_channel[] = {
|
||||
LL_ADC_CHANNEL_0,
|
||||
LL_ADC_CHANNEL_1,
|
||||
LL_ADC_CHANNEL_2,
|
||||
LL_ADC_CHANNEL_3,
|
||||
LL_ADC_CHANNEL_4,
|
||||
LL_ADC_CHANNEL_5,
|
||||
LL_ADC_CHANNEL_6,
|
||||
LL_ADC_CHANNEL_7,
|
||||
LL_ADC_CHANNEL_8,
|
||||
LL_ADC_CHANNEL_9,
|
||||
LL_ADC_CHANNEL_10,
|
||||
LL_ADC_CHANNEL_11,
|
||||
LL_ADC_CHANNEL_12,
|
||||
LL_ADC_CHANNEL_13,
|
||||
LL_ADC_CHANNEL_14,
|
||||
LL_ADC_CHANNEL_15,
|
||||
LL_ADC_CHANNEL_TEMPSENSOR_ADC1,
|
||||
LL_ADC_CHANNEL_VBAT,
|
||||
LL_ADC_CHANNEL_VREFINT,
|
||||
};
|
||||
|
||||
// Rank and sequence definitions for STM32G4 from adc_stm32.c Zephyr driver
|
||||
|
||||
#define RANK(n) LL_ADC_REG_RANK_##n
|
||||
static const uint32_t table_rank[] = {
|
||||
RANK(1), RANK(2), RANK(3), RANK(4), RANK(5), RANK(6), RANK(7), RANK(8),
|
||||
RANK(9), RANK(10), RANK(11), RANK(12), RANK(13), RANK(14), RANK(15), RANK(16),
|
||||
};
|
||||
|
||||
#define SEQ_LEN(n) LL_ADC_REG_SEQ_SCAN_ENABLE_##n##RANKS
|
||||
static const uint32_t table_seq_len[] = {
|
||||
LL_ADC_REG_SEQ_SCAN_DISABLE,
|
||||
SEQ_LEN(2),
|
||||
SEQ_LEN(3),
|
||||
SEQ_LEN(4),
|
||||
SEQ_LEN(5),
|
||||
SEQ_LEN(6),
|
||||
SEQ_LEN(7),
|
||||
SEQ_LEN(8),
|
||||
SEQ_LEN(9),
|
||||
SEQ_LEN(10),
|
||||
SEQ_LEN(11),
|
||||
SEQ_LEN(12),
|
||||
SEQ_LEN(13),
|
||||
SEQ_LEN(14),
|
||||
SEQ_LEN(15),
|
||||
SEQ_LEN(16),
|
||||
};
|
||||
|
||||
#endif /* STM32G4X */
|
||||
|
||||
// for ADC and DMA
|
||||
extern uint16_t adc_readings[];
|
||||
|
||||
void adc_update_value(unsigned int pos);
|
||||
|
||||
static void vref_setup()
|
||||
{
|
||||
#ifdef CONFIG_SOC_SERIES_STM32G4X
|
||||
// output 2.048V at VREF+ pin (also used internally for ADC and DAC)
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
|
||||
LL_VREFBUF_SetVoltageScaling(LL_VREFBUF_VOLTAGE_SCALE0);
|
||||
LL_VREFBUF_DisableHIZ();
|
||||
LL_VREFBUF_Enable();
|
||||
while (LL_VREFBUF_IsVREFReady() == 0) {
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void dac_setup()
|
||||
{
|
||||
#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
/* DAC1 at PA4 for load and DC/DC / PWM switch current measurement * VCC */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_DAC1);
|
||||
LL_DAC_SetOutputBuffer(DAC1, LL_DAC_CHANNEL_1, LL_DAC_OUTPUT_BUFFER_ENABLE);
|
||||
LL_DAC_Enable(DAC1, LL_DAC_CHANNEL_1);
|
||||
LL_DAC_ConvertData12RightAligned(DAC1, LL_DAC_CHANNEL_1, 4096 / 10);
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_DAC1);
|
||||
/* DAC1 at PA4 for bi-directional DC/DC current measurement at 0.5 * VCC */
|
||||
LL_DAC_SetOutputBuffer(DAC1, LL_DAC_CHANNEL_1, LL_DAC_OUTPUT_BUFFER_ENABLE);
|
||||
LL_DAC_Enable(DAC1, LL_DAC_CHANNEL_1);
|
||||
LL_DAC_ConvertData12RightAligned(DAC1, LL_DAC_CHANNEL_1, 4096 / 2);
|
||||
/* DAC1 at PA5 for uni-directional PWM and load current measurement at 0.1 * VCC */
|
||||
LL_DAC_SetOutputBuffer(DAC1, LL_DAC_CHANNEL_2, LL_DAC_OUTPUT_BUFFER_ENABLE);
|
||||
LL_DAC_Enable(DAC1, LL_DAC_CHANNEL_2);
|
||||
LL_DAC_ConvertData12RightAligned(DAC1, LL_DAC_CHANNEL_2, 4096 / 10);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void adc_init(ADC_TypeDef *adc)
|
||||
{
|
||||
LL_ADC_Disable(adc);
|
||||
#if defined(CONFIG_SOC_SERIES_STM32F0X)
|
||||
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_ADC1);
|
||||
LL_ADC_SetClock(adc, LL_ADC_CLOCK_SYNC_PCLK_DIV4);
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
|
||||
LL_ADC_EnableInternalRegulator(adc);
|
||||
k_busy_wait(LL_ADC_DELAY_INTERNAL_REGUL_STAB_US);
|
||||
LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(adc), LL_ADC_CLOCK_SYNC_PCLK_DIV4);
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
// ADC clock can be generated from SYSCLK or PLL (async mode) or derived from AHB clock
|
||||
// (sync mode). For synchronization with a timer, sync mode is preferred.
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC12);
|
||||
// LL_RCC_SetADCClockSource(LL_RCC_ADC12_CLKSOURCE_SYSCLK);
|
||||
|
||||
// Use DIV1 only, as DIV2 and DIV4 lead to errors in ADC readings (not sure why...)
|
||||
LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(adc), LL_ADC_CLOCK_SYNC_PCLK_DIV1);
|
||||
// LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(adc), LL_ADC_CLOCK_ASYNC_DIV1);
|
||||
|
||||
// Prepare for ADC calibration
|
||||
LL_ADC_DisableDeepPowerDown(adc);
|
||||
LL_ADC_EnableInternalRegulator(adc);
|
||||
// Datasheet: wait 20us for regulator to stabilize (taking 100us to be safe)
|
||||
// LL_ADC_DELAY_INTERNAL_REGUL_STAB_US is erroneously set to 10 in stm32g4xx_ll_adc.h
|
||||
k_busy_wait(100);
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
LL_ADC_StartCalibration(adc, LL_ADC_SINGLE_ENDED);
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
LL_ADC_StartCalibration(adc);
|
||||
#endif
|
||||
while (LL_ADC_IsCalibrationOnGoing(adc)) {
|
||||
}
|
||||
|
||||
if (LL_ADC_IsActiveFlag_ADRDY(adc)) {
|
||||
LL_ADC_ClearFlag_ADRDY(adc);
|
||||
}
|
||||
|
||||
// Enable internal reference voltage and temperature
|
||||
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(adc),
|
||||
LL_ADC_PATH_INTERNAL_VREFINT | LL_ADC_PATH_INTERNAL_TEMPSENSOR);
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32F0X)
|
||||
LL_ADC_REG_SetSequencerChannels(adc, ADC_CHSEL);
|
||||
LL_ADC_SetSamplingTimeCommonChannels(adc, LL_ADC_SAMPLINGTIME_239CYCLES_5);
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
LL_ADC_REG_SetSequencerChannels(adc, ADC_CHSEL);
|
||||
LL_ADC_SetSamplingTimeCommonChannels(adc, LL_ADC_SAMPLINGTIME_160CYCLES_5);
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
// more complicated sequencer allows to define the sequence independent
|
||||
// of the channel number (using ranks)
|
||||
int *num_ch = (adc == ADC1) ? &num_adc1_ch : &num_adc2_ch;
|
||||
for (int i = 0; i < NUM_ADC_CH; i++) {
|
||||
if (adc_registers[i] == (uint32_t)adc) {
|
||||
uint32_t ch_number = adc_ch_numbers[i];
|
||||
if (ch_number < sizeof(table_channel)) {
|
||||
LL_ADC_REG_SetSequencerRanks(adc, table_rank[*num_ch], table_channel[ch_number]);
|
||||
LL_ADC_SetChannelSamplingTime(adc, table_channel[ch_number],
|
||||
LL_ADC_SAMPLINGTIME_247CYCLES_5);
|
||||
(*num_ch)++;
|
||||
}
|
||||
}
|
||||
}
|
||||
LL_ADC_REG_SetSequencerLength(adc, table_seq_len[*num_ch - 1]);
|
||||
#endif
|
||||
|
||||
LL_ADC_SetDataAlignment(adc, LL_ADC_DATA_ALIGN_LEFT);
|
||||
LL_ADC_SetResolution(adc, LL_ADC_RESOLUTION_12B);
|
||||
LL_ADC_REG_SetOverrun(adc, LL_ADC_REG_OVR_DATA_OVERWRITTEN);
|
||||
// Enable DMA transfer on ADC and circular mode
|
||||
LL_ADC_REG_SetDMATransfer(adc, LL_ADC_REG_DMA_TRANSFER_UNLIMITED);
|
||||
|
||||
LL_ADC_Enable(adc);
|
||||
}
|
||||
|
||||
#define V_HIGH_ENABLE_NODE DT_CHILD(DT_PATH(adc_inputs), v_high)
|
||||
|
||||
static void adc_setup()
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_PROP(V_HIGH_ENABLE_NODE, enable_gpios))
|
||||
const struct gpio_dt_spec v_high_enable = GPIO_DT_SPEC_GET(V_HIGH_ENABLE_NODE, enable_gpios);
|
||||
if (device_is_ready(v_high_enable.port)) {
|
||||
gpio_pin_configure_dt(&v_high_enable, GPIO_OUTPUT_ACTIVE);
|
||||
}
|
||||
#endif
|
||||
|
||||
adc_init(ADC1);
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
adc_init(ADC2);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void adc_trigger_conversion(struct k_timer *timer_id)
|
||||
{
|
||||
LL_ADC_REG_StartConversion(ADC1);
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
LL_ADC_REG_StartConversion(ADC2);
|
||||
#endif
|
||||
}
|
||||
|
||||
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++) {
|
||||
adc_update_value(i);
|
||||
}
|
||||
}
|
||||
DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt registers
|
||||
}
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
static void DMA2_Channel1_IRQHandler(void *args)
|
||||
{
|
||||
if ((DMA2->ISR & DMA_ISR_TCIF1) != 0) { // Test if transfer completed on DMA channel 2
|
||||
for (unsigned int i = num_adc1_ch; i < num_adc1_ch + num_adc2_ch; i++) {
|
||||
adc_update_value(i);
|
||||
}
|
||||
}
|
||||
DMA2->IFCR |= 0x0FFFFFFF; // clear all interrupt registers
|
||||
|
||||
#ifdef CONFIG_CUSTOM_DCDC_CONTROLLER
|
||||
// Implement this function e.g. for cycle-by-cylce current limitation.
|
||||
// As it runs in an ISR with high frequency, it must be VERY fast!
|
||||
dcdc_low_level_controller();
|
||||
#endif
|
||||
}
|
||||
#endif // CONFIG_SOC_SERIES_STM32G4X
|
||||
|
||||
// Assuming DMA1 is mapped to ADC1 and DMA2 mapped to ADC2.
|
||||
static void dma_init(DMA_TypeDef *dma)
|
||||
{
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMAMUX1);
|
||||
if (dma == DMA1) {
|
||||
LL_DMA_SetPeriphRequest(dma, LL_DMA_CHANNEL_1, LL_DMAMUX_REQ_ADC1);
|
||||
}
|
||||
else if (dma == DMA2) {
|
||||
LL_DMA_SetPeriphRequest(dma, LL_DMA_CHANNEL_1, LL_DMAMUX_REQ_ADC2);
|
||||
}
|
||||
#endif // CONFIG_SOC_SERIES_STM32G4X
|
||||
|
||||
if (dma == DMA1) {
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
|
||||
|
||||
LL_DMA_ConfigAddresses(
|
||||
dma, LL_DMA_CHANNEL_1,
|
||||
LL_ADC_DMA_GetRegAddr(ADC1, LL_ADC_DMA_REG_REGULAR_DATA), // source address
|
||||
(uint32_t)(&(adc_readings[0])), // destination address
|
||||
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
|
||||
|
||||
// Configure the number of DMA transfers (data length in multiples of size per transfer)
|
||||
LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, num_adc1_ch);
|
||||
}
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
else if (dma == DMA2) {
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA2);
|
||||
|
||||
// If DMA 2 is mapped to ADC 2
|
||||
LL_DMA_ConfigAddresses(
|
||||
dma, LL_DMA_CHANNEL_1,
|
||||
LL_ADC_DMA_GetRegAddr(ADC2, LL_ADC_DMA_REG_REGULAR_DATA), // source address
|
||||
// destination address = position behind ADC_1 values
|
||||
(uint32_t)(&(adc_readings[num_adc1_ch])), LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
|
||||
|
||||
// Configure the number of DMA transfers (data length in multiples of size per transfer)
|
||||
LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, num_adc2_ch);
|
||||
}
|
||||
#endif // CONFIG_SOC_SERIES_STM32G4X
|
||||
|
||||
LL_DMA_SetMemoryIncMode(dma, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
|
||||
LL_DMA_SetMemorySize(dma, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD);
|
||||
LL_DMA_SetPeriphSize(dma, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_HALFWORD);
|
||||
LL_DMA_EnableIT_TE(dma, LL_DMA_CHANNEL_1); // transfer error interrupt
|
||||
LL_DMA_EnableIT_TC(dma, LL_DMA_CHANNEL_1); // transfer complete interrupt
|
||||
LL_DMA_SetMode(dma, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
|
||||
|
||||
LL_DMA_EnableChannel(dma, LL_DMA_CHANNEL_1);
|
||||
|
||||
// Configure NVIC for DMA (priority 2: second-lowest value for STM32L0/F0)
|
||||
if (dma == DMA1) {
|
||||
IRQ_CONNECT(DMA1_Channel1_IRQn, 2, DMA1_Channel1_IRQHandler, 0, 0);
|
||||
irq_enable(DMA1_Channel1_IRQn);
|
||||
}
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
else if (dma == DMA2) {
|
||||
IRQ_CONNECT(DMA2_Channel1_IRQn, 2, DMA2_Channel1_IRQHandler, 0, 0);
|
||||
irq_enable(DMA2_Channel1_IRQn);
|
||||
}
|
||||
#endif // CONFIG_SOC_SERIES_STM32G4X
|
||||
}
|
||||
|
||||
static void dma_setup()
|
||||
{
|
||||
dma_init(DMA1);
|
||||
LL_ADC_REG_StartConversion(ADC1);
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
dma_init(DMA2);
|
||||
LL_ADC_REG_StartConversion(ADC2);
|
||||
#endif // CONFIG_SOC_SERIES_STM32G4X
|
||||
}
|
||||
|
||||
void daq_setup()
|
||||
{
|
||||
static struct k_timer adc_trigger_timer;
|
||||
|
||||
vref_setup();
|
||||
dac_setup();
|
||||
adc_setup();
|
||||
dma_setup();
|
||||
|
||||
k_timer_init(&adc_trigger_timer, adc_trigger_conversion, NULL);
|
||||
k_timer_start(&adc_trigger_timer, K_MSEC(1), K_MSEC(1)); // 1 kHz
|
||||
|
||||
k_sleep(K_MSEC(500)); // wait for ADC to collect some measurement values
|
||||
daq_update();
|
||||
calibrate_current_sensors();
|
||||
daq_update();
|
||||
}
|
||||
|
||||
#endif // UNIT_TEST
|
||||
@@ -6,16 +6,11 @@
|
||||
|
||||
#include "data_objects.h"
|
||||
|
||||
#include <soc.h>
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include <zephyr/drivers/hwinfo.h>
|
||||
#include <zephyr/sys/crc.h>
|
||||
|
||||
#ifdef CONFIG_SOC_FAMILY_STM32
|
||||
#include <stm32_ll_utils.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -35,14 +30,6 @@ char device_type[] = DT_PROP(DT_PATH(pcb), type);
|
||||
char hardware_version[] = DT_PROP(DT_PATH(pcb), version_str);
|
||||
char firmware_version[] = FIRMWARE_VERSION_ID;
|
||||
|
||||
#ifdef CONFIG_SOC_FAMILY_STM32
|
||||
uint32_t flash_size = LL_GetFlashSize();
|
||||
uint32_t flash_page_size = FLASH_PAGE_SIZE;
|
||||
#else
|
||||
uint32_t flash_size = 128;
|
||||
uint32_t flash_page_size = 0x800;
|
||||
#endif
|
||||
|
||||
#if CONFIG_LV_TERMINAL_BATTERY
|
||||
#define bat_bus lv_bus
|
||||
#elif CONFIG_HV_TERMINAL_BATTERY
|
||||
@@ -829,7 +816,7 @@ THINGSET_ADD_ITEM_BOOL(ID_LOAD, 0x8B7, "sEnableDefault", &load.enable,
|
||||
"de": "Abschaltspannung Lastausgang"
|
||||
}
|
||||
}*/
|
||||
THINGSET_ADD_ITEM_FLOAT(ID_LOAD, 0xB8, "sDisconnectVoltage_V",
|
||||
THINGSET_ADD_ITEM_FLOAT(ID_LOAD, 0x8B8, "sDisconnectVoltage_V",
|
||||
&bat_conf_user.load_disconnect_voltage, 2, THINGSET_ANY_R | THINGSET_ANY_W,
|
||||
TS_SUBSET_NVM);
|
||||
|
||||
|
||||
+452
@@ -0,0 +1,452 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "dcdc.h"
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
#include <math.h> // for fabs function
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // for min/max function
|
||||
|
||||
#include "board.h"
|
||||
#include "device_status.h"
|
||||
#include "helper.h"
|
||||
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include <thingset.h>
|
||||
#include <thingset/sdk.h>
|
||||
#include <thingset/storage.h>
|
||||
|
||||
LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL);
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
|
||||
#define BUCK_DUTY_POWER_DECREASE (-1)
|
||||
#define BUCK_DUTY_POWER_INCREASE (1)
|
||||
|
||||
#define BOOST_DUTY_POWER_DECREASE (1)
|
||||
#define BOOST_DUTY_POWER_INCREASE (-1)
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_out))
|
||||
#define HV_OUT_NODE DT_CHILD(DT_PATH(outputs), hv_out)
|
||||
#endif
|
||||
|
||||
static void output_hvs_enable(void);
|
||||
static void output_hvs_disable(void);
|
||||
|
||||
void dcdc_init(struct dcdc *dcdc, const struct device *hb, struct dc_bus *high, struct dc_bus *low,
|
||||
enum dcdc_operation_mode op_mode)
|
||||
{
|
||||
dcdc->half_bridge = hb;
|
||||
dcdc->hvb = high;
|
||||
dcdc->lvb = low;
|
||||
dcdc->mode = op_mode;
|
||||
dcdc->enable = true;
|
||||
dcdc->state = DCDC_CONTROL_OFF;
|
||||
dcdc->inductor_current_max = DT_PROP(DT_PATH(pcb), dcdc_current_max);
|
||||
dcdc->hs_voltage_max = DT_PROP(DT_PATH(pcb), hs_voltage_max);
|
||||
dcdc->ls_voltage_max = DT_PROP(DT_PATH(pcb), ls_voltage_max);
|
||||
dcdc->ls_voltage_min = 9.0;
|
||||
dcdc->output_power_min = 1; // switch off if power < 1 W
|
||||
dcdc->restart_interval = 60;
|
||||
dcdc->off_timestamp = -10000; // start immediately
|
||||
|
||||
if (!device_is_ready(dcdc->half_bridge)) {
|
||||
LOG_ERR("Half bridge not ready");
|
||||
dcdc->enable = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// lower duty limit might have to be adjusted dynamically depending on LS voltage
|
||||
half_bridge_set_duty_limits(dcdc->half_bridge, 12 / dcdc->hs_voltage_max, 0.97);
|
||||
half_bridge_set_duty_cycle(dcdc->half_bridge, 0.97); // init with allowed value
|
||||
}
|
||||
|
||||
static void perturb_observe_buck(struct dcdc *dcdc)
|
||||
{
|
||||
if (dcdc->power >= dcdc->output_power_min) {
|
||||
dcdc->power_good_timestamp = uptime();
|
||||
}
|
||||
|
||||
if ((uptime() - dcdc->power_good_timestamp > 10 || dcdc->power < -10.0f)
|
||||
&& dcdc->mode != DCDC_MODE_AUTO)
|
||||
{
|
||||
// switch off after 10s low power or negative power (if not in nanogrid mode)
|
||||
dcdc->pwm_direction = 0;
|
||||
}
|
||||
else if (dcdc->lvb->voltage > dc_bus_sink_control_voltage(dcdc->lvb, 0)) {
|
||||
dcdc->state = DCDC_CONTROL_CV_LS;
|
||||
dcdc->pwm_direction = BUCK_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (dcdc->lvb->sink_current_margin < 0
|
||||
|| dcdc->inductor_current > dcdc->inductor_current_max)
|
||||
{
|
||||
dcdc->state = DCDC_CONTROL_CC_LS;
|
||||
dcdc->pwm_direction = BUCK_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (dcdc->hvb->src_current_margin > 0) {
|
||||
dcdc->state = DCDC_CONTROL_CC_HS;
|
||||
dcdc->pwm_direction = BUCK_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (dcdc->hvb->voltage < dc_bus_src_control_voltage(dcdc->hvb, 0)
|
||||
&& dcdc->power > dcdc->output_power_min)
|
||||
{
|
||||
// input voltage below limit
|
||||
dcdc->state = DCDC_CONTROL_CV_HS;
|
||||
dcdc->pwm_direction = BUCK_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (dcdc->temp_mosfets > DCDC_MOSFETS_MAX_TEMP) {
|
||||
dcdc->state = DCDC_CONTROL_DERATING;
|
||||
dcdc->pwm_direction = BUCK_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (dcdc->power < dcdc->output_power_min
|
||||
&& dcdc->lvb->voltage < dc_bus_src_control_voltage(dcdc->lvb, 0))
|
||||
{
|
||||
// no load condition (e.g. start-up of nanogrid) --> raise voltage
|
||||
dcdc->pwm_direction = BUCK_DUTY_POWER_INCREASE;
|
||||
}
|
||||
else {
|
||||
dcdc->state = DCDC_CONTROL_MPPT;
|
||||
if (dcdc->power_prev > dcdc->power) {
|
||||
dcdc->pwm_direction = -dcdc->pwm_direction;
|
||||
}
|
||||
}
|
||||
|
||||
dcdc->power_prev = dcdc->power;
|
||||
}
|
||||
|
||||
static void perturb_observe_boost(struct dcdc *dcdc)
|
||||
{
|
||||
if (-dcdc->power >= dcdc->output_power_min) {
|
||||
dcdc->power_good_timestamp = uptime();
|
||||
}
|
||||
|
||||
if ((uptime() - dcdc->power_good_timestamp > 10 || -dcdc->power < -10.0f)
|
||||
&& dcdc->mode != DCDC_MODE_AUTO)
|
||||
{
|
||||
// switch off after 10s low power or negative power (if not in nanogrid mode)
|
||||
dcdc->pwm_direction = 0;
|
||||
}
|
||||
else if (dcdc->hvb->voltage > dc_bus_sink_control_voltage(dcdc->hvb, 0)) {
|
||||
dcdc->state = DCDC_CONTROL_CV_HS;
|
||||
dcdc->pwm_direction = BOOST_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (dcdc->hvb->sink_current_margin < 0) {
|
||||
dcdc->state = DCDC_CONTROL_CC_HS;
|
||||
dcdc->pwm_direction = BOOST_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (dcdc->lvb->src_current_margin > 0
|
||||
|| -dcdc->inductor_current > dcdc->inductor_current_max)
|
||||
{
|
||||
dcdc->state = DCDC_CONTROL_CC_LS;
|
||||
dcdc->pwm_direction = BOOST_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (dcdc->lvb->voltage < dc_bus_src_control_voltage(dcdc->lvb, 0)
|
||||
&& -dcdc->power > dcdc->output_power_min)
|
||||
{
|
||||
// input voltage below limit
|
||||
dcdc->state = DCDC_CONTROL_CV_LS;
|
||||
dcdc->pwm_direction = BOOST_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (dcdc->temp_mosfets > DCDC_MOSFETS_MAX_TEMP) {
|
||||
dcdc->state = DCDC_CONTROL_DERATING;
|
||||
dcdc->pwm_direction = BOOST_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (-dcdc->power < dcdc->output_power_min
|
||||
&& dcdc->hvb->voltage < dc_bus_src_control_voltage(dcdc->hvb, 0))
|
||||
{
|
||||
// no load condition (e.g. start-up of nanogrid) --> raise voltage
|
||||
dcdc->pwm_direction = BOOST_DUTY_POWER_INCREASE;
|
||||
}
|
||||
else {
|
||||
dcdc->state = DCDC_CONTROL_MPPT;
|
||||
if (-dcdc->power_prev > -dcdc->power) {
|
||||
dcdc->pwm_direction = -dcdc->pwm_direction;
|
||||
}
|
||||
}
|
||||
|
||||
dcdc->power_prev = dcdc->power;
|
||||
}
|
||||
|
||||
__weak enum dcdc_operation_mode dcdc_check_start_conditions(struct dcdc *dcdc)
|
||||
{
|
||||
if (dcdc->enable == false
|
||||
|| dcdc->hvb->voltage
|
||||
> dcdc->hs_voltage_max // also critical for buck mode because of ringing
|
||||
|| dcdc->lvb->voltage > dcdc->ls_voltage_max || dcdc->lvb->voltage < dcdc->ls_voltage_min
|
||||
|| device_status_has_error(&dev_stat, ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE)
|
||||
|| (int)uptime() < (dcdc->off_timestamp + (int)dcdc->restart_interval))
|
||||
{
|
||||
return DCDC_MODE_OFF;
|
||||
}
|
||||
|
||||
if (dcdc->lvb->sink_current_margin > 0
|
||||
&& dcdc->lvb->voltage < dc_bus_sink_control_voltage(dcdc->lvb, 0)
|
||||
&& dcdc->hvb->src_current_margin < 0
|
||||
&& dcdc->hvb->voltage > dc_bus_src_control_voltage(dcdc->hvb, 0)
|
||||
&& dcdc->hvb->voltage * 0.85f > dcdc->lvb->voltage)
|
||||
{
|
||||
return DCDC_MODE_BUCK;
|
||||
}
|
||||
|
||||
if (dcdc->hvb->sink_current_margin > 0
|
||||
&& dcdc->hvb->voltage < dc_bus_sink_control_voltage(dcdc->hvb, 0)
|
||||
&& dcdc->lvb->src_current_margin < 0
|
||||
&& dcdc->lvb->voltage > dc_bus_src_control_voltage(dcdc->lvb, 0))
|
||||
{
|
||||
return DCDC_MODE_BOOST;
|
||||
}
|
||||
|
||||
return DCDC_MODE_OFF;
|
||||
}
|
||||
|
||||
static bool check_hs_mosfet_short(struct dcdc *dcdc)
|
||||
{
|
||||
static uint32_t first_time_detected;
|
||||
if (half_bridge_is_enabled(dcdc->half_bridge) == false && dcdc->inductor_current > 0.5F
|
||||
&& dcdc->lvb->voltage_filtered > dc_bus_sink_control_voltage(dcdc->lvb, 0))
|
||||
{
|
||||
// if there is current even though the DC/DC is switched off, the
|
||||
// high-side MOSFET must be broken --> set flag and let main() decide
|
||||
// what to do... (e.g. call dcdc_self_destruction)
|
||||
|
||||
uint32_t now = uptime();
|
||||
if (first_time_detected == 0) {
|
||||
first_time_detected = now;
|
||||
}
|
||||
else if (now - first_time_detected > 10) {
|
||||
// waited approx 10s before setting the flag
|
||||
device_status_set_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
|
||||
}
|
||||
}
|
||||
else {
|
||||
first_time_detected = 0;
|
||||
}
|
||||
|
||||
return device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
|
||||
}
|
||||
|
||||
static bool startup_inhibit(bool reset)
|
||||
{
|
||||
static uint32_t inhibit_start;
|
||||
|
||||
if (reset) {
|
||||
inhibit_start = uptime();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return (uptime() < inhibit_start + DCDC_STARTUP_INHIBIT_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
__weak void dcdc_control(struct dcdc *dcdc)
|
||||
{
|
||||
if (device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT)) {
|
||||
// DC/DC must not be operated anymore and the low-side switch might be forced on to blow
|
||||
// the fuse, so don't touch the half bridge
|
||||
return;
|
||||
}
|
||||
|
||||
if (half_bridge_is_enabled(dcdc->half_bridge) == false) {
|
||||
|
||||
if (check_hs_mosfet_short(dcdc)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int startup_mode = dcdc_check_start_conditions(dcdc);
|
||||
|
||||
if ((startup_mode == DCDC_MODE_BUCK && dcdc->mode == DCDC_MODE_BUCK)
|
||||
|| (startup_mode == DCDC_MODE_BOOST && dcdc->mode == DCDC_MODE_BOOST)
|
||||
|| (startup_mode != DCDC_MODE_OFF && dcdc->mode == DCDC_MODE_AUTO))
|
||||
{
|
||||
output_hvs_enable();
|
||||
|
||||
// startup allowed, but we need to wait until voltages settle
|
||||
if (startup_inhibit(false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char *mode_name = NULL;
|
||||
if (startup_mode == DCDC_MODE_BUCK) {
|
||||
mode_name = "buck";
|
||||
dcdc->pwm_direction = BUCK_DUTY_POWER_INCREASE;
|
||||
// Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush
|
||||
// currents and stress on MOSFETs
|
||||
half_bridge_set_duty_cycle(dcdc->half_bridge,
|
||||
dcdc->lvb->voltage / (dcdc->hvb->voltage - 1));
|
||||
}
|
||||
else {
|
||||
mode_name = "boost";
|
||||
dcdc->pwm_direction = BOOST_DUTY_POWER_INCREASE;
|
||||
// Will automatically start with max. duty (0.97) if connected to a
|
||||
// nanogrid not yet started up (zero voltage)
|
||||
half_bridge_set_duty_cycle(dcdc->half_bridge,
|
||||
dcdc->lvb->voltage / (dcdc->hvb->voltage + 1));
|
||||
}
|
||||
|
||||
half_bridge_start(dcdc->half_bridge);
|
||||
dcdc->power_good_timestamp = uptime();
|
||||
printf("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name,
|
||||
(double)dcdc->hvb->voltage, (double)dcdc->lvb->voltage,
|
||||
(double)half_bridge_get_duty_cycle(dcdc->half_bridge) * 100.0);
|
||||
}
|
||||
else {
|
||||
startup_inhibit(true); // reset inhibit counter
|
||||
}
|
||||
}
|
||||
else { // half bridge is on
|
||||
|
||||
const char *stop_reason = NULL;
|
||||
if (dcdc->lvb->voltage > dcdc->ls_voltage_max || dcdc->hvb->voltage > dcdc->hs_voltage_max)
|
||||
{
|
||||
stop_reason = "emergency (voltage limits exceeded)";
|
||||
}
|
||||
else if (dcdc->enable == false) {
|
||||
stop_reason = "disabled";
|
||||
}
|
||||
else {
|
||||
if (dcdc->mode == DCDC_MODE_BUCK
|
||||
|| (dcdc->mode == DCDC_MODE_AUTO && dcdc->inductor_current > 0.1f))
|
||||
{
|
||||
perturb_observe_buck(dcdc);
|
||||
}
|
||||
else {
|
||||
perturb_observe_boost(dcdc);
|
||||
}
|
||||
|
||||
if (dcdc->pwm_direction != 0) {
|
||||
half_bridge_set_compare(dcdc->half_bridge,
|
||||
half_bridge_get_compare(dcdc->half_bridge)
|
||||
+ dcdc->pwm_direction * CONFIG_DCDC_DUTY_STEP_SIZE);
|
||||
|
||||
// requires floating point support with CONFIG_CBPRINTF_FP_SUPPORT=y
|
||||
LOG_DBG("P %.2fW, inductor %.2fA, HS: %.2fV, %.2fA margin, "
|
||||
"LS: %.2fV (target %.2fV), %.2fA margin, "
|
||||
"PWM: %.1f, dcdc_state: %d, pwm_direction: %d",
|
||||
(double)dcdc->power, (double)dcdc->inductor_current,
|
||||
(double)dcdc->hvb->voltage, (double)dcdc->hvb->src_current_margin,
|
||||
(double)dcdc->lvb->voltage, (double)dcdc->lvb->sink_voltage_intercept,
|
||||
(double)dcdc->lvb->sink_current_margin,
|
||||
(double)half_bridge_get_duty_cycle(dcdc->half_bridge) * 100.0, dcdc->state,
|
||||
dcdc->pwm_direction);
|
||||
}
|
||||
else {
|
||||
stop_reason = "low power";
|
||||
}
|
||||
}
|
||||
|
||||
if (stop_reason != NULL) {
|
||||
dcdc_stop(dcdc);
|
||||
printf("DC/DC Stop: %s.\n", stop_reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dcdc_test(struct dcdc *dcdc)
|
||||
{
|
||||
if (half_bridge_is_enabled(dcdc->half_bridge)) {
|
||||
const char *stop_reason = NULL;
|
||||
if (dcdc->lvb->voltage > dcdc->ls_voltage_max || dcdc->hvb->voltage > dcdc->hs_voltage_max)
|
||||
{
|
||||
stop_reason = "emergency (voltage limits exceeded)";
|
||||
}
|
||||
else if (dcdc->enable == false) {
|
||||
stop_reason = "disabled";
|
||||
}
|
||||
else {
|
||||
if (half_bridge_get_duty_cycle(dcdc->half_bridge) > 0.50f) {
|
||||
half_bridge_set_compare(dcdc->half_bridge,
|
||||
half_bridge_get_compare(dcdc->half_bridge) - 1);
|
||||
}
|
||||
else {
|
||||
half_bridge_set_compare(dcdc->half_bridge,
|
||||
half_bridge_get_compare(dcdc->half_bridge) + 1);
|
||||
}
|
||||
}
|
||||
if (stop_reason != NULL) {
|
||||
dcdc_stop(dcdc);
|
||||
printf("DC/DC Stop: %s.\n", stop_reason);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (dcdc_check_start_conditions(dcdc) != DCDC_MODE_OFF) {
|
||||
// startup allowed, but we need to wait until voltages settle
|
||||
if (startup_inhibit(false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
half_bridge_set_duty_cycle(dcdc->half_bridge, dcdc->lvb->voltage / dcdc->hvb->voltage);
|
||||
half_bridge_start(dcdc->half_bridge);
|
||||
printf("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
|
||||
(double)dcdc->hvb->voltage, (double)dcdc->lvb->voltage,
|
||||
(double)half_bridge_get_duty_cycle(dcdc->half_bridge) * 100.0);
|
||||
}
|
||||
else {
|
||||
startup_inhibit(true); // reset inhibit counter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dcdc_stop(struct dcdc *dcdc)
|
||||
{
|
||||
half_bridge_stop(dcdc->half_bridge);
|
||||
dcdc->state = DCDC_CONTROL_OFF;
|
||||
dcdc->off_timestamp = uptime();
|
||||
output_hvs_disable();
|
||||
}
|
||||
|
||||
void dcdc_fuse_destruction(struct dcdc *dcdc)
|
||||
{
|
||||
static int counter = 0;
|
||||
|
||||
if (counter > 20) { // wait 20s to be able to send out data
|
||||
if (counter == 21) {
|
||||
LOG_ERR("Charge controller fuse destruction called!\n");
|
||||
#ifdef CONFIG_THINGSET_STORAGE
|
||||
thingset_storage_save();
|
||||
#endif
|
||||
}
|
||||
// repeated in case the half bridge was stopped in the meantime
|
||||
half_bridge_force_low_side(dcdc->half_bridge);
|
||||
// now the fuse should be triggered and we disappear
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
|
||||
static void output_hvs_enable(void)
|
||||
{
|
||||
#ifdef HV_OUT_NODE
|
||||
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
|
||||
if (device_is_ready(hv_out)) {
|
||||
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
|
||||
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_ACTIVE);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void output_hvs_disable(void)
|
||||
{
|
||||
#ifdef HV_OUT_NODE
|
||||
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
|
||||
if (device_is_ready(hv_out)) {
|
||||
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
|
||||
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_INACTIVE);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void dcdc_init(struct dcdc *dcdc, const struct device *hb, struct dc_bus *high, struct dc_bus *low,
|
||||
enum dcdc_operation_mode op_mode)
|
||||
{}
|
||||
|
||||
#endif /* BOARD_HAS_DCDC */
|
||||
@@ -1,406 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "dcdc.h"
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
#include <math.h> // for fabs function
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // for min/max function
|
||||
|
||||
#include "device_status.h"
|
||||
#include "half_bridge.h"
|
||||
#include "helper.h"
|
||||
#include "setup.h"
|
||||
|
||||
#include <thingset.h>
|
||||
#include <thingset/sdk.h>
|
||||
#include <thingset/storage.h>
|
||||
|
||||
LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL);
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
|
||||
#define BUCK_DUTY_POWER_DECREASE (-1)
|
||||
#define BUCK_DUTY_POWER_INCREASE (1)
|
||||
|
||||
#define BOOST_DUTY_POWER_DECREASE (1)
|
||||
#define BOOST_DUTY_POWER_INCREASE (-1)
|
||||
|
||||
#ifdef CONFIG_SOC_SERIES_STM32G4X
|
||||
#define DUTY_STEP_SIZE (3) // increased step size for fast microcontroller
|
||||
#else
|
||||
#define DUTY_STEP_SIZE (1) // single minimum step for other microcontrollers
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_out))
|
||||
#define HV_OUT_NODE DT_CHILD(DT_PATH(outputs), hv_out)
|
||||
#endif
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
|
||||
Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode)
|
||||
{
|
||||
hvb = high;
|
||||
lvb = low;
|
||||
mode = op_mode;
|
||||
enable = true;
|
||||
state = DCDC_CONTROL_OFF;
|
||||
inductor_current_max = DT_PROP(DT_PATH(pcb), dcdc_current_max);
|
||||
hs_voltage_max = DT_PROP(DT_PATH(pcb), hs_voltage_max);
|
||||
ls_voltage_max = DT_PROP(DT_PATH(pcb), ls_voltage_max);
|
||||
ls_voltage_min = 9.0;
|
||||
output_power_min = 1; // switch off if power < 1 W
|
||||
restart_interval = 60;
|
||||
off_timestamp = -10000; // start immediately
|
||||
|
||||
// lower duty limit might have to be adjusted dynamically depending on LS voltage
|
||||
half_bridge_init(DT_PROP(DT_INST(0, half_bridge), frequency) / 1000,
|
||||
DT_PROP(DT_INST(0, half_bridge), deadtime), 12 / hs_voltage_max, 0.97);
|
||||
}
|
||||
|
||||
void Dcdc::perturb_observe_buck()
|
||||
{
|
||||
if (power >= output_power_min) {
|
||||
power_good_timestamp = uptime();
|
||||
}
|
||||
|
||||
if ((uptime() - power_good_timestamp > 10 || power < -10.0f) && mode != DCDC_MODE_AUTO) {
|
||||
// switch off after 10s low power or negative power (if not in nanogrid mode)
|
||||
pwm_direction = 0;
|
||||
}
|
||||
else if (lvb->voltage > lvb->sink_control_voltage()) {
|
||||
state = DCDC_CONTROL_CV_LS;
|
||||
pwm_direction = BUCK_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (lvb->sink_current_margin < 0 || inductor_current > inductor_current_max) {
|
||||
state = DCDC_CONTROL_CC_LS;
|
||||
pwm_direction = BUCK_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (hvb->src_current_margin > 0) {
|
||||
state = DCDC_CONTROL_CC_HS;
|
||||
pwm_direction = BUCK_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (hvb->voltage < hvb->src_control_voltage() && power > output_power_min) {
|
||||
// input voltage below limit
|
||||
state = DCDC_CONTROL_CV_HS;
|
||||
pwm_direction = BUCK_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (temp_mosfets > DCDC_MOSFETS_MAX_TEMP) {
|
||||
state = DCDC_CONTROL_DERATING;
|
||||
pwm_direction = BUCK_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (power < output_power_min && lvb->voltage < lvb->src_control_voltage()) {
|
||||
// no load condition (e.g. start-up of nanogrid) --> raise voltage
|
||||
pwm_direction = BUCK_DUTY_POWER_INCREASE;
|
||||
}
|
||||
else {
|
||||
state = DCDC_CONTROL_MPPT;
|
||||
if (power_prev > power) {
|
||||
pwm_direction = -pwm_direction;
|
||||
}
|
||||
}
|
||||
|
||||
power_prev = power;
|
||||
}
|
||||
|
||||
void Dcdc::perturb_observe_boost()
|
||||
{
|
||||
if (-power >= output_power_min) {
|
||||
power_good_timestamp = uptime();
|
||||
}
|
||||
|
||||
if ((uptime() - power_good_timestamp > 10 || -power < -10.0f) && mode != DCDC_MODE_AUTO) {
|
||||
// switch off after 10s low power or negative power (if not in nanogrid mode)
|
||||
pwm_direction = 0;
|
||||
}
|
||||
else if (hvb->voltage > hvb->sink_control_voltage()) {
|
||||
state = DCDC_CONTROL_CV_HS;
|
||||
pwm_direction = BOOST_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (hvb->sink_current_margin < 0) {
|
||||
state = DCDC_CONTROL_CC_HS;
|
||||
pwm_direction = BOOST_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (lvb->src_current_margin > 0 || -inductor_current > inductor_current_max) {
|
||||
state = DCDC_CONTROL_CC_LS;
|
||||
pwm_direction = BOOST_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (lvb->voltage < lvb->src_control_voltage() && -power > output_power_min) {
|
||||
// input voltage below limit
|
||||
state = DCDC_CONTROL_CV_LS;
|
||||
pwm_direction = BOOST_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (temp_mosfets > DCDC_MOSFETS_MAX_TEMP) {
|
||||
state = DCDC_CONTROL_DERATING;
|
||||
pwm_direction = BOOST_DUTY_POWER_DECREASE;
|
||||
}
|
||||
else if (-power < output_power_min && hvb->voltage < hvb->src_control_voltage()) {
|
||||
// no load condition (e.g. start-up of nanogrid) --> raise voltage
|
||||
pwm_direction = BOOST_DUTY_POWER_INCREASE;
|
||||
}
|
||||
else {
|
||||
state = DCDC_CONTROL_MPPT;
|
||||
if (-power_prev > -power) {
|
||||
pwm_direction = -pwm_direction;
|
||||
}
|
||||
}
|
||||
|
||||
power_prev = power;
|
||||
}
|
||||
|
||||
__weak DcdcOperationMode Dcdc::check_start_conditions()
|
||||
{
|
||||
if (enable == false
|
||||
|| hvb->voltage > hs_voltage_max // also critical for buck mode because of ringing
|
||||
|| lvb->voltage > ls_voltage_max || lvb->voltage < ls_voltage_min
|
||||
|| dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE)
|
||||
|| (int)uptime() < (off_timestamp + (int)restart_interval))
|
||||
{
|
||||
return DCDC_MODE_OFF;
|
||||
}
|
||||
|
||||
if (lvb->sink_current_margin > 0 && lvb->voltage < lvb->sink_control_voltage()
|
||||
&& hvb->src_current_margin < 0 && hvb->voltage > hvb->src_control_voltage()
|
||||
&& hvb->voltage * 0.85f > lvb->voltage)
|
||||
{
|
||||
return DCDC_MODE_BUCK;
|
||||
}
|
||||
|
||||
if (hvb->sink_current_margin > 0 && hvb->voltage < hvb->sink_control_voltage()
|
||||
&& lvb->src_current_margin < 0 && lvb->voltage > lvb->src_control_voltage())
|
||||
{
|
||||
return DCDC_MODE_BOOST;
|
||||
}
|
||||
|
||||
return DCDC_MODE_OFF;
|
||||
}
|
||||
|
||||
bool Dcdc::check_hs_mosfet_short()
|
||||
{
|
||||
static uint32_t first_time_detected;
|
||||
if (half_bridge_enabled() == false && inductor_current > 0.5F
|
||||
&& lvb->voltage_filtered > lvb->sink_control_voltage())
|
||||
{
|
||||
// if there is current even though the DC/DC is switched off, the
|
||||
// high-side MOSFET must be broken --> set flag and let main() decide
|
||||
// what to do... (e.g. call dcdc_self_destruction)
|
||||
|
||||
uint32_t now = uptime();
|
||||
if (first_time_detected == 0) {
|
||||
first_time_detected = now;
|
||||
}
|
||||
else if (now - first_time_detected > 10) {
|
||||
// waited approx 10s before setting the flag
|
||||
dev_stat.set_error(ERR_DCDC_HS_MOSFET_SHORT);
|
||||
}
|
||||
}
|
||||
else {
|
||||
first_time_detected = 0;
|
||||
}
|
||||
|
||||
return dev_stat.has_error(ERR_DCDC_HS_MOSFET_SHORT);
|
||||
}
|
||||
|
||||
bool Dcdc::startup_inhibit(bool reset)
|
||||
{
|
||||
static uint32_t inhibit_start;
|
||||
|
||||
if (reset) {
|
||||
inhibit_start = uptime();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return (uptime() < inhibit_start + DCDC_STARTUP_INHIBIT_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
__weak void Dcdc::control()
|
||||
{
|
||||
if (half_bridge_enabled() == false) {
|
||||
|
||||
if (check_hs_mosfet_short()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int startup_mode = check_start_conditions();
|
||||
|
||||
if ((startup_mode == DCDC_MODE_BUCK && mode == DCDC_MODE_BUCK)
|
||||
|| (startup_mode == DCDC_MODE_BOOST && mode == DCDC_MODE_BOOST)
|
||||
|| (startup_mode != DCDC_MODE_OFF && mode == DCDC_MODE_AUTO))
|
||||
{
|
||||
output_hvs_enable();
|
||||
|
||||
// startup allowed, but we need to wait until voltages settle
|
||||
if (startup_inhibit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char *mode_name = NULL;
|
||||
if (startup_mode == DCDC_MODE_BUCK) {
|
||||
mode_name = "buck";
|
||||
pwm_direction = BUCK_DUTY_POWER_INCREASE;
|
||||
// Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush
|
||||
// currents and stress on MOSFETs
|
||||
half_bridge_set_duty_cycle(lvb->voltage / (hvb->voltage - 1));
|
||||
}
|
||||
else {
|
||||
mode_name = "boost";
|
||||
pwm_direction = BOOST_DUTY_POWER_INCREASE;
|
||||
// Will automatically start with max. duty (0.97) if connected to a
|
||||
// nanogrid not yet started up (zero voltage)
|
||||
half_bridge_set_duty_cycle(lvb->voltage / (hvb->voltage + 1));
|
||||
}
|
||||
|
||||
half_bridge_start();
|
||||
power_good_timestamp = uptime();
|
||||
printf("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name,
|
||||
(double)hvb->voltage, (double)lvb->voltage,
|
||||
(double)half_bridge_get_duty_cycle() * 100.0);
|
||||
}
|
||||
else {
|
||||
startup_inhibit(true); // reset inhibit counter
|
||||
}
|
||||
}
|
||||
else { // half bridge is on
|
||||
|
||||
const char *stop_reason = NULL;
|
||||
if (lvb->voltage > ls_voltage_max || hvb->voltage > hs_voltage_max) {
|
||||
stop_reason = "emergency (voltage limits exceeded)";
|
||||
}
|
||||
else if (enable == false) {
|
||||
stop_reason = "disabled";
|
||||
}
|
||||
else {
|
||||
if (mode == DCDC_MODE_BUCK || (mode == DCDC_MODE_AUTO && inductor_current > 0.1f)) {
|
||||
perturb_observe_buck();
|
||||
}
|
||||
else {
|
||||
perturb_observe_boost();
|
||||
}
|
||||
|
||||
if (pwm_direction != 0) {
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() + pwm_direction * DUTY_STEP_SIZE);
|
||||
|
||||
// requires floating point support with CONFIG_CBPRINTF_FP_SUPPORT=y
|
||||
LOG_DBG("P %.2fW, inductor %.2fA, HS: %.2fV, %.2fA margin, "
|
||||
"LS: %.2fV (target %.2fV), %.2fA margin, "
|
||||
"PWM: %.1f, dcdc_state: %d, pwm_direction: %d",
|
||||
(double)power, (double)inductor_current, (double)hvb->voltage,
|
||||
(double)hvb->src_current_margin, (double)lvb->voltage,
|
||||
(double)lvb->sink_voltage_intercept, (double)lvb->sink_current_margin,
|
||||
(double)half_bridge_get_duty_cycle() * 100.0, state, pwm_direction);
|
||||
}
|
||||
else {
|
||||
stop_reason = "low power";
|
||||
}
|
||||
}
|
||||
|
||||
if (stop_reason != NULL) {
|
||||
stop();
|
||||
printf("DC/DC Stop: %s.\n", stop_reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Dcdc::test()
|
||||
{
|
||||
if (half_bridge_enabled()) {
|
||||
const char *stop_reason = NULL;
|
||||
if (lvb->voltage > ls_voltage_max || hvb->voltage > hs_voltage_max) {
|
||||
stop_reason = "emergency (voltage limits exceeded)";
|
||||
}
|
||||
else if (enable == false) {
|
||||
stop_reason = "disabled";
|
||||
}
|
||||
else {
|
||||
if (half_bridge_get_duty_cycle() > 0.50f) {
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() - 1);
|
||||
}
|
||||
else {
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() + 1);
|
||||
}
|
||||
}
|
||||
if (stop_reason != NULL) {
|
||||
stop();
|
||||
printf("DC/DC Stop: %s.\n", stop_reason);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (check_start_conditions() != DCDC_MODE_OFF) {
|
||||
// startup allowed, but we need to wait until voltages settle
|
||||
if (startup_inhibit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
half_bridge_set_duty_cycle(lvb->voltage / hvb->voltage);
|
||||
half_bridge_start();
|
||||
printf("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
|
||||
(double)hvb->voltage, (double)lvb->voltage,
|
||||
(double)half_bridge_get_duty_cycle() * 100.0);
|
||||
}
|
||||
else {
|
||||
startup_inhibit(true); // reset inhibit counter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Dcdc::stop()
|
||||
{
|
||||
half_bridge_stop();
|
||||
state = DCDC_CONTROL_OFF;
|
||||
off_timestamp = uptime();
|
||||
output_hvs_disable();
|
||||
}
|
||||
|
||||
void Dcdc::fuse_destruction()
|
||||
{
|
||||
static int counter = 0;
|
||||
|
||||
if (counter > 20) { // wait 20s to be able to send out data
|
||||
LOG_ERR("Charge controller fuse destruction called!\n");
|
||||
thingset_storage_save();
|
||||
half_bridge_stop();
|
||||
half_bridge_init(50, 0, 0, 0.98); // reset safety limits to allow 0% duty cycle
|
||||
half_bridge_set_duty_cycle(0);
|
||||
half_bridge_start();
|
||||
// now the fuse should be triggered and we disappear
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
|
||||
void Dcdc::output_hvs_enable()
|
||||
{
|
||||
#ifdef HV_OUT_NODE
|
||||
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
|
||||
if (device_is_ready(hv_out)) {
|
||||
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
|
||||
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_ACTIVE);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void Dcdc::output_hvs_disable()
|
||||
{
|
||||
#ifdef HV_OUT_NODE
|
||||
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
|
||||
if (device_is_ready(hv_out)) {
|
||||
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
|
||||
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_INACTIVE);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode)
|
||||
{}
|
||||
|
||||
#endif /* BOARD_HAS_DCDC */
|
||||
+80
-108
@@ -13,16 +13,20 @@
|
||||
* @brief DC/DC buck/boost control functions
|
||||
*
|
||||
* High-level functions to control the DC/DC converter. This module does not directly access any
|
||||
* hardware. The PWM signal for the half bridge is generated in a separate module.
|
||||
* hardware. The PWM signal for the half bridge is generated by a half-bridge driver.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <zephyr/device.h>
|
||||
|
||||
#include "power_port.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DCDC_STARTUP_INHIBIT_TIME 3 // seconds
|
||||
|
||||
#define DCDC_MOSFETS_MAX_TEMP (80) // °C
|
||||
@@ -32,7 +36,7 @@
|
||||
*
|
||||
* Defines which type of device is connected to the high side and low side ports
|
||||
*/
|
||||
enum DcdcOperationMode
|
||||
enum dcdc_operation_mode
|
||||
{
|
||||
/**
|
||||
* DC/DC converter switched off
|
||||
@@ -69,7 +73,7 @@ enum DcdcOperationMode
|
||||
*
|
||||
* Allows to determine the current control state (off, CC, CV and MPPT)
|
||||
*/
|
||||
enum DcdcControlState
|
||||
enum dcdc_control_state
|
||||
{
|
||||
DCDC_CONTROL_OFF, ///< DC/DC switched off (low input power or actively disabled)
|
||||
DCDC_CONTROL_MPPT, ///< Maximum Power Point Tracking
|
||||
@@ -81,69 +85,22 @@ enum DcdcControlState
|
||||
};
|
||||
|
||||
/**
|
||||
* DC/DC class
|
||||
* DC/DC converter
|
||||
*
|
||||
* Contains all data belonging to the DC/DC sub-component of the PCB, incl.
|
||||
* actual measurements and calibration parameters.
|
||||
*/
|
||||
class Dcdc
|
||||
struct dcdc
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Initialize DC/DC and DC/DC port structs
|
||||
*
|
||||
* See http://libre.solar/docs/dcdc_control for detailed information
|
||||
*
|
||||
* @param high High voltage bus (e.g. solar input for MPPT buck)
|
||||
* @param low Low voltage bus (e.g. battery output for MPPT buck)
|
||||
* @param mode Operation mode (buck, boost or nanogrid)
|
||||
*/
|
||||
Dcdc(DcBus *high, DcBus *low, DcdcOperationMode mode);
|
||||
const struct device *half_bridge; ///< Half-bridge device generating the PWM signal
|
||||
|
||||
/**
|
||||
* Check for valid start conditions of the DC/DC converter
|
||||
*
|
||||
* @returns operation mode that is valid for starting up
|
||||
*/
|
||||
DcdcOperationMode check_start_conditions();
|
||||
|
||||
/**
|
||||
* Main control function for the DC/DC converter
|
||||
*
|
||||
* If DC/DC is off, this function checks start conditions and starts conversion if possible.
|
||||
*/
|
||||
void control();
|
||||
|
||||
/**
|
||||
* Test mode for DC/DC, ramping up/down to 50% duty cycle
|
||||
*/
|
||||
void test();
|
||||
|
||||
/**
|
||||
* Fast stop function (bypassing control loop)
|
||||
*
|
||||
* May be called from an ISR which detected overvoltage / overcurrent conditions.
|
||||
*
|
||||
* DC/DC will be restarted automatically from control function if condtions are valid.
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/**
|
||||
* Prevent overcharging of battery in case of shorted HS MOSFET
|
||||
*
|
||||
* This function switches the LS MOSFET continuously on to blow the battery input fuse. The
|
||||
* reason for self destruction should be logged and stored to EEPROM prior to calling this
|
||||
* function, as the charge controller power supply will be cut after the fuse is destroyed.
|
||||
*/
|
||||
void fuse_destruction();
|
||||
|
||||
DcdcOperationMode mode; ///< DC/DC mode (buck, boost or nanogrid)
|
||||
bool enable; ///< Can be used to disable the DC/DC power stage
|
||||
uint16_t state; ///< Control state (off / MPPT / CC / CV)
|
||||
enum dcdc_operation_mode mode; ///< DC/DC mode (buck, boost or nanogrid)
|
||||
bool enable; ///< Can be used to disable the DC/DC power stage
|
||||
uint16_t state; ///< Control state (off / MPPT / CC / CV)
|
||||
|
||||
// actual measurements
|
||||
DcBus *hvb; ///< Pointer to DC bus at high voltage side
|
||||
DcBus *lvb; ///< Pointer to DC bus at low voltage (inductor) side
|
||||
struct dc_bus *hvb; ///< Pointer to DC bus at high voltage side
|
||||
struct dc_bus *lvb; ///< Pointer to DC bus at low voltage (inductor) side
|
||||
float inductor_current; ///< Inductor current
|
||||
float power; ///< Low-side power
|
||||
float temp_mosfets; ///< MOSFET temperature measurement (if existing)
|
||||
@@ -155,62 +112,77 @@ public:
|
||||
int32_t power_good_timestamp; ///< Last time the DC/DC reached above minimum output power
|
||||
|
||||
// maximum allowed values
|
||||
float inductor_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)
|
||||
float inductor_current_max; ///< Maximum low-side (inductor) current
|
||||
float hs_voltage_max; ///< 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)
|
||||
|
||||
// calibration parameters
|
||||
uint32_t restart_interval; ///< Restart interval (s): When should we retry to start
|
||||
///< charging after low output power cut-off?
|
||||
|
||||
private:
|
||||
/**
|
||||
* MPPT perturb & observe control (buck mode)
|
||||
*
|
||||
* @returns calculated pwm duty cycle delta or 0 if the DC/DC should be stopped
|
||||
*/
|
||||
void perturb_observe_buck();
|
||||
|
||||
/**
|
||||
* MPPT perturb & observe control (boost mode)
|
||||
*
|
||||
* @returns calculated pwm duty cycle delta or 0 if the DC/DC should be stopped
|
||||
*/
|
||||
void perturb_observe_boost();
|
||||
|
||||
/**
|
||||
* If manual control of the reverse polarity MOSFET on the high-side is available, this
|
||||
* function enables it to use the high voltage side as output.
|
||||
*/
|
||||
void output_hvs_enable();
|
||||
|
||||
/**
|
||||
* Disable the reverse polarity MOSFET on the high-side (if option available)
|
||||
*/
|
||||
void output_hvs_disable();
|
||||
|
||||
/**
|
||||
* Check if the high-side MOSFET is shorted
|
||||
*/
|
||||
bool check_hs_mosfet_short();
|
||||
|
||||
/**
|
||||
* Check if we need to wait for voltages to settle before starting the DC/DC
|
||||
*
|
||||
* @param reset Reset the inhibit counter if set to true
|
||||
*
|
||||
* @returns true if we need to wait
|
||||
*/
|
||||
bool startup_inhibit(bool reset = false);
|
||||
};
|
||||
|
||||
#endif // __cplusplus
|
||||
/**
|
||||
* Initialize DC/DC struct and half bridge
|
||||
*
|
||||
* See http://libre.solar/docs/dcdc_control for detailed information
|
||||
*
|
||||
* @param dcdc DC/DC struct
|
||||
* @param half_bridge Half-bridge device generating the PWM signal
|
||||
* @param high High voltage bus (e.g. solar input for MPPT buck)
|
||||
* @param low Low voltage bus (e.g. battery output for MPPT buck)
|
||||
* @param mode Operation mode (buck, boost or nanogrid)
|
||||
*/
|
||||
void dcdc_init(struct dcdc *dcdc, const struct device *half_bridge, struct dc_bus *high,
|
||||
struct dc_bus *low, enum dcdc_operation_mode mode);
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/**
|
||||
* Check for valid start conditions of the DC/DC converter
|
||||
*
|
||||
* @param dcdc DC/DC struct
|
||||
*
|
||||
* @returns operation mode that is valid for starting up
|
||||
*/
|
||||
enum dcdc_operation_mode dcdc_check_start_conditions(struct dcdc *dcdc);
|
||||
|
||||
/**
|
||||
* Main control function for the DC/DC converter
|
||||
*
|
||||
* If DC/DC is off, this function checks start conditions and starts conversion if possible.
|
||||
*
|
||||
* @param dcdc DC/DC struct
|
||||
*/
|
||||
void dcdc_control(struct dcdc *dcdc);
|
||||
|
||||
/**
|
||||
* Test mode for DC/DC, ramping up/down to 50% duty cycle
|
||||
*
|
||||
* @param dcdc DC/DC struct
|
||||
*/
|
||||
void dcdc_test(struct dcdc *dcdc);
|
||||
|
||||
/**
|
||||
* Fast stop function (bypassing control loop)
|
||||
*
|
||||
* May be called from an ISR which detected overvoltage / overcurrent conditions.
|
||||
*
|
||||
* DC/DC will be restarted automatically from control function if condtions are valid.
|
||||
*
|
||||
* @param dcdc DC/DC struct
|
||||
*/
|
||||
void dcdc_stop(struct dcdc *dcdc);
|
||||
|
||||
/**
|
||||
* Prevent overcharging of battery in case of shorted HS MOSFET
|
||||
*
|
||||
* This function switches the LS MOSFET continuously on to blow the battery input fuse. The
|
||||
* reason for self destruction should be logged and stored to EEPROM prior to calling this
|
||||
* function, as the charge controller power supply will be cut after the fuse is destroyed.
|
||||
*
|
||||
* @param dcdc DC/DC struct
|
||||
*/
|
||||
void dcdc_fuse_destruction(struct dcdc *dcdc);
|
||||
|
||||
#ifdef CONFIG_CUSTOM_DCDC_CONTROLLER
|
||||
|
||||
|
||||
+56
-48
@@ -6,6 +6,11 @@
|
||||
|
||||
#include "device_status.h"
|
||||
|
||||
#include "bat_charger.h"
|
||||
#include "dcdc.h"
|
||||
#include "load.h"
|
||||
#include "power_port.h"
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include <math.h> // for fabs function
|
||||
@@ -16,7 +21,7 @@
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// must be called exactly once per second, otherwise energy calculation gets wrong
|
||||
void DeviceStatus::update_energy()
|
||||
void device_status_update_energy(struct device_status *ds)
|
||||
{
|
||||
// static variables so that it is not reset for each function call
|
||||
static int seconds_zero_solar = 0;
|
||||
@@ -35,13 +40,13 @@ void DeviceStatus::update_energy()
|
||||
static bool first_call = true;
|
||||
if (first_call) {
|
||||
// initialize values with values we got from EEPROM
|
||||
solar_in_total_Wh_prev = solar_in_total_Wh;
|
||||
load_out_total_Wh_prev = load_out_total_Wh;
|
||||
bat_chg_total_Wh_prev = bat_chg_total_Wh;
|
||||
bat_dis_total_Wh_prev = bat_dis_total_Wh;
|
||||
solar_in_total_Wh_prev = ds->solar_in_total_Wh;
|
||||
load_out_total_Wh_prev = ds->load_out_total_Wh;
|
||||
bat_chg_total_Wh_prev = ds->bat_chg_total_Wh;
|
||||
bat_dis_total_Wh_prev = ds->bat_dis_total_Wh;
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
grid_import_total_Wh_prev = grid_import_total_Wh;
|
||||
grid_export_total_Wh_prev = grid_export_total_Wh;
|
||||
grid_import_total_Wh_prev = ds->grid_import_total_Wh;
|
||||
grid_export_total_Wh_prev = ds->grid_export_total_Wh;
|
||||
#endif
|
||||
first_call = false;
|
||||
}
|
||||
@@ -58,14 +63,14 @@ void DeviceStatus::update_energy()
|
||||
// solar voltage > battery voltage after 5 hours of night time means sunrise in the morning
|
||||
// --> reset daily energy counters
|
||||
if (seconds_zero_solar > 60 * 60 * 5) {
|
||||
day_counter++;
|
||||
solar_in_total_Wh_prev = solar_in_total_Wh;
|
||||
load_out_total_Wh_prev = load_out_total_Wh;
|
||||
bat_chg_total_Wh_prev = bat_chg_total_Wh;
|
||||
bat_dis_total_Wh_prev = bat_dis_total_Wh;
|
||||
ds->day_counter++;
|
||||
solar_in_total_Wh_prev = ds->solar_in_total_Wh;
|
||||
load_out_total_Wh_prev = ds->load_out_total_Wh;
|
||||
bat_chg_total_Wh_prev = ds->bat_chg_total_Wh;
|
||||
bat_dis_total_Wh_prev = ds->bat_dis_total_Wh;
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
grid_import_total_Wh_prev = grid_import_total_Wh;
|
||||
grid_export_total_Wh_prev = grid_export_total_Wh;
|
||||
grid_import_total_Wh_prev = ds->grid_import_total_Wh;
|
||||
grid_export_total_Wh_prev = ds->grid_export_total_Wh;
|
||||
#endif
|
||||
solar_terminal.neg_energy_Wh = 0.0;
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
@@ -82,83 +87,86 @@ void DeviceStatus::update_energy()
|
||||
}
|
||||
#endif
|
||||
|
||||
bat_chg_total_Wh =
|
||||
ds->bat_chg_total_Wh =
|
||||
bat_chg_total_Wh_prev + (bat_terminal.pos_energy_Wh > 0 ? bat_terminal.pos_energy_Wh : 0);
|
||||
bat_dis_total_Wh =
|
||||
ds->bat_dis_total_Wh =
|
||||
bat_dis_total_Wh_prev + (bat_terminal.neg_energy_Wh > 0 ? bat_terminal.neg_energy_Wh : 0);
|
||||
|
||||
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
|
||||
solar_in_total_Wh = solar_in_total_Wh_prev
|
||||
+ (solar_terminal.neg_energy_Wh > 0 ? solar_terminal.neg_energy_Wh : 0);
|
||||
ds->solar_in_total_Wh = solar_in_total_Wh_prev
|
||||
+ (solar_terminal.neg_energy_Wh > 0 ? solar_terminal.neg_energy_Wh : 0);
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load_out_total_Wh = load_out_total_Wh_prev + (load.pos_energy_Wh > 0 ? load.pos_energy_Wh : 0);
|
||||
ds->load_out_total_Wh =
|
||||
load_out_total_Wh_prev + (load.pos_energy_Wh > 0 ? load.pos_energy_Wh : 0);
|
||||
#endif
|
||||
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
grid_import_total_Wh = grid_import_total_Wh_prev
|
||||
+ (grid_terminal.neg_energy_Wh > 0 ? grid_terminal.neg_energy_Wh : 0);
|
||||
grid_export_total_Wh = grid_export_total_Wh_prev
|
||||
+ (grid_terminal.pos_energy_Wh > 0 ? grid_terminal.pos_energy_Wh : 0);
|
||||
ds->grid_import_total_Wh =
|
||||
grid_import_total_Wh_prev
|
||||
+ (grid_terminal.neg_energy_Wh > 0 ? grid_terminal.neg_energy_Wh : 0);
|
||||
ds->grid_export_total_Wh =
|
||||
grid_export_total_Wh_prev
|
||||
+ (grid_terminal.pos_energy_Wh > 0 ? grid_terminal.pos_energy_Wh : 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
void DeviceStatus::update_min_max_values()
|
||||
void device_status_update_min_max_values(struct device_status *ds)
|
||||
{
|
||||
if (bat_terminal.bus->voltage > battery_voltage_max) {
|
||||
battery_voltage_max = bat_terminal.bus->voltage;
|
||||
if (bat_terminal.bus->voltage > ds->battery_voltage_max) {
|
||||
ds->battery_voltage_max = bat_terminal.bus->voltage;
|
||||
}
|
||||
|
||||
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR
|
||||
if (solar_terminal.bus->voltage > solar_voltage_max) {
|
||||
solar_voltage_max = solar_terminal.bus->voltage;
|
||||
if (solar_terminal.bus->voltage > ds->solar_voltage_max) {
|
||||
ds->solar_voltage_max = solar_terminal.bus->voltage;
|
||||
}
|
||||
#elif CONFIG_PWM_TERMINAL_SOLAR
|
||||
if (pwm_switch.ext_voltage > solar_voltage_max) {
|
||||
solar_voltage_max = pwm_switch.ext_voltage;
|
||||
if (pwm_switch.ext_voltage > ds->solar_voltage_max) {
|
||||
ds->solar_voltage_max = pwm_switch.ext_voltage;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
if (dcdc.inductor_current > dcdc_current_max) {
|
||||
dcdc_current_max = dcdc.inductor_current;
|
||||
if (dcdc.inductor_current > ds->dcdc_current_max) {
|
||||
ds->dcdc_current_max = dcdc.inductor_current;
|
||||
}
|
||||
|
||||
if (dcdc.temp_mosfets > mosfet_temp_max) {
|
||||
mosfet_temp_max = dcdc.temp_mosfets;
|
||||
if (dcdc.temp_mosfets > ds->mosfet_temp_max) {
|
||||
ds->mosfet_temp_max = dcdc.temp_mosfets;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
if (load.current > load_current_max) {
|
||||
load_current_max = load.current;
|
||||
if (load.current > ds->load_current_max) {
|
||||
ds->load_current_max = load.current;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
|
||||
if (-solar_terminal.power > solar_power_max_day) {
|
||||
solar_power_max_day = -solar_terminal.power;
|
||||
if (solar_power_max_day > solar_power_max_total) {
|
||||
solar_power_max_total = solar_power_max_day;
|
||||
if (-solar_terminal.power > ds->solar_power_max_day) {
|
||||
ds->solar_power_max_day = -solar_terminal.power;
|
||||
if (ds->solar_power_max_day > ds->solar_power_max_total) {
|
||||
ds->solar_power_max_total = ds->solar_power_max_day;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
if (load.power > load_power_max_day) {
|
||||
load_power_max_day = load.power;
|
||||
if (load_power_max_day > load_power_max_total) {
|
||||
load_power_max_total = load_power_max_day;
|
||||
if (load.power > ds->load_power_max_day) {
|
||||
ds->load_power_max_day = load.power;
|
||||
if (ds->load_power_max_day > ds->load_power_max_total) {
|
||||
ds->load_power_max_total = ds->load_power_max_day;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (charger.bat_temperature > static_cast<float>(bat_temp_max)) {
|
||||
bat_temp_max = charger.bat_temperature;
|
||||
if (charger.bat_temperature > static_cast<float>(ds->bat_temp_max)) {
|
||||
ds->bat_temp_max = charger.bat_temperature;
|
||||
}
|
||||
|
||||
if (internal_temp > static_cast<float>(int_temp_max)) {
|
||||
int_temp_max = internal_temp;
|
||||
if (ds->internal_temp > static_cast<float>(ds->int_temp_max)) {
|
||||
ds->int_temp_max = ds->internal_temp;
|
||||
}
|
||||
}
|
||||
|
||||
+63
-42
@@ -13,13 +13,13 @@
|
||||
* Device-level data storage and functions (like max/min values, error flags, etc.)
|
||||
*/
|
||||
|
||||
#include "bat_charger.h"
|
||||
#include "dcdc.h"
|
||||
#include "load.h"
|
||||
#include "power_port.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** Error Flags
|
||||
*
|
||||
* When adding new flags, please make sure to use only up to 32 errors
|
||||
@@ -110,17 +110,8 @@ enum ErrorFlag
|
||||
*
|
||||
* Stores error counters and some maximum ever measured values to EEPROM
|
||||
*/
|
||||
class DeviceStatus
|
||||
struct device_status
|
||||
{
|
||||
public:
|
||||
/** Updates the total energy counters for solar, battery and load bus
|
||||
*/
|
||||
void update_energy();
|
||||
|
||||
/** Updates the logged min/max values for voltages, power, temperatures etc.
|
||||
*/
|
||||
void update_min_max_values();
|
||||
|
||||
// total energy
|
||||
uint32_t bat_chg_total_Wh;
|
||||
uint32_t bat_dis_total_Wh;
|
||||
@@ -149,34 +140,64 @@ public:
|
||||
// instantaneous device-level data
|
||||
uint32_t error_flags; ///< Currently detected errors
|
||||
float internal_temp; ///< Internal temperature (measured in MCU)
|
||||
|
||||
/**
|
||||
* @brief sets one or more error flags in device state
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
*/
|
||||
inline void set_error(uint32_t e)
|
||||
{
|
||||
error_flags |= e;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief clears one or more error flags in device state
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
*/
|
||||
void clear_error(uint32_t e)
|
||||
{
|
||||
error_flags &= ~e;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief queries one or more error flags in device state
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
* @return true if any of the error flags given in e are set in device state
|
||||
*/
|
||||
bool has_error(uint32_t e)
|
||||
{
|
||||
return (error_flags & e) != 0;
|
||||
}
|
||||
};
|
||||
|
||||
/** Global device status */
|
||||
extern struct device_status dev_stat;
|
||||
|
||||
/**
|
||||
* Updates the total energy counters for solar, battery and load bus
|
||||
*
|
||||
* Must be called exactly once per second, otherwise energy calculation gets wrong.
|
||||
*
|
||||
* @param ds Device status
|
||||
*/
|
||||
void device_status_update_energy(struct device_status *ds);
|
||||
|
||||
/**
|
||||
* Updates the logged min/max values for voltages, power, temperatures etc.
|
||||
*
|
||||
* @param ds Device status
|
||||
*/
|
||||
void device_status_update_min_max_values(struct device_status *ds);
|
||||
|
||||
/**
|
||||
* Sets one or more error flags in device state
|
||||
*
|
||||
* @param ds Device status
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
*/
|
||||
static inline void device_status_set_error(struct device_status *ds, uint32_t e)
|
||||
{
|
||||
ds->error_flags |= e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears one or more error flags in device state
|
||||
*
|
||||
* @param ds Device status
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
*/
|
||||
static inline void device_status_clear_error(struct device_status *ds, uint32_t e)
|
||||
{
|
||||
ds->error_flags &= ~e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries one or more error flags in device state
|
||||
*
|
||||
* @param ds Device status
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
*
|
||||
* @return true if any of the error flags given in e are set in device state
|
||||
*/
|
||||
static inline bool device_status_has_error(const struct device_status *ds, uint32_t e)
|
||||
{
|
||||
return (ds->error_flags & e) != 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DEVICE_STATUS_H */
|
||||
|
||||
@@ -1,403 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT half_bridge
|
||||
|
||||
#include "half_bridge.h"
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "board.h"
|
||||
#include "mcu.h"
|
||||
|
||||
#ifdef CONFIG_SOC_FAMILY_STM32
|
||||
#include <soc.h>
|
||||
#include <stm32_ll_bus.h>
|
||||
#include <zephyr/drivers/pinctrl.h>
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
|
||||
// Get address of used timer from board dts
|
||||
#define TIMER_ADDR DT_REG_ADDR(DT_PARENT(DT_DRV_INST(0)))
|
||||
|
||||
static uint16_t tim_ccr_min; // capture/compare register min/max
|
||||
static uint16_t tim_ccr_max;
|
||||
static uint16_t tim_dt_clocks = 0;
|
||||
|
||||
static uint16_t clamp_ccr(uint16_t ccr_target)
|
||||
{
|
||||
// protection against wrong settings which could destroy the hardware
|
||||
if (ccr_target <= tim_ccr_min) {
|
||||
return tim_ccr_min;
|
||||
}
|
||||
else if (ccr_target >= tim_ccr_max) {
|
||||
return tim_ccr_max;
|
||||
}
|
||||
else {
|
||||
return ccr_target;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SOC_FAMILY_STM32
|
||||
|
||||
#define PINCTRL_NODE DT_NODELABEL(halfbridge)
|
||||
|
||||
PINCTRL_DT_DEFINE(PINCTRL_NODE);
|
||||
|
||||
static const struct pinctrl_dev_config *pincfg = PINCTRL_DT_DEV_CONFIG_GET(PINCTRL_NODE);
|
||||
|
||||
#if TIMER_ADDR == TIM3_BASE
|
||||
|
||||
static void tim_init_registers(int freq_kHz)
|
||||
{
|
||||
pinctrl_apply_state(pincfg, PINCTRL_STATE_DEFAULT);
|
||||
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
|
||||
|
||||
// No prescaler --> timer frequency == SystemClock
|
||||
TIM3->PSC = 0;
|
||||
|
||||
// Capture/Compare Mode Register 1
|
||||
// OCxM = 110: Select PWM mode 1 on OCx
|
||||
// OCxPE = 1: Enable preload register on OCx (reset value)
|
||||
TIM3->CCMR2 |= TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3PE;
|
||||
TIM3->CCMR2 |= TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4PE;
|
||||
|
||||
// Capture/Compare Enable Register
|
||||
// CCxP: Active high polarity on OCx (default = 0)
|
||||
TIM3->CCER &= ~(TIM_CCER_CC3P); // PB0 / TIM3_CH3: high-side
|
||||
TIM3->CCER |= TIM_CCER_CC4P; // PB1 / TIM3_CH4: low-side
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CMS = 01: Select center-aligned mode 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM3->CR1 |= TIM_CR1_CMS_0 | TIM_CR1_CEN;
|
||||
|
||||
// Force update generation (UG = 1)
|
||||
TIM3->EGR |= TIM_EGR_UG;
|
||||
|
||||
// Auto Reload Register
|
||||
// center-aligned mode counts up and down in each period, so we need half the clocks as
|
||||
// resolution for the given frequency.
|
||||
TIM3->ARR = SystemCoreClock / (freq_kHz * 1000) / 2;
|
||||
}
|
||||
|
||||
void half_bridge_start()
|
||||
{
|
||||
if (TIM3->CCR3 != 0U) {
|
||||
// Capture/Compare Enable Register
|
||||
// CCxE = 1: Enable the output on OCx
|
||||
// CCxP = 0: Active high polarity on OCx (default)
|
||||
TIM3->CCER |= TIM_CCER_CC3E;
|
||||
TIM3->CCER |= TIM_CCER_CC4E;
|
||||
}
|
||||
}
|
||||
|
||||
void half_bridge_stop()
|
||||
{
|
||||
TIM3->CCER &= ~(TIM_CCER_CC3E);
|
||||
TIM3->CCER &= ~(TIM_CCER_CC4E);
|
||||
}
|
||||
|
||||
uint16_t half_bridge_get_arr()
|
||||
{
|
||||
return TIM3->ARR;
|
||||
}
|
||||
|
||||
uint16_t half_bridge_get_ccr()
|
||||
{
|
||||
return TIM3->CCR3;
|
||||
}
|
||||
|
||||
void half_bridge_set_ccr(uint16_t ccr)
|
||||
{
|
||||
uint16_t ccr_clamped = clamp_ccr(ccr);
|
||||
|
||||
TIM3->CCR3 = ccr_clamped; // high-side
|
||||
TIM3->CCR4 = ccr_clamped + tim_dt_clocks; // low-side
|
||||
}
|
||||
|
||||
bool half_bridge_enabled()
|
||||
{
|
||||
return TIM3->CCER & TIM_CCER_CC3E;
|
||||
}
|
||||
|
||||
#elif TIMER_ADDR == TIM1_BASE
|
||||
|
||||
static void tim_init_registers(int freq_kHz)
|
||||
{
|
||||
pinctrl_apply_state(pincfg, PINCTRL_STATE_DEFAULT);
|
||||
|
||||
#ifdef LL_APB2_GRP1_PERIPH_TIM1
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
|
||||
#else
|
||||
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_TIM1);
|
||||
#endif
|
||||
|
||||
// No prescaler --> timer frequency == SystemClock
|
||||
TIM1->PSC = 0;
|
||||
|
||||
// Capture/Compare Mode Register 1
|
||||
// OC1M = 110: Select PWM mode 1 on OC1
|
||||
// OC1PE = 1: Enable preload register on OC1 (reset value)
|
||||
TIM1->CCMR1 |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
|
||||
|
||||
// Capture/Compare Enable Register
|
||||
// CC1E = 1: Enable the output on OC1
|
||||
// CC1P = 0: Active high polarity on OC1 (default)
|
||||
// CC1NE = 1: Enable the output on OC1N
|
||||
// CC1NP = 0: Active high polarity on OC1N (default)
|
||||
TIM1->CCER |= TIM_CCER_CC1E | TIM_CCER_CC1NE;
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CMS = 00: Select edge-aligned mode
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM1->CR1 |= TIM_CR1_CEN;
|
||||
|
||||
#ifdef CONFIG_SOC_SERIES_STM32G4X
|
||||
// Boards with STM32G4 MCU use ADC2 for synchronized ADC sampling. We use OC6
|
||||
// for triggering, but but don't configure any pin for PWM mode
|
||||
TIM1->CCMR3 |= TIM_CCMR3_OC6M_2 | TIM_CCMR3_OC6M_1 | TIM_CCMR3_OC6PE;
|
||||
TIM1->CCER |= TIM_CCER_CC6E;
|
||||
// Trigger ADC via TIM1_TRGO2 on OC6ref falling edge signal event
|
||||
TIM1->CR2 |= TIM_CR2_MMS2_3 | TIM_CR2_MMS2_2 | TIM_CR2_MMS2_0;
|
||||
#endif
|
||||
|
||||
// Force update generation (UG = 1)
|
||||
TIM1->EGR |= TIM_EGR_UG;
|
||||
|
||||
// Auto Reload Register
|
||||
// edge-aligned mode possible with TIM1 to increase resolution (no division by 2 necessary)
|
||||
TIM1->ARR = SystemCoreClock / (freq_kHz * 1000);
|
||||
|
||||
// Break and Dead-Time Register
|
||||
// DTG[7:0]: Dead-time generator setup
|
||||
TIM1->BDTR |= (tim_dt_clocks & (uint32_t)0x7F); // ensure that only the last 7 bits are changed
|
||||
|
||||
// Lock Break and Dead-Time Register
|
||||
// TODO: does not work properly... maybe HW bug?
|
||||
TIM1->BDTR |= TIM_BDTR_LOCK_1 | TIM_BDTR_LOCK_0;
|
||||
}
|
||||
|
||||
void half_bridge_start()
|
||||
{
|
||||
if (TIM1->CCR1 != 0U) {
|
||||
// Break and Dead-Time Register
|
||||
// MOE = 1: Main output enable
|
||||
TIM1->BDTR |= TIM_BDTR_MOE;
|
||||
}
|
||||
}
|
||||
|
||||
void half_bridge_stop()
|
||||
{
|
||||
// Break and Dead-Time Register
|
||||
// MOE = 1: Main output enable
|
||||
TIM1->BDTR &= ~(TIM_BDTR_MOE);
|
||||
}
|
||||
|
||||
uint16_t half_bridge_get_arr()
|
||||
{
|
||||
return TIM1->ARR;
|
||||
}
|
||||
|
||||
uint16_t half_bridge_get_ccr()
|
||||
{
|
||||
return TIM1->CCR1;
|
||||
}
|
||||
|
||||
void half_bridge_set_ccr(uint16_t ccr)
|
||||
{
|
||||
TIM1->CCR1 = clamp_ccr(ccr);
|
||||
|
||||
#ifdef CONFIG_SOC_SERIES_STM32G4X
|
||||
// Trigger ADC for current measurement in the middle of the cycle.
|
||||
TIM1->CCR6 = TIM1->CCR1 / 2;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool half_bridge_enabled()
|
||||
{
|
||||
return TIM1->BDTR & TIM_BDTR_MOE;
|
||||
}
|
||||
|
||||
#elif TIMER_ADDR == HRTIM1_BASE
|
||||
|
||||
/*
|
||||
* Below initial HRTIM implementation only works for channel A and it does not make use of the
|
||||
* possible higher resolution yet.
|
||||
*/
|
||||
|
||||
#include <stm32_ll_bus.h>
|
||||
#include <stm32_ll_system.h>
|
||||
|
||||
static void tim_init_registers(int freq_kHz)
|
||||
{
|
||||
pinctrl_apply_state(pincfg, PINCTRL_STATE_DEFAULT);
|
||||
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_HRTIM1);
|
||||
|
||||
// Enable periodic calibration of delay-locked loop (DLL) with lowest calibration period
|
||||
HRTIM1->sCommonRegs.DLLCR |= HRTIM_DLLCR_CALEN | HRTIM_DLLCR_CALRTE_0 | HRTIM_DLLCR_CALRTE_1;
|
||||
|
||||
// Wait for calibration to finish
|
||||
while ((HRTIM1_COMMON->ISR & HRTIM_ISR_DLLRDY) == 0) {
|
||||
}
|
||||
|
||||
// Prescaler 32 --> SystemClock (same as normal timers like TIM1)
|
||||
HRTIM1_TIMA->TIMxCR |= (5U << HRTIM_TIMCR_CK_PSC_Pos);
|
||||
|
||||
// Prescaler 8 / 2^3 --> same frequency as HRTIM
|
||||
HRTIM1_TIMA->DTxR |= (3U << HRTIM_DTR_DTPRSC_Pos);
|
||||
|
||||
// Continuous mode operation
|
||||
HRTIM1_TIMA->TIMxCR |= HRTIM_TIMCR_CONT;
|
||||
|
||||
// Enable preloading with timer reset update trigger
|
||||
// HRTIM1_TIMA->TIMxCR |= HRTIM_TIMCR_PREEN | HRTIM_TIMCR_TRSTU;
|
||||
|
||||
// Enable preloading with timer E update trigger
|
||||
// HRTIM1_TIMA->TIMxCR |= HRTIM_TIMCR_PREEN | HRTIM_TIMCR_TEU;
|
||||
|
||||
// Timer period register
|
||||
HRTIM1_TIMA->PERxR = SystemCoreClock / (freq_kHz * 1000) + 1;
|
||||
|
||||
HRTIM1_TIMA->SETx1R = HRTIM_SET1R_PER;
|
||||
HRTIM1_TIMA->RSTx1R = HRTIM_SET1R_CMP1;
|
||||
|
||||
HRTIM1_TIMA->OUTxR = HRTIM_OUTR_DTEN;
|
||||
|
||||
// Set deadtime values and lock deadtime signs
|
||||
HRTIM1_TIMA->DTxR |=
|
||||
HRTIM_DTR_DTFSLK | (tim_dt_clocks << 16U) | HRTIM_DTR_DTRSLK | tim_dt_clocks;
|
||||
|
||||
// activate trigger for ADC
|
||||
HRTIM1_COMMON->CR1 = HRTIM_CR1_ADC1USRC_0; // ADC trigger update: Timer A
|
||||
HRTIM1_COMMON->ADC1R = HRTIM_ADC1R_AD1TAC3; // ADC trigger event: Timer A compare 3
|
||||
|
||||
// Start timer A
|
||||
HRTIM1->sMasterRegs.MCR = HRTIM_MCR_TACEN;
|
||||
}
|
||||
|
||||
void half_bridge_start()
|
||||
{
|
||||
HRTIM1->sCommonRegs.OENR = HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN;
|
||||
}
|
||||
|
||||
void half_bridge_stop()
|
||||
{
|
||||
HRTIM1->sCommonRegs.ODISR = HRTIM_ODISR_TA1ODIS | HRTIM_ODISR_TA2ODIS;
|
||||
}
|
||||
|
||||
uint16_t half_bridge_get_arr()
|
||||
{
|
||||
return HRTIM1_TIMA->PERxR;
|
||||
}
|
||||
|
||||
uint16_t half_bridge_get_ccr()
|
||||
{
|
||||
return HRTIM1_TIMA->CMP1xR;
|
||||
}
|
||||
|
||||
void half_bridge_set_ccr(uint16_t ccr)
|
||||
{
|
||||
HRTIM1_TIMA->CMP1xR = clamp_ccr(ccr);
|
||||
|
||||
// Trigger ADC for current measurement in the middle of the cycle.
|
||||
// A negative offset of 80 clocks was found to improve current measurement accuracy and
|
||||
// compensate the ADC delay (no risk of underflow as CCR is around 214 even at 0.1% duty).
|
||||
HRTIM1_TIMA->CMP3xR = HRTIM1_TIMA->CMP1xR / 2 - 80;
|
||||
}
|
||||
|
||||
bool half_bridge_enabled()
|
||||
{
|
||||
return (HRTIM1->sCommonRegs.OENR & (HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN)) > 0;
|
||||
}
|
||||
|
||||
#endif // HRTIM1
|
||||
|
||||
#else // UNIT_TEST
|
||||
|
||||
const uint32_t SystemCoreClock = 24000000;
|
||||
|
||||
// dummy registers
|
||||
uint32_t tim_ccr = 0;
|
||||
uint32_t tim_arr = 0;
|
||||
bool pwm_enabled = false;
|
||||
|
||||
static void tim_init_registers(int freq_kHz)
|
||||
{
|
||||
// assuming edge-aligned PWM like with TIM1
|
||||
tim_arr = SystemCoreClock / (freq_kHz * 1000);
|
||||
}
|
||||
|
||||
void half_bridge_start()
|
||||
{
|
||||
pwm_enabled = true;
|
||||
}
|
||||
|
||||
void half_bridge_stop()
|
||||
{
|
||||
pwm_enabled = false;
|
||||
}
|
||||
|
||||
uint16_t half_bridge_get_arr()
|
||||
{
|
||||
return tim_arr;
|
||||
}
|
||||
|
||||
uint16_t half_bridge_get_ccr()
|
||||
{
|
||||
return tim_ccr;
|
||||
}
|
||||
|
||||
void half_bridge_set_ccr(uint16_t ccr)
|
||||
{
|
||||
tim_ccr = clamp_ccr(ccr); // high-side
|
||||
}
|
||||
|
||||
bool half_bridge_enabled()
|
||||
{
|
||||
return pwm_enabled;
|
||||
}
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
|
||||
static void tim_calculate_dt_clocks(int deadtime_ns)
|
||||
{
|
||||
// (clocks per ms * deadtime in ns) / 1000 == (clocks per ms * deadtime in ms)
|
||||
// although the C operator precedence does the "right thing" to allow deadtime_ns < 1000 to
|
||||
// be handled nicely, parentheses make this more explicit
|
||||
tim_dt_clocks = ((SystemCoreClock / (1000000)) * deadtime_ns) / 1000;
|
||||
}
|
||||
|
||||
void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_duty)
|
||||
{
|
||||
tim_calculate_dt_clocks(deadtime_ns);
|
||||
|
||||
tim_init_registers(freq_kHz);
|
||||
|
||||
tim_ccr_min = min_duty * half_bridge_get_arr();
|
||||
tim_ccr_max = max_duty * half_bridge_get_arr();
|
||||
|
||||
half_bridge_set_duty_cycle(max_duty); // init with allowed value
|
||||
}
|
||||
|
||||
void half_bridge_set_duty_cycle(float duty)
|
||||
{
|
||||
if (duty >= 0.0f && duty <= 1.0f) {
|
||||
half_bridge_set_ccr(half_bridge_get_arr() * duty);
|
||||
}
|
||||
}
|
||||
|
||||
float half_bridge_get_duty_cycle()
|
||||
{
|
||||
return (float)(half_bridge_get_ccr()) / half_bridge_get_arr();
|
||||
}
|
||||
|
||||
#endif // BOARD_HAS_DCDC
|
||||
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef HALF_BRIDGE_H_
|
||||
#define HALF_BRIDGE_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief PWM timer functions for half bridge of DC/DC converter
|
||||
*
|
||||
* Generates the synchronous PWM signal for the half bridge in the DC/DC converter. Depending on
|
||||
* the MCU, either the advanced timer TIM1 or the basic timer TIM3 is used.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Initiatializes the registers to generate the PWM signal and sets duty
|
||||
* cycle limits
|
||||
*
|
||||
* @param freq_kHz Switching frequency in kHz
|
||||
* @param deadtime_ns Deadtime in ns between switching the two FETs on/off
|
||||
* @param min_duty Minimum duty cycle (e.g. 0.5 for limiting input voltage)
|
||||
* @param max_duty Maximum duty cycle (e.g. 0.97 for charge pump)
|
||||
*/
|
||||
void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_duty);
|
||||
|
||||
/**
|
||||
* Set raw timer capture/compare register
|
||||
*
|
||||
* This function allows to change the PWM with minimum step size.
|
||||
*
|
||||
* @param ccr Timer CCR value (between 0 and ARR)
|
||||
*/
|
||||
void half_bridge_set_ccr(uint16_t ccr);
|
||||
|
||||
/**
|
||||
* Get raw timer capture/compare register
|
||||
*
|
||||
* @returns Timer CCR value (between 0 and ARR)
|
||||
*/
|
||||
uint16_t half_bridge_get_ccr();
|
||||
|
||||
/**
|
||||
* Get raw timer auto-reload register
|
||||
*
|
||||
* @returns Timer ARR value
|
||||
*/
|
||||
uint16_t half_bridge_get_arr();
|
||||
|
||||
/**
|
||||
* Set the duty cycle of the PWM signal
|
||||
*
|
||||
* @param duty Duty cycle between 0.0 and 1.0
|
||||
*/
|
||||
void half_bridge_set_duty_cycle(float duty);
|
||||
|
||||
/**
|
||||
* Read the currently set duty cycle
|
||||
*
|
||||
* @returns Duty cycle between 0.0 and 1.0
|
||||
*/
|
||||
float half_bridge_get_duty_cycle();
|
||||
|
||||
/**
|
||||
* Start the PWM generation
|
||||
*
|
||||
* Important: Valid duty cycle / CCR has to be set before starting
|
||||
*/
|
||||
void half_bridge_start();
|
||||
|
||||
/**
|
||||
* Stop the PWM generation
|
||||
*/
|
||||
void half_bridge_stop();
|
||||
|
||||
/**
|
||||
* Get status of the PWM output
|
||||
*
|
||||
* @returns True if PWM output enabled
|
||||
*/
|
||||
bool half_bridge_enabled();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HALF_BRIDGE_H_ */
|
||||
+5
-61
@@ -6,68 +6,11 @@
|
||||
|
||||
#include "hardware.h"
|
||||
|
||||
#include "half_bridge.h"
|
||||
#include "helper.h"
|
||||
#include "leds.h"
|
||||
#include "load.h"
|
||||
#include "mcu.h"
|
||||
#include "setup.h"
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), boot0))
|
||||
#define BOOT0_NODE DT_CHILD(DT_PATH(outputs), boot0)
|
||||
#endif
|
||||
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/sys/reboot.h>
|
||||
|
||||
LOG_MODULE_REGISTER(hardware, CONFIG_HW_LOG_LEVEL);
|
||||
|
||||
void start_stm32_bootloader()
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), boot0))
|
||||
// pin is connected to BOOT0 via resistor and capacitor
|
||||
const struct gpio_dt_spec boot0 = GPIO_DT_SPEC_GET(BOOT0_NODE, gpios);
|
||||
if (device_is_ready(boot0.port)) {
|
||||
gpio_pin_configure_dt(&boot0, GPIO_OUTPUT_ACTIVE);
|
||||
k_sleep(K_MSEC(100)); // wait for capacitor at BOOT0 pin to charge up
|
||||
reset_device();
|
||||
}
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
if ((FLASH->CR & FLASH_CR_OPTLOCK) != 0U) {
|
||||
/* Authorizes the Option Byte register programming */
|
||||
FLASH->OPTKEYR = FLASH_OPTKEY1;
|
||||
FLASH->OPTKEYR = FLASH_OPTKEY2;
|
||||
}
|
||||
|
||||
// Set proper bits for booting the embedded bootloader (see table
|
||||
// 5 in section 2.6.1 in document RM0440)
|
||||
|
||||
// nBOOT0: nBOOT0 option bit (equivalent to the BOOT0 pin)
|
||||
// nSWBOOT0: 0: BOOT0 taken from the option bit nBOOT0
|
||||
// nSWBOOT0: 1: BOOT0 taken from PB8/BOOT0 pin
|
||||
// nBOOT1: Together with the BOOT0 pin, this bit selects boot mode
|
||||
// from the Flash main memory, SRAM1 or the System memory.
|
||||
|
||||
FLASH->OPTR &= ~(FLASH_OPTR_nSWBOOT0 | FLASH_OPTR_nBOOT0);
|
||||
FLASH->OPTR |= FLASH_OPTR_nBOOT1;
|
||||
|
||||
// Save the current registers in flash, to be reloaded at reset
|
||||
FLASH->CR |= FLASH_CR_OPTSTRT;
|
||||
k_sleep(K_MSEC(20));
|
||||
|
||||
// Reload the option registers from flash; should trigger a system
|
||||
// reset.
|
||||
FLASH->CR |= FLASH_CR_OBL_LAUNCH;
|
||||
|
||||
// If OBL_LAUNCH did not reset (it should), we'll force it by
|
||||
// first locking back the flash registers and going for a reboot.
|
||||
FLASH->CR |= FLASH_CR_OPTLOCK;
|
||||
reset_device();
|
||||
#endif
|
||||
}
|
||||
|
||||
void reset_device()
|
||||
{
|
||||
sys_reboot(SYS_REBOOT_COLD);
|
||||
@@ -86,8 +29,9 @@ void task_wdt_callback(int channel_id, void *user_data)
|
||||
#else
|
||||
|
||||
// dummy functions for unit tests
|
||||
void start_stm32_bootloader() {}
|
||||
void reset_device() {}
|
||||
void task_wdt_callback(int channel_id, void *user_data) {}
|
||||
void reset_device()
|
||||
{}
|
||||
void task_wdt_callback(int channel_id, void *user_data)
|
||||
{}
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
|
||||
+1
-9
@@ -10,17 +10,9 @@
|
||||
/** @file
|
||||
*
|
||||
* @brief
|
||||
* Hardware-specific functions like timers, watchdog, bootloader
|
||||
* Device reset and watchdog functions
|
||||
*/
|
||||
|
||||
/** DC/DC or PWM control loop (implemented in main.cpp)
|
||||
*/
|
||||
void system_control();
|
||||
|
||||
/** Reset device and start STM32 internal bootloader
|
||||
*/
|
||||
void start_stm32_bootloader();
|
||||
|
||||
/**
|
||||
* Reset device
|
||||
*/
|
||||
|
||||
+47
-17
@@ -15,23 +15,36 @@
|
||||
#include "helper.h"
|
||||
#include "leds.h"
|
||||
|
||||
#define LOAD_CURRENT_MAX DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max)
|
||||
#include <libresolar/drivers/power_switch.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
LOG_MODULE_REGISTER(load, CONFIG_LOAD_LOG_LEVEL);
|
||||
|
||||
#define LOAD_CURRENT_MAX DT_PROP(LOAD_SWITCH_NODE, current_max)
|
||||
|
||||
#define PCB_LS_VOLTAGE_MAX DT_PROP(DT_PATH(pcb), ls_voltage_max)
|
||||
#define PCB_MOSFETS_TJ_MAX DT_PROP(DT_PATH(pcb), mosfets_tj_max)
|
||||
#define PCB_MOSFETS_TAU_JA DT_PROP(DT_PATH(pcb), mosfets_tau_ja)
|
||||
#define PCB_INTERNAL_TREF_MAX DT_PROP(DT_PATH(pcb), internal_tref_max)
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
extern struct device_status dev_stat;
|
||||
|
||||
LoadOutput::LoadOutput(DcBus *dc_bus, void (*switch_fn)(bool), void (*init_fn)(),
|
||||
bool (*pgood_fn)())
|
||||
: PowerPort(dc_bus), switch_set(switch_fn), pgood_check(pgood_fn)
|
||||
LoadOutput::LoadOutput(struct dc_bus *dc_bus, const struct device *sw, int led_pos)
|
||||
: power_switch(sw), led(led_pos)
|
||||
{
|
||||
power_port_init(this, dc_bus, false);
|
||||
|
||||
state = LOAD_STATE_OFF;
|
||||
|
||||
// call driver initialization function
|
||||
init_fn();
|
||||
if (!device_is_ready(power_switch)) {
|
||||
LOG_ERR("Power switch %s not ready", power_switch->name);
|
||||
}
|
||||
else {
|
||||
// not supported by all switches, so the return value is ignored
|
||||
power_switch_set_callback(power_switch, POWER_SWITCH_EVENT_FAULT, short_circuit_callback,
|
||||
this);
|
||||
}
|
||||
|
||||
switch_set(false);
|
||||
junction_temperature = 25; // starting point: 25°C
|
||||
@@ -61,7 +74,7 @@ void LoadOutput::control()
|
||||
oc_timestamp = uptime();
|
||||
}
|
||||
|
||||
if (pgood_check != NULL && !pgood_check()) {
|
||||
if (power_switch_power_good(power_switch) == 0) {
|
||||
flags_set(&error_flags, ERR_LOAD_OVERCURRENT);
|
||||
oc_timestamp = uptime();
|
||||
}
|
||||
@@ -71,14 +84,16 @@ void LoadOutput::control()
|
||||
flags_set(&error_flags, ERR_LOAD_BUS_SRC_CURRENT);
|
||||
}
|
||||
|
||||
if (bus->voltage < bus->src_control_voltage(disconnect_voltage)) {
|
||||
if (bus->voltage < dc_bus_src_control_voltage(bus, disconnect_voltage)) {
|
||||
flags_set(&error_flags, ERR_LOAD_SHEDDING);
|
||||
lvd_timestamp = uptime();
|
||||
}
|
||||
|
||||
// long-term overvoltage (overvoltage transients are detected as an ADC alert and switch
|
||||
// off the solar input instead of the load output)
|
||||
if (bus->voltage > bus->series_voltage(overvoltage) || bus->voltage > PCB_LS_VOLTAGE_MAX) {
|
||||
if (bus->voltage > dc_bus_series_voltage(bus, overvoltage)
|
||||
|| bus->voltage > PCB_LS_VOLTAGE_MAX)
|
||||
{
|
||||
ov_debounce_counter++;
|
||||
if (ov_debounce_counter > CONFIG_CONTROL_FREQUENCY) {
|
||||
// waited 1s before setting the flag
|
||||
@@ -102,7 +117,7 @@ void LoadOutput::control()
|
||||
// load is off: check if errors are resolved and if load can be switched on
|
||||
|
||||
if (flags_check(&error_flags, ERR_LOAD_SHEDDING)
|
||||
&& bus->voltage > bus->src_control_voltage(reconnect_voltage)
|
||||
&& bus->voltage > dc_bus_src_control_voltage(bus, reconnect_voltage)
|
||||
&& uptime() - lvd_timestamp > lvd_recovery_delay)
|
||||
{
|
||||
flags_clear(&error_flags, ERR_LOAD_SHEDDING);
|
||||
@@ -115,7 +130,7 @@ void LoadOutput::control()
|
||||
}
|
||||
|
||||
if (flags_check(&error_flags, ERR_LOAD_OVERVOLTAGE)
|
||||
&& bus->voltage < (bus->series_voltage(overvoltage) - ov_hysteresis)
|
||||
&& bus->voltage < (dc_bus_series_voltage(bus, overvoltage) - ov_hysteresis)
|
||||
&& bus->voltage < (PCB_LS_VOLTAGE_MAX - ov_hysteresis))
|
||||
{
|
||||
flags_clear(&error_flags, ERR_LOAD_OVERVOLTAGE);
|
||||
@@ -148,7 +163,8 @@ void LoadOutput::stop(uint32_t flag)
|
||||
|
||||
// flicker the load LED if failure was most probably caused by the user
|
||||
if (flags_check(&error_flags,
|
||||
ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT)) {
|
||||
ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT))
|
||||
{
|
||||
#if LED_EXISTS(load)
|
||||
leds_flicker(LED_POS(load), LED_TIMEOUT_INFINITE);
|
||||
#endif
|
||||
@@ -164,11 +180,25 @@ void LoadOutput::set_voltage_limits(float lvd, float lvr, float ov)
|
||||
overvoltage = ov;
|
||||
}
|
||||
|
||||
extern LoadOutput load;
|
||||
|
||||
void load_short_circuit_stop()
|
||||
void LoadOutput::switch_set(bool on)
|
||||
{
|
||||
load.stop(ERR_LOAD_SHORT_CIRCUIT);
|
||||
if (led >= 0) {
|
||||
leds_set(led, on, LED_TIMEOUT_INFINITE);
|
||||
}
|
||||
|
||||
if (device_is_ready(power_switch)) {
|
||||
if (on) {
|
||||
power_switch_on(power_switch);
|
||||
}
|
||||
else {
|
||||
power_switch_off(power_switch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LoadOutput::short_circuit_callback(const struct device *dev, uint32_t events, void *user_data)
|
||||
{
|
||||
static_cast<LoadOutput *>(user_data)->stop(ERR_LOAD_SHORT_CIRCUIT);
|
||||
}
|
||||
|
||||
#endif /* BOARD_HAS_LOAD_OUTPUT */
|
||||
|
||||
+22
-30
@@ -21,6 +21,8 @@
|
||||
|
||||
#include "power_port.h"
|
||||
|
||||
#include <zephyr/device.h>
|
||||
|
||||
/**
|
||||
* Load/USB output states
|
||||
*/
|
||||
@@ -65,7 +67,7 @@ enum LoadErrorFlag
|
||||
/**
|
||||
* Short circuit detected at load port
|
||||
*
|
||||
* Set by LoadOutput::control() after overcurrent comparator triggered, cleared only if
|
||||
* Set after the power switch reported a short circuit detected in hardware, cleared only if
|
||||
* load output is manually disabled and enabled again.
|
||||
*/
|
||||
ERR_LOAD_SHORT_CIRCUIT = 1U << 3,
|
||||
@@ -90,19 +92,17 @@ enum LoadErrorFlag
|
||||
*
|
||||
* Stores status of load output incl. 5V USB output (if existing on PCB)
|
||||
*/
|
||||
class LoadOutput : public PowerPort
|
||||
class LoadOutput : public power_port
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Initialize LoadOutput struct and overcurrent / short circuit protection comparator (if
|
||||
* existing)
|
||||
* Initialize LoadOutput struct and switch off the output
|
||||
*
|
||||
* @param dc_bus DC bus the load is connected to
|
||||
* @param switch_fn Pointer to function for enabling/disabling load switch
|
||||
* @param init_fn Pointer to function for load driver initialization
|
||||
* @param pgood_fn Pointer to pgood check
|
||||
* @param power_switch Power switch device of the output
|
||||
* @param led LED indicating the switch state or -1 if not existing
|
||||
*/
|
||||
LoadOutput(DcBus *dc_bus, void (*switch_fn)(bool), void (*init_fn)(), bool (*pgood_fn)());
|
||||
LoadOutput(struct dc_bus *dc_bus, const struct device *power_switch, int led = -1);
|
||||
|
||||
/** Main load control function, should be called by control timer
|
||||
*
|
||||
@@ -157,14 +157,24 @@ public:
|
||||
|
||||
private:
|
||||
/**
|
||||
* Pointer to the load switch function
|
||||
* Switch the output and the indicator LED (if existing)
|
||||
*/
|
||||
void (*switch_set)(bool);
|
||||
void switch_set(bool on);
|
||||
|
||||
/**
|
||||
* Pointer to the function to check the power output status
|
||||
* Callback for short circuits detected by the power switch hardware
|
||||
*/
|
||||
bool (*pgood_check)(void);
|
||||
static void short_circuit_callback(const struct device *dev, uint32_t events, void *user_data);
|
||||
|
||||
/**
|
||||
* Power switch device
|
||||
*/
|
||||
const struct device *power_switch;
|
||||
|
||||
/**
|
||||
* LED indicating the switch state or -1 if not existing
|
||||
*/
|
||||
int led;
|
||||
|
||||
/**
|
||||
* Used to prevent switching of because of very short voltage dip
|
||||
@@ -178,22 +188,4 @@ private:
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void load_out_init();
|
||||
void usb_out_init();
|
||||
|
||||
void load_out_set(bool);
|
||||
void usb_out_set(bool);
|
||||
|
||||
void load_short_circuit_stop();
|
||||
|
||||
bool pgood_check();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* LOAD_H */
|
||||
|
||||
@@ -1,253 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "load.h"
|
||||
|
||||
#ifdef CONFIG_SOC_FAMILY_STM32
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/drivers/pwm.h>
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include "board.h"
|
||||
#include "hardware.h"
|
||||
#include "leds.h"
|
||||
#include "mcu.h"
|
||||
|
||||
#include <stm32_ll_bus.h>
|
||||
#include <stm32_ll_system.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
#define LOAD_NODE DT_CHILD(DT_PATH(outputs), load)
|
||||
static const struct gpio_dt_spec load_switch = GPIO_DT_SPEC_GET(LOAD_NODE, gpios);
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
#define USB_NODE DT_CHILD(DT_PATH(outputs), usb_pwr)
|
||||
static const struct gpio_dt_spec usb_switch = GPIO_DT_SPEC_GET(USB_NODE, gpios);
|
||||
#endif
|
||||
|
||||
// short circuit detection comparator only present in PWM 2420 LUS board so far
|
||||
#ifdef CONFIG_BOARD_PWM_2420_LUS
|
||||
|
||||
static void lptim_init()
|
||||
{
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_LPTIM1);
|
||||
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
|
||||
|
||||
// Select alternate function mode on PB2 (first bit _1 = 1, second bit _0 = 0)
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE2)) | GPIO_MODER_MODE2_1;
|
||||
|
||||
// Select AF2 (LPTIM_OUT) on PB2
|
||||
GPIOB->AFR[0] |= 0x2U << GPIO_AFRL_AFSEL2_Pos;
|
||||
|
||||
// Set prescaler to 32 (resulting in 1 MHz timer frequency)
|
||||
LPTIM1->CFGR |= 0x5U << LPTIM_CFGR_PRESC_Pos;
|
||||
|
||||
// Enable trigger (rising edge)
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_TRIGEN_0;
|
||||
|
||||
// Select trigger 7 (COMP2_OUT)
|
||||
LPTIM1->CFGR |= 0x7U << LPTIM_CFGR_TRIGSEL_Pos;
|
||||
|
||||
// LPTIM1->CFGR |= LPTIM_CFGR_WAVPOL;
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_PRELOAD;
|
||||
|
||||
// Glitch filter of 8 cycles
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_TRGFLT_0 | LPTIM_CFGR_TRGFLT_1;
|
||||
|
||||
// Enable set-once mode
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_WAVE;
|
||||
|
||||
// Enable timer (must be done *before* changing ARR or CMP, but *after*
|
||||
// changing CFGR)
|
||||
LPTIM1->CR |= LPTIM_CR_ENABLE;
|
||||
|
||||
// Auto Reload Register
|
||||
LPTIM1->ARR = 1000;
|
||||
|
||||
// Set load switch-off delay in microseconds
|
||||
// (actually takes approx. 4 us longer than this setting)
|
||||
LPTIM1->CMP = 10;
|
||||
|
||||
// Continuous mode
|
||||
// LPTIM1->CR |= LPTIM_CR_CNTSTRT;
|
||||
// LPTIM1->CR |= LPTIM_CR_SNGSTRT;
|
||||
}
|
||||
|
||||
void ADC1_COMP_IRQHandler(void *args)
|
||||
{
|
||||
// interrupt called because of COMP2?
|
||||
if (COMP2->CSR & COMP_CSR_COMP2VALUE) {
|
||||
// Load should be switched off by LPTIM trigger already. This interrupt
|
||||
// is mainly used to indicate the failure.
|
||||
load_short_circuit_stop();
|
||||
}
|
||||
|
||||
// clear interrupt flag
|
||||
EXTI->PR |= EXTI_PR_PIF22;
|
||||
}
|
||||
|
||||
void short_circuit_comp_init()
|
||||
{
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
|
||||
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
|
||||
|
||||
// set GPIO pin to analog
|
||||
GPIOB->MODER &= ~(GPIO_MODER_MODE4);
|
||||
|
||||
// enable VREFINT buffer
|
||||
SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP;
|
||||
|
||||
// select PB4 as positive input
|
||||
COMP2->CSR |= COMP_INPUT_PLUS_IO2;
|
||||
|
||||
// select VREFINT divider as negative input
|
||||
COMP2->CSR |= COMP_INPUT_MINUS_1_4VREFINT;
|
||||
|
||||
// propagate comparator value to LPTIM input
|
||||
COMP2->CSR |= COMP_CSR_COMP2LPTIM1IN1;
|
||||
|
||||
// normal polarity
|
||||
// COMP2->CSR |= COMP_CSR_COMP2POLARITY;
|
||||
|
||||
// set high-speed mode (1.2us instead of 2.5us propagation delay, but 3.5uA instead of 0.5uA
|
||||
// current consumption)
|
||||
// COMP2->CSR |= COMP_CSR_COMP2SPEED;
|
||||
|
||||
// enable COMP2
|
||||
COMP2->CSR |= COMP_CSR_COMP2EN;
|
||||
|
||||
// enable EXTI software interrupt / event
|
||||
EXTI->IMR |= EXTI_IMR_IM22;
|
||||
EXTI->EMR |= EXTI_EMR_EM22;
|
||||
EXTI->RTSR |= EXTI_RTSR_RT22;
|
||||
EXTI->FTSR |= EXTI_FTSR_FT22;
|
||||
EXTI->SWIER |= EXTI_SWIER_SWI22;
|
||||
|
||||
// 1 = second-highest priority of STM32L0/F0
|
||||
IRQ_CONNECT(ADC1_COMP_IRQn, 1, ADC1_COMP_IRQHandler, 0, 0);
|
||||
irq_enable(ADC1_COMP_IRQn);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void load_out_set(bool status)
|
||||
{
|
||||
#if LED_EXISTS(load)
|
||||
leds_set(LED_POS(load), status, LED_TIMEOUT_INFINITE);
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
if (!device_is_ready(load_switch.port)) {
|
||||
printf("Load switch GPIO not ready\n");
|
||||
return;
|
||||
}
|
||||
gpio_pin_configure_dt(&load_switch, GPIO_OUTPUT_INACTIVE);
|
||||
if (status == true) {
|
||||
#ifdef CONFIG_BOARD_PWM_2420_LUS
|
||||
lptim_init();
|
||||
#else
|
||||
gpio_pin_set_dt(&load_switch, 1);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
gpio_pin_set_dt(&load_switch, 0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void usb_out_set(bool status)
|
||||
{
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
if (!device_is_ready(usb_switch.port)) {
|
||||
printf("USB switch GPIO not ready\n");
|
||||
return;
|
||||
}
|
||||
gpio_pin_configure_dt(&usb_switch, GPIO_OUTPUT_INACTIVE);
|
||||
if (status == true) {
|
||||
gpio_pin_set_dt(&usb_switch, 1);
|
||||
#if DT_PROP(DT_CHILD(DT_PATH(outputs), usb_pwr), latching_pgood)
|
||||
k_sleep(K_MSEC(50));
|
||||
gpio_pin_configure_dt(&usb_switch, GPIO_INPUT);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
gpio_pin_set_dt(&usb_switch, 0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), charge_pump))
|
||||
|
||||
#define CP_PWMS DT_CHILD(DT_PATH(outputs), charge_pump)
|
||||
#define CP_PWM_CONTROLLER DT_PWMS_CTLR(CP_PWMS)
|
||||
#define CP_PWM_PERIOD DT_PWMS_PERIOD(CP_PWMS)
|
||||
#define CP_PWM_CHANNEL DT_PWMS_CHANNEL(CP_PWMS)
|
||||
|
||||
void load_cp_enable()
|
||||
{
|
||||
const struct device *pwm_dev = DEVICE_DT_GET(CP_PWM_CONTROLLER);
|
||||
if (device_is_ready(pwm_dev)) {
|
||||
// set to 50% duty cycle
|
||||
pwm_set(pwm_dev, CP_PWM_CHANNEL, CP_PWM_PERIOD, CP_PWM_PERIOD / 2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void load_out_init()
|
||||
{
|
||||
// analog comparator to detect short circuits and trigger immediate load switch-off
|
||||
#ifdef CONFIG_BOARD_PWM_2420_LUS
|
||||
short_circuit_comp_init();
|
||||
#endif
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), charge_pump))
|
||||
// enable charge pump for high-side switches (if existing)
|
||||
load_cp_enable();
|
||||
#endif
|
||||
}
|
||||
|
||||
void usb_out_init()
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
bool pgood_check()
|
||||
{
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
return gpio_pin_get_dt(&usb_switch);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
#else // CONFIG_SOC_FAMILY_STM32
|
||||
|
||||
void load_out_init()
|
||||
{}
|
||||
|
||||
void usb_out_init()
|
||||
{}
|
||||
|
||||
void load_out_set(bool value)
|
||||
{}
|
||||
|
||||
void usb_out_set(bool value)
|
||||
{}
|
||||
|
||||
bool pgood_check()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
+21
-19
@@ -15,12 +15,13 @@
|
||||
#include "helper.h"
|
||||
#include "setup.h"
|
||||
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#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 "half_bridge.h" // PWM generation for DC/DC converter
|
||||
#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
|
||||
@@ -41,16 +42,12 @@ int main(void)
|
||||
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 BOARD_HAS_DCDC
|
||||
daq_set_hv_limit(DT_PROP(DT_PATH(pcb), hs_voltage_max));
|
||||
#endif
|
||||
|
||||
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
|
||||
solar_terminal.init_solar();
|
||||
power_port_init_solar(&solar_terminal);
|
||||
#endif
|
||||
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
grid_terminal.init_nanogrid();
|
||||
power_port_init_nanogrid(&grid_terminal);
|
||||
#endif
|
||||
|
||||
// make sure loaded configuration is valid
|
||||
@@ -59,6 +56,11 @@ int main(void)
|
||||
// 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);
|
||||
|
||||
@@ -86,31 +88,31 @@ int main(void)
|
||||
// energy + soc calculation must be called exactly once per second
|
||||
#if BOARD_HAS_DCDC
|
||||
if (dcdc.state != DCDC_CONTROL_OFF) {
|
||||
hv_terminal.energy_balance();
|
||||
power_port_energy_balance(&hv_terminal);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
if (pwm_switch.active() == 1) {
|
||||
pwm_switch.energy_balance();
|
||||
power_port_energy_balance(&pwm_switch);
|
||||
}
|
||||
#endif
|
||||
|
||||
lv_terminal.energy_balance();
|
||||
power_port_energy_balance(&lv_terminal);
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
if (load.state == 1) {
|
||||
load.energy_balance();
|
||||
power_port_energy_balance(&load);
|
||||
}
|
||||
#endif
|
||||
|
||||
dev_stat.update_energy();
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_energy(&dev_stat);
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
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();
|
||||
if (device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT)) {
|
||||
dcdc_fuse_destruction(&dcdc);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -144,7 +146,7 @@ void control_thread()
|
||||
// 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();
|
||||
power_port_update_bus_current_margins(&lv_terminal);
|
||||
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
pwm_switch.control();
|
||||
@@ -152,9 +154,9 @@ void control_thread()
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
hv_terminal.update_bus_current_margins();
|
||||
dcdc.control(); // control of DC/DC including MPPT algorithm
|
||||
charging |= half_bridge_enabled();
|
||||
power_port_update_bus_current_margins(&hv_terminal);
|
||||
dcdc_control(&dcdc); // control of DC/DC including MPPT algorithm
|
||||
charging |= half_bridge_is_enabled(dcdc.half_bridge);
|
||||
#endif
|
||||
|
||||
leds_set_charging(charging);
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef MCU_H_
|
||||
#define MCU_H_
|
||||
|
||||
#ifdef CONFIG_SOC_STM32F072XB
|
||||
#include "stm32f072xb.h"
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_SOC_STM32L072XX
|
||||
#include "stm32l072xx.h"
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_SOC_STM32G431XX)
|
||||
#include "stm32g431xx.h"
|
||||
#elif defined(CONFIG_SOC_STM32G474XX)
|
||||
#include "stm32g474xx.h"
|
||||
#endif
|
||||
|
||||
// factory calibration values for internal voltage reference and temperature sensor
|
||||
// (see MCU datasheet, not Reference Manual)
|
||||
#if defined(CONFIG_SOC_SERIES_STM32F0X)
|
||||
|
||||
#define VREFINT_CAL (*((uint16_t *)0x1FFFF7BA)) // VREFINT @3.3V/30°C
|
||||
#define VREFINT_VALUE 3300 // mV
|
||||
#define TSENSE_CAL1 (*((uint16_t *)0x1FFFF7B8))
|
||||
#define TSENSE_CAL2 (*((uint16_t *)0x1FFFF7C2))
|
||||
#define TSENSE_CAL1_VALUE 30.0 // temperature of first calibration point
|
||||
#define TSENSE_CAL2_VALUE 110.0 // temperature of second calibration point
|
||||
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
|
||||
#define VREFINT_CAL (*((uint16_t *)0x1FF80078)) // VREFINT @3.0V/25°C
|
||||
#define VREFINT_VALUE 3000 // mV
|
||||
#define TSENSE_CAL1 (*((uint16_t *)0x1FF8007A))
|
||||
#define TSENSE_CAL2 (*((uint16_t *)0x1FF8007E))
|
||||
#define TSENSE_CAL1_VALUE 30.0 // temperature of first calibration point
|
||||
#define TSENSE_CAL2_VALUE 130.0 // temperature of second calibration point
|
||||
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
|
||||
#define VREFINT_CAL (*((uint16_t *)0x1FFF75AA)) // VREFINT @3.0V/30°C
|
||||
#define VREFINT_VALUE 3000 // mV
|
||||
#define TSENSE_CAL1 (*((uint16_t *)0x1FFF75A8))
|
||||
#define TSENSE_CAL2 (*((uint16_t *)0x1FFF75CA))
|
||||
#define TSENSE_CAL1_VALUE 30.0 // temperature of first calibration point
|
||||
#define TSENSE_CAL2_VALUE 110.0 // temperature of second calibration point
|
||||
|
||||
#elif !defined(CONFIG_SOC_FAMILY_STM32)
|
||||
|
||||
#define VREFINT_CAL (4096 * 1.224 / 3.0) // VREFINT @3.0V/25°C
|
||||
#define VREFINT_VALUE 3000 // mV
|
||||
#define TSENSE_CAL1 (4096.0 * (670 - 161) / 3300) // datasheet: slope 1.61 mV/°C
|
||||
#define TSENSE_CAL2 (4096.0 * 670 / 3300) // datasheet: 670 mV
|
||||
#define TSENSE_CAL1_VALUE 30.0 // temperature of first calibration point
|
||||
#define TSENSE_CAL2_VALUE 130.0 // temperature of second calibration point
|
||||
|
||||
#endif
|
||||
|
||||
#endif // MCU_H_
|
||||
+173
-118
@@ -4,22 +4,25 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#if CONFIG_UEXT_OLED_DISPLAY
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/display/cfb.h>
|
||||
#include <zephyr/drivers/display.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "oled_ssd1306.h"
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include "half_bridge.h"
|
||||
#include "oled_logo.h"
|
||||
#include "setup.h"
|
||||
|
||||
LOG_MODULE_REGISTER(oled);
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), uext_en))
|
||||
#define UEXT_EN_GPIO DT_CHILD(DT_PATH(outputs), uext_en)
|
||||
#define UEXT_EN_NODE DT_CHILD(DT_PATH(outputs), uext_en)
|
||||
#endif
|
||||
|
||||
const unsigned char bmp_load[] = {
|
||||
@@ -39,145 +42,199 @@ const unsigned char bmp_disconnected[] = {
|
||||
0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
OledSSD1306 oled(DEVICE_DT_GET(DT_ALIAS(i2c_uext)));
|
||||
static const struct device *const display = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
|
||||
|
||||
static void draw_bitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h)
|
||||
{
|
||||
for (int16_t j = 0; j < h; j++) {
|
||||
for (int16_t i = 0; i < w; i++) {
|
||||
if (bitmap[i + (j / 8) * w] & BIT(j % 8)) {
|
||||
struct cfb_position pos = { (int16_t)(x + i), (int16_t)(y + j) };
|
||||
cfb_draw_point(display, &pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void draw_rect(int16_t x, int16_t y, int16_t w, int16_t h)
|
||||
{
|
||||
struct cfb_position start = { x, y };
|
||||
struct cfb_position end = { (int16_t)(x + w - 1), (int16_t)(y + h - 1) };
|
||||
|
||||
cfb_draw_rect(display, &start, &end);
|
||||
}
|
||||
|
||||
void oled_update()
|
||||
{
|
||||
char buf[30];
|
||||
unsigned int len;
|
||||
|
||||
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
|
||||
PowerPort &in_terminal = solar_terminal;
|
||||
struct power_port &in_terminal = solar_terminal;
|
||||
#elif CONFIG_HV_TERMINAL_NANOGRID
|
||||
PowerPort &in_terminal = grid_terminal;
|
||||
struct power_port &in_terminal = grid_terminal;
|
||||
#endif
|
||||
|
||||
oled.clear();
|
||||
|
||||
oled.drawBitmap(6, 0, bmp_pv_panel, 16, 16, 1);
|
||||
oled.drawBitmap(104, 0, bmp_load, 16, 16, 1);
|
||||
|
||||
if (half_bridge_enabled() == false) {
|
||||
oled.drawBitmap(27, 3, bmp_disconnected, 32, 8, 1);
|
||||
}
|
||||
else {
|
||||
oled.drawBitmap(34, 3, bmp_arrow_right, 5, 7, 1);
|
||||
}
|
||||
|
||||
if (load.state == LOAD_STATE_ON) {
|
||||
oled.drawBitmap(84, 3, bmp_arrow_right, 5, 7, 1);
|
||||
}
|
||||
else {
|
||||
oled.drawBitmap(81, 3, bmp_disconnected, 17, 7, 1);
|
||||
}
|
||||
|
||||
oled.drawRect(52, 2, 18, 9, 1); // battery shape
|
||||
oled.drawRect(69, 3, 3, 7, 1); // battery terminal
|
||||
|
||||
if (charger.soc >= 20) {
|
||||
oled.drawRect(54, 4, 2, 5, 1); // bar 1
|
||||
}
|
||||
if (charger.soc >= 40) {
|
||||
oled.drawRect(57, 4, 2, 5, 1); // bar 2
|
||||
}
|
||||
if (charger.soc >= 60) {
|
||||
oled.drawRect(60, 4, 2, 5, 1); // bar 3
|
||||
}
|
||||
if (charger.soc >= 80) {
|
||||
oled.drawRect(63, 4, 2, 5, 1); // bar 4
|
||||
}
|
||||
if (charger.soc >= 95) {
|
||||
oled.drawRect(66, 4, 2, 5, 1); // bar 5
|
||||
}
|
||||
|
||||
// solar panel data
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
if (pwm_switch.active()) {
|
||||
#else
|
||||
if (half_bridge_enabled()) {
|
||||
#endif
|
||||
oled.setTextCursor(0, 18);
|
||||
len = snprintf(buf, sizeof(buf), "%4.0fW",
|
||||
(abs(in_terminal.power) < 1) ? 0
|
||||
: -in_terminal.power); // remove negative zeros
|
||||
oled.writeString(buf, len);
|
||||
}
|
||||
else {
|
||||
oled.setTextCursor(8, 18);
|
||||
len = snprintf(buf, sizeof(buf), "n/a");
|
||||
oled.writeString(buf, len);
|
||||
}
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
if (in_terminal.bus->voltage > bat_terminal.bus->voltage)
|
||||
#endif
|
||||
{
|
||||
oled.setTextCursor(0, 26);
|
||||
len = snprintf(buf, sizeof(buf), "%4.1fV", in_terminal.bus->voltage);
|
||||
oled.writeString(buf, len);
|
||||
}
|
||||
|
||||
// battery data
|
||||
oled.setTextCursor(42, 18);
|
||||
len =
|
||||
snprintf(buf, sizeof(buf), "%5.1fW",
|
||||
(abs(bat_terminal.power) < 0.1) ? 0 : bat_terminal.power); // remove negative zeros
|
||||
oled.writeString(buf, len);
|
||||
oled.setTextCursor(42, 26);
|
||||
len = snprintf(buf, sizeof(buf), "%5.1fV", bat_terminal.bus->voltage);
|
||||
oled.writeString(buf, len);
|
||||
|
||||
// load data
|
||||
oled.setTextCursor(90, 18);
|
||||
len = snprintf(buf, sizeof(buf), "%5.1fW",
|
||||
(abs(load.power) < 0.1) ? 0 : load.power); // remove negative zeros
|
||||
oled.writeString(buf, len);
|
||||
oled.setTextCursor(90, 26);
|
||||
len = snprintf(buf, sizeof(buf), "%5.1fA\n", (abs(load.current) < 0.1) ? 0 : load.current);
|
||||
oled.writeString(buf, len);
|
||||
|
||||
oled.setTextCursor(0, 36);
|
||||
len = snprintf(buf, sizeof(buf), "Day +%5.0fWh -%5.0fWh", in_terminal.neg_energy_Wh,
|
||||
fabs(load.pos_energy_Wh));
|
||||
oled.writeString(buf, len);
|
||||
len = snprintf(buf, sizeof(buf), "Tot +%4.1fkWh -%4.1fkWh", dev_stat.solar_in_total_Wh / 1000.0,
|
||||
fabs(dev_stat.load_out_total_Wh) / 1000.0);
|
||||
oled.writeString(buf, len);
|
||||
|
||||
oled.setTextCursor(0, 56);
|
||||
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
bool pwm_enabled = pwm_switch.active();
|
||||
float duty_cycle = pwm_switch.get_duty_cycle();
|
||||
#else
|
||||
bool pwm_enabled = half_bridge_enabled();
|
||||
float duty_cycle = half_bridge_get_duty_cycle();
|
||||
bool pwm_enabled = half_bridge_is_enabled(dcdc.half_bridge);
|
||||
float duty_cycle = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
#endif
|
||||
|
||||
cfb_framebuffer_clear(display, false);
|
||||
|
||||
draw_bitmap(6, 0, bmp_pv_panel, 16, 16);
|
||||
|
||||
if (pwm_enabled == false) {
|
||||
draw_bitmap(27, 3, bmp_disconnected, 32, 8);
|
||||
}
|
||||
else {
|
||||
draw_bitmap(34, 3, bmp_arrow_right, 5, 7);
|
||||
}
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
draw_bitmap(104, 0, bmp_load, 16, 16);
|
||||
if (load.state == LOAD_STATE_ON) {
|
||||
draw_bitmap(84, 3, bmp_arrow_right, 5, 7);
|
||||
}
|
||||
else {
|
||||
draw_bitmap(81, 3, bmp_disconnected, 17, 7);
|
||||
}
|
||||
#endif
|
||||
|
||||
draw_rect(52, 2, 18, 9); // battery shape
|
||||
draw_rect(69, 3, 3, 7); // battery terminal
|
||||
|
||||
if (charger.soc >= 20) {
|
||||
draw_rect(54, 4, 2, 5); // bar 1
|
||||
}
|
||||
if (charger.soc >= 40) {
|
||||
draw_rect(57, 4, 2, 5); // bar 2
|
||||
}
|
||||
if (charger.soc >= 60) {
|
||||
draw_rect(60, 4, 2, 5); // bar 3
|
||||
}
|
||||
if (charger.soc >= 80) {
|
||||
draw_rect(63, 4, 2, 5); // bar 4
|
||||
}
|
||||
if (charger.soc >= 95) {
|
||||
draw_rect(66, 4, 2, 5); // bar 5
|
||||
}
|
||||
|
||||
// solar panel data (values close to zero are printed as 0 to remove negative zeros)
|
||||
if (pwm_enabled) {
|
||||
snprintf(buf, sizeof(buf), "%4.0fW",
|
||||
(fabsf(in_terminal.power) < 1.0F) ? 0.0 : -(double)in_terminal.power);
|
||||
cfb_print(display, buf, 0, 18);
|
||||
}
|
||||
else {
|
||||
cfb_print(display, "n/a", 8, 18);
|
||||
}
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
if (in_terminal.bus->voltage > bat_terminal.bus->voltage)
|
||||
#endif
|
||||
{
|
||||
snprintf(buf, sizeof(buf), "%4.1fV", (double)in_terminal.bus->voltage);
|
||||
cfb_print(display, buf, 0, 26);
|
||||
}
|
||||
|
||||
// battery data
|
||||
snprintf(buf, sizeof(buf), "%5.1fW",
|
||||
(fabsf(bat_terminal.power) < 0.1F) ? 0.0 : (double)bat_terminal.power);
|
||||
cfb_print(display, buf, 42, 18);
|
||||
snprintf(buf, sizeof(buf), "%5.1fV", (double)bat_terminal.bus->voltage);
|
||||
cfb_print(display, buf, 42, 26);
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
float load_energy_Wh = load.pos_energy_Wh;
|
||||
|
||||
// load data
|
||||
snprintf(buf, sizeof(buf), "%5.1fW", (fabsf(load.power) < 0.1F) ? 0.0 : (double)load.power);
|
||||
cfb_print(display, buf, 90, 18);
|
||||
snprintf(buf, sizeof(buf), "%5.1fA", (fabsf(load.current) < 0.1F) ? 0.0 : (double)load.current);
|
||||
cfb_print(display, buf, 90, 26);
|
||||
#else
|
||||
float load_energy_Wh = 0.0F;
|
||||
#endif
|
||||
|
||||
snprintf(buf, sizeof(buf), "Day +%5.0fWh -%5.0fWh", (double)in_terminal.neg_energy_Wh,
|
||||
(double)fabsf(load_energy_Wh));
|
||||
cfb_print(display, buf, 0, 36);
|
||||
snprintf(buf, sizeof(buf), "Tot +%4.1fkWh -%4.1fkWh", dev_stat.solar_in_total_Wh / 1000.0,
|
||||
fabs(dev_stat.load_out_total_Wh) / 1000.0);
|
||||
cfb_print(display, buf, 0, 44);
|
||||
|
||||
float temp = charger.ext_temp_sensor ? charger.bat_temperature : dev_stat.internal_temp;
|
||||
char tC = charger.ext_temp_sensor ? 'T' : 't';
|
||||
|
||||
if (pwm_enabled == true) {
|
||||
len = snprintf(buf, sizeof(buf), "%c %.0fC PWM %.0f%% SOC %d%%", tC, temp,
|
||||
duty_cycle * 100.0, charger.soc);
|
||||
oled.writeString(buf, len);
|
||||
snprintf(buf, sizeof(buf), "%c %.0fC PWM %.0f%% SOC %d%%", tC, (double)temp,
|
||||
(double)duty_cycle * 100.0, charger.soc);
|
||||
}
|
||||
else {
|
||||
len = snprintf(buf, sizeof(buf), "%c %.0fC PWM OFF SOC %d%%", tC, temp, charger.soc);
|
||||
oled.writeString(buf, len);
|
||||
snprintf(buf, sizeof(buf), "%c %.0fC PWM OFF SOC %d%%", tC, (double)temp, charger.soc);
|
||||
}
|
||||
cfb_print(display, buf, 0, 56);
|
||||
|
||||
cfb_framebuffer_finalize(display);
|
||||
}
|
||||
|
||||
static int oled_init()
|
||||
{
|
||||
uint8_t font_width;
|
||||
uint8_t font_height;
|
||||
int err;
|
||||
|
||||
#ifdef UEXT_EN_NODE
|
||||
const struct gpio_dt_spec uext_en = GPIO_DT_SPEC_GET(UEXT_EN_NODE, gpios);
|
||||
|
||||
err = gpio_pin_configure_dt(&uext_en, GPIO_OUTPUT_ACTIVE);
|
||||
if (err != 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
oled.display();
|
||||
/* the ready-time-ms of the display driver is counted from boot, not from power-on */
|
||||
k_sleep(K_MSEC(DT_PROP(DT_CHOSEN(zephyr_display), ready_time_ms)));
|
||||
#endif
|
||||
|
||||
/* deferred, as the UEXT port may not have been powered at boot */
|
||||
err = device_init(display);
|
||||
if (err != 0 && err != -EALREADY) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = cfb_framebuffer_init(display);
|
||||
if (err != 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* select the 5x8 font from oled_font.c with 1 pixel spacing between characters */
|
||||
for (int i = 0; i < cfb_get_numof_fonts(display); i++) {
|
||||
cfb_get_font_size(display, i, &font_width, &font_height);
|
||||
if (font_width == 5 && font_height == 8) {
|
||||
cfb_framebuffer_set_font(display, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
cfb_set_kerning(display, 1);
|
||||
|
||||
draw_bitmap(0, 0, oled_logo, 128, 64);
|
||||
cfb_framebuffer_finalize(display);
|
||||
|
||||
return display_blanking_off(display);
|
||||
}
|
||||
|
||||
void oled_thread()
|
||||
{
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), uext_en))
|
||||
const struct device *dev_uext_en = device_get_binding(DT_GPIO_LABEL(UEXT_EN_GPIO, gpios));
|
||||
gpio_pin_configure(dev_uext_en, DT_GPIO_PIN(UEXT_EN_GPIO, gpios),
|
||||
DT_GPIO_FLAGS(UEXT_EN_GPIO, gpios) | GPIO_OUTPUT_ACTIVE);
|
||||
#endif
|
||||
int err = oled_init();
|
||||
if (err != 0) {
|
||||
LOG_ERR("Failed to initialize OLED display: %d", err);
|
||||
return;
|
||||
}
|
||||
|
||||
oled.init(CONFIG_UEXT_OLED_BRIGHTNESS);
|
||||
/* keep the logo visible for a moment */
|
||||
k_sleep(K_SECONDS(2));
|
||||
|
||||
while (true) {
|
||||
oled_update();
|
||||
@@ -186,5 +243,3 @@ void oled_thread()
|
||||
}
|
||||
|
||||
K_THREAD_DEFINE(oled_thread_id, 1024, oled_thread, NULL, NULL, NULL, 6, 0, 1000);
|
||||
|
||||
#endif /* CONFIG_UEXT_OLED_DISPLAY */
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Adafruit Industries. All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
/*
|
||||
* Classic 5x7 fixed-space font of the Adafruit GFX library, reduced to printable ASCII
|
||||
* characters and converted to a font for the Zephyr character framebuffer.
|
||||
*
|
||||
* Each glyph consists of 5 columns, the LSB of each byte is the topmost pixel.
|
||||
*/
|
||||
|
||||
#include <zephyr/display/cfb.h>
|
||||
|
||||
static const uint8_t font_5x8_data[] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, /* ' ' */
|
||||
0x00, 0x00, 0x5f, 0x00, 0x00, /* '!' */
|
||||
0x00, 0x07, 0x00, 0x07, 0x00, /* '"' */
|
||||
0x14, 0x7f, 0x14, 0x7f, 0x14, /* '#' */
|
||||
0x24, 0x2a, 0x7f, 0x2a, 0x12, /* '$' */
|
||||
0x23, 0x13, 0x08, 0x64, 0x62, /* '%' */
|
||||
0x36, 0x49, 0x56, 0x20, 0x50, /* '&' */
|
||||
0x00, 0x08, 0x07, 0x03, 0x00, /* ''' */
|
||||
0x00, 0x1c, 0x22, 0x41, 0x00, /* '(' */
|
||||
0x00, 0x41, 0x22, 0x1c, 0x00, /* ')' */
|
||||
0x2a, 0x1c, 0x7f, 0x1c, 0x2a, /* '*' */
|
||||
0x08, 0x08, 0x3e, 0x08, 0x08, /* '+' */
|
||||
0x00, 0x80, 0x70, 0x30, 0x00, /* ',' */
|
||||
0x08, 0x08, 0x08, 0x08, 0x08, /* '-' */
|
||||
0x00, 0x00, 0x60, 0x60, 0x00, /* '.' */
|
||||
0x20, 0x10, 0x08, 0x04, 0x02, /* '/' */
|
||||
0x3e, 0x51, 0x49, 0x45, 0x3e, /* '0' */
|
||||
0x00, 0x42, 0x7f, 0x40, 0x00, /* '1' */
|
||||
0x72, 0x49, 0x49, 0x49, 0x46, /* '2' */
|
||||
0x21, 0x41, 0x49, 0x4d, 0x33, /* '3' */
|
||||
0x18, 0x14, 0x12, 0x7f, 0x10, /* '4' */
|
||||
0x27, 0x45, 0x45, 0x45, 0x39, /* '5' */
|
||||
0x3c, 0x4a, 0x49, 0x49, 0x31, /* '6' */
|
||||
0x41, 0x21, 0x11, 0x09, 0x07, /* '7' */
|
||||
0x36, 0x49, 0x49, 0x49, 0x36, /* '8' */
|
||||
0x46, 0x49, 0x49, 0x29, 0x1e, /* '9' */
|
||||
0x00, 0x00, 0x14, 0x00, 0x00, /* ':' */
|
||||
0x00, 0x40, 0x34, 0x00, 0x00, /* ';' */
|
||||
0x00, 0x08, 0x14, 0x22, 0x41, /* '<' */
|
||||
0x14, 0x14, 0x14, 0x14, 0x14, /* '=' */
|
||||
0x00, 0x41, 0x22, 0x14, 0x08, /* '>' */
|
||||
0x02, 0x01, 0x59, 0x09, 0x06, /* '?' */
|
||||
0x3e, 0x41, 0x5d, 0x59, 0x4e, /* '@' */
|
||||
0x7c, 0x12, 0x11, 0x12, 0x7c, /* 'A' */
|
||||
0x7f, 0x49, 0x49, 0x49, 0x36, /* 'B' */
|
||||
0x3e, 0x41, 0x41, 0x41, 0x22, /* 'C' */
|
||||
0x7f, 0x41, 0x41, 0x41, 0x3e, /* 'D' */
|
||||
0x7f, 0x49, 0x49, 0x49, 0x41, /* 'E' */
|
||||
0x7f, 0x09, 0x09, 0x09, 0x01, /* 'F' */
|
||||
0x3e, 0x41, 0x41, 0x51, 0x73, /* 'G' */
|
||||
0x7f, 0x08, 0x08, 0x08, 0x7f, /* 'H' */
|
||||
0x00, 0x41, 0x7f, 0x41, 0x00, /* 'I' */
|
||||
0x20, 0x40, 0x41, 0x3f, 0x01, /* 'J' */
|
||||
0x7f, 0x08, 0x14, 0x22, 0x41, /* 'K' */
|
||||
0x7f, 0x40, 0x40, 0x40, 0x40, /* 'L' */
|
||||
0x7f, 0x02, 0x1c, 0x02, 0x7f, /* 'M' */
|
||||
0x7f, 0x04, 0x08, 0x10, 0x7f, /* 'N' */
|
||||
0x3e, 0x41, 0x41, 0x41, 0x3e, /* 'O' */
|
||||
0x7f, 0x09, 0x09, 0x09, 0x06, /* 'P' */
|
||||
0x3e, 0x41, 0x51, 0x21, 0x5e, /* 'Q' */
|
||||
0x7f, 0x09, 0x19, 0x29, 0x46, /* 'R' */
|
||||
0x26, 0x49, 0x49, 0x49, 0x32, /* 'S' */
|
||||
0x03, 0x01, 0x7f, 0x01, 0x03, /* 'T' */
|
||||
0x3f, 0x40, 0x40, 0x40, 0x3f, /* 'U' */
|
||||
0x1f, 0x20, 0x40, 0x20, 0x1f, /* 'V' */
|
||||
0x3f, 0x40, 0x38, 0x40, 0x3f, /* 'W' */
|
||||
0x63, 0x14, 0x08, 0x14, 0x63, /* 'X' */
|
||||
0x03, 0x04, 0x78, 0x04, 0x03, /* 'Y' */
|
||||
0x61, 0x59, 0x49, 0x4d, 0x43, /* 'Z' */
|
||||
0x00, 0x7f, 0x41, 0x41, 0x41, /* '[' */
|
||||
0x02, 0x04, 0x08, 0x10, 0x20, /* backslash */
|
||||
0x00, 0x41, 0x41, 0x41, 0x7f, /* ']' */
|
||||
0x04, 0x02, 0x01, 0x02, 0x04, /* '^' */
|
||||
0x40, 0x40, 0x40, 0x40, 0x40, /* '_' */
|
||||
0x00, 0x03, 0x07, 0x08, 0x00, /* '`' */
|
||||
0x20, 0x54, 0x54, 0x78, 0x40, /* 'a' */
|
||||
0x7f, 0x28, 0x44, 0x44, 0x38, /* 'b' */
|
||||
0x38, 0x44, 0x44, 0x44, 0x28, /* 'c' */
|
||||
0x38, 0x44, 0x44, 0x28, 0x7f, /* 'd' */
|
||||
0x38, 0x54, 0x54, 0x54, 0x18, /* 'e' */
|
||||
0x00, 0x08, 0x7e, 0x09, 0x02, /* 'f' */
|
||||
0x18, 0xa4, 0xa4, 0x9c, 0x78, /* 'g' */
|
||||
0x7f, 0x08, 0x04, 0x04, 0x78, /* 'h' */
|
||||
0x00, 0x44, 0x7d, 0x40, 0x00, /* 'i' */
|
||||
0x20, 0x40, 0x40, 0x3d, 0x00, /* 'j' */
|
||||
0x7f, 0x10, 0x28, 0x44, 0x00, /* 'k' */
|
||||
0x00, 0x41, 0x7f, 0x40, 0x00, /* 'l' */
|
||||
0x7c, 0x04, 0x78, 0x04, 0x78, /* 'm' */
|
||||
0x7c, 0x08, 0x04, 0x04, 0x78, /* 'n' */
|
||||
0x38, 0x44, 0x44, 0x44, 0x38, /* 'o' */
|
||||
0xfc, 0x18, 0x24, 0x24, 0x18, /* 'p' */
|
||||
0x18, 0x24, 0x24, 0x18, 0xfc, /* 'q' */
|
||||
0x7c, 0x08, 0x04, 0x04, 0x08, /* 'r' */
|
||||
0x48, 0x54, 0x54, 0x54, 0x24, /* 's' */
|
||||
0x04, 0x04, 0x3f, 0x44, 0x24, /* 't' */
|
||||
0x3c, 0x40, 0x40, 0x20, 0x7c, /* 'u' */
|
||||
0x1c, 0x20, 0x40, 0x20, 0x1c, /* 'v' */
|
||||
0x3c, 0x40, 0x30, 0x40, 0x3c, /* 'w' */
|
||||
0x44, 0x28, 0x10, 0x28, 0x44, /* 'x' */
|
||||
0x4c, 0x90, 0x90, 0x90, 0x7c, /* 'y' */
|
||||
0x44, 0x64, 0x54, 0x4c, 0x44, /* 'z' */
|
||||
0x00, 0x08, 0x36, 0x41, 0x00, /* '{' */
|
||||
0x00, 0x00, 0x77, 0x00, 0x00, /* '|' */
|
||||
0x00, 0x41, 0x36, 0x08, 0x00, /* '}' */
|
||||
0x02, 0x01, 0x02, 0x04, 0x02, /* '~' */
|
||||
};
|
||||
|
||||
FONT_ENTRY_DEFINE(font_5x8, 5, 8, CFB_FONT_MONO_VPACKED, font_5x8_data, ' ', '~');
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef OLED_LOGO_H_
|
||||
#define OLED_LOGO_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* Libre Solar logo in SSD1306 page layout (128x64 pixels, LSB of each byte at the top) */
|
||||
static const uint8_t oled_logo[64 * 128 / 8] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
|
||||
0xC0, 0xE0, 0xE0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
|
||||
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x3F,
|
||||
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x08, 0x08, 0x08, 0x08, 0x08, 0xE7, 0xC7,
|
||||
0xC3, 0x87, 0x07, 0x07, 0x0F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1E, 0x1F, 0x1F, 0x3F,
|
||||
0x3E, 0x7E, 0x7E, 0xFC, 0xFC, 0xF8, 0xF0, 0xC0, 0x01, 0x03, 0x87, 0x87, 0x87, 0x87, 0x87, 0x00,
|
||||
0x30, 0x30, 0x30, 0x20, 0x20, 0x60, 0x40, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFC, 0xF8, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE,
|
||||
0xFE, 0xFC, 0xF8, 0xF0, 0xE3, 0xC7, 0x9F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x06, 0x18, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0x03, 0x07, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x07, 0x03, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x60, 0xC0,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0xC0, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0xE0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x80, 0xC0,
|
||||
0x60, 0x20, 0x20, 0x20, 0x20, 0x60, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0x40, 0x60, 0x20,
|
||||
0x20, 0x60, 0x40, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xE0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0xE0, 0xE0, 0x20, 0x20, 0x20, 0x20, 0x60, 0x40, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x18, 0x77,
|
||||
0xC0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x10, 0x10, 0x10, 0x10, 0x30, 0xD0, 0x08, 0x04, 0x03, 0x00,
|
||||
0x00, 0x00, 0x00, 0xFF, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0F,
|
||||
0x08, 0x18, 0x18, 0x10, 0x10, 0x30, 0xE0, 0x80, 0x00, 0x00, 0x10, 0xFF, 0x81, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0xC7, 0xFF, 0x10, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xF8, 0x8F, 0x81, 0x80, 0x87, 0x9C, 0xF0, 0x80, 0x00, 0x00,
|
||||
0x00, 0x00, 0xFF, 0xFF, 0x10, 0x10, 0x10, 0x30, 0xF0, 0x88, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
|
||||
0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0C, 0x06,
|
||||
0x03, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0E, 0x08, 0x00,
|
||||
0x00, 0x00, 0x00, 0x0F, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x02, 0x06,
|
||||
0x04, 0x04, 0x0C, 0x04, 0x04, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x06, 0x04, 0x0C,
|
||||
0x0C, 0x04, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
|
||||
0x0C, 0x0C, 0x00, 0x08, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0F, 0x0C, 0x00,
|
||||
0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0F, 0x0C, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
#endif /* OLED_LOGO_H_ */
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "power_port.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
void dc_bus_init(struct dc_bus *bus)
|
||||
{
|
||||
memset(bus, 0, sizeof(*bus));
|
||||
bus->series_multiplier = 1;
|
||||
}
|
||||
|
||||
void power_port_init(struct power_port *port, struct dc_bus *bus, bool assign_ref_current)
|
||||
{
|
||||
memset(port, 0, sizeof(*port));
|
||||
port->bus = bus;
|
||||
|
||||
if (assign_ref_current) {
|
||||
bus->ref_current = &port->current;
|
||||
}
|
||||
}
|
||||
|
||||
void power_port_init_solar(struct power_port *port)
|
||||
{
|
||||
port->neg_current_limit = -50; // derating based on max. DC/DC or PWM switch current only
|
||||
port->pos_current_limit = 0; // no current towards solar panel allowed
|
||||
}
|
||||
|
||||
void power_port_init_nanogrid(struct power_port *port)
|
||||
{
|
||||
port->pos_current_limit = 5.0F;
|
||||
port->neg_current_limit = -5.0F;
|
||||
|
||||
// 1 Ohm means 1V change of target voltage per amp, same droop for both current directions.
|
||||
port->bus->sink_droop_res = 0.1F;
|
||||
port->bus->src_droop_res = 0.1F;
|
||||
|
||||
// also initialize the connected bus
|
||||
port->bus->src_voltage_intercept = 30.0F; // starting buck mode above this point
|
||||
port->bus->sink_voltage_intercept = 28.0F; // boost mode until this voltage is reached
|
||||
}
|
||||
|
||||
void power_port_energy_balance(struct power_port *port)
|
||||
{
|
||||
// remark: timespan = 1s, so no multiplication with time necessary for energy calculation
|
||||
if (port->current >= 0.0F) {
|
||||
port->pos_energy_Wh += port->bus->voltage * port->current / 3600.0F;
|
||||
}
|
||||
else {
|
||||
port->neg_energy_Wh -= port->bus->voltage * port->current / 3600.0F;
|
||||
}
|
||||
}
|
||||
|
||||
void power_port_update_bus_current_margins(const struct power_port *port)
|
||||
{
|
||||
// charging direction of battery
|
||||
port->bus->sink_current_margin = port->pos_current_limit - port->current;
|
||||
|
||||
// discharging direction of battery
|
||||
port->bus->src_current_margin = port->neg_current_limit - port->current;
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "power_port.h"
|
||||
|
||||
void PowerPort::init_solar()
|
||||
{
|
||||
neg_current_limit = -50; // derating based on max. DC/DC or PWM switch current only
|
||||
pos_current_limit = 0; // no current towards solar panel allowed
|
||||
}
|
||||
|
||||
void PowerPort::init_nanogrid()
|
||||
{
|
||||
pos_current_limit = 5.0;
|
||||
neg_current_limit = -5.0;
|
||||
|
||||
// 1 Ohm means 1V change of target voltage per amp, same droop for both current directions.
|
||||
bus->sink_droop_res = 0.1;
|
||||
bus->src_droop_res = 0.1;
|
||||
|
||||
// also initialize the connected bus
|
||||
bus->src_voltage_intercept = 30.0; // starting buck mode above this point
|
||||
bus->sink_voltage_intercept = 28.0; // boost mode until this voltage is reached
|
||||
}
|
||||
|
||||
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.0F;
|
||||
}
|
||||
else {
|
||||
neg_energy_Wh -= bus->voltage * current / 3600.0F;
|
||||
}
|
||||
}
|
||||
|
||||
void PowerPort::update_bus_current_margins() const
|
||||
{
|
||||
// charging direction of battery
|
||||
bus->sink_current_margin = pos_current_limit - current;
|
||||
|
||||
// discharging direction of battery
|
||||
bus->src_current_margin = neg_current_limit - current;
|
||||
|
||||
// printf("pos: %.3f neg: %.3f current: %.3f\n", pos_current_limit, neg_current_limit, current);
|
||||
}
|
||||
+132
-109
@@ -16,20 +16,21 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
class PowerPort; // forward-declaration
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* DC bus class
|
||||
*
|
||||
* Stores measurement data and settings necessary for voltage control
|
||||
*/
|
||||
class DcBus
|
||||
struct dc_bus
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Measured bus voltage
|
||||
*/
|
||||
float voltage = 0;
|
||||
float voltage;
|
||||
|
||||
/**
|
||||
* Filtered bus voltage
|
||||
@@ -37,7 +38,7 @@ public:
|
||||
* Suitable filter time constants depend on type of DC bus. This filtering is applied in
|
||||
* addition to the raw ADC value filtering.
|
||||
*/
|
||||
float voltage_filtered = 0;
|
||||
float voltage_filtered;
|
||||
|
||||
/**
|
||||
* Multiplier for series connection of batteries
|
||||
@@ -46,7 +47,7 @@ public:
|
||||
*
|
||||
* This factor must be applied to all voltage setpoints
|
||||
*/
|
||||
int16_t series_multiplier = 1;
|
||||
int16_t series_multiplier;
|
||||
|
||||
/**
|
||||
* Upper voltage boundary where this bus may be used to sink current.
|
||||
@@ -54,7 +55,7 @@ public:
|
||||
* This value is the voltage at zero current. Values for other currents are calculated
|
||||
* using the droop resistance.
|
||||
*/
|
||||
float sink_voltage_intercept = 0;
|
||||
float sink_voltage_intercept;
|
||||
|
||||
/**
|
||||
* Lower voltage boundary where this bus may be used to source current.
|
||||
@@ -62,33 +63,33 @@ public:
|
||||
* This value is the voltage at zero current. Values for other currents are calculated
|
||||
* using the droop resistance.
|
||||
*/
|
||||
float src_voltage_intercept = 0;
|
||||
float src_voltage_intercept;
|
||||
|
||||
/**
|
||||
* Droop resistance to adjust voltage bounds for current in sourcing direction
|
||||
*
|
||||
* control voltage = nominal voltage - droop_res * current
|
||||
*/
|
||||
float src_droop_res = 0;
|
||||
float src_droop_res;
|
||||
|
||||
/**
|
||||
* Droop resistance to adjust voltage bounds for current in sinking direction
|
||||
*
|
||||
* control voltage = nominal voltage - droop_res * current
|
||||
*/
|
||||
float sink_droop_res = 0;
|
||||
float sink_droop_res;
|
||||
|
||||
/**
|
||||
* Pointer to current measurement that is used to determine the droop. This is typically the
|
||||
* battery or nanogrid terminal.
|
||||
*/
|
||||
float *ref_current = NULL;
|
||||
float *ref_current;
|
||||
|
||||
/**
|
||||
* Available additional current towards the DC bus until limits of the connected ports are
|
||||
* reached
|
||||
*/
|
||||
float sink_current_margin = 0;
|
||||
float sink_current_margin;
|
||||
|
||||
/**
|
||||
* Available additional current from the DC bus until limits of the connected ports are reached
|
||||
@@ -98,57 +99,11 @@ public:
|
||||
* we still get power via solar, current up to the amount of solar input can be diverted into
|
||||
* the load (until the margin gets 0 and the battery would be discharged).
|
||||
*/
|
||||
float src_current_margin = 0;
|
||||
|
||||
/**
|
||||
* Calculate current-compensated src control voltage, considering droop and series multiplier
|
||||
*
|
||||
* @param voltage_zero_current Voltage at zero current (without droop). If this parameter is
|
||||
* left empty, the src_voltage_intercept is considered.
|
||||
*/
|
||||
inline float src_control_voltage(float voltage_zero_current = 0)
|
||||
{
|
||||
float v0 = (voltage_zero_current == 0) ? src_voltage_intercept : voltage_zero_current;
|
||||
if (ref_current != nullptr) {
|
||||
return (v0 - src_droop_res * (*ref_current)) * series_multiplier;
|
||||
}
|
||||
else {
|
||||
return v0 * series_multiplier;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate current-compensated sink control voltage, considering droop and series multiplier
|
||||
*
|
||||
* @param voltage_zero_current Voltage at zero current (without droop). If this parameter is
|
||||
* left empty, the sink_voltage_intercept is considered.
|
||||
*/
|
||||
inline float sink_control_voltage(float voltage_zero_current = 0)
|
||||
{
|
||||
float v0 = (voltage_zero_current == 0) ? sink_voltage_intercept : voltage_zero_current;
|
||||
if (ref_current != nullptr) {
|
||||
return (v0 - sink_droop_res * (*ref_current)) * series_multiplier;
|
||||
}
|
||||
else {
|
||||
return v0 * series_multiplier;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate voltage for series connected batteries based on setpoint for single battery
|
||||
*
|
||||
* @param single_voltage Voltage for single battery
|
||||
*
|
||||
* @returns Total voltage
|
||||
*/
|
||||
inline float series_voltage(float single_voltage)
|
||||
{
|
||||
return single_voltage * series_multiplier;
|
||||
}
|
||||
float src_current_margin;
|
||||
};
|
||||
|
||||
/**
|
||||
* Power Port class
|
||||
* Power port
|
||||
*
|
||||
* Stores current measurements and limits for external terminals or internal ports
|
||||
*
|
||||
@@ -178,18 +133,17 @@ public:
|
||||
* a negative current at the high voltage side. The system boundary is the DC/DC sub-circuit,
|
||||
* which sources current from the high side port and sinks it through the low side port.
|
||||
*/
|
||||
class PowerPort
|
||||
struct power_port
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Each power port is connected to a DC bus, containing relevant voltage information
|
||||
*/
|
||||
DcBus *bus = NULL;
|
||||
struct dc_bus *bus;
|
||||
|
||||
/**
|
||||
* Measured current through this port (positive sign = current into the external device)
|
||||
*/
|
||||
float current = 0;
|
||||
float current;
|
||||
|
||||
/**
|
||||
* Filtered port current
|
||||
@@ -197,71 +151,140 @@ public:
|
||||
* Suitable filter time constants depend on type of port. This filtering is applied in
|
||||
* addition to the raw ADC value filtering.
|
||||
*/
|
||||
float current_filtered = 0;
|
||||
float current_filtered;
|
||||
|
||||
/**
|
||||
* Product of port current and bus voltage
|
||||
*/
|
||||
float power = 0;
|
||||
float power;
|
||||
|
||||
/**
|
||||
* Maximum positive current (valid values >= 0.0)
|
||||
*/
|
||||
float pos_current_limit = 0;
|
||||
float pos_current_limit;
|
||||
|
||||
/**
|
||||
* Maximum negative current (valid values <= 0.0)
|
||||
*/
|
||||
float neg_current_limit = 0;
|
||||
float neg_current_limit;
|
||||
|
||||
/**
|
||||
* Cumulated energy in positive current direction since last counter reset (Wh)
|
||||
*/
|
||||
float pos_energy_Wh = 0;
|
||||
float pos_energy_Wh;
|
||||
|
||||
/**
|
||||
* Cumulated energy in negative current direction since last counter reset (Wh)
|
||||
*/
|
||||
float neg_energy_Wh = 0;
|
||||
|
||||
/**
|
||||
* Constructor assigning the port to a DC bus
|
||||
*
|
||||
* @param dc_bus The DC bus this port is assigned to
|
||||
* @param assign_ref_current defines if the bus ref_current should point to the current
|
||||
* of this port (must be true for at least one port)
|
||||
*/
|
||||
PowerPort(DcBus *dc_bus, bool assign_ref_current = false) : bus(dc_bus)
|
||||
{
|
||||
if (assign_ref_current) {
|
||||
bus->ref_current = ¤t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize power port for solar panel connection
|
||||
*/
|
||||
void init_solar();
|
||||
|
||||
/**
|
||||
* Initialize power port for nanogrid connection
|
||||
*/
|
||||
void init_nanogrid();
|
||||
|
||||
/**
|
||||
* Energy balance calculation for power port
|
||||
*
|
||||
* Must be called exactly once per second, otherwise energy calculation gets wrong.
|
||||
*/
|
||||
void energy_balance();
|
||||
|
||||
/**
|
||||
* Sets current limits for control of the bus voltage
|
||||
*
|
||||
* 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() const;
|
||||
float neg_energy_Wh;
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize a DC bus
|
||||
*
|
||||
* All values are set to zero, except for the series multiplier, which is set to 1.
|
||||
*
|
||||
* @param bus DC bus
|
||||
*/
|
||||
void dc_bus_init(struct dc_bus *bus);
|
||||
|
||||
/**
|
||||
* Calculate current-compensated src control voltage, considering droop and series multiplier
|
||||
*
|
||||
* @param bus DC bus
|
||||
* @param voltage_zero_current Voltage at zero current (without droop). If this parameter is
|
||||
* 0, the src_voltage_intercept is considered.
|
||||
*/
|
||||
static inline float dc_bus_src_control_voltage(const struct dc_bus *bus, float voltage_zero_current)
|
||||
{
|
||||
float v0 = (voltage_zero_current == 0) ? bus->src_voltage_intercept : voltage_zero_current;
|
||||
if (bus->ref_current != NULL) {
|
||||
return (v0 - bus->src_droop_res * (*bus->ref_current)) * bus->series_multiplier;
|
||||
}
|
||||
else {
|
||||
return v0 * bus->series_multiplier;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate current-compensated sink control voltage, considering droop and series multiplier
|
||||
*
|
||||
* @param bus DC bus
|
||||
* @param voltage_zero_current Voltage at zero current (without droop). If this parameter is
|
||||
* 0, the sink_voltage_intercept is considered.
|
||||
*/
|
||||
static inline float dc_bus_sink_control_voltage(const struct dc_bus *bus,
|
||||
float voltage_zero_current)
|
||||
{
|
||||
float v0 = (voltage_zero_current == 0) ? bus->sink_voltage_intercept : voltage_zero_current;
|
||||
if (bus->ref_current != NULL) {
|
||||
return (v0 - bus->sink_droop_res * (*bus->ref_current)) * bus->series_multiplier;
|
||||
}
|
||||
else {
|
||||
return v0 * bus->series_multiplier;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate voltage for series connected batteries based on setpoint for single battery
|
||||
*
|
||||
* @param bus DC bus
|
||||
* @param single_voltage Voltage for single battery
|
||||
*
|
||||
* @returns Total voltage
|
||||
*/
|
||||
static inline float dc_bus_series_voltage(const struct dc_bus *bus, float single_voltage)
|
||||
{
|
||||
return single_voltage * bus->series_multiplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a power port and assign it to a DC bus
|
||||
*
|
||||
* All values are set to zero.
|
||||
*
|
||||
* @param port Power port
|
||||
* @param bus The DC bus this port is assigned to
|
||||
* @param assign_ref_current defines if the bus ref_current should point to the current
|
||||
* of this port (must be true for at least one port)
|
||||
*/
|
||||
void power_port_init(struct power_port *port, struct dc_bus *bus, bool assign_ref_current);
|
||||
|
||||
/**
|
||||
* Initialize power port for solar panel connection
|
||||
*
|
||||
* @param port Power port
|
||||
*/
|
||||
void power_port_init_solar(struct power_port *port);
|
||||
|
||||
/**
|
||||
* Initialize power port for nanogrid connection
|
||||
*
|
||||
* @param port Power port
|
||||
*/
|
||||
void power_port_init_nanogrid(struct power_port *port);
|
||||
|
||||
/**
|
||||
* Energy balance calculation for power port
|
||||
*
|
||||
* Must be called exactly once per second, otherwise energy calculation gets wrong.
|
||||
*
|
||||
* @param port Power port
|
||||
*/
|
||||
void power_port_energy_balance(struct power_port *port);
|
||||
|
||||
/**
|
||||
* Sets current limits for control of the bus voltage
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @param port Power port
|
||||
*/
|
||||
void power_port_update_bus_current_margins(const struct power_port *port);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* POWER_PORT_H */
|
||||
|
||||
+66
-40
@@ -13,50 +13,74 @@
|
||||
|
||||
#include "daq.h"
|
||||
#include "helper.h"
|
||||
#include "mcu.h"
|
||||
#include "setup.h"
|
||||
|
||||
#include <libresolar/drivers/power_switch.h>
|
||||
|
||||
LOG_MODULE_REGISTER(pwm_switch, CONFIG_PWM_LOG_LEVEL);
|
||||
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
|
||||
#define PWM_CURRENT_MAX (DT_PROP(DT_CHILD(DT_PATH(outputs), pwm_switch), current_max))
|
||||
#define PWM_PERIOD (DT_PHA(DT_CHILD(DT_PATH(outputs), pwm_switch), pwms, period))
|
||||
#define PWM_CURRENT_MAX (DT_PROP(PWM_SWITCH_NODE, current_max))
|
||||
|
||||
// duty cycle step corresponding to 100 us on-time
|
||||
#define PWM_DUTY_STEP (DT_PROP(PWM_SWITCH_NODE, frequency) / 10000.0F)
|
||||
|
||||
bool PwmSwitch::active()
|
||||
{
|
||||
return pwm_active();
|
||||
return power_switch_is_on(power_switch);
|
||||
}
|
||||
|
||||
bool PwmSwitch::signal_high()
|
||||
{
|
||||
return pwm_signal_high();
|
||||
return power_switch_is_conducting(power_switch);
|
||||
}
|
||||
|
||||
PwmSwitch::PwmSwitch(DcBus *dc_bus) : PowerPort(dc_bus)
|
||||
PwmSwitch::PwmSwitch(struct dc_bus *dc_bus, const struct device *sw) : power_switch(sw)
|
||||
{
|
||||
// period stored in nanoseconds
|
||||
pwm_signal_init_registers(1000 * 1000 * 1000 / PWM_PERIOD);
|
||||
power_port_init(this, dc_bus, false);
|
||||
|
||||
if (!device_is_ready(power_switch)) {
|
||||
LOG_ERR("PWM switch not ready");
|
||||
enable = false;
|
||||
return;
|
||||
}
|
||||
|
||||
power_switch_set_callback(power_switch, POWER_SWITCH_EVENT_PWM_EDGE, pwm_edge_callback, this);
|
||||
}
|
||||
|
||||
void PwmSwitch::pwm_edge_callback(const struct device *dev, uint32_t events, void *user_data)
|
||||
{
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 10 ms
|
||||
// at each rising edge if switch is not continuously on
|
||||
daq_inhibit_alert(DAQ_DEV, DAQ_CH(v_low), DAQ_ALERT_UPPER, 10);
|
||||
}
|
||||
|
||||
void PwmSwitch::duty_cycle_step(int steps)
|
||||
{
|
||||
// setting is ignored by the driver if the new duty cycle is out of range
|
||||
power_switch_set_duty_cycle(power_switch, get_duty_cycle() + steps * PWM_DUTY_STEP);
|
||||
}
|
||||
|
||||
void PwmSwitch::test()
|
||||
{
|
||||
if (pwm_active() && enable == false) {
|
||||
pwm_signal_stop();
|
||||
if (active() && enable == false) {
|
||||
power_switch_off(power_switch);
|
||||
off_timestamp = uptime();
|
||||
LOG_INF("PWM test mode stop.");
|
||||
}
|
||||
else if (!pwm_active() && enable == true) {
|
||||
else if (!active() && enable == true) {
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 50 ms
|
||||
adc_upper_alert_inhibit(ADC_POS(v_low), 50);
|
||||
pwm_signal_start(0.9F);
|
||||
daq_inhibit_alert(DAQ_DEV, DAQ_CH(v_low), DAQ_ALERT_UPPER, 50);
|
||||
power_switch_set_duty_cycle(power_switch, 0.9F);
|
||||
power_switch_on(power_switch);
|
||||
LOG_INF("PWM test mode start.");
|
||||
}
|
||||
}
|
||||
|
||||
void PwmSwitch::control()
|
||||
{
|
||||
if (pwm_active()) {
|
||||
if (active()) {
|
||||
if (current < -0.1F) {
|
||||
power_good_timestamp = uptime(); // reset the time
|
||||
}
|
||||
@@ -66,75 +90,77 @@ void PwmSwitch::control()
|
||||
|| bus->voltage < 9.0F // not enough voltage for MOSFET drivers anymore
|
||||
|| enable == false)
|
||||
{
|
||||
pwm_signal_stop();
|
||||
power_switch_off(power_switch);
|
||||
off_timestamp = uptime();
|
||||
LOG_INF("PWM charger stop, current = %d mA", (int)(current * 1000.0F));
|
||||
}
|
||||
else if (bus->voltage > bus->sink_control_voltage() + 0.3F) {
|
||||
pwm_signal_stop();
|
||||
else if (bus->voltage > dc_bus_sink_control_voltage(bus, 0) + 0.3F) {
|
||||
power_switch_off(power_switch);
|
||||
off_timestamp = uptime();
|
||||
dev_stat.set_error(ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
device_status_set_error(&dev_stat, ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
LOG_INF("PWM charger stop, overvoltage.");
|
||||
}
|
||||
else if (bus->voltage > bus->sink_control_voltage() // bus voltage above target
|
||||
|| current < neg_current_limit // port current limit exceeded
|
||||
|| current < -PWM_CURRENT_MAX) // PCB current limit exceeded
|
||||
else if (bus->voltage > dc_bus_sink_control_voltage(bus, 0) // bus voltage above target
|
||||
|| current < neg_current_limit // port current limit exceeded
|
||||
|| current < -PWM_CURRENT_MAX) // PCB current limit exceeded
|
||||
{
|
||||
// decrease power, as limits were reached
|
||||
|
||||
// the gate driver switch-off time is quite high (fall time around 1ms), so very short
|
||||
// on or off periods (duty cycle close to 0 and 1) should be avoided
|
||||
if (pwm_signal_get_duty_cycle() > 0.95F) {
|
||||
if (get_duty_cycle() > 0.95F) {
|
||||
// prevent very short off periods
|
||||
pwm_signal_set_duty_cycle(0.95F);
|
||||
power_switch_set_duty_cycle(power_switch, 0.95F);
|
||||
}
|
||||
else if (pwm_signal_get_duty_cycle() < 0.05F) {
|
||||
else if (get_duty_cycle() < 0.05F) {
|
||||
// prevent very short on periods and switch completely off instead
|
||||
pwm_signal_stop();
|
||||
power_switch_off(power_switch);
|
||||
off_timestamp = uptime();
|
||||
// consider this as overvoltage in order to start again with 5% duty cycle
|
||||
dev_stat.set_error(ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
device_status_set_error(&dev_stat, ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
LOG_INF("PWM charger stop, no further derating possible.");
|
||||
}
|
||||
else {
|
||||
// decrease the power with large steps to prevent long-term overvoltages if
|
||||
// the PWM switch was started with full battery and high solar irradiation
|
||||
pwm_signal_duty_cycle_step(-10);
|
||||
duty_cycle_step(-10);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// increase power (if not yet at 100% duty cycle)
|
||||
|
||||
if (pwm_signal_get_duty_cycle() > 0.95F) {
|
||||
if (get_duty_cycle() > 0.95F) {
|
||||
// prevent very short off periods and switch completely on instead
|
||||
pwm_signal_set_duty_cycle(1);
|
||||
power_switch_set_duty_cycle(power_switch, 1);
|
||||
}
|
||||
else {
|
||||
pwm_signal_duty_cycle_step(1);
|
||||
duty_cycle_step(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (dev_stat.has_error(ERR_PWM_SWITCH_OVERVOLTAGE)
|
||||
&& bus->voltage < bus->sink_control_voltage() - 0.5F)
|
||||
if (device_status_has_error(&dev_stat, ERR_PWM_SWITCH_OVERVOLTAGE)
|
||||
&& bus->voltage < dc_bus_sink_control_voltage(bus, 0) - 0.5F)
|
||||
{
|
||||
dev_stat.clear_error(ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
device_status_clear_error(&dev_stat, ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (bus->sink_current_margin > 0 && // charging allowed
|
||||
bus->voltage < bus->sink_control_voltage()
|
||||
bus->voltage < dc_bus_sink_control_voltage(bus, 0)
|
||||
&& ext_voltage > bus->voltage + offset_voltage_start
|
||||
&& uptime() > (off_timestamp + restart_interval) && enable == true)
|
||||
{
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 50 ms
|
||||
adc_upper_alert_inhibit(ADC_POS(v_low), 50);
|
||||
daq_inhibit_alert(DAQ_DEV, DAQ_CH(v_low), DAQ_ALERT_UPPER, 50);
|
||||
|
||||
if (dev_stat.has_error(ERR_PWM_SWITCH_OVERVOLTAGE)) {
|
||||
if (device_status_has_error(&dev_stat, ERR_PWM_SWITCH_OVERVOLTAGE)) {
|
||||
// start with minimum duty cycle in order to prevent another overvoltage event
|
||||
pwm_signal_start(0.05F);
|
||||
power_switch_set_duty_cycle(power_switch, 0.05F);
|
||||
power_switch_on(power_switch);
|
||||
}
|
||||
else {
|
||||
pwm_signal_start(1.0F);
|
||||
power_switch_set_duty_cycle(power_switch, 1.0F);
|
||||
power_switch_on(power_switch);
|
||||
}
|
||||
|
||||
power_good_timestamp = uptime();
|
||||
@@ -145,13 +171,13 @@ void PwmSwitch::control()
|
||||
|
||||
void PwmSwitch::stop()
|
||||
{
|
||||
pwm_signal_stop();
|
||||
power_switch_off(power_switch);
|
||||
off_timestamp = uptime();
|
||||
}
|
||||
|
||||
float PwmSwitch::get_duty_cycle()
|
||||
{
|
||||
return pwm_signal_get_duty_cycle();
|
||||
return power_switch_get_duty_cycle(power_switch);
|
||||
}
|
||||
|
||||
#endif /* BOARD_HAS_PWM_PORT */
|
||||
|
||||
+22
-65
@@ -22,15 +22,17 @@
|
||||
|
||||
#include "power_port.h"
|
||||
|
||||
#include <zephyr/device.h>
|
||||
|
||||
/**
|
||||
* PWM charger type
|
||||
*
|
||||
* Contains all data belonging to the PWM switching sub-component.
|
||||
*/
|
||||
class PwmSwitch : public PowerPort
|
||||
class PwmSwitch : public power_port
|
||||
{
|
||||
public:
|
||||
PwmSwitch(DcBus *dc_bus);
|
||||
PwmSwitch(struct dc_bus *dc_bus, const struct device *power_switch);
|
||||
|
||||
/**
|
||||
* Main control function for the PWM switching algorithm
|
||||
@@ -106,70 +108,25 @@ public:
|
||||
* Last time the current through the switch was above minimum
|
||||
*/
|
||||
time_t power_good_timestamp;
|
||||
|
||||
private:
|
||||
/**
|
||||
* Increase or decrease the duty cycle by the specified number of steps
|
||||
*
|
||||
* The duty cycle is not changed if the result would be out of range.
|
||||
*/
|
||||
void duty_cycle_step(int steps);
|
||||
|
||||
/**
|
||||
* Callback for the rising edges of the PWM signal
|
||||
*/
|
||||
static void pwm_edge_callback(const struct device *dev, uint32_t events, void *user_data);
|
||||
|
||||
/**
|
||||
* Power switch device
|
||||
*/
|
||||
const struct device *power_switch;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Read the currently set duty cycle
|
||||
*
|
||||
* @returns Duty cycle between 0.0 and 1.0
|
||||
*/
|
||||
float pwm_signal_get_duty_cycle();
|
||||
|
||||
/**
|
||||
* Set the duty cycle of the PWM signal
|
||||
*
|
||||
* @param duty Duty cycle between 0.0 and 1.0
|
||||
*/
|
||||
void pwm_signal_set_duty_cycle(float duty);
|
||||
|
||||
/**
|
||||
* Change raw timer capture/compare register by defined step
|
||||
*
|
||||
* @param delta Steps to be added/substracted from current CCR value
|
||||
*/
|
||||
void pwm_signal_duty_cycle_step(int delta);
|
||||
|
||||
/**
|
||||
* Initiatializes the registers to generate the PWM signal and sets duty
|
||||
* cycle limits
|
||||
*
|
||||
* @param freq_Hz Switching frequency in Hz
|
||||
*/
|
||||
void pwm_signal_init_registers(int freq_Hz);
|
||||
|
||||
/**
|
||||
* Start the PWM generation
|
||||
*
|
||||
* @param duty Duty cycle between 0.0 and 1.0
|
||||
*/
|
||||
void pwm_signal_start(float pwm_duty);
|
||||
|
||||
/**
|
||||
* Stop the PWM generation
|
||||
*/
|
||||
void pwm_signal_stop();
|
||||
|
||||
/**
|
||||
* Check if the PWM pin voltage level is high (on-state)
|
||||
*
|
||||
* @returns true if pin is high, false if pin is low
|
||||
*/
|
||||
bool pwm_signal_high();
|
||||
|
||||
/**
|
||||
* Check if the PWM generation is active
|
||||
*
|
||||
* @returns true if PWM signal generation is active
|
||||
*/
|
||||
bool pwm_active();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* PWM_SWITCH_H */
|
||||
|
||||
@@ -1,188 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "pwm_switch.h"
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "board.h"
|
||||
#include "daq.h"
|
||||
#include "helper.h"
|
||||
#include "mcu.h"
|
||||
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#include <stm32_ll_bus.h>
|
||||
#include <stm32_ll_rcc.h>
|
||||
#include <stm32_ll_tim.h>
|
||||
|
||||
#include <soc.h>
|
||||
#include <zephyr/drivers/pinctrl.h>
|
||||
|
||||
// all PWM charge controllers use TIM3 at the moment
|
||||
static TIM_TypeDef *tim = TIM3;
|
||||
|
||||
#if DT_PWMS_CHANNEL(DT_CHILD(DT_PATH(outputs), pwm_switch)) == 1
|
||||
#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH1
|
||||
#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH1
|
||||
#define LL_TIM_OC_GetCompare LL_TIM_OC_GetCompareCH1
|
||||
#elif DT_PWMS_CHANNEL(DT_CHILD(DT_PATH(outputs), pwm_switch)) == 2
|
||||
#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH2
|
||||
#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH2
|
||||
#define LL_TIM_OC_GetCompare LL_TIM_OC_GetCompareCH2
|
||||
#elif DT_PWMS_CHANNEL(DT_CHILD(DT_PATH(outputs), pwm_switch)) == 3
|
||||
#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH3
|
||||
#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH3
|
||||
#define LL_TIM_OC_GetCompare LL_TIM_OC_GetCompareCH3
|
||||
#elif DT_PWMS_CHANNEL(DT_CHILD(DT_PATH(outputs), pwm_switch)) == 4
|
||||
#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH4
|
||||
#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH4
|
||||
#define LL_TIM_OC_GetCompare LL_TIM_OC_GetCompareCH4
|
||||
#else
|
||||
#error "PWM Switch channel not defined properly!"
|
||||
#endif
|
||||
|
||||
// to check if PWM signal is high or low (not sure how to get pin config from devicetree...)
|
||||
#if defined(CONFIG_BOARD_PWM_2420_LUS)
|
||||
#define PWM_GPIO_PIN_HIGH (GPIOB->IDR & GPIO_IDR_ID1)
|
||||
#elif defined(CONFIG_BOARD_MPPT_2420_HPX)
|
||||
#define PWM_GPIO_PIN_HIGH (GPIOC->IDR & GPIO_IDR_ID7)
|
||||
#endif
|
||||
|
||||
#define PINCTRL_NODE DT_NODELABEL(pwm3)
|
||||
|
||||
PINCTRL_DT_DEFINE(PINCTRL_NODE);
|
||||
|
||||
static const struct pinctrl_dev_config *pincfg = PINCTRL_DT_DEV_CONFIG_GET(PINCTRL_NODE);
|
||||
|
||||
static bool _pwm_active;
|
||||
static int _pwm_resolution;
|
||||
|
||||
static void TIM3_IRQHandler(void *args)
|
||||
{
|
||||
LL_TIM_ClearFlag_UPDATE(tim);
|
||||
|
||||
if ((int)LL_TIM_OC_GetCompare(tim) < _pwm_resolution) {
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 10 ms
|
||||
// at each rising edge if switch is not continuously on
|
||||
adc_upper_alert_inhibit(ADC_POS(v_low), 10);
|
||||
}
|
||||
}
|
||||
|
||||
void pwm_signal_init_registers(int freq_Hz)
|
||||
{
|
||||
pinctrl_apply_state(pincfg, PINCTRL_STATE_DEFAULT);
|
||||
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
|
||||
|
||||
// Set timer clock to 10 kHz
|
||||
LL_TIM_SetPrescaler(TIM3, SystemCoreClock / 10000 - 1);
|
||||
|
||||
LL_TIM_OC_SetMode(tim, LL_TIM_CHANNEL, LL_TIM_OCMODE_PWM1);
|
||||
LL_TIM_OC_EnablePreload(tim, LL_TIM_CHANNEL);
|
||||
LL_TIM_OC_SetPolarity(tim, LL_TIM_CHANNEL, LL_TIM_OCPOLARITY_HIGH);
|
||||
|
||||
// Interrupt on timer update
|
||||
LL_TIM_EnableIT_UPDATE(tim);
|
||||
|
||||
// Force update generation (UG = 1)
|
||||
LL_TIM_GenerateEvent_UPDATE(tim);
|
||||
|
||||
// set PWM frequency and resolution
|
||||
_pwm_resolution = 10000 / freq_Hz;
|
||||
|
||||
// Period goes from 0 to ARR (including ARR value), so substract 1 clock cycle
|
||||
LL_TIM_SetAutoReload(tim, _pwm_resolution - 1);
|
||||
|
||||
// 1 = second-highest priority of STM32L0/F0
|
||||
IRQ_CONNECT(TIM3_IRQn, 1, TIM3_IRQHandler, 0, 0);
|
||||
irq_enable(TIM3_IRQn);
|
||||
|
||||
LL_TIM_EnableCounter(tim);
|
||||
}
|
||||
|
||||
void pwm_signal_set_duty_cycle(float duty)
|
||||
{
|
||||
LL_TIM_OC_SetCompare(tim, _pwm_resolution * duty);
|
||||
}
|
||||
|
||||
void pwm_signal_duty_cycle_step(int delta)
|
||||
{
|
||||
uint32_t ccr_new = (int)LL_TIM_OC_GetCompare(tim) + delta;
|
||||
if (ccr_new <= _pwm_resolution && ccr_new >= 0) {
|
||||
LL_TIM_OC_SetCompare(tim, ccr_new);
|
||||
}
|
||||
}
|
||||
|
||||
float pwm_signal_get_duty_cycle()
|
||||
{
|
||||
return (float)(LL_TIM_OC_GetCompare(tim)) / _pwm_resolution;
|
||||
}
|
||||
|
||||
void pwm_signal_start(float pwm_duty)
|
||||
{
|
||||
pwm_signal_set_duty_cycle(pwm_duty);
|
||||
LL_TIM_CC_EnableChannel(tim, LL_TIM_CHANNEL);
|
||||
_pwm_active = true;
|
||||
}
|
||||
|
||||
void pwm_signal_stop()
|
||||
{
|
||||
LL_TIM_CC_DisableChannel(tim, LL_TIM_CHANNEL);
|
||||
_pwm_active = false;
|
||||
}
|
||||
|
||||
bool pwm_signal_high()
|
||||
{
|
||||
return PWM_GPIO_PIN_HIGH;
|
||||
}
|
||||
|
||||
bool pwm_active()
|
||||
{
|
||||
return _pwm_active;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// dummy functions for unit tests
|
||||
float pwm_signal_get_duty_cycle()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pwm_signal_set_duty_cycle(float duty)
|
||||
{}
|
||||
|
||||
void pwm_signal_duty_cycle_step(int delta)
|
||||
{}
|
||||
|
||||
void pwm_signal_init_registers(int freq_Hz)
|
||||
{}
|
||||
|
||||
void pwm_signal_start(float pwm_duty)
|
||||
{}
|
||||
|
||||
void pwm_signal_stop()
|
||||
{}
|
||||
|
||||
bool pwm_signal_high()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool pwm_active()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* BOARD_HAS_PWM_PORT */
|
||||
+41
-24
@@ -19,55 +19,63 @@
|
||||
#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 "half_bridge.h" // PWM generation for DC/DC converter
|
||||
#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
|
||||
#include "pwm_switch.h" // PWM charge controller
|
||||
|
||||
DcBus lv_bus;
|
||||
PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT)
|
||||
// low voltage terminal (battery for typical MPPT)
|
||||
extern struct power_port lv_terminal;
|
||||
struct dc_bus lv_bus = { .series_multiplier = 1, .ref_current = &lv_terminal.current };
|
||||
struct power_port lv_terminal = { .bus = &lv_bus };
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
DcBus hv_bus;
|
||||
PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT)
|
||||
// high voltage terminal (solar for typical MPPT)
|
||||
extern struct power_port hv_terminal;
|
||||
struct dc_bus hv_bus = { .series_multiplier = 1, .ref_current = &hv_terminal.current };
|
||||
struct power_port hv_terminal = { .bus = &hv_bus };
|
||||
struct dcdc dcdc;
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
Dcdc dcdc(&hv_bus, &lv_bus, DCDC_MODE_AUTO);
|
||||
#define DCDC_MODE DCDC_MODE_AUTO
|
||||
#elif CONFIG_HV_TERMINAL_BATTERY
|
||||
Dcdc dcdc(&hv_bus, &lv_bus, DCDC_MODE_BOOST);
|
||||
#define DCDC_MODE DCDC_MODE_BOOST
|
||||
#else
|
||||
Dcdc dcdc(&hv_bus, &lv_bus, DCDC_MODE_BUCK);
|
||||
#define DCDC_MODE DCDC_MODE_BUCK
|
||||
#endif // CONFIG_HV_TERMINAL
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
PwmSwitch pwm_switch(&lv_bus);
|
||||
PwmSwitch pwm_switch(&lv_bus, DEVICE_DT_GET(PWM_SWITCH_NODE));
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
LoadOutput load(&lv_bus, &load_out_set, &load_out_init, NULL);
|
||||
#if LED_EXISTS(load)
|
||||
LoadOutput load(&lv_bus, DEVICE_DT_GET(LOAD_SWITCH_NODE), LED_POS(load));
|
||||
#else
|
||||
LoadOutput load(&lv_bus, DEVICE_DT_GET(LOAD_SWITCH_NODE));
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_USB_OUTPUT
|
||||
LoadOutput usb_pwr(&lv_bus, &usb_out_set, &usb_out_init, &pgood_check);
|
||||
LoadOutput usb_pwr(&lv_bus, DEVICE_DT_GET(USB_SWITCH_NODE));
|
||||
#endif
|
||||
|
||||
#if CONFIG_HV_TERMINAL_SOLAR
|
||||
PowerPort &solar_terminal = hv_terminal;
|
||||
struct power_port &solar_terminal = hv_terminal;
|
||||
#elif CONFIG_LV_TERMINAL_SOLAR
|
||||
PowerPort &solar_terminal = lv_terminal;
|
||||
struct power_port &solar_terminal = lv_terminal;
|
||||
#elif CONFIG_PWM_TERMINAL_SOLAR
|
||||
PowerPort &solar_terminal = pwm_switch;
|
||||
struct power_port &solar_terminal = pwm_switch;
|
||||
#endif
|
||||
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
PowerPort &grid_terminal = hv_terminal;
|
||||
struct power_port &grid_terminal = hv_terminal;
|
||||
#endif
|
||||
|
||||
#if CONFIG_LV_TERMINAL_BATTERY
|
||||
PowerPort &bat_terminal = lv_terminal;
|
||||
struct power_port &bat_terminal = lv_terminal;
|
||||
#elif CONFIG_HV_TERMINAL_BATTERY
|
||||
PowerPort &bat_terminal = hv_terminal;
|
||||
struct power_port &bat_terminal = hv_terminal;
|
||||
#endif
|
||||
|
||||
Charger charger(&bat_terminal);
|
||||
@@ -75,15 +83,29 @@ 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;
|
||||
struct device_status 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;
|
||||
|
||||
/*
|
||||
* Initialize the charge controller objects after all devices are ready (also used by the unit
|
||||
* tests, which don't call main())
|
||||
*/
|
||||
static int setup_objects(void)
|
||||
{
|
||||
#if BOARD_HAS_DCDC
|
||||
dcdc_init(&dcdc, DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SYS_INIT(setup_objects, APPLICATION, 0);
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#include <soc.h>
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
static inline void timestamp_inc(struct k_timer *timer_id)
|
||||
@@ -104,11 +126,6 @@ void setup()
|
||||
*/
|
||||
void *temp = k_malloc(4);
|
||||
k_free(temp);
|
||||
|
||||
#ifdef CONFIG_SOC_SERIES_STM32G4X
|
||||
// disable 5k pull-down required for USB-C PD on PB4 and PB6 so that they can be used as inputs
|
||||
PWR->CR3 |= PWR_CR3_UCPD_DBDIS;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+10
-9
@@ -10,30 +10,31 @@
|
||||
|
||||
#include "bat_charger.h"
|
||||
#include "board.h"
|
||||
#include "dcdc.h"
|
||||
#include "device_status.h"
|
||||
#include "load.h"
|
||||
#include "power_port.h"
|
||||
#include "pwm_switch.h"
|
||||
#include "thingset.h"
|
||||
|
||||
extern DcBus lv_bus;
|
||||
extern PowerPort lv_terminal;
|
||||
extern struct dc_bus lv_bus;
|
||||
extern struct power_port lv_terminal;
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
extern DcBus hv_bus;
|
||||
extern PowerPort hv_terminal;
|
||||
extern Dcdc dcdc;
|
||||
extern struct dc_bus hv_bus;
|
||||
extern struct power_port hv_terminal;
|
||||
extern struct dcdc dcdc;
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
extern PwmSwitch pwm_switch;
|
||||
#endif
|
||||
|
||||
extern PowerPort &bat_terminal;
|
||||
extern PowerPort &solar_terminal;
|
||||
extern PowerPort &grid_terminal;
|
||||
extern struct power_port &bat_terminal;
|
||||
extern struct power_port &solar_terminal;
|
||||
extern struct power_port &grid_terminal;
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
extern struct device_status dev_stat;
|
||||
extern Charger charger;
|
||||
extern BatConf bat_conf;
|
||||
extern BatConf bat_conf_user;
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
|
||||
if BOARD_MPPT_1210_HUS
|
||||
|
||||
config LIBRE_SOLAR_TYPE_ID
|
||||
default 4
|
||||
|
||||
if SPI
|
||||
|
||||
config SPI_STM32_INTERRUPT
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
hs-voltage-max = <40>;
|
||||
ls-voltage-max = <16>;
|
||||
dcdc-current-max = <10>;
|
||||
ls-fuse;
|
||||
};
|
||||
|
||||
chosen {
|
||||
@@ -57,26 +58,33 @@
|
||||
};
|
||||
};
|
||||
|
||||
load_switch: load-switch {
|
||||
compatible = "libresolar,gpio-power-switch";
|
||||
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
|
||||
current-max = <10>;
|
||||
};
|
||||
|
||||
usb_switch: usb-switch {
|
||||
compatible = "libresolar,gpio-power-switch";
|
||||
gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
|
||||
outputs {
|
||||
compatible = "outputs";
|
||||
|
||||
load {
|
||||
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
|
||||
current-max = <10>;
|
||||
kconfig-flag; /* make node visible in Kconfig */
|
||||
};
|
||||
|
||||
usb-pwr {
|
||||
gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
|
||||
boot0 {
|
||||
gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
};
|
||||
|
||||
ntc_bat: ntc-bat {
|
||||
compatible = "libresolar,daq-ntc-thermistor";
|
||||
daq-channel = <&daq_temp_bat>;
|
||||
pullup-ohm = <10000>;
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
compatible = "libresolar,stm32-daq";
|
||||
|
||||
v-low {
|
||||
io-channels = <&adc1 0>;
|
||||
@@ -105,9 +113,9 @@
|
||||
divider = <150>; // 3*50
|
||||
};
|
||||
|
||||
temp-bat {
|
||||
daq_temp_bat: temp-bat {
|
||||
io-channels = <&adc1 7>;
|
||||
multiplier = <10000>;
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
@@ -173,7 +181,7 @@
|
||||
bias-pull-up; // avoid junk characters if pin is left floating
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
uext_i2c: &i2c1 {
|
||||
pinctrl-0 = <&i2c1_scl_pb6 &i2c1_sda_pb7>;
|
||||
pinctrl-names = "default";
|
||||
clock-frequency = <I2C_BITRATE_FAST>;
|
||||
@@ -195,11 +203,13 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-gptim-half-bridge";
|
||||
pinctrl-0 = <&tim3_ch3_pb0 &tim3_ch4_pb1>;
|
||||
pinctrl-names = "default";
|
||||
high-side-channel = <3>;
|
||||
low-side-channel = <4>;
|
||||
frequency = <50000>;
|
||||
deadtime = <230>;
|
||||
deadtime-ns = <230>;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -42,20 +42,12 @@
|
||||
outputs {
|
||||
compatible = "outputs";
|
||||
|
||||
load {
|
||||
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
|
||||
usb-pwr {
|
||||
gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
|
||||
/delete-node/ v-solar;
|
||||
/delete-node/ boot0;
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
compatible = "libresolar,stm32-daq";
|
||||
|
||||
i-load {
|
||||
io-channels = <&adc1 6>;
|
||||
@@ -74,3 +66,14 @@
|
||||
/delete-node/ temp-bat;
|
||||
};
|
||||
};
|
||||
|
||||
/* no battery temperature sensor input */
|
||||
/delete-node/ &ntc_bat;
|
||||
|
||||
&load_switch {
|
||||
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
|
||||
&usb_switch {
|
||||
gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
|
||||
-1
@@ -1,4 +1,3 @@
|
||||
identifier: mppt_1210_hus
|
||||
name: MPPT-1210-HUS
|
||||
type: mcu
|
||||
arch: arm
|
||||
@@ -20,6 +20,7 @@
|
||||
hs-voltage-max = <90>;
|
||||
ls-voltage-max = <32>;
|
||||
dcdc-current-max = <20>;
|
||||
ls-fuse;
|
||||
};
|
||||
|
||||
chosen {
|
||||
@@ -48,26 +49,30 @@
|
||||
};
|
||||
};
|
||||
|
||||
load_switch: load-switch {
|
||||
compatible = "libresolar,gpio-power-switch";
|
||||
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
|
||||
/* 200 us period = 5 kHz */
|
||||
charge-pump-pwms = <&pwm8 2 200000 PWM_POLARITY_NORMAL>;
|
||||
current-max = <20>;
|
||||
};
|
||||
|
||||
outputs {
|
||||
compatible = "outputs";
|
||||
|
||||
load {
|
||||
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
|
||||
current-max = <20>;
|
||||
kconfig-flag; /* make node visible in Kconfig */
|
||||
};
|
||||
|
||||
charge_pump {
|
||||
pwms = <&pwm8 2 200000 PWM_POLARITY_NORMAL>; /* 200 us period = 5 kHz */
|
||||
};
|
||||
|
||||
can_en {
|
||||
gpios = <&gpiob 11 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
};
|
||||
|
||||
ntc_fets: ntc-fets {
|
||||
compatible = "libresolar,daq-ntc-thermistor";
|
||||
daq-channel = <&daq_temp_fets>;
|
||||
pullup-ohm = <10000>;
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
compatible = "libresolar,stm32-daq";
|
||||
|
||||
v-low {
|
||||
io-channels = <&adc1 12>;
|
||||
@@ -81,9 +86,9 @@
|
||||
divider = <2200>;
|
||||
};
|
||||
|
||||
temp-fets {
|
||||
daq_temp_fets: temp-fets {
|
||||
io-channels = <&adc1 11>;
|
||||
multiplier = <10000>; // 10k NTC series resistor
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
@@ -177,7 +182,7 @@
|
||||
bias-pull-up; // avoid junk characters if pin is left floating
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
uext_i2c: &i2c1 {
|
||||
pinctrl-0 = <&i2c1_scl_pa15 &i2c1_sda_pb7>;
|
||||
pinctrl-names = "default";
|
||||
clock-frequency = <I2C_BITRATE_STANDARD>;
|
||||
@@ -206,11 +211,13 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-advtim-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pc13>;
|
||||
pinctrl-names = "default";
|
||||
channel = <1>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
adc-trigger;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -32,11 +32,10 @@
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
eeprom@50 {
|
||||
eeprom: eeprom@50 {
|
||||
// Microchip 24AA32A
|
||||
compatible = "atmel,at24";
|
||||
reg = <0x50>;
|
||||
label = "EEPROM_0";
|
||||
size = <32768>;
|
||||
pagesize = <32>;
|
||||
address-width = <16>;
|
||||
|
||||
-1
@@ -1,4 +1,3 @@
|
||||
identifier: mppt_2420_hc
|
||||
name: MPPT-2420-HC
|
||||
type: mcu
|
||||
arch: arm
|
||||
@@ -3,9 +3,6 @@
|
||||
|
||||
if BOARD_MPPT_2420_HPX
|
||||
|
||||
config LIBRE_SOLAR_TYPE_ID
|
||||
default 7
|
||||
|
||||
if SPI
|
||||
|
||||
config SPI_STM32_INTERRUPT
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
hs-voltage-max = <80>;
|
||||
ls-voltage-max = <32>;
|
||||
dcdc-current-max = <20>;
|
||||
ls-fuse;
|
||||
};
|
||||
|
||||
chosen {
|
||||
@@ -41,32 +42,30 @@
|
||||
};
|
||||
};
|
||||
|
||||
load_switch: load-switch {
|
||||
compatible = "libresolar,gpio-power-switch";
|
||||
gpios = <&gpiob 15 GPIO_ACTIVE_LOW>;
|
||||
/* 200 us period = 5 kHz */
|
||||
charge-pump-pwms = <&pwm8 1 200000 PWM_POLARITY_NORMAL>;
|
||||
current-max = <20>;
|
||||
};
|
||||
|
||||
outputs {
|
||||
compatible = "outputs";
|
||||
|
||||
pwm_switch {
|
||||
pwms = <&pwm3 2 (20*1000*1000) PWM_POLARITY_NORMAL>; /* 20 ms period = 50 Hz */
|
||||
current-max = <20>;
|
||||
kconfig-flag; /* make node visible in Kconfig */
|
||||
};
|
||||
|
||||
load {
|
||||
gpios = <&gpiob 15 GPIO_ACTIVE_LOW>;
|
||||
current-max = <20>;
|
||||
kconfig-flag; /* make node visible in Kconfig */
|
||||
};
|
||||
|
||||
charge_pump {
|
||||
pwms = <&pwm8 1 200000 PWM_POLARITY_NORMAL>; /* 200 us period = 5 kHz */
|
||||
};
|
||||
|
||||
hv-out {
|
||||
gpios = <&gpiob 12 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
};
|
||||
|
||||
ntc_fets: ntc-fets {
|
||||
compatible = "libresolar,daq-ntc-thermistor";
|
||||
daq-channel = <&daq_temp_fets>;
|
||||
pullup-ohm = <10000>;
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
compatible = "libresolar,stm32-daq";
|
||||
|
||||
v-low {
|
||||
io-channels = <&adc1 12>;
|
||||
@@ -93,9 +92,9 @@
|
||||
offset = <37414>; // 65536 / (1 + 8.2/120 + 8.2/12)
|
||||
};
|
||||
|
||||
temp-fets {
|
||||
daq_temp_fets: temp-fets {
|
||||
io-channels = <&adc1 6>;
|
||||
multiplier = <10000>; // 10k NTC series resistor
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
@@ -184,7 +183,7 @@
|
||||
bias-pull-up; // avoid junk characters if pin is left floating
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
uext_i2c: &i2c1 {
|
||||
pinctrl-0 = <&i2c1_scl_pa15 &i2c1_sda_pb7>;
|
||||
pinctrl-names = "default";
|
||||
status = "okay";
|
||||
@@ -226,21 +225,27 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-advtim-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
pinctrl-names = "default";
|
||||
channel = <1>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
adc-trigger;
|
||||
};
|
||||
};
|
||||
|
||||
/* TIM3 (CH2) used for PWM power switch */
|
||||
&timers3 {
|
||||
status = "okay";
|
||||
pwm3: pwm {
|
||||
status = "okay";
|
||||
|
||||
pwm_switch: pwm-switch {
|
||||
compatible = "libresolar,stm32-pwm-power-switch";
|
||||
pinctrl-0 = <&tim3_ch2_pc7>;
|
||||
pinctrl-names = "default";
|
||||
channel = <2>;
|
||||
frequency = <50>;
|
||||
current-max = <20>;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
-1
@@ -1,4 +1,3 @@
|
||||
identifier: mppt_2420_hpx
|
||||
name: MPPT-2420-HPX
|
||||
type: mcu
|
||||
arch: arm
|
||||
@@ -3,9 +3,6 @@
|
||||
|
||||
if BOARD_MPPT_2420_LC
|
||||
|
||||
config LIBRE_SOLAR_TYPE_ID
|
||||
default 1
|
||||
|
||||
if SPI
|
||||
|
||||
config SPI_STM32_INTERRUPT
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
hs-voltage-max = <55>;
|
||||
ls-voltage-max = <32>;
|
||||
dcdc-current-max = <20>;
|
||||
ls-fuse;
|
||||
};
|
||||
|
||||
chosen {
|
||||
@@ -47,15 +48,15 @@
|
||||
};
|
||||
};
|
||||
|
||||
load_switch: load-switch {
|
||||
compatible = "libresolar,gpio-power-switch";
|
||||
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
|
||||
current-max = <20>;
|
||||
};
|
||||
|
||||
outputs {
|
||||
compatible = "outputs";
|
||||
|
||||
load {
|
||||
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
|
||||
current-max = <20>;
|
||||
kconfig-flag; /* make node visible in Kconfig */
|
||||
};
|
||||
|
||||
uext_en {
|
||||
gpios = <&gpioc 14 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
@@ -73,18 +74,30 @@
|
||||
};
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
ntc_bat: ntc-bat {
|
||||
compatible = "libresolar,daq-ntc-thermistor";
|
||||
daq-channel = <&daq_temp_bat>;
|
||||
pullup-ohm = <10000>;
|
||||
};
|
||||
|
||||
temp-bat {
|
||||
ntc_fets: ntc-fets {
|
||||
compatible = "libresolar,daq-ntc-thermistor";
|
||||
daq-channel = <&daq_temp_fets>;
|
||||
pullup-ohm = <10000>;
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "libresolar,stm32-daq";
|
||||
|
||||
daq_temp_bat: temp-bat {
|
||||
io-channels = <&adc1 0>;
|
||||
multiplier = <10000>; // 10k NTC series resistor
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
temp-fets {
|
||||
daq_temp_fets: temp-fets {
|
||||
io-channels = <&adc1 1>;
|
||||
multiplier = <10000>; // 10k NTC series resistor
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
@@ -182,7 +195,7 @@
|
||||
bias-pull-up; // avoid junk characters if pin is left floating
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
uext_i2c: &i2c1 {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&i2c1_scl_pb6 &i2c1_sda_pb7>;
|
||||
pinctrl-names = "default";
|
||||
@@ -225,17 +238,18 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-advtim-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
pinctrl-names = "default";
|
||||
channel = <1>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
};
|
||||
};
|
||||
|
||||
&can1 {
|
||||
pinctrl-0 = <&can_rx_pb8 &can_tx_pb9>;
|
||||
pinctrl-names = "default";
|
||||
bus-speed = <500000>;
|
||||
bitrate = <500000>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
-1
@@ -1,4 +1,3 @@
|
||||
identifier: mppt_2420_lc
|
||||
name: MPPT-2420-LC
|
||||
type: mcu
|
||||
arch: arm
|
||||
@@ -3,10 +3,6 @@
|
||||
|
||||
if BOARD_MPPT_2420_RC
|
||||
|
||||
config LIBRE_SOLAR_TYPE_ID
|
||||
default 0
|
||||
|
||||
|
||||
if SPI
|
||||
|
||||
config SPI_STM32_INTERRUPT
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
hs-voltage-max = <80>;
|
||||
ls-voltage-max = <32>;
|
||||
dcdc-current-max = <20>;
|
||||
ls-fuse;
|
||||
};
|
||||
|
||||
chosen {
|
||||
@@ -54,8 +55,20 @@
|
||||
};
|
||||
};
|
||||
|
||||
ntc_fets: ntc-fets {
|
||||
compatible = "libresolar,daq-ntc-thermistor";
|
||||
daq-channel = <&daq_temp_fets>;
|
||||
pullup-ohm = <10000>;
|
||||
};
|
||||
|
||||
ntc_bat: ntc-bat {
|
||||
compatible = "libresolar,daq-ntc-thermistor";
|
||||
daq-channel = <&daq_temp_bat>;
|
||||
pullup-ohm = <10000>;
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
compatible = "libresolar,stm32-daq";
|
||||
|
||||
v-low {
|
||||
io-channels = <&adc1 12>;
|
||||
@@ -75,15 +88,15 @@
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
temp-fets {
|
||||
daq_temp_fets: temp-fets {
|
||||
io-channels = <&adc1 2>;
|
||||
multiplier = <10000>; // 10k NTC series resistor
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
temp-bat {
|
||||
daq_temp_bat: temp-bat {
|
||||
io-channels = <&adc1 14>;
|
||||
multiplier = <10000>; // 10k NTC series resistor
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
@@ -181,11 +194,13 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-advtim-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
pinctrl-names = "default";
|
||||
channel = <1>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
adc-trigger;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -210,7 +225,7 @@
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
uext_i2c: &i2c1 {
|
||||
/* shared between UEXT and EEPROM */
|
||||
status = "okay";
|
||||
pinctrl-0 = <&i2c1_scl_pa15 &i2c1_sda_pb7>;
|
||||
|
||||
-1
@@ -1,4 +1,3 @@
|
||||
identifier: mppt_2420_rc
|
||||
name: MPPT-2420-RC
|
||||
type: mcu
|
||||
arch: arm
|
||||
@@ -3,9 +3,6 @@
|
||||
|
||||
if BOARD_PWM_2420_LUS
|
||||
|
||||
config LIBRE_SOLAR_TYPE_ID
|
||||
default 5
|
||||
|
||||
if SPI
|
||||
|
||||
config SPI_STM32_INTERRUPT
|
||||
@@ -13,5 +10,4 @@ config SPI_STM32_INTERRUPT
|
||||
|
||||
endif # SPI
|
||||
|
||||
|
||||
endif # BOARD_PWM_2420_LUS
|
||||
|
||||
@@ -61,34 +61,36 @@
|
||||
};
|
||||
};
|
||||
|
||||
/* load switch with short-circuit comparator at i-load measurement (PB4) */
|
||||
load_switch: load-switch {
|
||||
compatible = "libresolar,stm32-lptim-power-switch";
|
||||
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
|
||||
pinctrl-0 = <&lptim1_out_pb2 &comp2_inp_pb4>;
|
||||
pinctrl-names = "default";
|
||||
current-max = <15>;
|
||||
};
|
||||
|
||||
usb_switch: usb-switch {
|
||||
compatible = "libresolar,gpio-power-switch";
|
||||
gpios = <&gpiob 5 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
|
||||
outputs {
|
||||
compatible = "outputs";
|
||||
|
||||
pwm_switch {
|
||||
pwms = <&pwm3 4 (20*1000*1000) PWM_POLARITY_NORMAL>; /* 20 ms period = 50 Hz */
|
||||
// Current reduced to 15A. Increase to 20A PCB max values
|
||||
// only if attached to a big heat sink.
|
||||
current-max = <15>;
|
||||
kconfig-flag; /* make node visible in Kconfig */
|
||||
};
|
||||
|
||||
load {
|
||||
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
|
||||
current-max = <15>;
|
||||
kconfig-flag; /* make node visible in Kconfig */
|
||||
};
|
||||
|
||||
usb-pwr {
|
||||
gpios = <&gpiob 5 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
|
||||
boot0 {
|
||||
gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
};
|
||||
|
||||
ntc_bat: ntc-bat {
|
||||
compatible = "libresolar,daq-ntc-thermistor";
|
||||
daq-channel = <&daq_temp_bat>;
|
||||
pullup-ohm = <8200>;
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
compatible = "libresolar,stm32-daq";
|
||||
|
||||
v-low {
|
||||
io-channels = <&adc1 0>;
|
||||
@@ -118,9 +120,9 @@
|
||||
divider = <136>; // 2*68
|
||||
};
|
||||
|
||||
temp-bat {
|
||||
daq_temp_bat: temp-bat {
|
||||
io-channels = <&adc1 7>;
|
||||
multiplier = <8200>; // 8.2k NTC series resistor
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
@@ -187,7 +189,7 @@
|
||||
bias-pull-up; // avoid junk characters if pin is left floating
|
||||
};
|
||||
|
||||
&i2c1 {
|
||||
uext_i2c: &i2c1 {
|
||||
pinctrl-0 = <&i2c1_scl_pb6 &i2c1_sda_pb7>;
|
||||
pinctrl-names = "default";
|
||||
status = "okay";
|
||||
@@ -207,11 +209,15 @@
|
||||
|
||||
&timers3 {
|
||||
status = "okay";
|
||||
st,prescaler = <10000>;
|
||||
pwm3: pwm {
|
||||
status = "okay";
|
||||
|
||||
pwm_switch: pwm-switch {
|
||||
compatible = "libresolar,stm32-pwm-power-switch";
|
||||
pinctrl-0 = <&tim3_ch4_pb1>;
|
||||
pinctrl-names = "default";
|
||||
channel = <4>;
|
||||
frequency = <50>;
|
||||
/* reduced to 15A, increase to 20A PCB max value only if attached to a big heat sink */
|
||||
current-max = <15>;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "adc-inputs";
|
||||
compatible = "libresolar,stm32-daq";
|
||||
|
||||
i-load {
|
||||
io-channels = <&adc1 5>;
|
||||
|
||||
-1
@@ -1,4 +1,3 @@
|
||||
identifier: pwm_2420_lus
|
||||
name: PWM-2420-LUS
|
||||
type: mcu
|
||||
arch: arm
|
||||
@@ -0,0 +1,9 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
if SHIELD_UEXT_OLED
|
||||
|
||||
config SSD1306_DEFAULT_CONTRAST
|
||||
default 1
|
||||
|
||||
endif # SHIELD_UEXT_OLED
|
||||
@@ -0,0 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config SHIELD_UEXT_OLED
|
||||
def_bool $(shields_list_contains,uext_oled)
|
||||
@@ -0,0 +1,6 @@
|
||||
shield:
|
||||
name: uext_oled
|
||||
full_name: SSD1306 128x64 OLED display at UEXT connector
|
||||
vendor: others
|
||||
supported_features:
|
||||
- display
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/ {
|
||||
chosen {
|
||||
zephyr,display = &uext_oled;
|
||||
};
|
||||
};
|
||||
|
||||
&uext_i2c {
|
||||
status = "okay";
|
||||
|
||||
uext_oled: ssd1306@3c {
|
||||
compatible = "solomon,ssd1306";
|
||||
reg = <0x3c>;
|
||||
width = <128>;
|
||||
height = <64>;
|
||||
segment-offset = <0>;
|
||||
page-offset = <0>;
|
||||
display-offset = <0>;
|
||||
multiplex-ratio = <63>;
|
||||
segment-remap;
|
||||
com-invdir;
|
||||
prechargep = <0xF1>;
|
||||
/* initialized by the application after the UEXT port was powered */
|
||||
zephyr,deferred-init;
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config BOARD_SPINVERT_BOOST_4820
|
||||
select SOC_STM32G0B1XX
|
||||
@@ -0,0 +1,15 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# keep first
|
||||
board_runner_args(stm32cubeprogrammer "--port=swd" "--reset-mode=hw")
|
||||
|
||||
board_runner_args(pyocd "--target=stm32g0b1cctx")
|
||||
board_runner_args(pyocd "--flash-opt=-O reset_type=hw")
|
||||
board_runner_args(pyocd "--flash-opt=-O connect_mode=under-reset")
|
||||
board_runner_args(jlink "--device=STM32G0B1CC" "--speed=4000")
|
||||
|
||||
# keep first
|
||||
include(${ZEPHYR_BASE}/boards/common/stm32cubeprogrammer.board.cmake)
|
||||
include(${ZEPHYR_BASE}/boards/common/openocd-stm32.board.cmake)
|
||||
include(${ZEPHYR_BASE}/boards/common/pyocd.board.cmake)
|
||||
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
|
||||
@@ -0,0 +1,6 @@
|
||||
board:
|
||||
name: spinvert_boost_4820
|
||||
full_name: Spinvert Boost 4820
|
||||
vendor: spinvert
|
||||
socs:
|
||||
- name: stm32g0b1xx
|
||||
@@ -0,0 +1,254 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/dts-v1/;
|
||||
#include <st/g0/stm32g0b1Xc.dtsi>
|
||||
#include <st/g0/stm32g0b1c(b-c-e)txn-pinctrl.dtsi>
|
||||
|
||||
/ {
|
||||
model = "Spinvert Boost 4820";
|
||||
compatible = "st,stm32g0b1";
|
||||
|
||||
pcb {
|
||||
compatible = "charge-controller";
|
||||
|
||||
type = "Boost 4820";
|
||||
version-str = "v0.1";
|
||||
version-num = <1>;
|
||||
|
||||
/* high side: battery output (80 V capacitors, 100 V MOSFETs) */
|
||||
hs-voltage-max = <70>;
|
||||
/* low side: rectified generator voltage (58 V TVS diodes D12, D13, D17) */
|
||||
ls-voltage-max = <60>;
|
||||
/* max. inductor current */
|
||||
dcdc-current-max = <10>;
|
||||
};
|
||||
|
||||
chosen {
|
||||
thingset,can = &fdcan1;
|
||||
/* only UART, available at the SBU pins of the USB-C connector (debug accessory mode) */
|
||||
thingset,serial = &usart1;
|
||||
zephyr,console = &usart1;
|
||||
zephyr,shell-uart = &usart1;
|
||||
zephyr,sram = &sram0;
|
||||
zephyr,flash = &flash0;
|
||||
};
|
||||
|
||||
/* dual-color LED with common anode */
|
||||
leds {
|
||||
compatible = "charlieplexing-leds";
|
||||
gpios = <&gpiob 8 GPIO_ACTIVE_LOW>, /* red */
|
||||
<&gpiob 9 GPIO_ACTIVE_LOW>; /* green */
|
||||
|
||||
pwr {
|
||||
states = <0 1>;
|
||||
};
|
||||
};
|
||||
|
||||
outputs {
|
||||
compatible = "outputs";
|
||||
|
||||
dump_load {
|
||||
pwms = <&pwm2 2 PWM_MSEC(1) PWM_POLARITY_NORMAL>; /* 1 ms period = 1 kHz */
|
||||
current-max = <20>;
|
||||
};
|
||||
|
||||
can_en {
|
||||
/* TCAN334 is in standby mode if STB is high */
|
||||
gpios = <&gpiod 2 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
};
|
||||
|
||||
ntc_fets: ntc-fets {
|
||||
compatible = "libresolar,daq-ntc-thermistor";
|
||||
daq-channel = <&daq_temp_fets>;
|
||||
pullup-ohm = <10000>;
|
||||
};
|
||||
|
||||
adc-inputs {
|
||||
compatible = "libresolar,stm32-daq";
|
||||
|
||||
i-dump-load {
|
||||
io-channels = <&adc1 0>;
|
||||
// no amplifier, resistor: 2 mOhm
|
||||
multiplier = <1000>; // 1000
|
||||
divider = <2>; // 2*1
|
||||
};
|
||||
|
||||
v-low {
|
||||
io-channels = <&adc1 2>;
|
||||
multiplier = <105600>;
|
||||
divider = <5600>;
|
||||
};
|
||||
|
||||
i-dcdc {
|
||||
io-channels = <&adc1 3>;
|
||||
// amp gain: 50 (INA299A2), resistor: 2 mOhm, offset set via REF pin (DAC1)
|
||||
multiplier = <1000>; // 1000
|
||||
divider = <100>; // 2*50
|
||||
};
|
||||
|
||||
v-high {
|
||||
io-channels = <&adc1 5>;
|
||||
multiplier = <102200>;
|
||||
divider = <2200>;
|
||||
};
|
||||
|
||||
daq_temp_fets: temp-fets {
|
||||
io-channels = <&adc1 6>;
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
/* generator phase voltages vs. GND for frequency measurement: 2x 150k / 10k divider */
|
||||
v-ac-l1 {
|
||||
io-channels = <&adc1 8>;
|
||||
multiplier = <310000>;
|
||||
divider = <10000>;
|
||||
};
|
||||
|
||||
v-ac-l2 {
|
||||
io-channels = <&adc1 9>;
|
||||
multiplier = <310000>;
|
||||
divider = <10000>;
|
||||
};
|
||||
|
||||
v-ac-l3 {
|
||||
io-channels = <&adc1 10>;
|
||||
multiplier = <310000>;
|
||||
divider = <10000>;
|
||||
};
|
||||
|
||||
temp-mcu {
|
||||
io-channels = <&adc1 12>;
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
|
||||
vref-mcu {
|
||||
io-channels = <&adc1 13>;
|
||||
multiplier = <1>;
|
||||
divider = <1>;
|
||||
};
|
||||
};
|
||||
|
||||
aliases {
|
||||
uart-dbg = &usart1;
|
||||
};
|
||||
};
|
||||
|
||||
&clk_hse {
|
||||
clock-frequency = <DT_FREQ_M(8)>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&clk_hsi48 {
|
||||
status = "okay";
|
||||
crs-usb-sof;
|
||||
};
|
||||
|
||||
/* 8 MHz / 1 * 16 = 128 MHz VCO */
|
||||
&pll {
|
||||
div-m = <1>;
|
||||
mul-n = <16>;
|
||||
div-p = <2>;
|
||||
div-q = <2>;
|
||||
div-r = <2>;
|
||||
clocks = <&clk_hse>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&rcc {
|
||||
clocks = <&pll>;
|
||||
clock-frequency = <DT_FREQ_M(64)>;
|
||||
ahb-prescaler = <1>;
|
||||
apb1-prescaler = <1>;
|
||||
};
|
||||
|
||||
&usart1 {
|
||||
pinctrl-0 = <&usart1_tx_pb6 &usart1_rx_pb7>;
|
||||
pinctrl-names = "default";
|
||||
current-speed = <115200>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&usart1_rx_pb7 {
|
||||
bias-pull-up; // avoid junk characters if pin is left floating
|
||||
};
|
||||
|
||||
zephyr_udc0: &usb {
|
||||
pinctrl-0 = <&usb_dm_pa11 &usb_dp_pa12>;
|
||||
pinctrl-names = "default";
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&iwdg {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&adc1 {
|
||||
pinctrl-0 = <&adc1_in0_pa0 &adc1_in2_pa2 &adc1_in3_pa3 &adc1_in5_pa5 &adc1_in6_pa6
|
||||
&adc1_in8_pb0 &adc1_in9_pb1 &adc1_in10_pb2>;
|
||||
pinctrl-names = "default";
|
||||
st,adc-clock-source = "SYNC";
|
||||
st,adc-prescaler = <4>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
/* reference voltage for the DC/DC current amplifier (INA299 REF pin) */
|
||||
&dac1 {
|
||||
pinctrl-0 = <&dac1_out1_pa4>;
|
||||
pinctrl-names = "default";
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&timers1 {
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "libresolar,stm32-advtim-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch2_pb3 &tim1_ch2n_pd3>;
|
||||
pinctrl-names = "default";
|
||||
channel = <2>;
|
||||
frequency = <128000>;
|
||||
deadtime-ns = <300>;
|
||||
};
|
||||
};
|
||||
|
||||
/* TIM2 (CH2) used for dump load */
|
||||
&timers2 {
|
||||
status = "okay";
|
||||
st,prescaler = <63>; /* reduce counter clock frequency to 1 MHz */
|
||||
|
||||
pwm2: pwm {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&tim2_ch2_pa1>;
|
||||
pinctrl-names = "default";
|
||||
};
|
||||
};
|
||||
|
||||
&fdcan1 {
|
||||
clocks = <&rcc STM32_CLOCK(APB1, 12)>,
|
||||
<&rcc STM32_SRC_PLL_Q FDCAN_SEL(1)>;
|
||||
pinctrl-0 = <&fdcan1_rx_pd0 &fdcan1_tx_pd1>;
|
||||
pinctrl-names = "default";
|
||||
bitrate = <500000>;
|
||||
bitrate-data = <2000000>;
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&flash0 {
|
||||
partitions {
|
||||
compatible = "fixed-partitions";
|
||||
#address-cells = <1>;
|
||||
#size-cells = <1>;
|
||||
|
||||
/* 4 KiB of storage at the end of the 256 KiB of flash (second bank) */
|
||||
storage_partition: partition@3f000 {
|
||||
reg = <0x0003f000 0x00001000>;
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
CONFIG_ARM_MPU=y
|
||||
CONFIG_HW_STACK_PROTECTION=y
|
||||
CONFIG_MPU_ALLOW_FLASH_WRITE=y
|
||||
|
||||
CONFIG_CLOCK_CONTROL=y
|
||||
|
||||
CONFIG_PINCTRL=y
|
||||
|
||||
CONFIG_GPIO=y
|
||||
|
||||
CONFIG_SERIAL=y
|
||||
|
||||
CONFIG_CONSOLE=y
|
||||
CONFIG_UART_CONSOLE=y
|
||||
|
||||
CONFIG_PWM=y
|
||||
|
||||
CONFIG_FLASH=y
|
||||
CONFIG_FLASH_PAGE_LAYOUT=y
|
||||
CONFIG_FLASH_LOG_LEVEL_WRN=y
|
||||
|
||||
CONFIG_NVS=y
|
||||
CONFIG_NVS_LOG_LEVEL_WRN=y
|
||||
|
||||
CONFIG_CAN=y
|
||||
CONFIG_ISOTP=y
|
||||
CONFIG_ISOTP_FAST=y
|
||||
CONFIG_THINGSET_CAN=y
|
||||
|
||||
# The STM32G0B1 doesn't have an RNG, so a timer-based (non-random) number generator is used for the
|
||||
# automatic CAN node address assignment of ThingSet. ENTROPY_GENERATOR is required by THINGSET_CAN,
|
||||
# even though there is no entropy driver.
|
||||
CONFIG_ENTROPY_GENERATOR=y
|
||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||
@@ -0,0 +1 @@
|
||||
source [find board/st_nucleo_g0.cfg]
|
||||
@@ -0,0 +1,21 @@
|
||||
identifier: spinvert_boost_4820
|
||||
name: Spinvert Boost 4820
|
||||
type: mcu
|
||||
arch: arm
|
||||
toolchain:
|
||||
- zephyr
|
||||
- gnuarmemb
|
||||
ram: 144
|
||||
flash: 256
|
||||
supported:
|
||||
- adc
|
||||
- can
|
||||
- dac
|
||||
- gpio
|
||||
- nvs
|
||||
- pwm
|
||||
- uart
|
||||
- usb_device
|
||||
- usbd
|
||||
- watchdog
|
||||
vendor: spinvert
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
#
|
||||
# Only contains most important options, as HTML rendering is done by Sphinx
|
||||
|
||||
INPUT = ../app/src
|
||||
INPUT = ../app/src ../include
|
||||
OUTPUT_DIRECTORY = build/doxygen
|
||||
RECURSIVE = YES
|
||||
GENERATE_HTML = NO
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
# Architecture Migration Plan
|
||||
|
||||
This document describes the target architecture of the charge controller firmware and the steps
|
||||
to migrate the current application towards it. The goal is a firmware that can be adapted to
|
||||
different boards and use cases mainly via devicetree and Kconfig, that allows to plug in
|
||||
different control algorithms and that is not tied to a single communication protocol.
|
||||
|
||||
## Current limitations
|
||||
|
||||
1. **Topology hard-coded in the application:** `setup.cpp` instantiates a fixed set of global
|
||||
objects (`lv_bus`, `hv_bus`, `dcdc`, `pwm_switch`, `load`, `usb_pwr`). `BOARD_HAS_*`
|
||||
preprocessor conditions are spread over `main.cpp`, `daq.cpp` and `data_objects.cpp`.
|
||||
Boards with two DC/DC converters, multiple load outputs or a second battery are not
|
||||
possible without touching many files.
|
||||
2. **Use case selected via Kconfig choices:** Port roles (`HV_TERMINAL_SOLAR`,
|
||||
`LV_TERMINAL_BATTERY`, ...) are Kconfig choices mapped to reference aliases
|
||||
(`solar_terminal`, `bat_terminal`) and `#define`s in the data objects.
|
||||
3. **Board-specific measurement mapping in one place:** `daq_update()` knows every ADC channel
|
||||
by name and how they are combined (e.g. `lv_terminal_current = i_dcdc - i_load - i_pwm`).
|
||||
4. **Monolithic control algorithm:** `Dcdc::perturb_observe_buck()` is a priority-ordered
|
||||
if/else chain that mixes MPPT, CV, CC, derating and nanogrid droop. The duty cycle is changed
|
||||
by ±1 step at 10 Hz. Replacing the algorithm requires replacing the entire function.
|
||||
5. **ThingSet is the data model:** `data_objects.cpp` points directly to fields of global C++
|
||||
objects. Other protocols would have to access the same globals. Writes from the ThingSet
|
||||
thread are not synchronized with the control thread.
|
||||
6. **Implicit timing:** Energy calculation relies on "called exactly once per second", the
|
||||
control period on `k_sleep(K_MSEC(100))`.
|
||||
7. **Hardware access in the application:** Register-level code for STM32 timers, comparators
|
||||
and the bootloader lives in `app/src` (`load_driver.c`, `pwm_switch_driver.c`,
|
||||
`hardware.cpp`, `mcu.h`).
|
||||
|
||||
## Target architecture
|
||||
|
||||
```
|
||||
┌──────────────────────────────────────────────────────────────────────────┐
|
||||
│ Protocol adapters ThingSet │ CAN BMS (Pylontech/Victron) │ CANopen │ Modbus │ OLED
|
||||
├──────────────────────────────────────────────────────────────────────────┤
|
||||
│ Data model zbus channels: measurements, status, events, commands; settings
|
||||
├──────────────────────────────────────────────────────────────────────────┤
|
||||
│ Application charger profiles │ load mgmt │ energy/SoC │ supervisor/errors
|
||||
├──────────────────────────────────────────────────────────────────────────┤
|
||||
│ Control framework setpoint sources ──► limit arbitration ──► regulators (PI)
|
||||
├──────────────────────────────────────────────────────────────────────────┤
|
||||
│ Power model (DT) dc-bus │ power-port │ converter │ switch (instances from DT)
|
||||
├──────────────────────────────────────────────────────────────────────────┤
|
||||
│ Drivers daq │ half_bridge │ power_switch │ Zephyr sensor/gpio/can/...
|
||||
└──────────────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
Each layer only depends on layers below. Protocols never access control internals directly.
|
||||
|
||||
### Drivers
|
||||
|
||||
Zephyr device drivers with a generic API, a devicetree binding and an emulated or native_sim
|
||||
backend for tests:
|
||||
|
||||
- `daq`: continuous sampling, filtering and limit checking of analog inputs (done)
|
||||
- `half_bridge`: complementary PWM for DC/DC converters (done)
|
||||
- `power_switch`: on/off switches for load and USB outputs including power-good feedback,
|
||||
charge pumps for high-side gate drivers and hardware short-circuit protection. Later also the
|
||||
low-frequency PWM switch of PWM charge controllers.
|
||||
- Zephyr sensor API for NTC thermistors instead of the custom `ntc_temp()` in the application.
|
||||
|
||||
### Power model described in devicetree
|
||||
|
||||
The power topology is described in devicetree and instantiated with `DT_FOREACH_STATUS_OKAY`:
|
||||
|
||||
```dts
|
||||
power {
|
||||
bus_lv: dc-bus-lv {
|
||||
compatible = "power,dc-bus";
|
||||
voltage = <&daq V_LOW>;
|
||||
};
|
||||
bus_hv: dc-bus-hv {
|
||||
compatible = "power,dc-bus";
|
||||
voltage = <&daq V_HIGH>;
|
||||
};
|
||||
dcdc0: dcdc {
|
||||
compatible = "power,buck-boost";
|
||||
half-bridge = <&halfbridge>;
|
||||
high-bus = <&bus_hv>;
|
||||
low-bus = <&bus_lv>;
|
||||
inductor-current = <&daq I_DCDC>;
|
||||
};
|
||||
port_solar: solar {
|
||||
compatible = "power,port";
|
||||
bus = <&bus_hv>;
|
||||
role = "solar"; /* use case overlay may change to "grid", "battery" */
|
||||
current-from = <&dcdc0>; /* derived from inductor current */
|
||||
};
|
||||
port_bat: battery {
|
||||
compatible = "power,port";
|
||||
bus = <&bus_lv>;
|
||||
role = "battery";
|
||||
current-derived; /* residual of all other ports at the bus (KCL) */
|
||||
};
|
||||
port_load: load {
|
||||
compatible = "power,port";
|
||||
bus = <&bus_lv>;
|
||||
role = "load";
|
||||
current = <&daq I_LOAD>;
|
||||
switch = <&load_switch>;
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
- Port roles move from Kconfig to devicetree. Use cases become overlays.
|
||||
- Derived measurements are declared instead of hand-coded in `daq_update()`.
|
||||
- The existing `DcBus` / `PowerPort` concepts (limits, energy counters, droop) are kept.
|
||||
|
||||
### Control framework
|
||||
|
||||
- **Setpoint sources** publish limits: charger (CV/CC), MPPT (input voltage target), thermal
|
||||
derating, nanogrid droop, external commands (e.g. BMS via CAN).
|
||||
- **Limit arbitration** selects the most restrictive constraint per converter and reports which
|
||||
one is active (replaces the `DCDC_CONTROL_*` states).
|
||||
- **Regulators** (min-selected PI loops for output voltage, inductor current and input voltage)
|
||||
run in the DAQ sample callback at kHz rate.
|
||||
- MPPT becomes a setpoint source adjusting the input voltage at ~10 Hz, so different algorithms
|
||||
(P&O, incremental conductance, fractional Voc) can be exchanged without touching the
|
||||
regulators.
|
||||
- Sources are registered via iterable sections (`CTRL_SOURCE_DEFINE(...)`), so custom
|
||||
algorithms can live in out-of-tree modules. This replaces `CONFIG_CUSTOMIZATION`.
|
||||
- Fixed and explicit rates: regulators in ISR, setpoint sources and protection at 10 Hz, charger
|
||||
state machine and energy/SoC at 1 Hz with measured time deltas.
|
||||
|
||||
### Application modules
|
||||
|
||||
- **charger:** state machine as today, battery chemistries as constant profile tables
|
||||
- **load manager:** LVD/LVR, overcurrent recovery, load shedding for N load ports
|
||||
- **energy/SoC:** coulomb counting plus OCV correction behind an exchangeable interface
|
||||
- **supervisor:** error/event registry instead of a single global 32-bit `error_flags` field,
|
||||
owner of fail-safe actions (e.g. fuse destruction)
|
||||
|
||||
### Data model and protocols
|
||||
|
||||
- State is published on zbus channels (measurement snapshots, status, events), commands and
|
||||
configuration changes are received via zbus. This also solves thread synchronization.
|
||||
- Persistent configuration via Zephyr settings (or ThingSet storage behind an adapter) with
|
||||
validation callbacks.
|
||||
- Each protocol is an adapter:
|
||||
- ThingSet (data items split per module, compiled only if the module exists)
|
||||
- CAN BMS protocols (BMS sends charge voltage/current limits, which become a setpoint source)
|
||||
- CANopen, J1939, DroneCAN, Modbus RTU, OLED display
|
||||
|
||||
### Code organization
|
||||
|
||||
- Core written in C following Zephyr conventions (devicetree macros, iterable sections, device
|
||||
model). C++ remains possible in leaf modules.
|
||||
- Repository layout as a Zephyr module:
|
||||
- `drivers/`, `include/libresolar/drivers/`, `dts/bindings/`
|
||||
- `subsys/power/` (power model and control framework)
|
||||
- `subsys/charger/`
|
||||
- `subsys/comm/{thingset,can_bms,canopen}/`
|
||||
- `app/` only containing `main.c` and use-case overlays/configs
|
||||
- Closed-loop simulation on native_sim: the emulated DAQ and half-bridge drivers are connected
|
||||
to a plant model (PV curve, battery RC model, inductor), so MPPT, CC/CV transitions and fault
|
||||
cases can be tested with ztest in CI.
|
||||
|
||||
## Migration steps
|
||||
|
||||
The migration is done incrementally. Each step keeps the firmware working on all supported
|
||||
boards and passes the existing tests.
|
||||
|
||||
### Step 1: Drivers
|
||||
|
||||
Move all remaining hardware access from `app/src` into Zephyr drivers.
|
||||
|
||||
- [x] `daq` driver (replaces ADC/DMA code in the application)
|
||||
- [x] `half_bridge` driver (replaces timer code for DC/DC converters)
|
||||
- [x] `power_switch` driver API
|
||||
- [x] GPIO backend (`libresolar,gpio-power-switch`) with optional latching power-good and
|
||||
charge pump for high-side gate drivers
|
||||
- [x] STM32 LPTIM backend with comparator-based short-circuit protection
|
||||
(`libresolar,stm32-lptim-power-switch`, PWM 2420 LUS), needs hardware test
|
||||
- [x] Load and USB outputs use the driver, `load_driver.c` removed
|
||||
- [x] PWM support in the `power_switch` API
|
||||
- [x] STM32 timer backend (`libresolar,stm32-pwm-power-switch`) for PWM charge controllers
|
||||
(replaces `pwm_switch_driver.c`), needs hardware test
|
||||
- [x] Emulated backend (`libresolar,power-switch-emul`) for tests on native_sim
|
||||
- [x] NTC thermistors via Zephyr sensor API (replaces `ntc_temp()` in `daq.cpp`)
|
||||
- [x] `libresolar,daq-ntc-thermistor` sensor driver using the filtered DAQ readings, as
|
||||
Zephyr's NTC driver requires exclusive ADC access
|
||||
- [x] Bootloader entry and SoC-specific setup out of the application (`hardware.cpp`,
|
||||
`setup.cpp`, `mcu.h`)
|
||||
- Unused STM32 bootloader entry and flash size information removed (no ThingSet functions
|
||||
referencing them anymore)
|
||||
- USB-C dead battery pull-downs of STM32G4 are already disabled by Zephyr's SoC init
|
||||
- DC/DC duty cycle step size configurable via `CONFIG_DCDC_DUTY_STEP_SIZE` instead of
|
||||
depending on the SoC series in the code
|
||||
|
||||
### Step 1b: Conversion from C++ to C
|
||||
|
||||
Convert the application from C++ to C, so that the power model and all further modules can be
|
||||
written in C and instantiated from the devicetree with static initializers.
|
||||
|
||||
- Bottom-up, one module per commit. C++ code can call the converted C modules, so every
|
||||
intermediate state builds and passes the tests.
|
||||
- Classes become structs with prefixed functions (e.g. `LoadOutput::control()` →
|
||||
`load_control(struct load_output *load)`).
|
||||
- `LoadOutput` and `PwmSwitch` reference their power port via pointer instead of inheriting from
|
||||
`PowerPort`, so that the ports can be owned by the power model in step 2.
|
||||
- Default member initializers and constructors become explicit init functions called in a
|
||||
defined order from `main()`.
|
||||
- Field names are kept, so ThingSet items and the data layout don't change.
|
||||
- Finally the unit tests are converted and C++ support is disabled.
|
||||
|
||||
### Step 2: Devicetree power model
|
||||
|
||||
- Bindings for `dc-bus`, `power-port` and converters
|
||||
- Generic measurement update based on the devicetree description (replaces `daq.cpp` and
|
||||
`setup.cpp`), initially behind the existing `DcBus` / `PowerPort` API
|
||||
|
||||
### Step 3: Data layer
|
||||
|
||||
- Introduce zbus channels for measurements, status and commands
|
||||
- Split ThingSet data objects per module
|
||||
|
||||
### Step 4: Control framework
|
||||
|
||||
- Port the existing perturb & observe algorithm as the first setpoint source without changing
|
||||
its behavior
|
||||
- Add PI regulators behind a Kconfig option and compare them in simulation and on hardware
|
||||
- Requires the closed-loop simulation to be in place first
|
||||
|
||||
### Step 5: Port roles in devicetree
|
||||
|
||||
- Remove the `HV_TERMINAL_*` / `LV_TERMINAL_*` / `PWM_TERMINAL_*` Kconfig choices
|
||||
|
||||
### Step 6: Additional protocol
|
||||
|
||||
- Add a second CAN protocol (e.g. BMS protocol) to validate the adapter boundary
|
||||
@@ -47,5 +47,6 @@ The full license text is available at `<https://creativecommons.org/licenses/by-
|
||||
src/api/half_bridge
|
||||
src/api/load
|
||||
src/api/power_port
|
||||
src/api/power_switch
|
||||
src/api/pwm_switch
|
||||
src/api/misc
|
||||
|
||||
@@ -0,0 +1,290 @@
|
||||
# Power Model Design (Migration Step 2)
|
||||
|
||||
Status: **draft for review**
|
||||
|
||||
This document describes how the power topology of a board (DC buses, power ports and converters)
|
||||
is described in the devicetree and how measurements are mapped to it. It implements step 2 of
|
||||
the [architecture migration plan](architecture-migration-plan.md).
|
||||
|
||||
## Goals
|
||||
|
||||
- A board's power topology is fully described in its devicetree. Adding a board with a different
|
||||
combination of converters, switches and outputs requires no changes in the application code.
|
||||
- Measurements are assigned to buses, ports and converters in the devicetree instead of the
|
||||
hand-written `daq_update()`.
|
||||
- Derived values (e.g. battery current as residual of all other currents at the bus) follow
|
||||
generic rules instead of board-specific code.
|
||||
- Hardware limits move from the `/pcb` node to the components they belong to.
|
||||
|
||||
## Non-goals (later steps)
|
||||
|
||||
- Changing the control algorithms (step 4). The existing `Dcdc`, `PwmSwitch`, `LoadOutput` and
|
||||
`Charger` logic stays as it is.
|
||||
- Port roles (solar, battery, grid) in the devicetree (step 5). The `HV_TERMINAL_*` /
|
||||
`LV_TERMINAL_*` Kconfig choices stay for now.
|
||||
- Data layer and ThingSet split (step 3).
|
||||
|
||||
## Current state
|
||||
|
||||
The topology is implicit in `setup.cpp`, `daq.cpp` and `board.h`:
|
||||
|
||||
| Object | Defined in | Measurements (`daq_update()`) |
|
||||
|----------------|---------------|------------------------------------------------------------------|
|
||||
| `lv_bus` | `setup.cpp` | `v_low`, filtered with τ = 10 s |
|
||||
| `hv_bus` | `setup.cpp` | `v_high`, filtered with τ = 10 s (only with DC/DC) |
|
||||
| `dcdc` | `setup.cpp` | `i_dcdc` as inductor current, `temp_fets` |
|
||||
| `hv_terminal` | `setup.cpp` | derived: `-i_dcdc * V_lv / V_hv` (lossless converter) |
|
||||
| `load` | `setup.cpp` | `i_load` |
|
||||
| `pwm_switch` | `setup.cpp` | `-duty * i_pwm`, filtered τ = 1.5 s; ext. voltage `v_low - v_pwm` |
|
||||
| `lv_terminal` | `setup.cpp` | derived: `i_dcdc - i_load - i_pwm_port`, filtered τ = 10 s |
|
||||
|
||||
Limits are taken from the `/pcb` node (`hs-voltage-max`, `ls-voltage-max`, `dcdc-current-max`,
|
||||
`ls-fuse`, thermal model parameters).
|
||||
|
||||
## Concepts and sign conventions
|
||||
|
||||
- **DC bus:** Node with a single voltage. Ports and converters are connected to buses.
|
||||
- **Power port:** Connection of a bus to an external device (battery, solar panel, load) or an
|
||||
internal sub-circuit. **Positive port current flows from the bus into the external device**,
|
||||
e.g. charging current of a battery, current into a load. A solar panel has negative current.
|
||||
This is the existing convention of `PowerPort`.
|
||||
- **Converter:** Transfers power between two buses (DC/DC half bridge). The inductor current is
|
||||
positive in buck direction (from high side to low side).
|
||||
|
||||
### Current calculation rules
|
||||
|
||||
1. **Measured:** The port current is the value of a DAQ channel, optionally inverted.
|
||||
2. **Switched sampling:** For PWM switches, the current is only sampled while the switch is
|
||||
conducting (DAQ sample filter), and the port current is the sampled value multiplied by the
|
||||
duty cycle.
|
||||
3. **Converter injection:** A converter injects `+i_L` into its low bus and `-i_L * V_low / V_high`
|
||||
into its high bus (lossless approximation, same as today).
|
||||
4. **Kirchhoff residual:** At most one port per bus may be marked `current-derived`. Its current is
|
||||
the sum of all converter injections into the bus minus the currents of all other ports.
|
||||
|
||||
Rule 4 replaces both special cases of today: the HV terminal is the residual of the HV bus with
|
||||
only the converter injection, and the battery terminal is the residual of the LV bus.
|
||||
|
||||
### Filtering
|
||||
|
||||
Every bus voltage and port current gets a filtered value with a configurable time constant
|
||||
(`filter-time-constant-ms`, default 10 s). The filter constant is calculated from the control
|
||||
frequency (`c = dt / (tau + dt)`), so the existing constants are reproduced exactly:
|
||||
10 s → 0.0099, 1.5 s → 0.0625. The filter is initialized with the first sample.
|
||||
|
||||
## Devicetree bindings
|
||||
|
||||
All bindings in `dts/bindings/power/`. DAQ channels are referenced via phandles to the labelled
|
||||
child nodes of the DAQ device (same pattern as the NTC sensor driver).
|
||||
|
||||
### `libresolar,dc-bus`
|
||||
|
||||
| Property | Type | Description |
|
||||
|---------------------------|---------|----------------------------------------------------------|
|
||||
| `voltage-channel` | phandle | DAQ channel measuring the bus voltage (required) |
|
||||
| `voltage-max-mv` | int | Maximum voltage in mV, used for overvoltage alerts |
|
||||
| `filter-time-constant-ms` | int | Low-pass filter time constant, default 10000 |
|
||||
|
||||
### `libresolar,power-port`
|
||||
|
||||
| Property | Type | Description |
|
||||
|---------------------------|---------|----------------------------------------------------------------|
|
||||
| `bus` | phandle | Bus the port is connected to (required) |
|
||||
| `current-channel` | phandle | DAQ channel measuring the port current |
|
||||
| `current-inverted` | boolean | Channel measures current flowing into the bus |
|
||||
| `current-derived` | boolean | Current calculated as Kirchhoff residual (exclusive with above) |
|
||||
| `switch` | phandle | Power switch device of the port |
|
||||
| `current-switched` | boolean | Current only sampled while switch conducts, scaled by duty |
|
||||
| `external-voltage-channel`| phandle | DAQ channel measuring the voltage across the switch |
|
||||
| `temperature` | phandle | Temperature sensor (e.g. battery NTC) |
|
||||
| `filter-time-constant-ms` | int | Low-pass filter time constant, default 10000 |
|
||||
|
||||
The external voltage of a switched port is `V_bus - V_channel` (voltage at the external terminal
|
||||
while the switch is open, e.g. solar panel voltage of a PWM charger).
|
||||
|
||||
### `libresolar,dcdc`
|
||||
|
||||
| Property | Type | Description |
|
||||
|----------------------------|---------|-----------------------------------------------------------|
|
||||
| `half-bridge` | phandle | Half-bridge device (required) |
|
||||
| `high-bus`, `low-bus` | phandle | Connected buses (required) |
|
||||
| `inductor-current-channel` | phandle | DAQ channel measuring the inductor current (required) |
|
||||
| `current-max-ma` | int | Maximum inductor current in mA (replaces `dcdc-current-max`) |
|
||||
| `temperature` | phandle | MOSFET temperature sensor |
|
||||
| `ls-fuse` | boolean | Moved from `/pcb` |
|
||||
|
||||
Thermal model parameters of the load switch (`internal-tref-max`, `mosfets-tj-max`,
|
||||
`mosfets-tau-ja`) move to the power switch binding.
|
||||
|
||||
### Example: MPPT 2420 HC (DC/DC and load output)
|
||||
|
||||
```dts
|
||||
adc-inputs {
|
||||
compatible = "libresolar,stm32-daq";
|
||||
daq_v_low: v-low { ... };
|
||||
daq_v_high: v-high { ... };
|
||||
daq_i_dcdc: i-dcdc { ... };
|
||||
daq_i_load: i-load { ... };
|
||||
daq_temp_fets: temp-fets { ... };
|
||||
};
|
||||
|
||||
power {
|
||||
bus_lv: bus-lv {
|
||||
compatible = "libresolar,dc-bus";
|
||||
voltage-channel = <&daq_v_low>;
|
||||
voltage-max-mv = <32000>;
|
||||
};
|
||||
|
||||
bus_hv: bus-hv {
|
||||
compatible = "libresolar,dc-bus";
|
||||
voltage-channel = <&daq_v_high>;
|
||||
voltage-max-mv = <90000>;
|
||||
};
|
||||
|
||||
dcdc: dcdc {
|
||||
compatible = "libresolar,dcdc";
|
||||
half-bridge = <&halfbridge>;
|
||||
high-bus = <&bus_hv>;
|
||||
low-bus = <&bus_lv>;
|
||||
inductor-current-channel = <&daq_i_dcdc>;
|
||||
current-max-ma = <20000>;
|
||||
temperature = <&ntc_fets>;
|
||||
ls-fuse;
|
||||
};
|
||||
|
||||
port_hv: port-hv {
|
||||
compatible = "libresolar,power-port";
|
||||
bus = <&bus_hv>;
|
||||
current-derived;
|
||||
};
|
||||
|
||||
port_lv: port-lv {
|
||||
compatible = "libresolar,power-port";
|
||||
bus = <&bus_lv>;
|
||||
current-derived;
|
||||
};
|
||||
|
||||
port_load: port-load {
|
||||
compatible = "libresolar,power-port";
|
||||
bus = <&bus_lv>;
|
||||
current-channel = <&daq_i_load>;
|
||||
switch = <&load_switch>;
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
### Example: PWM 2420 LUS (PWM switch, load and USB output)
|
||||
|
||||
```dts
|
||||
power {
|
||||
bus_lv: bus-lv {
|
||||
compatible = "libresolar,dc-bus";
|
||||
voltage-channel = <&daq_v_low>;
|
||||
voltage-max-mv = <32000>;
|
||||
};
|
||||
|
||||
port_pwm: port-pwm {
|
||||
compatible = "libresolar,power-port";
|
||||
bus = <&bus_lv>;
|
||||
switch = <&pwm_switch>;
|
||||
current-channel = <&daq_i_pwm>;
|
||||
current-inverted;
|
||||
current-switched;
|
||||
external-voltage-channel = <&daq_v_pwm>;
|
||||
filter-time-constant-ms = <1500>;
|
||||
};
|
||||
|
||||
port_lv: port-lv {
|
||||
compatible = "libresolar,power-port";
|
||||
bus = <&bus_lv>;
|
||||
current-derived;
|
||||
temperature = <&ntc_bat>;
|
||||
};
|
||||
|
||||
port_load: port-load {
|
||||
compatible = "libresolar,power-port";
|
||||
bus = <&bus_lv>;
|
||||
current-channel = <&daq_i_load>;
|
||||
switch = <&load_switch>;
|
||||
};
|
||||
|
||||
port_usb: port-usb {
|
||||
compatible = "libresolar,power-port";
|
||||
bus = <&bus_lv>;
|
||||
switch = <&usb_switch>; /* no current measurement */
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
The `power` node is a plain container (no compatible), so it can be extended by overlays.
|
||||
|
||||
## Software structure
|
||||
|
||||
### Power model module
|
||||
|
||||
New module `app/src/power_model.c` (moves to `subsys/power/` in a later step). It requires the
|
||||
C conversion of step 1b.
|
||||
|
||||
- Defines one `struct dc_bus` per `libresolar,dc-bus` node and one `struct power_port` per
|
||||
`libresolar,power-port` node with static initializers using `DT_FOREACH_STATUS_OKAY`.
|
||||
- Generates constant descriptor tables from the devicetree (DAQ channel indices, flags, filter
|
||||
constants, references between nodes as table indices).
|
||||
- `power_model_init()`: calibrates current sensor offsets (all current channels, as all switches
|
||||
and converters are off at startup), installs the DAQ sample filter for switched ports.
|
||||
- `power_model_update()`: replaces `daq_update()`, executed in the control thread. Order:
|
||||
bus voltages → measured/switched port currents → converter values and injections →
|
||||
Kirchhoff residuals → powers → filters → temperatures.
|
||||
- Access macros: `POWER_BUS(node_id)`, `POWER_PORT(node_id)`, e.g.
|
||||
`POWER_PORT(DT_NODELABEL(port_load))`.
|
||||
|
||||
The existing global names (`lv_bus`, `hv_bus`, `lv_terminal`, `hv_terminal`) become pointers to
|
||||
or macros for the model instances, so the remaining application code changes as little as
|
||||
possible in this step.
|
||||
|
||||
### Application objects with behavior
|
||||
|
||||
After step 1b, `struct load_output` and `struct pwm_switch` reference their port via a
|
||||
`struct power_port *port` pointer. In this step, the pointer is initialized with the model
|
||||
instance and the switch device is taken from the port node.
|
||||
|
||||
`struct dcdc` keeps its own fields (`inductor_current`, `power`, `temp_mosfets`), which are written by
|
||||
the power model, and gets its limits from the `libresolar,dcdc` node.
|
||||
|
||||
## Implementation steps
|
||||
|
||||
Each step keeps all boards building and the tests passing.
|
||||
|
||||
1. **Bindings and DAQ channel labels:** Add bindings, label all DAQ channels, add `power` nodes
|
||||
to all boards and the test overlay (not used by the code yet).
|
||||
2. **Power model for buses and ports:** `power_model.cpp` with measured, switched and derived
|
||||
currents and filtering. Replaces `daq_update()` and the bus/port definitions in `setup.cpp`.
|
||||
Unit tests on native_sim with the emulated DAQ and power switches, including a check that the
|
||||
results are identical to the previous `daq_update()` for the same readings.
|
||||
3. **Converter node:** `struct dcdc` instantiated from the `libresolar,dcdc` node, limits moved from
|
||||
`/pcb` (`hs-voltage-max`, `ls-voltage-max`, `dcdc-current-max`, `ls-fuse`), Kconfig symbol
|
||||
`HS_MOSFET_FAIL_SAFE_PROTECTION` reads `ls-fuse` from the new node.
|
||||
4. **Cleanup:** Remove `BOARD_HAS_*` macros where the model makes them obsolete, update docs.
|
||||
|
||||
## Open questions
|
||||
|
||||
1. ~~C or C++ for the model?~~ *Decided:* The application is converted to C before this step
|
||||
(step 1b of the migration plan).
|
||||
|
||||
2. **PWM switch as port or as converter?** Physically, the PWM switch connects the solar terminal
|
||||
to the battery bus, similar to a converter between a "PV bus" and the LV bus. Modelling it as
|
||||
a converter would be more consistent with the DC/DC, but `PwmSwitch::control()` uses the
|
||||
battery bus voltage and control targets directly.
|
||||
*Recommendation:* Keep it as a switched port in this step (as shown above) and revisit when
|
||||
the PWM control moves to the control framework.
|
||||
|
||||
3. ~~Composition vs. inheritance for `LoadOutput` / `PwmSwitch`?~~ *Decided:* Composition via
|
||||
port pointer, introduced with the C conversion in step 1b.
|
||||
|
||||
4. **Units in the devicetree:** Existing `/pcb` properties use volts and amps. Integer mV / mA
|
||||
allow fractional values (e.g. 7.5 A).
|
||||
*Recommendation:* mV / mA with `-mv` / `-ma` suffixes in the property names (used in this
|
||||
document). The existing `current-max` property of the power switches (in A) would be renamed
|
||||
accordingly.
|
||||
|
||||
5. **Unused channels:** `hv-ext-sense` (HPX, RC) and `i-dump-load` (Spinvert) are sampled but not
|
||||
used by the application today. They stay as DAQ channels without power model node.
|
||||
+10
-1
@@ -1,5 +1,14 @@
|
||||
Data Acquisition
|
||||
================
|
||||
|
||||
.. doxygenfile:: daq.h
|
||||
Driver
|
||||
------
|
||||
|
||||
.. doxygenfile:: drivers/daq.h
|
||||
:project: app
|
||||
|
||||
Application
|
||||
-----------
|
||||
|
||||
.. doxygenfile:: src/daq.h
|
||||
:project: app
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
Power Switch Driver
|
||||
===================
|
||||
|
||||
.. doxygenfile:: power_switch.h
|
||||
:project: app
|
||||
@@ -1,13 +1,65 @@
|
||||
Unit Tests
|
||||
==========
|
||||
|
||||
Core functions of the firmware are covered by unit-tests (located in ``tests`` directory). Build
|
||||
and run the tests with the following command:
|
||||
Core functions of the firmware are covered by unit tests based on the Zephyr ztest framework. The
|
||||
tests are located in the ``tests`` directory:
|
||||
|
||||
- ``tests/app``: Unit tests of the application (charger, DC/DC control, load output, etc.)
|
||||
- ``tests/drivers``: Tests of the drivers in this repository
|
||||
|
||||
All tests are run on ``native_sim`` with emulated drivers using twister:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
cd tests
|
||||
west build -p -b native_posix -t run
|
||||
west twister -T tests --integration
|
||||
|
||||
The driver tests are additionally built (but not run) for some of the boards to ensure that the
|
||||
drivers compile for all supported MCUs.
|
||||
|
||||
Hardware tests
|
||||
--------------
|
||||
|
||||
The half-bridge drivers configure timers which drive the MOSFETs of the power stage. A wrong
|
||||
configuration (e.g. missing dead time) could destroy the hardware. So the drivers are tested on a
|
||||
Nucleo-G474RE board instead of an actual charge controller. The outputs of the half bridge are
|
||||
wired to input capture channels of another timer (loop-back), which measures the frequency, duty
|
||||
cycle, dead time and overlap of the signals at the pins.
|
||||
|
||||
The test is located in ``tests/drivers/half_bridge_loopback`` and covers the drivers for
|
||||
advanced-control timers (TIM1), general-purpose timers (TIM3) and the HRTIM. The required wiring
|
||||
is described in ``boards/nucleo_g474re.overlay`` of the test.
|
||||
|
||||
Twister only runs the test on boards providing the ``half_bridge_loopback`` fixture, which has to
|
||||
be specified in a hardware map file (e.g. ``map.yaml``) together with the ID of the ST-LINK probe:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
- connected: true
|
||||
id: 00180012544B500420343637
|
||||
platform: nucleo_g474re
|
||||
product: STLINK-V3
|
||||
runner: pyocd
|
||||
serial: /dev/ttyACM0
|
||||
fixtures:
|
||||
- half_bridge_loopback
|
||||
|
||||
A hardware map with the IDs of the connected probes can be generated with
|
||||
``west twister --generate-hardware-map map.yaml``. Afterwards, run the tests with:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
west twister -T tests/drivers/half_bridge_loopback --device-testing --hardware-map map.yaml
|
||||
|
||||
Without a hardware map (e.g. in CI), the test is only built.
|
||||
|
||||
Firmware builds
|
||||
---------------
|
||||
|
||||
The firmware can be built for all boards and board revisions in the same way as the tests:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
west twister -T app --integration
|
||||
|
||||
|
||||
Conformance testing
|
||||
|
||||
@@ -25,6 +25,7 @@ Afterwards, most important folders in your workspace will look like this:
|
||||
├── charge-controller-firmware/ # application firmware repository
|
||||
│ ├── app/ # application source files
|
||||
│ ├── boards/ # board specifications
|
||||
│ ├── drivers/ # driver source files
|
||||
│ ├── tests/ # unit test source files
|
||||
│ └── west.yml # main manifest file
|
||||
│
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
add_subdirectory_ifdef(CONFIG_DAQ daq)
|
||||
add_subdirectory_ifdef(CONFIG_HALF_BRIDGE half_bridge)
|
||||
add_subdirectory_ifdef(CONFIG_POWER_SWITCH power_switch)
|
||||
add_subdirectory_ifdef(CONFIG_LIBRESOLAR_STM32_TIM stm32_tim)
|
||||
add_subdirectory(sensor)
|
||||
@@ -0,0 +1,12 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
menu "Libre Solar drivers"
|
||||
|
||||
rsource "daq/Kconfig"
|
||||
rsource "half_bridge/Kconfig"
|
||||
rsource "power_switch/Kconfig"
|
||||
rsource "sensor/Kconfig"
|
||||
rsource "stm32_tim/Kconfig"
|
||||
|
||||
endmenu
|
||||
@@ -0,0 +1,9 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
|
||||
zephyr_library_sources(daq_common.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_DAQ_STM32_CHSEL daq_stm32_chsel.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_DAQ_STM32_RANK daq_stm32_rank.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_DAQ_EMUL daq_emul.c)
|
||||
@@ -0,0 +1,54 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
menuconfig DAQ
|
||||
bool "Data acquisition (DAQ) drivers"
|
||||
default y if DT_HAS_LIBRESOLAR_STM32_DAQ_ENABLED
|
||||
default y if DT_HAS_LIBRESOLAR_DAQ_EMUL_ENABLED
|
||||
help
|
||||
Enable drivers for data acquisition, i.e. continuous sampling, filtering and limit
|
||||
checking of analog inputs.
|
||||
|
||||
if DAQ
|
||||
|
||||
config DAQ_INIT_PRIORITY
|
||||
int "DAQ init priority"
|
||||
default KERNEL_INIT_PRIORITY_DEVICE
|
||||
help
|
||||
DAQ driver device initialization priority.
|
||||
|
||||
module = DAQ
|
||||
module-str = daq
|
||||
source "subsys/logging/Kconfig.template.log_config"
|
||||
|
||||
config DAQ_STM32_CHSEL
|
||||
bool "STM32 DAQ driver for ADCs with fixed channel sequence"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_STM32_DAQ_ENABLED
|
||||
depends on SOC_SERIES_STM32F0X || SOC_SERIES_STM32L0X || SOC_SERIES_STM32G0X
|
||||
select USE_STM32_LL_ADC
|
||||
select USE_STM32_LL_DAC
|
||||
select USE_STM32_LL_DMA
|
||||
help
|
||||
Enable DAQ driver for STM32 ADCs converting the selected channels in ascending order
|
||||
(STM32F0, STM32L0 and STM32G0).
|
||||
|
||||
config DAQ_STM32_RANK
|
||||
bool "STM32 DAQ driver for ADCs with configurable channel sequence"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_STM32_DAQ_ENABLED
|
||||
depends on SOC_SERIES_STM32G4X
|
||||
select USE_STM32_LL_ADC
|
||||
select USE_STM32_LL_DAC
|
||||
select USE_STM32_LL_DMA
|
||||
help
|
||||
Enable DAQ driver for STM32 ADCs with ranks to define the channel sequence (STM32G4).
|
||||
|
||||
config DAQ_EMUL
|
||||
bool "Emulated DAQ driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_DAQ_EMUL_ENABLED
|
||||
help
|
||||
Enable emulated DAQ driver for tests on native_sim.
|
||||
|
||||
endif # DAQ
|
||||
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "daq_common.h"
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
LOG_MODULE_REGISTER(daq, CONFIG_DAQ_LOG_LEVEL);
|
||||
|
||||
static inline bool channel_valid(const struct daq_common_config *config, int channel)
|
||||
{
|
||||
return channel >= 0 && channel < config->num_channels;
|
||||
}
|
||||
|
||||
static inline uint32_t filtered_raw(const struct device *dev, int channel)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
const struct daq_common_data *data = dev->data;
|
||||
|
||||
return data->channels[channel].filtered >> config->filter_const[channel];
|
||||
}
|
||||
|
||||
static inline float raw_to_voltage(int32_t raw, int32_t vref_mv)
|
||||
{
|
||||
return (raw * vref_mv) / ((float)DAQ_RAW_SCALE * 1000);
|
||||
}
|
||||
|
||||
int daq_common_init(const struct device *dev)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
data->channels[i].offset = config->offset[i];
|
||||
|
||||
#ifdef CONFIG_GPIO
|
||||
const struct gpio_dt_spec *enable = &config->enable_gpios[i];
|
||||
|
||||
if (enable->port != NULL) {
|
||||
if (!gpio_is_ready_dt(enable)) {
|
||||
LOG_ERR("Enable GPIO of channel %d not ready", i);
|
||||
return -ENODEV;
|
||||
}
|
||||
gpio_pin_configure_dt(enable, GPIO_OUTPUT_ACTIVE);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void check_alert(const struct device *dev, int channel, struct daq_alert *alert,
|
||||
bool exceeded)
|
||||
{
|
||||
daq_alert_callback_t callback = alert->callback;
|
||||
|
||||
if (alert->debounce_ms < INT16_MAX) {
|
||||
alert->debounce_ms++;
|
||||
}
|
||||
|
||||
if (callback != NULL && exceeded) {
|
||||
if (alert->debounce_ms > 1) {
|
||||
callback(dev, channel);
|
||||
}
|
||||
}
|
||||
else if (alert->debounce_ms > 0) {
|
||||
// reset debounce counter only if already close to triggering to allow setting negative
|
||||
// values to specify a one-time inhibit delay
|
||||
alert->debounce_ms = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void daq_common_process(const struct device *dev, int channel)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
struct daq_common_data *data = dev->data;
|
||||
struct daq_channel_data *ch = &data->channels[channel];
|
||||
const uint16_t raw = data->buf[ch->buf_idx];
|
||||
daq_sample_filter_t filter = data->sample_filter;
|
||||
|
||||
if (filter == NULL || (data->sample_filter_mask & BIT(channel)) == 0 || filter(dev, channel)) {
|
||||
/*
|
||||
* Low-pass filtering of raw readings
|
||||
* (see also: http://techteach.no/simview/lowpass_filter/doc/filter_algorithm.pdf)
|
||||
*
|
||||
* y(n) = c * x(n) + (1 - c) * y(n-1) with filter constant c = 1/(2^filter_const)
|
||||
*
|
||||
* The filtered value is stored left-shifted by filter_const, so that the new raw reading
|
||||
* can be added without shifting (equivalent to the multiplication with c).
|
||||
*/
|
||||
ch->filtered = raw + ch->filtered - (ch->filtered >> config->filter_const[channel]);
|
||||
}
|
||||
|
||||
check_alert(dev, channel, &ch->alerts[DAQ_ALERT_UPPER],
|
||||
raw >= ch->alerts[DAQ_ALERT_UPPER].limit);
|
||||
check_alert(dev, channel, &ch->alerts[DAQ_ALERT_LOWER],
|
||||
raw <= ch->alerts[DAQ_ALERT_LOWER].limit);
|
||||
}
|
||||
|
||||
int daq_get_raw(const struct device *dev, int channel)
|
||||
{
|
||||
if (!channel_valid(dev->config, channel)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return filtered_raw(dev, channel);
|
||||
}
|
||||
|
||||
int daq_get_vref_mv(const struct device *dev)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
const struct daq_common_data *data = dev->data;
|
||||
|
||||
if (data->vref_mv != 0) {
|
||||
return data->vref_mv;
|
||||
}
|
||||
|
||||
if (config->vref_channel < 0 || config->vrefint_cal == NULL) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
// factory calibration values are 12-bit right-aligned readings
|
||||
uint32_t vrefint_raw = filtered_raw(dev, config->vref_channel) >> 4;
|
||||
if (vrefint_raw == 0) {
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
return config->vrefint_cal_mv * *config->vrefint_cal / vrefint_raw;
|
||||
}
|
||||
|
||||
float daq_get_pin_voltage(const struct device *dev, int channel)
|
||||
{
|
||||
int vref_mv = daq_get_vref_mv(dev);
|
||||
|
||||
if (!channel_valid(dev->config, channel) || vref_mv < 0) {
|
||||
return 0.0F;
|
||||
}
|
||||
|
||||
return raw_to_voltage(filtered_raw(dev, channel), vref_mv);
|
||||
}
|
||||
|
||||
float daq_get_value(const struct device *dev, int channel)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
const struct daq_common_data *data = dev->data;
|
||||
int vref_mv = daq_get_vref_mv(dev);
|
||||
|
||||
if (!channel_valid(config, channel) || vref_mv < 0) {
|
||||
return 0.0F;
|
||||
}
|
||||
|
||||
int32_t raw = (int32_t)filtered_raw(dev, channel) - data->channels[channel].offset;
|
||||
|
||||
return raw_to_voltage(raw, vref_mv) * config->gain[channel];
|
||||
}
|
||||
|
||||
int daq_get_internal_temp(const struct device *dev, float *temp)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
int vref_mv = daq_get_vref_mv(dev);
|
||||
|
||||
if (config->temp_channel < 0 || config->ts_cal1 == NULL || config->ts_cal2 == NULL) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
if (vref_mv < 0) {
|
||||
return vref_mv;
|
||||
}
|
||||
|
||||
// calibration values are 12-bit right-aligned readings taken at ts_cal_mv reference voltage
|
||||
int32_t raw = (filtered_raw(dev, config->temp_channel) >> 4) * vref_mv / config->ts_cal_mv;
|
||||
int32_t cal1 = *config->ts_cal1;
|
||||
int32_t cal2 = *config->ts_cal2;
|
||||
|
||||
*temp = (float)(config->ts_cal2_temp - config->ts_cal1_temp) / (cal2 - cal1) * (raw - cal1)
|
||||
+ config->ts_cal1_temp;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int daq_calibrate_offset(const struct device *dev, int channel)
|
||||
{
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
if (!channel_valid(dev->config, channel)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
data->channels[channel].offset = filtered_raw(dev, channel);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int daq_set_alert(const struct device *dev, int channel, enum daq_alert_type type, float limit,
|
||||
daq_alert_callback_t callback)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
struct daq_common_data *data = dev->data;
|
||||
int vref_mv = daq_get_vref_mv(dev);
|
||||
|
||||
if (!channel_valid(config, channel) || (type != DAQ_ALERT_UPPER && type != DAQ_ALERT_LOWER)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (vref_mv < 0) {
|
||||
return vref_mv;
|
||||
}
|
||||
|
||||
struct daq_channel_data *ch = &data->channels[channel];
|
||||
const float scale = ((float)DAQ_RAW_SCALE * 1000 / config->gain[channel]) / vref_mv;
|
||||
float raw = limit * scale + ch->offset;
|
||||
|
||||
// even if we have a higher limit, we must limit it to the range of possible readings
|
||||
ch->alerts[type].limit = raw > UINT16_MAX ? UINT16_MAX : (raw < 0.0F ? 0 : (uint16_t)raw);
|
||||
ch->alerts[type].callback = callback;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int daq_inhibit_alert(const struct device *dev, int channel, enum daq_alert_type type,
|
||||
int16_t timeout_ms)
|
||||
{
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
if (!channel_valid(dev->config, channel)
|
||||
|| (type != DAQ_ALERT_UPPER && type != DAQ_ALERT_LOWER))
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
// set negative value so that we get a final debouncing of this timeout + the original delay
|
||||
data->channels[channel].alerts[type].debounce_ms = -timeout_ms;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int daq_set_sample_filter(const struct device *dev, daq_sample_filter_t filter,
|
||||
uint32_t channel_mask)
|
||||
{
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
data->sample_filter = NULL;
|
||||
data->sample_filter_mask = channel_mask;
|
||||
data->sample_filter = filter;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int daq_set_sample_callback(const struct device *dev, daq_sample_callback_t callback)
|
||||
{
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
data->sample_callback = callback;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef LIBRESOLAR_DRIVERS_DAQ_COMMON_H_
|
||||
#define LIBRESOLAR_DRIVERS_DAQ_COMMON_H_
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Common part of the DAQ drivers (filtering, alerts and scaling)
|
||||
*
|
||||
* Backends store new samples in a buffer and call daq_common_process() for each channel. The
|
||||
* config and data structs of the backends must start with struct daq_common_config and
|
||||
* struct daq_common_data.
|
||||
*/
|
||||
|
||||
#include <libresolar/drivers/daq.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** Sampling period of all DAQ backends (alert debouncing assumes 1 ms) */
|
||||
#define DAQ_SAMPLE_PERIOD_MS 1
|
||||
|
||||
struct daq_alert
|
||||
{
|
||||
volatile daq_alert_callback_t callback;
|
||||
volatile uint16_t limit;
|
||||
volatile int16_t debounce_ms;
|
||||
};
|
||||
|
||||
struct daq_channel_data
|
||||
{
|
||||
/** Filtered raw reading left-shifted by filter_const */
|
||||
volatile uint32_t filtered;
|
||||
struct daq_alert alerts[2];
|
||||
/** Offset to be subtracted from the raw reading */
|
||||
uint16_t offset;
|
||||
/** Position of the channel in the sample buffer */
|
||||
uint8_t buf_idx;
|
||||
};
|
||||
|
||||
struct daq_common_config
|
||||
{
|
||||
const uint8_t *filter_const;
|
||||
const float *gain;
|
||||
const uint16_t *offset;
|
||||
const uint8_t *adc_channel;
|
||||
const struct gpio_dt_spec *enable_gpios;
|
||||
/** Factory calibration values (NULL if not available) */
|
||||
const uint16_t *vrefint_cal;
|
||||
const uint16_t *ts_cal1;
|
||||
const uint16_t *ts_cal2;
|
||||
uint16_t vrefint_cal_mv;
|
||||
uint16_t ts_cal_mv;
|
||||
int16_t ts_cal1_temp;
|
||||
int16_t ts_cal2_temp;
|
||||
/** Channel indices of internal reference and temperature sensor (-1 if not available) */
|
||||
int8_t vref_channel;
|
||||
int8_t temp_channel;
|
||||
uint8_t num_channels;
|
||||
};
|
||||
|
||||
struct daq_common_data
|
||||
{
|
||||
struct daq_channel_data *channels;
|
||||
/** Buffer with the latest raw sample of each channel */
|
||||
volatile uint16_t *buf;
|
||||
daq_sample_filter_t sample_filter;
|
||||
uint32_t sample_filter_mask;
|
||||
daq_sample_callback_t sample_callback;
|
||||
/** Fixed reference voltage or 0 if calculated from the internal reference */
|
||||
uint16_t vref_mv;
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize the common part (offsets and enable GPIOs)
|
||||
*
|
||||
* The backend has to set the buffer and the buffer indices afterwards.
|
||||
*/
|
||||
int daq_common_init(const struct device *dev);
|
||||
|
||||
/** Filter the latest sample of a channel and check its alerts */
|
||||
void daq_common_process(const struct device *dev, int channel);
|
||||
|
||||
/** Call the sample callback after a new sample of all channels was processed */
|
||||
static inline void daq_common_sample_done(const struct device *dev)
|
||||
{
|
||||
const struct daq_common_data *data = dev->data;
|
||||
daq_sample_callback_t callback = data->sample_callback;
|
||||
|
||||
if (callback != NULL) {
|
||||
callback(dev);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Helper macros to create the common config of a backend from the devicetree
|
||||
*/
|
||||
|
||||
#define DAQ_FILTER_CONST(child) DT_PROP(child, filter_const),
|
||||
#define DAQ_GAIN(child) ((float)DT_PROP(child, multiplier) / DT_PROP(child, divider)),
|
||||
#define DAQ_OFFSET(child) DT_PROP_OR(child, offset, 0),
|
||||
#define DAQ_ADC_CHANNEL(child) DT_IO_CHANNELS_INPUT(child),
|
||||
#define DAQ_ENABLE_GPIO(child) GPIO_DT_SPEC_GET_OR(child, enable_gpios, { 0 }),
|
||||
|
||||
#define DAQ_CHILD_IDX_OR_NONE(node_id, name) \
|
||||
COND_CODE_1(DT_NODE_EXISTS(DT_CHILD(node_id, name)), \
|
||||
(DT_NODE_CHILD_IDX(DT_CHILD(node_id, name))), (-1))
|
||||
|
||||
/** Define the arrays used by DAQ_COMMON_CONFIG_INIT() */
|
||||
#define DAQ_COMMON_DEFINE(node_id) \
|
||||
BUILD_ASSERT(DT_CHILD_NUM(node_id) <= 32, "Too many DAQ channels"); \
|
||||
static const uint8_t daq_filter_const[] = { DT_FOREACH_CHILD(node_id, DAQ_FILTER_CONST) }; \
|
||||
static const float daq_gain[] = { DT_FOREACH_CHILD(node_id, DAQ_GAIN) }; \
|
||||
static const uint16_t daq_offset[] = { DT_FOREACH_CHILD(node_id, DAQ_OFFSET) }; \
|
||||
static const uint8_t daq_adc_channel[] = { DT_FOREACH_CHILD(node_id, DAQ_ADC_CHANNEL) }; \
|
||||
static const struct gpio_dt_spec daq_enable_gpios[] = { DT_FOREACH_CHILD(node_id, \
|
||||
DAQ_ENABLE_GPIO) }; \
|
||||
static struct daq_channel_data daq_channels[DT_CHILD_NUM(node_id)]; \
|
||||
static volatile uint16_t daq_buf[DT_CHILD_NUM(node_id)]
|
||||
|
||||
/** Initializer for struct daq_common_config (without calibration values) */
|
||||
#define DAQ_COMMON_CONFIG_INIT(node_id) \
|
||||
.filter_const = daq_filter_const, .gain = daq_gain, .offset = daq_offset, \
|
||||
.adc_channel = daq_adc_channel, .enable_gpios = daq_enable_gpios, \
|
||||
.vref_channel = DAQ_CHILD_IDX_OR_NONE(node_id, vref_mcu), \
|
||||
.temp_channel = DAQ_CHILD_IDX_OR_NONE(node_id, temp_mcu), \
|
||||
.num_channels = DT_CHILD_NUM(node_id)
|
||||
|
||||
/** Initializer for struct daq_common_data */
|
||||
#define DAQ_COMMON_DATA_INIT() .channels = daq_channels, .buf = daq_buf
|
||||
|
||||
#endif /* LIBRESOLAR_DRIVERS_DAQ_COMMON_H_ */
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Emulated DAQ driver for tests on native_sim
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT libresolar_daq_emul
|
||||
|
||||
#include "daq_common.h"
|
||||
|
||||
#include <libresolar/drivers/daq_emul.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
|
||||
BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) <= 1, "Only one DAQ instance supported");
|
||||
|
||||
#define DAQ_NODE DT_DRV_INST(0)
|
||||
|
||||
/*
|
||||
* Typical calibration values of an STM32 temperature sensor (0.76 V at 30 °C, 2.5 mV/°C)
|
||||
* as 12-bit readings at 3.3 V
|
||||
*/
|
||||
static const uint16_t ts_cal1 = 4096 * 760 / 3300;
|
||||
static const uint16_t ts_cal2 = 4096 * 1010 / 3300;
|
||||
|
||||
DAQ_COMMON_DEFINE(DAQ_NODE);
|
||||
|
||||
static const struct daq_common_config daq_emul_config = {
|
||||
DAQ_COMMON_CONFIG_INIT(DAQ_NODE),
|
||||
.ts_cal1 = &ts_cal1,
|
||||
.ts_cal2 = &ts_cal2,
|
||||
.ts_cal_mv = 3300,
|
||||
.ts_cal1_temp = 30,
|
||||
.ts_cal2_temp = 130,
|
||||
};
|
||||
|
||||
static struct daq_common_data daq_emul_data = {
|
||||
DAQ_COMMON_DATA_INIT(),
|
||||
.vref_mv = DAQ_EMUL_VREF_MV,
|
||||
};
|
||||
|
||||
static inline bool channel_valid(const struct device *dev, int channel)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
|
||||
return channel >= 0 && channel < config->num_channels;
|
||||
}
|
||||
|
||||
int daq_emul_set_raw(const struct device *dev, int channel, uint16_t raw)
|
||||
{
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
if (!channel_valid(dev, channel)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
data->buf[data->channels[channel].buf_idx] = raw;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int daq_emul_set_value(const struct device *dev, int channel, float value)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
if (!channel_valid(dev, channel)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
// 12-bit resolution as provided by the ADC
|
||||
float raw12 = (value / config->gain[channel]) / (DAQ_EMUL_VREF_MV / 1000.0F) * 4096;
|
||||
raw12 = CLAMP(raw12, -4096.0F, 4096.0F);
|
||||
|
||||
int32_t raw = (int32_t)raw12 * 16 + data->channels[channel].offset;
|
||||
|
||||
return daq_emul_set_raw(dev, channel, CLAMP(raw, 0, UINT16_MAX));
|
||||
}
|
||||
|
||||
int daq_emul_process(const struct device *dev, int channel)
|
||||
{
|
||||
if (!channel_valid(dev, channel)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
daq_common_process(dev, channel);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void daq_emul_process_all(const struct device *dev)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
daq_common_process(dev, i);
|
||||
}
|
||||
|
||||
daq_common_sample_done(dev);
|
||||
}
|
||||
|
||||
void daq_emul_preset_filtered(const struct device *dev)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
struct daq_channel_data *ch = &data->channels[i];
|
||||
|
||||
ch->filtered = (uint32_t)data->buf[ch->buf_idx] << config->filter_const[i];
|
||||
}
|
||||
}
|
||||
|
||||
void daq_emul_reset_filtered(const struct device *dev)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
data->channels[i].filtered = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int daq_emul_init(const struct device *dev)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
data->channels[i].buf_idx = i;
|
||||
}
|
||||
|
||||
return daq_common_init(dev);
|
||||
}
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, daq_emul_init, NULL, &daq_emul_data, &daq_emul_config, POST_KERNEL,
|
||||
CONFIG_DAQ_INIT_PRIORITY, NULL);
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef LIBRESOLAR_DRIVERS_DAQ_STM32_H_
|
||||
#define LIBRESOLAR_DRIVERS_DAQ_STM32_H_
|
||||
|
||||
#include <stm32_ll_adc.h>
|
||||
|
||||
/**
|
||||
* Initializer for the factory calibration values in struct daq_common_config
|
||||
*
|
||||
* The addresses and calibration conditions are defined in the LL header of each STM32 series.
|
||||
*/
|
||||
#define DAQ_STM32_CAL_CONFIG_INIT() \
|
||||
.vrefint_cal = VREFINT_CAL_ADDR, .vrefint_cal_mv = VREFINT_CAL_VREF, \
|
||||
.ts_cal1 = TEMPSENSOR_CAL1_ADDR, .ts_cal2 = TEMPSENSOR_CAL2_ADDR, \
|
||||
.ts_cal_mv = TEMPSENSOR_CAL_VREFANALOG, .ts_cal1_temp = TEMPSENSOR_CAL1_TEMP, \
|
||||
.ts_cal2_temp = TEMPSENSOR_CAL2_TEMP
|
||||
|
||||
#endif /* LIBRESOLAR_DRIVERS_DAQ_STM32_H_ */
|
||||
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief DAQ driver for STM32 ADCs with a fixed channel sequence (STM32F0, STM32L0, STM32G0)
|
||||
*
|
||||
* The channels are selected with a bit mask and converted in ascending order of the channel
|
||||
* numbers. All channels have to be connected to ADC1.
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT libresolar_stm32_daq
|
||||
|
||||
#include "daq_common.h"
|
||||
#include "daq_stm32.h"
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/irq.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include <soc.h>
|
||||
#include <stm32_ll_adc.h>
|
||||
#include <stm32_ll_bus.h>
|
||||
#include <stm32_ll_dac.h>
|
||||
#include <stm32_ll_dma.h>
|
||||
|
||||
LOG_MODULE_DECLARE(daq, CONFIG_DAQ_LOG_LEVEL);
|
||||
|
||||
BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) <= 1, "Only one DAQ instance supported");
|
||||
|
||||
#define DAQ_NODE DT_DRV_INST(0)
|
||||
|
||||
#define ADC_CHSEL_FN(child) BIT(DT_IO_CHANNELS_INPUT(child)) |
|
||||
#define ADC_CHSEL (DT_FOREACH_CHILD(DAQ_NODE, ADC_CHSEL_FN) 0)
|
||||
|
||||
/* priority 2: second-lowest value for STM32L0/F0 */
|
||||
#define DMA_IRQ_PRIORITY 2
|
||||
|
||||
struct daq_stm32_data
|
||||
{
|
||||
struct daq_common_data common;
|
||||
struct k_timer trigger_timer;
|
||||
};
|
||||
|
||||
DAQ_COMMON_DEFINE(DAQ_NODE);
|
||||
|
||||
static const struct daq_common_config daq_stm32_config = {
|
||||
DAQ_COMMON_CONFIG_INIT(DAQ_NODE),
|
||||
DAQ_STM32_CAL_CONFIG_INIT(),
|
||||
};
|
||||
|
||||
static struct daq_stm32_data daq_stm32_data = {
|
||||
.common = { DAQ_COMMON_DATA_INIT() },
|
||||
};
|
||||
|
||||
static void dac_setup(void)
|
||||
{
|
||||
/* DAC1 at PA4 as reference voltage for current measurement amplifiers at 0.1 * VCC */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_DAC1);
|
||||
LL_DAC_SetOutputBuffer(DAC1, LL_DAC_CHANNEL_1, LL_DAC_OUTPUT_BUFFER_ENABLE);
|
||||
LL_DAC_Enable(DAC1, LL_DAC_CHANNEL_1);
|
||||
LL_DAC_ConvertData12RightAligned(DAC1, LL_DAC_CHANNEL_1, 4096 / 10);
|
||||
}
|
||||
|
||||
static int adc_setup(ADC_TypeDef *adc)
|
||||
{
|
||||
#if defined(CONFIG_SOC_SERIES_STM32F0X)
|
||||
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_ADC1);
|
||||
LL_ADC_SetClock(adc, LL_ADC_CLOCK_SYNC_PCLK_DIV4);
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
|
||||
LL_ADC_EnableInternalRegulator(adc);
|
||||
k_busy_wait(LL_ADC_DELAY_INTERNAL_REGUL_STAB_US);
|
||||
LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(adc), LL_ADC_CLOCK_SYNC_PCLK_DIV4);
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G0X)
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC);
|
||||
LL_ADC_SetClock(adc, LL_ADC_CLOCK_SYNC_PCLK_DIV4);
|
||||
LL_ADC_EnableInternalRegulator(adc);
|
||||
k_busy_wait(LL_ADC_DELAY_INTERNAL_REGUL_STAB_US);
|
||||
#endif
|
||||
|
||||
if (LL_ADC_IsEnabled(adc)) {
|
||||
LL_ADC_Disable(adc);
|
||||
}
|
||||
|
||||
LL_ADC_StartCalibration(adc);
|
||||
while (LL_ADC_IsCalibrationOnGoing(adc)) {
|
||||
}
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G0X)
|
||||
// a few ADC clock cycles are required between end of calibration and enabling the ADC
|
||||
k_busy_wait(1);
|
||||
#endif
|
||||
|
||||
if (LL_ADC_IsActiveFlag_ADRDY(adc)) {
|
||||
LL_ADC_ClearFlag_ADRDY(adc);
|
||||
}
|
||||
|
||||
// Enable internal reference voltage and temperature
|
||||
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(adc),
|
||||
LL_ADC_PATH_INTERNAL_VREFINT | LL_ADC_PATH_INTERNAL_TEMPSENSOR);
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32F0X)
|
||||
LL_ADC_REG_SetSequencerChannels(adc, ADC_CHSEL);
|
||||
LL_ADC_SetSamplingTimeCommonChannels(adc, LL_ADC_SAMPLINGTIME_239CYCLES_5);
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
LL_ADC_REG_SetSequencerChannels(adc, ADC_CHSEL);
|
||||
LL_ADC_SetSamplingTimeCommonChannels(adc, LL_ADC_SAMPLINGTIME_160CYCLES_5);
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G0X)
|
||||
LL_ADC_REG_SetSequencerConfigurable(adc, LL_ADC_REG_SEQ_FIXED);
|
||||
LL_ADC_ClearFlag_CCRDY(adc);
|
||||
LL_ADC_REG_SetSequencerChannels(adc, ADC_CHSEL);
|
||||
|
||||
// channel configuration must be applied before starting conversions
|
||||
int timeout_us = 1000;
|
||||
while (!LL_ADC_IsActiveFlag_CCRDY(adc) && timeout_us-- > 0) {
|
||||
k_busy_wait(1);
|
||||
}
|
||||
if (timeout_us < 0) {
|
||||
LOG_ERR("ADC channel configuration timed out");
|
||||
return -ETIMEDOUT;
|
||||
}
|
||||
LL_ADC_ClearFlag_CCRDY(adc);
|
||||
|
||||
LL_ADC_SetSamplingTimeCommonChannels(adc, LL_ADC_SAMPLINGTIME_COMMON_1,
|
||||
LL_ADC_SAMPLINGTIME_160CYCLES_5);
|
||||
#endif
|
||||
|
||||
LL_ADC_SetDataAlignment(adc, LL_ADC_DATA_ALIGN_LEFT);
|
||||
LL_ADC_SetResolution(adc, LL_ADC_RESOLUTION_12B);
|
||||
LL_ADC_REG_SetOverrun(adc, LL_ADC_REG_OVR_DATA_OVERWRITTEN);
|
||||
// Enable DMA transfer on ADC and circular mode
|
||||
LL_ADC_REG_SetDMATransfer(adc, LL_ADC_REG_DMA_TRANSFER_UNLIMITED);
|
||||
|
||||
LL_ADC_Enable(adc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void dma_isr(const void *arg)
|
||||
{
|
||||
const struct device *dev = arg;
|
||||
const struct daq_common_config *config = dev->config;
|
||||
|
||||
if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) { // transfer completed on DMA channel 1
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
daq_common_process(dev, i);
|
||||
}
|
||||
daq_common_sample_done(dev);
|
||||
}
|
||||
DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt flags
|
||||
}
|
||||
|
||||
static void dma_setup(const struct device *dev)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
struct daq_common_data *data = dev->data;
|
||||
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32G0X)
|
||||
LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_1, LL_DMAMUX_REQ_ADC1);
|
||||
#endif
|
||||
|
||||
LL_DMA_ConfigAddresses(DMA1, LL_DMA_CHANNEL_1,
|
||||
LL_ADC_DMA_GetRegAddr(ADC1, LL_ADC_DMA_REG_REGULAR_DATA),
|
||||
(uint32_t)data->buf, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
|
||||
|
||||
// Configure the number of DMA transfers (data length in multiples of size per transfer)
|
||||
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_1, config->num_channels);
|
||||
|
||||
LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
|
||||
LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD);
|
||||
LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_HALFWORD);
|
||||
LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1); // transfer error interrupt
|
||||
LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1); // transfer complete interrupt
|
||||
LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
|
||||
|
||||
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
|
||||
|
||||
IRQ_CONNECT(DMA1_Channel1_IRQn, DMA_IRQ_PRIORITY, dma_isr, DEVICE_DT_INST_GET(0), 0);
|
||||
irq_enable(DMA1_Channel1_IRQn);
|
||||
}
|
||||
|
||||
static void trigger_conversion(struct k_timer *timer)
|
||||
{
|
||||
ARG_UNUSED(timer);
|
||||
|
||||
LL_ADC_REG_StartConversion(ADC1);
|
||||
}
|
||||
|
||||
static int daq_stm32_init(const struct device *dev)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
struct daq_stm32_data *data = dev->data;
|
||||
int err;
|
||||
|
||||
// channels are converted and stored by the DMA in ascending order of the channel numbers
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
uint8_t idx = 0;
|
||||
|
||||
for (int j = 0; j < config->num_channels; j++) {
|
||||
if (config->adc_channel[j] < config->adc_channel[i]) {
|
||||
idx++;
|
||||
}
|
||||
else if (config->adc_channel[j] == config->adc_channel[i] && j != i) {
|
||||
LOG_ERR("ADC channel %d used more than once", config->adc_channel[i]);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
data->common.channels[i].buf_idx = idx;
|
||||
}
|
||||
|
||||
err = daq_common_init(dev);
|
||||
if (err != 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
dac_setup();
|
||||
|
||||
err = adc_setup(ADC1);
|
||||
if (err != 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
dma_setup(dev);
|
||||
LL_ADC_REG_StartConversion(ADC1);
|
||||
|
||||
k_timer_init(&data->trigger_timer, trigger_conversion, NULL);
|
||||
k_timer_start(&data->trigger_timer, K_MSEC(DAQ_SAMPLE_PERIOD_MS), K_MSEC(DAQ_SAMPLE_PERIOD_MS));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, daq_stm32_init, NULL, &daq_stm32_data, &daq_stm32_config, POST_KERNEL,
|
||||
CONFIG_DAQ_INIT_PRIORITY, NULL);
|
||||
@@ -0,0 +1,342 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief DAQ driver for STM32 ADCs with a configurable channel sequence (STM32G4)
|
||||
*
|
||||
* The channels can be connected to ADC1 and ADC2. They are converted in the order of the
|
||||
* devicetree nodes. The reference voltage is generated by the internal reference buffer
|
||||
* (VREFBUF) with 2.048 V.
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT libresolar_stm32_daq
|
||||
|
||||
#include "daq_common.h"
|
||||
#include "daq_stm32.h"
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/irq.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include <soc.h>
|
||||
#include <stm32_ll_adc.h>
|
||||
#include <stm32_ll_bus.h>
|
||||
#include <stm32_ll_dac.h>
|
||||
#include <stm32_ll_dma.h>
|
||||
#include <stm32_ll_system.h>
|
||||
|
||||
LOG_MODULE_DECLARE(daq, CONFIG_DAQ_LOG_LEVEL);
|
||||
|
||||
BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) <= 1, "Only one DAQ instance supported");
|
||||
|
||||
#define DAQ_NODE DT_DRV_INST(0)
|
||||
|
||||
/* priority 2: second-lowest value for STM32L0/F0 */
|
||||
#define DMA_IRQ_PRIORITY 2
|
||||
|
||||
/* reference voltage generated by VREFBUF */
|
||||
#define VREF_MV 2048
|
||||
|
||||
#define ADC_BASE_FN(child) DT_REG_ADDR(DT_IO_CHANNELS_CTLR(child)),
|
||||
static const uint32_t adc_base[] = { DT_FOREACH_CHILD(DAQ_NODE, ADC_BASE_FN) };
|
||||
|
||||
// Depending on the channel, not just a single bit has to be set in the right location. So we need
|
||||
// a map to get correct bit settings for each channel.
|
||||
static const uint32_t table_channel[] = {
|
||||
LL_ADC_CHANNEL_0,
|
||||
LL_ADC_CHANNEL_1,
|
||||
LL_ADC_CHANNEL_2,
|
||||
LL_ADC_CHANNEL_3,
|
||||
LL_ADC_CHANNEL_4,
|
||||
LL_ADC_CHANNEL_5,
|
||||
LL_ADC_CHANNEL_6,
|
||||
LL_ADC_CHANNEL_7,
|
||||
LL_ADC_CHANNEL_8,
|
||||
LL_ADC_CHANNEL_9,
|
||||
LL_ADC_CHANNEL_10,
|
||||
LL_ADC_CHANNEL_11,
|
||||
LL_ADC_CHANNEL_12,
|
||||
LL_ADC_CHANNEL_13,
|
||||
LL_ADC_CHANNEL_14,
|
||||
LL_ADC_CHANNEL_15,
|
||||
LL_ADC_CHANNEL_TEMPSENSOR_ADC1,
|
||||
LL_ADC_CHANNEL_VBAT,
|
||||
LL_ADC_CHANNEL_VREFINT,
|
||||
};
|
||||
|
||||
// Rank and sequence definitions for STM32G4 from adc_stm32.c Zephyr driver
|
||||
|
||||
#define RANK(n) LL_ADC_REG_RANK_##n
|
||||
static const uint32_t table_rank[] = {
|
||||
RANK(1), RANK(2), RANK(3), RANK(4), RANK(5), RANK(6), RANK(7), RANK(8),
|
||||
RANK(9), RANK(10), RANK(11), RANK(12), RANK(13), RANK(14), RANK(15), RANK(16),
|
||||
};
|
||||
|
||||
#define SEQ_LEN(n) LL_ADC_REG_SEQ_SCAN_ENABLE_##n##RANKS
|
||||
static const uint32_t table_seq_len[] = {
|
||||
LL_ADC_REG_SEQ_SCAN_DISABLE,
|
||||
SEQ_LEN(2),
|
||||
SEQ_LEN(3),
|
||||
SEQ_LEN(4),
|
||||
SEQ_LEN(5),
|
||||
SEQ_LEN(6),
|
||||
SEQ_LEN(7),
|
||||
SEQ_LEN(8),
|
||||
SEQ_LEN(9),
|
||||
SEQ_LEN(10),
|
||||
SEQ_LEN(11),
|
||||
SEQ_LEN(12),
|
||||
SEQ_LEN(13),
|
||||
SEQ_LEN(14),
|
||||
SEQ_LEN(15),
|
||||
SEQ_LEN(16),
|
||||
};
|
||||
|
||||
struct daq_stm32_data
|
||||
{
|
||||
struct daq_common_data common;
|
||||
struct k_timer trigger_timer;
|
||||
uint8_t num_adc1_ch;
|
||||
uint8_t num_adc2_ch;
|
||||
};
|
||||
|
||||
DAQ_COMMON_DEFINE(DAQ_NODE);
|
||||
|
||||
static const struct daq_common_config daq_stm32_config = {
|
||||
DAQ_COMMON_CONFIG_INIT(DAQ_NODE),
|
||||
DAQ_STM32_CAL_CONFIG_INIT(),
|
||||
};
|
||||
|
||||
static struct daq_stm32_data daq_stm32_data = {
|
||||
.common = { DAQ_COMMON_DATA_INIT(), .vref_mv = VREF_MV },
|
||||
};
|
||||
|
||||
static void vref_setup(void)
|
||||
{
|
||||
// output 2.048V at VREF+ pin (also used internally for ADC and DAC)
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
|
||||
LL_VREFBUF_SetVoltageScaling(LL_VREFBUF_VOLTAGE_SCALE0);
|
||||
LL_VREFBUF_DisableHIZ();
|
||||
LL_VREFBUF_Enable();
|
||||
while (LL_VREFBUF_IsVREFReady() == 0) {
|
||||
}
|
||||
}
|
||||
|
||||
static void dac_setup(void)
|
||||
{
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_DAC1);
|
||||
/* DAC1 at PA4 for bi-directional DC/DC current measurement at 0.5 * VCC */
|
||||
LL_DAC_SetOutputBuffer(DAC1, LL_DAC_CHANNEL_1, LL_DAC_OUTPUT_BUFFER_ENABLE);
|
||||
LL_DAC_Enable(DAC1, LL_DAC_CHANNEL_1);
|
||||
LL_DAC_ConvertData12RightAligned(DAC1, LL_DAC_CHANNEL_1, 4096 / 2);
|
||||
/* DAC1 at PA5 for uni-directional PWM and load current measurement at 0.1 * VCC */
|
||||
LL_DAC_SetOutputBuffer(DAC1, LL_DAC_CHANNEL_2, LL_DAC_OUTPUT_BUFFER_ENABLE);
|
||||
LL_DAC_Enable(DAC1, LL_DAC_CHANNEL_2);
|
||||
LL_DAC_ConvertData12RightAligned(DAC1, LL_DAC_CHANNEL_2, 4096 / 10);
|
||||
}
|
||||
|
||||
static void adc_setup(const struct device *dev, ADC_TypeDef *adc)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
int num_ch = 0;
|
||||
|
||||
// ADC clock can be generated from SYSCLK or PLL (async mode) or derived from AHB clock
|
||||
// (sync mode). For synchronization with a timer, sync mode is preferred.
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC12);
|
||||
|
||||
if (LL_ADC_IsEnabled(adc)) {
|
||||
LL_ADC_Disable(adc);
|
||||
}
|
||||
|
||||
// Use DIV1 only, as DIV2 and DIV4 lead to errors in ADC readings (not sure why...)
|
||||
LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(adc), LL_ADC_CLOCK_SYNC_PCLK_DIV1);
|
||||
|
||||
// Prepare for ADC calibration
|
||||
LL_ADC_DisableDeepPowerDown(adc);
|
||||
LL_ADC_EnableInternalRegulator(adc);
|
||||
// Datasheet: wait 20us for regulator to stabilize (taking 100us to be safe)
|
||||
// LL_ADC_DELAY_INTERNAL_REGUL_STAB_US is erroneously set to 10 in stm32g4xx_ll_adc.h
|
||||
k_busy_wait(100);
|
||||
|
||||
LL_ADC_StartCalibration(adc, LL_ADC_SINGLE_ENDED);
|
||||
while (LL_ADC_IsCalibrationOnGoing(adc)) {
|
||||
}
|
||||
|
||||
if (LL_ADC_IsActiveFlag_ADRDY(adc)) {
|
||||
LL_ADC_ClearFlag_ADRDY(adc);
|
||||
}
|
||||
|
||||
// Enable internal reference voltage and temperature
|
||||
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(adc),
|
||||
LL_ADC_PATH_INTERNAL_VREFINT | LL_ADC_PATH_INTERNAL_TEMPSENSOR);
|
||||
|
||||
// more complicated sequencer allows to define the sequence independent of the channel number
|
||||
// (using ranks)
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
if (adc_base[i] == (uint32_t)adc) {
|
||||
uint32_t channel = table_channel[config->adc_channel[i]];
|
||||
|
||||
LL_ADC_REG_SetSequencerRanks(adc, table_rank[num_ch], channel);
|
||||
LL_ADC_SetChannelSamplingTime(adc, channel, LL_ADC_SAMPLINGTIME_247CYCLES_5);
|
||||
num_ch++;
|
||||
}
|
||||
}
|
||||
LL_ADC_REG_SetSequencerLength(adc, table_seq_len[num_ch - 1]);
|
||||
|
||||
LL_ADC_SetDataAlignment(adc, LL_ADC_DATA_ALIGN_LEFT);
|
||||
LL_ADC_SetResolution(adc, LL_ADC_RESOLUTION_12B);
|
||||
LL_ADC_REG_SetOverrun(adc, LL_ADC_REG_OVR_DATA_OVERWRITTEN);
|
||||
// Enable DMA transfer on ADC and circular mode
|
||||
LL_ADC_REG_SetDMATransfer(adc, LL_ADC_REG_DMA_TRANSFER_UNLIMITED);
|
||||
|
||||
LL_ADC_Enable(adc);
|
||||
}
|
||||
|
||||
static void process_adc_channels(const struct device *dev, ADC_TypeDef *adc)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
const struct daq_stm32_data *data = dev->data;
|
||||
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
if (adc_base[i] == (uint32_t)adc) {
|
||||
daq_common_process(dev, i);
|
||||
}
|
||||
}
|
||||
|
||||
// ADC2 is sampled after ADC1 (if used)
|
||||
if (adc == ADC2 || data->num_adc2_ch == 0) {
|
||||
daq_common_sample_done(dev);
|
||||
}
|
||||
}
|
||||
|
||||
static void dma1_isr(const void *arg)
|
||||
{
|
||||
if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) { // transfer completed on DMA channel 1
|
||||
process_adc_channels(arg, ADC1);
|
||||
}
|
||||
DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt flags
|
||||
}
|
||||
|
||||
static void dma2_isr(const void *arg)
|
||||
{
|
||||
if ((DMA2->ISR & DMA_ISR_TCIF1) != 0) { // transfer completed on DMA channel 1
|
||||
process_adc_channels(arg, ADC2);
|
||||
}
|
||||
DMA2->IFCR |= 0x0FFFFFFF; // clear all interrupt flags
|
||||
}
|
||||
|
||||
// DMA1 is mapped to ADC1 and DMA2 is mapped to ADC2
|
||||
static void dma_setup(DMA_TypeDef *dma, ADC_TypeDef *adc, volatile uint16_t *buf, uint32_t length)
|
||||
{
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMAMUX1);
|
||||
LL_DMA_SetPeriphRequest(dma, LL_DMA_CHANNEL_1,
|
||||
(dma == DMA1) ? LL_DMAMUX_REQ_ADC1 : LL_DMAMUX_REQ_ADC2);
|
||||
|
||||
LL_AHB1_GRP1_EnableClock((dma == DMA1) ? LL_AHB1_GRP1_PERIPH_DMA1 : LL_AHB1_GRP1_PERIPH_DMA2);
|
||||
|
||||
LL_DMA_ConfigAddresses(dma, LL_DMA_CHANNEL_1,
|
||||
LL_ADC_DMA_GetRegAddr(adc, LL_ADC_DMA_REG_REGULAR_DATA), (uint32_t)buf,
|
||||
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
|
||||
|
||||
// Configure the number of DMA transfers (data length in multiples of size per transfer)
|
||||
LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, length);
|
||||
|
||||
LL_DMA_SetMemoryIncMode(dma, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
|
||||
LL_DMA_SetMemorySize(dma, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD);
|
||||
LL_DMA_SetPeriphSize(dma, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_HALFWORD);
|
||||
LL_DMA_EnableIT_TE(dma, LL_DMA_CHANNEL_1); // transfer error interrupt
|
||||
LL_DMA_EnableIT_TC(dma, LL_DMA_CHANNEL_1); // transfer complete interrupt
|
||||
LL_DMA_SetMode(dma, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
|
||||
|
||||
LL_DMA_EnableChannel(dma, LL_DMA_CHANNEL_1);
|
||||
|
||||
if (dma == DMA1) {
|
||||
IRQ_CONNECT(DMA1_Channel1_IRQn, DMA_IRQ_PRIORITY, dma1_isr, DEVICE_DT_INST_GET(0), 0);
|
||||
irq_enable(DMA1_Channel1_IRQn);
|
||||
}
|
||||
else {
|
||||
IRQ_CONNECT(DMA2_Channel1_IRQn, DMA_IRQ_PRIORITY, dma2_isr, DEVICE_DT_INST_GET(0), 0);
|
||||
irq_enable(DMA2_Channel1_IRQn);
|
||||
}
|
||||
}
|
||||
|
||||
static void trigger_conversion(struct k_timer *timer)
|
||||
{
|
||||
const struct daq_stm32_data *data = k_timer_user_data_get(timer);
|
||||
|
||||
if (data->num_adc1_ch > 0) {
|
||||
LL_ADC_REG_StartConversion(ADC1);
|
||||
}
|
||||
if (data->num_adc2_ch > 0) {
|
||||
LL_ADC_REG_StartConversion(ADC2);
|
||||
}
|
||||
}
|
||||
|
||||
static int daq_stm32_init(const struct device *dev)
|
||||
{
|
||||
const struct daq_common_config *config = dev->config;
|
||||
struct daq_stm32_data *data = dev->data;
|
||||
int err;
|
||||
|
||||
// the DMA stores the channels of ADC1 first, followed by the channels of ADC2
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
if (config->adc_channel[i] >= ARRAY_SIZE(table_channel)) {
|
||||
LOG_ERR("Invalid ADC channel %d", config->adc_channel[i]);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (adc_base[i] == (uint32_t)ADC1) {
|
||||
data->common.channels[i].buf_idx = data->num_adc1_ch++;
|
||||
}
|
||||
else if (adc_base[i] == (uint32_t)ADC2) {
|
||||
data->common.channels[i].buf_idx = data->num_adc2_ch++;
|
||||
}
|
||||
else {
|
||||
LOG_ERR("Only ADC1 and ADC2 are supported");
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < config->num_channels; i++) {
|
||||
if (adc_base[i] == (uint32_t)ADC2) {
|
||||
data->common.channels[i].buf_idx += data->num_adc1_ch;
|
||||
}
|
||||
}
|
||||
|
||||
err = daq_common_init(dev);
|
||||
if (err != 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
vref_setup();
|
||||
dac_setup();
|
||||
|
||||
// configure both ADCs before starting conversions, as they share common settings
|
||||
if (data->num_adc1_ch > 0) {
|
||||
adc_setup(dev, ADC1);
|
||||
}
|
||||
if (data->num_adc2_ch > 0) {
|
||||
adc_setup(dev, ADC2);
|
||||
}
|
||||
|
||||
if (data->num_adc1_ch > 0) {
|
||||
dma_setup(DMA1, ADC1, &data->common.buf[0], data->num_adc1_ch);
|
||||
LL_ADC_REG_StartConversion(ADC1);
|
||||
}
|
||||
if (data->num_adc2_ch > 0) {
|
||||
dma_setup(DMA2, ADC2, &data->common.buf[data->num_adc1_ch], data->num_adc2_ch);
|
||||
LL_ADC_REG_StartConversion(ADC2);
|
||||
}
|
||||
|
||||
k_timer_init(&data->trigger_timer, trigger_conversion, NULL);
|
||||
k_timer_user_data_set(&data->trigger_timer, data);
|
||||
k_timer_start(&data->trigger_timer, K_MSEC(DAQ_SAMPLE_PERIOD_MS), K_MSEC(DAQ_SAMPLE_PERIOD_MS));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, daq_stm32_init, NULL, &daq_stm32_data, &daq_stm32_config, POST_KERNEL,
|
||||
CONFIG_DAQ_INIT_PRIORITY, NULL);
|
||||
@@ -0,0 +1,11 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
|
||||
zephyr_library_include_directories(../stm32_tim)
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_STM32_ADVTIM half_bridge_stm32_advtim.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_STM32_GPTIM half_bridge_stm32_gptim.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_STM32_HRTIM half_bridge_stm32_hrtim.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_HALF_BRIDGE_EMUL half_bridge_emul.c)
|
||||
@@ -0,0 +1,60 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
menuconfig HALF_BRIDGE
|
||||
bool "Half-bridge drivers"
|
||||
default y if DT_HAS_LIBRESOLAR_STM32_ADVTIM_HALF_BRIDGE_ENABLED
|
||||
default y if DT_HAS_LIBRESOLAR_STM32_GPTIM_HALF_BRIDGE_ENABLED
|
||||
default y if DT_HAS_LIBRESOLAR_STM32_HRTIM_HALF_BRIDGE_ENABLED
|
||||
default y if DT_HAS_LIBRESOLAR_HALF_BRIDGE_EMUL_ENABLED
|
||||
help
|
||||
Enable drivers for half bridges consisting of two complementary PWM outputs with dead time,
|
||||
e.g. used in DC/DC converters.
|
||||
|
||||
if HALF_BRIDGE
|
||||
|
||||
config HALF_BRIDGE_INIT_PRIORITY
|
||||
int "Half-bridge init priority"
|
||||
default KERNEL_INIT_PRIORITY_DEVICE
|
||||
help
|
||||
Half-bridge driver device initialization priority.
|
||||
|
||||
module = HALF_BRIDGE
|
||||
module-str = half_bridge
|
||||
source "subsys/logging/Kconfig.template.log_config"
|
||||
|
||||
config HALF_BRIDGE_STM32_ADVTIM
|
||||
bool "STM32 advanced-control timer half-bridge driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_STM32_ADVTIM_HALF_BRIDGE_ENABLED
|
||||
select LIBRESOLAR_STM32_TIM
|
||||
help
|
||||
Enable half-bridge driver for STM32 advanced-control timers (e.g. TIM1) using
|
||||
complementary outputs with hardware dead time generation.
|
||||
|
||||
config HALF_BRIDGE_STM32_GPTIM
|
||||
bool "STM32 general-purpose timer half-bridge driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_STM32_GPTIM_HALF_BRIDGE_ENABLED
|
||||
select LIBRESOLAR_STM32_TIM
|
||||
help
|
||||
Enable half-bridge driver for STM32 general-purpose timers (e.g. TIM3) using two
|
||||
independent channels with dead time generated by software.
|
||||
|
||||
config HALF_BRIDGE_STM32_HRTIM
|
||||
bool "STM32 HRTIM half-bridge driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_STM32_HRTIM_HALF_BRIDGE_ENABLED
|
||||
select PINCTRL
|
||||
select RESET
|
||||
help
|
||||
Enable half-bridge driver for the STM32 high-resolution timer (HRTIM).
|
||||
|
||||
config HALF_BRIDGE_EMUL
|
||||
bool "Emulated half-bridge driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_HALF_BRIDGE_EMUL_ENABLED
|
||||
help
|
||||
Enable emulated half-bridge driver for tests on native_sim.
|
||||
|
||||
endif # HALF_BRIDGE
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Emulated half-bridge driver for tests on native_sim
|
||||
*
|
||||
* Behaves like an edge-aligned STM32 timer, i.e. the period is clock-frequency / frequency.
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT libresolar_half_bridge_emul
|
||||
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
struct half_bridge_emul_config
|
||||
{
|
||||
uint32_t clock_frequency;
|
||||
uint32_t frequency;
|
||||
};
|
||||
|
||||
struct half_bridge_emul_data
|
||||
{
|
||||
uint32_t period;
|
||||
uint32_t compare;
|
||||
uint32_t compare_min;
|
||||
uint32_t compare_max;
|
||||
bool enabled;
|
||||
bool low_side_forced;
|
||||
};
|
||||
|
||||
static int half_bridge_emul_start(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
data->enabled = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int half_bridge_emul_stop(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
data->enabled = false;
|
||||
data->low_side_forced = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool half_bridge_emul_is_enabled(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
return data->enabled;
|
||||
}
|
||||
|
||||
static int half_bridge_emul_set_compare(const struct device *dev, uint32_t compare)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
data->compare = CLAMP(compare, data->compare_min, data->compare_max);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t half_bridge_emul_get_compare(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
return data->compare;
|
||||
}
|
||||
|
||||
static uint32_t half_bridge_emul_get_period(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
return data->period;
|
||||
}
|
||||
|
||||
static int half_bridge_emul_set_compare_limits(const struct device *dev, uint32_t min, uint32_t max)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (min > max || max > data->period) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
data->compare_min = min;
|
||||
data->compare_max = max;
|
||||
|
||||
return half_bridge_emul_set_compare(dev, data->compare);
|
||||
}
|
||||
|
||||
static int half_bridge_emul_force_low_side(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
data->compare = 0;
|
||||
data->enabled = true;
|
||||
data->low_side_forced = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DEVICE_API(half_bridge, half_bridge_emul_api) = {
|
||||
.start = half_bridge_emul_start,
|
||||
.stop = half_bridge_emul_stop,
|
||||
.is_enabled = half_bridge_emul_is_enabled,
|
||||
.set_compare = half_bridge_emul_set_compare,
|
||||
.get_compare = half_bridge_emul_get_compare,
|
||||
.get_period = half_bridge_emul_get_period,
|
||||
.set_compare_limits = half_bridge_emul_set_compare_limits,
|
||||
.force_low_side = half_bridge_emul_force_low_side,
|
||||
};
|
||||
|
||||
static int half_bridge_emul_init(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_emul_config *config = dev->config;
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
data->period = config->clock_frequency / config->frequency;
|
||||
data->compare_max = data->period;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define HALF_BRIDGE_EMUL_INIT(n) \
|
||||
static const struct half_bridge_emul_config half_bridge_emul_config_##n = { \
|
||||
.clock_frequency = DT_INST_PROP(n, clock_frequency), \
|
||||
.frequency = DT_INST_PROP(n, frequency), \
|
||||
}; \
|
||||
\
|
||||
static struct half_bridge_emul_data half_bridge_emul_data_##n; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(n, half_bridge_emul_init, NULL, &half_bridge_emul_data_##n, \
|
||||
&half_bridge_emul_config_##n, POST_KERNEL, \
|
||||
CONFIG_HALF_BRIDGE_INIT_PRIORITY, &half_bridge_emul_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(HALF_BRIDGE_EMUL_INIT)
|
||||
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Half-bridge driver for STM32 advanced-control timers (e.g. TIM1)
|
||||
*
|
||||
* The high-side switch is driven by output CHx and the low-side switch by the complementary
|
||||
* output CHxN. The timer runs in edge-aligned mode and the dead time is generated in hardware.
|
||||
*
|
||||
* Starting and stopping is done with the main output enable (MOE). While stopped, both outputs
|
||||
* are actively driven low.
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT libresolar_stm32_advtim_half_bridge
|
||||
|
||||
#include "stm32_tim.h"
|
||||
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
LOG_MODULE_REGISTER(half_bridge_stm32_advtim, CONFIG_HALF_BRIDGE_LOG_LEVEL);
|
||||
|
||||
#ifndef LL_TIM_CHANNEL_CH1N
|
||||
#error "Advanced-control timers with complementary outputs not supported by this MCU"
|
||||
#endif
|
||||
|
||||
struct half_bridge_stm32_advtim_config
|
||||
{
|
||||
struct stm32_tim_config tim;
|
||||
uint32_t frequency;
|
||||
uint32_t deadtime_ns;
|
||||
/** Timer channel (1-4) for the high-side switch, CHxN is used for the low-side switch */
|
||||
uint8_t channel;
|
||||
bool adc_trigger;
|
||||
};
|
||||
|
||||
struct half_bridge_stm32_advtim_data
|
||||
{
|
||||
uint32_t compare_min;
|
||||
uint32_t compare_max;
|
||||
bool low_side_forced;
|
||||
};
|
||||
|
||||
static const uint32_t ch2ll_n[] = {
|
||||
LL_TIM_CHANNEL_CH1N,
|
||||
LL_TIM_CHANNEL_CH2N,
|
||||
LL_TIM_CHANNEL_CH3N,
|
||||
#ifdef LL_TIM_CHANNEL_CH4N
|
||||
LL_TIM_CHANNEL_CH4N,
|
||||
#endif
|
||||
};
|
||||
|
||||
static int half_bridge_stm32_advtim_start(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (stm32_tim_get_compare(tim, config->channel) == 0U) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* main output enable */
|
||||
LL_TIM_EnableAllOutputs(tim);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_advtim_stop(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
|
||||
LL_TIM_DisableAllOutputs(config->tim.timer);
|
||||
data->low_side_forced = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool half_bridge_stm32_advtim_is_enabled(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
|
||||
return LL_TIM_IsEnabledAllOutputs(config->tim.timer);
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_advtim_set_compare(const struct device *dev, uint32_t compare)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
/* protection against wrong settings which could destroy the hardware */
|
||||
compare = CLAMP(compare, data->compare_min, data->compare_max);
|
||||
|
||||
stm32_tim_set_compare(tim, config->channel, compare);
|
||||
|
||||
#ifdef LL_TIM_CHANNEL_CH6
|
||||
if (config->adc_trigger) {
|
||||
/* trigger ADC for current measurement in the middle of the on-time */
|
||||
LL_TIM_OC_SetCompareCH6(tim, compare / 2);
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t half_bridge_stm32_advtim_get_compare(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
|
||||
return stm32_tim_get_compare(config->tim.timer, config->channel);
|
||||
}
|
||||
|
||||
static uint32_t half_bridge_stm32_advtim_get_period(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
|
||||
return LL_TIM_GetAutoReload(config->tim.timer);
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_advtim_set_compare_limits(const struct device *dev, uint32_t min,
|
||||
uint32_t max)
|
||||
{
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (min > max || max > half_bridge_stm32_advtim_get_period(dev)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
data->compare_min = min;
|
||||
data->compare_max = max;
|
||||
|
||||
return half_bridge_stm32_advtim_set_compare(dev, half_bridge_stm32_advtim_get_compare(dev));
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_advtim_force_low_side(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
data->low_side_forced = true;
|
||||
|
||||
/*
|
||||
* A compare value of 0 keeps OCxREF low in PWM mode 1, so CHx (high side) stays off and the
|
||||
* complementary output CHxN (low side) stays on. The output mode itself can't be changed, as
|
||||
* it is locked after initialization.
|
||||
*/
|
||||
stm32_tim_set_compare(tim, config->channel, 0);
|
||||
|
||||
/* load preload registers immediately */
|
||||
LL_TIM_GenerateEvent_UPDATE(tim);
|
||||
|
||||
LL_TIM_EnableAllOutputs(tim);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DEVICE_API(half_bridge, half_bridge_stm32_advtim_api) = {
|
||||
.start = half_bridge_stm32_advtim_start,
|
||||
.stop = half_bridge_stm32_advtim_stop,
|
||||
.is_enabled = half_bridge_stm32_advtim_is_enabled,
|
||||
.set_compare = half_bridge_stm32_advtim_set_compare,
|
||||
.get_compare = half_bridge_stm32_advtim_get_compare,
|
||||
.get_period = half_bridge_stm32_advtim_get_period,
|
||||
.set_compare_limits = half_bridge_stm32_advtim_set_compare_limits,
|
||||
.force_low_side = half_bridge_stm32_advtim_force_low_side,
|
||||
};
|
||||
|
||||
static int half_bridge_stm32_advtim_init_adc_trigger(TIM_TypeDef *tim)
|
||||
{
|
||||
#ifdef LL_TIM_CHANNEL_CH6
|
||||
if (!IS_TIM_TRGO2_INSTANCE(tim)) {
|
||||
LOG_ERR("ADC trigger not supported by this timer");
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
/* use OC6 for ADC triggering, but don't configure any pin for PWM mode */
|
||||
LL_TIM_OC_SetMode(tim, LL_TIM_CHANNEL_CH6, LL_TIM_OCMODE_PWM1);
|
||||
LL_TIM_OC_EnablePreload(tim, LL_TIM_CHANNEL_CH6);
|
||||
LL_TIM_CC_EnableChannel(tim, LL_TIM_CHANNEL_CH6);
|
||||
|
||||
/* trigger ADC via TRGO2 on OC6REF falling edge (OC4REF is not used) */
|
||||
LL_TIM_SetTriggerOutput2(tim, LL_TIM_TRGO2_OC4_RISING_OC6_FALLING);
|
||||
|
||||
return 0;
|
||||
#else
|
||||
LOG_ERR("ADC trigger not supported by this timer");
|
||||
return -ENOTSUP;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_advtim_init(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
uint32_t ll_channel = stm32_tim_ll_channel(config->channel);
|
||||
uint32_t tim_clk;
|
||||
int err;
|
||||
|
||||
if (!IS_TIM_BREAK_INSTANCE(tim) || config->channel > ARRAY_SIZE(ch2ll_n)) {
|
||||
LOG_ERR("Complementary output not supported for channel %u", config->channel);
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
err = stm32_tim_init(&config->tim, &tim_clk);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* edge-aligned mode to increase resolution (no division by 2 necessary) */
|
||||
LL_TIM_SetCounterMode(tim, LL_TIM_COUNTERMODE_UP);
|
||||
LL_TIM_SetAutoReload(tim, tim_clk / config->frequency);
|
||||
|
||||
/*
|
||||
* Drive both outputs to their idle level (low, as OISx and OISxN are 0 after reset) while the
|
||||
* main output is disabled, instead of leaving them floating.
|
||||
*/
|
||||
LL_TIM_SetOffStates(tim, LL_TIM_OSSI_ENABLE, LL_TIM_OSSR_DISABLE);
|
||||
|
||||
/* PWM mode 1 with preload and active high polarity for both outputs */
|
||||
LL_TIM_OC_SetMode(tim, ll_channel, LL_TIM_OCMODE_PWM1);
|
||||
LL_TIM_OC_EnablePreload(tim, ll_channel);
|
||||
LL_TIM_CC_EnableChannel(tim, ll_channel | ch2ll_n[config->channel - 1]);
|
||||
|
||||
if (config->adc_trigger) {
|
||||
err = half_bridge_stm32_advtim_init_adc_trigger(tim);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
LL_TIM_OC_SetDeadTime(
|
||||
tim, __LL_TIM_CALC_DEADTIME(tim_clk, LL_TIM_CLOCKDIVISION_DIV1, config->deadtime_ns));
|
||||
|
||||
/* lock break and dead-time register to prevent accidental changes */
|
||||
LL_TIM_CC_SetLockLevel(tim, LL_TIM_LOCKLEVEL_3);
|
||||
|
||||
data->compare_min = 0;
|
||||
data->compare_max = LL_TIM_GetAutoReload(tim);
|
||||
|
||||
/* force update generation to load the preload registers and start the counter */
|
||||
LL_TIM_GenerateEvent_UPDATE(tim);
|
||||
LL_TIM_EnableCounter(tim);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define HALF_BRIDGE_STM32_ADVTIM_INIT(n) \
|
||||
STM32_TIM_DEFINE(n) \
|
||||
\
|
||||
static const struct half_bridge_stm32_advtim_config half_bridge_stm32_advtim_config_##n = { \
|
||||
.tim = STM32_TIM_CONFIG(n), \
|
||||
.frequency = DT_INST_PROP(n, frequency), \
|
||||
.deadtime_ns = DT_INST_PROP(n, deadtime_ns), \
|
||||
.channel = DT_INST_PROP(n, channel), \
|
||||
.adc_trigger = DT_INST_PROP(n, adc_trigger), \
|
||||
}; \
|
||||
\
|
||||
static struct half_bridge_stm32_advtim_data half_bridge_stm32_advtim_data_##n; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(n, half_bridge_stm32_advtim_init, NULL, \
|
||||
&half_bridge_stm32_advtim_data_##n, \
|
||||
&half_bridge_stm32_advtim_config_##n, POST_KERNEL, \
|
||||
CONFIG_HALF_BRIDGE_INIT_PRIORITY, &half_bridge_stm32_advtim_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(HALF_BRIDGE_STM32_ADVTIM_INIT)
|
||||
@@ -0,0 +1,281 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Half-bridge driver for STM32 general-purpose timers (e.g. TIM3)
|
||||
*
|
||||
* The high-side and low-side switches are driven by two independent channels, as general-purpose
|
||||
* timers have no hardware dead time generation. The timer runs in center-aligned mode and the dead
|
||||
* time is generated by shifting the compare value of the low-side channel, which has inverted
|
||||
* polarity.
|
||||
*
|
||||
* The output channels are always enabled after initialization. Starting and stopping is done by
|
||||
* switching between PWM mode and forced output levels, so that both outputs are actively driven
|
||||
* to their off state while stopped.
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT libresolar_stm32_gptim_half_bridge
|
||||
|
||||
#include "stm32_tim.h"
|
||||
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
LOG_MODULE_REGISTER(half_bridge_stm32_gptim, CONFIG_HALF_BRIDGE_LOG_LEVEL);
|
||||
|
||||
struct half_bridge_stm32_gptim_config
|
||||
{
|
||||
struct stm32_tim_config tim;
|
||||
uint32_t frequency;
|
||||
uint32_t deadtime_ns;
|
||||
/** Timer channel (1-4) for the high-side switch */
|
||||
uint8_t high_side_channel;
|
||||
/** Timer channel (1-4) for the low-side switch */
|
||||
uint8_t low_side_channel;
|
||||
};
|
||||
|
||||
struct half_bridge_stm32_gptim_data
|
||||
{
|
||||
uint32_t deadtime_clocks;
|
||||
uint32_t compare_min;
|
||||
uint32_t compare_max;
|
||||
bool enabled;
|
||||
bool low_side_forced;
|
||||
};
|
||||
|
||||
static inline uint32_t ll_channels(const struct half_bridge_stm32_gptim_config *config)
|
||||
{
|
||||
return stm32_tim_ll_channel(config->high_side_channel)
|
||||
| stm32_tim_ll_channel(config->low_side_channel);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the output compare modes of both channels
|
||||
*
|
||||
* Note that the low-side output has inverted polarity, so OCxREF must be high to switch it off.
|
||||
*/
|
||||
static void set_output_modes(const struct half_bridge_stm32_gptim_config *config, uint32_t hs_mode,
|
||||
uint32_t ls_mode)
|
||||
{
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
LL_TIM_OC_SetMode(tim, stm32_tim_ll_channel(config->high_side_channel), hs_mode);
|
||||
LL_TIM_OC_SetMode(tim, stm32_tim_ll_channel(config->low_side_channel), ls_mode);
|
||||
}
|
||||
|
||||
static inline void set_outputs_off(const struct half_bridge_stm32_gptim_config *config)
|
||||
{
|
||||
set_output_modes(config, LL_TIM_OCMODE_FORCED_INACTIVE, LL_TIM_OCMODE_FORCED_ACTIVE);
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_gptim_start(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (stm32_tim_get_compare(tim, config->high_side_channel) == 0U) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
set_output_modes(config, LL_TIM_OCMODE_PWM1, LL_TIM_OCMODE_PWM1);
|
||||
data->enabled = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_gptim_stop(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
|
||||
set_outputs_off(config);
|
||||
data->enabled = false;
|
||||
data->low_side_forced = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool half_bridge_stm32_gptim_is_enabled(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
|
||||
return data->enabled;
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_gptim_set_compare(const struct device *dev, uint32_t compare)
|
||||
{
|
||||
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
/* protection against wrong settings which could destroy the hardware */
|
||||
compare = CLAMP(compare, data->compare_min, data->compare_max);
|
||||
|
||||
stm32_tim_set_compare(tim, config->high_side_channel, compare);
|
||||
stm32_tim_set_compare(tim, config->low_side_channel, compare + data->deadtime_clocks);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t half_bridge_stm32_gptim_get_compare(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
||||
|
||||
return stm32_tim_get_compare(config->tim.timer, config->high_side_channel);
|
||||
}
|
||||
|
||||
static uint32_t half_bridge_stm32_gptim_get_period(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
||||
|
||||
return LL_TIM_GetAutoReload(config->tim.timer);
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_gptim_set_compare_limits(const struct device *dev, uint32_t min,
|
||||
uint32_t max)
|
||||
{
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (min > max || max > half_bridge_stm32_gptim_get_period(dev)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
data->compare_min = min;
|
||||
data->compare_max = max;
|
||||
|
||||
return half_bridge_stm32_gptim_set_compare(dev, half_bridge_stm32_gptim_get_compare(dev));
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_gptim_force_low_side(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
|
||||
data->low_side_forced = true;
|
||||
data->enabled = true;
|
||||
|
||||
/*
|
||||
* OCxREF forced low for both channels: The high-side output (active high) is off and the
|
||||
* low-side output (active low) is on.
|
||||
*/
|
||||
set_output_modes(config, LL_TIM_OCMODE_FORCED_INACTIVE, LL_TIM_OCMODE_FORCED_INACTIVE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DEVICE_API(half_bridge, half_bridge_stm32_gptim_api) = {
|
||||
.start = half_bridge_stm32_gptim_start,
|
||||
.stop = half_bridge_stm32_gptim_stop,
|
||||
.is_enabled = half_bridge_stm32_gptim_is_enabled,
|
||||
.set_compare = half_bridge_stm32_gptim_set_compare,
|
||||
.get_compare = half_bridge_stm32_gptim_get_compare,
|
||||
.get_period = half_bridge_stm32_gptim_get_period,
|
||||
.set_compare_limits = half_bridge_stm32_gptim_set_compare_limits,
|
||||
.force_low_side = half_bridge_stm32_gptim_force_low_side,
|
||||
};
|
||||
|
||||
static int half_bridge_stm32_gptim_init(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
uint32_t ll_channel_hs = stm32_tim_ll_channel(config->high_side_channel);
|
||||
uint32_t ll_channel_ls = stm32_tim_ll_channel(config->low_side_channel);
|
||||
uint32_t tim_clk;
|
||||
int err;
|
||||
|
||||
if (!IS_TIM_COUNTER_MODE_SELECT_INSTANCE(tim)) {
|
||||
LOG_ERR("Center-aligned mode not supported by this timer");
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
#ifdef IS_TIM_BREAK_INSTANCE
|
||||
if (IS_TIM_BREAK_INSTANCE(tim)) {
|
||||
/* outputs would additionally require main output enable (MOE) */
|
||||
LOG_ERR("Timer has dead time generator, use advtim half-bridge instead");
|
||||
return -ENOTSUP;
|
||||
}
|
||||
#endif
|
||||
|
||||
err = stm32_tim_init(&config->tim, &tim_clk);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Center-aligned mode counts up and down in each period, so we need half the clocks as
|
||||
* resolution for the given frequency.
|
||||
*/
|
||||
LL_TIM_SetCounterMode(tim, LL_TIM_COUNTERMODE_CENTER_DOWN);
|
||||
LL_TIM_SetAutoReload(tim, tim_clk / config->frequency / 2);
|
||||
|
||||
/* compare preload, high-side active high, low-side active low */
|
||||
LL_TIM_OC_EnablePreload(tim, ll_channel_hs);
|
||||
LL_TIM_OC_SetPolarity(tim, ll_channel_hs, LL_TIM_OCPOLARITY_HIGH);
|
||||
|
||||
LL_TIM_OC_EnablePreload(tim, ll_channel_ls);
|
||||
LL_TIM_OC_SetPolarity(tim, ll_channel_ls, LL_TIM_OCPOLARITY_LOW);
|
||||
|
||||
/* PWM mode is only selected when starting, outputs are actively driven off until then */
|
||||
set_outputs_off(config);
|
||||
LL_TIM_CC_EnableChannel(tim, ll_channels(config));
|
||||
|
||||
/*
|
||||
* (clocks per ms * deadtime in ns) / 1000 == (clocks per ms * deadtime in ms)
|
||||
* although the C operator precedence does the "right thing" to allow deadtime_ns < 1000 to
|
||||
* be handled nicely, parentheses make this more explicit
|
||||
*/
|
||||
data->deadtime_clocks = ((tim_clk / 1000000) * config->deadtime_ns) / 1000;
|
||||
|
||||
data->compare_min = 0;
|
||||
data->compare_max = LL_TIM_GetAutoReload(tim);
|
||||
|
||||
/* force update generation to load the preload registers and start the counter */
|
||||
LL_TIM_GenerateEvent_UPDATE(tim);
|
||||
LL_TIM_EnableCounter(tim);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define HALF_BRIDGE_STM32_GPTIM_INIT(n) \
|
||||
BUILD_ASSERT(DT_INST_PROP(n, high_side_channel) != DT_INST_PROP(n, low_side_channel), \
|
||||
"High-side and low-side channel must be different"); \
|
||||
\
|
||||
STM32_TIM_DEFINE(n) \
|
||||
\
|
||||
static const struct half_bridge_stm32_gptim_config half_bridge_stm32_gptim_config_##n = { \
|
||||
.tim = STM32_TIM_CONFIG(n), \
|
||||
.frequency = DT_INST_PROP(n, frequency), \
|
||||
.deadtime_ns = DT_INST_PROP(n, deadtime_ns), \
|
||||
.high_side_channel = DT_INST_PROP(n, high_side_channel), \
|
||||
.low_side_channel = DT_INST_PROP(n, low_side_channel), \
|
||||
}; \
|
||||
\
|
||||
static struct half_bridge_stm32_gptim_data half_bridge_stm32_gptim_data_##n; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(n, half_bridge_stm32_gptim_init, NULL, \
|
||||
&half_bridge_stm32_gptim_data_##n, &half_bridge_stm32_gptim_config_##n, \
|
||||
POST_KERNEL, CONFIG_HALF_BRIDGE_INIT_PRIORITY, \
|
||||
&half_bridge_stm32_gptim_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(HALF_BRIDGE_STM32_GPTIM_INIT)
|
||||
@@ -0,0 +1,304 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Half-bridge driver for the STM32 high-resolution timer (HRTIM)
|
||||
*
|
||||
* This initial implementation only supports timer A with outputs TA1 (high-side) and TA2
|
||||
* (low-side) and does not make use of the possible higher resolution yet.
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT libresolar_stm32_hrtim_half_bridge
|
||||
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/clock_control.h>
|
||||
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
|
||||
#include <zephyr/drivers/pinctrl.h>
|
||||
#include <zephyr/drivers/reset.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include <soc.h>
|
||||
|
||||
LOG_MODULE_REGISTER(half_bridge_stm32_hrtim, CONFIG_HALF_BRIDGE_LOG_LEVEL);
|
||||
|
||||
/*
|
||||
* A negative offset of the ADC trigger was found to improve current measurement accuracy and
|
||||
* compensate the ADC delay (no risk of underflow as compare value is around 214 even at 0.1%
|
||||
* duty cycle).
|
||||
*/
|
||||
#define ADC_TRIGGER_OFFSET 80
|
||||
|
||||
/* Timeout for delay-locked loop (DLL) calibration */
|
||||
#define DLL_CALIBRATION_TIMEOUT_US 10000
|
||||
|
||||
struct half_bridge_stm32_hrtim_config
|
||||
{
|
||||
HRTIM_TypeDef *hrtim;
|
||||
const struct stm32_pclken *pclken;
|
||||
size_t pclk_len;
|
||||
struct reset_dt_spec reset;
|
||||
const struct pinctrl_dev_config *pcfg;
|
||||
uint32_t frequency;
|
||||
uint32_t deadtime_ns;
|
||||
bool adc_trigger;
|
||||
};
|
||||
|
||||
struct half_bridge_stm32_hrtim_data
|
||||
{
|
||||
uint32_t compare_min;
|
||||
uint32_t compare_max;
|
||||
bool low_side_forced;
|
||||
};
|
||||
|
||||
static inline HRTIM_Timerx_TypeDef *timer_a(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_hrtim_config *config = dev->config;
|
||||
|
||||
return &config->hrtim->sTimerxRegs[0];
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_hrtim_start(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_hrtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
config->hrtim->sCommonRegs.OENR = HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_hrtim_stop(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_hrtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
|
||||
config->hrtim->sCommonRegs.ODISR = HRTIM_ODISR_TA1ODIS | HRTIM_ODISR_TA2ODIS;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
/* restore PWM generation for output 1 */
|
||||
HRTIM_Timerx_TypeDef *tim = timer_a(dev);
|
||||
tim->SETx1R = HRTIM_SET1R_PER;
|
||||
tim->RSTx1R = HRTIM_RST1R_CMP1;
|
||||
data->low_side_forced = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool half_bridge_stm32_hrtim_is_enabled(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_hrtim_config *config = dev->config;
|
||||
|
||||
return (config->hrtim->sCommonRegs.OENR & (HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN)) > 0;
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_hrtim_set_compare(const struct device *dev, uint32_t compare)
|
||||
{
|
||||
const struct half_bridge_stm32_hrtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
HRTIM_Timerx_TypeDef *tim = timer_a(dev);
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
/* protection against wrong settings which could destroy the hardware */
|
||||
compare = CLAMP(compare, data->compare_min, data->compare_max);
|
||||
|
||||
tim->CMP1xR = compare;
|
||||
|
||||
if (config->adc_trigger) {
|
||||
/* trigger ADC for current measurement in the middle of the on-time */
|
||||
tim->CMP3xR = compare / 2 - ADC_TRIGGER_OFFSET;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t half_bridge_stm32_hrtim_get_compare(const struct device *dev)
|
||||
{
|
||||
return timer_a(dev)->CMP1xR;
|
||||
}
|
||||
|
||||
static uint32_t half_bridge_stm32_hrtim_get_period(const struct device *dev)
|
||||
{
|
||||
return timer_a(dev)->PERxR;
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_hrtim_set_compare_limits(const struct device *dev, uint32_t min,
|
||||
uint32_t max)
|
||||
{
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (min > max || max > half_bridge_stm32_hrtim_get_period(dev)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
data->compare_min = min;
|
||||
data->compare_max = max;
|
||||
|
||||
return half_bridge_stm32_hrtim_set_compare(dev, half_bridge_stm32_hrtim_get_compare(dev));
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_hrtim_force_low_side(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_hrtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
HRTIM_Timerx_TypeDef *tim = timer_a(dev);
|
||||
|
||||
data->low_side_forced = true;
|
||||
|
||||
/*
|
||||
* Remove all set events and reset output 1 (high side) by software, so that it stays
|
||||
* inactive. Output 2 (low side) is generated as the complement with dead time and stays on.
|
||||
*/
|
||||
tim->SETx1R = 0;
|
||||
tim->RSTx1R = HRTIM_RST1R_SRT;
|
||||
|
||||
config->hrtim->sCommonRegs.OENR = HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DEVICE_API(half_bridge, half_bridge_stm32_hrtim_api) = {
|
||||
.start = half_bridge_stm32_hrtim_start,
|
||||
.stop = half_bridge_stm32_hrtim_stop,
|
||||
.is_enabled = half_bridge_stm32_hrtim_is_enabled,
|
||||
.set_compare = half_bridge_stm32_hrtim_set_compare,
|
||||
.get_compare = half_bridge_stm32_hrtim_get_compare,
|
||||
.get_period = half_bridge_stm32_hrtim_get_period,
|
||||
.set_compare_limits = half_bridge_stm32_hrtim_set_compare_limits,
|
||||
.force_low_side = half_bridge_stm32_hrtim_force_low_side,
|
||||
};
|
||||
|
||||
static int half_bridge_stm32_hrtim_init(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_hrtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
|
||||
HRTIM_TypeDef *hrtim = config->hrtim;
|
||||
HRTIM_Timerx_TypeDef *tim = timer_a(dev);
|
||||
uint32_t tim_clk;
|
||||
int err;
|
||||
|
||||
if (!device_is_ready(clk)) {
|
||||
LOG_ERR("Clock control device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
err = clock_control_on(clk, (clock_control_subsys_t)&config->pclken[0]);
|
||||
if (err < 0) {
|
||||
LOG_ERR("Could not initialize clock (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (config->pclk_len < 2) {
|
||||
LOG_ERR("Timer clock source is not specified");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
err = clock_control_get_rate(clk, (clock_control_subsys_t)&config->pclken[1], &tim_clk);
|
||||
if (err < 0) {
|
||||
LOG_ERR("Timer clock rate get error (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (config->reset.dev != NULL) {
|
||||
(void)reset_line_toggle_dt(&config->reset);
|
||||
}
|
||||
|
||||
err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
|
||||
if (err < 0) {
|
||||
LOG_ERR("Pinctrl setup failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* enable periodic calibration of delay-locked loop (DLL) with lowest calibration period */
|
||||
hrtim->sCommonRegs.DLLCR |= HRTIM_DLLCR_CALEN | HRTIM_DLLCR_CALRTE_0 | HRTIM_DLLCR_CALRTE_1;
|
||||
|
||||
if (!WAIT_FOR((hrtim->sCommonRegs.ISR & HRTIM_ISR_DLLRDY) != 0, DLL_CALIBRATION_TIMEOUT_US,
|
||||
k_busy_wait(1)))
|
||||
{
|
||||
LOG_ERR("DLL calibration timed out");
|
||||
return -ETIMEDOUT;
|
||||
}
|
||||
|
||||
/* prescaler 32 --> counter frequency == timer clock (same as normal timers like TIM1) */
|
||||
tim->TIMxCR |= (5U << HRTIM_TIMCR_CK_PSC_Pos);
|
||||
|
||||
/* dead time prescaler 8 / 2^3 --> same frequency as counter */
|
||||
tim->DTxR |= (3U << HRTIM_DTR_DTPRSC_Pos);
|
||||
|
||||
/* continuous mode operation */
|
||||
tim->TIMxCR |= HRTIM_TIMCR_CONT;
|
||||
|
||||
tim->PERxR = tim_clk / config->frequency + 1;
|
||||
|
||||
/* output 1 set at period and reset at compare 1, output 2 complementary with dead time */
|
||||
tim->SETx1R = HRTIM_SET1R_PER;
|
||||
tim->RSTx1R = HRTIM_RST1R_CMP1;
|
||||
tim->OUTxR = HRTIM_OUTR_DTEN;
|
||||
|
||||
/* set dead time values and lock dead time signs */
|
||||
uint32_t deadtime_clocks = ((tim_clk / 1000000) * config->deadtime_ns) / 1000;
|
||||
tim->DTxR |= HRTIM_DTR_DTFSLK | ((deadtime_clocks << HRTIM_DTR_DTF_Pos) & HRTIM_DTR_DTF)
|
||||
| HRTIM_DTR_DTRSLK | ((deadtime_clocks << HRTIM_DTR_DTR_Pos) & HRTIM_DTR_DTR);
|
||||
|
||||
if (config->adc_trigger) {
|
||||
/* ADC trigger update: timer A, ADC trigger event: timer A compare 3 */
|
||||
hrtim->sCommonRegs.CR1 |= HRTIM_CR1_ADC1USRC_0;
|
||||
hrtim->sCommonRegs.ADC1R = HRTIM_ADC1R_AD1TAC3;
|
||||
}
|
||||
|
||||
data->compare_min = 0;
|
||||
data->compare_max = tim->PERxR;
|
||||
|
||||
/* start timer A */
|
||||
hrtim->sMasterRegs.MCR = HRTIM_MCR_TACEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define TIMER(n) DT_INST_PARENT(n)
|
||||
|
||||
#define HALF_BRIDGE_STM32_HRTIM_INIT(n) \
|
||||
PINCTRL_DT_INST_DEFINE(n); \
|
||||
\
|
||||
static const struct stm32_pclken half_bridge_stm32_hrtim_pclken_##n[] = \
|
||||
STM32_DT_CLOCKS(TIMER(n)); \
|
||||
\
|
||||
static const struct half_bridge_stm32_hrtim_config half_bridge_stm32_hrtim_config_##n = { \
|
||||
.hrtim = (HRTIM_TypeDef *)DT_REG_ADDR(TIMER(n)), \
|
||||
.pclken = half_bridge_stm32_hrtim_pclken_##n, \
|
||||
.pclk_len = DT_NUM_CLOCKS(TIMER(n)), \
|
||||
.reset = RESET_DT_SPEC_GET_OR(TIMER(n), { 0 }), \
|
||||
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
|
||||
.frequency = DT_INST_PROP(n, frequency), \
|
||||
.deadtime_ns = DT_INST_PROP(n, deadtime_ns), \
|
||||
.adc_trigger = DT_INST_PROP(n, adc_trigger), \
|
||||
}; \
|
||||
\
|
||||
static struct half_bridge_stm32_hrtim_data half_bridge_stm32_hrtim_data_##n; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(n, half_bridge_stm32_hrtim_init, NULL, \
|
||||
&half_bridge_stm32_hrtim_data_##n, &half_bridge_stm32_hrtim_config_##n, \
|
||||
POST_KERNEL, CONFIG_HALF_BRIDGE_INIT_PRIORITY, \
|
||||
&half_bridge_stm32_hrtim_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(HALF_BRIDGE_STM32_HRTIM_INIT)
|
||||
@@ -0,0 +1,11 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
|
||||
zephyr_library_include_directories(../stm32_tim)
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_POWER_SWITCH_GPIO power_switch_gpio.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POWER_SWITCH_STM32_LPTIM power_switch_stm32_lptim.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POWER_SWITCH_STM32_PWM power_switch_stm32_pwm.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POWER_SWITCH_EMUL power_switch_emul.c)
|
||||
@@ -0,0 +1,72 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
menuconfig POWER_SWITCH
|
||||
bool "Power switch drivers"
|
||||
default y if DT_HAS_LIBRESOLAR_GPIO_POWER_SWITCH_ENABLED
|
||||
default y if DT_HAS_LIBRESOLAR_STM32_LPTIM_POWER_SWITCH_ENABLED
|
||||
default y if DT_HAS_LIBRESOLAR_STM32_PWM_POWER_SWITCH_ENABLED
|
||||
default y if DT_HAS_LIBRESOLAR_POWER_SWITCH_EMUL_ENABLED
|
||||
help
|
||||
Enable drivers for power switches, e.g. used for load or USB outputs.
|
||||
|
||||
if POWER_SWITCH
|
||||
|
||||
config POWER_SWITCH_INIT_PRIORITY
|
||||
int "Power switch init priority"
|
||||
default 70
|
||||
help
|
||||
Power switch driver device initialization priority. Must be lower than the priority
|
||||
of the GPIO and PWM drivers.
|
||||
|
||||
module = POWER_SWITCH
|
||||
module-str = power_switch
|
||||
source "subsys/logging/Kconfig.template.log_config"
|
||||
|
||||
config POWER_SWITCH_GPIO
|
||||
bool "GPIO power switch driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_GPIO_POWER_SWITCH_ENABLED
|
||||
select GPIO
|
||||
help
|
||||
Enable driver for power switches driven by a GPIO.
|
||||
|
||||
DT_COMPAT_LIBRESOLAR_GPIO_POWER_SWITCH := libresolar,gpio-power-switch
|
||||
|
||||
config POWER_SWITCH_GPIO_CHARGE_PUMP
|
||||
bool
|
||||
default y
|
||||
depends on POWER_SWITCH_GPIO
|
||||
depends on $(dt_compat_any_has_prop,$(DT_COMPAT_LIBRESOLAR_GPIO_POWER_SWITCH),charge-pump-pwms)
|
||||
select PWM
|
||||
help
|
||||
Support for charge pump PWM signals of GPIO power switches.
|
||||
|
||||
config POWER_SWITCH_STM32_LPTIM
|
||||
bool "STM32 LPTIM power switch driver with short-circuit protection"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_STM32_LPTIM_POWER_SWITCH_ENABLED
|
||||
depends on SOC_SERIES_STM32L0X
|
||||
select GPIO
|
||||
select PINCTRL
|
||||
help
|
||||
Enable power switch driver using LPTIM1 and COMP2 of STM32L0 for hardware short-circuit
|
||||
protection.
|
||||
|
||||
config POWER_SWITCH_STM32_PWM
|
||||
bool "STM32 timer PWM power switch driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_STM32_PWM_POWER_SWITCH_ENABLED
|
||||
select LIBRESOLAR_STM32_TIM
|
||||
help
|
||||
Enable power switch driver using a PWM signal of an STM32 timer, e.g. for PWM solar
|
||||
charge controllers.
|
||||
|
||||
config POWER_SWITCH_EMUL
|
||||
bool "Emulated power switch driver"
|
||||
default y
|
||||
depends on DT_HAS_LIBRESOLAR_POWER_SWITCH_EMUL_ENABLED
|
||||
help
|
||||
Enable emulated power switch driver for tests on native_sim.
|
||||
|
||||
endif # POWER_SWITCH
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user