1 /*
2 * Copyright (c) 2024 Tenstorrent AI ULC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 #include <zephyr/logging/log.h>
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/drivers/gpio/gpio_emul.h>
12
13 /* size of stack area used by each thread */
14 #define STACKSIZE (2048)
15
16 /* scheduling priority used by each thread */
17 #define PRIORITY 7
18
test_handler(const struct device * port,struct gpio_callback * cb,gpio_port_pins_t pins)19 void test_handler(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins)
20 {
21 printk("Interrupt detected!\n");
22 }
23
gpio_sample(void)24 void gpio_sample(void)
25 {
26 const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(gpio_emul));
27
28 if (!device_is_ready(dev)) {
29 printk("%s: device not ready.\n", dev->name);
30 return;
31 }
32
33 /* Configure pin 0 as output and toggle */
34 gpio_pin_configure(dev, 0, GPIO_OUTPUT);
35 gpio_pin_set(dev, 0, 1);
36 gpio_pin_set(dev, 0, 0);
37
38 /* Configure pin 1 as input */
39 gpio_pin_configure(dev, 1, GPIO_INPUT);
40
41 /* Read pin 1 */
42 gpio_emul_input_set(dev, 1, 1);
43 gpio_pin_get(dev, 1);
44 gpio_emul_input_set(dev, 1, 0);
45 gpio_pin_get(dev, 1);
46
47 /* Setup pin 1 for interrupt */
48 gpio_pin_interrupt_configure(dev, 1, GPIO_INT_EDGE_RISING);
49 static struct gpio_callback gpio_cb;
50
51 gpio_init_callback(&gpio_cb, test_handler, BIT(1));
52 gpio_add_callback(dev, &gpio_cb);
53
54 /* Trigger interrupt */
55 gpio_emul_input_set(dev, 1, 1);
56
57 /* Remove interrupt */
58 gpio_remove_callback(dev, &gpio_cb);
59 }
60
gpio_thread(void * dummy1,void * dummy2,void * dummy3)61 static void gpio_thread(void *dummy1, void *dummy2, void *dummy3)
62 {
63 ARG_UNUSED(dummy1);
64 ARG_UNUSED(dummy2);
65 ARG_UNUSED(dummy3);
66
67 gpio_sample();
68 }
69
70 K_THREAD_DEFINE(thread_gpio, STACKSIZE, gpio_thread, NULL, NULL, NULL, PRIORITY, 0, 0);
71