diff --git a/boards/Kconfig b/boards/Kconfig index 89a3908..d9e1e56 100644 --- a/boards/Kconfig +++ b/boards/Kconfig @@ -196,4 +196,12 @@ config CUSTOM_DATA_NODES_FILE Use a custom file instead of default data_nodes.cpp for ThingSet configuration. +config CUSTOM_DCDC_CONTROLLER + bool "Custom implementation of DC/DC controller" + depends on $(dt_compat_enabled,dcdc) + default n + help + Use a custom implementation instead of the default perturb & observe + MPPT control. + endif diff --git a/src/daq.cpp b/src/daq.cpp index cf2b12b..653ce14 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -34,16 +34,6 @@ static volatile AdcAlert adc_alerts_lower[NUM_ADC_CH] = {}; extern DeviceStatus dev_stat; -#ifdef CONFIG_SOC_SERIES_STM32G4X -// Using internal reference buffer at VREF+ pin, set to 2048 mV -#define VREF (2048) -#else -// internal STM reference voltage -#define VREF (VREFINT_VALUE * VREFINT_CAL / adc_value(ADC_POS_VREF_MCU)) -#endif - -#define ADC_GAIN(name) ((float)DT_ADC_GAIN_##name##_NUMERATOR / DT_ADC_GAIN_##name##_DENOMINATOR) - /** * Average value for ADC channel * diff --git a/src/daq.h b/src/daq.h index 51ffe52..c56c44f 100644 --- a/src/daq.h +++ b/src/daq.h @@ -24,6 +24,18 @@ extern "C" { #define ADC_FILTER_CONST 5 // filter multiplier = 1/(2^ADC_FILTER_CONST) +#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 +#define VREF (VREFINT_VALUE * VREFINT_CAL / adc_value(ADC_POS_VREF_MCU)) +#endif + +#define ADC_GAIN(name) ((float)DT_ADC_GAIN_##name##_NUMERATOR / DT_ADC_GAIN_##name##_DENOMINATOR) + /** * Struct to definie upper and lower limit alerts for any ADC channel */ diff --git a/src/daq_driver.c b/src/daq_driver.c index dd35cef..23decd0 100644 --- a/src/daq_driver.c +++ b/src/daq_driver.c @@ -34,6 +34,7 @@ #include #endif +#include "dcdc.h" // for low-level control function called by DMA #include "board.h" #include "debug.h" @@ -226,6 +227,12 @@ static void DMA2_Channel1_IRQHandler(void *args) } } 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 diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 3325596..f9ae995 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -162,7 +162,7 @@ int Dcdc::check_start_conditions() return 0; } -void Dcdc::control() +__weak void Dcdc::control() { if (half_bridge_enabled()) { const char *stop_reason = NULL; diff --git a/src/dcdc.h b/src/dcdc.h index fdbefe8..f51a743 100644 --- a/src/dcdc.h +++ b/src/dcdc.h @@ -15,6 +15,8 @@ #include #include +#ifdef __cplusplus + #include "power_port.h" /** DC/DC basic operation mode @@ -130,4 +132,29 @@ private: int perturb_observe_controller(); }; +#endif // __cplusplus + + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef CONFIG_CUSTOM_DCDC_CONTROLLER + +/** + * Low-level control function + * + * Implement this function e.g. for cycle-by-cylce current limitation. + * + * It is called from the DMA after each new current reading, i.e. it runs in ISR context with + * high frequency and must be VERY fast! + */ +void dcdc_low_level_controller(); + +#endif // CONFIG_CUSTOM_DCDC_CONTROLLER + +#ifdef __cplusplus +} +#endif + #endif /* DCDC_H */ diff --git a/test/zephyr.h b/test/zephyr.h index 80ef740..035ba05 100644 --- a/test/zephyr.h +++ b/test/zephyr.h @@ -10,3 +10,10 @@ * @brief Mock necessary functions and generated Devicetree or Kconfig defines from Zephyr * for unit tests */ + +#ifndef __deprecated +#define __deprecated __attribute__((deprecated)) +#endif +#ifndef __weak +#define __weak __attribute__((__weak__)) +#endif