From 24f751b2e52e8a862760110f90a10a6480a96855 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Thu, 23 Jan 2020 14:17:30 +0100 Subject: [PATCH] Initial support of MPPT 2420 HPX board - Some flags changed to make the firmware compile for new STM32G431 MCU - Zephyr board definition added - Serial, OLED display and LED working - ADC compiling, but not yet working --- boards/arm/mppt_2420_hpx/CMakeLists.txt | 7 + boards/arm/mppt_2420_hpx/Kconfig | 5 + boards/arm/mppt_2420_hpx/Kconfig.board | 6 + boards/arm/mppt_2420_hpx/Kconfig.defconfig | 58 ++++++++ boards/arm/mppt_2420_hpx/board.cmake | 7 + boards/arm/mppt_2420_hpx/dts_fixup.h | 19 +++ boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts | 131 ++++++++++++++++++ boards/arm/mppt_2420_hpx/mppt_2420_hpx.yaml | 18 +++ .../arm/mppt_2420_hpx/mppt_2420_hpx_defconfig | 58 ++++++++ boards/arm/mppt_2420_hpx/pinmux.c | 87 ++++++++++++ boards/mppt_2420_hpx.json | 42 ++++++ platformio.ini | 14 ++ src/eeprom.cpp | 2 +- src/half_bridge.cpp | 43 +++--- src/load.cpp | 2 + src/load_drv.cpp | 2 + src/mcu.h | 16 +++ src/pcb.h | 2 + src/pcbs/mppt_2420_hpx_0v1.h | 107 ++++++++++++++ src/pcbs/pcb_stub.h | 1 - 20 files changed, 600 insertions(+), 27 deletions(-) create mode 100644 boards/arm/mppt_2420_hpx/CMakeLists.txt create mode 100644 boards/arm/mppt_2420_hpx/Kconfig create mode 100644 boards/arm/mppt_2420_hpx/Kconfig.board create mode 100644 boards/arm/mppt_2420_hpx/Kconfig.defconfig create mode 100644 boards/arm/mppt_2420_hpx/board.cmake create mode 100644 boards/arm/mppt_2420_hpx/dts_fixup.h create mode 100644 boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts create mode 100644 boards/arm/mppt_2420_hpx/mppt_2420_hpx.yaml create mode 100644 boards/arm/mppt_2420_hpx/mppt_2420_hpx_defconfig create mode 100644 boards/arm/mppt_2420_hpx/pinmux.c create mode 100644 boards/mppt_2420_hpx.json create mode 100644 src/pcbs/mppt_2420_hpx_0v1.h diff --git a/boards/arm/mppt_2420_hpx/CMakeLists.txt b/boards/arm/mppt_2420_hpx/CMakeLists.txt new file mode 100644 index 0000000..d1b8108 --- /dev/null +++ b/boards/arm/mppt_2420_hpx/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_hpx/Kconfig b/boards/arm/mppt_2420_hpx/Kconfig new file mode 100644 index 0000000..abfb339 --- /dev/null +++ b/boards/arm/mppt_2420_hpx/Kconfig @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2019 Martin Jäger / Libre Solar + +# include generic charge controller Kconfig +rsource "../../Kconfig" diff --git a/boards/arm/mppt_2420_hpx/Kconfig.board b/boards/arm/mppt_2420_hpx/Kconfig.board new file mode 100644 index 0000000..0c98f23 --- /dev/null +++ b/boards/arm/mppt_2420_hpx/Kconfig.board @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2019 Martin Jäger / Libre Solar + +config BOARD_MPPT_2420_HPX + bool "Libre Solar MPPT 2420 HPX" + depends on SOC_STM32G431XX diff --git a/boards/arm/mppt_2420_hpx/Kconfig.defconfig b/boards/arm/mppt_2420_hpx/Kconfig.defconfig new file mode 100644 index 0000000..ea4c886 --- /dev/null +++ b/boards/arm/mppt_2420_hpx/Kconfig.defconfig @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2019 Martin Jäger / Libre Solar + +if BOARD_MPPT_2420_HPX + +config BOARD + default "mppt_2420_hpx" + +config HAS_DCDC_CONVERTER + default y + +config DCDC_PWM_FREQUENCY_KHZ + default 50 + +config DCDC_PWM_DEADTIME_NS + default 230 + +config HAS_LOAD_OUTPUT + default y + +#config HAS_PWM_SWITCH +# default y + +config NUM_LED_PINS + default 1 + +if UART_CONSOLE + +config UART_1 + default y + +endif # UART_CONSOLE + +if I2C + +config I2C_1 + default y + +endif # I2C + +if PWM + +config PWM_STM32_2 + default y + +endif # PWM + +if SPI + +config SPI_1 + default y + +config SPI_STM32_INTERRUPT + default y + +endif # SPI + +endif # BOARD_MPPT_2420_HPX diff --git a/boards/arm/mppt_2420_hpx/board.cmake b/boards/arm/mppt_2420_hpx/board.cmake new file mode 100644 index 0000000..9def324 --- /dev/null +++ b/boards/arm/mppt_2420_hpx/board.cmake @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: Apache-2.0 + +board_runner_args(pyocd "--target=stm32g431rbtx") +board_runner_args(jlink "--device=STM32G431RB" "--speed=4000") + +include(${ZEPHYR_BASE}/boards/common/pyocd.board.cmake) +include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake) diff --git a/boards/arm/mppt_2420_hpx/dts_fixup.h b/boards/arm/mppt_2420_hpx/dts_fixup.h new file mode 100644 index 0000000..bfc0aa4 --- /dev/null +++ b/boards/arm/mppt_2420_hpx/dts_fixup.h @@ -0,0 +1,19 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2020 Martin Jäger / Libre Solar + */ + +#define DT_ADC_1_BASE_ADDRESS DT_ST_STM32_ADC_50000000_BASE_ADDRESS +#define DT_ADC_1_IRQ DT_ST_STM32_ADC_50000000_IRQ_0 +#define DT_ADC_1_IRQ_PRI DT_ST_STM32_ADC_50000000_IRQ_0_PRIORITY +#define DT_ADC_1_NAME DT_ST_STM32_ADC_50000000_LABEL +#define DT_ADC_1_CLOCK_BITS DT_ST_STM32_ADC_50000000_CLOCK_BITS_0 +#define DT_ADC_1_CLOCK_BUS DT_ST_STM32_ADC_50000000_CLOCK_BUS_0 + +#define DT_ADC_2_BASE_ADDRESS DT_ST_STM32_ADC_50000800_BASE_ADDRESS +#define DT_ADC_2_IRQ DT_ST_STM32_ADC_50000800_IRQ_0 +#define DT_ADC_2_IRQ_PRI DT_ST_STM32_ADC_50000800_IRQ_0_PRIORITY +#define DT_ADC_2_NAME DT_ST_STM32_ADC_50000800_LABEL +#define DT_ADC_2_CLOCK_BITS DT_ST_STM32_ADC_50000800_CLOCK_BITS_0 +#define DT_ADC_2_CLOCK_BUS DT_ST_STM32_ADC_50000800_CLOCK_BUS_0 diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts new file mode 100644 index 0000000..fd42308 --- /dev/null +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts @@ -0,0 +1,131 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2020 Martin Jäger / Libre Solar + */ + +/dts-v1/; +#include + +/ { + model = "Libre Solar MPPT 2420 HPX v0.1"; + compatible = "st,stm32g431"; + + soc { + adc1: adc@50000000 { + compatible = "st,stm32-adc"; + reg = <0x50000000 0x400>; + clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00002000>; + interrupts = <18 0>; + status = "disabled"; + label = "ADC_1"; + #io-channel-cells = <1>; + }; + + adc2: adc@50000100 { + compatible = "st,stm32-adc"; + reg = <0x50000100 0x400>; + clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00002000>; + interrupts = <18 0>; + status = "disabled"; + label = "ADC_2"; + #io-channel-cells = <1>; + }; + + 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>; + }; + }; + + chosen { + zephyr,console = &usart1; + zephyr,shell-uart = &usart1; + zephyr,sram = &sram0; + zephyr,flash = &flash0; + }; + + leds { + compatible = "gpio-leds"; + pwr { + gpios = <&gpioc 15 GPIO_INT_ACTIVE_HIGH>; + label = "Power LED"; + }; + }; + + switches { + compatible = "switch"; + load-dis { + gpios = <&gpiob 15 GPIO_DIR_IN>; + label = "Load 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"; +}; + +&lpuart1 { + current-speed = <115200>; + status = "okay"; +}; + +&i2c1 { + status = "okay"; +}; + +&spi1 { + 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>; + + /* Set 4Kb of storage at the end of the 128Kb of flash */ + storage_partition: partition@1f000 { + label = "storage"; + reg = <0x0001f000 0x00001000>; + }; + }; +}; + +&iwdg { + status = "okay"; +}; + +&adc1 { + status = "okay"; +}; diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.yaml b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.yaml new file mode 100644 index 0000000..cd86b26 --- /dev/null +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.yaml @@ -0,0 +1,18 @@ +identifier: mppt_2420_hpx +name: MPPT-2420-HPX +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_hpx/mppt_2420_hpx_defconfig b/boards/arm/mppt_2420_hpx/mppt_2420_hpx_defconfig new file mode 100644 index 0000000..6e3b8e9 --- /dev/null +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx_defconfig @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: Apache-2.0 + +CONFIG_ARM=y +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 diff --git a/boards/arm/mppt_2420_hpx/pinmux.c b/boards/arm/mppt_2420_hpx/pinmux.c new file mode 100644 index 0000000..5e7e750 --- /dev/null +++ b/boards/arm/mppt_2420_hpx/pinmux.c @@ -0,0 +1,87 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2020 Martin Jäger / Libre Solar + */ + +#include +#include +#include +#include +#include + +#include + +static const struct pin_config pinconf[] = { +#ifdef CONFIG_UART_1 + {STM32_PIN_PA9, STM32G4X_PINMUX_FUNC_PA9_USART1_TX}, + {STM32_PIN_PA10, STM32G4X_PINMUX_FUNC_PA10_USART1_RX}, +#endif /* CONFIG_UART_1 */ +#ifdef CONFIG_LPUART_1 + {STM32_PIN_PA2, STM32G4X_PINMUX_FUNC_PA2_LPUART1_TX}, + {STM32_PIN_PA3, STM32G4X_PINMUX_FUNC_PA3_LPUART1_RX}, +#endif /* CONFIG_LPUART_1 */ +#ifdef CONFIG_UART_2 + {STM32_PIN_PA2, STM32G4X_PINMUX_FUNC_PA2_USART2_TX}, + {STM32_PIN_PA15, STM32G4X_PINMUX_FUNC_PA15_USART2_RX}, +#endif /* CONFIG_UART_2 */ +#ifdef CONFIG_I2C_1 + {STM32_PIN_PB8, STM32G4X_PINMUX_FUNC_PB8_I2C1_SCL}, + {STM32_PIN_PB9, STM32G4X_PINMUX_FUNC_PB9_I2C1_SDA}, +#endif /* CONFIG_I2C_1 */ +#ifdef CONFIG_PWM_STM32_2 + {STM32_PIN_PA5, STM32G4X_PINMUX_FUNC_PA5_PWM2_CH1}, +#endif /* CONFIG_PWM_STM32_2 */ +#ifdef CONFIG_SPI_1 + /* SPI1 on the Arduino connectors pins A2, D3, D11, D12 */ +#ifdef CONFIG_SPI_STM32_USE_HW_SS + {STM32_PIN_PA4, STM32G4X_PINMUX_FUNC_PA4_SPI1_NSS}, +#endif /* CONFIG_SPI_STM32_USE_HW_SS */ + /* SPI1_SCK should output on PA5, but is used for LD2 */ + {STM32_PIN_PB3, STM32G4X_PINMUX_FUNC_PB3_SPI1_SCK}, + {STM32_PIN_PA6, STM32G4X_PINMUX_FUNC_PA6_SPI1_MISO}, + {STM32_PIN_PA7, STM32G4X_PINMUX_FUNC_PA7_SPI1_MOSI}, +#endif /* CONFIG_SPI_1 */ +#ifdef CONFIG_SPI_2 + /* SPI2 on the ST Morpho Connector CN10 pins 16, 30, 28, 26*/ +#ifdef CONFIG_SPI_STM32_USE_HW_SS + {STM32_PIN_PB12, STM32G4X_PINMUX_FUNC_PB12_SPI2_NSS}, +#endif /* CONFIG_SPI_STM32_USE_HW_SS */ + {STM32_PIN_PB13, STM32G4X_PINMUX_FUNC_PB13_SPI2_SCK}, + {STM32_PIN_PB14, STM32G4X_PINMUX_FUNC_PB14_SPI2_MISO}, + {STM32_PIN_PB15, STM32G4X_PINMUX_FUNC_PB15_SPI2_MOSI}, +#endif /* CONFIG_SPI_1 */ +#ifdef CONFIG_SPI_3 + /* SPI3 on the ST Morpho Connector CN7 pins 17, 1, 2, 3*/ +#ifdef CONFIG_SPI_STM32_USE_HW_SS + {STM32_PIN_PA15, STM32G4X_PINMUX_FUNC_PA15_SPI3_NSS}, +#endif /* CONFIG_SPI_STM32_USE_HW_SS */ + {STM32_PIN_PC10, STM32G4X_PINMUX_FUNC_PC10_SPI3_SCK}, + {STM32_PIN_PC11, STM32G4X_PINMUX_FUNC_PC11_SPI3_MISO}, + {STM32_PIN_PC12, STM32G4X_PINMUX_FUNC_PC12_SPI3_MOSI}, +#endif /* CONFIG_SPI_3 */ +#ifdef CONFIG_USB_DC_STM32 + {STM32_PIN_PA11, STM32G4X_PINMUX_FUNC_PA11_USB_DM}, + {STM32_PIN_PA12, STM32G4X_PINMUX_FUNC_PA12_USB_DP}, +#endif /* CONFIG_USB_DC_STM32 */ +}; + +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, +}; +const int led_pins[CONFIG_NUM_LED_PINS] = { + DT_GPIO_LEDS_PWR_GPIOS_PIN, +}; diff --git a/boards/mppt_2420_hpx.json b/boards/mppt_2420_hpx.json new file mode 100644 index 0000000..7536850 --- /dev/null +++ b/boards/mppt_2420_hpx.json @@ -0,0 +1,42 @@ +{ + "build": { + "cpu": "cortex-m4", + "extra_flags": "-DSTM32G4xx -DSTM32G431xx", + "f_cpu": "150000000L", + "mcu": "stm32g431rb", + "variant": "MPPT-2420-HPX" + }, + "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 HPX v0.1", + "upload": { + "maximum_ram_size": 32768, + "maximum_size": 131072, + "protocol": "jlink", + "protocols": [ + "jlink", + "stlink", + "blackmagic", + "mbed" + ] + }, + "url": "http://libre.solar/", + "vendor": "Libre Solar" +} diff --git a/platformio.ini b/platformio.ini index f80500b..7c7143c 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-hpx-v0.1-zephyr # mppt-1210-hus-v0.2 # mppt-1210-hus-v0.4 # mppt-1210-hus-v0.6 @@ -39,6 +40,8 @@ framework = mbed upload_protocol = stlink #upload_protocol = jlink +debug_tool = jlink + # Custom upload tools (overwrites upload_protocol) #upload_command = st-flash write $SOURCE 0x8000000 #upload_command = dfu-util -d 0483:df11 -a 0 -s 0x08000000:leave -D $SOURCE @@ -110,6 +113,17 @@ platform = https://github.com/platformio/platform-ststm32.git lib_ignore = ESP32 extra_scripts = generate_version_file.py +[env:mppt-2420-hpx-v0.1-zephyr] +board = mppt_2420_hpx +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/PWM-2420-LUS [env:pwm-2420-lus-v0.2] board = nucleo_l073rz diff --git a/src/eeprom.cpp b/src/eeprom.cpp index c1de962..04f7d56 100644 --- a/src/eeprom.cpp +++ b/src/eeprom.cpp @@ -36,7 +36,7 @@ const uint16_t eeprom_data_objects[] = { 0xA6 // day count }; -#ifndef UNIT_TEST +#if !defined(UNIT_TEST) && (defined(STM32F0) || defined(STM32L0)) uint32_t _calc_crc(const uint8_t *buf, size_t len) { diff --git a/src/half_bridge.cpp b/src/half_bridge.cpp index 595565b..75af010 100644 --- a/src/half_bridge.cpp +++ b/src/half_bridge.cpp @@ -10,15 +10,16 @@ #include "pcb.h" #include "config.h" +#include + static int _pwm_resolution; static float _min_duty; static float _max_duty; static uint8_t _deadtime_clocks; - static bool _enabled; -#ifndef UNIT_TEST +#if defined(STM32L0) class PWM_TIM3 { @@ -27,23 +28,14 @@ class PWM_TIM3 static void _init_registers(uint32_t pwm_resolution) { // Enable peripheral clock of GPIOB -#if defined(STM32F0) - RCC->AHBENR |= RCC_AHBENR_GPIOBEN; -#elif defined(STM32L0) RCC->IOPENR |= RCC_IOPENR_IOPBEN; -#endif // Enable TIM3 clock RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // Select alternate function mode on PB0 and PB1 (first bit _1 = 1, second bit _0 = 0) -#if defined(STM32F0) - GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER0)) | GPIO_MODER_MODER0_1; - GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER1)) | GPIO_MODER_MODER1_1; -#elif defined(STM32L0) GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE0)) | GPIO_MODER_MODE0_1; GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE1)) | GPIO_MODER_MODE1_1; -#endif // Select AF2 on PB0 and PB1 #ifdef __MBED__ @@ -114,7 +106,8 @@ class PWM_TIM3 } }; -#if defined(STM32F0) +#elif defined(STM32F0) + class PWM_TIM1 { public: @@ -200,17 +193,12 @@ class PWM_TIM1 TIM1->CCR1 = val; } }; -#endif -#if (PWM_TIM == 1) -typedef PWM_TIM1 PWM_TIM_HW; -#elif (PWM_TIM == 3) -typedef PWM_TIM3 PWM_TIM_HW; -#endif - -#else /* #ifndef UNIT_TEST */ +#else /* UNIT_TEST */ +#ifdef UNIT_TEST const uint32_t SystemCoreClock = 24000000; +#endif class PWM_UT { @@ -249,10 +237,16 @@ uint32_t PWM_UT::m_pwm_resolution = 0; bool PWM_UT::m_started = false; uint32_t PWM_UT::m_ccr = 0; -typedef PWM_UT PWM_TIM_HW; - #endif /* UNIT_TEST */ +#if (PWM_TIM == 1) +typedef PWM_TIM1 PWM_TIM_HW; +#elif (PWM_TIM == 3) +typedef PWM_TIM3 PWM_TIM_HW; +#else +typedef PWM_UT PWM_TIM_HW; +#endif + void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_duty) { // timers are counting at system clock @@ -265,9 +259,8 @@ void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_d // be handled nicely, parentheses make this more explicit _deadtime_clocks = ((SystemCoreClock / (1000000)) * deadtime_ns) / 1000; + // set PWM frequency and resolution (in fact half the resolution) PWM_TIM_HW::_init_registers(_pwm_resolution); - // set PWM frequency and resolution (in fact half the resolution) - _min_duty = min_duty * _pwm_resolution; _max_duty = max_duty * _pwm_resolution; @@ -294,7 +287,7 @@ void half_bridge_set_duty_cycle(float duty) { _half_bridge_set_duty_cycle(_pwm_resolution * duty); } -#include + void half_bridge_duty_cycle_step(int delta) { _half_bridge_set_duty_cycle(PWM_TIM_HW::get_ccr() + delta); diff --git a/src/load.cpp b/src/load.cpp index 4f8ea18..1922d0a 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -208,7 +208,9 @@ void LoadOutput::stop(uint32_t error_flag) // flicker the load LED if failure was most probably caused by the user if (error_flag & (ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT)) { +#ifdef LED_LOAD leds_flicker(LED_LOAD); +#endif oc_timestamp = uptime(); } } diff --git a/src/load_drv.cpp b/src/load_drv.cpp index 68daed7..cbc8ba8 100644 --- a/src/load_drv.cpp +++ b/src/load_drv.cpp @@ -155,7 +155,9 @@ extern "C" void ADC1_COMP_IRQHandler(void) void LoadOutput::switch_set(bool status) { pgood = status; +#ifdef LED_LOAD leds_set(LED_LOAD, status); +#endif #if defined(__MBED__) && defined(PIN_LOAD_EN) DigitalOut load_enable(PIN_LOAD_EN); diff --git a/src/mcu.h b/src/mcu.h index 7f67eb4..826a064 100644 --- a/src/mcu.h +++ b/src/mcu.h @@ -19,6 +19,11 @@ #define STM32F072 #endif +#ifdef CONFIG_SOC_STM32G431XX +#define STM32G4 +#define STM32G431 +#endif + #ifdef STM32L073 #include "stm32l073xx.h" #endif @@ -27,6 +32,10 @@ #include "stm32f072xx.h" #endif +#ifdef STM32G431 +#include "stm32g431xx.h" +#endif + // factory calibration values for internal voltage reference and temperature sensor // (see MCU datasheet, not Reference Manual) #if defined(STM32F0) @@ -43,6 +52,13 @@ const uint16_t TSENSE_CAL2 = *((uint16_t *)0x1FF8007E); #define TSENSE_CAL1_VALUE 30.0 // temperature of first calibration point #define TSENSE_CAL2_VALUE 130.0 // temperature of second calibration point +#elif defined(STM32G4) + const uint16_t VREFINT_CAL = *((uint16_t *)0x1FFF75AA); // VREFINT @3.0V/30°C + #define VREFINT_VALUE 3000 // mV + const uint16_t TSENSE_CAL1 = *((uint16_t *)0x1FFF75A8); + const uint16_t TSENSE_CAL2 = *((uint16_t *)0x1FFF75CA); + #define TSENSE_CAL1_VALUE 30.0 // temperature of first calibration point + #define TSENSE_CAL2_VALUE 110.0 // temperature of second calibration point #elif defined(UNIT_TEST) #define VREFINT_CAL (4096 * 1.224 / 3.0) // VREFINT @3.0V/25°C #define VREFINT_VALUE 3000 // mV diff --git a/src/pcb.h b/src/pcb.h index 112dac5..097bc97 100644 --- a/src/pcb.h +++ b/src/pcb.h @@ -24,6 +24,8 @@ #include "pcbs/mppt_1210_hus_0v4.h" #elif defined(CONFIG_BOARD_MPPT_1210_HUS_0V7) #include "pcbs/mppt_1210_hus_0v7.h" +#elif defined(CONFIG_BOARD_MPPT_2420_HPX) + #include "pcbs/mppt_2420_hpx_0v1.h" #elif defined(CONFIG_BOARD_PWM_2420_LUS_0V2) || defined(CONFIG_BOARD_PWM_2420_LUS_0V3) #include "pcbs/pwm_2420_lus.h" #elif defined(UNIT_TEST) diff --git a/src/pcbs/mppt_2420_hpx_0v1.h b/src/pcbs/mppt_2420_hpx_0v1.h new file mode 100644 index 0000000..953b4ca --- /dev/null +++ b/src/pcbs/mppt_2420_hpx_0v1.h @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2020 Martin Jäger / Libre Solar + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef MPPT_1210_HUS_0V4_H +#define MPPT_1210_HUS_0V4_H + +#ifdef __MBED__ +#include "mbed.h" +#elif defined(__ZEPHYR__) +#include +#endif + +#define DEVICE_TYPE "MPPT-1210-HUS" +#define HARDWARE_VERSION "v0.4" + +// specify features of charge controller +#define CONFIG_HAS_DCDC_CONVERTER 1 +#define CONFIG_HAS_PWM_SWITCH 0 +#define CONFIG_HAS_LOAD_OUTPUT 1 + +// DC/DC converter settings +#define PWM_FREQUENCY 50 // kHz 50 = better for cloud solar to increase efficiency +#define PWM_DEADTIME 230 // ns +//#define PWM_TIM 1 // use TIM1 timer + +#define DCDC_CURRENT_MAX 10 // PCB maximum DCDC output current +#define LOAD_CURRENT_MAX 10 // PCB maximum load switch current + +#define LOW_SIDE_VOLTAGE_MAX 16 // Maximum voltage at battery port (V) +#define HIGH_SIDE_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V) + +#define PIN_UEXT_TX PA_2 +#define PIN_UEXT_RX PA_3 +#define PIN_UEXT_SCL PB_6 +#define PIN_UEXT_SDA PB_7 +#define PIN_UEXT_MISO PB_4 +#define PIN_UEXT_MOSI PB_5 +#define PIN_UEXT_SCK PB_3 +#define PIN_UEXT_SSEL PA_15 + +#define PIN_SWD_TX PA_9 +#define PIN_SWD_RX PA_10 + +#define PIN_LOAD_EN PC_13 +#define PIN_USB_PWR_EN PB_12 + +#define PIN_REF_I_DCDC PA_4 + +enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; + +// assignment LED numbers on PCB to their meaning +#define NUM_LEDS 1 + +#define LED_PWR 0 // LED1 + +// LED pins and pin state configuration to switch above LEDs on +#define NUM_LED_PINS 1 + +// defined in board definition pinmux.c +extern const char *led_ports[CONFIG_NUM_LED_PINS]; +extern const int led_pins[CONFIG_NUM_LED_PINS]; + +const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { + { PIN_HIGH }, // LED1 +}; + +// pin definition only needed in adc_dma.cpp to detect if they are present on the PCB +#define PIN_ADC_TEMP_FETS PA_5 + +// typical value for Semitec 103AT-5 thermistor: 3435 +#define NTC_BETA_VALUE 3435 +#define NTC_SERIES_RESISTOR 10000.0 + +#define ADC_GAIN_V_BAT (105.6 / 5.6) // both voltage dividers: 100k + 5.6k +#define ADC_GAIN_V_SOLAR (105.6 / 5.6) +#define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm +#define ADC_GAIN_I_DCDC (1000 / 4 / 50) + +// position in the array written by the DMA controller +enum { + ADC_POS_V_BAT, // ADC 0 (PA_0) + ADC_POS_V_SOLAR, // ADC 1 (PA_1) + ADC_POS_TEMP_FETS, // ADC 5 (PA_5) + ADC_POS_I_DCDC, // ADC 7 (PA_7) + ADC_POS_I_LOAD, // ADC 6 (PA_6) + ADC_POS_VREF_MCU, // ADC 17 + ADC_POS_TEMP_MCU, // ADC 16 + NUM_ADC_CH // trick to get the number of elements +}; + +// selected ADC 1 channels (has to match with above enum) +#define NUM_ADC_1_CH 5 + +const uint32_t adc_1_sequence[] = { + LL_ADC_CHANNEL_12, // V_BAT + LL_ADC_CHANNEL_15, // V_SOLAR + LL_ADC_CHANNEL_6, // TEMP_FETS + LL_ADC_CHANNEL_1, // I_DCDC + LL_ADC_CHANNEL_2, // I_LOAD + LL_ADC_CHANNEL_VREFINT, // VREF_MCU + LL_ADC_CHANNEL_TEMPSENSOR_ADC1, // TEMP_MCU +}; + +#endif diff --git a/src/pcbs/pcb_stub.h b/src/pcbs/pcb_stub.h index c37f5d8..c4d54fc 100644 --- a/src/pcbs/pcb_stub.h +++ b/src/pcbs/pcb_stub.h @@ -17,7 +17,6 @@ #define PWM_FREQUENCY 50 // kHz #define PWM_DEADTIME 230 // ns -#define PWM_TIM 3 // use TIM3 timer #define PWM_CURRENT_MAX 15 // PCB maximum PWM switch (solar) current #define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current