Instantly share code, notes, and snippets.
Last active
May 30, 2021 20:52
-
Save Abhiroop/d83755d7a5703f704fbfb9c3d116d87c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <zephyr.h> | |
#include <device.h> | |
#include <drivers/gpio.h> | |
#include <sys/util.h> | |
#include <sys/printk.h> | |
#include <inttypes.h> | |
#define SLEEP_TIME_MS 1 | |
#define SW0_NODE DT_ALIAS(sw0) | |
#if DT_NODE_HAS_STATUS(SW0_NODE, okay) | |
#define SW0_GPIO_LABEL DT_GPIO_LABEL(SW0_NODE, gpios) | |
#define SW0_GPIO_PIN DT_GPIO_PIN(SW0_NODE, gpios) | |
#define SW0_GPIO_FLAGS (GPIO_INPUT | DT_GPIO_FLAGS(SW0_NODE, gpios)) | |
#else | |
#error "Unsupported board: sw0 devicetree alias is not defined" | |
#define SW0_GPIO_LABEL "" | |
#define SW0_GPIO_PIN 0 | |
#define SW0_GPIO_FLAGS 0 | |
#endif | |
#define LED0_NODE DT_ALIAS(led0) | |
#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios) | |
#define LED0_GPIO_LABEL DT_GPIO_LABEL(LED0_NODE, gpios) | |
#define LED0_GPIO_PIN DT_GPIO_PIN(LED0_NODE, gpios) | |
#define LED0_GPIO_FLAGS (GPIO_OUTPUT | DT_GPIO_FLAGS(LED0_NODE, gpios)) | |
#endif | |
static const struct device *initialize_led(void); | |
static void match_led_to_button(const struct device *button, | |
const struct device *led); | |
static struct gpio_callback button_cb_data; | |
const struct device *led; | |
void button_pressed(const struct device *dev, struct gpio_callback *cb, | |
uint32_t pins) | |
{ | |
match_led_to_button(dev, led); | |
} | |
void main(void) | |
{ | |
const struct device *button; | |
int ret; | |
button = device_get_binding(SW0_GPIO_LABEL); | |
if (button == NULL) { | |
printk("Error: didn't find %s device\n", SW0_GPIO_LABEL); | |
return; | |
} | |
ret = gpio_pin_configure(button, SW0_GPIO_PIN, SW0_GPIO_FLAGS); | |
if (ret != 0) { | |
printk("Error %d: failed to configure %s pin %d\n", | |
ret, SW0_GPIO_LABEL, SW0_GPIO_PIN); | |
return; | |
} | |
ret = gpio_pin_interrupt_configure(button, | |
SW0_GPIO_PIN, | |
GPIO_INT_EDGE_BOTH); | |
if (ret != 0) { | |
printk("Error %d: failed to configure interrupt on %s pin %d\n", | |
ret, SW0_GPIO_LABEL, SW0_GPIO_PIN); | |
return; | |
} | |
gpio_init_callback(&button_cb_data, button_pressed, BIT(SW0_GPIO_PIN)); | |
gpio_add_callback(button, &button_cb_data); | |
printk("Set up button at %s pin %d\n", SW0_GPIO_LABEL, SW0_GPIO_PIN); | |
led = initialize_led(); | |
printk("Press the button\n"); | |
} | |
#ifdef LED0_GPIO_LABEL | |
static const struct device *initialize_led(void) | |
{ | |
const struct device *led; | |
int ret; | |
led = device_get_binding(LED0_GPIO_LABEL); | |
if (led == NULL) { | |
printk("Didn't find LED device %s\n", LED0_GPIO_LABEL); | |
return NULL; | |
} | |
ret = gpio_pin_configure(led, LED0_GPIO_PIN, LED0_GPIO_FLAGS); | |
if (ret != 0) { | |
printk("Error %d: failed to configure LED device %s pin %d\n", | |
ret, LED0_GPIO_LABEL, LED0_GPIO_PIN); | |
return NULL; | |
} | |
printk("Set up LED at %s pin %d\n", LED0_GPIO_LABEL, LED0_GPIO_PIN); | |
return led; | |
} | |
static void match_led_to_button(const struct device *button, | |
const struct device *led) | |
{ | |
bool val; | |
val = gpio_pin_get(button, SW0_GPIO_PIN); | |
gpio_pin_set(led, LED0_GPIO_PIN, val); | |
} | |
#else /* !defined(LED0_GPIO_LABEL) */ | |
static const struct device *initialize_led(void) | |
{ | |
printk("No LED device was defined\n"); | |
return NULL; | |
} | |
static void match_led_to_button(const struct device *button, | |
const struct device *led) | |
{ | |
return; | |
} | |
#endif /* LED0_GPIO_LABEL */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment