From cb988639cecae1ef2490c0a39fe16e534da4e57a Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Thu, 9 Jan 2020 13:56:02 +0100 Subject: [PATCH] Zephyr: Added PWM 2420 LUS board --- boards/arm/pwm_2420_lus_0v3/CMakeLists.txt | 7 ++ boards/arm/pwm_2420_lus_0v3/Kconfig | 5 ++ boards/arm/pwm_2420_lus_0v3/Kconfig.board | 6 ++ boards/arm/pwm_2420_lus_0v3/Kconfig.defconfig | 42 ++++++++++ boards/arm/pwm_2420_lus_0v3/board.cmake | 6 ++ boards/arm/pwm_2420_lus_0v3/pinmux.c | 67 +++++++++++++++ .../arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts | 84 +++++++++++++++++++ .../pwm_2420_lus_0v3/pwm_2420_lus_0v3.yaml | 17 ++++ .../pwm_2420_lus_0v3_defconfig | 37 ++++++++ .../arm/pwm_2420_lus_0v3/support/openocd.cfg | 27 ++++++ boards/pwm_2420_lus_0v3.json | 43 ++++++++++ platformio.ini | 11 +++ src/load.cpp | 8 +- src/pcbs/mppt_1210_hus_0v4.h | 2 +- src/pcbs/pwm_2420_lus.h | 15 +++- src/pwm_switch.cpp | 2 +- 16 files changed, 373 insertions(+), 6 deletions(-) create mode 100644 boards/arm/pwm_2420_lus_0v3/CMakeLists.txt create mode 100644 boards/arm/pwm_2420_lus_0v3/Kconfig create mode 100644 boards/arm/pwm_2420_lus_0v3/Kconfig.board create mode 100644 boards/arm/pwm_2420_lus_0v3/Kconfig.defconfig create mode 100644 boards/arm/pwm_2420_lus_0v3/board.cmake create mode 100644 boards/arm/pwm_2420_lus_0v3/pinmux.c create mode 100644 boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts create mode 100644 boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.yaml create mode 100644 boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3_defconfig create mode 100644 boards/arm/pwm_2420_lus_0v3/support/openocd.cfg create mode 100644 boards/pwm_2420_lus_0v3.json diff --git a/boards/arm/pwm_2420_lus_0v3/CMakeLists.txt b/boards/arm/pwm_2420_lus_0v3/CMakeLists.txt new file mode 100644 index 0000000..d1b8108 --- /dev/null +++ b/boards/arm/pwm_2420_lus_0v3/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/pwm_2420_lus_0v3/Kconfig b/boards/arm/pwm_2420_lus_0v3/Kconfig new file mode 100644 index 0000000..abfb339 --- /dev/null +++ b/boards/arm/pwm_2420_lus_0v3/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/pwm_2420_lus_0v3/Kconfig.board b/boards/arm/pwm_2420_lus_0v3/Kconfig.board new file mode 100644 index 0000000..76e35d0 --- /dev/null +++ b/boards/arm/pwm_2420_lus_0v3/Kconfig.board @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2019 Martin Jäger / Libre Solar + +config BOARD_PWM_2420_LUS_0V3 + bool "Libre Solar PWM 2420 LUS v0.3" + depends on SOC_STM32L073XX diff --git a/boards/arm/pwm_2420_lus_0v3/Kconfig.defconfig b/boards/arm/pwm_2420_lus_0v3/Kconfig.defconfig new file mode 100644 index 0000000..0882fa4 --- /dev/null +++ b/boards/arm/pwm_2420_lus_0v3/Kconfig.defconfig @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2019 Martin Jäger / Libre Solar + +if BOARD_PWM_2420_LUS_0V3 + +config BOARD + default "pwm_2420_lus_0v3" + +config HAS_PWM_SWITCH + default y + +config HAS_LOAD_OUTPUT + default y + +config NUM_LED_PINS + default 3 + +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_PWM_2420_LUS_0V3 diff --git a/boards/arm/pwm_2420_lus_0v3/board.cmake b/boards/arm/pwm_2420_lus_0v3/board.cmake new file mode 100644 index 0000000..6f20255 --- /dev/null +++ b/boards/arm/pwm_2420_lus_0v3/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/pwm_2420_lus_0v3/pinmux.c b/boards/arm/pwm_2420_lus_0v3/pinmux.c new file mode 100644 index 0000000..7595ac6 --- /dev/null +++ b/boards/arm/pwm_2420_lus_0v3/pinmux.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2018 Ilya Tagunov + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +#include + +// preliminary fix until PR for pinmux_stm32l0.h got accepted and merged to Zephyr master branch, +// see also here: https://github.com/zephyrproject-rtos/zephyr/pull/20997 +#define STM32L0_PINMUX_FUNC_PB6_I2C1_SCL \ + (STM32_PINMUX_ALT_FUNC_1 | STM32_OPENDRAIN_PULLUP) +#define STM32L0_PINMUX_FUNC_PB7_I2C1_SDA \ + (STM32_PINMUX_ALT_FUNC_1 | STM32_OPENDRAIN_PULLUP) + +/* 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_PB6, STM32L0_PINMUX_FUNC_PB6_I2C1_SCL}, + {STM32_PIN_PB7, STM32L0_PINMUX_FUNC_PB7_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); + +const char *led_ports[CONFIG_NUM_LED_PINS] = { + DT_ALIAS_LED_A_GPIOS_CONTROLLER, + DT_ALIAS_LED_B_GPIOS_CONTROLLER, + DT_ALIAS_LED_C_GPIOS_CONTROLLER, +}; +const int led_pins[CONFIG_NUM_LED_PINS] = { + DT_ALIAS_LED_A_GPIOS_PIN, + DT_ALIAS_LED_B_GPIOS_PIN, + DT_ALIAS_LED_C_GPIOS_PIN, +}; diff --git a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts new file mode 100644 index 0000000..f86b915 --- /dev/null +++ b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts @@ -0,0 +1,84 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2019 Martin Jäger / Libre Solar + */ + +/dts-v1/; +#include + +/ { + model = "Libre Solar PWM 2420 LUS v0.3"; + compatible = "st,stm32l073"; + + chosen { + zephyr,console = &usart1; + zephyr,shell-uart = &usart1; + zephyr,sram = &sram0; + zephyr,flash = &flash0; + }; + + leds { + compatible = "gpio-leds"; + led_pin_a: led_pin_a { + gpios = <&gpiob 13 GPIO_INT_ACTIVE_HIGH>; + label = "LED pin A"; + }; + led_pin_b: led_pin_b { + gpios = <&gpiob 14 GPIO_INT_ACTIVE_HIGH>; + label = "LED pin B"; + }; + led_pin_c: led_pin_c { + gpios = <&gpiob 15 GPIO_INT_ACTIVE_HIGH>; + label = "LED pin C"; + }; + // normal GPIO pins also configured as LEDs to be + // able to assign aliases + pin_load_dis: pin_load_dis { + gpios = <&gpiob 2 GPIO_INT_ACTIVE_HIGH>; + label = "Load disable"; + }; + pin_usb_pwr_dis: pin_usb_pwr_dis { + gpios = <&gpiob 5 GPIO_INT_ACTIVE_HIGH>; + label = "USB power disable"; + }; + }; + + aliases { + led-a = &led_pin_a; + led-b = &led_pin_b; + led-c = &led_pin_c; + + /* WARNING: this one is DISable and not ENable... change name + and implement proper handling! */ + load-en = &pin_load_dis; + usb-pwr-en = &pin_usb_pwr_dis; + }; +}; + +&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/pwm_2420_lus_0v3/pwm_2420_lus_0v3.yaml b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.yaml new file mode 100644 index 0000000..017513e --- /dev/null +++ b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.yaml @@ -0,0 +1,17 @@ +identifier: pwm_2420_lus_0v3 +name: PWM-2420-LUS-0V3 +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/pwm_2420_lus_0v3/pwm_2420_lus_0v3_defconfig b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3_defconfig new file mode 100644 index 0000000..f7481bb --- /dev/null +++ b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3_defconfig @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Zephyr Kernel Configuration +CONFIG_ARM=y +CONFIG_SOC_SERIES_STM32L0X=y + +# Platform Configuration +CONFIG_SOC_STM32L073XX=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 + +# PLL configuration +CONFIG_CLOCK_STM32_PLL_SRC_HSI=y +CONFIG_CLOCK_STM32_PLL_MULTIPLIER=4 +CONFIG_CLOCK_STM32_PLL_DIVISOR=2 +CONFIG_CLOCK_STM32_AHB_PRESCALER=1 +CONFIG_CLOCK_STM32_APB1_PRESCALER=1 diff --git a/boards/arm/pwm_2420_lus_0v3/support/openocd.cfg b/boards/arm/pwm_2420_lus_0v3/support/openocd.cfg new file mode 100644 index 0000000..00f68a6 --- /dev/null +++ b/boards/arm/pwm_2420_lus_0v3/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/pwm_2420_lus_0v3.json b/boards/pwm_2420_lus_0v3.json new file mode 100644 index 0000000..0749bca --- /dev/null +++ b/boards/pwm_2420_lus_0v3.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": "PWM-2420-LUS" + }, + "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 PWM 2420 LUS", + "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 56d474a..2a73d5d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -120,6 +120,17 @@ board = nucleo_l073rz build_flags = ${env.build_flags} -D CONFIG_BOARD_PWM_2420_LUS_0V3 +[env:pwm-2420-lus-v0.3-zephyr] +board = pwm_2420_lus_0v3 +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 + [env:pwm-2420-lus-v0.3-bl] board = nucleo_l073rz build_flags = ${env.build_flags} diff --git a/src/load.cpp b/src/load.cpp index 9f72fd6..5f69475 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -13,11 +13,15 @@ #include "device_status.h" #include "debug.h" +#ifdef __ZEPHYR__ +#include +#endif + extern DeviceStatus dev_stat; extern LoadOutput load; // necessary to call emergency stop function -#if defined(PIN_I_LOAD_COMP) && PIN_LOAD_DIS == PB_2 +#if defined(__MBED__) && defined(PIN_I_LOAD_COMP) && PIN_LOAD_DIS == PB_2 static void lptim_init() { // Enable peripheral clock of GPIOB @@ -69,7 +73,7 @@ static void lptim_init() static void short_circuit_comp_init() { -#ifdef PIN_I_LOAD_COMP +#if defined(__MBED__) && defined(PIN_I_LOAD_COMP) MBED_ASSERT(PIN_I_LOAD_COMP == PB_4); // set GPIO pin to analog diff --git a/src/pcbs/mppt_1210_hus_0v4.h b/src/pcbs/mppt_1210_hus_0v4.h index 2cb2c32..d917b81 100644 --- a/src/pcbs/mppt_1210_hus_0v4.h +++ b/src/pcbs/mppt_1210_hus_0v4.h @@ -69,7 +69,7 @@ static const PinName led_pins[NUM_LED_PINS] = { PB_14, PB_13, PB_2, PB_11, PB_10 }; #elif defined(__ZEPHYR__) -// defined in boar definition pinmux.c +// 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 diff --git a/src/pcbs/pwm_2420_lus.h b/src/pcbs/pwm_2420_lus.h index 97f0fdd..d77fb66 100644 --- a/src/pcbs/pwm_2420_lus.h +++ b/src/pcbs/pwm_2420_lus.h @@ -7,6 +7,12 @@ #ifndef PWM_2420_LUS_H #define PWM_2420_LUS_H +#ifdef __MBED__ +#include "mbed.h" +#elif defined(__ZEPHYR__) +#include +#endif + #define DEVICE_TYPE "PWM-2420-LUS" #ifdef CONFIG_BOARD_PWM_2420_LUS_0V2 @@ -15,8 +21,6 @@ #define HARDWARE_VERSION "v0.3" #endif -#include "mbed.h" - // specify features of charge controller #define CONFIG_HAS_DCDC_CONVERTER 0 #define CONFIG_HAS_PWM_SWITCH 1 @@ -76,10 +80,17 @@ 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 3 + +#ifdef __MBED__ static const PinName led_pins[NUM_LED_PINS] = { // A B C PB_13, PB_14, PB_15 }; +#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 #ifndef LEDS_WRONG_POLARITY static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index 895af07..bc91825 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -18,7 +18,7 @@ static bool _pwm_active; -#ifndef UNIT_TEST +#if defined(__MBED__) && !defined(UNIT_TEST) static int _pwm_resolution;