Skip to content

Commit c0cf714

Browse files
mnkpcarlescufi
authored andcommitted
samples: threads: update to use new GPIO API
Update sample application to use new GPIO API: - GPIO flags defined by the devicetree - replace gpio_pin_write with gpio_pin_set function Signed-off-by: Piotr Mienkowski <piotr.mienkowski@gmail.com>
1 parent d443857 commit c0cf714

File tree

1 file changed

+44
-24
lines changed
  • samples/basic/threads/src

1 file changed

+44
-24
lines changed

samples/basic/threads/src/main.c

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,6 @@
1717
/* scheduling priority used by each thread */
1818
#define PRIORITY 7
1919

20-
/* Change this if you have an LED connected to a custom port */
21-
#ifndef DT_ALIAS_LED0_GPIOS_CONTROLLER
22-
#define DT_ALIAS_LED0_GPIOS_CONTROLLER LED0_GPIO_PORT
23-
#endif
24-
#ifndef DT_ALIAS_LED1_GPIOS_CONTROLLER
25-
#define DT_ALIAS_LED1_GPIOS_CONTROLLER LED1_GPIO_PORT
26-
#endif
27-
28-
#define PORT0 DT_ALIAS_LED0_GPIOS_CONTROLLER
29-
#define PORT1 DT_ALIAS_LED1_GPIOS_CONTROLLER
30-
31-
32-
/* Change this if you have an LED connected to a custom pin */
33-
#define LED0 DT_ALIAS_LED0_GPIOS_PIN
34-
#define LED1 DT_ALIAS_LED1_GPIOS_PIN
35-
3620
struct printk_data_t {
3721
void *fifo_reserved; /* 1st word reserved for use by fifo */
3822
u32_t led;
@@ -41,18 +25,40 @@ struct printk_data_t {
4125

4226
K_FIFO_DEFINE(printk_fifo);
4327

44-
void blink(const char *port, u32_t sleep_ms, u32_t led, u32_t id)
28+
#ifndef DT_ALIAS_LED0_GPIOS_FLAGS
29+
#define DT_ALIAS_LED0_GPIOS_FLAGS 0
30+
#endif
31+
32+
#ifndef DT_ALIAS_LED1_GPIOS_FLAGS
33+
#define DT_ALIAS_LED1_GPIOS_FLAGS 0
34+
#endif
35+
36+
struct led {
37+
const char *gpio_dev_name;
38+
const char *gpio_pin_name;
39+
unsigned int gpio_pin;
40+
unsigned int gpio_flags;
41+
};
42+
43+
void blink(const struct led *led, u32_t sleep_ms, u32_t id)
4544
{
46-
int cnt = 0;
4745
struct device *gpio_dev;
46+
int cnt = 0;
47+
int ret;
4848

49-
gpio_dev = device_get_binding(port);
50-
__ASSERT_NO_MSG(gpio_dev != NULL);
49+
gpio_dev = device_get_binding(led->gpio_dev_name);
50+
__ASSERT(gpio_dev != NULL, "Error: didn't find %s device\n",
51+
led->gpio_dev_name);
5152

52-
gpio_pin_configure(gpio_dev, led, GPIO_DIR_OUT);
53+
ret = gpio_pin_configure(gpio_dev, led->gpio_pin, led->gpio_flags);
54+
if (ret != 0) {
55+
printk("Error %d: failed to configure pin %d '%s'\n",
56+
ret, led->gpio_pin, led->gpio_pin_name);
57+
return;
58+
}
5359

5460
while (1) {
55-
gpio_pin_write(gpio_dev, led, cnt % 2);
61+
gpio_pin_set(gpio_dev, led->gpio_pin, cnt % 2);
5662

5763
struct printk_data_t tx_data = { .led = id, .cnt = cnt };
5864

@@ -71,12 +77,26 @@ void blink(const char *port, u32_t sleep_ms, u32_t led, u32_t id)
7177

7278
void blink1(void)
7379
{
74-
blink(PORT0, 100, LED0, 0);
80+
const struct led led1 = {
81+
.gpio_dev_name = DT_ALIAS_LED0_GPIOS_CONTROLLER,
82+
.gpio_pin_name = DT_ALIAS_LED0_LABEL,
83+
.gpio_pin = DT_ALIAS_LED0_GPIOS_PIN,
84+
.gpio_flags = GPIO_OUTPUT | DT_ALIAS_LED0_GPIOS_FLAGS,
85+
};
86+
87+
blink(&led1, 100, 0);
7588
}
7689

7790
void blink2(void)
7891
{
79-
blink(PORT1, 1000, LED1, 1);
92+
const struct led led2 = {
93+
.gpio_dev_name = DT_ALIAS_LED1_GPIOS_CONTROLLER,
94+
.gpio_pin_name = DT_ALIAS_LED1_LABEL,
95+
.gpio_pin = DT_ALIAS_LED1_GPIOS_PIN,
96+
.gpio_flags = GPIO_OUTPUT | DT_ALIAS_LED1_GPIOS_FLAGS,
97+
};
98+
99+
blink(&led2, 1000, 1);
80100
}
81101

82102
void uart_out(void)

0 commit comments

Comments
 (0)