From 7633dc1d930ee241beb469dc31468574e53ea41a Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Sun, 24 Nov 2019 10:39:11 +0100 Subject: [PATCH] Zephyr: Clock setup fixed and LEDs implemented --- boards/arm/mppt1210hus_0v4/Kconfig.board | 8 +- boards/arm/mppt1210hus_0v4/Kconfig.defconfig | 10 +-- .../arm/mppt1210hus_0v4/mppt1210hus_0v4.dts | 35 +++++++-- .../mppt1210hus_0v4/mppt1210hus_0v4_defconfig | 13 +--- boards/arm/mppt1210hus_0v4/pinmux.c | 4 +- src/leds.cpp | 76 ++++++++++++++++++- src/leds.h | 10 ++- src/main_zephyr.cpp | 19 ++++- src/pcbs/mppt_1210_hus_0v4.h | 29 +++++-- zephyr/CMakeLists.txt | 16 ++-- zephyr/prj.conf | 1 + 11 files changed, 174 insertions(+), 47 deletions(-) diff --git a/boards/arm/mppt1210hus_0v4/Kconfig.board b/boards/arm/mppt1210hus_0v4/Kconfig.board index f36a58d..d50f13b 100755 --- a/boards/arm/mppt1210hus_0v4/Kconfig.board +++ b/boards/arm/mppt1210hus_0v4/Kconfig.board @@ -1,10 +1,10 @@ -# Kconfig - STM32 Nucleo-64 development board with STM32L073RZ MCU configuration +# Kconfig - Libre Solar MPPT 1210 HUS v0.4 # -# Copyright (c) 2018 Ilya Tagunov +# Copyright (c) 2019 Martin Jäger (Libre Solar) # # SPDX-License-Identifier: Apache-2.0 # -config BOARD_NUCLEO_L073RZ - bool "NUCLEO-64 L073RZ Development Board" +config BOARD_MPPT1210HUS_0V4 + bool "Libre Solar MPPT 1210 HUS v0.4" depends on SOC_STM32L073XX diff --git a/boards/arm/mppt1210hus_0v4/Kconfig.defconfig b/boards/arm/mppt1210hus_0v4/Kconfig.defconfig index 6713277..50a9905 100755 --- a/boards/arm/mppt1210hus_0v4/Kconfig.defconfig +++ b/boards/arm/mppt1210hus_0v4/Kconfig.defconfig @@ -1,14 +1,14 @@ -# Kconfig - STM32 Nucleo-64 development board with STM32L073RZ MCU +# Kconfig - Libre Solar MPPT 1210 HUS v0.4 # -# Copyright (c) 2018 Ilya Tagunov +# Copyright (c) 2019 Martin Jäger (Libre Solar) # # SPDX-License-Identifier: Apache-2.0 # -if BOARD_NUCLEO_L073RZ +if BOARD_MPPT1210HUS_0V4 config BOARD - default "nucleo_l073rz" + default "mppt1210hus_0v4" if UART_CONSOLE @@ -34,4 +34,4 @@ config SPI_STM32_INTERRUPT endif # SPI -endif # BOARD_NUCLEO_L073RZ +endif # BOARD_MPPT1210HUS_0V4 diff --git a/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts index d01e45e..f12bbaa 100755 --- a/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts +++ b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Ilya Tagunov + * Copyright (c) 2019 Martin Jäger (Libre Solar) * * SPDX-License-Identifier: Apache-2.0 */ @@ -20,14 +20,39 @@ leds { compatible = "gpio-leds"; - green_led_2: led_2 { - gpios = <&gpioa 5 GPIO_INT_ACTIVE_HIGH>; - label = "User LD2"; + led_pin_gnd: led_gnd { + gpios = <&gpiob 14 GPIO_INT_ACTIVE_LOW>; + label = "LED GND"; + }; + led_pin_soc12: led_soc12 { + gpios = <&gpiob 13 GPIO_INT_ACTIVE_HIGH>; + label = "LED SOC12"; + }; + led_pin_soc3: led_soc3 { + gpios = <&gpiob 2 GPIO_INT_ACTIVE_HIGH>; + label = "LED SOC3"; + }; + led_pin_rxtx: led_rxtx { + gpios = <&gpiob 11 GPIO_INT_ACTIVE_HIGH>; + label = "LED RXTX"; + }; + led_pin_load: led_load { + gpios = <&gpiob 10 GPIO_INT_ACTIVE_HIGH>; + label = "LED Load"; + }; + pin_usb_pwr_en: tmp_usb_pwr_en { + gpios = <&gpiob 12 GPIO_INT_ACTIVE_HIGH>; + label = "USB power enable"; }; }; aliases { - led0 = &green_led_2; + led-a = &led_pin_gnd; + led-b = &led_pin_soc12; + led-c = &led_pin_soc3; + led-d = &led_pin_rxtx; + led-e = &led_pin_load; + usb-pwr-en = &pin_usb_pwr_en; }; }; diff --git a/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4_defconfig b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4_defconfig index c35340f..2210e5a 100755 --- a/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4_defconfig +++ b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4_defconfig @@ -6,7 +6,7 @@ CONFIG_SOC_SERIES_STM32L0X=y # Platform Configuration CONFIG_SOC_STM32L073XX=y -CONFIG_BOARD_NUCLEO_L073RZ=y +CONFIG_BOARD_MPPT1210HUS_0V4=y # General Kernel Options CONFIG_CORTEX_M_SYSTICK=y @@ -29,16 +29,9 @@ CONFIG_GPIO=y 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_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 -CONFIG_CLOCK_STM32_APB2_PRESCALER=1 diff --git a/boards/arm/mppt1210hus_0v4/pinmux.c b/boards/arm/mppt1210hus_0v4/pinmux.c index 99972dc..eb63b0d 100755 --- a/boards/arm/mppt1210hus_0v4/pinmux.c +++ b/boards/arm/mppt1210hus_0v4/pinmux.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Ilya Tagunov + * Copyright (c) 2019 Martin Jäger (Libre Solar) * * SPDX-License-Identifier: Apache-2.0 */ @@ -12,7 +12,7 @@ #include -/* pin assignments for NUCLEO-L073RZ board */ +/* pin assignments for Libre Solar MPPT board */ static const struct pin_config pinconf[] = { #ifdef CONFIG_UART_1 {STM32_PIN_PA9, STM32L0_PINMUX_FUNC_PA9_USART1_TX}, diff --git a/src/leds.cpp b/src/leds.cpp index 36a2a01..1a23773 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -140,7 +140,76 @@ extern "C" void TIM22_IRQHandler(void) charlieplexing(); } -#endif +#endif // STM32 + +#elif defined(__ZEPHYR__) + +#include +#include + +#define SLEEP_TIME_MS (1000/60/NUM_LEDS) // 60 Hz + +void leds_update_thread() +{ + int led_count = 0; + int flicker_count = 0; + bool flicker_state = 1; + struct device *led_devs[NUM_LED_PINS]; + + for (int pin = 0; pin < NUM_LED_PINS; pin++) { + led_devs[pin] = device_get_binding(led_ports[pin]); + } + + leds_init(); + + while (1) { + + // could be increased to value >NUM_LEDS to reduce on-time + if (led_count >= NUM_LEDS) { + led_count = 0; + } + + if (flicker_count > 30) { + flicker_count = 0; + flicker_state = !flicker_state; + } + + if (led_count < NUM_LEDS && ( + led_states[led_count] == LED_STATE_ON || + (led_states[led_count] == LED_STATE_FLICKER && flicker_state == 1) || + (led_states[led_count] == LED_STATE_BLINK && blink_state == 1) + )) { + for (int pin_number = 0; pin_number < NUM_LED_PINS; pin_number++) { + switch (led_pin_setup[led_count][pin_number]) { + case PIN_HIGH: + gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_OUT); + gpio_pin_write(led_devs[pin_number], led_pins[pin_number], 1); + break; + case PIN_LOW: + gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_OUT); + gpio_pin_write(led_devs[pin_number], led_pins[pin_number], 0); + break; + case PIN_FLOAT: + gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_IN); + break; + } + } + } + else { + // all pins floating + for (int pin_number = 0; pin_number < NUM_LED_PINS; pin_number++) { + gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_IN); + } + } + + led_count++; + flicker_count++; + + k_sleep(SLEEP_TIME_MS); + } +} + +#endif // ZEPHYR or MBED void leds_init(bool enabled) { @@ -154,11 +223,11 @@ void leds_init(bool enabled) trigger_timeout[led] = -1; } } +#ifdef __MBED__ timer_start(NUM_LEDS * 60); // 60 Hz +#endif } -#endif /* UNIT_TEST */ - void leds_set_charging(bool enabled) { charging = enabled; @@ -241,7 +310,6 @@ void leds_update_soc(int soc, bool load_off_low_soc) int blink_chg = LED_STATE_ON; #endif - #ifdef LED_PWR led_states[LED_PWR] = blink_chg; trigger_timeout[LED_PWR] = -1; diff --git a/src/leds.h b/src/leds.h index df4e2c0..9e23778 100644 --- a/src/leds.h +++ b/src/leds.h @@ -32,10 +32,18 @@ enum LedState { LED_STATE_FLICKER }; -/** Initialize LEDs and start timer for charlieplexing +/** + * Initialize LEDs and start timer for charlieplexing. This function only needs to be called if + * used in a single-thread application (mbed) */ void leds_init(bool enabled = true); +#ifdef __ZEPHYR__ +/** Main thread for LED control in Zephyr, performs pin initializations and charlieplexing at 60 Hz + */ +void leds_update_thread(); +#endif + /** Enables/disables dedicated charging LED if existing or * blinks SOC LED when solar power is coming in. */ diff --git a/src/main_zephyr.cpp b/src/main_zephyr.cpp index 1aeff88..2771bdf 100644 --- a/src/main_zephyr.cpp +++ b/src/main_zephyr.cpp @@ -18,6 +18,8 @@ #include #include +#include +#include #include "thingset.h" // handles access to internal data via communication interfaces #include "pcb.h" // hardware-specific settings @@ -75,19 +77,30 @@ extern ThingSet ts; // defined in data_objects.cpp time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable) +#define USB_PWR_EN_PORT DT_ALIAS_USB_PWR_EN_GPIOS_CONTROLLER +#define USB_PWR_EN_PIN DT_ALIAS_USB_PWR_EN_GPIOS_PIN + void main(void) { + u32_t cnt = 0; + struct device *dev_usb_en; + + dev_usb_en = device_get_binding(USB_PWR_EN_PORT); + gpio_pin_configure(dev_usb_en, USB_PWR_EN_PIN, GPIO_DIR_OUT); + gpio_pin_write(dev_usb_en, USB_PWR_EN_PIN, 1); + printf("Booting Libre Solar Charge Controller: %s\n", CONFIG_BOARD); while (1) { - - k_sleep(1000); + gpio_pin_write(dev_usb_en, USB_PWR_EN_PIN, cnt % 2); + cnt++; + k_sleep(5000); } } //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(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); diff --git a/src/pcbs/mppt_1210_hus_0v4.h b/src/pcbs/mppt_1210_hus_0v4.h index 6bbac5b..9dab016 100644 --- a/src/pcbs/mppt_1210_hus_0v4.h +++ b/src/pcbs/mppt_1210_hus_0v4.h @@ -17,13 +17,15 @@ #ifndef MPPT_1210_HUS_0V4_H #define MPPT_1210_HUS_0V4_H -#define DEVICE_TYPE "MPPT-1210-HUS" -#define HARDWARE_VERSION "v0.4" - #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 FEATURE_DCDC_CONVERTER 1 #define FEATURE_PWM_SWITCH 0 @@ -68,13 +70,31 @@ 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 + +#ifdef __MBED__ static const PinName led_pins[NUM_LED_PINS] = { // GND SOC12 SOC3 RXTX LOAD PB_14, PB_13, PB_2, PB_11, PB_10 }; +#elif defined(__ZEPHYR__) +static const char *led_ports[NUM_LED_PINS] = { + DT_ALIAS_LED_A_GPIOS_CONTROLLER, + DT_ALIAS_LED_B_GPIOS_CONTROLLER, + DT_ALIAS_LED_C_GPIOS_CONTROLLER, + DT_ALIAS_LED_D_GPIOS_CONTROLLER, + DT_ALIAS_LED_E_GPIOS_CONTROLLER +}; +static const int led_pins[NUM_LED_PINS] = { + DT_ALIAS_LED_A_GPIOS_PIN, // GND + DT_ALIAS_LED_B_GPIOS_PIN, // SOC12 + DT_ALIAS_LED_C_GPIOS_PIN, // SOC3 + DT_ALIAS_LED_D_GPIOS_PIN, // RXTX + DT_ALIAS_LED_E_GPIOS_PIN // LOAD +}; +#endif // MBED or ZEPHYR + static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { { PIN_HIGH, PIN_LOW, PIN_LOW, PIN_LOW, PIN_LOW }, // LED1 { PIN_LOW, PIN_HIGH, PIN_LOW, PIN_LOW, PIN_LOW }, // LED2 @@ -82,7 +102,6 @@ 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/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 02a7ffb..f269a35 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -1,14 +1,14 @@ # SPDX-License-Identifier: Apache-2.0 -#set(BOARD nucleo_l073rz) +#set(BOARD mppt1210hus_0v4) 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) +project(libre_solar_charge_controller) -add_definitions(-DMPPT_1210_HUS_0V7 -D__ZEPHYR__ -DDEBUG_PRINT_FLAGS=7) +add_definitions(-DMPPT_1210_HUS_0V4 -D__ZEPHYR__ -DDEBUG_PRINT_FLAGS=7) include_directories(../src) include_directories(../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src) @@ -28,8 +28,8 @@ 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) +#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 index 1ecaa95..fbb65a8 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -6,6 +6,7 @@ CONFIG_HEAP_MEM_POOL_SIZE=0 CONFIG_GPIO=y CONFIG_SERIAL=y +CONFIG_UART_1=y CONFIG_I2C=y # for OLED display