From 93f142d32d6636341f9eb132bab4f17b25afcbf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Sun, 1 Nov 2026 14:46:09 +0100 Subject: [PATCH] app: remove SoC-specific code The STM32 bootloader entry function as well as the flash size and page size variables were only used by ThingSet items of the DFU group, which were removed with the switch to the ThingSet SDK. The unused code is removed together with mcu.h and the obsolete HW_LOG_LEVEL option. The USB-C dead battery pull-downs of the STM32G4 are already disabled by the Zephyr SoC initialization if USB device support is not enabled, so the register access in setup() is not necessary anymore. The DC/DC duty cycle step size depending on the SoC series is moved from the code to the new Kconfig option DCDC_DUTY_STEP_SIZE with the same defaults. Co-Authored-By: Claude Opus 5.5 --- app/Kconfig | 18 ++++---- app/src/data_objects.cpp | 13 ------ app/src/dcdc.cpp | 11 ++--- app/src/hardware.cpp | 65 +++-------------------------- app/src/hardware.h | 10 +---- app/src/mcu.h | 24 ----------- app/src/setup.cpp | 6 --- docs/architecture-migration-plan.md | 7 +++- 8 files changed, 26 insertions(+), 128 deletions(-) delete mode 100644 app/src/mcu.h diff --git a/app/Kconfig b/app/Kconfig index 6a69486..2f6115c 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -16,6 +16,17 @@ 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 DCDC_DUTY_STEP_SIZE + int + default 3 if SOC_SERIES_STM32G4X + default 1 + help + 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 @@ -373,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 diff --git a/app/src/data_objects.cpp b/app/src/data_objects.cpp index 922af76..0d20df7 100644 --- a/app/src/data_objects.cpp +++ b/app/src/data_objects.cpp @@ -6,16 +6,11 @@ #include "data_objects.h" -#include #include #include #include -#ifdef CONFIG_SOC_FAMILY_STM32 -#include -#endif - #include #include @@ -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 diff --git a/app/src/dcdc.cpp b/app/src/dcdc.cpp index 43bc34d..1102a4b 100644 --- a/app/src/dcdc.cpp +++ b/app/src/dcdc.cpp @@ -35,12 +35,6 @@ LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL); #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 @@ -301,8 +295,9 @@ __weak void Dcdc::control() } if (pwm_direction != 0) { - half_bridge_set_compare(half_bridge, half_bridge_get_compare(half_bridge) - + pwm_direction * DUTY_STEP_SIZE); + half_bridge_set_compare(half_bridge, + half_bridge_get_compare(half_bridge) + + 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, " diff --git a/app/src/hardware.cpp b/app/src/hardware.cpp index 9272709..7c6ded2 100644 --- a/app/src/hardware.cpp +++ b/app/src/hardware.cpp @@ -6,67 +6,11 @@ #include "hardware.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 +#include #include -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); @@ -85,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 */ diff --git a/app/src/hardware.h b/app/src/hardware.h index eece92c..3c7a2c9 100644 --- a/app/src/hardware.h +++ b/app/src/hardware.h @@ -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 */ diff --git a/app/src/mcu.h b/app/src/mcu.h deleted file mode 100644 index 4e01ddb..0000000 --- a/app/src/mcu.h +++ /dev/null @@ -1,24 +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 - -#endif // MCU_H_ diff --git a/app/src/setup.cpp b/app/src/setup.cpp index 8e49a8a..be94cd0 100644 --- a/app/src/setup.cpp +++ b/app/src/setup.cpp @@ -86,7 +86,6 @@ uint32_t timestamp; #ifndef UNIT_TEST -#include #include static inline void timestamp_inc(struct k_timer *timer_id) @@ -107,11 +106,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 diff --git a/docs/architecture-migration-plan.md b/docs/architecture-migration-plan.md index 6029416..14031cf 100644 --- a/docs/architecture-migration-plan.md +++ b/docs/architecture-migration-plan.md @@ -181,8 +181,13 @@ Move all remaining hardware access from `app/src` into Zephyr drivers. - [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 -- [ ] Bootloader entry and SoC-specific setup out of the application (`hardware.cpp`, +- [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 2: Devicetree power model