diff --git a/boards/arm/mppt_2420_lc_0v10/CMakeLists.txt b/boards/arm/mppt_2420_lc_0v10/CMakeLists.txt new file mode 100755 index 0000000..d1b8108 --- /dev/null +++ b/boards/arm/mppt_2420_lc_0v10/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/mppt_2420_lc_0v10/Kconfig b/boards/arm/mppt_2420_lc_0v10/Kconfig new file mode 100644 index 0000000..2aa1ba7 --- /dev/null +++ b/boards/arm/mppt_2420_lc_0v10/Kconfig @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2020 Martin Jäger / Libre Solar + +# include generic charge controller Kconfig +rsource "../../Kconfig" diff --git a/boards/arm/mppt_2420_lc_0v10/Kconfig.board b/boards/arm/mppt_2420_lc_0v10/Kconfig.board new file mode 100755 index 0000000..98e59f9 --- /dev/null +++ b/boards/arm/mppt_2420_lc_0v10/Kconfig.board @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2020 Martin Jäger / Libre Solar + +config BOARD_MPPT_2420_LC_0V10 + bool "Libre Solar MPPT 2420 LC v0.10" + depends on SOC_STM32F072XB diff --git a/boards/arm/mppt_2420_lc_0v10/Kconfig.defconfig b/boards/arm/mppt_2420_lc_0v10/Kconfig.defconfig new file mode 100755 index 0000000..479b250 --- /dev/null +++ b/boards/arm/mppt_2420_lc_0v10/Kconfig.defconfig @@ -0,0 +1,48 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2019 Martin Jäger / Libre Solar + +if BOARD_MPPT_2420_LC_0V10 + +config BOARD + default "mppt_2420_lc_0v10" + +config HAS_DCDC_CONVERTER + default y + +config DCDC_PWM_FREQUENCY_KHZ + default 70 + +config DCDC_PWM_DEADTIME_NS + default 300 + +config HAS_LOAD_OUTPUT + default y + +config NUM_LED_PINS + default 2 + +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_MPPT_2420_LC_0V10 diff --git a/boards/arm/mppt_2420_lc_0v10/board.cmake b/boards/arm/mppt_2420_lc_0v10/board.cmake new file mode 100755 index 0000000..3a7bd63 --- /dev/null +++ b/boards/arm/mppt_2420_lc_0v10/board.cmake @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: Apache-2.0 + +board_runner_args(jlink "--device=STM32F072CB" "--speed=4000") + +include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake) +include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake) diff --git a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts new file mode 100755 index 0000000..3e03054 --- /dev/null +++ b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts @@ -0,0 +1,78 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2020 Martin Jäger / Libre Solar + */ + +/dts-v1/; +#include + +/ { + model = "Libre Solar MPPT 2420 LC v0.10"; + compatible = "st,stm32f072"; + + chosen { + zephyr,console = &usart1; + zephyr,shell-uart = &usart1; + zephyr,sram = &sram0; + zephyr,flash = &flash0; + }; + + leds { + compatible = "gpio-leds"; + pwr { + gpios = <&gpiob 14 GPIO_INT_ACTIVE_LOW>; + label = "LED GND"; + }; + load { + gpios = <&gpiob 15 GPIO_INT_ACTIVE_HIGH>; + label = "LED Load"; + }; + }; + + switches { + compatible = "switch"; + load-dis { + gpios = <&gpiob 2 GPIO_DIR_IN>; + label = "Load disable"; + }; + uext-dis { + gpios = <&gpioc 14 GPIO_DIR_IN>; + label = "UEXT disable"; + }; + }; + + aliases { + uart-dbg = &usart1; + uext-uart = &usart2; + spi-uext = &spi1; + i2c-uext = &i2c1; + }; +}; + +&usart1 { + current-speed = <115200>; + status = "okay"; +}; + +&usart2 { + current-speed = <115200>; + status = "okay"; +}; + +&i2c1 { + status = "okay"; + clock-frequency = ; +}; + +&spi1 { + status = "okay"; +}; + +&iwdg { + status = "okay"; +}; + +&adc1 { + status = "okay"; +}; diff --git a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.yaml b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.yaml new file mode 100755 index 0000000..d1aca38 --- /dev/null +++ b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.yaml @@ -0,0 +1,17 @@ +identifier: mppt_2420_LC_0v10 +name: MPPT-2420-LC-0V10 +type: mcu +arch: arm +toolchain: + - zephyr + - gnuarmemb + - xtools +ram: 16 +flash: 128 +supported: + - arduino_i2c + - gpio + - i2c + - spi + - watchdog + - adc diff --git a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10_defconfig b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10_defconfig new file mode 100755 index 0000000..dbac771 --- /dev/null +++ b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10_defconfig @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Zephyr Kernel Configuration +CONFIG_ARM=y +CONFIG_SOC_SERIES_STM32F0X=y + +# Platform Configuration +CONFIG_SOC_STM32F072XB=y + +# General Kernel Options +CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=48000000 + +# 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_HSE_CLOCK=8000000 +CONFIG_CLOCK_STM32_SYSCLK_SRC_PLL=y +# PLL configuration +CONFIG_CLOCK_STM32_PLL_SRC_HSE=y + +CONFIG_CLOCK_STM32_PLL_MULTIPLIER=6 +CONFIG_CLOCK_STM32_AHB_PRESCALER=1 +CONFIG_CLOCK_STM32_APB1_PRESCALER=1 diff --git a/boards/arm/mppt_2420_lc_0v10/pinmux.c b/boards/arm/mppt_2420_lc_0v10/pinmux.c new file mode 100755 index 0000000..227ed17 --- /dev/null +++ b/boards/arm/mppt_2420_lc_0v10/pinmux.c @@ -0,0 +1,70 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2020 Martin Jäger / Libre Solar + */ + +#include +#include +#include +#include +#include + +#include + +// preliminary fix, as Zephyr doesn't yet include all possible pinmuxes +#ifndef STM32F0_PINMUX_FUNC_PB6_I2C1_SCL +#define STM32F0_PINMUX_FUNC_PB6_I2C1_SCL \ + (STM32_PINMUX_ALT_FUNC_1 | STM32_OPENDRAIN_PULLUP) +#endif +#ifndef STM32F0_PINMUX_FUNC_PB7_I2C1_SDA +#define STM32F0_PINMUX_FUNC_PB7_I2C1_SDA \ + (STM32_PINMUX_ALT_FUNC_1 | STM32_OPENDRAIN_PULLUP) +#endif + +static const struct pin_config pinconf[] = { +#ifdef CONFIG_UART_1 + {STM32_PIN_PA9, STM32F0_PINMUX_FUNC_PA9_USART1_TX}, + {STM32_PIN_PA10, STM32F0_PINMUX_FUNC_PA10_USART1_RX}, +#endif /* CONFIG_UART_1 */ +#ifdef CONFIG_UART_2 + {STM32_PIN_PA2, STM32F0_PINMUX_FUNC_PA2_USART2_TX}, + {STM32_PIN_PA3, STM32F0_PINMUX_FUNC_PA3_USART2_RX}, +#endif /* CONFIG_UART_2 */ +#ifdef CONFIG_I2C_1 + {STM32_PIN_PB6, STM32F0_PINMUX_FUNC_PB6_I2C1_SCL}, + {STM32_PIN_PB7, STM32F0_PINMUX_FUNC_PB7_I2C1_SDA}, +#endif /* CONFIG_I2C_1 */ +#ifdef CONFIG_SPI_1 + {STM32_PIN_PB3, STM32F0_PINMUX_FUNC_PB3_SPI1_SCK}, + {STM32_PIN_PB4, STM32F0_PINMUX_FUNC_PB4_SPI1_MISO}, + {STM32_PIN_PB5, STM32F0_PINMUX_FUNC_PB5_SPI1_MOSI}, +#endif /* CONFIG_SPI_1 */ +#ifdef CONFIG_ADC_1 + {STM32_PIN_PA0, STM32F0_PINMUX_FUNC_PA0_ADC_IN0}, +#endif /* CONFIG_ADC_1 */ +#ifdef CONFIG_DAC_1 + {STM32_PIN_PA4, STM32F0_PINMUX_FUNC_PA4_DAC_OUT1}, +#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); + +const char *led_ports[CONFIG_NUM_LED_PINS] = { + DT_GPIO_LEDS_PWR_GPIOS_CONTROLLER, + DT_GPIO_LEDS_LOAD_GPIOS_CONTROLLER +}; +const int led_pins[CONFIG_NUM_LED_PINS] = { + DT_GPIO_LEDS_PWR_GPIOS_PIN, + DT_GPIO_LEDS_LOAD_GPIOS_PIN +}; diff --git a/boards/arm/mppt_2420_lc_0v10/support/openocd.cfg b/boards/arm/mppt_2420_lc_0v10/support/openocd.cfg new file mode 100755 index 0000000..f0d0b22 --- /dev/null +++ b/boards/arm/mppt_2420_lc_0v10/support/openocd.cfg @@ -0,0 +1,12 @@ +source [find board/st_nucleo_f0.cfg] + +$_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/mppt_2420_lc_0v10.json b/boards/mppt_2420_lc_0v10.json new file mode 100644 index 0000000..a4c4fa0 --- /dev/null +++ b/boards/mppt_2420_lc_0v10.json @@ -0,0 +1,41 @@ +{ + "build": { + "cpu": "cortex-m0", + "extra_flags": "-DSTM32F072xB", + "f_cpu": "48000000L", + "mcu": "stm32f072rbt6" + }, + "connectivity": [ + "can" + ], + "debug": { + "default_tools": [ + "stlink" + ], + "jlink_device": "STM32F072RB", + "onboard_tools": [ + "stlink" + ], + "openocd_board": "st_nucleo_f0", + "svd_path": "STM32F072x.svd" + }, + "frameworks": [ + "mbed", + "stm32cube", + "zephyr" + ], + "name": "Libre Solar MPPT 2420 LC v0.10", + "upload": { + "maximum_ram_size": 16384, + "maximum_size": 131072, + "protocol": "stlink", + "protocols": [ + "jlink", + "stlink", + "blackmagic", + "mbed" + ] + }, + "url": "http://libre.solar/", + "vendor": "Libre Solar" +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 7c7143c..09df52a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,6 +10,7 @@ # Select the charge controller hardware by uncommenting one of the following lines default_envs = # mppt-2420-lc-v0.10 +# mppt-2420-lc-v0.10-zephyr # mppt-2420-hpx-v0.1-zephyr # mppt-1210-hus-v0.2 # mppt-1210-hus-v0.4 @@ -78,6 +79,17 @@ board = nucleo_f072rb build_flags = ${env.build_flags} -D CONFIG_BOARD_MPPT_2420_LC_0V10 +[env:mppt-2420-lc-v0.10-zephyr] +board = mppt_2420_lc_0v10 +framework = zephyr +build_flags = + -std=gnu++17 + -fsingle-precision-constant + -Wl,-Map,memory.map +platform = https://github.com/platformio/platform-ststm32.git +lib_ignore = ESP32 +extra_scripts = generate_version_file.py + # https://github.com/LibreSolar/MPPT-1210-HUS/tree/586626f3d8 [env:mppt-1210-hus-v0.2] board = nucleo_f072rb diff --git a/src/daq_zephyr.c b/src/daq_zephyr.c index e1737ef..1f8c568 100644 --- a/src/daq_zephyr.c +++ b/src/daq_zephyr.c @@ -20,6 +20,12 @@ #include #include #include +#elif defined(CONFIG_SOC_SERIES_STM32F0X) +#include +#include +#include +#include +#include #elif defined(CONFIG_SOC_SERIES_STM32G4X) #include #include @@ -99,7 +105,9 @@ static void adc_setup() struct adc_channel_cfg channel_cfg = { .gain = ADC_GAIN_1, .reference = ADC_REF_INTERNAL, -#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X) +#if defined(CONFIG_SOC_SERIES_STM32F0X) + .acquisition_time = ADC_ACQ_TIME(ADC_ACQ_TIME_TICKS, 240), +#elif defined(CONFIG_SOC_SERIES_STM32L0X) .acquisition_time = ADC_ACQ_TIME(ADC_ACQ_TIME_TICKS, 161), #elif defined(CONFIG_SOC_SERIES_STM32G4X) // ToDo: channel-specific acquisition time. Only internal reference voltage diff --git a/src/eeprom.cpp b/src/eeprom.cpp index 20b9065..9b10933 100644 --- a/src/eeprom.cpp +++ b/src/eeprom.cpp @@ -63,7 +63,7 @@ uint32_t _calc_crc(const uint8_t *buf, size_t len) #endif // UNIT_TEST -#if defined(EEPROM_24AA01) || defined(EEPROM_24AA32) +#if (defined(EEPROM_24AA01) || defined(EEPROM_24AA32)) && defined(__MBED__) #ifdef EEPROM_24AA01 #define EEPROM_PAGE_SIZE 8 // see datasheet of 24AA01 diff --git a/src/ext/oled.cpp b/src/ext/oled.cpp index 7dd3e90..b5d0658 100644 --- a/src/ext/oled.cpp +++ b/src/ext/oled.cpp @@ -15,6 +15,12 @@ #include #include +#if defined(__ZEPHYR__) +#include +#include +#include +#endif + #include "main.h" #include "pcb.h" #include "oled_ssd1306.h" @@ -59,11 +65,18 @@ OledSSD1306 oled(i2c_dev); void ExtOled::enable() { -#ifdef PIN_UEXT_DIS +#if defined(PIN_UEXT_DIS) && defined(__MBED__) DigitalOut uext_dis(PIN_UEXT_DIS); uext_dis = 0; #endif +#if defined(DT_SWITCH_UEXT_DIS_GPIOS_CONTROLLER) && defined(__ZEPHYR__) + struct device *dev_uext_dis = + device_get_binding(DT_SWITCH_UEXT_DIS_GPIOS_CONTROLLER); + gpio_pin_configure(dev_uext_dis, DT_SWITCH_UEXT_DIS_GPIOS_PIN, GPIO_DIR_OUT); + gpio_pin_write(dev_uext_dis, DT_SWITCH_UEXT_DIS_GPIOS_PIN, 0); +#endif + #ifdef OLED_BRIGHTNESS oled.init(OLED_BRIGHTNESS); #else diff --git a/src/mcu.h b/src/mcu.h index 826a064..1098530 100644 --- a/src/mcu.h +++ b/src/mcu.h @@ -14,7 +14,7 @@ #define STM32L073 #endif -#ifdef CONFIG_SOC_STM32F072XX +#ifdef CONFIG_SOC_STM32F072XB #define STM32F0 #define STM32F072 #endif @@ -29,7 +29,7 @@ #endif #ifdef STM32F072 -#include "stm32f072xx.h" +#include "stm32f072xb.h" #endif #ifdef STM32G431 diff --git a/src/pcbs/mppt_2420_lc_0v10.h b/src/pcbs/mppt_2420_lc_0v10.h index 3f75b3c..08dd461 100644 --- a/src/pcbs/mppt_2420_lc_0v10.h +++ b/src/pcbs/mppt_2420_lc_0v10.h @@ -10,7 +10,11 @@ #define DEVICE_TYPE "MPPT-2420-LC" #define HARDWARE_VERSION "v0.10" +#ifdef __MBED__ #include "mbed.h" +#elif defined(__ZEPHYR__) +#include +#endif // specify features of charge controller #define CONFIG_HAS_DCDC_CONVERTER 1 @@ -71,11 +75,19 @@ enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; // LED pins and pin state configuration to switch above LEDs on #define NUM_LED_PINS 2 + +#ifdef __MBED__ static const PinName led_pins[NUM_LED_PINS] = { // PWR LOAD PB_14, PB_15 }; -static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { +#elif defined(__ZEPHYR__) +// defined in board definition pinmux.c +extern const char *led_ports[CONFIG_NUM_LED_PINS]; +extern const int led_pins[CONFIG_NUM_LED_PINS]; +#endif // MBED or ZEPHYR + +static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { { PIN_HIGH, PIN_LOW }, // LED1 { PIN_LOW, PIN_HIGH } // LED2 }; diff --git a/zephyr/prj.conf b/zephyr/prj.conf index da21e89..d147597 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -1,8 +1,8 @@ # direct printf calls to console CONFIG_STDOUT_CONSOLE=y -# default is 1024 -CONFIG_HEAP_MEM_POOL_SIZE=1024 +# default is 1024, but we need just a few bytes for ExtManager +CONFIG_HEAP_MEM_POOL_SIZE=256 CONFIG_GPIO=y CONFIG_SERIAL=y @@ -12,7 +12,8 @@ CONFIG_I2C_1=y CONFIG_ADC=y CONFIG_ADC_1=y -CONFIG_LOG=y +# disable log by default to save memory for STM32F0 with little RAM +CONFIG_LOG=n #CONFIG_NEWLIB_LIBC_NANO=y CONFIG_NEWLIB_LIBC=y