Skip to content

Commit

Permalink
app: led: represent LEDs as LEDs, not GPIOs
Browse files Browse the repository at this point in the history
Switch from representing LEDs as GPIOs to representing LEDs as LEDs:

- Rename the state-gpios/activity-gpios devicetree properties to
  state-led/activity-leds and use phandles to refer to existing LED
  devicetree nodes. This simplifies the CANnectivity firmware devicetree
  overlays and avoids having to repeat LED GPIO specifications already
  present in the board devicetree.
- Switch from using the Zephyr GPIO API for controlling LEDs to using the
  Zephyr LED API. This allows using LEDs controlled by a dedicated LED
  controller IP/external IC.

Suggested-by: Fabio Baltieri <[email protected]>
Signed-off-by: Henrik Brix Andersen <[email protected]>
  • Loading branch information
henrikbrixandersen committed Jan 18, 2025
1 parent 2ee71e1 commit 7a06b97
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 110 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The CANnectivity firmware supports the following features, some of which depend
- CAN FD (flexible data rate)
- Fast-speed and Hi-speed USB
- Multiple, independent CAN channels
- GPIO-controlled LEDs:
- LEDs:
- CAN channel state LEDs
- CAN channel activity LEDs
- Visual CAN channel identification
Expand Down
2 changes: 1 addition & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ target_sources(app PRIVATE
src/usb.c
)

target_sources_ifdef(CONFIG_CANNECTIVITY_LED_GPIO app PRIVATE
target_sources_ifdef(CONFIG_CANNECTIVITY_LED app PRIVATE
src/led.c
)

Expand Down
16 changes: 9 additions & 7 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ configdefault USB_MAX_POWER

endif # USB_DEVICE_STACK

config CANNECTIVITY_LED_GPIO
config CANNECTIVITY_LED
bool "LED support"
default y
depends on $(dt_compat_any_has_prop,cannectivity-channel,state-gpios) || \
$(dt_compat_any_has_prop,cannectivity-channel,activity-gpios) || \
depends on $(dt_compat_any_has_prop,cannectivity-channel,state-led) || \
$(dt_compat_any_has_prop,cannectivity-channel,activity-leds) || \
($(dt_alias_enabled,led0) && !$(dt_compat_enabled,cannectivity-channel))
select GPIO
select LED
select SMF
select SMF_ANCESTOR_SUPPORT
select SMF_INITIAL_TRANSITION
select POLL
select USB_DEVICE_GS_USB_IDENTIFICATION if USB_DEVICE_GS_USB
select USBD_GS_USB_IDENTIFICATION if USBD_GS_USB
help
Enable support for GPIO-controlled CAN channel status/activity LEDs.
Enable support for CAN channel status/activity LEDs.

if CANNECTIVITY_LED_GPIO
if CANNECTIVITY_LED

config CANNECTIVITY_LED_EVENT_MSGQ_SIZE
int "LED event message queue size"
Expand All @@ -101,7 +101,7 @@ config CANNECTIVITY_LED_THREAD_PRIO
help
Priority level for the LED finite-state machine thread.

endif # CANNECTIVITY_LED_GPIO
endif # CANNECTIVITY_LED

config CANNECTIVITY_TIMESTAMP_COUNTER
bool "Hardware timestamp support"
Expand Down Expand Up @@ -137,6 +137,7 @@ config CANNECTIVITY_DFU_BUTTON
bool "DFU button support"
default y
depends on $(dt_alias_enabled,mcuboot-button0)
select GPIO
select REBOOT
help
Enable support for rebooting into Device Firmware Upgrade (DFU) mode by holding the DFU
Expand All @@ -155,6 +156,7 @@ config CANNECTIVITY_DFU_LED
bool # hidden
default y
depends on $(dt_alias_enabled,mcuboot-led0)
select LED
help
Enable support for controlling the Device Firmware Upgrade (DFU) LED (identified by the
"mcuboot-led0" devicetree alias).
Expand Down
8 changes: 4 additions & 4 deletions app/boards/canbardo_same70n20b.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
compatible = "cannectivity-channel";
can-controller = <&can0>;
termination-gpios = <&piod 1 GPIO_ACTIVE_HIGH>;
state-gpios = <&piod 22 GPIO_ACTIVE_LOW>;
activity-gpios = <&piod 24 GPIO_ACTIVE_LOW>;
state-led = <&can_0_ledg>;
activity-leds = <&can_0_ledy>;
};

