Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[topic-gpio] samples: reel_board: mesh_badge: Convert to the new GPIO API #22137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions boards/arm/reel_board/dts/reel_board.dtsi
Expand Up @@ -9,15 +9,15 @@
leds {
compatible = "gpio-leds";
red_led: led_0 {
gpios = <&gpio0 11 0>;
gpios = <&gpio0 11 GPIO_ACTIVE_LOW>;
label = "User D3 red";
};
green_led: led_1 {
gpios = <&gpio0 12 0>;
gpios = <&gpio0 12 GPIO_ACTIVE_LOW>;
label = "User D3 green";
};
blue_led: led_2 {
gpios = <&gpio1 9 0>;
gpios = <&gpio1 9 GPIO_ACTIVE_LOW>;
label = "User D3 blue";
};
};
Expand All @@ -38,7 +38,7 @@
gpio_keys {
compatible = "gpio-keys";
user_button: button_0 {
gpios = <&gpio0 7 GPIO_PUD_PULL_UP>;
gpios = <&gpio0 7 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
label = "User Button";
};
};
Expand Down
2 changes: 1 addition & 1 deletion boards/arm/reel_board/reel_board.dts
Expand Up @@ -28,7 +28,7 @@
leds {
compatible = "gpio-leds";
back_led: led_3 {
gpios = <&gpio0 13 0>;
gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
label = "User D13 green";
};
};
Expand Down
9 changes: 1 addition & 8 deletions samples/boards/reel_board/mesh_badge/src/board.h
Expand Up @@ -5,15 +5,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

enum led_periph_device {
DEV_IDX_LED0 = 0,
DEV_IDX_LED1,
DEV_IDX_LED2,
};

