app: use half-bridge driver for DC/DC converter
Replace the register-level half_bridge.c module by the new half-bridge drivers. The Dcdc class gets the half-bridge device injected via its constructor, so that the emulated driver can be used in unit tests. Board devicetrees are migrated to the new compatibles: - TIM1 boards use libresolar,stm32-half-bridge with complementary outputs on channel 1. The ADC trigger via OC6/TRGO2 is enabled for the STM32G4 boards as before. - MPPT 1210 HUS uses libresolar,stm32-half-bridge with TIM3 channel 3 (high side) and channel 4 (low side). - Nucleo G474RE uses libresolar,stm32-hrtim-half-bridge. The HRTIM node gets the timer clock source as second clock. Instead of the half-bridge compatible, the halfbridge node label is now used to detect if the board has a DC/DC converter. The half-bridge unit tests are removed, as they are superseded by the driver test suite in tests/drivers/half_bridge. Note for out-of-tree boards: Nodes with "half-bridge" compatible must be migrated to one of the new compatibles, and the deadtime property was renamed to deadtime-ns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
fd67d021a8
commit
6c2251e2c8
+2
-2
@@ -32,7 +32,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.
|
||||
@@ -401,7 +401,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
|
||||
|
||||
@@ -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-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
pinctrl-names = "default";
|
||||
channel = <1>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
adc-trigger;
|
||||
};
|
||||
};
|
||||
*/
|
||||
|
||||
@@ -8,7 +8,6 @@ target_sources(app PRIVATE
|
||||
daq_driver.c
|
||||
device_status.cpp
|
||||
dcdc.cpp
|
||||
half_bridge.c
|
||||
hardware.cpp
|
||||
oled.cpp
|
||||
leds.cpp
|
||||
|
||||
+2
-1
@@ -15,7 +15,8 @@
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#define BOARD_HAS_DCDC DT_HAS_COMPAT_STATUS_OKAY(half_bridge)
|
||||
#define HALF_BRIDGE_NODE DT_NODELABEL(halfbridge)
|
||||
#define BOARD_HAS_DCDC DT_NODE_HAS_STATUS_OKAY(HALF_BRIDGE_NODE)
|
||||
#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))
|
||||
|
||||
+36
-25
@@ -16,10 +16,11 @@
|
||||
#include <stdlib.h> // for min/max function
|
||||
|
||||
#include "device_status.h"
|
||||
#include "half_bridge.h"
|
||||
#include "helper.h"
|
||||
#include "setup.h"
|
||||
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include <thingset.h>
|
||||
#include <thingset/sdk.h>
|
||||
#include <thingset/storage.h>
|
||||
@@ -46,8 +47,9 @@ LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL);
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
|
||||
Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode)
|
||||
Dcdc::Dcdc(const struct device *hb, DcBus *high, DcBus *low, DcdcOperationMode op_mode)
|
||||
{
|
||||
half_bridge = hb;
|
||||
hvb = high;
|
||||
lvb = low;
|
||||
mode = op_mode;
|
||||
@@ -61,9 +63,15 @@ Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode)
|
||||
restart_interval = 60;
|
||||
off_timestamp = -10000; // start immediately
|
||||
|
||||
if (!device_is_ready(half_bridge)) {
|
||||
LOG_ERR("Half bridge not ready");
|
||||
enable = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// 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);
|
||||
half_bridge_set_duty_limits(half_bridge, 12 / hs_voltage_max, 0.97);
|
||||
half_bridge_set_duty_cycle(half_bridge, 0.97); // init with allowed value
|
||||
}
|
||||
|
||||
void Dcdc::perturb_observe_buck()
|
||||
@@ -186,7 +194,7 @@ __weak DcdcOperationMode Dcdc::check_start_conditions()
|
||||
bool Dcdc::check_hs_mosfet_short()
|
||||
{
|
||||
static uint32_t first_time_detected;
|
||||
if (half_bridge_enabled() == false && inductor_current > 0.5F
|
||||
if (half_bridge_is_enabled(half_bridge) == 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
|
||||
@@ -224,7 +232,7 @@ bool Dcdc::startup_inhibit(bool reset)
|
||||
|
||||
__weak void Dcdc::control()
|
||||
{
|
||||
if (half_bridge_enabled() == false) {
|
||||
if (half_bridge_is_enabled(half_bridge) == false) {
|
||||
|
||||
if (check_hs_mosfet_short()) {
|
||||
return;
|
||||
@@ -249,21 +257,21 @@ __weak void Dcdc::control()
|
||||
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));
|
||||
half_bridge_set_duty_cycle(half_bridge, 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_set_duty_cycle(half_bridge, lvb->voltage / (hvb->voltage + 1));
|
||||
}
|
||||
|
||||
half_bridge_start();
|
||||
half_bridge_start(half_bridge);
|
||||
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);
|
||||
(double)half_bridge_get_duty_cycle(half_bridge) * 100.0);
|
||||
}
|
||||
else {
|
||||
startup_inhibit(true); // reset inhibit counter
|
||||
@@ -287,7 +295,8 @@ __weak void Dcdc::control()
|
||||
}
|
||||
|
||||
if (pwm_direction != 0) {
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() + pwm_direction * DUTY_STEP_SIZE);
|
||||
half_bridge_set_compare(half_bridge, half_bridge_get_compare(half_bridge)
|
||||
+ 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, "
|
||||
@@ -296,7 +305,8 @@ __weak void Dcdc::control()
|
||||
(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);
|
||||
(double)half_bridge_get_duty_cycle(half_bridge) * 100.0, state,
|
||||
pwm_direction);
|
||||
}
|
||||
else {
|
||||
stop_reason = "low power";
|
||||
@@ -312,7 +322,7 @@ __weak void Dcdc::control()
|
||||
|
||||
void Dcdc::test()
|
||||
{
|
||||
if (half_bridge_enabled()) {
|
||||
if (half_bridge_is_enabled(half_bridge)) {
|
||||
const char *stop_reason = NULL;
|
||||
if (lvb->voltage > ls_voltage_max || hvb->voltage > hs_voltage_max) {
|
||||
stop_reason = "emergency (voltage limits exceeded)";
|
||||
@@ -321,11 +331,11 @@ void Dcdc::test()
|
||||
stop_reason = "disabled";
|
||||
}
|
||||
else {
|
||||
if (half_bridge_get_duty_cycle() > 0.50f) {
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() - 1);
|
||||
if (half_bridge_get_duty_cycle(half_bridge) > 0.50f) {
|
||||
half_bridge_set_compare(half_bridge, half_bridge_get_compare(half_bridge) - 1);
|
||||
}
|
||||
else {
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() + 1);
|
||||
half_bridge_set_compare(half_bridge, half_bridge_get_compare(half_bridge) + 1);
|
||||
}
|
||||
}
|
||||
if (stop_reason != NULL) {
|
||||
@@ -340,11 +350,11 @@ void Dcdc::test()
|
||||
return;
|
||||
}
|
||||
|
||||
half_bridge_set_duty_cycle(lvb->voltage / hvb->voltage);
|
||||
half_bridge_start();
|
||||
half_bridge_set_duty_cycle(half_bridge, lvb->voltage / hvb->voltage);
|
||||
half_bridge_start(half_bridge);
|
||||
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);
|
||||
(double)half_bridge_get_duty_cycle(half_bridge) * 100.0);
|
||||
}
|
||||
else {
|
||||
startup_inhibit(true); // reset inhibit counter
|
||||
@@ -354,7 +364,7 @@ void Dcdc::test()
|
||||
|
||||
void Dcdc::stop()
|
||||
{
|
||||
half_bridge_stop();
|
||||
half_bridge_stop(half_bridge);
|
||||
state = DCDC_CONTROL_OFF;
|
||||
off_timestamp = uptime();
|
||||
output_hvs_disable();
|
||||
@@ -367,10 +377,11 @@ void Dcdc::fuse_destruction()
|
||||
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();
|
||||
half_bridge_stop(half_bridge);
|
||||
// reset safety limits to allow 0% duty cycle
|
||||
half_bridge_set_duty_limits(half_bridge, 0, 0.98);
|
||||
half_bridge_set_duty_cycle(half_bridge, 0);
|
||||
half_bridge_start(half_bridge);
|
||||
// now the fuse should be triggered and we disappear
|
||||
}
|
||||
counter++;
|
||||
@@ -400,7 +411,7 @@ void Dcdc::output_hvs_disable()
|
||||
|
||||
#else
|
||||
|
||||
Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode)
|
||||
Dcdc::Dcdc(const struct device *hb, DcBus *high, DcBus *low, DcdcOperationMode op_mode)
|
||||
{}
|
||||
|
||||
#endif /* BOARD_HAS_DCDC */
|
||||
|
||||
+7
-2
@@ -13,12 +13,14 @@
|
||||
* @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>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include "power_port.h"
|
||||
@@ -94,11 +96,12 @@ public:
|
||||
*
|
||||
* See http://libre.solar/docs/dcdc_control for detailed information
|
||||
*
|
||||
* @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)
|
||||
*/
|
||||
Dcdc(DcBus *high, DcBus *low, DcdcOperationMode mode);
|
||||
Dcdc(const struct device *half_bridge, DcBus *high, DcBus *low, DcdcOperationMode mode);
|
||||
|
||||
/**
|
||||
* Check for valid start conditions of the DC/DC converter
|
||||
@@ -137,6 +140,8 @@ public:
|
||||
*/
|
||||
void fuse_destruction();
|
||||
|
||||
const struct device *half_bridge; ///< Half-bridge device generating the PWM signal
|
||||
|
||||
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)
|
||||
|
||||
@@ -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_ */
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
#include "hardware.h"
|
||||
|
||||
#include "half_bridge.h"
|
||||
#include "helper.h"
|
||||
#include "leds.h"
|
||||
#include "load.h"
|
||||
|
||||
+3
-2
@@ -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
|
||||
@@ -154,7 +155,7 @@ void control_thread()
|
||||
#if BOARD_HAS_DCDC
|
||||
hv_terminal.update_bus_current_margins();
|
||||
dcdc.control(); // control of DC/DC including MPPT algorithm
|
||||
charging |= half_bridge_enabled();
|
||||
charging |= half_bridge_is_enabled(dcdc.half_bridge);
|
||||
#endif
|
||||
|
||||
leds_set_charging(charging);
|
||||
|
||||
+6
-5
@@ -15,7 +15,8 @@
|
||||
|
||||
#include "oled_ssd1306.h"
|
||||
|
||||
#include "half_bridge.h"
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include "setup.h"
|
||||
|
||||
#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), uext_en))
|
||||
@@ -57,7 +58,7 @@ void oled_update()
|
||||
oled.drawBitmap(6, 0, bmp_pv_panel, 16, 16, 1);
|
||||
oled.drawBitmap(104, 0, bmp_load, 16, 16, 1);
|
||||
|
||||
if (half_bridge_enabled() == false) {
|
||||
if (half_bridge_is_enabled(dcdc.half_bridge) == false) {
|
||||
oled.drawBitmap(27, 3, bmp_disconnected, 32, 8, 1);
|
||||
}
|
||||
else {
|
||||
@@ -94,7 +95,7 @@ void oled_update()
|
||||
#if BOARD_HAS_PWM_PORT
|
||||
if (pwm_switch.active()) {
|
||||
#else
|
||||
if (half_bridge_enabled()) {
|
||||
if (half_bridge_is_enabled(dcdc.half_bridge)) {
|
||||
#endif
|
||||
oled.setTextCursor(0, 18);
|
||||
len = snprintf(buf, sizeof(buf), "%4.0fW",
|
||||
@@ -149,8 +150,8 @@ void oled_update()
|
||||
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
|
||||
|
||||
float temp = charger.ext_temp_sensor ? charger.bat_temperature : dev_stat.internal_temp;
|
||||
|
||||
+3
-4
@@ -19,7 +19,6 @@
|
||||
#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
|
||||
@@ -32,11 +31,11 @@ PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typic
|
||||
DcBus hv_bus;
|
||||
PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT)
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
Dcdc dcdc(&hv_bus, &lv_bus, DCDC_MODE_AUTO);
|
||||
Dcdc dcdc(DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE_AUTO);
|
||||
#elif CONFIG_HV_TERMINAL_BATTERY
|
||||
Dcdc dcdc(&hv_bus, &lv_bus, DCDC_MODE_BOOST);
|
||||
Dcdc dcdc(DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE_BOOST);
|
||||
#else
|
||||
Dcdc dcdc(&hv_bus, &lv_bus, DCDC_MODE_BUCK);
|
||||
Dcdc dcdc(DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE_BUCK);
|
||||
#endif // CONFIG_HV_TERMINAL
|
||||
#endif
|
||||
|
||||
|
||||
@@ -195,11 +195,13 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-half-bridge";
|
||||
pinctrl-0 = <&tim3_ch3_pb0 &tim3_ch4_pb1>;
|
||||
pinctrl-names = "default";
|
||||
channel = <3>;
|
||||
low-side-channel = <4>;
|
||||
frequency = <50000>;
|
||||
deadtime = <230>;
|
||||
deadtime-ns = <230>;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -206,11 +206,13 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pc13>;
|
||||
pinctrl-names = "default";
|
||||
channel = <1>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
adc-trigger;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -226,11 +226,13 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
pinctrl-names = "default";
|
||||
channel = <1>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
adc-trigger;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -225,11 +225,12 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
pinctrl-names = "default";
|
||||
channel = <1>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -181,11 +181,13 @@
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
compatible = "libresolar,stm32-half-bridge";
|
||||
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch1n_pb13>;
|
||||
pinctrl-names = "default";
|
||||
channel = <1>;
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
deadtime-ns = <300>;
|
||||
adc-trigger;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
# Copyright (c) The Libre Solar Project Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
description: Half bridge, consisting of two coupled PWM outputs
|
||||
|
||||
compatible: "half-bridge"
|
||||
|
||||
include: [base.yaml, pinctrl-device.yaml]
|
||||
|
||||
properties:
|
||||
frequency:
|
||||
type: int
|
||||
required: true
|
||||
description: PWM frequency in Hz
|
||||
|
||||
deadtime:
|
||||
type: int
|
||||
required: true
|
||||
description: Dead time for synchronous PWM generation in nanoseconds
|
||||
@@ -30,7 +30,6 @@ target_sources(app PRIVATE
|
||||
src/tests_daq.cpp
|
||||
src/tests_dcdc.cpp
|
||||
src/tests_device_status.cpp
|
||||
src/tests_half_bridge.cpp
|
||||
src/tests_load.cpp
|
||||
src/tests_power_port.cpp
|
||||
)
|
||||
|
||||
@@ -130,15 +130,12 @@
|
||||
};
|
||||
};
|
||||
|
||||
timers1: timers1 {
|
||||
status = "okay";
|
||||
|
||||
halfbridge: halfbridge {
|
||||
compatible = "half-bridge";
|
||||
frequency = <70000>;
|
||||
deadtime = <300>;
|
||||
};
|
||||
};
|
||||
halfbridge: halfbridge {
|
||||
compatible = "libresolar,half-bridge-emul";
|
||||
clock-frequency = <24000000>;
|
||||
frequency = <70000>;
|
||||
deadtime-ns = <300>;
|
||||
};
|
||||
|
||||
timers3: timers {
|
||||
status = "okay";
|
||||
|
||||
@@ -33,7 +33,6 @@ int main()
|
||||
err += daq_tests();
|
||||
err += bat_charger_tests();
|
||||
err += power_port_tests();
|
||||
err += half_bridge_tests();
|
||||
err += dcdc_tests();
|
||||
err += device_status_tests();
|
||||
err += load_tests();
|
||||
|
||||
@@ -13,8 +13,6 @@ int daq_tests();
|
||||
|
||||
int power_port_tests();
|
||||
|
||||
int half_bridge_tests();
|
||||
|
||||
int dcdc_tests();
|
||||
|
||||
int device_status_tests();
|
||||
|
||||
+42
-40
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "tests.h"
|
||||
|
||||
#include "half_bridge.h"
|
||||
#include <libresolar/drivers/half_bridge.h>
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
@@ -46,7 +46,8 @@ static void start_buck()
|
||||
{
|
||||
dev_stat.error_flags = 0;
|
||||
init_structs_buck();
|
||||
half_bridge_init(70, 200, 12 / dcdc.hs_voltage_max, 0.97);
|
||||
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);
|
||||
dcdc.control();
|
||||
dcdc.control();
|
||||
dcdc.control(); // call multiple times because of startup delay
|
||||
@@ -55,7 +56,7 @@ static void start_buck()
|
||||
|
||||
static void init_structs_boost(int num_batteries = 1)
|
||||
{
|
||||
half_bridge_stop();
|
||||
half_bridge_stop(dcdc.half_bridge);
|
||||
|
||||
hv_terminal.init_solar();
|
||||
lv_terminal.bus->voltage = 20;
|
||||
@@ -86,7 +87,8 @@ static void init_structs_boost(int num_batteries = 1)
|
||||
static void start_boost()
|
||||
{
|
||||
init_structs_boost();
|
||||
half_bridge_init(70, 200, 12 / dcdc.hs_voltage_max, 0.97);
|
||||
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);
|
||||
dcdc.control();
|
||||
dcdc.control();
|
||||
dcdc.control(); // call multiple times because of startup delay
|
||||
@@ -205,19 +207,19 @@ void no_boost_start_if_solar_voltage_low()
|
||||
void buck_increasing_power()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after > pwm_before);
|
||||
}
|
||||
|
||||
void buck_derating_output_voltage_too_high()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
lv_terminal.bus->voltage = lv_terminal.bus->sink_voltage_intercept + 0.1f;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after < pwm_before); // less duty cycle = higher voltage
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CV_LS, dcdc.state);
|
||||
}
|
||||
@@ -225,11 +227,11 @@ void buck_derating_output_voltage_too_high()
|
||||
void buck_derating_output_current_too_high()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
lv_terminal.current = lv_terminal.pos_current_limit + 0.1f;
|
||||
lv_terminal.update_bus_current_margins();
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CC_LS, dcdc.state);
|
||||
}
|
||||
@@ -237,10 +239,10 @@ void buck_derating_output_current_too_high()
|
||||
void buck_derating_inductor_current_too_high()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
dcdc.inductor_current = dcdc.inductor_current_max + 0.1f;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CC_LS, dcdc.state);
|
||||
}
|
||||
@@ -248,11 +250,11 @@ void buck_derating_inductor_current_too_high()
|
||||
void buck_derating_input_voltage_too_low()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
hv_terminal.bus->voltage = hv_terminal.bus->src_voltage_intercept - 0.1f;
|
||||
dcdc.power = 1.2f;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CV_HS, dcdc.state);
|
||||
}
|
||||
@@ -260,11 +262,11 @@ void buck_derating_input_voltage_too_low()
|
||||
void buck_derating_input_current_too_high()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
hv_terminal.current = hv_terminal.neg_current_limit - 0.1f;
|
||||
hv_terminal.update_bus_current_margins();
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CC_HS, dcdc.state);
|
||||
}
|
||||
@@ -272,10 +274,10 @@ void buck_derating_input_current_too_high()
|
||||
void buck_derating_temperature_limits_exceeded()
|
||||
{
|
||||
start_buck();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
dcdc.temp_mosfets = 81;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
}
|
||||
|
||||
@@ -284,7 +286,7 @@ void buck_stop_input_power_too_low()
|
||||
start_buck();
|
||||
dcdc.power_good_timestamp = time(NULL) - 11;
|
||||
dcdc.control();
|
||||
TEST_ASSERT(half_bridge_enabled() == false);
|
||||
TEST_ASSERT(half_bridge_is_enabled(dcdc.half_bridge) == false);
|
||||
}
|
||||
|
||||
void buck_stop_high_voltage_emergency()
|
||||
@@ -292,7 +294,7 @@ void buck_stop_high_voltage_emergency()
|
||||
start_buck();
|
||||
lv_terminal.bus->voltage = dcdc.ls_voltage_max + 0.1f;
|
||||
dcdc.control();
|
||||
TEST_ASSERT(half_bridge_enabled() == false);
|
||||
TEST_ASSERT(half_bridge_is_enabled(dcdc.half_bridge) == false);
|
||||
}
|
||||
|
||||
void buck_correct_mppt_operation()
|
||||
@@ -301,15 +303,15 @@ void buck_correct_mppt_operation()
|
||||
|
||||
dcdc.power = 5;
|
||||
dcdc.control();
|
||||
float pwm1 = half_bridge_get_duty_cycle();
|
||||
float pwm1 = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
dcdc.power = 7;
|
||||
dcdc.control();
|
||||
float pwm2 = half_bridge_get_duty_cycle();
|
||||
float pwm2 = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm2 > pwm1);
|
||||
|
||||
dcdc.power = 6; // decrease power to make the direction turn around
|
||||
dcdc.control();
|
||||
float pwm3 = half_bridge_get_duty_cycle();
|
||||
float pwm3 = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm3 < pwm2);
|
||||
}
|
||||
|
||||
@@ -319,42 +321,42 @@ void boost_increasing_power()
|
||||
{
|
||||
// make sure that without changing anything, dcdc_control goes in direction of increasing power
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after < pwm_before);
|
||||
}
|
||||
|
||||
void boost_derating_output_voltage_too_high()
|
||||
{
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
hv_terminal.bus->voltage = hv_terminal.bus->sink_voltage_intercept + 0.5f;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after > pwm_before); // higher duty cycle = less power
|
||||
}
|
||||
|
||||
void boost_derating_output_current_too_high()
|
||||
{
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
hv_terminal.current = hv_terminal.pos_current_limit + 0.1f;
|
||||
hv_terminal.update_bus_current_margins();
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after > pwm_before);
|
||||
}
|
||||
|
||||
void boost_derating_input_voltage_too_low()
|
||||
{
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
lv_terminal.bus->voltage = lv_terminal.bus->src_voltage_intercept - 0.1f;
|
||||
dcdc.power = -1.2f;
|
||||
hv_terminal.update_bus_current_margins();
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT_EQUAL(DCDC_CONTROL_CV_LS, dcdc.state);
|
||||
TEST_ASSERT(pwm_after > pwm_before);
|
||||
}
|
||||
@@ -362,20 +364,20 @@ void boost_derating_input_voltage_too_low()
|
||||
void boost_derating_input_current_too_high()
|
||||
{
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
dcdc.inductor_current = lv_terminal.neg_current_limit - 0.1f;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after > pwm_before);
|
||||
}
|
||||
|
||||
void boost_derating_temperature_limits_exceeded()
|
||||
{
|
||||
start_boost();
|
||||
float pwm_before = half_bridge_get_duty_cycle();
|
||||
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
dcdc.temp_mosfets = 81;
|
||||
dcdc.control();
|
||||
float pwm_after = half_bridge_get_duty_cycle();
|
||||
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm_after > pwm_before);
|
||||
}
|
||||
|
||||
@@ -384,7 +386,7 @@ void boost_stop_input_power_too_low()
|
||||
start_boost();
|
||||
dcdc.power_good_timestamp = time(NULL) - 11;
|
||||
dcdc.control();
|
||||
TEST_ASSERT(half_bridge_enabled() == false);
|
||||
TEST_ASSERT(half_bridge_is_enabled(dcdc.half_bridge) == false);
|
||||
}
|
||||
|
||||
void boost_stop_high_voltage_emergency()
|
||||
@@ -392,7 +394,7 @@ void boost_stop_high_voltage_emergency()
|
||||
start_boost();
|
||||
hv_terminal.bus->voltage = dcdc.hs_voltage_max + 0.1f;
|
||||
dcdc.control();
|
||||
TEST_ASSERT(half_bridge_enabled() == false);
|
||||
TEST_ASSERT(half_bridge_is_enabled(dcdc.half_bridge) == false);
|
||||
}
|
||||
|
||||
void boost_correct_mppt_operation()
|
||||
@@ -401,15 +403,15 @@ void boost_correct_mppt_operation()
|
||||
|
||||
dcdc.power = -5;
|
||||
dcdc.control();
|
||||
float pwm1 = half_bridge_get_duty_cycle();
|
||||
float pwm1 = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
dcdc.power = -7;
|
||||
dcdc.control();
|
||||
float pwm2 = half_bridge_get_duty_cycle();
|
||||
float pwm2 = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm2 < pwm1);
|
||||
|
||||
dcdc.power = -6; // decrease power to make the direction turn around
|
||||
dcdc.control();
|
||||
float pwm3 = half_bridge_get_duty_cycle();
|
||||
float pwm3 = half_bridge_get_duty_cycle(dcdc.half_bridge);
|
||||
TEST_ASSERT(pwm3 > pwm2);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) The Libre Solar Project Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "half_bridge.h"
|
||||
#include "tests.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#define MAX_PWM_DUTY 0.97
|
||||
#define MIN_PWM_DUTY 0.1
|
||||
#define MID_PWM_DUTY ((MIN_PWM_DUTY + MAX_PWM_DUTY) / 2)
|
||||
#define PWM_F_KHZ 70
|
||||
#define PWM_DEADTIME_NS 300
|
||||
|
||||
const float duty_epsilon = 0.006;
|
||||
// calculated duty cycle float may deviate +/- duty_epsilon from test target value
|
||||
|
||||
static void init_structs()
|
||||
{
|
||||
half_bridge_init(PWM_F_KHZ, PWM_DEADTIME_NS, MIN_PWM_DUTY, MAX_PWM_DUTY);
|
||||
half_bridge_stop();
|
||||
}
|
||||
|
||||
void half_bridge_set_duty_cycle_works()
|
||||
{
|
||||
half_bridge_set_duty_cycle(MID_PWM_DUTY);
|
||||
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MID_PWM_DUTY, half_bridge_get_duty_cycle());
|
||||
}
|
||||
|
||||
void half_bridge_starts_up()
|
||||
{
|
||||
half_bridge_set_duty_cycle(MID_PWM_DUTY);
|
||||
half_bridge_start();
|
||||
TEST_ASSERT_EQUAL(true, half_bridge_enabled());
|
||||
}
|
||||
|
||||
void half_bridge_stops()
|
||||
{
|
||||
half_bridge_set_duty_cycle(MID_PWM_DUTY);
|
||||
half_bridge_start();
|
||||
half_bridge_stop();
|
||||
TEST_ASSERT_EQUAL(false, half_bridge_enabled());
|
||||
}
|
||||
|
||||
void half_bridge_duty_limits_not_violated()
|
||||
{
|
||||
// maximum limit
|
||||
half_bridge_set_duty_cycle(1.0);
|
||||
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MAX_PWM_DUTY, half_bridge_get_duty_cycle());
|
||||
|
||||
// minimum limit
|
||||
half_bridge_set_duty_cycle(0.0);
|
||||
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MIN_PWM_DUTY, half_bridge_get_duty_cycle());
|
||||
}
|
||||
|
||||
void half_bridge_ccr_limits_not_violated()
|
||||
{
|
||||
// maximum limit
|
||||
half_bridge_set_duty_cycle(MAX_PWM_DUTY);
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() + 1);
|
||||
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MAX_PWM_DUTY, half_bridge_get_duty_cycle());
|
||||
|
||||
// minimum limit
|
||||
half_bridge_set_duty_cycle(MIN_PWM_DUTY);
|
||||
half_bridge_set_ccr(half_bridge_get_ccr() - 1);
|
||||
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MIN_PWM_DUTY, half_bridge_get_duty_cycle());
|
||||
}
|
||||
|
||||
int half_bridge_tests()
|
||||
{
|
||||
init_structs();
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(half_bridge_set_duty_cycle_works);
|
||||
RUN_TEST(half_bridge_starts_up);
|
||||
RUN_TEST(half_bridge_stops);
|
||||
|
||||
RUN_TEST(half_bridge_duty_limits_not_violated);
|
||||
RUN_TEST(half_bridge_ccr_limits_not_violated);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user