channel1 {
compatible = "cannectivity-channel";
can-controller = <&can1>;
termination-gpios = <&piod 13 GPIO_ACTIVE_HIGH>;
state-gpios = <&piod 17 GPIO_ACTIVE_LOW>;
activity-gpios = <&piod 18 GPIO_ACTIVE_LOW>;
state-led = <&can_1_ledg>;
activity-leds = <&can_1_ledy>;
};
};
};
Expand Down
3 changes: 1 addition & 2 deletions app/boards/candlelight_stm32f072xb.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
channel0 {
compatible = "cannectivity-channel";
can-controller = <&can1>;
activity-gpios = <&gpioa 0 GPIO_ACTIVE_LOW>,
<&gpioa 1 GPIO_ACTIVE_LOW>;
activity-leds = <&led_rx &led_tx>;
};
};
};
Expand Down
3 changes: 1 addition & 2 deletions app/boards/candlelightfd_stm32g0b1xx.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
channel0 {
compatible = "cannectivity-channel";
can-controller = <&fdcan1>;
activity-gpios = <&gpioa 3 GPIO_ACTIVE_LOW>,
<&gpioa 4 GPIO_ACTIVE_LOW>;
activity-leds = <&led_rx &led_tx>;
};
};
};
Expand Down
4 changes: 2 additions & 2 deletions app/boards/candlelightfd_stm32g0b1xx_dual.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
compatible = "cannectivity-channel";
can-controller = <&fdcan1>;
/* Use RX LED for channel 0 state + activity */
state-gpios = <&gpioa 3 GPIO_ACTIVE_LOW>;
state-led = <&led_rx>;
};

channel1 {
compatible = "cannectivity-channel";
can-controller = <&fdcan2>;
/* Use TX LED for channel 0 state + activity */
state-gpios = <&gpioa 4 GPIO_ACTIVE_LOW>;
state-led = <&led_tx>;
};
};
};
Expand Down
4 changes: 2 additions & 2 deletions app/boards/lpcxpresso55s16_lpc55s16.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
compatible = "cannectivity-channel";
can-controller = <&can0>;
termination-gpios = <&gpio1 4 GPIO_ACTIVE_HIGH>; /* Red LED for testing */
state-gpios = <&gpio1 6 GPIO_ACTIVE_HIGH>;
activity-gpios = <&gpio1 7 GPIO_ACTIVE_HIGH>;
state-led = <&blue_led>;
activity-leds = <&green_led>;
};
};
};
Expand Down
4 changes: 2 additions & 2 deletions app/boards/mks_canable_v20_stm32g431xx.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
channel0 {
compatible = "cannectivity-channel";
can-controller = <&fdcan1>;
state-gpios = <&gpioa 15 GPIO_ACTIVE_LOW>; /* blue led */
activity-gpios = <&gpioa 0 GPIO_ACTIVE_LOW>; /* green led */
state-led = <&blue_led>;
activity-leds = <&green_led>;
};
};
};
Expand Down
46 changes: 40 additions & 6 deletions app/boards/native_sim.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@
#include <zephyr/dt-bindings/gpio/gpio.h>