enum periph_device {
DEV_IDX_BUTTON = 0,
DEV_IDX_HDC1010,
DEV_IDX_HDC1010 = 0,
DEV_IDX_MMA8652,
DEV_IDX_APDS9960,
DEV_IDX_EPD,
Expand Down
4 changes: 2 additions & 2 deletions samples/boards/reel_board/mesh_badge/src/mesh.c
Expand Up @@ -62,7 +62,8 @@ static struct k_work mesh_start_work;

/* Definitions of models user data (Start) */
static struct led_onoff_state led_onoff_state[] = {
{ .dev_id = DEV_IDX_LED0 },
/* Use LED 0 for this model */
{ .dev_id = 0 },
};

static void heartbeat(u8_t hops, u16_t feat)
Expand Down Expand Up @@ -155,7 +156,6 @@ static void gen_onoff_set_unack(struct bt_mesh_model *model,
printk("addr 0x%02x state 0x%02x\n",
bt_mesh_model_elem(model)->addr, state->current);

/* Pin set low turns on LED's on the reel board */
if (set_led_state(state->dev_id, onoff)) {
printk("Failed to set led state\n");

Expand Down
53 changes: 0 additions & 53 deletions samples/boards/reel_board/mesh_badge/src/periphs.c
Expand Up @@ -17,54 +17,13 @@ struct device_info {
char *name;
};

struct led_device_info {
struct device *dev;
char *name;
u32_t pin;
};

static struct led_device_info led_dev_info[] = {
/* red front LED */
{ NULL, DT_ALIAS_LED0_GPIOS_CONTROLLER, DT_ALIAS_LED0_GPIOS_PIN },
/* green front LED */
{ NULL, DT_ALIAS_LED1_GPIOS_CONTROLLER, DT_ALIAS_LED1_GPIOS_PIN },
/* blue front LED */
{ NULL, DT_ALIAS_LED2_GPIOS_CONTROLLER, DT_ALIAS_LED2_GPIOS_PIN },
};

static struct device_info dev_info[] = {
{ NULL, DT_ALIAS_SW0_GPIOS_CONTROLLER },
{ NULL, DT_INST_0_TI_HDC1010_LABEL },
{ NULL, DT_INST_0_NXP_MMA8652FC_LABEL },
{ NULL, DT_INST_0_AVAGO_APDS9960_LABEL },
{ NULL, DT_INST_0_SOLOMON_SSD16XXFB_LABEL },
};

static void configure_gpios(void)
{
gpio_pin_configure(led_dev_info[DEV_IDX_LED0].dev,
led_dev_info[DEV_IDX_LED0].pin, GPIO_DIR_OUT);
gpio_pin_write(led_dev_info[DEV_IDX_LED0].dev,
led_dev_info[DEV_IDX_LED0].pin, 1);

gpio_pin_configure(led_dev_info[DEV_IDX_LED1].dev,
led_dev_info[DEV_IDX_LED1].pin, GPIO_DIR_OUT);
gpio_pin_write(led_dev_info[DEV_IDX_LED1].dev,
led_dev_info[DEV_IDX_LED1].pin, 1);

gpio_pin_configure(led_dev_info[DEV_IDX_LED2].dev,
led_dev_info[DEV_IDX_LED2].pin, GPIO_DIR_OUT);
gpio_pin_write(led_dev_info[DEV_IDX_LED2].dev,
led_dev_info[DEV_IDX_LED2].pin, 1);
}

int set_led_state(u8_t id, bool state)
{
/* Invert state because of active low state for GPIO LED pins */
return gpio_pin_write(led_dev_info[id].dev, led_dev_info[id].pin,
!state);
}

int get_hdc1010_val(struct sensor_value *val)
{
if (sensor_sample_fetch(dev_info[DEV_IDX_HDC1010].dev)) {
Expand Down Expand Up @@ -195,18 +154,6 @@ int periphs_init(void)
}
}

/* Bind leds */
for (i = 0U; i < ARRAY_SIZE(led_dev_info); i++) {
led_dev_info[i].dev = device_get_binding(led_dev_info[i].name);
if (led_dev_info[i].dev == NULL) {
printk("Failed to get %s led device\n",
led_dev_info[i].name);
return -EBUSY;
}
}

configure_gpios();

configure_accel();

return 0;
Expand Down
46 changes: 26 additions & 20 deletions samples/boards/reel_board/mesh_badge/src/reel_board.c
Expand Up @@ -47,8 +47,6 @@ struct font_info {

#define STAT_COUNT 128

#define EDGE (GPIO_INT_EDGE | GPIO_INT_DOUBLE_EDGE)

#ifdef DT_ALIAS_SW0_GPIOS_FLAGS
#define PULL_UP DT_ALIAS_SW0_GPIOS_FLAGS
#else
Expand All @@ -66,11 +64,15 @@ static char str_buf[256];
static struct {
struct device *dev;
const char *name;
u32_t pin;
gpio_pin_t pin;
gpio_flags_t flags;
} leds[] = {
{ .name = DT_ALIAS_LED0_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED0_GPIOS_PIN, },
{ .name = DT_ALIAS_LED1_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED1_GPIOS_PIN, },
{ .name = DT_ALIAS_LED2_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED2_GPIOS_PIN, },
{ .name = DT_ALIAS_LED0_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED0_GPIOS_PIN,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just do DT_ALIAS_LED0_GPIOS which expands to the controller name, pin, and flags in a single initializer expression. Though without a pre-defined standard structure synchronized to generation of that expression it's technically more future-proof to spelling out each element.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks for letting me know of this DT-generated macro. As you yourself mention, since this uses designated initializers I'd rather not change it at this point.

.flags = DT_ALIAS_LED0_GPIOS_FLAGS},
{ .name = DT_ALIAS_LED1_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED1_GPIOS_PIN,
.flags = DT_ALIAS_LED1_GPIOS_FLAGS},
{ .name = DT_ALIAS_LED2_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED2_GPIOS_PIN,
.flags = DT_ALIAS_LED2_GPIOS_FLAGS}
};

struct k_delayed_work led_timer;
Expand Down Expand Up @@ -444,11 +446,7 @@ static void long_press(struct k_work *work)

static bool button_is_pressed(void)
{
u32_t val;

gpio_pin_read(gpio, DT_ALIAS_SW0_GPIOS_PIN, &val);

return !val;
return gpio_pin_get(gpio, DT_ALIAS_SW0_GPIOS_PIN) > 0;
}

static void button_interrupt(struct device *dev, struct gpio_callback *cb,
Expand Down Expand Up @@ -517,24 +515,32 @@ static int configure_button(void)
}

gpio_pin_configure(gpio, DT_ALIAS_SW0_GPIOS_PIN,
(GPIO_DIR_IN | GPIO_INT | PULL_UP | EDGE));
GPIO_INPUT | DT_ALIAS_SW0_GPIOS_FLAGS);

gpio_init_callback(&button_cb, button_interrupt, BIT(DT_ALIAS_SW0_GPIOS_PIN));
gpio_add_callback(gpio, &button_cb);
gpio_pin_interrupt_configure(gpio, DT_ALIAS_SW0_GPIOS_PIN,
GPIO_INT_EDGE_BOTH);

gpio_init_callback(&button_cb, button_interrupt,
BIT(DT_ALIAS_SW0_GPIOS_PIN));

gpio_pin_enable_callback(gpio, DT_ALIAS_SW0_GPIOS_PIN);
gpio_add_callback(gpio, &button_cb);

return 0;
}

int set_led_state(u8_t id, bool state)
{
return gpio_pin_set(leds[id].dev, leds[id].pin, state);
}

static void led_timeout(struct k_work *work)
{
static int led_cntr;
int i;

/* Disable all LEDs */
for (i = 0; i < ARRAY_SIZE(leds); i++) {
gpio_pin_write(leds[i].dev, leds[i].pin, 1);
set_led_state(i, 0);
}

/* Stop after 5 iterations */
Expand All @@ -545,7 +551,7 @@ static void led_timeout(struct k_work *work)

/* Select and enable current LED */
i = led_cntr++ % ARRAY_SIZE(leds);
gpio_pin_write(leds[i].dev, leds[i].pin, 0);
set_led_state(i, 1);

k_delayed_work_submit(&led_timer, K_MSEC(100));
}
Expand All @@ -561,9 +567,9 @@ static int configure_leds(void)
return -ENODEV;
}

gpio_pin_configure(leds[i].dev, leds[i].pin, GPIO_DIR_OUT);
gpio_pin_write(leds[i].dev, leds[i].pin, 1);

gpio_pin_configure(leds[i].dev, leds[i].pin,
leds[i].flags |
GPIO_OUTPUT_INACTIVE);
}

k_delayed_work_init(&led_timer, led_timeout);
Expand Down