LED setup via devicetree
- New charlieplexing-leds binding - board.dts files updated - Pin and LED setup removed from pinmux.c and board.h files
This commit is contained in:
+132
-52
@@ -5,7 +5,102 @@
|
||||
*/
|
||||
|
||||
#include "leds.h"
|
||||
#include "board.h"
|
||||
|
||||
#include <zephyr.h>
|
||||
#ifndef UNIT_TEST
|
||||
#include <drivers/gpio.h>
|
||||
#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 <zephyr.h>
|
||||
#include <drivers/gpio.h>
|
||||
#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
|
||||
}
|
||||
|
||||
+81
-34
@@ -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 <zephyr.h>
|
||||
|
||||
/*
|
||||
* 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();
|
||||
|
||||
|
||||
+2
-2
@@ -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();
|
||||
}
|
||||
|
||||
+3
-5
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user