/ {
leds {
compatible = "gpio-leds";

led_ch0_state: led_ch0_state {
gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>;
label = "Channel 0 state LED";
};

led_ch0_activity: led_ch0_activity {
gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>;
label = "Channel 0 activity LED";
};

led_ch1_state: led_ch1_state {
gpios = <&gpio0 2 GPIO_ACTIVE_HIGH>;
label = "Channel 1 state LED";
};

led_ch1_activity: led_ch1_activity {
gpios = <&gpio0 3 GPIO_ACTIVE_HIGH>;
label = "Channel 1 activity LED";
};

led_ch2_state: led_ch2_state {
gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>;
label = "Channel 2 state LED";
};

led_ch2_activity: led_ch2_activity {
gpios = <&gpio0 5 GPIO_ACTIVE_HIGH>;
label = "Channel 2 activity LED";
};
};

cannectivity: cannectivity {
compatible = "cannectivity";
timestamp-counter = <&counter0>;
Expand All @@ -15,24 +49,24 @@
compatible = "cannectivity-channel";
can-controller = <&can_loopback0>;
termination-gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>;
state-gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>;
activity-gpios = <&gpio0 2 GPIO_ACTIVE_HIGH>;
state-led = <&led_ch0_state>;
activity-leds = <&led_ch0_activity>;
};

channel1 {
compatible = "cannectivity-channel";
can-controller = <&can_loopback1>;
termination-gpios = <&gpio0 3 GPIO_ACTIVE_HIGH>;
state-gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>;
activity-gpios = <&gpio0 5 GPIO_ACTIVE_HIGH>;
state-led = <&led_ch1_state>;
activity-leds = <&led_ch1_activity>;
};

channel2 {
compatible = "cannectivity-channel";
can-controller = <&can_loopback2>;
termination-gpios = <&gpio0 6 GPIO_ACTIVE_HIGH>;
state-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>;
activity-gpios = <&gpio0 8 GPIO_ACTIVE_HIGH>;
state-led = <&led_ch2_state>;
activity-leds = <&led_ch2_activity>;
};
};

Expand Down
4 changes: 2 additions & 2 deletions app/boards/nucleo_h723zg_stm32h723xx.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
channel0 {
compatible = "cannectivity-channel";
can-controller = <&fdcan1>;
state-gpios = <&gpiob 0 GPIO_ACTIVE_HIGH>;
activity-gpios = <&gpioe 1 GPIO_ACTIVE_HIGH>;
state-led = <&green_led>;
activity-leds = <&yellow_led>;
};
};
};
Expand Down
3 changes: 1 addition & 2 deletions app/boards/ucan_stm32f072xb.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
channel0 {
compatible = "cannectivity-channel";
can-controller = <&can1>;
activity-gpios = <&gpioa 0 GPIO_ACTIVE_HIGH>,
<&gpioa 1 GPIO_ACTIVE_HIGH>;
activity-leds = <&led_rx &led_tx>;
};
};
};
Expand Down
5 changes: 2 additions & 3 deletions app/boards/usb2canfdv1_stm32g0b1xx.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
channel0 {
compatible = "cannectivity-channel";
can-controller = <&fdcan1>;
state-gpios = <&gpioa 2 GPIO_ACTIVE_LOW>;
activity-gpios = <&gpioa 0 GPIO_ACTIVE_LOW>,
<&gpioa 1 GPIO_ACTIVE_LOW>;
state-led = <&led_ready>;
activity-leds = <&led_rxd &led_txd>;
};
};
};
Expand Down
26 changes: 12 additions & 14 deletions app/dts/bindings/cannectivity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ description: |
compatible = "cannectivity-channel";
can-controller = <&can0>;
termination-gpios = <&gpio0 6 GPIO_ACTIVE_HIGH>;
state-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>;
activity-gpios = <&gpio0 8 GPIO_ACTIVE_HIGH>;
state-led = <&ch0_state_led>;
activity-leds = <&ch0_activity_led>;
};
channel {
compatible = "cannectivity-channel";
can-controller = <&can1>;
termination-gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>;
state-gpios = <&gpio0 10 GPIO_ACTIVE_HIGH>;
activity-gpios = <&gpio0 11 GPIO_ACTIVE_HIGH>;
state-led = <&ch1_state_led>;
activity-leds = <&ch1_activity_led>;
};
};
Expand Down Expand Up @@ -59,17 +59,15 @@ child-binding:
active when the CAN termination is enabled and inactive when the CAN termination is
disabled.
state-gpios:
type: phandle-array
state-led:
type: phandle
description: |
GPIO to use to control the CAN channel state LED. This GPIO is driven active when
the LED is to be turned on and inactive when the LED is to be turned off.
Phandle for the CAN channel state LED.
activity-gpios:
type: phandle-array
activity-leds:
type: phandles
description: |
GPIO to use to control the CAN channel activity LED. This GPIO is driven active when the LED
is to be turned on and inactive when the LED is to be turned off.
Phandle(s) for the CAN channel activity LED(s).
If two GPIOs are specified, the GPIO at index 0 will be used for indicating RX activity, and
the GPIO at index 1 will be used for indicating TX activity.
If two LED phandles are specified, the LED phandle at index 0 will be used for indicating RX
activity, and the LED phandle at index 1 will be used for indicating TX activity.
21 changes: 9 additions & 12 deletions app/src/dfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <zephyr/sys_clock.h>
#include <zephyr/dfu/mcuboot.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/led.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/reboot.h>
Expand All @@ -25,7 +26,7 @@ LOG_MODULE_REGISTER(dfu, CONFIG_CANNECTIVITY_LOG_LEVEL);
#define DFU_BUTTON_NODE DT_ALIAS(mcuboot_button0)

