Skip to content

Commit 7e1877e

Browse files
mbolivar-nordiccarlescufi
authored andcommitted
samples: threads: devicetree cleanups
Use a gpio_dt_spec, GPIO_DT_SPEC_GET_OR, and DT_PROP_OR. Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
1 parent c0c471b commit 7e1877e

File tree

1 file changed

+26
-34
lines changed
  • samples/basic/threads/src

1 file changed

+26
-34
lines changed

samples/basic/threads/src/main.c

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
#define LED0_NODE DT_ALIAS(led0)
2121
#define LED1_NODE DT_ALIAS(led1)
2222

23+
#if !DT_NODE_HAS_STATUS(LED0_NODE, okay)
24+
#error "Unsupported board: led0 devicetree alias is not defined"
25+
#endif
26+
27+
#if !DT_NODE_HAS_STATUS(LED1_NODE, okay)
28+
#error "Unsupported board: led1 devicetree alias is not defined"
29+
#endif
30+
2331
struct printk_data_t {
2432
void *fifo_reserved; /* 1st word reserved for use by fifo */
2533
uint32_t led;
@@ -29,34 +37,40 @@ struct printk_data_t {
2937
K_FIFO_DEFINE(printk_fifo);
3038

3139
struct led {
32-
const char *gpio_dev_name;
40+
struct gpio_dt_spec spec;
3341
const char *gpio_pin_name;
34-
unsigned int gpio_pin;
35-
unsigned int gpio_flags;
42+
};
43+
44+
static const struct led led0 = {
45+
.spec = GPIO_DT_SPEC_GET_OR(LED0_NODE, gpios, {0}),
46+
.gpio_pin_name = DT_PROP_OR(LED0_NODE, label, ""),
47+
};
48+
49+
static const struct led led1 = {
50+
.spec = GPIO_DT_SPEC_GET_OR(LED1_NODE, gpios, {0}),
51+
.gpio_pin_name = DT_PROP_OR(LED1_NODE, label, ""),
3652
};
3753

3854
void blink(const struct led *led, uint32_t sleep_ms, uint32_t id)
3955
{
40-
const struct device *gpio_dev;
56+
const struct gpio_dt_spec *spec = &led->spec;
4157
int cnt = 0;
4258
int ret;
4359

44-
gpio_dev = device_get_binding(led->gpio_dev_name);
45-
if (gpio_dev == NULL) {
46-
printk("Error: didn't find %s device\n",
47-
led->gpio_dev_name);
60+
if (!device_is_ready(spec->port)) {
61+
printk("Error: %s device is not ready\n", spec->port->name);
4862
return;
4963
}
5064

51-
ret = gpio_pin_configure(gpio_dev, led->gpio_pin, led->gpio_flags);
65+
ret = gpio_pin_configure_dt(spec, GPIO_OUTPUT);
5266
if (ret != 0) {
53-
printk("Error %d: failed to configure pin %d '%s'\n",
54-
ret, led->gpio_pin, led->gpio_pin_name);
67+
printk("Error %d: failed to configure pin %d (LED '%s')\n",
68+
ret, spec->pin, led->gpio_pin_name);
5569
return;
5670
}
5771

5872
while (1) {
59-
gpio_pin_set(gpio_dev, led->gpio_pin, cnt % 2);
73+
gpio_pin_set(spec->port, spec->pin, cnt % 2);
6074

6175
struct printk_data_t tx_data = { .led = id, .cnt = cnt };
6276

@@ -75,33 +89,11 @@ void blink(const struct led *led, uint32_t sleep_ms, uint32_t id)
7589

7690
void blink0(void)
7791
{
78-
const struct led led0 = {
79-
#if DT_NODE_HAS_STATUS(LED0_NODE, okay)
80-
.gpio_dev_name = DT_GPIO_LABEL(LED0_NODE, gpios),
81-
.gpio_pin_name = DT_LABEL(LED0_NODE),
82-
.gpio_pin = DT_GPIO_PIN(LED0_NODE, gpios),
83-
.gpio_flags = GPIO_OUTPUT | DT_GPIO_FLAGS(LED0_NODE, gpios),
84-
#else
85-
#error "Unsupported board: led0 devicetree alias is not defined"
86-
#endif
87-
};
88-
8992
blink(&led0, 100, 0);
9093
}
9194

9295
void blink1(void)
9396
{
94-
const struct led led1 = {
95-
#if DT_NODE_HAS_STATUS(LED1_NODE, okay)
96-
.gpio_dev_name = DT_GPIO_LABEL(LED1_NODE, gpios),
97-
.gpio_pin_name = DT_LABEL(LED1_NODE),
98-
.gpio_pin = DT_GPIO_PIN(LED1_NODE, gpios),
99-
.gpio_flags = GPIO_OUTPUT | DT_GPIO_FLAGS(LED1_NODE, gpios),
100-
#else
101-
#error "Unsupported board: led1 devicetree alias is not defined"
102-
#endif
103-
};
104-
10597
blink(&led1, 1000, 1);
10698
}
10799

0 commit comments

Comments
 (0)