summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-08-23 18:47:48 +0200
committerxengineering <me@xengineering.eu>2024-08-23 18:47:48 +0200
commit73ecc1b734d4ea1102f9aa1057782a381e2768e2 (patch)
tree29678d70a6cc83ee04a0a6ad9d0af8a54767274e
parentfa22728ea19c81202899305fc118176ca3f7042b (diff)
downloadiot-contact-73ecc1b734d4ea1102f9aa1057782a381e2768e2.tar
iot-contact-73ecc1b734d4ea1102f9aa1057782a381e2768e2.tar.zst
iot-contact-73ecc1b734d4ea1102f9aa1057782a381e2768e2.zip
firmware: Implement printk output on button press
-rw-r--r--firmware/prj.conf2
-rw-r--r--firmware/src/main.c40
2 files changed, 42 insertions, 0 deletions
diff --git a/firmware/prj.conf b/firmware/prj.conf
index d505d79..24239db 100644
--- a/firmware/prj.conf
+++ b/firmware/prj.conf
@@ -3,3 +3,5 @@ CONFIG_SHELL_PROMPT_UART="[iot-contact] "
CONFIG_NETWORKING=y
CONFIG_NET_SHELL=y
+
+CONFIG_GPIO=y
diff --git a/firmware/src/main.c b/firmware/src/main.c
index 7a1076c..1ee66d1 100644
--- a/firmware/src/main.c
+++ b/firmware/src/main.c
@@ -1,6 +1,46 @@
#include <zephyr/kernel.h>
+#include <zephyr/device.h>
+#include <zephyr/drivers/gpio.h>
+
+#define SW0_NODE DT_ALIAS(sw0)
+#if !DT_NODE_HAS_STATUS(SW0_NODE, okay)
+#error "Unsupported board: Devicetree alias for button is not defined"
+#endif
+static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(SW0_NODE, gpios);
+static struct gpio_callback button_cb_data;
+
+void button_pressed(const struct device *dev, struct gpio_callback *cb,
+ uint32_t pins)
+{
+ printk("Button pressed\n");
+}
int main(void)
{
+ int ret;
+
+ if (!gpio_is_ready_dt(&button)) {
+ printk("Error: button device %s is not ready\n",
+ button.port->name);
+ return 0;
+ }
+
+ ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
+ if (ret != 0) {
+ printk("Error %d: failed to configure %s pin %d\n",
+ ret, button.port->name, button.pin);
+ return 0;
+ }
+
+ ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE);
+ if (ret != 0) {
+ printk("Error %d: failed to configure interrupt on %s pin %d\n",
+ ret, button.port->name, button.pin);
+ return 0;
+ }
+
+ gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin));
+ gpio_add_callback(button.port, &button_cb_data);
+
return 0;
}