diff --git a/.travis.yml b/.travis.yml index 5ae0f4f..547bf20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,7 +30,8 @@ addons: script: - cp src/config.h_template src/config.h # - platformio ci --lib="lib/Adafruit_GFX" --lib="lib/ThingSet" --project-conf=platformio.ini -e libresolar_0_10,libresolar_0_05 - - platformio run -e libresolar_0_10 -e libresolar_0_05 -e cloudsolar_0_2 -e cloudsolar_0_4 + - platformio run -e libresolar_0_10 -e libresolar_0_05 -e cloudsolar_0_2 -e cloudsolar_0_4 -e pwm_0_2 + - platformio test -e unit_test_native - doxygen Doxyfile deploy: diff --git a/platformio.ini b/platformio.ini index 2f50320..e546458 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,6 +16,7 @@ #env_default = cloudsolar_0_6 #env_default = pwm_0_1 #env_default = pwm_0_2 +#env_default = unit_test_native [common] @@ -127,3 +128,12 @@ build_flags = ${common.build_flags} -D PCB_PWM_02=1 lib_deps = ${common.lib_deps} lib_ignore = ${common.lib_ignore} + +[env:unit_test_native] +platform = native +build_flags = + -D LITTLE_ENDIAN + -D UNIT_TEST +# include src directory (otherwise unit-tests will only include lib directory) +test_build_project_src = true +lib_ignore = USB, mbed-USBDevice, mbed-mbedtls, USBSerial, ESP32, Adafruit_GFX \ No newline at end of file diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index 90d0a94..40097d3 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#ifndef UNIT_TEST + #include "mbed.h" #include "config.h" @@ -463,3 +465,5 @@ extern "C" void TIM6_IRQHandler(void) #endif #endif /* TESTING_PIL */ + +#endif /* UNIT_TEST */ diff --git a/src/adc_dma_pil.cpp b/src/adc_dma_pil.cpp index 49507d7..7de56b6 100644 --- a/src/adc_dma_pil.cpp +++ b/src/adc_dma_pil.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#ifndef UNIT_TEST + #include "mbed.h" #include "config.h" @@ -124,3 +126,5 @@ void adc_setup() {;} void adc_timer_start(int freq_Hz) {;} #endif /* TESTING_PIL */ + +#endif /* UNIT_TEST */ diff --git a/src/charger.cpp b/src/charger.cpp index 405998b..56b981a 100644 --- a/src/charger.cpp +++ b/src/charger.cpp @@ -19,6 +19,7 @@ #include "pcb.h" #include "half_bridge.h" #include "log.h" +#include // private function void _enter_state(battery_state_t* bat_state, int next_state) diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 6ded15f..2b9d7d1 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include "mbed.h" #include "pcb.h" #include "config.h" @@ -29,6 +28,7 @@ #include "hardware.h" #include "eeprom.h" #include "pwm_switch.h" +#include #ifdef PIL_TESTING #include "pil_test.h" @@ -209,7 +209,9 @@ const data_object_t data_objects[] = { //{0xD4, TS_CAL, TS_ACCESS_READ | TS_ACCESS_WRITE_AUTH, TS_T_FLOAT32, 1, (void*) &(dcdc.offset_voltage_stop), "SolarOffsetStop_V"} // FUNCTION CALLS (EXEC) ////////////////////////////////////////////////// +#ifndef UNIT_TEST {0xE0, TS_EXEC, TS_ACCESS_EXEC, TS_T_BOOL, 0, (void*) &NVIC_SystemReset, "Reset"}, +#endif {0xE1, TS_EXEC, TS_ACCESS_EXEC, TS_T_BOOL, 0, (void*) &start_dfu_bootloader, "Bootloader"}, {0xE2, TS_EXEC, TS_ACCESS_EXEC, TS_T_BOOL, 0, (void*) &eeprom_store_data, "SaveSettings"}, }; @@ -286,4 +288,4 @@ void data_objects_read_eeprom() } } -#endif /* CUSTOM_DATA_OBJECTS_FILE */ \ No newline at end of file +#endif /* CUSTOM_DATA_OBJECTS_FILE */ diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 5ef43ca..807ecd4 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -23,6 +23,7 @@ #include // for time(NULL) function #include // for fabs function +#include extern log_data_t log_data; diff --git a/src/eeprom.cpp b/src/eeprom.cpp index ea1718f..4cec96d 100644 --- a/src/eeprom.cpp +++ b/src/eeprom.cpp @@ -16,9 +16,9 @@ #include "pcb.h" #include "thingset.h" -#include "mbed.h" #include "eeprom.h" #include +#include // versioning of EEPROM layout (2 bytes) // change the version number each time the data object array below is changed! @@ -43,6 +43,8 @@ const uint16_t eeprom_data_objects[] = { 0xA6 // day count }; +#ifndef UNIT_TEST + uint32_t _calc_crc(uint8_t *buf, size_t len) { RCC->AHBENR |= RCC_AHBENR_CRCEN; @@ -66,6 +68,8 @@ uint32_t _calc_crc(uint8_t *buf, size_t len) return CRC->DR; } +#endif // UNIT_TEST + #if defined(PIN_EEPROM_SDA) && defined(PIN_EEPROM_SCL) #ifdef PCB_LS_010 @@ -284,4 +288,4 @@ void eeprom_update() if (time(NULL) % EEPROM_UPDATE_INTERVAL == 0 && time(NULL) > 0) { eeprom_store_data(); } -} \ No newline at end of file +} diff --git a/src/eeprom.h b/src/eeprom.h index c54209b..3434499 100644 --- a/src/eeprom.h +++ b/src/eeprom.h @@ -22,8 +22,6 @@ * @brief Handling of internal or external EEPROM to store device configuration */ -#include "mbed.h" - /** Write data to EEPROM address * * @returns 0 for success diff --git a/src/half_bridge_tim1.cpp b/src/half_bridge_tim1.cpp index 19da900..cce2fa0 100644 --- a/src/half_bridge_tim1.cpp +++ b/src/half_bridge_tim1.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#ifndef UNIT_TEST + #include "half_bridge.h" #include "pcb.h" #include "config.h" @@ -169,3 +171,5 @@ bool half_bridge_enabled() } #endif /* PWM_TIM */ + +#endif /* UNIT_TEST */ diff --git a/src/half_bridge_tim3.cpp b/src/half_bridge_tim3.cpp index 53f3b6d..52d0bc5 100644 --- a/src/half_bridge_tim3.cpp +++ b/src/half_bridge_tim3.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#ifndef UNIT_TEST /* Generates PWM signal on PB0 (high-side) and PB1 (low-side) using timer TIM3 */ @@ -172,3 +173,5 @@ bool half_bridge_enabled() } #endif /* PWM_TIM */ + +#endif /* UNIT_TEST */ diff --git a/src/hardware.cpp b/src/hardware.cpp index 134ed11..5b1879f 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#ifndef UNIT_TEST + #include "hardware.h" #include "pcb.h" @@ -280,3 +282,16 @@ extern "C" void system_init_hook() while (42); } } + +#else + +// dummy functions for unit tests +void hw_load_switch(bool enabled) {;} +void hw_usb_out(bool enabled) {;} +void init_watchdog(float timeout) {;} +void feed_the_dog() {;} +void control_timer_start(int freq_Hz) {;} +void system_control() {;} +void start_dfu_bootloader() {;} + +#endif /* UNIT_TEST */ diff --git a/src/interface_can.cpp b/src/interface_can.cpp index 3f689fe..bf62a0b 100644 --- a/src/interface_can.cpp +++ b/src/interface_can.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#ifndef UNIT_TEST + #include "config.h" #ifdef CAN_ENABLED @@ -276,4 +278,6 @@ void can_receive() { } } -#endif /* CAN_ENABLED */ \ No newline at end of file +#endif /* CAN_ENABLED */ + +#endif /* UNIT_TEST */ diff --git a/src/leds.cpp b/src/leds.cpp index e048b42..43e3960 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -1,3 +1,20 @@ +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UNIT_TEST #include "leds.h" #include "pcb.h" @@ -241,3 +258,5 @@ void leds_update_soc(int soc) } #endif } + +#endif /* UNIT_TEST */ diff --git a/src/load.cpp b/src/load.cpp index b44b44e..a9919bf 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -1,5 +1,5 @@ -/* LibreSolar MPPT charge controller firmware - * Copyright (c) 2016-2018 Martin Jäger (www.libre.solar) +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ #include "pcb.h" #include "hardware.h" #include "log.h" +#include void load_init(load_output_t *load) { @@ -161,4 +162,3 @@ void load_control(load_output_t *load) } debounce_counter = 0; } - diff --git a/src/pcb.h b/src/pcb.h index 2e4bfd7..8ac6ccc 100644 --- a/src/pcb.h +++ b/src/pcb.h @@ -77,6 +77,8 @@ #include "pcbs/pcb_pwm_01.h" #elif defined(PCB_PWM_02) #include "pcbs/pcb_pwm_02.h" +#elif defined(UNIT_TEST) + #include "pcbs/pcb_stub.h" #else #error "PCB has to be specified!" #endif diff --git a/src/pcbs/pcb_stub.h b/src/pcbs/pcb_stub.h new file mode 100644 index 0000000..8e3e504 --- /dev/null +++ b/src/pcbs/pcb_stub.h @@ -0,0 +1,98 @@ +/* LibreSolar MPPT charge controller firmware + * Copyright (c) 2016-2018 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PCB_PWM_02_H_ +#define __PCB_PWM_02_H_ + +#define CHARGER_TYPE_PWM 1 // PWM charge controller instead of MPPT + +#define PWM_TIM 3 // use TIM3 timer + +#define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current +#define LOAD_CURRENT_MAX 20 // PCB maximum load switch current + +#define PIN_UEXT_TX 0 +#define PIN_UEXT_RX 0 +#define PIN_UEXT_SCL 0 +#define PIN_UEXT_SDA 0 +#define PIN_UEXT_MISO 0 +#define PIN_UEXT_MOSI 0 +#define PIN_UEXT_SCK 0 +#define PIN_UEXT_SSEL 0 + +#define PIN_SWD_TX 0 +#define PIN_SWD_RX 0 + +#define PIN_LOAD_DIS 0 +#define PIN_USB_PWR_DIS 0 + +#define PIN_REF_I_DCDC 0 + +#define PIN_TEMP_INT_PD 0 + +enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; + +// assignment LED numbers on PCB to their meaning +#define NUM_LEDS 5 + +#define LED_SOC_1 0 // LED1 +#define LED_SOC_2 1 // LED2 +#define LED_SOC_3 2 // LED3 +#define LED_LOAD 3 // LED4 +#define LED_RXTX 4 // LED5, used to indicate when sending data + +// LED pins and pin state configuration to switch above LEDs on +#define NUM_LED_PINS 3 +static const int led_pins[NUM_LED_PINS] = { + // A B C + 0, 0, 0 +}; +static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { + { PIN_HIGH, PIN_LOW, PIN_FLOAT }, // LED1 + { PIN_LOW, PIN_HIGH, PIN_FLOAT }, // LED2 + { PIN_HIGH, PIN_FLOAT, PIN_LOW }, // LED3 + { PIN_FLOAT, PIN_HIGH, PIN_LOW }, // LED4 + { PIN_FLOAT, PIN_LOW, PIN_HIGH } // LED5 +}; + +// pin definition only needed in adc_dma.cpp to detect if they are present on the PCB +#define PIN_ADC_TEMP_BAT + +// typical value for Semitec 103AT-5 thermistor: 3435 +#define NTC_BETA_VALUE 3435 +#define NTC_SERIES_RESISTOR 8200.0 + +#define ADC_GAIN_V_BAT (132 / 12) +#define ADC_GAIN_V_SOLAR (1 + 120/12 + 120/8.2) +#define ADC_GAIN_I_LOAD (1000 / 2 / (68/2.2)) // op amp gain: 68/2.2, resistor: 2 mOhm +#define ADC_GAIN_I_SOLAR (1000 / 2 / (68/2.2)) + +#define ADC_OFFSET_V_SOLAR (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset + +// position in the array written by the DMA controller +enum { + ADC_POS_V_BAT, // ADC 0 (PA_0) + ADC_POS_V_SOLAR, // ADC 1 (PA_1) + ADC_POS_I_LOAD, // ADC 5 (PA_5) + ADC_POS_I_SOLAR, // ADC 6 (PA_6) + ADC_POS_TEMP_BAT, // ADC 7 (PA_7) + ADC_POS_VREF_MCU, // ADC 17 + ADC_POS_TEMP_MCU, // ADC 18 + NUM_ADC_CH // trick to get the number of elements +}; + + +#endif diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 25b57c2..d1fe03b 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -1,5 +1,5 @@ -/* LibreSolar MPPT charge controller firmware - * Copyright (c) 2016-2018 Martin Jäger (www.libre.solar) +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,6 +14,8 @@ * limitations under the License. */ +#ifndef UNIT_TEST + #include "pwm_switch.h" #include "config.h" #include "pcb.h" @@ -179,4 +181,6 @@ void pwm_switch_init(pwm_switch_t *pwm_switch) {} void pwm_switch_control(pwm_switch_t *pwm_switch, power_port_t *solar_port, power_port_t *bat_port) {} void pwm_switch_duty_cycle_step(int delta) {} -#endif \ No newline at end of file +#endif + +#endif /* UNIT_TEST */ diff --git a/src/thingset_serial.cpp b/src/thingset_serial.cpp index 8871640..63ae3a6 100644 --- a/src/thingset_serial.cpp +++ b/src/thingset_serial.cpp @@ -1,3 +1,20 @@ +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UNIT_TEST #include "mbed.h" #include "thingset.h" @@ -176,3 +193,5 @@ void usb_serial_pub() { } #endif /* USB_SERIAL_ENABLED */ + +#endif /* UNIT_TEST */ diff --git a/src/uext/uext_oled.cpp b/src/uext/uext_oled.cpp index 5e90c5a..93bdc5b 100644 --- a/src/uext/uext_oled.cpp +++ b/src/uext/uext_oled.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#ifndef UNIT_TEST + #include "config.h" #ifdef OLED_ENABLED // otherwise don't compile code to reduce firmware size @@ -132,4 +134,6 @@ void uext_process_1s() oled.display(); } -#endif /* OLED_ENABLED */ \ No newline at end of file +#endif /* OLED_ENABLED */ + +#endif /* UNIT_TEST */ diff --git a/src/uext/uext_template.cpp b/src/uext/uext_template.cpp index c9cf028..1ef8bf5 100644 --- a/src/uext/uext_template.cpp +++ b/src/uext/uext_template.cpp @@ -44,4 +44,4 @@ void uext_process_1s(void) // add functions to be called every second here } -#endif /* UEXT_DUMMY_ENABLED */ \ No newline at end of file +#endif /* UEXT_DUMMY_ENABLED */ diff --git a/src/uext/uext_wifi.cpp b/src/uext/uext_wifi.cpp index 0ed4ecf..46eba05 100644 --- a/src/uext/uext_wifi.cpp +++ b/src/uext/uext_wifi.cpp @@ -1,3 +1,6 @@ + +#ifndef UNIT_TEST + #include "uext.h" #include "config.h" @@ -251,3 +254,5 @@ void uext_process_1s() } #endif /* WIFI_ENABLED */ + +#endif /* UNIT_TEST */ diff --git a/test/charger_tests.cpp b/test/charger_tests.cpp new file mode 100644 index 0000000..ac86cc6 --- /dev/null +++ b/test/charger_tests.cpp @@ -0,0 +1,149 @@ + +#include "tests.h" + +#include "battery.h" +#include "power_port.h" +#include "charger.h" + +#include +#include + +extern battery_conf_t bat_conf; +extern battery_state_t bat_state; +extern power_port_t ls_port; + +static void init_structs() +{ + battery_conf_init(&bat_conf, BAT_TYPE_FLOODED, 6, 100); + battery_state_init(&bat_state); + power_port_init_bat(&ls_port, &bat_conf); + bat_state.chg_state = CHG_STATE_IDLE; +} + +void no_start_at_high_voltage() +{ + init_structs(); + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge + 0.1, 0); + TEST_ASSERT_EQUAL(CHG_STATE_IDLE, bat_state.chg_state); +} + +void no_start_after_short_rest() +{ + init_structs(); + bat_state.time_state_changed = time(NULL) - bat_conf.time_limit_recharge + 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0); + TEST_ASSERT_EQUAL(CHG_STATE_IDLE, bat_state.chg_state); +} + +void no_start_outside_temperature_limits() +{ + init_structs(); + bat_state.temperature = bat_conf.charge_temp_max + 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0); + TEST_ASSERT_EQUAL(CHG_STATE_IDLE, bat_state.chg_state); + + bat_state.temperature = bat_conf.charge_temp_min - 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0); + TEST_ASSERT_EQUAL(CHG_STATE_IDLE, bat_state.chg_state); +} + +void start_if_everything_just_fine() +{ + init_structs(); + bat_state.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0); + TEST_ASSERT_EQUAL(CHG_STATE_BULK, bat_state.chg_state); +} + +void enter_topping_at_voltage_setpoint() +{ + init_structs(); + bat_state.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0); + TEST_ASSERT_EQUAL(CHG_STATE_BULK, bat_state.chg_state); + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, 0); + TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, bat_state.chg_state); +} + +void stop_topping_after_time_limit() +{ + enter_topping_at_voltage_setpoint(); + + bat_state.time_state_changed = time(NULL) - bat_conf.time_limit_topping + 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping + 0.1); + TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, bat_state.chg_state); + + bat_state.time_state_changed = time(NULL) - bat_conf.time_limit_topping - 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping + 0.1); + TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, bat_state.chg_state); +} + +void stop_topping_at_cutoff_current() +{ + enter_topping_at_voltage_setpoint(); + + bat_state.time_state_changed = time(NULL) - 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping - 0.1); + TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, bat_state.chg_state); +} + +void trickle_to_idle_for_li_ion() +{ + enter_topping_at_voltage_setpoint(); + battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100); + + bat_state.time_state_changed = time(NULL) - 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping - 0.1); + TEST_ASSERT_EQUAL(CHG_STATE_IDLE, bat_state.chg_state); +} + +void trickle_to_equalization_if_enabled() +{ + enter_topping_at_voltage_setpoint(); + bat_conf.equalization_enabled = true; + + bat_state.time_state_changed = time(NULL) - 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping - 0.1); + TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, bat_state.chg_state); +} + +void no_trickle_if_low_current_because_of_low_input() +{ + enter_topping_at_voltage_setpoint(); + + bat_state.time_state_changed = time(NULL) - 200; + bat_state.time_voltage_limit_reached = time(NULL) - 3; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping - 0.1, bat_conf.current_cutoff_topping - 0.1); + TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, bat_state.chg_state); + + bat_state.time_voltage_limit_reached = time(NULL) - 1; + charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping - 0.1, bat_conf.current_cutoff_topping - 0.1); + TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, bat_state.chg_state); +} + +void restart_bulk_from_trickle_if_voltage_drops() +{ + TEST_ASSERT(0); +} + +void charger_tests() +{ + UNITY_BEGIN(); + + RUN_TEST(no_start_at_high_voltage); + RUN_TEST(no_start_after_short_rest); + RUN_TEST(no_start_outside_temperature_limits); + RUN_TEST(start_if_everything_just_fine); + RUN_TEST(enter_topping_at_voltage_setpoint); + RUN_TEST(stop_topping_after_time_limit); + RUN_TEST(stop_topping_at_cutoff_current); + RUN_TEST(trickle_to_idle_for_li_ion); + //RUN_TEST(trickle_to_equalization_if_enabled); + RUN_TEST(no_trickle_if_low_current_because_of_low_input); + //RUN_TEST(restart_bulk_from_trickle_if_voltage_drops); + + // TODO: temperature compensation + // TODO: current compensation + + UNITY_END(); +} \ No newline at end of file diff --git a/test/half_bridge_stub.cpp b/test/half_bridge_stub.cpp new file mode 100644 index 0000000..ad4c186 --- /dev/null +++ b/test/half_bridge_stub.cpp @@ -0,0 +1,105 @@ +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "half_bridge.h" +#include "pcb.h" +#include "config.h" + + +static int _pwm_resolution; +static float _min_duty; +static float _max_duty; + +static bool _enabled; + +void _init_registers(int freq_kHz, int deadtime_ns) +{ +} + +void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_duty) +{ + _init_registers(freq_kHz, deadtime_ns); + + _min_duty = min_duty; + _max_duty = max_duty; + half_bridge_set_duty_cycle(_max_duty); // init with allowed value + + _enabled = false; +} + +void half_bridge_set_duty_cycle(float duty) +{ + float duty_target; + // protection against wrong settings which could destroy the hardware + if (duty < _min_duty) { + duty_target = _min_duty; + } + else if (duty > _max_duty) { + duty_target = _max_duty; + } + else { + duty_target = duty; + } + + //TIM1->CCR1 = _pwm_resolution / 2 * duty_target; +} + +void half_bridge_duty_cycle_step(int delta) +{ + float duty_target; + //duty_target = (float)(TIM1->CCR1 + delta) / (_pwm_resolution / 2); + + // protection against wrong settings which could destroy the hardware + if (duty_target < _min_duty) { + half_bridge_set_duty_cycle(_min_duty); + } + else if (duty_target > _max_duty) { + half_bridge_set_duty_cycle(_max_duty); + } + else { + //TIM1->CCR1 = TIM1->CCR1 + delta; + } +} + +float half_bridge_get_duty_cycle() +{ + return 0;//(float)(TIM1->CCR1) / (_pwm_resolution / 2);; +} + +void half_bridge_start(float pwm_duty) +{ + half_bridge_set_duty_cycle(pwm_duty); + + // Break and Dead-Time Register + // MOE = 1: Main output enable + //TIM1->BDTR |= TIM_BDTR_MOE; + + _enabled = true; +} + +void half_bridge_stop() +{ + // Break and Dead-Time Register + // MOE = 1: Main output enable + //TIM1->BDTR &= ~(TIM_BDTR_MOE); + + _enabled = false; +} + +bool half_bridge_enabled() +{ + return _enabled; +} diff --git a/test/main.cpp b/test/main.cpp new file mode 100644 index 0000000..1d1af09 --- /dev/null +++ b/test/main.cpp @@ -0,0 +1,41 @@ +#include "thingset.h" // handles access to internal data via communication interfaces +#include "pcb.h" // hardware-specific settings +#include "config.h" // user-specific configuration + +#include "half_bridge.h" // PWM generation for DC/DC converter +#include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc. +#include "dcdc.h" // DC/DC converter control (hardware independent) +#include "pwm_switch.h" // PWM charge controller +#include "battery.h" // battery settings +#include "charger.h" // charger state machine +#include "adc_dma.h" // ADC using DMA and conversion to measurement values +#include "uext.h" // communication interfaces, displays, etc. in UEXT connector +#include "eeprom.h" // external I2C EEPROM +#include "load.h" // load and USB output management +#include "leds.h" // LED switching using charlieplexing +#include "log.h" // log data (error memory, min/max measurements, etc.) +#include "data_objects.h" // for access to internal data via ThingSet + +#include "tests.h" + +dcdc_t dcdc = {}; +power_port_t hs_port = {}; // high-side (solar for typical MPPT) +power_port_t ls_port = {}; // low-side (battery for typical MPPT) +power_port_t *bat_port = NULL; +pwm_switch_t pwm_switch = {}; // only necessary for PWM charger +battery_conf_t bat_conf; // actual (used) battery configuration +battery_conf_t bat_conf_user; // temporary storage where the user can write to +battery_state_t bat_state; // battery state information +load_output_t load; +log_data_t log_data; +extern ThingSet ts; // defined in data_objects.cpp + +time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable) + +int main() { + charger_tests(); + + // TODO + //battery_tests(); + //load_tests(); +} \ No newline at end of file diff --git a/test/tests.h b/test/tests.h new file mode 100644 index 0000000..9688576 --- /dev/null +++ b/test/tests.h @@ -0,0 +1,6 @@ +#include +#include + +void charger_tests(); + +void battery_tests();