diff --git a/README.md b/README.md index 99fe200..4d44680 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ See the Libre Solar website for a detailed instruction how to [develop software] ## API documentation -The documentation auto-generated by Doxygen can be found [here](https://libre.solar/MPPT-Charger_Software/). +The documentation auto-generated by Doxygen can be found [here](https://libre.solar/charge-controller-firmware/). ## Additional firmware documentation (docs folder) diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index bb64d5c..90d0a94 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -15,6 +15,11 @@ */ #include "mbed.h" +#include "config.h" + +// don't use this file during processor-in-the-loop tests +#ifndef PIL_TESTING + #include "adc_dma.h" #include "pcb.h" // contains defines for pins #include // log for thermistor calculation @@ -456,3 +461,5 @@ extern "C" void TIM6_IRQHandler(void) } #endif + +#endif /* TESTING_PIL */ diff --git a/src/adc_dma_pil.cpp b/src/adc_dma_pil.cpp new file mode 100644 index 0000000..49507d7 --- /dev/null +++ b/src/adc_dma_pil.cpp @@ -0,0 +1,126 @@ +/* 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. + */ + +#include "mbed.h" +#include "config.h" + +#ifdef PIL_TESTING // processor-in-the-loop test + +#include "adc_dma.h" +#include "pcb.h" // contains defines for pins +#include // log for thermistor calculation +#include "log.h" +#include "pwm_switch.h" + +#include "pil_test.h" + +extern Serial serial; +extern log_data_t log_data; +extern float mcu_temp; + +extern pil_test_data_t sim_data; +bool sim_data_initialized = false; + +void _init_sim_data() +{ + sim_data.solar_voltage = 12.0; + sim_data.battery_voltage = 12.6; + sim_data.dcdc_current = 0; + sim_data.load_current = 0; + sim_data.mcu_temperature = 25; + sim_data.bat_temperature = 25; + sim_data.internal_temperature = 25; + sim_data_initialized = true; +} + +//---------------------------------------------------------------------------- +void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load, power_port_t *hs, power_port_t *ls) +{ + if (sim_data_initialized == false) + _init_sim_data(); + + ls->voltage = sim_data.battery_voltage; + load->voltage = ls->voltage; + + hs->voltage = sim_data.solar_voltage; + + load->current = sim_data.load_current; + + dcdc->ls_current = sim_data.dcdc_current; + ls->current = dcdc->ls_current - load->current; + hs->current = -dcdc->ls_current * ls->voltage / hs->voltage; + + bat->temperature = sim_data.bat_temperature; + dcdc->temp_mosfets = sim_data.internal_temperature; + mcu_temp = sim_data.mcu_temperature; + + if (ls->voltage > log_data.battery_voltage_max) { + log_data.battery_voltage_max = ls->voltage; + } + + if (hs->voltage > log_data.solar_voltage_max) { + log_data.solar_voltage_max = hs->voltage; + } + + if (ls->current > log_data.dcdc_current_max) { + log_data.dcdc_current_max = ls->current; + } + + if (load->current > log_data.load_current_max) { + log_data.load_current_max = load->current; + } + + if (ls->current > 0) { + uint16_t solar_power = ls->voltage * ls->current; + if (solar_power > log_data.solar_power_max_day) { + log_data.solar_power_max_day = solar_power; + if (log_data.solar_power_max_day > log_data.solar_power_max_total) { + log_data.solar_power_max_total = log_data.solar_power_max_day; + } + } + } + + if (load->current > 0) { + uint16_t load_power = ls->voltage * load->current; + if (load_power > log_data.load_power_max_day) { + log_data.load_power_max_day = load_power; + if (log_data.load_power_max_day > log_data.load_power_max_total) { + log_data.load_power_max_total = log_data.load_power_max_day; + } + } + } + + if (dcdc->temp_mosfets > log_data.mosfet_temp_max) { + log_data.mosfet_temp_max = dcdc->temp_mosfets; + } + + if (bat->temperature > log_data.bat_temp_max) { + log_data.bat_temp_max = bat->temperature; + } + + if (mcu_temp > log_data.int_temp_max) { + log_data.int_temp_max = mcu_temp; + } +} + +// dummy functions +void calibrate_current_sensors(dcdc_t *dcdc, load_output_t *load) {;} +void detect_battery_temperature(battery_state_t *bat, float bat_temp) {;} +void dma_setup() {;} +void adc_setup() {;} +void adc_timer_start(int freq_Hz) {;} + +#endif /* TESTING_PIL */ diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 9e19c8f..aae6edb 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -18,6 +18,9 @@ #include "pcb.h" #include "config.h" +// can be used to configure custom data objects in separate file instead (e.g. data_objects_custom.cpp) +#ifndef CUSTOM_DATA_OBJECTS_FILE + #include "thingset.h" #include "battery.h" #include "log.h" @@ -27,6 +30,11 @@ #include "eeprom.h" #include "pwm_switch.h" +#ifdef PIL_TESTING +#include "pil_test.h" +pil_test_data_t sim_data; +#endif + extern log_data_t log_data; extern battery_state_t bat_state; extern battery_conf_t bat_conf; @@ -119,6 +127,15 @@ const data_object_t data_objects[] = { {0x62, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_BOOL, 0, (void*) &(dcdc.enabled), "DCDCEn"}, #endif +#ifdef PIL_TESTING // only used during processor-in-the-loop test + {0x63, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(sim_data.solar_voltage), "SimSolar_V"}, + {0x64, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(sim_data.battery_voltage), "SimBat_V"}, + {0x65, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(sim_data.dcdc_current), "SimDCDC_A"}, + {0x66, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(sim_data.load_current), "SimLoad_A"}, + {0x67, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 1, (void*) &(sim_data.bat_temperature), "SimBat_degC"}, + {0x68, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 1, (void*) &(sim_data.mcu_temperature), "SimMCU_degC"}, +#endif + // OUTPUT DATA //////////////////////////////////////////////////////////// // using IDs >= 0x70 except for high priority data objects @@ -268,3 +285,5 @@ void data_objects_read_eeprom() battery_conf_overwrite(&bat_conf, &bat_conf_user); } } + +#endif /* CUSTOM_DATA_OBJECTS_FILE */ \ No newline at end of file diff --git a/src/eeprom.cpp b/src/eeprom.cpp index 438a1d7..d61cf53 100644 --- a/src/eeprom.cpp +++ b/src/eeprom.cpp @@ -185,7 +185,7 @@ int eeprom_read (unsigned int addr, uint8_t* ret, int len) void eeprom_restore_data() { - uint8_t buf_req[200] = {0}; // ThingSet request buffer, initialize with zeros + uint8_t buf_req[300]; // ThingSet request buffer // EEPROM header uint8_t buf_header[EEPROM_HEADER_SIZE]; @@ -216,11 +216,14 @@ void eeprom_restore_data() printf("EEPROM: CRC of data not correct, expected 0x%x (data_len = %d)\n", (unsigned int)crc, len); } } + else { + printf("EEPROM: Buffer too small or data layout version changed\n"); + } } void eeprom_store_data() { - uint8_t buf[300] = {0}; // initialize with zeros + uint8_t buf[300]; int len = ts.pub_msg_cbor(buf + EEPROM_HEADER_SIZE, sizeof(buf) - EEPROM_HEADER_SIZE, eeprom_data_objects, sizeof(eeprom_data_objects)/sizeof(uint16_t)); uint32_t crc = _calc_crc(buf + EEPROM_HEADER_SIZE, len); diff --git a/src/half_bridge_tim1.cpp b/src/half_bridge_tim1.cpp index 9caa00f..bb67e0c 100644 --- a/src/half_bridge_tim1.cpp +++ b/src/half_bridge_tim1.cpp @@ -14,10 +14,9 @@ * limitations under the License. */ -#ifndef UNIT_TEST - #include "half_bridge.h" #include "pcb.h" +#include "config.h" #if defined(STM32F0) && (PWM_TIM == 1) @@ -146,9 +145,11 @@ void half_bridge_start(float pwm_duty) { half_bridge_set_duty_cycle(pwm_duty); +#ifndef PIL_TESTING // Break and Dead-Time Register // MOE = 1: Main output enable TIM1->BDTR |= TIM_BDTR_MOE; +#endif _enabled = true; } @@ -168,5 +169,3 @@ 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 2f9bb9c..dc05646 100644 --- a/src/half_bridge_tim3.cpp +++ b/src/half_bridge_tim3.cpp @@ -18,10 +18,9 @@ /* Generates PWM signal on PB0 (high-side) and PB1 (low-side) using timer TIM3 */ -#ifndef UNIT_TEST - #include "half_bridge.h" #include "pcb.h" +#include "config.h" // timer used for PWM generation has to be globally selected for the used PCB #if (PWM_TIM == 3) @@ -111,7 +110,7 @@ void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_d _enabled = false; } -void half_bridge_set_duty_cycle(float duty) +void half_bridge_set_duty_cycle(float duty) { float duty_target; @@ -146,6 +145,7 @@ void half_bridge_start(float pwm_duty) { half_bridge_set_duty_cycle(pwm_duty); +#ifndef PIL_TESTING // Capture/Compare Enable Register // CCxE = 1: Enable the output on OCx // CCxP = 0: Active high polarity on OCx (default) @@ -153,6 +153,7 @@ void half_bridge_start(float pwm_duty) // CCxNP = 0: Active high polarity on OC1N (default) TIM3->CCER |= TIM_CCER_CC3E; TIM3->CCER |= TIM_CCER_CC4E; +#endif _enabled = true; } @@ -171,5 +172,3 @@ bool half_bridge_enabled() } #endif /* PWM_TIM */ - -#endif /* UNIT_TEST */ diff --git a/src/pil_test.h b/src/pil_test.h new file mode 100644 index 0000000..1b62e8b --- /dev/null +++ b/src/pil_test.h @@ -0,0 +1,30 @@ +/* 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 "mbed.h" +#include "config.h" + +/** Data for Processor-In-the-Loop test + */ +typedef struct { + float solar_voltage; + float battery_voltage; + float dcdc_current; + float load_current; + float bat_temperature; + float mcu_temperature; + float internal_temperature; +} pil_test_data_t; \ No newline at end of file