Skip to content

Commit

Permalink
leds: move state notifier gpio to DTS
Browse files Browse the repository at this point in the history
use DTS alias for gpios to connect with state notifier GPIO backend

Signed-off-by: Robert Gałat <[email protected]>
  • Loading branch information
RobertGalatNordic committed May 10, 2024
1 parent 7625cb2 commit e686083
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 64 deletions.
31 changes: 31 additions & 0 deletions samples/sid_end_device/boards/nrf52840dk_nrf52840.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@ sid_semtech: &spi2 {
};

/{
state_notifier_gpios{
compatible = "gpio-keys";
state_notifier_error: error {
gpios = <&gpio1 0x7 0x0>;
label = "Application state error";
};
state_notifier_dfu: dfu {
gpios = <&gpio1 0x1 0x0>;
label = "Application state dfu";
};
state_notifier_sending: sending {
gpios = <&gpio1 0x3 0x0>;
label = "Application state sending";
};
state_notifier_receiving: receiving {
gpios = <&gpio1 0x2 0x0>;
label = "Application state receiving";
};
};

aliases {
state-notifier-connected = &led0;
state-notifier-time-sync = &led1;
state-notifier-registered = &led2;
state-notifier-working = &led3;
state-notifier-error = &state_notifier_error;
state-notifier-dfu = &state_notifier_dfu;
state-notifier-sending = &state_notifier_sending;
state-notifier-receiving = &state_notifier_receiving;
};

semtech_sx1262_gpios{
compatible = "gpio-keys";
semtech_sx1262_cs: cs {
Expand Down
31 changes: 31 additions & 0 deletions samples/sid_end_device/boards/nrf5340dk_nrf5340_cpuapp.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@
};

/{
state_notifier_gpios{
compatible = "gpio-keys";
state_notifier_error: error {
gpios = <&gpio1 0x8 0x0>;
label = "Application state error";
};
state_notifier_dfu: dfu {
gpios = <&gpio1 0x2 0x0>;
label = "Application state dfu";
};
state_notifier_sending: sending {
gpios = <&gpio1 0x4 0x0>;
label = "Application state sending";
};
state_notifier_receiving: receiving {
gpios = <&gpio1 0x3 0x0>;
label = "Application state receiving";
};
};

aliases {
state-notifier-connected = &led0;
state-notifier-time-sync = &led1;
state-notifier-registered = &led2;
state-notifier-working = &led3;
state-notifier-error = &state_notifier_error;
state-notifier-dfu = &state_notifier_dfu;
state-notifier-sending = &state_notifier_sending;
state-notifier-receiving = &state_notifier_receiving;
};

semtech_sx1262_gpios{
compatible = "gpio-keys";
semtech_sx1262_cs: cs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ sid_semtech: &spi00 {
};

/{
aliases {
state-notifier-connected = &led0;
state-notifier-time-sync = &led1;
state-notifier-registered = &led2;
state-notifier-working = &led3;
};

semtech_sx1262_gpios{
compatible = "gpio-keys";
semtech_sx1262_cs: cs {
Expand All @@ -57,23 +64,8 @@ sid_semtech: &spi00 {
label = "semtech_sx1262 DIO1";
};
};
aliases {
led0 = &led0;
led1 = &led1;
led2 = &led2;
led3 = &led3;
watchdog0 = &wdt30;
/delete-property/ sw0;
/delete-property/ sw1;
/delete-property/ sw2;
/delete-property/ sw3;
};
};

/delete-node/ &button3;
/delete-node/ &button2;
/delete-node/ &button1;
/delete-node/ &button0;
};

&gpiote20 {
status = "okay";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
};

/{
aliases {
state-notifier-connected = &led0;
state-notifier-time-sync = &led1;
state-notifier-registered = &led2;
state-notifier-working = &led3;
};

semtech_sx1262_gpios{
compatible = "gpio-keys";
semtech_sx1262_cs: cs {
Expand Down
43 changes: 10 additions & 33 deletions utils/include/application_states.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,16 @@
#define APPLICATION_STATES_H
#include <zephyr/kernel.h>

/* X(state_name, gpio_port, gpio_pin) */
#if defined(NRF52840_XXAA)
#define X_APPLICAITON_STATES \
X(error, gpio1, 7) \
X(working, gpio0, 16) \
X(registered, gpio0, 15) \
X(time_sync, gpio0, 14) \
X(connected, gpio0, 13) \
X(dfu, gpio1, 1) \
X(sending, gpio1, 3) \
X(receiving, gpio1, 2)
#elif defined(NRF5340_XXAA)
#define X_APPLICAITON_STATES \
X(error, gpio1, 8) \
X(working, gpio0, 31) \
X(registered, gpio0, 30) \
X(time_sync, gpio0, 29) \
X(connected, gpio0, 28) \
X(dfu, gpio1, 2) \
X(sending, gpio1, 4) \
X(receiving, gpio1, 3)
#elif defined(NRF54L15_ENGA_XXAA)
/* X(state_name) */

#define X_APPLICAITON_STATES \
X(error, not_connected, 0) \
X(working, not_connected, 0) \
X(registered, not_connected, 0) \
X(time_sync, not_connected, 0) \
X(connected, not_connected, 0) \
X(dfu, not_connected, 0) \
X(sending, not_connected, 0) \
X(receiving, not_connected, 0)
#else
#error "Unknow device application states."
#endif
X(error) \
X(working) \
X(registered) \
X(time_sync) \
X(connected) \
X(dfu) \
X(sending) \
X(receiving)

#endif /* APPLICATION_STATES_H */
2 changes: 1 addition & 1 deletion utils/include/sidewalk_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern const char *const sidewalk_version_component[];
LOG_PRINTK("Sidewalk SDK = %d.%d.%d.%d\n", SID_SDK_MAJOR_VERSION, \
SID_SDK_MINOR_VERSION, SID_SDK_PATCH_VERSION, SID_SDK_BUILD_VERSION); \
LOG_PRINTK("build time = %s\n", build_time_stamp); \
LOG_PRINTK("board = %s\n", CONFIG_BOARD); \
LOG_PRINTK("board = %s\n", CONFIG_BOARD_TARGET); \
LOG_PRINTK("----------------------------------------------------------------\n")
#else
#define PRINT_SIDEWALK_VERSION()
Expand Down
23 changes: 9 additions & 14 deletions utils/src/state_notifier_gpio_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,32 @@

#include <state_notifier.h>

struct gpio_pin {
const struct device *port;
gpio_pin_t pin;
};

static inline struct gpio_pin state_to_pin_mapper(enum application_state state)
static inline struct gpio_dt_spec state_to_pin_mapper(enum application_state state)
{
// clang-format off
switch (state) {
#define X(name, port_obj, pin_num, ...)\
#define X(name, ...)\
case APPLICATION_STATE_ENUM(name):\
return (struct gpio_pin){ .port =COND_CODE_1(DT_NODE_EXISTS(port_obj), DEVICE_DT_GET_OR_NULL(DT_NODELABEL(port_obj)), (NULL)), .pin = pin_num};
return (struct gpio_dt_spec)GPIO_DT_SPEC_GET_OR(DT_ALIAS(state_notifier_##name), gpios, { 0 });
X_APPLICAITON_STATES
#undef X
}
// clang-format on
return (struct gpio_pin){ NULL, 0 };
return (struct gpio_dt_spec){ 0 };
}

static void gpio_enumerate_state(enum application_state state_id, uint32_t value)
{
struct gpio_pin gpio_id = state_to_pin_mapper(state_id);
if (gpio_id.port == NULL) {
struct gpio_dt_spec gpio = state_to_pin_mapper(state_id);
if (gpio.port == NULL) {
return;
}

#if defined(NRF54L15_ENGA_XXAA)
gpio_pin_set_raw(gpio_id.port, gpio_id.pin, value);
gpio_pin_set_raw(gpio.port, gpio.pin, value);
#else
/* output activated with low state */
gpio_pin_set_raw(gpio_id.port, gpio_id.pin, !value);
gpio_pin_set_raw(gpio.port, gpio.pin, !value);
#endif
}

Expand All @@ -53,7 +48,7 @@ static void state_change_handler_gpio(const struct notifier_state *state)

static void gpio_initializer(const enum application_state state_id, const uint32_t value)
{
struct gpio_pin pin = state_to_pin_mapper(state_id);
struct gpio_dt_spec pin = state_to_pin_mapper(state_id);

if (pin.port == NULL) {
return;
Expand Down

0 comments on commit e686083

Please sign in to comment.