1 /* board.c - Generic HW interaction hooks */
2
3 /*
4 * Copyright (c) 2021 Nordic Semiconductor
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <zephyr/bluetooth/mesh.h>
10 #include <zephyr/drivers/gpio.h>
11 #include "board.h"
12
13 /* Locate led0 as alias or label by that name */
14 #if DT_NODE_EXISTS(DT_ALIAS(led0))
15 #define LED0 DT_ALIAS(led0)
16 #elif DT_NODE_EXISTS(DT_NODELABEL(led0))
17 #define LED0 DT_NODELABEL(led0)
18 #else
19 #define LED0 DT_INVALID_NODE
20 #endif
21
22 /* Locate button0 as alias or label by sw0 or button0 */
23 #if DT_NODE_EXISTS(DT_ALIAS(sw0))
24 #define BUTTON0 DT_ALIAS(sw0)
25 #elif DT_NODE_EXISTS(DT_ALIAS(button0))
26 #define BUTTON0 DT_ALIAS(button0)
27 #elif DT_NODE_EXISTS(DT_NODELABEL(sw0))
28 #define BUTTON0 DT_NODELABEL(sw0)
29 #elif DT_NODE_EXISTS(DT_NODELABEL(button0))
30 #define BUTTON0 DT_NODELABEL(button0)
31 #else
32 #define BUTTON0 DT_INVALID_NODE
33 #endif
34
35 #if DT_NODE_EXISTS(LED0)
36 #define LED0_DEV DT_PHANDLE(LED0, gpios)
37 #define LED0_PIN DT_PHA(LED0, gpios, pin)
38 #define LED0_FLAGS DT_PHA(LED0, gpios, flags)
39
40 static const struct device *const led_dev = DEVICE_DT_GET(LED0_DEV);
41 #endif /* LED0 */
42
43 #if DT_NODE_EXISTS(BUTTON0)
44 #define BUTTON0_DEV DT_PHANDLE(BUTTON0, gpios)
45 #define BUTTON0_PIN DT_PHA(BUTTON0, gpios, pin)
46 #define BUTTON0_FLAGS DT_PHA(BUTTON0, gpios, flags)
47
48 static const struct device *const button_dev = DEVICE_DT_GET(BUTTON0_DEV);
49 static struct k_work *button_work;
50
button_cb(const struct device * port,struct gpio_callback * cb,gpio_port_pins_t pins)51 static void button_cb(const struct device *port, struct gpio_callback *cb,
52 gpio_port_pins_t pins)
53 {
54 k_work_submit(button_work);
55 }
56 #endif /* BUTTON0 */
57
led_init(void)58 static int led_init(void)
59 {
60 #if DT_NODE_EXISTS(LED0)
61 int err;
62
63 if (!device_is_ready(led_dev)) {
64 return -ENODEV;
65 }
66
67 err = gpio_pin_configure(led_dev, LED0_PIN,
68 LED0_FLAGS | GPIO_OUTPUT_INACTIVE);
69 if (err) {
70 return err;
71 }
72 #else
73 printk("WARNING: LEDs not supported on this board.\n");
74 #endif
75
76 return 0;
77 }
78
button_init(struct k_work * button_pressed)79 static int button_init(struct k_work *button_pressed)
80 {
81 #if DT_NODE_EXISTS(BUTTON0)
82 int err;
83
84 err = gpio_pin_configure(button_dev, BUTTON0_PIN,
85 BUTTON0_FLAGS | GPIO_INPUT);
86 if (err) {
87 return err;
88 }
89
90 static struct gpio_callback gpio_cb;
91
92 err = gpio_pin_interrupt_configure(button_dev, BUTTON0_PIN,
93 GPIO_INT_EDGE_TO_ACTIVE);
94 if (err) {
95 return err;
96 }
97
98 button_work = button_pressed;
99
100 gpio_init_callback(&gpio_cb, button_cb, BIT(BUTTON0_PIN));
101 gpio_add_callback(button_dev, &gpio_cb);
102 #else
103 printk("WARNING: Buttons not supported on this board.\n");
104 #endif
105
106 return 0;
107 }
108
board_init(struct k_work * button_pressed)109 int board_init(struct k_work *button_pressed)
110 {
111 int err;
112
113 err = led_init();
114 if (err) {
115 return err;
116 }
117
118 return button_init(button_pressed);
119 }
120
board_led_set(bool val)121 void board_led_set(bool val)
122 {
123 #if DT_NODE_EXISTS(LED0)
124 gpio_pin_set(led_dev, LED0_PIN, val);
125 #endif
126 }
127
board_output_number(bt_mesh_output_action_t action,uint32_t number)128 void board_output_number(bt_mesh_output_action_t action, uint32_t number)
129 {
130 }
131
board_prov_complete(void)132 void board_prov_complete(void)
133 {
134 }
135