#ifdef CONFIG_CANNECTIVITY_DFU_LED
struct gpio_dt_spec dfu_led = GPIO_DT_SPEC_GET(DFU_LED_NODE, gpios);
struct led_dt_spec dfu_led = LED_DT_SPEC_GET(DFU_LED_NODE);
#endif /* CONFIG_CANNECTIVITY_DFU_LED */

#ifdef CONFIG_CANNECTIVITY_DFU_BUTTON
Expand All @@ -47,7 +48,11 @@ static void dfu_button_poll(struct k_work *work)

if (err > 0) {
#ifdef CONFIG_CANNECTIVITY_DFU_LED
err = gpio_pin_toggle_dt(&dfu_led);
if (k_sem_count_get(&dfu_button_sem) % 2U == 0U) {
err = led_on_dt(&dfu_led);
} else {
err = led_off_dt(&dfu_led);
}
if (err != 0) {
LOG_ERR("failed to toggle DFU LED (err %d)", err);
goto done;
Expand All @@ -66,7 +71,7 @@ static void dfu_button_poll(struct k_work *work)

done:
#ifdef CONFIG_CANNECTIVITY_DFU_LED
err = gpio_pin_set_dt(&dfu_led, 0);
err = led_off_dt(&dfu_led);
if (err != 0) {
LOG_ERR("failed to turn off DFU LED (err %d)", err);
return;
Expand Down Expand Up @@ -128,19 +133,11 @@ static int dfu_button_init(void)
#ifdef CONFIG_CANNECTIVITY_DFU_LED
static int dfu_led_init(void)
{
int err;

if (!gpio_is_ready_dt(&dfu_led)) {
if (!led_is_ready_dt(&dfu_led)) {
LOG_ERR("DFU LED device not ready");
return -ENODEV;
}

err = gpio_pin_configure_dt(&dfu_led, GPIO_OUTPUT_INACTIVE);
if (err != 0) {
LOG_ERR("failed to turn off DFU LED (err %d)", err);
return err;
}

return 0;
}
#endif /* CONFIG_CANNECTIVITY_DFU_LED */
Expand Down
Loading

0 comments on commit 7a06b97

Please sign in to comment.