From 90666d697b2b432155516544b7e265f5dbdf24b7 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Fri, 3 May 2019 10:27:09 +0200 Subject: [PATCH] Refactoring of files and function names - Files for interfaces connected via UEXT are stored in separate uext/ directory - UEXT init and process function names independent of connected module - LED function names adjusted to start with leds_ - main.cpp structure simplified --- platformio.ini | 2 +- src/config.h_template | 3 + src/data_objects.cpp | 4 +- src/dcdc.cpp | 4 +- src/half_bridge_tim1.cpp | 4 +- src/half_bridge_tim3.cpp | 4 +- src/hardware.cpp | 48 ++-- src/hardware.h | 4 - src/interface.h | 99 -------- src/interface_gsm.cpp | 31 --- src/interface_lora.cpp | 31 --- src/leds.cpp | 175 +++++++------- src/leds.h | 43 +++- src/main.cpp | 213 +++++++----------- ...terface_serial.cpp => thingset_serial.cpp} | 45 +++- src/thingset_serial.h | 47 ++++ src/uext.h | 45 ++++ .../uext_oled.cpp} | 35 ++- src/uext/uext_template.cpp | 47 ++++ .../uext_wifi.cpp} | 9 +- 20 files changed, 440 insertions(+), 453 deletions(-) delete mode 100644 src/interface.h delete mode 100644 src/interface_gsm.cpp delete mode 100644 src/interface_lora.cpp rename src/{interface_serial.cpp => thingset_serial.cpp} (88%) create mode 100644 src/thingset_serial.h create mode 100644 src/uext.h rename src/{interface_oled.cpp => uext/uext_oled.cpp} (82%) create mode 100644 src/uext/uext_template.cpp rename src/{interface_wifi.cpp => uext/uext_wifi.cpp} (98%) diff --git a/platformio.ini b/platformio.ini index a49c829..2f50320 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,7 +12,7 @@ #env_default = libresolar_0_05 #env_default = libresolar_0_10 #env_default = cloudsolar_0_2 -env_default = cloudsolar_0_4 +#env_default = cloudsolar_0_4 #env_default = cloudsolar_0_6 #env_default = pwm_0_1 #env_default = pwm_0_2 diff --git a/src/config.h_template b/src/config.h_template index 546830b..d08d44c 100644 --- a/src/config.h_template +++ b/src/config.h_template @@ -36,6 +36,9 @@ // can be used to configure custom data objects in separate file instead (e.g. data_objects_custom.cpp) //#define CUSTOM_DATA_OBJECTS_FILE +// Empty UEXT port +#define UEXT_DUMMY_ENABLED + // OLED display at UEXT port //#define OLED_ENABLED diff --git a/src/data_objects.cpp b/src/data_objects.cpp index d5fee38..6ded15f 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -1,5 +1,5 @@ -/* LibreSolar Battery Management System 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. diff --git a/src/dcdc.cpp b/src/dcdc.cpp index fa58806..5ef43ca 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.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. diff --git a/src/half_bridge_tim1.cpp b/src/half_bridge_tim1.cpp index bb67e0c..19da900 100644 --- a/src/half_bridge_tim1.cpp +++ b/src/half_bridge_tim1.cpp @@ -1,5 +1,5 @@ -/* mbed library for half bridge driver PWM generation - * Copyright (c) 2016-2017 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. diff --git a/src/half_bridge_tim3.cpp b/src/half_bridge_tim3.cpp index dc05646..53f3b6d 100644 --- a/src/half_bridge_tim3.cpp +++ b/src/half_bridge_tim3.cpp @@ -1,5 +1,5 @@ -/* mbed library for half bridge driver PWM generation - * Copyright (c) 2016-2017 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. diff --git a/src/hardware.cpp b/src/hardware.cpp index 8a9717a..134ed11 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -1,3 +1,18 @@ +/* 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 "hardware.h" #include "pcb.h" @@ -5,8 +20,7 @@ #include "mbed.h" #include "half_bridge.h" #include "us_ticker_data.h" - -extern bool led_states[]; +#include "leds.h" #ifdef PIN_LOAD_EN DigitalOut load_enable(PIN_LOAD_EN); @@ -26,19 +40,6 @@ DigitalOut usb_pwr_dis(PIN_USB_PWR_DIS); DigitalOut uext_dis(PIN_UEXT_DIS); #endif -//---------------------------------------------------------------------------- -void init_load() -{ -#ifdef PIN_UEXT_DIS - uext_dis = 0; -#endif - -#ifdef PIN_USB_PWR_EN - usb_pwr_en = 1; -#endif -} - - //---------------------------------------------------------------------------- void hw_load_switch(bool enabled) { @@ -52,10 +53,7 @@ void hw_load_switch(bool enabled) else load_disable = 1; #endif -#ifdef LED_LOAD - if (enabled) led_states[LED_LOAD] = 1; - else led_states[LED_LOAD] = 0; -#endif + leds_set_load(enabled); } //---------------------------------------------------------------------------- @@ -201,19 +199,11 @@ void mbed_die(void) hw_load_switch(false); hw_usb_out(false); - for (int i = 0; i < NUM_LEDS; i++) { - led_states[i] = 0; - } - - int led_blink = 1; + leds_init(false); while (1) { - for (int led = 0; led < NUM_LEDS; led++) { - led_states[led] = (led % 2) ^ led_blink; - } - - led_blink = !led_blink; + leds_toggle_error(); wait_ms(100); // stay here to indicate something was wrong diff --git a/src/hardware.h b/src/hardware.h index 7f35626..02b4513 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -23,10 +23,6 @@ * Hardware-specific functions like timers, output switches, bootloader */ -/** Initialize load and USB output control pins - */ -void init_load(); - /** Enable/disable load switch */ void hw_load_switch(bool enabled); diff --git a/src/interface.h b/src/interface.h deleted file mode 100644 index 9b5a6ef..0000000 --- a/src/interface.h +++ /dev/null @@ -1,99 +0,0 @@ -/* 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 INTERFACE_H -#define INTERFACE_H - -/** @file - * - * @brief - * Communication interfaces connected to UEXT port - */ - -#include "mbed.h" -#include -#include "dcdc.h" -#include "power_port.h" -#include "battery.h" -#include "load.h" - -/** OLED display based on SSD1306 IC, connected to UEXT port via I2C - */ -void oled_output(dcdc_t *dcdc, power_port_t *solar_port, power_port_t *bat_port, battery_state_t *bat, load_output_t *load); - -/** UART serial interface (either in UEXT connector or from additional SWD serial) - */ -void uart_serial_init(Serial* s); -void uart_serial_process(); -void uart_serial_pub(); - -/** Serial interface via USB CDC device class (currently only supported with STM32F0) - */ -void usb_serial_init(); -void usb_serial_process(); -void usb_serial_pub(); - -/** CAN bus interface - */ -void can_send_data(); -void can_receive(); -void can_process_outbox(); -void can_process_inbox(); -void can_send_object_name(int obj_id, uint8_t can_dest_id); - -/** GSM mobile interface via UEXT connector serial interface - * - * Initialization (called once at program startup) - */ -void gsm_init(); - -/** GSM mobile interface via UEXT connector serial interface - * - * Process function, called every second - */ -void gsm_process(); - -/** LoRa interface via UEXT connector SPI interface - * - * Initialization (called once at program startup) - */ -void lora_init(); - -/** LoRa interface via UEXT connector SPI interface - * - * Process function, called every second - */ -void lora_process(); - -/** WiFi interface with ESP32 - * - * Initialization (called once at program startup) - */ -void wifi_init(); - -/** WiFi interface with ESP32 - * - * Process function, called every second - */ -void wifi_process(); - -/** SD card connected to UEXT connector (e.g. via Olimex adapter) - * - * The new mbed implementation of SD cards require RTOS --> currently not supported - */ -void output_sdcard(); - -#endif /* INTERFACE_H */ diff --git a/src/interface_gsm.cpp b/src/interface_gsm.cpp deleted file mode 100644 index 8e0d133..0000000 --- a/src/interface_gsm.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* 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 "config.h" - -#ifdef GSM_ENABLED // otherwise don't compile code to reduce firmware size - -void gsm_init(void) -{ - // TODO -} - -void gsm_process(void) -{ - // TODO -} - -#endif /* GSM_ENABLED */ diff --git a/src/interface_lora.cpp b/src/interface_lora.cpp deleted file mode 100644 index 15215ac..0000000 --- a/src/interface_lora.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* 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 "config.h" - -#ifdef LORA_ENABLED // otherwise don't compile code to reduce firmware size - -void lora_init(void) -{ - // TODO -} - -void lora_process(void) -{ - // TODO -} - -#endif /* LORA_ENABLED */ \ No newline at end of file diff --git a/src/leds.cpp b/src/leds.cpp index 27f41f6..e048b42 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -4,82 +4,17 @@ #include "mbed.h" -bool led_states[NUM_LEDS]; +static bool led_states[NUM_LEDS]; -int blink_state = 1; -bool flicker_state = 1; +static int blink_state = 1; +static bool flicker_state = 1; -bool charging = false; -bool rx_flag = false; -bool tx_flag = false; -int rxtx_trigger_timestamp; +static bool charging = false; +static bool rx_flag = false; +static bool tx_flag = false; +static int rxtx_trigger_timestamp; - -#if defined(STM32F0) - -void led_timer_start(int freq_Hz) // max. 10 kHz -{ - // Enable TIM17 clock - RCC->APB2ENR |= RCC_APB2ENR_TIM17EN; - - // Set timer clock to 10 kHz - TIM17->PSC = SystemCoreClock / 10000 - 1; - - // Interrupt on timer update - TIM17->DIER |= TIM_DIER_UIE; - - // Auto Reload Register sets interrupt frequency - TIM17->ARR = 10000 / freq_Hz - 1; - - // 3 = lowest priority of STM32L0/F0 - NVIC_SetPriority(TIM17_IRQn, 3); - NVIC_EnableIRQ(TIM17_IRQn); - - // Control Register 1 - // TIM_CR1_CEN = 1: Counter enable - TIM17->CR1 |= TIM_CR1_CEN; -} - -extern "C" void TIM17_IRQHandler(void) -{ - TIM17->SR &= ~TIM_SR_UIF; // clear update interrupt flag to restart timer - charlieplexing(); -} - -#elif defined(STM32L0) - -void led_timer_start(int freq_Hz) // max. 10 kHz -{ - // Enable TIM22 clock - RCC->APB2ENR |= RCC_APB2ENR_TIM22EN; - - // Set timer clock to 10 kHz - TIM22->PSC = SystemCoreClock / 10000 - 1; - - // Interrupt on timer update - TIM22->DIER |= TIM_DIER_UIE; - - // Auto Reload Register sets interrupt frequency - TIM22->ARR = 10000 / freq_Hz - 1; - - // 3 = lowest priority of STM32L0/F0 - NVIC_SetPriority(TIM22_IRQn, 3); - NVIC_EnableIRQ(TIM22_IRQn); - - // Control Register 1 - // TIM_CR1_CEN = 1: Counter enable - TIM22->CR1 |= TIM_CR1_CEN; -} - -extern "C" void TIM22_IRQHandler(void) -{ - TIM22->SR &= ~(1 << 0); - charlieplexing(); -} - -#endif - -void charlieplexing() +static void charlieplexing() { static int led_count = 0; static int flicker_count = 0; @@ -123,12 +58,76 @@ void charlieplexing() flicker_count++; } +#if defined(STM32F0) -void leds_init() +static void timer_start(int freq_Hz) // max. 10 kHz +{ + // Enable TIM17 clock + RCC->APB2ENR |= RCC_APB2ENR_TIM17EN; + + // Set timer clock to 10 kHz + TIM17->PSC = SystemCoreClock / 10000 - 1; + + // Interrupt on timer update + TIM17->DIER |= TIM_DIER_UIE; + + // Auto Reload Register sets interrupt frequency + TIM17->ARR = 10000 / freq_Hz - 1; + + // 3 = lowest priority of STM32L0/F0 + NVIC_SetPriority(TIM17_IRQn, 3); + NVIC_EnableIRQ(TIM17_IRQn); + + // Control Register 1 + // TIM_CR1_CEN = 1: Counter enable + TIM17->CR1 |= TIM_CR1_CEN; +} + +extern "C" void TIM17_IRQHandler(void) +{ + TIM17->SR &= ~TIM_SR_UIF; // clear update interrupt flag to restart timer + charlieplexing(); +} + +#elif defined(STM32L0) + +static void timer_start(int freq_Hz) // max. 10 kHz +{ + // Enable TIM22 clock + RCC->APB2ENR |= RCC_APB2ENR_TIM22EN; + + // Set timer clock to 10 kHz + TIM22->PSC = SystemCoreClock / 10000 - 1; + + // Interrupt on timer update + TIM22->DIER |= TIM_DIER_UIE; + + // Auto Reload Register sets interrupt frequency + TIM22->ARR = 10000 / freq_Hz - 1; + + // 3 = lowest priority of STM32L0/F0 + NVIC_SetPriority(TIM22_IRQn, 3); + NVIC_EnableIRQ(TIM22_IRQn); + + // Control Register 1 + // TIM_CR1_CEN = 1: Counter enable + TIM22->CR1 |= TIM_CR1_CEN; +} + +extern "C" void TIM22_IRQHandler(void) +{ + TIM22->SR &= ~(1 << 0); + charlieplexing(); +} + +#endif + +void leds_init(bool enabled) { for (int led = 0; led < NUM_LEDS; led++) { - led_states[led] = 1; + led_states[led] = enabled; } + timer_start(NUM_LEDS * 60); // 60 Hz } void leds_set_charging(bool enabled) @@ -139,7 +138,15 @@ void leds_set_charging(bool enabled) #endif } -void update_rxtx_led() +void leds_set_load(bool enabled) +{ +#ifdef LED_LOAD + if (enabled) led_states[LED_LOAD] = 1; + else led_states[LED_LOAD] = 0; +#endif +} + +void leds_update_rxtx() { #ifdef LED_RXTX if (time(NULL) >= rxtx_trigger_timestamp + 2) { @@ -158,27 +165,35 @@ void update_rxtx_led() #endif } -void trigger_rx_led() +void leds_trigger_rx() { rx_flag = true; tx_flag = false; rxtx_trigger_timestamp = time(NULL); } -void trigger_tx_led() +void leds_trigger_tx() { rx_flag = false; tx_flag = true; rxtx_trigger_timestamp = time(NULL); } -void toggle_led_blink() +void leds_toggle_blink() { blink_state = !blink_state; } +void leds_toggle_error() +{ + for (int led = 0; led < NUM_LEDS; led++) { + led_states[led] = (led % 2) ^ blink_state; + } + blink_state = !blink_state; +} + //---------------------------------------------------------------------------- -void update_soc_led(int soc) +void leds_update_soc(int soc) { #ifndef LED_DCDC // if there is no dedicated LED for the DC/DC converter status, blink SOC diff --git a/src/leds.h b/src/leds.h index e5e06ef..876a48f 100644 --- a/src/leds.h +++ b/src/leds.h @@ -23,15 +23,42 @@ * Control of status LEDs with charlieplexing */ -void leds_init(); -void led_timer_start(int freq_Hz); -void charlieplexing(); +/** Initialize LEDs and start timer for charlieplexing + */ +void leds_init(bool enabled = true); +/** Enables/disables dedicated charging LED if existing or + * blinks SOC LED when solar power is coming in. + */ void leds_set_charging(bool enabled); -void update_rxtx_led(); -void trigger_tx_led(); -void trigger_rx_led(); -void update_soc_led(int soc); -void toggle_led_blink(); + +/** Enables/disables dedicated load output LED (if existing) + */ +void leds_set_load(bool enabled); + +/** Updates SOC LED bar (if existing) + */ +void leds_update_soc(int soc); + +/** Sets RX/TX LED to TX state (= constant on) for 2 seconds + */ +void leds_trigger_tx(); + +/** Sets RX/TX LED to RX state (= flicker) for 2 seconds + */ +void leds_trigger_rx(); + +/** Must be called regularly and sets flicker or on state of RX/TX LED + */ +void leds_update_rxtx(); + +/** Toggles blinking LEDs and must be called every second + */ +void leds_toggle_blink(); + +/** Toggles between even and uneven LEDs switched on/off to create + * annoying flashing in case of an error. + */ +void leds_toggle_error(); #endif /* LEDS_H */ \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8c38b1e..e3c9300 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,9 +18,9 @@ #include "mbed.h" -#include "thingset.h" -#include "pcb.h" -#include "config.h" +#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. @@ -29,15 +29,13 @@ #include "battery.h" // battery settings #include "charger.h" // charger state machine #include "adc_dma.h" // ADC using DMA and conversion to measurement values -#include "interface.h" // communication interfaces, displays, etc. +#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" -#include "log.h" -#include "data_objects.h" - -//---------------------------------------------------------------------------- -// global variables +#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 "thingset_serial.h" // UART or USB serial communication Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial"); @@ -45,7 +43,7 @@ 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 for PWM charger +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 @@ -55,17 +53,11 @@ extern ThingSet ts; // defined in data_objects.cpp time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable) -//---------------------------------------------------------------------------- -// function prototypes - -void setup(); - -void interfaces_init(); -void interfaces_process_asap(); // called each time the main loop is in idle -void interfaces_process_1s(); // called every second - - -void system_control() // called by control timer (see hardware.cpp) +/** High priority function for DC/DC control and safety functions + * + * Called by control timer with 10 Hz frequency (see hardware.cpp). + */ +void system_control() { static int counter = 0; @@ -96,121 +88,13 @@ void system_control() // called by control timer (see hardware.cpp) counter++; } -//---------------------------------------------------------------------------- +/** Main function including initialization and continuous loop + */ int main() -{ - setup(); - wait(2); // safety feature: be able to re-flash before starting - - interfaces_init(); - init_watchdog(10); // 10s should be enough for communication ports - - switch(dcdc.mode) - { - case MODE_NANOGRID: - power_port_init_nanogrid(&hs_port); - power_port_init_bat(&ls_port, &bat_conf); - bat_port = &ls_port; - break; - case MODE_MPPT_BUCK: // typical MPPT charge controller operation - power_port_init_solar(&hs_port); - power_port_init_bat(&ls_port, &bat_conf); - bat_port = &ls_port; - break; - case MODE_MPPT_BOOST: // for charging of e-bike battery via solar panel - power_port_init_solar(&ls_port); - power_port_init_bat(&hs_port, &bat_conf); - bat_port = &hs_port; - break; - } - control_timer_start(CONTROL_FREQUENCY); - - time_t last_call = timestamp; - - // the main loop is suitable for slow tasks like communication (even blocking wait allowed) - while(1) { - - interfaces_process_asap(); - - update_soc_led(bat_state.soc); - - time_t now = timestamp; - if (now >= last_call + 1 || now < last_call) { // called once per second (or slower if blocking wait occured somewhere) - - //printf("Still alive... time: %d, mode: %d\n", (int)time(NULL), dcdc.mode); - - charger_state_machine(bat_port, &bat_conf, &bat_state, bat_port->voltage, bat_port->current); - - load_state_machine(&load, ls_port.input_allowed); - - eeprom_update(); - interfaces_process_1s(); - toggle_led_blink(); - - last_call = now; - } - feed_the_dog(); - sleep(); // wake-up by timer interrupts - } -} - -void interfaces_init() -{ - uart_serial_init(&serial); - //usb_serial_init(); - -#ifdef GSM_ENABLED - gsm_init(); -#endif - -#ifdef LORA_ENABLED - lora_init(); -#endif - -#ifdef WIFI_ENABLED - wifi_init(); -#endif -} - -void interfaces_process_asap() -{ - uart_serial_process(); - usb_serial_process(); - update_rxtx_led(); -} - -void interfaces_process_1s() -{ -#ifdef OLED_ENABLED - oled_output(&dcdc, &hs_port, &ls_port, &bat_state, &load); -#endif - -#ifdef GSM_ENABLED - gsm_process(); -#endif - -#ifdef LORA_ENABLED - lora_process(); -#endif - -#ifdef WIFI_ENABLED - wifi_process(); -#endif - - // will only actually publish the messages if enabled via ThingSet - uart_serial_pub(); - usb_serial_pub(); - - fflush(stdout); -} - -//---------------------------------------------------------------------------- -void setup() { serial.baud(115200); leds_init(); - led_timer_start(NUM_LEDS * 60); // 60 Hz battery_conf_init(&bat_conf, BATTERY_TYPE, BATTERY_NUM_CELLS, BATTERY_CAPACITY); battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values @@ -225,18 +109,77 @@ void setup() half_bridge_init(PWM_FREQUENCY, 300, 12 / dcdc.hs_voltage_max, 0.97); // lower duty limit might have to be adjusted dynamically depending on LS voltage #endif + // Configuration from EEPROM data_objects_read_eeprom(); ts.set_conf_callback(data_objects_update_conf); // after each configuration change, data should be written back to EEPROM + // ADC, DMA and sensor calibration adc_setup(); dma_setup(); - adc_timer_start(1000); // 1 kHz - wait(0.5); // wait for ADC to collect some measurement values - update_measurements(&dcdc, &bat_state, &load, &hs_port, &ls_port); calibrate_current_sensors(&dcdc, &load); + + // Communication interfaces + uart_serial_init(&serial); + uext_init(); + + init_watchdog(10); // 10s should be enough for communication ports + + // Setup of DC/DC power stage + switch(dcdc.mode) { + case MODE_NANOGRID: + power_port_init_nanogrid(&hs_port); + power_port_init_bat(&ls_port, &bat_conf); + bat_port = &ls_port; + break; + case MODE_MPPT_BUCK: // typical MPPT charge controller operation + power_port_init_solar(&hs_port); + power_port_init_bat(&ls_port, &bat_conf); + bat_port = &ls_port; + break; + case MODE_MPPT_BOOST: // for charging of e-bike battery via solar panel + power_port_init_solar(&ls_port); + power_port_init_bat(&hs_port, &bat_conf); + bat_port = &hs_port; + break; + } + + wait(2); // safety feature: be able to re-flash before starting + control_timer_start(CONTROL_FREQUENCY); + + // the main loop is suitable for slow tasks like communication (even blocking wait allowed) + time_t last_call = timestamp; + while (1) { + + thingset_serial_process_asap(); + uext_process_asap(); + + leds_update_rxtx(); + + time_t now = timestamp; + if (now >= last_call + 1 || now < last_call) { // called once per second (or slower if blocking wait occured somewhere) + + //printf("Still alive... time: %d, mode: %d\n", (int)time(NULL), dcdc.mode); + + charger_state_machine(bat_port, &bat_conf, &bat_state, bat_port->voltage, bat_port->current); + + load_state_machine(&load, ls_port.input_allowed); + + eeprom_update(); + + leds_update_soc(bat_state.soc); + leds_toggle_blink(); + + uext_process_1s(); + thingset_serial_process_1s(); + + last_call = now; + } + feed_the_dog(); + sleep(); // wake-up by timer interrupts + } } #endif diff --git a/src/interface_serial.cpp b/src/thingset_serial.cpp similarity index 88% rename from src/interface_serial.cpp rename to src/thingset_serial.cpp index bccc49f..8871640 100644 --- a/src/interface_serial.cpp +++ b/src/thingset_serial.cpp @@ -3,7 +3,7 @@ #include "thingset.h" #include "config.h" -#include "interface.h" +#include "thingset_serial.h" #ifdef USB_SERIAL_ENABLED #include "USBSerial.h" @@ -27,6 +27,39 @@ bool pub_uart_enabled = false; // start with sending data disabled bool usb_serial_command_flag = false; bool pub_usb_enabled = true; + +void thingset_serial_init(Serial* s) +{ +#ifdef UART_SERIAL_ENABLED + uart_serial_init(s); +#endif +#ifdef USB_SERIAL_ENABLED + usb_serial_init(); +#endif +} + +void thingset_serial_process_asap() +{ +#ifdef UART_SERIAL_ENABLED + uart_serial_process(); +#endif +#ifdef USB_SERIAL_ENABLED + usb_serial_process(); +#endif +} + +void thingset_serial_process_1s() +{ +#ifdef UART_SERIAL_ENABLED + uart_serial_pub(); +#endif +#ifdef USB_SERIAL_ENABLED + usb_serial_pub(); +#endif + + fflush(stdout); +} + #ifdef UART_SERIAL_ENABLED void uart_serial_isr() @@ -85,11 +118,6 @@ void uart_serial_process() } } -#else -// dummy functions -void uart_serial_init(Serial* s) {;} -void uart_serial_pub() {;} -void uart_serial_process() {;} #endif /* UART_SERIAL_ENABLED */ @@ -147,9 +175,4 @@ void usb_serial_pub() { } } -#else -// dummy functions -void usb_serial_init() {;} -void usb_serial_pub() {;} -void usb_serial_process() {;} #endif /* USB_SERIAL_ENABLED */ diff --git a/src/thingset_serial.h b/src/thingset_serial.h new file mode 100644 index 0000000..bd3073c --- /dev/null +++ b/src/thingset_serial.h @@ -0,0 +1,47 @@ +/* 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 THINGSET_SERIAL_H +#define THINGSET_SERIAL_H + +/** @file + * + * @brief + * ThingSet protocol based communication via UART or USB serial port + */ + +#include "mbed.h" + +/** UART serial interface (either in UEXT connector or from additional SWD serial) + */ +void thingset_serial_init(Serial* s); +void thingset_serial_process_asap(); +void thingset_serial_process_1s(); + + +/** UART serial interface (either in UEXT connector or from additional SWD serial) + */ +void uart_serial_init(Serial* s); +void uart_serial_process(); +void uart_serial_pub(); + +/** Serial interface via USB CDC device class (currently only supported with STM32F0) + */ +void usb_serial_init(); +void usb_serial_process(); +void usb_serial_pub(); + +#endif /* THINGSET_SERIAL_H */ diff --git a/src/uext.h b/src/uext.h new file mode 100644 index 0000000..a95d608 --- /dev/null +++ b/src/uext.h @@ -0,0 +1,45 @@ +/* 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 UEXT_H +#define UEXT_H + +/** @file + * + * @brief + * Communication interfaces connected to UEXT port + */ + +/* UEXT interface initialization + * + * This function is called only once at startup. + */ +void uext_init(); + +/** UEXT interface process function + * + * This function is called in each main loop, as soon as all other tasks finished. + */ +void uext_process_asap(); + +/** UEXT interface process function + * + * This function is called every second, if no other task was blocking for a longer time. + * It should be used for state machines, etc. + */ +void uext_process_1s(); + +#endif /* UEXT_H */ diff --git a/src/interface_oled.cpp b/src/uext/uext_oled.cpp similarity index 82% rename from src/interface_oled.cpp rename to src/uext/uext_oled.cpp index 9b54f2e..5e90c5a 100644 --- a/src/interface_oled.cpp +++ b/src/uext/uext_oled.cpp @@ -18,13 +18,22 @@ #ifdef OLED_ENABLED // otherwise don't compile code to reduce firmware size -#include "interface.h" +#include "uext.h" #include "pcb.h" #include "half_bridge.h" - +#include "dcdc.h" +#include "power_port.h" +#include "battery.h" +#include "load.h" #include "log.h" + extern log_data_t log_data; +extern dcdc_t dcdc; +extern power_port_t hs_port; +extern power_port_t ls_port; +extern battery_state_t bat_state; +extern load_output_t load; #include "Adafruit_SSD1306.h" @@ -47,11 +56,13 @@ const unsigned char bmp_disconnected [] = { 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - I2C i2c(PIN_UEXT_SDA, PIN_UEXT_SCL); Adafruit_SSD1306_I2c oled(i2c, PIN_UEXT_SSEL, 0x78, 64, 128); -void oled_output(dcdc_t *dcdc, power_port_t *solar_port, power_port_t *bat_port, battery_state_t *bat, load_output_t *load) +void uext_init() {;} // no need for initialization +void uext_process_asap() {;} + +void uext_process_1s() { float tmp; @@ -67,7 +78,7 @@ void oled_output(dcdc_t *dcdc, power_port_t *solar_port, power_port_t *bat_port oled.drawBitmap(34, 3, bmp_arrow_right, 5, 7, 1); } - if (load->enabled) { + if (load.enabled) { oled.drawBitmap(84, 3, bmp_arrow_right, 5, 7, 1); } else { @@ -84,7 +95,7 @@ void oled_output(dcdc_t *dcdc, power_port_t *solar_port, power_port_t *bat_port // solar panel data if (half_bridge_enabled()) { - tmp = -solar_port->voltage * solar_port->current; + tmp = -hs_port.voltage * hs_port.current; oled.setTextCursor(0, 18); oled.printf("%4.0fW", (abs(tmp) < 1) ? 0 : tmp); // remove negative zeros } @@ -94,29 +105,29 @@ void oled_output(dcdc_t *dcdc, power_port_t *solar_port, power_port_t *bat_port } //if (solar_port->voltage > bat_port->voltage) { oled.setTextCursor(0, 26); - oled.printf("%4.1fV", solar_port->voltage); + oled.printf("%4.1fV", hs_port.voltage); //} // battery data - tmp = bat_port->voltage * bat_port->current; + tmp = ls_port.voltage * ls_port.current; oled.setTextCursor(42, 18); oled.printf("%5.1fW", (abs(tmp) < 0.1) ? 0 : tmp); // remove negative zeros oled.setTextCursor(42, 26); - oled.printf("%5.1fV", bat_port->voltage); + oled.printf("%5.1fV", ls_port.voltage); // load data - tmp = bat_port->voltage * load->current; + tmp = ls_port.voltage * load.current; oled.setTextCursor(90, 18); oled.printf("%5.1fW", (abs(tmp) < 0.1) ? 0 : tmp); // remove negative zeros oled.setTextCursor(90, 26); - oled.printf("%5.1fA\n", (abs(load->current) < 0.1) ? 0 : load->current); + oled.printf("%5.1fA\n", (abs(load.current) < 0.1) ? 0 : load.current); oled.setTextCursor(0, 36); oled.printf("Day +%5.0fWh -%5.0fWh", log_data.solar_in_day_Wh, fabs(log_data.load_out_day_Wh)); oled.printf("Tot +%4.1fkWh -%4.1fkWh", log_data.solar_in_total_Wh / 1000.0, fabs(log_data.load_out_total_Wh) / 1000.0); oled.setTextCursor(0, 56); - oled.printf("T %.0fC PWM %.0f%% SOC %d%%", dcdc->temp_mosfets, half_bridge_get_duty_cycle() * 100.0, bat->soc); + oled.printf("T %.0fC PWM %.0f%% SOC %d%%", dcdc.temp_mosfets, half_bridge_get_duty_cycle() * 100.0, bat_state.soc); oled.display(); } diff --git a/src/uext/uext_template.cpp b/src/uext/uext_template.cpp new file mode 100644 index 0000000..c9cf028 --- /dev/null +++ b/src/uext/uext_template.cpp @@ -0,0 +1,47 @@ +/* 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 "config.h" + +/** UEXT interface description template + * + * The UEXT port can be used to connect several different extension modules to the charge controller. + * + * Only one interface can be used at a time. The interfaces are selected via #define in config.h + * + * Use this file as a template for custom interfaces. If the UEXT port is not used, UEXT_DUMMY_ENABLED + * has to be defined in config.h to implement the functions. + */ + +// Only compile if this UEXT interface is enabled in config.h +#ifdef UEXT_DUMMY_ENABLED + +void uext_init(void) +{ + // add you init functions here +} + +void uext_process_asap(void) +{ + // add functions to be called as soon as possible during each loop in main function +} + +void uext_process_1s(void) +{ + // add functions to be called every second here +} + +#endif /* UEXT_DUMMY_ENABLED */ \ No newline at end of file diff --git a/src/interface_wifi.cpp b/src/uext/uext_wifi.cpp similarity index 98% rename from src/interface_wifi.cpp rename to src/uext/uext_wifi.cpp index 6da8802..0ed4ecf 100644 --- a/src/interface_wifi.cpp +++ b/src/uext/uext_wifi.cpp @@ -1,4 +1,4 @@ -#include "interface.h" +#include "uext.h" #include "config.h" #ifdef WIFI_ENABLED @@ -128,8 +128,6 @@ int wifi_send_emoncms_data() return 0; } -void wifi_init(void) {;} // everything is handled in state machine in wifi_process() - int wifi_reset(void) { int res = 0; @@ -157,7 +155,10 @@ int wifi_reset(void) return res; } -void wifi_process() +void uext_init(void) {;} // everything is handled in state machine in wifi_process() +void uext_process_asap(void) {;} + +void uext_process_1s() { static wifi_states state = STATE_WIFI_RESET; static int error_counter = 0;