20
20
#define LED0_NODE DT_ALIAS(led0)
21
21
#define LED1_NODE DT_ALIAS(led1)
22
22
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
+
23
31
struct printk_data_t {
24
32
void * fifo_reserved ; /* 1st word reserved for use by fifo */
25
33
uint32_t led ;
@@ -29,34 +37,40 @@ struct printk_data_t {
29
37
K_FIFO_DEFINE (printk_fifo );
30
38
31
39
struct led {
32
- const char * gpio_dev_name ;
40
+ struct gpio_dt_spec spec ;
33
41
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 , "" ),
36
52
};
37
53
38
54
void blink (const struct led * led , uint32_t sleep_ms , uint32_t id )
39
55
{
40
- const struct device * gpio_dev ;
56
+ const struct gpio_dt_spec * spec = & led -> spec ;
41
57
int cnt = 0 ;
42
58
int ret ;
43
59
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 );
48
62
return ;
49
63
}
50
64
51
- ret = gpio_pin_configure ( gpio_dev , led -> gpio_pin , led -> gpio_flags );
65
+ ret = gpio_pin_configure_dt ( spec , GPIO_OUTPUT );
52
66
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 );
55
69
return ;
56
70
}
57
71
58
72
while (1 ) {
59
- gpio_pin_set (gpio_dev , led -> gpio_pin , cnt % 2 );
73
+ gpio_pin_set (spec -> port , spec -> pin , cnt % 2 );
60
74
61
75
struct printk_data_t tx_data = { .led = id , .cnt = cnt };
62
76
@@ -75,33 +89,11 @@ void blink(const struct led *led, uint32_t sleep_ms, uint32_t id)
75
89
76
90
void blink0 (void )
77
91
{
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
-
89
92
blink (& led0 , 100 , 0 );
90
93
}
91
94
92
95
void blink1 (void )
93
96
{
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
-
105
97
blink (& led1 , 1000 , 1 );
106
98
}
107
99
0 commit comments