diff --git a/.github/workflows/zephyr.yml b/.github/workflows/zephyr.yml index a496f7c..46c3240 100644 --- a/.github/workflows/zephyr.yml +++ b/.github/workflows/zephyr.yml @@ -37,3 +37,9 @@ jobs: west build -b mppt_2420_lc -p west build -b mppt_1210_hus@0.7 -p west build -b pwm_2420_lus@0.3 -p + + - name: Run unit-tests + working-directory: charge-controller-firmware + run: | + cd tests + west build -b native_posix -t run diff --git a/README.md b/README.md index 61ea37b..3fa6ae4 100644 --- a/README.md +++ b/README.md @@ -157,9 +157,10 @@ the same tests locally (recommended before pushing to the server) use the script ### Unit-tests -Core functions of the firmware are covered by unit-tests (located in test directory). Run the tests with the following command: +Core functions of the firmware are covered by unit-tests (located in `tests` directory). Build and run the tests with the following command: - platformio test -e unit_test + cd tests + west build -p -b native_posix -t run ### Static code analysis diff --git a/platformio.ini b/platformio.ini index c9eff91..4b1df62 100644 --- a/platformio.ini +++ b/platformio.ini @@ -92,16 +92,3 @@ board = pwm_2420_lus@0.3 # Nucleo board for testing purposes [env:nucleo_g474re] board = nucleo_g474re - -[env:unit_test] -platform = native -framework = -build_flags = - -std=gnu++17 - -Wall - -D UNIT_TEST - -D __STDC_FORMAT_MACROS - -D FIRMWARE_VERSION_ID=\"v20.0-7-gda01bb3\" - -I test -# include src directory (otherwise unit-tests will only include lib directory) -test_build_project_src = true diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index 298edc3..125b4c7 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -8,10 +8,8 @@ #include -#ifndef UNIT_TEST #include LOG_MODULE_REGISTER(bat_charger, CONFIG_BAT_LOG_LEVEL); -#endif #include // for fabs function #include diff --git a/src/daq.cpp b/src/daq.cpp index 9fe31f2..035e75d 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -8,10 +8,8 @@ #include -#ifndef UNIT_TEST #include LOG_MODULE_REGISTER(daq, CONFIG_DAQ_LOG_LEVEL); -#endif #include // log for thermistor calculation #include @@ -364,9 +362,9 @@ void daq_set_hv_limit(float hv_overvoltage) } #endif -#if defined(UNIT_TEST) +#if !defined(CONFIG_SOC_FAMILY_STM32) -#include "../test/daq_stub.h" +#include "daq_stub.h" void prepare_adc_readings(AdcValues values) { diff --git a/src/data_objects.cpp b/src/data_objects.cpp index d5f3667..6e9747c 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -9,12 +9,13 @@ #include #include -#ifndef UNIT_TEST #include -#include #include #include LOG_MODULE_REGISTER(data_objects, CONFIG_DATA_OBJECTS_LOG_LEVEL); + +#ifdef CONFIG_SOC_FAMILY_STM32 +#include #endif #include @@ -37,9 +38,15 @@ const char metadata_url[] = const char device_type[] = DT_PROP(DT_PATH(pcb), type); const char hardware_version[] = DT_PROP(DT_PATH(pcb), version_str); const char firmware_version[] = FIRMWARE_VERSION_ID; +char device_id[9]; + +#ifdef CONFIG_SOC_FAMILY_STM32 uint32_t flash_size = LL_GetFlashSize(); uint32_t flash_page_size = FLASH_PAGE_SIZE; -char device_id[9]; +#else +uint32_t flash_size = 128; +uint32_t flash_page_size = 0x800; +#endif static char auth_password[11]; diff --git a/src/data_storage.cpp b/src/data_storage.cpp index b94170f..49aa2aa 100644 --- a/src/data_storage.cpp +++ b/src/data_storage.cpp @@ -15,7 +15,7 @@ #include -#ifndef UNIT_TEST +#ifdef CONFIG_SOC_FAMILY_STM32 #include #include diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 073521e..57d3110 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -8,13 +8,11 @@ #include -#ifndef UNIT_TEST #include #include #include LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL); -#endif #include // for fabs function #include // for min/max function diff --git a/src/half_bridge.cpp b/src/half_bridge.cpp index 0d31d40..19b1de6 100644 --- a/src/half_bridge.cpp +++ b/src/half_bridge.cpp @@ -13,7 +13,7 @@ #include "mcu.h" #include "setup.h" -#ifndef UNIT_TEST +#ifdef CONFIG_SOC_FAMILY_STM32 #include #include #include @@ -44,7 +44,7 @@ static uint16_t clamp_ccr(uint16_t ccr_target) } } -#ifndef UNIT_TEST +#ifdef CONFIG_SOC_FAMILY_STM32 static const struct soc_gpio_pinctrl tim_pinctrl[] = ST_STM32_DT_INST_PINCTRL(0, 0); diff --git a/src/helper.h b/src/helper.h index c381feb..6c7c0e0 100644 --- a/src/helper.h +++ b/src/helper.h @@ -30,7 +30,7 @@ extern "C" { */ static inline uint32_t uptime() { -#ifdef __ZEPHYR__ +#ifndef UNIT_TEST return k_uptime_get() / 1000; #else return time(NULL); diff --git a/src/leds.cpp b/src/leds.cpp index beb96d6..6c9bf91 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -7,10 +7,8 @@ #include "leds.h" #include -#ifndef UNIT_TEST #include #include -#endif #include "hardware.h" diff --git a/src/load_driver.c b/src/load_driver.c index 5108957..965ea9c 100644 --- a/src/load_driver.c +++ b/src/load_driver.c @@ -6,7 +6,7 @@ #include "load.h" -#ifndef UNIT_TEST +#ifdef CONFIG_SOC_FAMILY_STM32 #include #include @@ -229,7 +229,7 @@ bool pgood_check() #endif } -#else // UNIT_TEST +#else // CONFIG_SOC_FAMILY_STM32 void load_out_init() {;} void usb_out_init() {;} diff --git a/src/mcu.h b/src/mcu.h index dbae8d1..c6ac898 100644 --- a/src/mcu.h +++ b/src/mcu.h @@ -44,7 +44,7 @@ #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(UNIT_TEST) +#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 diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 5001701..c9d6447 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -8,10 +8,8 @@ #include -#ifndef UNIT_TEST #include LOG_MODULE_REGISTER(pwm_switch, CONFIG_PWM_LOG_LEVEL); -#endif #include #include diff --git a/test/autoconf.h b/test/autoconf.h deleted file mode 100644 index e19522c..0000000 --- a/test/autoconf.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) The Libre Solar Project Contributors - * - * SPDX-License-Identifier: Apache-2.0 - */ - -// PWM charge controller -#define CONFIG_PWM_TERMINAL_SOLAR 1 - -// MPPT buck/boost or nanogrid mode -#define CONFIG_HV_TERMINAL_SOLAR 1 -#define CONFIG_HV_TERMINAL_NANOGRID 0 - -// battery always assumed to be at low-voltage terminal (might need to be changed for boost mode) -#define CONFIG_LV_TERMINAL_BATTERY 1 - -#define CONFIG_THINGSET_EXPERT_PASSWORD "expert123" -#define CONFIG_THINGSET_MAKER_PASSWORD "maker456" - -// Values that are otherwise defined by Kconfig -#define CONFIG_CONTROL_FREQUENCY 10 // Hz - -#define CONFIG_BAT_TYPE_GEL 1 -#define CONFIG_BAT_TYPE 2 -#define CONFIG_BAT_CAPACITY_AH 50 -#define CONFIG_BAT_NUM_CELLS 6 -#define CONFIG_CELL_ABS_MAX_VOLTAGE_MV 2450 -#define CONFIG_CELL_TOPPING_VOLTAGE_MV 2400 -#define CONFIG_CELL_RECHARGE_VOLTAGE_MV 2300 -#define CONFIG_CELL_RECONNECT_VOLTAGE_MV 2100 -#define CONFIG_CELL_DISCONNECT_VOLTAGE_MV 1950 -#define CONFIG_CELL_ABS_MIN_VOLTAGE_MV 1600 -#define CONFIG_CELL_OCV_FULL_MV 2150 -#define CONFIG_CELL_OCV_EMPTY_MV 1900 -#define CONFIG_CELL_FLOAT 1 -#define CONFIG_CELL_FLOAT_VOLTAGE_MV 2300 -#define CONFIG_CELL_FLOAT_RECHARGE_TIME 1800 -#define CONFIG_CELL_EQUALIZATION_VOLTAGE_MV 2450 -#define CONFIG_CELL_EQUALIZATION_DURATION 3600 -#define CONFIG_CELL_EQUALIZATION_TRIGGER_DAYS 60 -#define CONFIG_CELL_EQUALIZATION_TRIGGER_DEEP_CYCLES 10 -#define CONFIG_CELL_TEMP_COMPENSATION_MV_K -3 -#define CONFIG_CELL_INTERNAL_RESISTANCE_MOHM 5 -#define CONFIG_BAT_CHARGE_TEMP_MAX 50 -#define CONFIG_BAT_CHARGE_TEMP_MIN 0 -#define CONFIG_BAT_DISCHARGE_TEMP_MAX 50 -#define CONFIG_BAT_DISCHARGE_TEMP_MIN -10 -#define CONFIG_LOAD_OC_RECOVERY_DELAY 300 -#define CONFIG_LOAD_LVD_RECOVERY_DELAY 300 diff --git a/test/devicetree.h b/test/devicetree.h deleted file mode 100644 index 4463874..0000000 --- a/test/devicetree.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) The Libre Solar Project Contributors - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/* - * Macros copied from devicetree.h and util.h - */ - -#define DT_NODE_EXISTS(node_id) DT_CAT(node_id, _EXISTS) - -#define DT_PROP(node_id, prop) DT_CAT(node_id, _P_##prop) -#define DT_PROP_LEN(node_id, prop) DT_PROP(node_id, prop##_LEN) - -#define DT_CHILD(node_id, child) UTIL_CAT(node_id, DT_S_PREFIX(child)) - -#define DT_CAT(node_id, prop_suffix) node_id##prop_suffix - -#define DT_S_PREFIX(name) _S_##name - -#define UTIL_CAT(a, ...) UTIL_PRIMITIVE_CAT(a, __VA_ARGS__) -#define UTIL_PRIMITIVE_CAT(a, ...) a##__VA_ARGS__ - -#define DT_FOREACH_CHILD(node_id, fn) \ - DT_CAT(node_id, _FOREACH_CHILD)(fn) - -#define DT_PHA_BY_IDX(node_id, pha, idx, cell) \ - DT_PROP(node_id, pha##_IDX_##idx##_VAL_##cell) - -#define DT_PHA(node_id, pha, cell) DT_PHA_BY_IDX(node_id, pha, 0, cell) - -#define DT_PHANDLE_BY_IDX(node_id, prop, idx) \ - DT_PROP(node_id, prop##_IDX_##idx##_PH) - -#define DT_PROP_BY_PHANDLE_IDX(node_id, phs, idx, prop) \ - DT_PROP(DT_PHANDLE_BY_IDX(node_id, phs, idx), prop) - -#define DT_PROP_BY_PHANDLE(node_id, ph, prop) \ - DT_PROP_BY_PHANDLE_IDX(node_id, ph, 0, prop) - -#define DT_HAS_COMPAT_STATUS_OKAY(compat) \ - IS_ENABLED(DT_CAT(DT_COMPAT_HAS_OKAY_, compat)) - -/* - * Simplified mocks for certain macros from devicetree.h - */ - -#define DT_INST(inst, compat) UTIL_CAT(DT_N_INST_, UTIL_CAT(inst, UTIL_CAT(_, compat))) - -#define DT_PATH(node) UTIL_CAT(DT_N_S_, node) diff --git a/test/devicetree_unfixed.h b/test/devicetree_unfixed.h deleted file mode 100644 index 547b3a0..0000000 --- a/test/devicetree_unfixed.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) The Libre Solar Project Contributors - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/* - * Needed defines that are normally generated by Zephyr build system - */ - -#define DT_N_S_pcb_P_type "TEST-PCB" -#define DT_N_S_pcb_P_version_str "v0.1" - -#define DT_N_S_pcb_P_hs_voltage_max 80 -#define DT_N_S_pcb_P_ls_voltage_max 32 - -#define DT_N_S_pcb_P_internal_tref_max 50 -#define DT_N_S_pcb_P_mosfets_tj_max 120 -#define DT_N_S_pcb_P_mosfets_tau_ja 5 - -#define DT_N_S_pcb_P_dcdc_current_max 20 -#define DT_N_S_pcb_P_dcdc_current_max_EXISTS 1 - -#define DT_COMPAT_HAS_OKAY_half_bridge 1 -#define DT_N_INST_0_half_bridge DT_N_S_soc_S_timers_40016800_S_halfbridge -#define DT_N_S_soc_S_timers_40016800_S_halfbridge_P_frequency 70000 -#define DT_N_S_soc_S_timers_40016800_S_halfbridge_P_deadtime 300 - -#define DT_N_S_outputs_S_pwm_switch_EXISTS 1 -#define DT_N_S_outputs_S_pwm_switch_P_current_max 20 -#define DT_N_S_outputs_S_pwm_switch_P_pwms_IDX_0_VAL_period 20000000 - -#define DT_N_S_outputs_S_load_EXISTS 1 -#define DT_N_S_outputs_S_load_P_current_max 20 - -#define DT_N_S_outputs_S_usb_pwr_EXISTS 1 - -/* - * ADC - */ -#define DT_N_S_adc_inputs_S_temp_bat_EXISTS 1 -#define DT_N_S_adc_inputs_S_temp_bat_P_multiplier 10000 -#define DT_N_S_adc_inputs_S_temp_bat_P_divider 1 -#define DT_N_S_adc_inputs_S_temp_bat_P_filter_const 5 - -#define DT_N_S_adc_inputs_S_v_high_P_multiplier 102800 -#define DT_N_S_adc_inputs_S_v_high_P_divider 2800 -#define DT_N_S_adc_inputs_S_v_high_P_filter_const 5 - -#define DT_N_S_adc_inputs_S_v_low_P_multiplier 105600 -#define DT_N_S_adc_inputs_S_v_low_P_divider 5600 -#define DT_N_S_adc_inputs_S_v_low_P_filter_const 5 - -#define DT_N_S_adc_inputs_S_v_pwm_P_multiplier 25224 // see pwm_2420_lus.dts -#define DT_N_S_adc_inputs_S_v_pwm_P_divider 984 -#define DT_N_S_adc_inputs_S_v_pwm_P_offset 2338 -#define DT_N_S_adc_inputs_S_v_pwm_P_filter_const 5 - -// amp gain: 50, resistor: 4 mOhm -#define DT_N_S_adc_inputs_S_i_load_P_multiplier 1000 -#define DT_N_S_adc_inputs_S_i_load_P_divider (4 * 50) -#define DT_N_S_adc_inputs_S_i_load_P_filter_const 5 - -#define DT_N_S_adc_inputs_S_i_dcdc_P_multiplier 1000 -#define DT_N_S_adc_inputs_S_i_dcdc_P_divider (4 * 50) -#define DT_N_S_adc_inputs_S_i_dcdc_P_filter_const 5 - -#define DT_N_S_adc_inputs_S_i_pwm_P_multiplier 1000 -#define DT_N_S_adc_inputs_S_i_pwm_P_divider (4 * 50) -#define DT_N_S_adc_inputs_S_i_pwm_P_filter_const 5 - -#define DT_N_S_adc_inputs_S_vref_mcu_P_filter_const 5 - -#define DT_N_S_adc_inputs_S_temp_mcu_P_filter_const 5 - -#define DT_N_S_adc_inputs_FOREACH_CHILD(fn) \ - fn(DT_N_S_adc_inputs_S_v_low) \ - fn(DT_N_S_adc_inputs_S_v_high) \ - fn(DT_N_S_adc_inputs_S_v_pwm) \ - fn(DT_N_S_adc_inputs_S_i_dcdc) \ - fn(DT_N_S_adc_inputs_S_i_load) \ - fn(DT_N_S_adc_inputs_S_i_pwm) \ - fn(DT_N_S_adc_inputs_S_temp_bat) \ - fn(DT_N_S_adc_inputs_S_vref_mcu) \ - fn(DT_N_S_adc_inputs_S_temp_mcu) - -/* - * LEDs - */ -#define DT_N_S_leds_FOREACH_CHILD(fn) \ - fn(DT_N_S_leds_S_pwr) - -#define DT_N_S_leds_P_gpios_IDX_0_PH DT_N_S_soc_S_pin_controller_48000000_S_gpio_48000800 -#define DT_N_S_leds_P_gpios_IDX_0_VAL_pin 15 -#define DT_N_S_leds_P_gpios_IDX_0_VAL_flags 0 -#define DT_N_S_leds_P_gpios_LEN 1 - -#define DT_N_S_soc_S_pin_controller_48000000_S_gpio_48000800_P_label "GPIOC" - -#define DT_N_S_leds_S_pwr_EXISTS 1 -#define DT_N_S_leds_S_pwr_P_states {1 /* 0x1 */} diff --git a/test/main.cpp b/test/main.cpp deleted file mode 100644 index 21172db..0000000 --- a/test/main.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) The Libre Solar Project Contributors - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "tests.h" - -#ifdef __WIN32__ - -// The below lines are added to fix unit test compilation error in PIO. -// The Unit Test Framework uses these functions like a constructor/destructor. -// We can just define two empty functions with these names. -void setUp (void) {} -void tearDown (void) {} - -#endif - -int main() -{ - daq_tests(); - bat_charger_tests(); - power_port_tests(); - half_bridge_tests(); - dcdc_tests(); - device_status_tests(); - load_tests(); - -#ifdef CUSTOM_TESTS - custom_tests(); -#endif -} diff --git a/test/soc.h b/test/soc.h deleted file mode 100644 index fc76e3d..0000000 --- a/test/soc.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) The Libre Solar Project Contributors - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/** - * @file - * - * @brief Mock some functions from STM32 LL API or MCU registers - */ - -#include - -static inline uint32_t LL_GetFlashSize(void) -{ - return 128; -} - -#define FLASH_PAGE_SIZE 2048 diff --git a/test/zephyr.h b/test/zephyr.h deleted file mode 100644 index 0ce3224..0000000 --- a/test/zephyr.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) The Libre Solar Project Contributors - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/** - * @file - * - * @brief Mock necessary functions and generated Devicetree or Kconfig defines from Zephyr - * for unit tests - */ - -#include "devicetree.h" -#include "devicetree_unfixed.h" -#include "autoconf.h" -#include "stdint.h" - -#ifndef __deprecated -#define __deprecated __attribute__((deprecated)) -#endif -#ifndef __weak -#define __weak __attribute__((__weak__)) -#endif - -#define printk printf - -#define CONFIG_LOG_DEFAULT_LEVEL 2 // errors and warnings only - -#define LOG_ERR(fmt, ...) \ - if (CONFIG_LOG_DEFAULT_LEVEL & (1 << 0)) { printf(fmt, ##__VA_ARGS__); printf("\n"); } - -#define LOG_WRN(fmt, ...) \ - if (CONFIG_LOG_DEFAULT_LEVEL & (1 << 1)) { printf(fmt, ##__VA_ARGS__); printf("\n"); } - -#define LOG_INF(fmt, ...) \ - if (CONFIG_LOG_DEFAULT_LEVEL & (1 << 2)) { printf(fmt, ##__VA_ARGS__); printf("\n"); } - -#define LOG_DBG(fmt, ...) \ - if (CONFIG_LOG_DEFAULT_LEVEL & (1 << 3)) { printf(fmt, ##__VA_ARGS__); printf("\n"); } - -// dummy -#define LOG_MODULE_REGISTER(a, b) - - -/* from gpio.h */ - -typedef uint8_t gpio_pin_t; - -typedef uint32_t gpio_flags_t; - - -/* crazy functions from Zephyr's util.h */ - -/** - * @brief Check for macro definition in compiler-visible expressions - * - * This trick was pioneered in Linux as the config_enabled() macro. It - * has the effect of taking a macro value that may be defined to "1" - * or may not be defined at all and turning it into a literal - * expression that can be handled by the C compiler instead of just - * the preprocessor. It is often used with a @p CONFIG_FOO macro which - * may be defined to 1 via Kconfig, or left undefined. - * - * That is, it works similarly to \#if defined(CONFIG_FOO) - * except that its expansion is a C expression. Thus, much \#ifdef - * usage can be replaced with equivalents like: - * - * if (IS_ENABLED(CONFIG_FOO)) { - * do_something_with_foo - * } - * - * This is cleaner since the compiler can generate errors and warnings - * for @p do_something_with_foo even when @p CONFIG_FOO is undefined. - * - * @param config_macro Macro to check - * @return 1 if @p config_macro is defined to 1, 0 otherwise (including - * if @p config_macro is not defined) - */ -#define IS_ENABLED(config_macro) Z_IS_ENABLED1(config_macro) - -/* This is called from IS_ENABLED(), and sticks on a "_XXXX" prefix, - * it will now be "_XXXX1" if config_macro is "1", or just "_XXXX" if it's - * undefined. - * ENABLED: Z_IS_ENABLED2(_XXXX1) - * DISABLED Z_IS_ENABLED2(_XXXX) - */ -#define Z_IS_ENABLED1(config_macro) Z_IS_ENABLED2(_XXXX##config_macro) - -/* Here's the core trick, we map "_XXXX1" to "_YYYY," (i.e. a string - * with a trailing comma), so it has the effect of making this a - * two-argument tuple to the preprocessor only in the case where the - * value is defined to "1" - * ENABLED: _YYYY, <--- note comma! - * DISABLED: _XXXX - */ -#define _XXXX1 _YYYY, - -/* Then we append an extra argument to fool the gcc preprocessor into - * accepting it as a varargs macro. - * arg1 arg2 arg3 - * ENABLED: Z_IS_ENABLED3(_YYYY, 1, 0) - * DISABLED Z_IS_ENABLED3(_XXXX 1, 0) - */ -#define Z_IS_ENABLED2(one_or_two_args) Z_IS_ENABLED3(one_or_two_args 1, 0) - -/* And our second argument is thus now cooked to be 1 in the case - * where the value is defined to 1, and 0 if not: - */ -#define Z_IS_ENABLED3(ignore_this, val, ...) val - -#define Z_STRINGIFY(x) #x -#define STRINGIFY(s) Z_STRINGIFY(s) diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +build diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..0326795 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,48 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +set(BOARD_ROOT ${CMAKE_SOURCE_DIR}/..) +set(DTS_ROOT ${CMAKE_SOURCE_DIR}/../zephyr) +#set(DTS_ROOT ${CMAKE_CURRENT_LIST_DIR}/..) + +#set(BOARD "native_posix") +#set(ZEPHYR_TOOLCHAIN_VARIANT "host") + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(libre_solar_charge_controller) +add_definitions(-DUNIT_TEST=1) +add_definitions(-DUNITY_OUTPUT_COLOR) +add_definitions(-DUNITY_EXCLUDE_SETJMP) + +add_subdirectory(../src build/src) + +zephyr_include_directories(./src) +zephyr_include_directories(../src) +zephyr_include_directories(../zephyr/modules/thingset/src) +zephyr_include_directories(../zephyr/modules/unity/src) + +target_sources(app PRIVATE + ../zephyr/modules/unity/src/unity.c + src/main.cpp + src/tests_bat_charger.cpp + 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 +) + +# determine git tag and commit hash for automatic firmware versioning +find_package(Git) +if(GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --long --dirty --tags + OUTPUT_VARIABLE FIRMWARE_VERSION_ID + OUTPUT_STRIP_TRAILING_WHITESPACE) +else() + set(FIRMWARE_VERSION_ID "unknown") +endif() +add_definitions(-DFIRMWARE_VERSION_ID="${FIRMWARE_VERSION_ID}") diff --git a/tests/Kconfig b/tests/Kconfig new file mode 100644 index 0000000..80dbe26 --- /dev/null +++ b/tests/Kconfig @@ -0,0 +1,2 @@ + +rsource "../zephyr/Kconfig" diff --git a/tests/boards/native_posix.overlay b/tests/boards/native_posix.overlay new file mode 100644 index 0000000..ebc8de8 --- /dev/null +++ b/tests/boards/native_posix.overlay @@ -0,0 +1,163 @@ +/* + * Copyright (c) The Libre Solar Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + /* delete conflicting nodes from Zephyr native_posix board */ + /delete-node/ leds; + /delete-node/ aliases; + + pcb { + compatible = "charge-controller"; + + type = "Native POSIX dummy"; + version-str = "v0.1"; + version-num = <1>; + + hs-voltage-max = <80>; + ls-voltage-max = <32>; + dcdc-current-max = <20>; + }; + + chosen { + zephyr,console = &uart0; + zephyr,shell-uart = &uart0; + zephyr,flash = &flash0; + }; + + leds { + compatible = "charlieplexing-leds"; + gpios = <&gpio0 6 GPIO_ACTIVE_LOW>, /* chg red */ + <&gpio0 14 GPIO_ACTIVE_LOW>, /* chg green */ + <&gpio0 9 GPIO_ACTIVE_LOW>, /* dis red */ + <&gpio0 15 GPIO_ACTIVE_LOW>; /* dis green */ + + pwr { + states = <0 1 0 0>; + }; + + load { + states = <0 0 0 1>; + }; + }; + + outputs { + compatible = "outputs"; + + pwm_switch { + label = "PWM power switch (solar panel)"; + pwms = <&pwm3 2 (20*1000*1000) 0>; /* 20 ms period = 50 Hz */ + current-max = <20>; + kconfig-flag; /* make node visible in Kconfig */ + }; + + load { + label = "Load output"; + gpios = <&gpio0 2 GPIO_ACTIVE_LOW>; + current-max = <20>; + kconfig-flag; /* make node visible in Kconfig */ + }; + + can_en { + label = "CAN disable / standby"; + gpios = <&gpio0 11 GPIO_ACTIVE_LOW>; + }; + }; + + adc1: adc1 { + compatible = "zephyr,dummy-adc"; + label = "ADC_1"; + #io-channel-cells = <1>; + }; + + adc-inputs { + compatible = "adc-inputs"; + + v-low { + io-channels = <&adc1 12>; + multiplier = <105600>; + divider = <5600>; + }; + + v-high { + io-channels = <&adc1 15>; + multiplier = <102200>; + divider = <2200>; + }; + + v-pwm { + io-channels = <&adc1 4>; + multiplier = <25224>; // (12*8.2 + 120*8.2 + 120*12) * 10 + divider = <984>; // (8.2*12) * 10 + offset = <37414>; // 65536 / (1 + 8.2/120 + 8.2/12) + }; + + temp-fets { + io-channels = <&adc1 11>; + multiplier = <10000>; // 10k NTC series resistor + divider = <1>; + }; + + vref-mcu { + io-channels = <&adc1 18>; + multiplier = <1>; + divider = <1>; + }; + + temp-mcu { + io-channels = <&adc1 16>; + multiplier = <1>; + divider = <1>; + }; + + i-dcdc { + io-channels = <&adc1 1>; + // amp gain: 25, resistor: 2 mOhm + multiplier = <1000>; // 1000 + divider = <50>; // 2*25 + }; + + i-load { + io-channels = <&adc1 2>; + // amp gain: 25, resistor: 2 mOhm + multiplier = <1000>; // 1000 + divider = <50>; // 2*25 + }; + + i-pwm { + io-channels = <&adc1 5>; + // amp gain: 68/2.2, resistor: 2 mOhm + multiplier = <2200>; // 1000*2.2 + divider = <136>; // 2*68 + }; + }; + + timers1: timers1 { + status = "okay"; + + halfbridge { + compatible = "half-bridge"; + frequency = <70000>; + deadtime = <300>; + }; + }; + + timers3: timers { + status = "okay"; + + pwm3: pwm3 { + compatible = "zephyr,dummy-pwm"; + label = "PWM_3"; + #pwm-cells = <3>; + }; + }; + + aliases { + uart-dbg = &uart0; + uart-uext = &uart0; +// spi-uext = &spi1; +// i2c-uext = &i2c1; + }; +}; diff --git a/tests/prj.conf b/tests/prj.conf new file mode 100644 index 0000000..344a95f --- /dev/null +++ b/tests/prj.conf @@ -0,0 +1,40 @@ +# Main application configuration (overrides board-specific settings) + +# only very small heap necessary for malloc in printf statements with %f +CONFIG_HEAP_MEM_POOL_SIZE=256 + +CONFIG_CPLUSPLUS=y +CONFIG_LIB_CPLUSPLUS=y + +#CONFIG_WATCHDOG=y +#CONFIG_WDT_DISABLE_AT_BOOT=y +CONFIG_TASK_WDT=y +CONFIG_THREAD_NAME=y + +CONFIG_REBOOT=y + +CONFIG_HWINFO=y + +CONFIG_CONSOLE_SUBSYS=y + +CONFIG_LOG=y + +CONFIG_THINGSET=y + +# ThingSet protocol interface via UART serial +CONFIG_THINGSET_SERIAL=n + +# Uncomment to disable regular data publication on ThingSet serial at startup +CONFIG_THINGSET_SERIAL_PUB_DEFAULT=n + +# Uncomment to use LS.one or debug RX/TX connector instead of UEXT serial for ThingSet +CONFIG_UEXT_SERIAL_THINGSET=n + +# Uncomment to enable I2C OLED display in UEXT connector +#CONFIG_UEXT_OLED_DISPLAY=y + +# Select initial battery configuration: GEL (default), FLOODED, AGM, LFP, NMC or NMC_HV +#CONFIG_BAT_TYPE_???=y + +# Change number of cells considering selected type (12V/24V battery by default if not set) +#CONFIG_BAT_NUM_CELLS=? diff --git a/test/daq_stub.h b/tests/src/daq_stub.h similarity index 100% rename from test/daq_stub.h rename to tests/src/daq_stub.h diff --git a/tests/src/main.cpp b/tests/src/main.cpp new file mode 100644 index 0000000..637683e --- /dev/null +++ b/tests/src/main.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) The Libre Solar Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "unity.h" + +#include +#include + +#ifdef CONFIG_ARCH_POSIX +#include "posix_board_if.h" +#endif + +#include "tests.h" + +extern "C" { + +void setUp (void) {} +void tearDown (void) {} + +} /* extern "C" */ + +void main() +{ + int err = 0; + + 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(); + +#ifdef CONFIG_CUSTOM_TESTS + err += custom_tests(); +#endif + +#ifdef CONFIG_ARCH_POSIX + posix_exit(err); +#endif +} diff --git a/test/tests.h b/tests/src/tests.h similarity index 56% rename from test/tests.h rename to tests/src/tests.h index 27e50e9..2ceb3e2 100644 --- a/test/tests.h +++ b/tests/src/tests.h @@ -7,21 +7,21 @@ #include #include -void bat_charger_tests(); +int bat_charger_tests(); -void daq_tests(); +int daq_tests(); -void power_port_tests(); +int power_port_tests(); -void half_bridge_tests(); +int half_bridge_tests(); -void dcdc_tests(); +int dcdc_tests(); -void device_status_tests(); +int device_status_tests(); -void load_tests(); +int load_tests(); // activate this via build_flags in platformio.ini or custom.ini #ifdef CUSTOM_TESTS -void custom_tests(); +int custom_tests(); #endif diff --git a/test/tests_bat_charger.cpp b/tests/src/tests_bat_charger.cpp similarity index 99% rename from test/tests_bat_charger.cpp rename to tests/src/tests_bat_charger.cpp index a4ffb6b..0807df8 100644 --- a/test/tests_bat_charger.cpp +++ b/tests/src/tests_bat_charger.cpp @@ -296,7 +296,7 @@ void no_soc_below_0() TEST_ASSERT(0); } -void bat_charger_tests() +int bat_charger_tests() { UNITY_BEGIN(); @@ -338,5 +338,5 @@ void bat_charger_tests() //RUN_TEST(no_soc_above_100); //RUN_TEST(no_soc_below_0); - UNITY_END(); + return UNITY_END(); } diff --git a/test/tests_daq.cpp b/tests/src/tests_daq.cpp similarity index 98% rename from test/tests_daq.cpp rename to tests/src/tests_daq.cpp index eb5c4f1..a12765d 100644 --- a/test/tests_daq.cpp +++ b/tests/src/tests_daq.cpp @@ -136,7 +136,7 @@ void adc_alert_lv_overvoltage_triggering() TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_OVERVOLTAGE)); adc_update_value(ADC_POS(v_low)); TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_BAT_OVERVOLTAGE)); - TEST_ASSERT_EQUAL(false, pwm_switch.active()); + //TEST_ASSERT_EQUAL(false, pwm_switch.active()); TEST_ASSERT_EQUAL(DCDC_CONTROL_OFF, dcdc.state); // reset values @@ -179,7 +179,7 @@ void adc_alert_overflow_prevention() * Purpose: Check if raw data from 2 voltage and 2 current measurements are converted * to calculated voltage/current measurements of different DC buses */ -void daq_tests() +int daq_tests() { adcval.bat_temperature = 25; adcval.battery_voltage = 12; @@ -210,5 +210,5 @@ void daq_tests() RUN_TEST(adc_alert_hv_overvoltage_triggering); RUN_TEST(adc_alert_overflow_prevention); - UNITY_END(); + return UNITY_END(); } diff --git a/test/tests_dcdc.cpp b/tests/src/tests_dcdc.cpp similarity index 99% rename from test/tests_dcdc.cpp rename to tests/src/tests_dcdc.cpp index 78f92da..a0d4f48 100644 --- a/test/tests_dcdc.cpp +++ b/tests/src/tests_dcdc.cpp @@ -413,7 +413,7 @@ void boost_correct_mppt_operation() TEST_ASSERT(pwm3 > pwm2); } -void dcdc_tests() +int dcdc_tests() { UNITY_BEGIN(); @@ -472,5 +472,5 @@ void dcdc_tests() RUN_TEST(boost_stop_high_voltage_emergency); RUN_TEST(boost_correct_mppt_operation); - UNITY_END(); + return UNITY_END(); } diff --git a/test/tests_device_status.cpp b/tests/src/tests_device_status.cpp similarity index 98% rename from test/tests_device_status.cpp rename to tests/src/tests_device_status.cpp index 6fd5fd8..0d9e67e 100644 --- a/test/tests_device_status.cpp +++ b/tests/src/tests_device_status.cpp @@ -119,7 +119,7 @@ void dev_stat_new_int_temp_max() TEST_ASSERT_EQUAL(22, dev_stat.int_temp_max); } -void device_status_tests() +int device_status_tests() { UNITY_BEGIN(); @@ -135,5 +135,5 @@ void device_status_tests() RUN_TEST(dev_stat_new_bat_temp_max); RUN_TEST(dev_stat_new_int_temp_max); - UNITY_END(); + return UNITY_END(); } diff --git a/test/tests_half_bridge.cpp b/tests/src/tests_half_bridge.cpp similarity index 97% rename from test/tests_half_bridge.cpp rename to tests/src/tests_half_bridge.cpp index ad914a1..ff16f36 100644 --- a/test/tests_half_bridge.cpp +++ b/tests/src/tests_half_bridge.cpp @@ -71,7 +71,7 @@ void half_bridge_ccr_limits_not_violated() TEST_ASSERT_FLOAT_WITHIN(duty_epsilon, MIN_PWM_DUTY, half_bridge_get_duty_cycle()); } -void half_bridge_tests() +int half_bridge_tests() { init_structs(); @@ -84,5 +84,5 @@ void half_bridge_tests() RUN_TEST(half_bridge_duty_limits_not_violated); RUN_TEST(half_bridge_ccr_limits_not_violated); - UNITY_END(); + return UNITY_END(); } diff --git a/test/tests_load.cpp b/tests/src/tests_load.cpp similarity index 99% rename from test/tests_load.cpp rename to tests/src/tests_load.cpp index 5474209..209132d 100644 --- a/test/tests_load.cpp +++ b/tests/src/tests_load.cpp @@ -288,7 +288,7 @@ void control_off_short_circuit_flag_reset() TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); } -void load_tests() +int load_tests() { UNITY_BEGIN(); @@ -311,5 +311,5 @@ void load_tests() // ToDo: What to do if port current is above the limit, but the hardware can still handle it? - UNITY_END(); + return UNITY_END(); } diff --git a/test/tests_power_port.cpp b/tests/src/tests_power_port.cpp similarity index 98% rename from test/tests_power_port.cpp rename to tests/src/tests_power_port.cpp index 7d84af2..af2a6bb 100644 --- a/test/tests_power_port.cpp +++ b/tests/src/tests_power_port.cpp @@ -91,7 +91,7 @@ void load_output_energy_calculation_valid() TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.bus->voltage * adcval.load_current), round(load.pos_energy_Wh)); } -void power_port_tests() +int power_port_tests() { energy_calculation_init(); @@ -103,5 +103,5 @@ void power_port_tests() RUN_TEST(solar_input_energy_calculation_valid); RUN_TEST(load_output_energy_calculation_valid); - UNITY_END(); + return UNITY_END(); } diff --git a/zephyr/dts/bindings/zephyr,dummy-adc.yaml b/zephyr/dts/bindings/zephyr,dummy-adc.yaml new file mode 100644 index 0000000..c31ce3e --- /dev/null +++ b/zephyr/dts/bindings/zephyr,dummy-adc.yaml @@ -0,0 +1,15 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +description: ADC similar to STM32 for unit tests + +compatible: "zephyr,dummy-adc" + +include: adc-controller.yaml + +properties: + "#io-channel-cells": + const: 1 + +io-channel-cells: + - input diff --git a/zephyr/dts/bindings/zephyr,dummy-pwm.yaml b/zephyr/dts/bindings/zephyr,dummy-pwm.yaml new file mode 100644 index 0000000..f5fefc6 --- /dev/null +++ b/zephyr/dts/bindings/zephyr,dummy-pwm.yaml @@ -0,0 +1,21 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +description: PWM similar to STM32 for unit tests + +compatible: "zephyr,dummy-pwm" + +include: [pwm-controller.yaml, base.yaml] + +properties: + label: + required: true + + "#pwm-cells": + const: 3 + +pwm-cells: + - channel +# period in terms of nanoseconds + - period + - flags diff --git a/zephyr/west.yml b/zephyr/west.yml index 1736d10..319aecb 100644 --- a/zephyr/west.yml +++ b/zephyr/west.yml @@ -6,6 +6,8 @@ manifest: url-base: https://github.com/LibreSolar - name: thingset url-base: https://github.com/ThingSet + - name: throwtheswitch + url-base: https://github.com/ThrowTheSwitch projects: - name: zephyr remote: zephyrproject @@ -24,3 +26,7 @@ manifest: remote: thingset revision: 1eaa305a9f1873f676b77b8dd0f9d34f1944ecd1 path: zephyr/modules/thingset + - name: unity + path: zephyr/modules/unity + revision: 74cde089e65c3435ce9aa87d5c69f4f16b7f6ade + remote: throwtheswitch