Starting test-driven development (TDD) with Unity

- Using PlatformIO's embedded unit testing with native environment
- mbed-dependent .cpp files escaped with #ifndef UNIT_TEST
- First tests for charger module added under test/ directory
This commit is contained in:
Martin Jaeger
2019-05-04 11:31:37 +02:00
parent 90666d697b
commit d9f7103e97
26 changed files with 519 additions and 16 deletions
+4
View File
@@ -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 */
+4
View File
@@ -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 */
+1
View File
@@ -19,6 +19,7 @@
#include "pcb.h"
#include "half_bridge.h"
#include "log.h"
#include <time.h>
// private function
void _enter_state(battery_state_t* bat_state, int next_state)
+4 -2
View File
@@ -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 <stdio.h>
#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 */
#endif /* CUSTOM_DATA_OBJECTS_FILE */
+1
View File
@@ -23,6 +23,7 @@
#include <time.h> // for time(NULL) function
#include <math.h> // for fabs function
#include <stdio.h>
extern log_data_t log_data;
+6 -2
View File
@@ -16,9 +16,9 @@
#include "pcb.h"
#include "thingset.h"
#include "mbed.h"
#include "eeprom.h"
#include <inttypes.h>
#include <time.h>
// 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();
}
}
}
-2
View File
@@ -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
+4
View File
@@ -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 */
+3
View File
@@ -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 */
+15
View File
@@ -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 */
+5 -1
View File
@@ -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 */
#endif /* CAN_ENABLED */
#endif /* UNIT_TEST */
+19
View File
@@ -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 */
+3 -3
View File
@@ -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 <time.h>
void load_init(load_output_t *load)
{
@@ -161,4 +162,3 @@ void load_control(load_output_t *load)
}
debounce_counter = 0;
}
+2
View File
@@ -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
+98
View File
@@ -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
+7 -3
View File
@@ -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
#endif
#endif /* UNIT_TEST */
+19
View File
@@ -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 */
+5 -1
View File
@@ -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 */
#endif /* OLED_ENABLED */
#endif /* UNIT_TEST */
+1 -1
View File
@@ -44,4 +44,4 @@ void uext_process_1s(void)
// add functions to be called every second here
}
#endif /* UEXT_DUMMY_ENABLED */
#endif /* UEXT_DUMMY_ENABLED */
+5
View File
@@ -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 */