diff --git a/.gitignore b/.gitignore index f05dfe2..5e9eb31 100644 --- a/.gitignore +++ b/.gitignore @@ -3,13 +3,13 @@ .gcc-flags.json .vscode/browse.vc.db* .vscode -.vscode/c_cpp_properties.json -.vscode/launch.json -.vscode/*.db -.vscode/.browse.c_cpp.db* src/config.cpp src/config.h src/version.h html memory.map custom.ini +zephyr/build +tools +*.bak +*_bak diff --git a/boards/arm/mppt1210hus_0v4/CMakeLists.txt b/boards/arm/mppt1210hus_0v4/CMakeLists.txt new file mode 100755 index 0000000..d1b8108 --- /dev/null +++ b/boards/arm/mppt1210hus_0v4/CMakeLists.txt @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: Apache-2.0 + +if(CONFIG_PINMUX) +zephyr_library() +zephyr_library_sources(pinmux.c) +zephyr_library_include_directories(${ZEPHYR_BASE}/drivers) +endif() diff --git a/boards/arm/mppt1210hus_0v4/Kconfig.board b/boards/arm/mppt1210hus_0v4/Kconfig.board new file mode 100755 index 0000000..f36a58d --- /dev/null +++ b/boards/arm/mppt1210hus_0v4/Kconfig.board @@ -0,0 +1,10 @@ +# Kconfig - STM32 Nucleo-64 development board with STM32L073RZ MCU configuration +# +# Copyright (c) 2018 Ilya Tagunov +# +# SPDX-License-Identifier: Apache-2.0 +# + +config BOARD_NUCLEO_L073RZ + bool "NUCLEO-64 L073RZ Development Board" + depends on SOC_STM32L073XX diff --git a/boards/arm/mppt1210hus_0v4/Kconfig.defconfig b/boards/arm/mppt1210hus_0v4/Kconfig.defconfig new file mode 100755 index 0000000..6713277 --- /dev/null +++ b/boards/arm/mppt1210hus_0v4/Kconfig.defconfig @@ -0,0 +1,37 @@ +# Kconfig - STM32 Nucleo-64 development board with STM32L073RZ MCU +# +# Copyright (c) 2018 Ilya Tagunov +# +# SPDX-License-Identifier: Apache-2.0 +# + +if BOARD_NUCLEO_L073RZ + +config BOARD + default "nucleo_l073rz" + +if UART_CONSOLE + +config UART_2 + default y + +endif # UART_CONSOLE + +if I2C + +config I2C_1 + default y + +endif # I2C + +if SPI + +config SPI_1 + default y + +config SPI_STM32_INTERRUPT + default y + +endif # SPI + +endif # BOARD_NUCLEO_L073RZ diff --git a/boards/arm/mppt1210hus_0v4/board.cmake b/boards/arm/mppt1210hus_0v4/board.cmake new file mode 100755 index 0000000..6f20255 --- /dev/null +++ b/boards/arm/mppt1210hus_0v4/board.cmake @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: Apache-2.0 + +board_runner_args(jlink "--device=STM32L073RZ" "--speed=4000") + +include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake) +include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake) diff --git a/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts new file mode 100755 index 0000000..d01e45e --- /dev/null +++ b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2018 Ilya Tagunov + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/dts-v1/; +#include + +/ { + model = "Libre Solar MPPT 1210 HUS v0.4"; + compatible = "st,stm32l073rz-nucleo", "st,stm32l073"; + + chosen { + zephyr,console = &usart1; + zephyr,shell-uart = &usart1; + zephyr,sram = &sram0; + zephyr,flash = &flash0; + }; + + leds { + compatible = "gpio-leds"; + green_led_2: led_2 { + gpios = <&gpioa 5 GPIO_INT_ACTIVE_HIGH>; + label = "User LD2"; + }; + }; + + aliases { + led0 = &green_led_2; + }; +}; + +&usart1 { + current-speed = <115200>; + status = "okay"; +}; + +&usart2 { + current-speed = <115200>; + status = "okay"; +}; + +arduino_i2c: &i2c1 { + status = "okay"; + clock-frequency = ; +}; + +arduino_spi: &spi1 { + status = "okay"; +}; + +&iwdg { + status = "okay"; +}; + +&adc1 { + status = "okay"; +}; diff --git a/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.yaml b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.yaml new file mode 100755 index 0000000..a94e903 --- /dev/null +++ b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.yaml @@ -0,0 +1,17 @@ +identifier: mppt1210hus_0v4 +name: MPPT-1210-HUS-0V4 +type: mcu +arch: arm +toolchain: + - zephyr + - gnuarmemb + - xtools +ram: 20 +flash: 192 +supported: + - arduino_i2c + - gpio + - i2c + - spi + - watchdog + - adc diff --git a/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4_defconfig b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4_defconfig new file mode 100755 index 0000000..c35340f --- /dev/null +++ b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4_defconfig @@ -0,0 +1,44 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Zephyr Kernel Configuration +CONFIG_ARM=y +CONFIG_SOC_SERIES_STM32L0X=y + +# Platform Configuration +CONFIG_SOC_STM32L073XX=y +CONFIG_BOARD_NUCLEO_L073RZ=y + +# General Kernel Options +CONFIG_CORTEX_M_SYSTICK=y +CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=32000000 + +# Serial Drivers +CONFIG_SERIAL=y +CONFIG_UART_INTERRUPT_DRIVEN=y +# enable console +CONFIG_CONSOLE=y +CONFIG_UART_CONSOLE=y + +# Pinmux Driver +CONFIG_PINMUX=y + +# GPIO Controller +CONFIG_GPIO=y + +# Clock configuration +CONFIG_CLOCK_CONTROL=y +# SYSCLK selection +CONFIG_CLOCK_STM32_SYSCLK_SRC_PLL=y +# HSE configuration +CONFIG_CLOCK_STM32_HSE_CLOCK=8000000 +# however, the board does not have an external oscillator, so just use +# the 8MHz clock signal coming from integrated STLink +CONFIG_CLOCK_STM32_HSE_BYPASS=y +# PLL configuration +CONFIG_CLOCK_STM32_PLL_SRC_HSE=y +# produce 32MHz clock at PLL output +CONFIG_CLOCK_STM32_PLL_MULTIPLIER=8 +CONFIG_CLOCK_STM32_PLL_DIVISOR=2 +CONFIG_CLOCK_STM32_AHB_PRESCALER=1 +CONFIG_CLOCK_STM32_APB1_PRESCALER=1 +CONFIG_CLOCK_STM32_APB2_PRESCALER=1 diff --git a/boards/arm/mppt1210hus_0v4/pinmux.c b/boards/arm/mppt1210hus_0v4/pinmux.c new file mode 100755 index 0000000..99972dc --- /dev/null +++ b/boards/arm/mppt1210hus_0v4/pinmux.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018 Ilya Tagunov + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +#include + +/* pin assignments for NUCLEO-L073RZ board */ +static const struct pin_config pinconf[] = { +#ifdef CONFIG_UART_1 + {STM32_PIN_PA9, STM32L0_PINMUX_FUNC_PA9_USART1_TX}, + {STM32_PIN_PA10, STM32L0_PINMUX_FUNC_PA10_USART1_RX}, +#endif /* CONFIG_UART_1 */ +#ifdef CONFIG_UART_2 + {STM32_PIN_PA2, STM32L0_PINMUX_FUNC_PA2_USART2_TX}, + {STM32_PIN_PA3, STM32L0_PINMUX_FUNC_PA3_USART2_RX}, +#endif /* CONFIG_UART_2 */ +#ifdef CONFIG_I2C_1 + {STM32_PIN_PB8, STM32L0_PINMUX_FUNC_PB8_I2C1_SCL}, + {STM32_PIN_PB9, STM32L0_PINMUX_FUNC_PB9_I2C1_SDA}, +#endif /* CONFIG_I2C_1 */ +#ifdef CONFIG_SPI_1 + {STM32_PIN_PA5, STM32L0_PINMUX_FUNC_PA5_SPI1_SCK}, + {STM32_PIN_PA6, STM32L0_PINMUX_FUNC_PA6_SPI1_MISO}, + {STM32_PIN_PA7, STM32L0_PINMUX_FUNC_PA7_SPI1_MOSI}, +#endif /* CONFIG_SPI_1 */ +#ifdef CONFIG_ADC_1 + {STM32_PIN_PA0, STM32L0_PINMUX_FUNC_PA0_ADC_IN0}, +#endif /* CONFIG_ADC_1 */ +}; + +static int pinmux_stm32_init(struct device *port) +{ + ARG_UNUSED(port); + + stm32_setup_pins(pinconf, ARRAY_SIZE(pinconf)); + + return 0; +} + +SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, + CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY); diff --git a/boards/arm/mppt1210hus_0v4/support/openocd.cfg b/boards/arm/mppt1210hus_0v4/support/openocd.cfg new file mode 100755 index 0000000..00f68a6 --- /dev/null +++ b/boards/arm/mppt1210hus_0v4/support/openocd.cfg @@ -0,0 +1,27 @@ +# This is an ST NUCLEO-L073RZ board with single STM32L073RZ chip. +# http://www.st.com/en/evaluation-tools/nucleo-l073rz.html +source [find interface/stlink.cfg] + +transport select hla_swd + +set WORKAREASIZE 0x2000 + +source [find target/stm32l0.cfg] + +# Add the second flash bank. +set _FLASHNAME $_CHIPNAME.flash1 +flash bank $_FLASHNAME stm32lx 0 0 0 0 $_TARGETNAME + +# There is only system reset line and JTAG/SWD command can be issued when SRST +reset_config srst_only + +$_TARGETNAME configure -event gdb-attach { + echo "Debugger attaching: halting execution" + reset halt + gdb_breakpoint_override hard +} + +$_TARGETNAME configure -event gdb-detach { + echo "Debugger detaching: resuming execution" + resume +} diff --git a/boards/mppt1210hus_0v4.json b/boards/mppt1210hus_0v4.json new file mode 100644 index 0000000..edbe160 --- /dev/null +++ b/boards/mppt1210hus_0v4.json @@ -0,0 +1,43 @@ +{ + "build": { + "cpu": "cortex-m0plus", + "extra_flags": "-DSTM32L073xx", + "f_cpu": "32000000L", + "framework_extra_flags": { + "arduino": "-D__CORTEX_SC=0" + }, + "mcu": "stm32l073rz", + "variant": "MPPT-1210-HUS" + }, + "debug": { + "default_tools": [ + "stlink" + ], + "jlink_device": "STM32L073RZ", + "onboard_tools": [ + "stlink" + ], + "openocd_board": "st_nucleo_l073rz", + "svd_path": "STM32L07x.svd" + }, + "frameworks": [ + "arduino", + "mbed", + "stm32cube", + "zephyr" + ], + "name": "Libre Solar MPPT 1210 HUS v0.4", + "upload": { + "maximum_ram_size": 20480, + "maximum_size": 196608, + "protocol": "stlink", + "protocols": [ + "jlink", + "stlink", + "blackmagic", + "mbed" + ] + }, + "url": "http://libre.solar/", + "vendor": "Libre Solar" +} diff --git a/platformio.ini b/platformio.ini index 84c3b50..6ea4c44 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,6 +14,7 @@ default_envs = # mppt-1210-hus-v0.4 # mppt-1210-hus-v0.6 # mppt-1210-hus-v0.7 +# mppt-1210-hus-v0.4-zephyr # pwm-2420-lus-v0.2 # pwm-2420-lus-v0.3 # pwm-2420-lus-v0.3-bl @@ -103,6 +104,17 @@ board = nucleo_l073rz build_flags = ${env.build_flags} -D MPPT_1210_HUS_0V7 +[env:mppt-1210-hus-v0.4-zephyr] +board = mppt1210hus_0v4 +framework = zephyr +build_flags = + -std=gnu++17 + -fsingle-precision-constant + -Wl,-Map,memory.map +platform = https://github.com/platformio/platform-ststm32.git +lib_ignore = USB, mbed-USBDevice, mbed-mbedtls, USBSerial, ESP32, Adafruit_GFX, SX1276GenericLib +extra_scripts = + # https://github.com/LibreSolar/PWM-2420-LUS [env:pwm-2420-lus-v0.2] board = nucleo_l073rz diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index c8b3049..35fe79b 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -14,17 +14,19 @@ * limitations under the License. */ -#ifndef UNIT_TEST +#include "adc_dma.h" + +#include // log for thermistor calculation +#include + +#ifdef __MBED__ #include "mbed.h" #endif +#include "mcu.h" #include "main.h" #include "debug.h" -#include - -#include "adc_dma.h" #include "pcb.h" // contains defines for pins -#include // log for thermistor calculation // factory calibration values for internal voltage reference and temperature sensor // (see MCU datasheet, not RM) @@ -51,7 +53,7 @@ #define TSENSE_CAL2_VALUE 130.0 // temperature of second calibration point #endif -#ifdef PIN_REF_I_DCDC +#if defined(__MBED__) && defined(PIN_REF_I_DCDC) AnalogOut ref_i_dcdc(PIN_REF_I_DCDC); #endif @@ -60,10 +62,10 @@ static float solar_current_offset; static float load_current_offset; // for ADC and DMA -static volatile uint16_t adc_readings[NUM_ADC_CH] = {0}; -static volatile uint32_t adc_filtered[NUM_ADC_CH] = {0}; -static volatile AdcAlert adc_alerts_upper[NUM_ADC_CH] = {0}; -static volatile AdcAlert adc_alerts_lower[NUM_ADC_CH] = {0}; +static volatile uint16_t adc_readings[NUM_ADC_CH] = {}; +static volatile uint32_t adc_filtered[NUM_ADC_CH] = {}; +static volatile AdcAlert adc_alerts_upper[NUM_ADC_CH] = {}; +static volatile AdcAlert adc_alerts_lower[NUM_ADC_CH] = {}; extern DeviceStatus dev_stat; @@ -321,7 +323,7 @@ void adc_set_lv_alerts(float upper, float lower) adc_alerts_lower[ADC_POS_V_BAT].callback = low_voltage_alert; } -#ifndef UNIT_TEST +#if defined(__MBED__) && !defined(UNIT_TEST) void dma_setup() { diff --git a/src/can_msg_queue.cpp b/src/can_msg_queue.cpp index d4f1fcc..7fe6af5 100644 --- a/src/can_msg_queue.cpp +++ b/src/can_msg_queue.cpp @@ -1,4 +1,4 @@ -#ifndef UNIT_TEST +#if defined(__MBED__) && !defined(UNIT_TEST) #include "mbed.h" #ifdef STM32F0 // STM32L0 does not have CAN diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 2f7e56f..8151d83 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -192,7 +192,7 @@ const data_object_t data_objects[] = { #endif // FUNCTION CALLS (EXEC) ////////////////////////////////////////////////// -#ifndef UNIT_TEST +#if defined(__MBED__) && !defined(UNIT_TEST) {0xE0, TS_EXEC, TS_EXEC_ALL, TS_T_BOOL, 0, (void*) &NVIC_SystemReset, "Reset"}, #endif {0xE1, TS_EXEC, TS_EXEC_ALL, TS_T_BOOL, 0, (void*) &start_stm32_bootloader, "BootloaderSTM"}, diff --git a/src/eeprom.cpp b/src/eeprom.cpp index b4b3e42..e1eb81b 100644 --- a/src/eeprom.cpp +++ b/src/eeprom.cpp @@ -14,11 +14,14 @@ * limitations under the License. */ +#include "eeprom.h" + +#include +#include + +#include "mcu.h" #include "pcb.h" #include "thingset.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! @@ -154,7 +157,7 @@ int eeprom_read (unsigned int addr, uint8_t* ret, int len) return i2c_eeprom.read(device, (char*)ret, len); } -#elif defined(STM32L0) // internal EEPROM +#elif defined(STM32L0) && defined(__MBED__) // internal EEPROM int eeprom_write (unsigned int addr, const uint8_t* data, int len) { @@ -200,6 +203,12 @@ int eeprom_read (unsigned int addr, uint8_t* ret, int len) return 0; } +#else + +// ToDo for Zephyr +int eeprom_write (unsigned int addr, const uint8_t* data, int len) { return 1; } +int eeprom_read (unsigned int addr, uint8_t* ret, int len) { return 1; } + #endif // STM32L0 @@ -216,7 +225,7 @@ void eeprom_restore_data() uint8_t buf_req[300]; // ThingSet request buffer // EEPROM header - uint8_t buf_header[EEPROM_HEADER_SIZE]; + uint8_t buf_header[EEPROM_HEADER_SIZE] = {}; // initialize to avoid compiler warning if (eeprom_read(0, buf_header, EEPROM_HEADER_SIZE) < 0) { printf("EEPROM: read error!\n"); return; diff --git a/src/eeprom.h b/src/eeprom.h index f89ebfb..f40349e 100644 --- a/src/eeprom.h +++ b/src/eeprom.h @@ -17,6 +17,8 @@ #ifndef EEPROM_H #define EEPROM_H +#include + /** @file * * @brief Handling of internal or external EEPROM to store device configuration diff --git a/src/half_bridge.cpp b/src/half_bridge.cpp index b4b3bc1..f53252a 100644 --- a/src/half_bridge.cpp +++ b/src/half_bridge.cpp @@ -16,11 +16,11 @@ #include "half_bridge.h" + +#include "mcu.h" #include "pcb.h" #include "config.h" - - static int _pwm_resolution; static float _min_duty; static float _max_duty; @@ -57,8 +57,13 @@ class PWM_TIM3 #endif // Select AF2 on PB0 and PB1 +#ifdef __MBED__ GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFRL0_Pos; GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFRL1_Pos; +#else + GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFSEL0_Pos; + GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFSEL1_Pos; +#endif // No prescaler --> timer frequency == SystemClock TIM3->PSC = 0; diff --git a/src/hardware.cpp b/src/hardware.cpp index 2fac443..d4784ad 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -14,15 +14,17 @@ * limitations under the License. */ -#ifndef UNIT_TEST +#if defined(__MBED__) #include "hardware.h" -#include "pcb.h" -#include "load.h" #include "mbed.h" + +#include "mcu.h" +#include "pcb.h" +#include "load.h" #include "half_bridge.h" -#include "us_ticker_data.h" +//#include "us_ticker_data.h" #include "leds.h" extern LoadOutput load; diff --git a/src/leds.cpp b/src/leds.cpp index 62722e5..36a2a01 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -25,7 +25,7 @@ static int blink_state = 1; // global state of all blinking static bool charging = false; -#ifndef UNIT_TEST +#ifdef __MBED__ #include "mbed.h" diff --git a/src/load.cpp b/src/load.cpp index f393013..70a49c6 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -15,6 +15,7 @@ */ #include "load.h" + #include "pcb.h" #include "config.h" #include "hardware.h" @@ -145,7 +146,7 @@ void LoadOutput::switch_set(bool status) pgood = status; leds_set(LED_LOAD, status); -#ifdef PIN_LOAD_EN +#if defined(__MBED__) && defined(PIN_LOAD_EN) DigitalOut load_enable(PIN_LOAD_EN); if (status == true) { load_enable = 1; @@ -155,7 +156,7 @@ void LoadOutput::switch_set(bool status) } #endif -#ifdef PIN_LOAD_DIS +#if defined(__MBED__) && defined(PIN_LOAD_DIS) DigitalOut load_disable(PIN_LOAD_DIS); if (status == true) { #ifdef PIN_I_LOAD_COMP @@ -175,12 +176,12 @@ void LoadOutput::usb_set(bool status) { usb_pgood = status; -#ifdef PIN_USB_PWR_EN +#if defined(__MBED__) && defined(PIN_USB_PWR_EN) DigitalOut usb_pwr_en(PIN_USB_PWR_EN); if (status == true) usb_pwr_en = 1; else usb_pwr_en = 0; #endif -#ifdef PIN_USB_PWR_DIS +#if defined(__MBED__) && defined(PIN_USB_PWR_DIS) DigitalOut usb_pwr_dis(PIN_USB_PWR_DIS); if (status == true) usb_pwr_dis = 0; else usb_pwr_dis = 1; diff --git a/src/main.cpp b/src/main_mbed.cpp similarity index 99% rename from src/main.cpp rename to src/main_mbed.cpp index e8b6863..a32d228 100644 --- a/src/main.cpp +++ b/src/main_mbed.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#ifndef UNIT_TEST +#ifdef __MBED__ /** @file * @@ -217,4 +217,4 @@ void system_control() counter++; } -#endif +#endif // __MBED__ diff --git a/src/main_zephyr.cpp b/src/main_zephyr.cpp new file mode 100644 index 0000000..cdfeeca --- /dev/null +++ b/src/main_zephyr.cpp @@ -0,0 +1,95 @@ +/* Libre Solar Battery Management System 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. + */ + +#ifdef __ZEPHYR__ + +#include +#include + +#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 "bat_charger.h" // battery settings and 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 "device_status.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 +#include "thingset_can.h" // CAN bus communication + +PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT) + +#if FEATURE_DCDC_CONVERTER +PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_lv_port; // internal low voltage side of DC/DC converter +Dcdc dcdc(&hv_terminal, &dcdc_lv_port, DCDC_MODE_INIT); +#endif + +#if FEATURE_PWM_SWITCH +PowerPort pwm_terminal; // external terminal of PWM switch port (normally solar) +PowerPort pwm_port_int; // internal side of PWM switch +PwmSwitch pwm_switch(&pwm_terminal, &pwm_port_int); +PowerPort &solar_terminal = pwm_terminal; +#else +PowerPort &solar_terminal = SOLAR_TERMINAL; // defined in config.h +#endif + +#if FEATURE_LOAD_OUTPUT +PowerPort load_terminal; // load terminal (also connected to lv_bus) +LoadOutput load(&load_terminal); +#endif + +PowerPort &bat_terminal = BATTERY_TERMINAL; // defined in config.h +#ifdef GRID_TERMINAL +PowerPort &grid_terminal = GRID_TERMINAL; +#endif + +Charger charger(&bat_terminal); + +BatConf bat_conf; // actual (used) battery configuration +BatConf bat_conf_user; // temporary storage where the user can write to + +DeviceStatus dev_stat; + +extern ThingSet ts; // defined in data_objects.cpp + +time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable) + +void main(void) +{ + printf("Booting Libre Solar Charge Controller: %s\n", CONFIG_BOARD); + + while (1) { + + k_sleep(1000); + } +} + +//K_THREAD_DEFINE(ts_serial_id, 4096, thingset_serial_thread, NULL, NULL, NULL, 5, 0, K_NO_WAIT); + +//K_THREAD_DEFINE(leds_id, 256, leds_update_thread, NULL, NULL, NULL, 4, 0, K_NO_WAIT); + +//K_THREAD_DEFINE(uext_thread, 1024, uext_process_1s, NULL, NULL, NULL, 6, 0, K_NO_WAIT); + +#endif // __ZEPHYR__ diff --git a/src/mcu.h b/src/mcu.h new file mode 100644 index 0000000..8d2a4ea --- /dev/null +++ b/src/mcu.h @@ -0,0 +1,25 @@ + +#ifndef MCU_H_ +#define MCU_H_ + +// mbed defines STM32Fxx variables automatically, so we define them also for Zephyr + +#ifdef CONFIG_SOC_STM32L073XX +#define STM32L0 +#define STM32L073 +#endif + +#ifdef CONFIG_SOC_STM32F072XX +#define STM32F0 +#define STM32F072 +#endif + +#ifdef STM32L073 +#include "stm32l073xx.h" +#endif + +#ifdef STM32F072 +#include "stm32f072xx.h" +#endif + +#endif // MCU_H_ diff --git a/src/pcbs/mppt_1210_hus_0v4.h b/src/pcbs/mppt_1210_hus_0v4.h index 38726de..6bbac5b 100644 --- a/src/pcbs/mppt_1210_hus_0v4.h +++ b/src/pcbs/mppt_1210_hus_0v4.h @@ -20,7 +20,9 @@ #define DEVICE_TYPE "MPPT-1210-HUS" #define HARDWARE_VERSION "v0.4" +#ifdef __MBED__ #include "mbed.h" +#endif // specify features of charge controller #define FEATURE_DCDC_CONVERTER 1 @@ -66,6 +68,7 @@ enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; #define LED_RXTX 3 // LED4, used to indicate when sending data #define LED_LOAD 4 // LED5 +#ifdef __MBED__ // LED pins and pin state configuration to switch above LEDs on #define NUM_LED_PINS 5 static const PinName led_pins[NUM_LED_PINS] = { @@ -79,6 +82,7 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { { PIN_LOW, PIN_LOW, PIN_LOW, PIN_HIGH, PIN_LOW }, // LED4 { PIN_LOW, PIN_LOW, PIN_LOW, PIN_LOW, PIN_HIGH } // LED5 }; +#endif // __MBED__ // pin definition only needed in adc_dma.cpp to detect if they are present on the PCB #define PIN_ADC_TEMP_FETS PA_5 diff --git a/src/pcbs/mppt_1210_hus_0v7.h b/src/pcbs/mppt_1210_hus_0v7.h index efc483f..90edcce 100644 --- a/src/pcbs/mppt_1210_hus_0v7.h +++ b/src/pcbs/mppt_1210_hus_0v7.h @@ -20,7 +20,9 @@ #define DEVICE_TYPE "MPPT-1210-HUS" #define HARDWARE_VERSION "v0.7.1" +#ifdef __MBED__ #include "mbed.h" +#endif // specify features of charge controller #define FEATURE_DCDC_CONVERTER 1 @@ -75,6 +77,7 @@ enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; #define LED_LOAD 3 // LED4 #define LED_RXTX 4 // LED5, used to indicate when sending data +#ifdef __MBED__ // LED pins and pin state configuration to switch above LEDs on #define NUM_LED_PINS 3 static const PinName led_pins[NUM_LED_PINS] = { @@ -88,6 +91,7 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { { PIN_FLOAT, PIN_HIGH, PIN_LOW }, // LED4 { PIN_FLOAT, PIN_LOW, PIN_HIGH } // LED5 }; +#endif // __MBED__ // pin definition only needed in adc_dma.cpp to detect if they are present on the PCB #define PIN_ADC_TEMP_BAT diff --git a/src/thingset_interface.cpp b/src/thingset_interface.cpp index fdd5410..e92f88f 100644 --- a/src/thingset_interface.cpp +++ b/src/thingset_interface.cpp @@ -16,11 +16,17 @@ #ifndef UNIT_TEST -#include "config.h" -#include "mbed.h" -#include "thingset.h" #include "thingset_interface.h" +#include // for for_each loop + +#ifdef __MBED__ +#include "mbed.h" +#endif + +#include "config.h" +#include "thingset.h" + /* * Construct all global ThingSetInterfaces here. * All of these are added to the list of devices diff --git a/src/thingset_serial.cpp b/src/thingset_serial.cpp index a6193e4..3fb07e8 100644 --- a/src/thingset_serial.cpp +++ b/src/thingset_serial.cpp @@ -14,12 +14,12 @@ * limitations under the License. */ -#ifndef UNIT_TEST +#ifdef __MBED__ + +#include "thingset_serial.h" #include "config.h" -#include "mbed.h" #include "thingset.h" -#include "thingset_serial.h" char ThingSetStream::buf_resp[1000]; @@ -90,4 +90,4 @@ void ThingSetStream::process_input() } } } -#endif /* UNIT_TEST */ +#endif /* __MBED__ */ diff --git a/src/thingset_serial.h b/src/thingset_serial.h index d87d657..9efa481 100644 --- a/src/thingset_serial.h +++ b/src/thingset_serial.h @@ -24,6 +24,7 @@ */ #include "mbed.h" + #include "thingset_interface.h" class ThingSetStream: public ThingSetInterface @@ -71,4 +72,5 @@ template class ThingSetSerial: public ThingSetStream private: T& ser; }; + #endif /* THINGSET_SERIAL_H */ diff --git a/src/uext/uext_oled.cpp b/src/uext/uext_oled.cpp index 44d2e3c..312930e 100644 --- a/src/uext/uext_oled.cpp +++ b/src/uext/uext_oled.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#ifndef UNIT_TEST +#ifdef __MBED__ #include "main.h" @@ -187,4 +187,4 @@ void UExtOled::process_1s() { #endif /* OLED_ENABLED */ -#endif /* UNIT_TEST */ +#endif /* __MBED__ */ diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt new file mode 100644 index 0000000..02a7ffb --- /dev/null +++ b/zephyr/CMakeLists.txt @@ -0,0 +1,35 @@ +# SPDX-License-Identifier: Apache-2.0 + +#set(BOARD nucleo_l073rz) +set(BOARD_ROOT ${CMAKE_SOURCE_DIR}/..) + +cmake_minimum_required(VERSION 3.13.1) +include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) + +project(libre_solar_bms) + +add_definitions(-DMPPT_1210_HUS_0V7 -D__ZEPHYR__ -DDEBUG_PRINT_FLAGS=7) +include_directories(../src) +include_directories(../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src) + +#target_sources(app PRIVATE ../src/adc_dma.cpp) +target_sources(app PRIVATE ../src/bat_charger.cpp) +#target_sources(app PRIVATE ../src/data_objects.cpp) +target_sources(app PRIVATE ../src/device_status.cpp) +target_sources(app PRIVATE ../src/dcdc.cpp) +#target_sources(app PRIVATE ../src/eeprom.cpp) +target_sources(app PRIVATE ../src/half_bridge.cpp) +#target_sources(app PRIVATE ../src/hardware.cpp) +#target_sources(app PRIVATE ../src/helper.cpp) +#target_sources(app PRIVATE ../src/leds.cpp) +#target_sources(app PRIVATE ../src/load.cpp) +target_sources(app PRIVATE ../src/main_zephyr.cpp) +target_sources(app PRIVATE ../src/power_port.cpp) +target_sources(app PRIVATE ../src/pwm_switch.cpp) +#target_sources(app PRIVATE ../src/thingset_serial.cpp) + +target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/thingset.cpp) +target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/thingset_cbor.cpp) +target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/cbor.c) +target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/thingset_json.cpp) +target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/jsmn.c) diff --git a/zephyr/prj.conf b/zephyr/prj.conf new file mode 100644 index 0000000..1ecaa95 --- /dev/null +++ b/zephyr/prj.conf @@ -0,0 +1,29 @@ +# direct printf calls to console +CONFIG_STDOUT_CONSOLE=y + +# default is 1024 +CONFIG_HEAP_MEM_POOL_SIZE=0 + +CONFIG_GPIO=y +CONFIG_SERIAL=y +CONFIG_I2C=y + +# for OLED display +#CONFIG_DISPLAY=y +#CONFIG_SSD1306=y + +CONFIG_LOG=y + +CONFIG_CFB_LOG_LEVEL_DBG=y +#CONFIG_CHARACTER_FRAMEBUFFER=y + +#CONFIG_NEWLIB_LIBC_NANO=y +CONFIG_NEWLIB_LIBC=y +CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y + +CONFIG_CONSOLE_SUBSYS=y +CONFIG_CONSOLE_GETCHAR=y + +CONFIG_CPLUSPLUS=y + +CONFIG_BUILD_OUTPUT_BIN=y