Replace the register-level timer code in pwm_switch_driver.c by the STM32 PWM power switch driver. The duty cycle steps of the control algorithm are kept at 100 us on-time per step, independent of the timer resolution of the driver. The rising edges of the PWM signal are only reported while the switch is on. Before, the overvoltage alert of the battery voltage was also inhibited periodically while the switch was off. The PWM switch is now a child node of the timer with the label pwm_switch instead of a child of the outputs node referencing a Zephyr PWM device. The unit tests use the emulated power switch, so that the dummy PWM binding is not needed anymore. The obsolete kconfig-flag and latching-pgood properties are removed from the outputs binding. Note for out-of-tree boards: The pwm_switch child of the outputs node must be converted to a libresolar,stm32-pwm-power-switch node below the st,stm32-timers node. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
3.5 KiB
C++
118 lines
3.5 KiB
C++
/*
|
|
* Copyright (c) The Libre Solar Project Contributors
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/** @file
|
|
*
|
|
* @brief Setup of ports and other essential charge controller objects
|
|
*/
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include "thingset.h" // handles access to internal data via communication interfaces
|
|
|
|
#include "bat_charger.h" // battery settings and charger state machine
|
|
#include "board.h"
|
|
#include "daq.h" // ADC using DMA and conversion to measurement values
|
|
#include "data_objects.h" // for access to internal data via ThingSet
|
|
#include "dcdc.h" // DC/DC converter control (hardware independent)
|
|
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
|
|
#include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc.
|
|
#include "leds.h" // LED switching using charlieplexing
|
|
#include "load.h" // load and USB output management
|
|
#include "pwm_switch.h" // PWM charge controller
|
|
|
|
DcBus lv_bus;
|
|
PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT)
|
|
|
|
#if BOARD_HAS_DCDC
|
|
DcBus hv_bus;
|
|
PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT)
|
|
#if CONFIG_HV_TERMINAL_NANOGRID
|
|
Dcdc dcdc(DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE_AUTO);
|
|
#elif CONFIG_HV_TERMINAL_BATTERY
|
|
Dcdc dcdc(DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE_BOOST);
|
|
#else
|
|
Dcdc dcdc(DEVICE_DT_GET(HALF_BRIDGE_NODE), &hv_bus, &lv_bus, DCDC_MODE_BUCK);
|
|
#endif // CONFIG_HV_TERMINAL
|
|
#endif
|
|
|
|
#if BOARD_HAS_PWM_PORT
|
|
PwmSwitch pwm_switch(&lv_bus, DEVICE_DT_GET(PWM_SWITCH_NODE));
|
|
#endif
|
|
|
|
#if BOARD_HAS_LOAD_OUTPUT
|
|
#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, DEVICE_DT_GET(USB_SWITCH_NODE));
|
|
#endif
|
|
|
|
#if CONFIG_HV_TERMINAL_SOLAR
|
|
PowerPort &solar_terminal = hv_terminal;
|
|
#elif CONFIG_LV_TERMINAL_SOLAR
|
|
PowerPort &solar_terminal = lv_terminal;
|
|
#elif CONFIG_PWM_TERMINAL_SOLAR
|
|
PowerPort &solar_terminal = pwm_switch;
|
|
#endif
|
|
|
|
#if CONFIG_HV_TERMINAL_NANOGRID
|
|
PowerPort &grid_terminal = hv_terminal;
|
|
#endif
|
|
|
|
#if CONFIG_LV_TERMINAL_BATTERY
|
|
PowerPort &bat_terminal = lv_terminal;
|
|
#elif CONFIG_HV_TERMINAL_BATTERY
|
|
PowerPort &bat_terminal = hv_terminal;
|
|
#endif
|
|
|
|
Charger charger(&bat_terminal);
|
|
|
|
BatConf bat_conf; // actual (used) battery configuration
|
|
BatConf bat_conf_user; // temporary storage where the user can write to
|
|
|
|
DeviceStatus dev_stat;
|
|
|
|
// current unix timestamp (independent of time(NULL), as it is user-configurable)
|
|
// uint32_t considered large enough, so we avoid 64-bit math (overflow in year 2106)
|
|
uint32_t timestamp;
|
|
|
|
#ifndef UNIT_TEST
|
|
|
|
#include <soc.h>
|
|
#include <zephyr/kernel.h>
|
|
|
|
static inline void timestamp_inc(struct k_timer *timer_id)
|
|
{
|
|
ARG_UNUSED(timer_id);
|
|
timestamp++;
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
static struct k_timer timestamp_timer;
|
|
k_timer_init(×tamp_timer, timestamp_inc, NULL);
|
|
k_timer_start(×tamp_timer, K_MSEC(1000), K_MSEC(1000));
|
|
|
|
/*
|
|
* printf from newlib-nano requires malloc, but Zephyr garbage-collects heap management if it
|
|
* is not used anywhere in the code. Below dummy calls force Zephyr to build with heap support.
|
|
*/
|
|
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
|