diff --git a/boards/arm/mppt_1210_hus/board.h b/boards/arm/mppt_1210_hus/board.h index 994cc77..d1763fd 100644 --- a/boards/arm/mppt_1210_hus/board.h +++ b/boards/arm/mppt_1210_hus/board.h @@ -7,51 +7,6 @@ #ifndef MPPT_1210_HUS_H #define MPPT_1210_HUS_H -enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; - -// assignment LED numbers on PCB to their meaning -#define NUM_LEDS 5 - -#define LED_SOC_1 0 // LED1 -#define LED_SOC_2 1 // LED2 -#define LED_SOC_3 2 // LED3 -#if DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 4 -#define LED_RXTX 3 // LED4, used to indicate when sending data -#define LED_LOAD 4 // LED5 -#else -#define LED_LOAD 3 // LED4 -#define LED_RXTX 4 // LED5, used to indicate when sending data -#endif - -// LED pins and pin state configuration to switch above LEDs on -#if DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 4 -#define NUM_LED_PINS 5 -#else -#define NUM_LED_PINS 3 -#endif - -// defined in board definition pinmux.c -extern const char *led_ports[NUM_LED_PINS]; -extern const int led_pins[NUM_LED_PINS]; - -#if DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 4 -static const enum 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 - { PIN_LOW, PIN_LOW, PIN_HIGH, PIN_LOW, PIN_LOW }, // LED3 - { PIN_LOW, PIN_LOW, PIN_LOW, PIN_HIGH, PIN_LOW }, // LED4 - { PIN_LOW, PIN_LOW, PIN_LOW, PIN_LOW, PIN_HIGH } // LED5 -}; -#else -static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { - { PIN_HIGH, PIN_LOW, PIN_FLOAT }, // LED1 - { PIN_LOW, PIN_HIGH, PIN_FLOAT }, // LED2 - { PIN_HIGH, PIN_FLOAT, PIN_LOW }, // LED3 - { PIN_FLOAT, PIN_HIGH, PIN_LOW }, // LED4 - { PIN_FLOAT, PIN_LOW, PIN_HIGH } // LED5 -}; -#endif - // typical value for Semitec 103AT-5 thermistor: 3435 #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 10000.0 diff --git a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts index 9929183..9025f95 100755 --- a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts +++ b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts @@ -37,32 +37,46 @@ }; leds { - compatible = "gpio-leds"; - a { - gpios = <&gpiob 13 GPIO_ACTIVE_HIGH>; - label = "LED pin A"; + compatible = "charlieplexing-leds"; + gpios = <&gpiob 13 GPIO_ACTIVE_HIGH>, /* Pin A */ + <&gpiob 2 GPIO_ACTIVE_HIGH>, /* Pin C */ + <&gpiob 14 GPIO_ACTIVE_HIGH>; /* Pin B */ + + soc_1 { + states = <1 0 2>; }; - b { - gpios = <&gpiob 14 GPIO_ACTIVE_HIGH>; - label = "LED pin B"; + + soc_2 { + states = <0 1 2>; }; - c { - gpios = <&gpiob 2 GPIO_ACTIVE_HIGH>; - label = "LED pin C"; + + soc_3 { + states = <1 2 0>; + }; + + rxtx { + states = <2 1 0>; + }; + + load { + states = <2 0 1>; }; }; outputs { compatible = "outputs"; + load { label = "Load output"; gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>; current-max = <10>; }; + usb-pwr { label = "USB charging port enable"; gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>; }; + boot0 { label = "BOOT0 pin trigger"; gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; diff --git a/boards/arm/mppt_1210_hus/pinmux.c b/boards/arm/mppt_1210_hus/pinmux.c index bac244d..781a8b6 100755 --- a/boards/arm/mppt_1210_hus/pinmux.c +++ b/boards/arm/mppt_1210_hus/pinmux.c @@ -50,31 +50,3 @@ static int pinmux_stm32_init(struct device *port) SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY); - -#if DT_CHARGE_CONTROLLER_PCB_VERSION_NUM == 4 -const char *led_ports[] = { - DT_GPIO_LEDS_GND_GPIOS_CONTROLLER, - DT_GPIO_LEDS_SOC12_GPIOS_CONTROLLER, - DT_GPIO_LEDS_SOC3_GPIOS_CONTROLLER, - DT_GPIO_LEDS_RXTX_GPIOS_CONTROLLER, - DT_GPIO_LEDS_LOAD_GPIOS_CONTROLLER -}; -const int led_pins[] = { - DT_GPIO_LEDS_GND_GPIOS_PIN, - DT_GPIO_LEDS_SOC12_GPIOS_PIN, - DT_GPIO_LEDS_SOC3_GPIOS_PIN, - DT_GPIO_LEDS_RXTX_GPIOS_PIN, - DT_GPIO_LEDS_LOAD_GPIOS_PIN -}; -#else -const char *led_ports[] = { - DT_GPIO_LEDS_A_GPIOS_CONTROLLER, - DT_GPIO_LEDS_C_GPIOS_CONTROLLER, - DT_GPIO_LEDS_B_GPIOS_CONTROLLER, -}; -const int led_pins[] = { - DT_GPIO_LEDS_A_GPIOS_PIN, - DT_GPIO_LEDS_C_GPIOS_PIN, - DT_GPIO_LEDS_B_GPIOS_PIN, -}; -#endif diff --git a/boards/arm/mppt_2420_hpx/board.h b/boards/arm/mppt_2420_hpx/board.h index 92b4399..e7418c0 100644 --- a/boards/arm/mppt_2420_hpx/board.h +++ b/boards/arm/mppt_2420_hpx/board.h @@ -7,26 +7,6 @@ #ifndef MPPT_2420_HPX_H #define MPPT_2420_HPX_H -#include - -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[NUM_LED_PINS]; -extern const int led_pins[NUM_LED_PINS]; - -static 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 diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts index e6cf158..b0f959d 100644 --- a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts @@ -37,11 +37,11 @@ }; leds { - compatible = "gpio-leds"; + compatible = "charlieplexing-leds"; + gpios = <&gpioc 15 GPIO_ACTIVE_HIGH>; pwr { - gpios = <&gpioc 15 GPIO_ACTIVE_HIGH>; - label = "Power LED"; + states = <1>; }; }; diff --git a/boards/arm/mppt_2420_hpx/pinmux.c b/boards/arm/mppt_2420_hpx/pinmux.c index bf2da2d..b4b3d81 100644 --- a/boards/arm/mppt_2420_hpx/pinmux.c +++ b/boards/arm/mppt_2420_hpx/pinmux.c @@ -55,11 +55,3 @@ static int pinmux_stm32_init(struct device *port) SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY); - - -const char *led_ports[] = { - DT_GPIO_LEDS_PWR_GPIOS_CONTROLLER, -}; -const int led_pins[] = { - DT_GPIO_LEDS_PWR_GPIOS_PIN, -}; diff --git a/boards/arm/mppt_2420_lc/board.h b/boards/arm/mppt_2420_lc/board.h index 3ac39e3..2122faa 100644 --- a/boards/arm/mppt_2420_lc/board.h +++ b/boards/arm/mppt_2420_lc/board.h @@ -9,26 +9,6 @@ #define CONFIG_CAN 1 -enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; - -// assignment LED numbers on PCB to their meaning -#define NUM_LEDS 2 - -#define LED_PWR 0 // LED1 -#define LED_LOAD 1 // LED2 - -// LED pins and pin state configuration to switch above LEDs on -#define NUM_LED_PINS 2 - -// defined in board definition pinmux.c -extern const char *led_ports[NUM_LED_PINS]; -extern const int led_pins[NUM_LED_PINS]; - -static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { - { PIN_HIGH, PIN_LOW }, // LED1 - { PIN_LOW, PIN_HIGH } // LED2 -}; - // typical value for Semitec 103AT-5 thermistor: 3435 #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 10000.0 diff --git a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts index f00d02b..e768123 100755 --- a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts +++ b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts @@ -37,36 +37,43 @@ }; leds { - compatible = "gpio-leds"; + compatible = "charlieplexing-leds"; + gpios = <&gpiob 14 GPIO_ACTIVE_HIGH>, + <&gpiob 15 GPIO_ACTIVE_HIGH>; + pwr { - gpios = <&gpiob 14 GPIO_ACTIVE_HIGH>; - label = "LED GND"; + states = <1 0>; }; + load { - gpios = <&gpiob 15 GPIO_ACTIVE_HIGH>; - label = "LED Load"; + states = <0 1>; }; }; outputs { compatible = "outputs"; + load { label = "Load output"; gpios = <&gpiob 2 GPIO_ACTIVE_LOW>; current-max = <20>; }; + uext_en { label = "UEXT enable"; gpios = <&gpioc 14 GPIO_ACTIVE_LOW>; }; + can_en { label = "CAN disable / standby"; gpios = <&gpioa 15 GPIO_ACTIVE_LOW>; }; + aux_en { label = "5V output enable"; gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; }; + vbus_en { label = "CAN bus power supply enable"; gpios = <&gpioc 15 GPIO_ACTIVE_LOW>; diff --git a/boards/arm/mppt_2420_lc/pinmux.c b/boards/arm/mppt_2420_lc/pinmux.c index e05abd0..c01f33c 100755 --- a/boards/arm/mppt_2420_lc/pinmux.c +++ b/boards/arm/mppt_2420_lc/pinmux.c @@ -61,12 +61,3 @@ static int pinmux_stm32_init(struct device *port) SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY); - -const char *led_ports[] = { - DT_GPIO_LEDS_PWR_GPIOS_CONTROLLER, - DT_GPIO_LEDS_LOAD_GPIOS_CONTROLLER -}; -const int led_pins[] = { - DT_GPIO_LEDS_PWR_GPIOS_PIN, - DT_GPIO_LEDS_LOAD_GPIOS_PIN -}; diff --git a/boards/arm/mppt_2420_rc/board.h b/boards/arm/mppt_2420_rc/board.h index c6d728b..0d6e6d5 100644 --- a/boards/arm/mppt_2420_rc/board.h +++ b/boards/arm/mppt_2420_rc/board.h @@ -7,26 +7,6 @@ #ifndef MPPT_2420_RC_H #define MPPT_2420_RC_H -#include - -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[NUM_LED_PINS]; -extern const int led_pins[NUM_LED_PINS]; - -static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { - { PIN_HIGH }, // LED1 -}; - // typical value for Semitec 103AT-5 thermistor: 3435 #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 10000.0 diff --git a/boards/arm/mppt_2420_rc/mppt_2420_rc.dts b/boards/arm/mppt_2420_rc/mppt_2420_rc.dts index 632f2ed..665df69 100644 --- a/boards/arm/mppt_2420_rc/mppt_2420_rc.dts +++ b/boards/arm/mppt_2420_rc/mppt_2420_rc.dts @@ -37,10 +37,11 @@ }; leds { - compatible = "gpio-leds"; + compatible = "charlieplexing-leds"; + gpios = <&gpiob 15 GPIO_ACTIVE_HIGH>; + pwr { - gpios = <&gpiob 15 GPIO_ACTIVE_HIGH>; - label = "Power LED"; + states = <1>; }; }; diff --git a/boards/arm/mppt_2420_rc/pinmux.c b/boards/arm/mppt_2420_rc/pinmux.c index 798b1d1..30ffdad 100644 --- a/boards/arm/mppt_2420_rc/pinmux.c +++ b/boards/arm/mppt_2420_rc/pinmux.c @@ -50,11 +50,3 @@ static int pinmux_stm32_init(struct device *port) SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY); - - -const char *led_ports[] = { - DT_GPIO_LEDS_PWR_GPIOS_CONTROLLER, -}; -const int led_pins[] = { - DT_GPIO_LEDS_PWR_GPIOS_PIN, -}; diff --git a/boards/arm/pwm_2420_lus/board.h b/boards/arm/pwm_2420_lus/board.h index 1fd7a67..4e1fb28 100644 --- a/boards/arm/pwm_2420_lus/board.h +++ b/boards/arm/pwm_2420_lus/board.h @@ -11,43 +11,6 @@ #define PIN_EXT_BTN PB_12 -enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; - -// assignment LED numbers on PCB to their meaning -#define NUM_LEDS 5 - -#define LED_SOC_1 0 // LED1 -#define LED_SOC_2 1 // LED2 -#define LED_SOC_3 2 // LED3 -#define LED_LOAD 3 // LED4 -#define LED_RXTX 4 // LED5, used to indicate when sending data - -// LED pins and pin state configuration to switch above LEDs on -#define NUM_LED_PINS 3 - -// defined in board definition pinmux.c -extern const char *led_ports[NUM_LED_PINS]; -extern const int led_pins[NUM_LED_PINS]; - -#ifndef LEDS_WRONG_POLARITY -static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { - { PIN_HIGH, PIN_LOW, PIN_FLOAT }, // LED1 - { PIN_LOW, PIN_HIGH, PIN_FLOAT }, // LED2 - { PIN_HIGH, PIN_FLOAT, PIN_LOW }, // LED3 - { PIN_FLOAT, PIN_HIGH, PIN_LOW }, // LED4 - { PIN_FLOAT, PIN_LOW, PIN_HIGH } // LED5 -}; -#else -// LEDs with wrong polarity -static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { - { PIN_LOW, PIN_HIGH, PIN_FLOAT }, // LED1 - { PIN_HIGH, PIN_LOW, PIN_FLOAT }, // LED2 - { PIN_LOW, PIN_FLOAT, PIN_HIGH }, // LED3 - { PIN_FLOAT, PIN_LOW, PIN_HIGH }, // LED4 - { PIN_FLOAT, PIN_HIGH, PIN_LOW } // LED5 -}; -#endif - // typical value for Semitec 103AT-5 thermistor: 3435 #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 8200.0 diff --git a/boards/arm/pwm_2420_lus/pinmux.c b/boards/arm/pwm_2420_lus/pinmux.c index 0400d44..830717f 100644 --- a/boards/arm/pwm_2420_lus/pinmux.c +++ b/boards/arm/pwm_2420_lus/pinmux.c @@ -48,14 +48,3 @@ static int pinmux_stm32_init(struct device *port) SYS_INIT(pinmux_stm32_init, PRE_KERNEL_1, CONFIG_PINMUX_STM32_DEVICE_INITIALIZATION_PRIORITY); - -const char *led_ports[] = { - DT_GPIO_LEDS_A_GPIOS_CONTROLLER, - DT_GPIO_LEDS_B_GPIOS_CONTROLLER, - DT_GPIO_LEDS_C_GPIOS_CONTROLLER, -}; -const int led_pins[] = { - DT_GPIO_LEDS_A_GPIOS_PIN, - DT_GPIO_LEDS_B_GPIOS_PIN, - DT_GPIO_LEDS_C_GPIOS_PIN, -}; diff --git a/boards/arm/pwm_2420_lus/pwm_2420_lus.dts b/boards/arm/pwm_2420_lus/pwm_2420_lus.dts index f91da97..53c9980 100644 --- a/boards/arm/pwm_2420_lus/pwm_2420_lus.dts +++ b/boards/arm/pwm_2420_lus/pwm_2420_lus.dts @@ -35,23 +35,35 @@ }; leds { - compatible = "gpio-leds"; - a { - gpios = <&gpiob 13 GPIO_ACTIVE_HIGH>; - label = "LED pin A"; + compatible = "charlieplexing-leds"; + gpios = <&gpiob 13 GPIO_ACTIVE_HIGH>, /* Pin A */ + <&gpiob 14 GPIO_ACTIVE_HIGH>, /* Pin B */ + <&gpiob 15 GPIO_ACTIVE_HIGH>; /* Pin C */ + + soc_1 { + states = <1 0 2>; }; - b { - gpios = <&gpiob 14 GPIO_ACTIVE_HIGH>; - label = "LED pin B"; + + soc_2 { + states = <0 1 2>; }; - c { - gpios = <&gpiob 15 GPIO_ACTIVE_HIGH>; - label = "LED pin C"; + + soc_3 { + states = <1 2 0>; + }; + + load { + states = <2 1 0>; + }; + + rxtx { + states = <2 0 1>; }; }; outputs { compatible = "outputs"; + pwm_switch { label = "PWM power switch (solar panel)"; pwms = <&pwm3 4 (20*1000*1000)>; /* 20 ms period = 50 Hz */ @@ -60,15 +72,18 @@ current-max = <15>; kconfig-flag; /* make node visible in Kconfig */ }; + load { label = "Load output"; gpios = <&gpiob 2 GPIO_ACTIVE_LOW>; current-max = <15>; }; + usb-pwr { label = "USB charging port disable"; gpios = <&gpiob 5 GPIO_ACTIVE_LOW>; }; + boot0 { label = "BOOT0 pin trigger"; gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; diff --git a/src/leds.cpp b/src/leds.cpp index e3c48a1..7cab44a 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -5,7 +5,102 @@ */ #include "leds.h" -#include "board.h" + +#include +#ifndef UNIT_TEST +#include +#endif + +#include "hardware.h" + +#define SLEEP_TIME_MS (1000/60/NUM_LEDS) // 60 Hz + +/* + * The LED setup is derived from the devicetree configuration in board.dts file + * + * Example devicetree fragment: + * + * leds { + * compatible = "charlieplexing-leds"; + * gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>, + * <&gpiob 2 GPIO_ACTIVE_HIGH>, + * <&gpioc 3 GPIO_ACTIVE_HIGH>; + * soc_1 { + * states = <1 0 2>; + * }; + * soc_2 { + * states = <0 1 2>; + * }; + * soc_3 { + * states = <1 2 0>; + * }; + * load { + * states = <2 0 1>; + * }; + * }; + */ + +/* + * Number of LED pins = length of "gpios" array in "leds" devicetree node + */ +#define NUM_LED_PINS DT_PROP_LEN(DT_PATH(leds), gpios) + +/* + * LED pin states to switch a specific LED on + * + * 0 = low, 1 = high, 2 = floating (see enum PinState in leds.h) + * + * Above example board.dts expands to: + * + * static const int led_pin_states[4][3] { + * {1, 0, 2}, + * {0, 1, 2}, + * {1, 2, 0}, + * {2, 0, 1}, + * }; + */ +#define LED_STATES(node_id) DT_PROP(node_id, states), +static const int led_pin_states[NUM_LEDS][NUM_LED_PINS] = { + DT_FOREACH_CHILD(DT_PATH(leds), LED_STATES) +}; + +/* + * GPIO pin numbers (for up to 5 LED pins) + */ +static const int led_pins[] = { + DT_PHA_BY_IDX(DT_PATH(leds), gpios, 0, pin), +#if NUM_LED_PINS >= 2 + DT_PHA_BY_IDX(DT_PATH(leds), gpios, 1, pin), +#endif +#if NUM_LED_PINS >= 3 + DT_PHA_BY_IDX(DT_PATH(leds), gpios, 2, pin), +#endif +#if NUM_LED_PINS >= 4 + DT_PHA_BY_IDX(DT_PATH(leds), gpios, 3, pin), +#endif +#if NUM_LED_PINS >= 5 + DT_PHA_BY_IDX(DT_PATH(leds), gpios, 4, pin), +#endif +}; + +/* + * GPIO port labels (for up to 5 LED pins) + */ +static const char *const led_ports[] = { + DT_PROP_BY_PHANDLE_IDX(DT_PATH(leds), gpios, 0, label), +#if NUM_LED_PINS >= 2 + DT_PROP_BY_PHANDLE_IDX(DT_PATH(leds), gpios, 1, label), +#endif +#if NUM_LED_PINS >= 3 + DT_PROP_BY_PHANDLE_IDX(DT_PATH(leds), gpios, 2, label), +#endif +#if NUM_LED_PINS >= 4 + DT_PROP_BY_PHANDLE_IDX(DT_PATH(leds), gpios, 3, label), +#endif +#if NUM_LED_PINS >= 5 + DT_PROP_BY_PHANDLE_IDX(DT_PATH(leds), gpios, 4, label), +#endif +}; static int led_states[NUM_LEDS]; // must be one of enum LedState static int trigger_timeout[NUM_LEDS]; // seconds until trigger should end @@ -14,17 +109,10 @@ static bool blink_state = true; // global state of all blinking static bool charging = false; -#ifndef UNIT_TEST - -#include -#include -#include "hardware.h" - -#define SLEEP_TIME_MS (1000/60/NUM_LEDS) // 60 Hz - void leds_update_thread() { - int led_count = 0; +#ifndef UNIT_TEST + unsigned int led_count = 0; int flicker_count = 0; bool flicker_state = true; struct device *led_devs[NUM_LED_PINS]; @@ -35,7 +123,7 @@ void leds_update_thread() led_devs[pin] = device_get_binding(led_ports[pin]); } - leds_init(); + leds_init(true); while (true) { @@ -55,9 +143,10 @@ void leds_update_thread() 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]) { + )) + { + for (unsigned int pin_number = 0; pin_number < NUM_LED_PINS; pin_number++) { + switch (led_pin_states[led_count][pin_number]) { case PIN_HIGH: gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_OUTPUT); gpio_pin_set(led_devs[pin_number], led_pins[pin_number], 1); @@ -74,7 +163,7 @@ void leds_update_thread() } else { // all pins floating - for (int pin_number = 0; pin_number < NUM_LED_PINS; pin_number++) { + for (unsigned int pin_number = 0; pin_number < NUM_LED_PINS; pin_number++) { gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_INPUT); } } @@ -84,13 +173,12 @@ void leds_update_thread() k_sleep(K_MSEC(SLEEP_TIME_MS)); } -} - #endif // UNIT_TEST +} void leds_init(bool enabled) { - for (int led = 0; led < NUM_LEDS; led++) { + for (unsigned int led = 0; led < NUM_LEDS; led++) { if (enabled) { led_states[led] = LED_STATE_ON; trigger_timeout[led] = 0; // switched off the first time the main loop is called @@ -105,15 +193,13 @@ void leds_init(bool enabled) void leds_set_charging(bool enabled) { charging = enabled; -#ifdef LED_DCDC - led_states[LED_DCDC] = LED_STATE_ON; -#endif } -void leds_set(int led, bool enabled, int timeout) +void leds_set(unsigned int led, bool enabled, int timeout) { - if (led >= NUM_LEDS || led < 0) + if (led >= NUM_LEDS || led < 0) { return; + } if (enabled) { led_states[led] = LED_STATE_ON; @@ -125,17 +211,17 @@ void leds_set(int led, bool enabled, int timeout) trigger_timeout[led] = timeout; } -void leds_on(int led, int timeout) +void leds_on(unsigned int led, int timeout) { leds_set(led, true, timeout); } -void leds_off(int led) +void leds_off(unsigned int led) { leds_set(led, false, -1); } -void leds_blink(int led, int timeout) +void leds_blink(unsigned int led, int timeout) { if (led < NUM_LEDS && led >= 0) { led_states[led] = LED_STATE_BLINK; @@ -143,7 +229,7 @@ void leds_blink(int led, int timeout) } } -void leds_flicker(int led, int timeout) +void leds_flicker(unsigned int led, int timeout) { if (led < NUM_LEDS && led >= 0) { led_states[led] = LED_STATE_FLICKER; @@ -155,7 +241,7 @@ void leds_update_1s() { blink_state = !blink_state; - for (int led = 0; led < NUM_LEDS; led++) { + for (unsigned int led = 0; led < NUM_LEDS; led++) { if (trigger_timeout[led] > 0) { trigger_timeout[led]--; } @@ -167,44 +253,38 @@ void leds_update_1s() void leds_toggle_error() { - for (int led = 0; led < NUM_LEDS; led++) { + for (unsigned int led = 0; led < NUM_LEDS; led++) { led_states[led] = (led % 2) ^ blink_state; } blink_state = !blink_state; } -//---------------------------------------------------------------------------- void leds_update_soc(int soc, bool load_off_low_soc) { -#ifndef LED_DCDC - // if there is no dedicated LED for the DC/DC converter status, blink SOC - // or power LED when charger is enabled + // blink SOC or power LED when charger (DC/DC or PWM switch) is enabled int blink_chg = (charging) ? LED_STATE_BLINK : LED_STATE_ON; -#else - int blink_chg = LED_STATE_ON; -#endif -#ifdef LED_PWR - led_states[LED_PWR] = blink_chg; - trigger_timeout[LED_PWR] = -1; -#elif defined(LED_SOC_3) // 3-bar SOC gauge - trigger_timeout[LED_SOC_1] = -1; - trigger_timeout[LED_SOC_2] = -1; - trigger_timeout[LED_SOC_3] = -1; +#if LED_EXISTS(pwr) + led_states[LED_POS(pwr)] = blink_chg; + trigger_timeout[LED_POS(pwr)] = -1; +#elif LED_EXISTS(soc_3) // 3-bar SOC gauge + trigger_timeout[LED_POS(soc_1)] = -1; + trigger_timeout[LED_POS(soc_2)] = -1; + trigger_timeout[LED_POS(soc_3)] = -1; if (soc > 80 && !load_off_low_soc) { - led_states[LED_SOC_1] = blink_chg; - led_states[LED_SOC_2] = LED_STATE_ON; - led_states[LED_SOC_3] = LED_STATE_ON; + led_states[LED_POS(soc_1)] = blink_chg; + led_states[LED_POS(soc_2)] = LED_STATE_ON; + led_states[LED_POS(soc_3)] = LED_STATE_ON; } else if (soc > 20 && !load_off_low_soc) { - led_states[LED_SOC_1] = LED_STATE_OFF; - led_states[LED_SOC_2] = blink_chg; - led_states[LED_SOC_3] = LED_STATE_ON; + led_states[LED_POS(soc_1)] = LED_STATE_OFF; + led_states[LED_POS(soc_2)] = blink_chg; + led_states[LED_POS(soc_3)] = LED_STATE_ON; } else { - led_states[LED_SOC_1] = LED_STATE_OFF; - led_states[LED_SOC_2] = LED_STATE_OFF; - led_states[LED_SOC_3] = blink_chg; + led_states[LED_POS(soc_1)] = LED_STATE_OFF; + led_states[LED_POS(soc_2)] = LED_STATE_OFF; + led_states[LED_POS(soc_3)] = blink_chg; } #endif } diff --git a/src/leds.h b/src/leds.h index 474dea1..55d564e 100644 --- a/src/leds.h +++ b/src/leds.h @@ -7,7 +7,8 @@ #ifndef LEDS_H #define LEDS_H -/** @file +/** + * @file * * @brief * Control of status LEDs with charlieplexing @@ -17,7 +18,36 @@ extern "C" { #endif -/** LED state type +#include + +/* + * Find out if LED was specified in board devicetree file + */ +#define LED_EXISTS(node) DT_NODE_EXISTS(DT_CHILD(DT_PATH(leds), node)) + +#define LED_TIMEOUT_INFINITE (-1) + +/* + * Find out the position of an LED in the LED states array (using below enum) + */ +#define LED_POS(node) DT_N_S_leds_S_##node##_LED_POS + +/* + * Creates a unique name used by LED_POS macro for below enum + */ +#define LED_ENUM(node) node##_LED_POS, + +/* + * Enum for numbering of LEDs in the order specified in board.dts + */ +// cppcheck-suppress syntaxError +enum { + DT_FOREACH_CHILD(DT_PATH(leds), LED_ENUM) + NUM_LEDS // trick to get the number of elements +}; + +/** + * LED states */ enum LedState { LED_STATE_OFF = 0, @@ -27,68 +57,85 @@ enum LedState { }; /** - * Initialize LEDs and start timer for charlieplexing. This function only needs to be called if - * used in a single-thread application (mbed) + * Pin states */ -void leds_init(bool enabled = true); +enum PinState { + PIN_LOW = 0, + PIN_HIGH, + PIN_FLOAT +}; -/** Main thread for LED control in Zephyr, performs pin initializations and charlieplexing at 60 Hz +/** + * Initialize LEDs (called at the beginning of leds_update_thread) + */ +void leds_init(bool enabled); + +/** + * Main thread for LED control. Performs pin initializations and charlieplexing at 60 Hz */ void leds_update_thread(); -/** Enables/disables dedicated charging LED if existing or - * blinks SOC LED when solar power is coming in. +/** + * Enables/disables dedicated charging LED if existing or blinks SOC LED when solar power is coming + * in. */ void leds_set_charging(bool enabled); -/** Enables/disables LED +/** + * Enables/disables LED * - * @param led Number of LED in array defined in PCB configuration - * @param enabled LED is switched on if enabled is set to true - * @param timeout Defines for how long this state should be set (-1 for permanent setting) + * @param led Number of LED in array defined in PCB configuration + * @param enabled LED is switched on if enabled is set to true + * @param timeout Defines for how long this state should be set (-1 for permanent setting) */ -void leds_set(int led, bool enabled, int timeout = -1); +void leds_set(unsigned int led, bool enabled, int timeout); -/** Enables LED +/** + * Enable LED * - * @param led Number of LED in array defined in PCB configuration - * @param timeout Defines for how long this state should be set (-1 for permanent setting) + * @param led Number of LED in array defined in PCB configuration + * @param timeout Defines for how long this state should be set (-1 for permanent setting) */ -void leds_on(int led, int timeout = -1); +void leds_on(unsigned int led, int timeout); -/** Disables LED +/** + * Disable LED * - * @param led Number of LED in array defined in PCB configuration + * @param led Number of LED in array defined in PCB configuration */ -void leds_off(int led); +void leds_off(unsigned int led); -/** Blinks LED +/** + * Blink LED * - * @param led Number of LED in array defined in PCB configuration - * @param timeout Defines for how long this state should be set (-1 for permanent setting) + * @param led Number of LED in array defined in PCB configuration + * @param timeout Defines for how long this state should be set (-1 for permanent setting) */ -void leds_blink(int led, int timeout = -1); +void leds_blink(unsigned int led, int timeout); -/** Flickers LED +/** + * Flicker LED * - * @param led Number of LED in array defined in PCB configuration - * @param timeout Defines for how long this state should be set (-1 for permanent setting) + * @param led Number of LED in array defined in PCB configuration + * @param timeout Defines for how long this state should be set (-1 for permanent setting) */ -void leds_flicker(int led, int timeout = -1); +void leds_flicker(unsigned int led, int timeout); /** Updates LED blink and timeout states, must be called every second */ void leds_update_1s(); -/** Updates SOC LED bar (if existing) +/** + * Update SOC LED bar (if existing) * - * @param soc SOC in percent - * @param load_off_low_soc Prevents showing two SOC LEDs if load is switched off because of low SOC + * @param soc SOC in percent + * @param load_off_low_soc Prevents showing two SOC LEDs if load is switched off because of low SOC */ -void leds_update_soc(int soc, bool load_off_low_soc = false); +void leds_update_soc(int soc, bool load_off_low_soc); -/** Toggles between even and uneven LEDs switched on/off to create - * annoying flashing in case of an error. +/** + * Toggle between even and uneven LEDs switched on/off to create + * annoying flashing in case of an error. */ void leds_toggle_error(); diff --git a/src/load.cpp b/src/load.cpp index 3308618..331cac6 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -143,8 +143,8 @@ void LoadOutput::stop(uint32_t flag) if (flags_check(&error_flags, ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT)) { -#ifdef LED_LOAD - leds_flicker(LED_LOAD); +#if LED_EXISTS(load) + leds_flicker(LED_POS(load), LED_TIMEOUT_INFINITE); #endif oc_timestamp = uptime(); } diff --git a/src/load_driver.c b/src/load_driver.c index 7fe9e80..6117b4a 100644 --- a/src/load_driver.c +++ b/src/load_driver.c @@ -34,6 +34,7 @@ static struct device *dev_usb; #include "board.h" #include "hardware.h" #include "debug.h" +#include "leds.h" #if defined(PIN_I_LOAD_COMP) && PIN_LOAD_DIS == PB_2 static void lptim_init() @@ -148,13 +149,10 @@ void short_circuit_comp_init() #endif } -/* ToDo: dirty hack as C doesn't support functions with default values as in leds.h */ -void leds_set(int led, bool enabled, int timeout); - void load_out_set(bool status) { -#ifdef LED_LOAD - leds_set(LED_LOAD, status, -1); +#if LED_EXISTS(load) + leds_set(LED_POS(load), status, LED_TIMEOUT_INFINITE); #endif #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) diff --git a/test/board.h b/test/board.h index c92fb94..7bf27c3 100644 --- a/test/board.h +++ b/test/board.h @@ -30,31 +30,6 @@ // Values that are otherwise defined by Kconfig #define CONFIG_CONTROL_FREQUENCY 10 // Hz -enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; - -// assignment LED numbers on PCB to their meaning -#define NUM_LEDS 5 - -#define LED_SOC_1 0 // LED1 -#define LED_SOC_2 1 // LED2 -#define LED_SOC_3 2 // LED3 -#define LED_LOAD 3 // LED4 -#define LED_RXTX 4 // LED5, used to indicate when sending data - -// LED pins and pin state configuration to switch above LEDs on -#define NUM_LED_PINS 3 -static const int led_pins[NUM_LED_PINS] = { - // A B C - 0, 0, 0 -}; -static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { - { PIN_HIGH, PIN_LOW, PIN_FLOAT }, // LED1 - { PIN_LOW, PIN_HIGH, PIN_FLOAT }, // LED2 - { PIN_HIGH, PIN_FLOAT, PIN_LOW }, // LED3 - { PIN_FLOAT, PIN_HIGH, PIN_LOW }, // LED4 - { PIN_FLOAT, PIN_LOW, PIN_HIGH } // LED5 -}; - // pin definition only needed in adc_dma.cpp to detect if they are present on the PCB #define PIN_ADC_TEMP_BAT diff --git a/test/devicetree_unfixed.h b/test/devicetree_unfixed.h index a94ede3..3b3596a 100644 --- a/test/devicetree_unfixed.h +++ b/test/devicetree_unfixed.h @@ -32,6 +32,9 @@ #define DT_N_S_outputs_S_usb_pwr_EXISTS 1 +/* + * ADC + */ #define DT_N_S_adc_inputs_S_temp_bat_EXISTS 1 #define DT_N_S_adc_inputs_S_v_high_P_multiplier 105600 @@ -60,3 +63,18 @@ fn(DT_N_S_adc_inputs_S_temp_bat) \ fn(DT_N_S_adc_inputs_S_vref_mcu) \ fn(DT_N_S_adc_inputs_S_temp_mcu) + +/* + * LEDs + */ +#define DT_N_S_leds_FOREACH_CHILD(fn) \ + fn(DT_N_S_leds_S_pwr) + +#define DT_N_S_leds_P_gpios_IDX_0_PH DT_N_S_soc_S_pin_controller_48000000_S_gpio_48000800 +#define DT_N_S_leds_P_gpios_IDX_0_VAL_pin 15 +#define DT_N_S_leds_P_gpios_LEN 1 + +#define DT_N_S_soc_S_pin_controller_48000000_S_gpio_48000800_P_label "GPIOC" + +#define DT_N_S_leds_S_pwr_EXISTS 1 +#define DT_N_S_leds_S_pwr_P_states {1 /* 0x1 */} diff --git a/zephyr/dts/bindings/charlieplexing-leds.yaml b/zephyr/dts/bindings/charlieplexing-leds.yaml new file mode 100644 index 0000000..82c8002 --- /dev/null +++ b/zephyr/dts/bindings/charlieplexing-leds.yaml @@ -0,0 +1,19 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2020 Martin Jäger / Libre Solar + +description: Description for an ADC input including gain (multiplier/divider) and offset + +compatible: "charlieplexing-leds" + +properties: + gpios: + type: phandle-array + description: GPIO handle of pin + +child-binding: + description: GPIO child node + properties: + states: + type: array + description: | + Pin states to switch on LED identified by child node. 1=on, 0=off, 2=floating diff --git a/zephyr/mppt_1210_hus.overlay_0v4 b/zephyr/mppt_1210_hus.overlay_0v4 index 9cada71..7bf971e 100644 --- a/zephyr/mppt_1210_hus.overlay_0v4 +++ b/zephyr/mppt_1210_hus.overlay_0v4 @@ -11,42 +11,47 @@ }; leds { - compatible = "gpio-leds"; - gnd { - gpios = <&gpiob 14 GPIO_ACTIVE_HIGH>; - label = "LED GND"; + compatible = "charlieplexing-leds"; + gpios = <&gpiob 14 GPIO_ACTIVE_HIGH>, /* GND */ + <&gpiob 13 GPIO_ACTIVE_HIGH>, /* SOC_12 */ + <&gpiob 2 GPIO_ACTIVE_HIGH>, /* SOC_3 */ + <&gpiob 11 GPIO_ACTIVE_HIGH>, /* RXTX */ + <&gpiob 10 GPIO_ACTIVE_HIGH>; /* LOAD */ + + soc_1 { + states = <1 0 0 0 0>; }; - soc12 { - gpios = <&gpiob 13 GPIO_ACTIVE_HIGH>; - label = "LED SOC12"; + + soc_2 { + states = <0 1 0 0 0>; }; - soc3 { - gpios = <&gpiob 2 GPIO_ACTIVE_HIGH>; - label = "LED SOC3"; + + soc_3 { + states = <0 0 1 0 0>; }; + rxtx { - gpios = <&gpiob 11 GPIO_ACTIVE_HIGH>; - label = "LED RXTX"; + states = <0 0 0 1 0>; }; + load { - gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>; - label = "LED Load"; + states = <0 0 0 0 1>; }; - /delete-node/ a; - /delete-node/ b; - /delete-node/ c; }; outputs { compatible = "outputs"; + load { gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>; label = "Load enable"; }; + usb-pwr { gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; label = "USB charging port enable"; }; + /delete-node/ v-solar; /delete-node/ boot0; }; diff --git a/zephyr/prj.conf b/zephyr/prj.conf index 64ce61f..4925934 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -36,5 +36,3 @@ CONFIG_EXT_THINGSET_SERIAL=y CONFIG_REBOOT=y CONFIG_WATCHDOG=y - -CONFIG_LEGACY_DEVICETREE_MACROS=y