diff --git a/.travis.yml b/.travis.yml index e7babdd..39c29d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ addons: script: # check for trailing white spaces (comparing with first commit in git log) - git diff --check `git rev-list HEAD | tail -n 1`.. - - platformio run -e mppt_2420_lc -e mppt_1210_hus -e mppt_2420_hpx -e pwm_2420_lus + - platformio run -e mppt_2420_lc -e mppt_1210_hus -e mppt_2420_hc -e pwm_2420_lus - platformio test -e unit_test - platformio check -e mppt_1210_hus -e pwm_2420_lus --skip-packages --fail-on-defect high - doxygen Doxyfile diff --git a/boards/arm/mppt_2420_hc/CMakeLists.txt b/boards/arm/mppt_2420_hc/CMakeLists.txt new file mode 100644 index 0000000..ac5a362 --- /dev/null +++ b/boards/arm/mppt_2420_hc/CMakeLists.txt @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: Apache-2.0 + +if(CONFIG_PINMUX) +zephyr_library() +zephyr_library_sources(pinmux.c) +zephyr_library_include_directories(${ZEPHYR_BASE}/drivers) +endif() + +zephyr_include_directories(.) diff --git a/boards/arm/mppt_2420_hc/Kconfig.board b/boards/arm/mppt_2420_hc/Kconfig.board new file mode 100644 index 0000000..8e46db9 --- /dev/null +++ b/boards/arm/mppt_2420_hc/Kconfig.board @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2020 Martin Jäger / Libre Solar + +config BOARD_MPPT_2420_HC + bool "Libre Solar MPPT 2420 HC" + depends on SOC_STM32G431XX diff --git a/boards/arm/mppt_2420_hc/Kconfig.defconfig b/boards/arm/mppt_2420_hc/Kconfig.defconfig new file mode 100644 index 0000000..27aa351 --- /dev/null +++ b/boards/arm/mppt_2420_hc/Kconfig.defconfig @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2020 Martin Jäger / Libre Solar + +if BOARD_MPPT_2420_HC + +config BOARD + default "mppt_2420_hc" + +config LIBRE_SOLAR_TYPE_ID + default 8 + +endif # BOARD_MPPT_2420_HC diff --git a/boards/arm/mppt_2420_hc/board.cmake b/boards/arm/mppt_2420_hc/board.cmake new file mode 100644 index 0000000..73f98d2 --- /dev/null +++ b/boards/arm/mppt_2420_hc/board.cmake @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +board_runner_args(pyocd "--target=stm32g431rbtx") +board_runner_args(jlink "--device=STM32G431RB" "--speed=4000" "--reset-after-load") + +include(${ZEPHYR_BASE}/boards/common/pyocd.board.cmake) +include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake) +include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake) diff --git a/boards/arm/mppt_2420_hc/mppt_2420_hc.dts b/boards/arm/mppt_2420_hc/mppt_2420_hc.dts new file mode 100644 index 0000000..adac2cd --- /dev/null +++ b/boards/arm/mppt_2420_hc/mppt_2420_hc.dts @@ -0,0 +1,229 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2020 Martin Jäger / Libre Solar + */ + +/dts-v1/; +#include + +/ { + model = "Libre Solar MPPT 2420 HC"; + compatible = "st,stm32g431"; + + pcb { + compatible = "charge-controller"; + + type = "MPPT 2420 HC"; + version-str = "v0.1"; + version-num = <1>; + + hs-voltage-max = <80>; + ls-voltage-max = <32>; + }; + + dcdc { + compatible = "dcdc"; + timer = <&timers1>; + pwm-frequency = <70000>; + pwm-deadtime = <300>; + current-max = <20>; + }; + + chosen { + zephyr,console = &usart1; + zephyr,shell-uart = &usart1; + zephyr,sram = &sram0; + zephyr,flash = &flash0; + }; + + /* temporary solution w/o using RGB LED */ + leds { + compatible = "charlieplexing-leds"; + gpios = <&gpiob 14 GPIO_ACTIVE_LOW>, /* red */ + <&gpiob 15 GPIO_ACTIVE_LOW>, /* green */ + <&gpiob 9 GPIO_ACTIVE_LOW>; /* blue */ + + pwr { + states = <1 0 0>; + }; + + load { + states = <0 1 0>; + }; + + rxtx { + states = <0 0 1>; + }; + }; + + outputs { + compatible = "outputs"; + + load { + label = "Load output"; + gpios = <&gpiob 2 GPIO_ACTIVE_LOW>; + current-max = <20>; + kconfig-flag; /* make node visible in Kconfig */ + }; + + charge_pump { + label = "Charge pump for HS MOSFET drivers"; + pwms = <&pwm8 2 200000 PWM_POLARITY_NORMAL>; /* 200 us period = 5 kHz */ + }; + + can_en { + label = "CAN disable / standby"; + gpios = <&gpiob 11 GPIO_ACTIVE_LOW>; + }; + }; + + adc-inputs { + compatible = "adc-inputs"; + + v-low { + io-channels = <&adc1 12>; + multiplier = <105600>; + divider = <5600>; + }; + + v-high { + io-channels = <&adc1 15>; + multiplier = <102200>; + divider = <2200>; + }; + + temp-fets { + io-channels = <&adc1 11>; + multiplier = <10000>; // 10k NTC series resistor + divider = <1>; + }; + + vref-mcu { + io-channels = <&adc1 18>; + multiplier = <1>; + divider = <1>; + }; + + temp-mcu { + io-channels = <&adc1 16>; + multiplier = <1>; + divider = <1>; + }; + + i-dcdc { + io-channels = <&adc2 1>; + // amp gain: 25, resistor: 2 mOhm + multiplier = <1000>; // 1000 + divider = <50>; // 2*25 + }; + + i-load { + io-channels = <&adc2 2>; + // amp gain: 25, resistor: 2 mOhm + multiplier = <1000>; // 1000 + divider = <50>; // 2*25 + }; + }; + + soc { + dac1: dac@50000800 { + compatible = "st,stm32-dac"; + reg = <0x50000800 0x400>; + clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00010000>; + status = "disabled"; + label = "DAC_1"; + #io-channel-cells = <1>; + }; + }; + + aliases { + uart-dbg = &usart1; + uart-uext = &usart2; + spi-uext = &spi1; + i2c-uext = &i2c1; + }; +}; + +&usart1 { + current-speed = <115200>; + status = "okay"; +}; + +&usart2 { + current-speed = <115200>; + status = "okay"; +}; + +&i2c1 { + status = "okay"; +}; + +&spi1 { + cs-gpios = <&gpiob 6 GPIO_ACTIVE_LOW>; + status = "okay"; +}; + +&usb { + status = "okay"; +}; + +&rtc { + status = "okay"; +}; + +&flash0 { + /* + * For more information, see: + * http://docs.zephyrproject.org/latest/guides/dts/index.html#flash-partitions + */ + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + }; +}; + +&iwdg { + status = "okay"; +}; + +/* TIM8 (CH2) used for charge pump, but is currently not working with original Zephyr driver */ +&timers8 { + status = "okay"; + pwm8: pwm { + status = "okay"; + }; +}; + +&i2c1 { + status = "okay"; + clock-frequency = ; + eeprom@50 { + // Microchip 24AA32A + compatible = "atmel,at24"; + reg = <0x50>; + label = "EEPROM_0"; + size = <32768>; + pagesize = <32>; + address-width = <16>; + /* + * timeout of 5 ms as suggested in datasheet seems too optimistic + * (several write errors occured during testing) + */ + timeout = <7>; + }; +}; + +/* +// This requires https://github.com/zephyrproject-rtos/zephyr/pull/22832 +&can1 { + bus-speed = <250000>; + bus-speed-data = <250000>; + sjw = <1>; + sjw-data = <1>; + sample-point = <875>; + sample-point-data = <875>; + status = "okay"; +}; +*/ \ No newline at end of file diff --git a/boards/arm/mppt_2420_hc/mppt_2420_hc.yaml b/boards/arm/mppt_2420_hc/mppt_2420_hc.yaml new file mode 100644 index 0000000..0a7c32c --- /dev/null +++ b/boards/arm/mppt_2420_hc/mppt_2420_hc.yaml @@ -0,0 +1,18 @@ +identifier: mppt_2420_hc +name: MPPT-2420-HC +type: mcu +arch: arm +toolchain: + - zephyr + - gnuarmemb + - xtools +ram: 32 +flash: 128 +supported: + - nvs + - pwm + - i2c + - gpio + - usb device + - counter + - spi diff --git a/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig b/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig new file mode 100644 index 0000000..774b159 --- /dev/null +++ b/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig @@ -0,0 +1,72 @@ +# SPDX-License-Identifier: Apache-2.0 + +CONFIG_SOC_SERIES_STM32G4X=y +CONFIG_SOC_STM32G431XX=y + +# 170MHz system clock only in 'boost power' mode. RM0440, section +# 5.1.5 states that the R1MODE bit must be cleared before system can +# be 170MHz. This requires an update to the stm32 clock control +# driver, so default to 150MHz until then. +# CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=170000000 + +# 150MHz system clock in 'normal power' mode +CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=150000000 + +# enable uart driver +CONFIG_SERIAL=y + +# enable pinmux +CONFIG_PINMUX=y + +# enable GPIO +CONFIG_GPIO=y + +# clock configuration +CONFIG_CLOCK_CONTROL=y + +# Use PLLCLK for SYSCLK +CONFIG_CLOCK_STM32_SYSCLK_SRC_PLL=y + +# Use HSI (16MHz) to feed into PLL +CONFIG_CLOCK_STM32_PLL_SRC_HSI=y + +CONFIG_CLOCK_STM32_PLL_M_DIVISOR=4 +CONFIG_CLOCK_STM32_PLL_P_DIVISOR=7 +CONFIG_CLOCK_STM32_PLL_Q_DIVISOR=2 +CONFIG_CLOCK_STM32_PLL_R_DIVISOR=2 + +# Produce 150MHz clock at PLLCLK output +CONFIG_CLOCK_STM32_PLL_N_MULTIPLIER=75 + +# Comment out above and uncomment below for 170MHz. Note that you +# must have configured the mcu for boost power mode. +# CONFIG_CLOCK_STM32_PLL_N_MULTIPLIER=85 + +# Produce Max (150MHz or 170MHz) HCLK +CONFIG_CLOCK_STM32_AHB_PRESCALER=1 + +# Produce Max (150MHz or 170MHz) APB1 clocks and APB2 clocks +CONFIG_CLOCK_STM32_APB1_PRESCALER=1 +CONFIG_CLOCK_STM32_APB2_PRESCALER=1 + +# Console +CONFIG_CONSOLE=y +CONFIG_UART_CONSOLE=y + +# Enable MPU +CONFIG_ARM_MPU=y + +CONFIG_PWM=y + +CONFIG_I2C=y +CONFIG_EEPROM=y +CONFIG_EEPROM_AT24=y + +# don't enable CAN by default until PR #22832 is merged to upstream Zephyr +#CONFIG_CAN=y +#CONFIG_THINGSET_CAN=y + +CONFIG_ISOTP=y +CONFIG_ISOTP_RX_BUF_COUNT=1 +CONFIG_ISOTP_RX_BUF_SIZE=100 +CONFIG_ISOTP_RX_SF_FF_BUF_COUNT=2 diff --git a/boards/arm/mppt_2420_hc/pinmux.c b/boards/arm/mppt_2420_hc/pinmux.c new file mode 100644 index 0000000..505dc5e --- /dev/null +++ b/boards/arm/mppt_2420_hc/pinmux.c @@ -0,0 +1,62 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2020 Martin Jäger / Libre Solar + */ + +#include +#include +#include +#include +#include + +#include + +/* this should normally go to drivers/pinmux/stm32/pinmux_stm32g4x.h */ +#define STM32G4X_PINMUX_FUNC_PB8_PWM8_CH2 \ + (STM32_PINMUX_ALT_FUNC_10 | STM32_PUSHPULL_NOPULL) + +#define STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1 \ + (STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL) +#define STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N \ + (STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL) + +static const struct pin_config pinconf[] = { + /* USART1 */ + {STM32_PIN_PA9, STM32G4X_PINMUX_FUNC_PA9_USART1_TX}, + {STM32_PIN_PA10, STM32G4X_PINMUX_FUNC_PA10_USART1_RX}, + /* TIM1_CH1 and TIM1_CH1N */ + {STM32_PIN_PA8, STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1}, + {STM32_PIN_PB13, STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N}, + /* PWM for charge pump */ + {STM32_PIN_PB8, STM32G4X_PINMUX_FUNC_PB8_PWM8_CH2}, +#if DT_NODE_HAS_STATUS(DT_NODELABEL(uart2), okay) && CONFIG_SERIAL + {STM32_PIN_PA2, STM32G4X_PINMUX_FUNC_PA2_USART2_TX}, + {STM32_PIN_PA3, STM32G4X_PINMUX_FUNC_PA3_USART2_RX}, +#endif +#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C + {STM32_PIN_PA15, STM32G4X_PINMUX_FUNC_PA15_I2C1_SCL}, + {STM32_PIN_PB7, STM32G4X_PINMUX_FUNC_PB7_I2C1_SDA}, +#endif +#if DT_NODE_HAS_STATUS(DT_NODELABEL(spi1), okay) && CONFIG_SPI + {STM32_PIN_PB3, STM32G4X_PINMUX_FUNC_PB3_SPI1_SCK}, + {STM32_PIN_PB4, STM32G4X_PINMUX_FUNC_PB4_SPI1_MISO}, + {STM32_PIN_PB5, STM32G4X_PINMUX_FUNC_PB5_SPI1_MOSI}, +#endif +#if DT_NODE_HAS_STATUS(DT_NODELABEL(can1), okay) && CONFIG_CAN + {STM32_PIN_PA11, STM32G4X_PINMUX_FUNC_PA11_FDCAN1_RX}, + {STM32_PIN_PA12, STM32G4X_PINMUX_FUNC_PA12_FDCAN1_TX}, +#endif +}; + +static int pinmux_stm32_init(const 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/mppt_2420_hc.json b/boards/mppt_2420_hc.json new file mode 100644 index 0000000..bf14c5a --- /dev/null +++ b/boards/mppt_2420_hc.json @@ -0,0 +1,42 @@ +{ + "build": { + "cpu": "cortex-m4", + "extra_flags": "-DSTM32G4xx -DSTM32G431xx", + "f_cpu": "150000000L", + "mcu": "stm32g431rb", + "variant": "MPPT-2420-HC" + }, + "connectivity": [ + "can" + ], + "debug": { + "default_tools": [ + "jlink" + ], + "jlink_device": "STM32G431RB", + "onboard_tools": [ + "stlink" + ], + "openocd_board": "st_nucleo_g431rb", + "svd_path": "STM32G431xx.svd" + }, + "frameworks": [ + "arduino", + "stm32cube", + "zephyr" + ], + "name": "Libre Solar MPPT 2420 HC", + "upload": { + "maximum_ram_size": 32768, + "maximum_size": 131072, + "protocol": "jlink", + "protocols": [ + "jlink", + "stlink", + "blackmagic", + "mbed" + ] + }, + "url": "https://libre.solar/", + "vendor": "Libre Solar" +} diff --git a/check.bat b/check.bat index d7529f0..ed93ed8 100644 --- a/check.bat +++ b/check.bat @@ -17,7 +17,7 @@ if errorlevel 1 ( ) echo "---------- Running compile check -------------" -platformio run -e mppt_2420_lc -e mppt_1210_hus -e mppt_2420_hpx -e pwm_2420_lus +platformio run -e mppt_2420_lc -e mppt_1210_hus -e mppt_2420_hc -e pwm_2420_lus if errorlevel 1 ( echo Failure Reason is %errorlevel% exit /b %errorlevel% diff --git a/check.sh b/check.sh index 2fab31d..ff7e07d 100755 --- a/check.sh +++ b/check.sh @@ -17,7 +17,7 @@ if [ $? != 0 ]; then fi echo "---------- Running compile check -------------" -platformio run -e mppt_2420_lc -e mppt_1210_hus -e mppt_2420_hpx -e pwm_2420_lus +platformio run -e mppt_2420_lc -e mppt_1210_hus -e mppt_2420_hc -e pwm_2420_lus if [ $? != 0 ]; then exit 1; fi diff --git a/platformio.ini b/platformio.ini index a0d8fbd..ba4a832 100644 --- a/platformio.ini +++ b/platformio.ini @@ -67,6 +67,10 @@ board = mppt_2420_lc [env:mppt_2420_rc] board = mppt_2420_rc +# https://github.com/LibreSolar/mppt-2420-hc +[env:mppt_2420_hc] +board = mppt_2420_hc + # https://github.com/LibreSolar/MPPT-1210-HUS [env:mppt_1210_hus] board = mppt_1210_hus diff --git a/src/load_driver.c b/src/load_driver.c index 5b6a0d6..4990d1c 100644 --- a/src/load_driver.c +++ b/src/load_driver.c @@ -191,6 +191,20 @@ void usb_out_set(bool status) #define CP_PWM_PERIOD (DT_PHA(DT_CHILD(DT_PATH(outputs), charge_pump), pwms, period)) #define CP_PWM_CHANNEL (DT_PHA(DT_CHILD(DT_PATH(outputs), charge_pump), pwms, channel)) +#if CP_PWM_CHANNEL == 1 +#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH1 +#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH1 +#elif CP_PWM_CHANNEL == 2 +#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH2 +#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH2 +#elif CP_PWM_CHANNEL == 3 +#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH3 +#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH3 +#elif CP_PWM_CHANNEL == 4 +#define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH4 +#define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH4 +#endif + /* Currently hard-coded for TIM8 as Zephyr driver doesn't work with this timer at the moment */ void load_cp_enable() { @@ -201,9 +215,9 @@ void load_cp_enable() // Set timer clock to 100 kHz LL_TIM_SetPrescaler(TIM8, SystemCoreClock / 100000 - 1); - LL_TIM_OC_SetMode(TIM8, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_PWM1); - LL_TIM_OC_EnablePreload(TIM8, LL_TIM_CHANNEL_CH1); - LL_TIM_OC_SetPolarity(TIM8, LL_TIM_CHANNEL_CH1, LL_TIM_OCPOLARITY_HIGH); + LL_TIM_OC_SetMode(TIM8, LL_TIM_CHANNEL, LL_TIM_OCMODE_PWM1); + LL_TIM_OC_EnablePreload(TIM8, LL_TIM_CHANNEL); + LL_TIM_OC_SetPolarity(TIM8, LL_TIM_CHANNEL, LL_TIM_OCPOLARITY_HIGH); // Interrupt on timer update LL_TIM_EnableIT_UPDATE(TIM8); @@ -219,9 +233,9 @@ void load_cp_enable() LL_TIM_EnableCounter(TIM8); - LL_TIM_OC_SetCompareCH1(TIM8, _pwm_resolution / 2); // 50% duty + LL_TIM_OC_SetCompare(TIM8, _pwm_resolution / 2); // 50% duty - LL_TIM_CC_EnableChannel(TIM8, LL_TIM_CHANNEL_CH1); + LL_TIM_CC_EnableChannel(TIM8, LL_TIM_CHANNEL); LL_TIM_EnableAllOutputs(TIM8);