./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
37 / 47
78.7%
Branches:
12 / 22
54.5%
Functions:
6 / 6
100.0%

kfsw-modules/button-test/src/button_test_gpio.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4
5 #include <zephyr/device.h>
6 #include <zephyr/devicetree.h>
7 #include <zephyr/drivers/gpio.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/sys/util.h>
10
11 #include <kfsw/platform/time.h>
12
13 #include "button_test_internal.h"
14
15 #if CONFIG_KFSW_BUTTON_TEST_GPIO
16 /* TODO(template): the hardware backend. Everything here is bound from
17 * devicetree, and the generic layer builds and tests without this file.
18 */
19 #define KFSW_BUTTON_TEST_BUTTON_NODE DT_CHOSEN(kfsw_button_test_button)
20
21 BUILD_ASSERT(DT_NODE_EXISTS(KFSW_BUTTON_TEST_BUTTON_NODE),
22 "kfsw,button-test-button must select a GPIO button node");
23 BUILD_ASSERT(DT_NODE_HAS_PROP(KFSW_BUTTON_TEST_BUTTON_NODE, gpios),
24 "kfsw,button-test-button must provide a gpios property");
25
26 static const struct gpio_dt_spec button_test_button =
27 GPIO_DT_SPEC_GET(KFSW_BUTTON_TEST_BUTTON_NODE, gpios);
28 static struct gpio_callback button_test_gpio_callback;
29
30 static void button_test_debounce_work_handler(struct k_work *work);
31 static K_WORK_DELAYABLE_DEFINE(button_test_debounce_work, button_test_debounce_work_handler);
32
33 10 static void button_test_debounce_work_handler(struct k_work *work)
34 {
35 int pressed;
36
37 ARG_UNUSED(work);
38
39 10 pressed = gpio_pin_get_dt(&button_test_button);
40
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (pressed >= 0) {
41 10 kfsw_button_test_process_level(pressed != 0, kfsw_time_monotonic_ms());
42 }
43 10 }
44
45 12 static void button_test_gpio_edge(const struct device *port, struct gpio_callback *callback,
46 uint32_t pins)
47 {
48 ARG_UNUSED(port);
49 ARG_UNUSED(callback);
50 ARG_UNUSED(pins);
51
52 12 (void)k_work_reschedule(&button_test_debounce_work,
53 12 K_MSEC(CONFIG_KFSW_BUTTON_TEST_DEBOUNCE_MS));
54 12 }
55
56 2 int kfsw_button_test_gpio_prepare(bool *initially_pressed)
57 {
58 int pressed;
59 int result;
60
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (initially_pressed == NULL) {
62 ✗ return -EINVAL;
63 }
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!gpio_is_ready_dt(&button_test_button)) {
65 ✗ return -ENODEV;
66 }
67
68 2 result = gpio_pin_configure_dt(&button_test_button, GPIO_INPUT);
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
70 ✗ return result;
71 }
72 2 pressed = gpio_pin_get_dt(&button_test_button);
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (pressed < 0) {
74 ✗ return pressed;
75 }
76
77 2 *initially_pressed = pressed != 0;
78 2 return 0;
79 }
80
81 2 int kfsw_button_test_gpio_start(void)
82 {
83 int result;
84
85 2 gpio_init_callback(&button_test_gpio_callback, button_test_gpio_edge,
86 2 BIT(button_test_button.pin));
87 2 result = gpio_add_callback(button_test_button.port, &button_test_gpio_callback);
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
89 ✗ return result;
90 }
91 2 result = gpio_pin_interrupt_configure_dt(&button_test_button, GPIO_INT_EDGE_BOTH);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
93 ✗ (void)gpio_remove_callback(button_test_button.port, &button_test_gpio_callback);
94 ✗ return result;
95 }
96
97 /* Reconcile any transition between the initial sample and interrupt enable. */
98 2 (void)k_work_reschedule(&button_test_debounce_work,
99 2 K_MSEC(CONFIG_KFSW_BUTTON_TEST_DEBOUNCE_MS));
100 2 return 0;
101 }
102 #endif
103
104 #if CONFIG_KFSW_BUTTON_TEST_LED_GPIO
105 #define KFSW_BUTTON_TEST_GREEN_LED_NODE DT_CHOSEN(kfsw_button_test_led_green)
106 #define KFSW_BUTTON_TEST_BLUE_LED_NODE DT_CHOSEN(kfsw_button_test_led_blue)
107 #define KFSW_BUTTON_TEST_RED_LED_NODE DT_CHOSEN(kfsw_button_test_led_red)
108
109 #define KFSW_BUTTON_TEST_ASSERT_LED(node_id, chosen_name) \
110 BUILD_ASSERT(DT_NODE_EXISTS(node_id), chosen_name " must select a GPIO LED node"); \
111 BUILD_ASSERT(DT_NODE_HAS_PROP(node_id, gpios), chosen_name " must provide gpios")
112
113 KFSW_BUTTON_TEST_ASSERT_LED(KFSW_BUTTON_TEST_GREEN_LED_NODE, "kfsw,button-test-led-green");
114 KFSW_BUTTON_TEST_ASSERT_LED(KFSW_BUTTON_TEST_BLUE_LED_NODE, "kfsw,button-test-led-blue");
115 KFSW_BUTTON_TEST_ASSERT_LED(KFSW_BUTTON_TEST_RED_LED_NODE, "kfsw,button-test-led-red");
116
117 static const struct gpio_dt_spec button_test_leds[KFSW_BUTTON_TEST_LED_COUNT] = {
118 [KFSW_BUTTON_TEST_LED_GREEN] = GPIO_DT_SPEC_GET(KFSW_BUTTON_TEST_GREEN_LED_NODE, gpios),
119 [KFSW_BUTTON_TEST_LED_BLUE] = GPIO_DT_SPEC_GET(KFSW_BUTTON_TEST_BLUE_LED_NODE, gpios),
120 [KFSW_BUTTON_TEST_LED_RED] = GPIO_DT_SPEC_GET(KFSW_BUTTON_TEST_RED_LED_NODE, gpios),
121 };
122
123 4 int kfsw_button_test_led_gpio_prepare(void)
124 {
125
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (size_t index = 0U; index < ARRAY_SIZE(button_test_leds); index++) {
126 int result;
127
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (!gpio_is_ready_dt(&button_test_leds[index])) {
129 ✗ return -ENODEV;
130 }
131 12 result = gpio_pin_configure_dt(&button_test_leds[index], GPIO_OUTPUT_INACTIVE);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (result != 0) {
133 ✗ return result;
134 }
135 }
136 4 return 0;
137 }
138
139 158 int kfsw_button_test_led_gpio_set(enum kfsw_button_test_led led, bool on)
140 {
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 if ((unsigned int)led >= ARRAY_SIZE(button_test_leds)) {
142 ✗ return -EINVAL;
143 }
144 158 return gpio_pin_set_dt(&button_test_leds[led], on ? 1 : 0);
145 }
146 #endif
147