Skip to content

Instantly share code, notes, and snippets.

@ExtremeGTX
Last active October 23, 2019 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ExtremeGTX/fb5a3a35696eee48ec27541b282dfb76 to your computer and use it in GitHub Desktop.
Save ExtremeGTX/fb5a3a35696eee48ec27541b282dfb76 to your computer and use it in GitHub Desktop.
This is for Zephyr EDGE_BOTH interrupt test
/*
This test use pins 16,17 Bank_0 on Espressif DevkitC V4
Just Connect both pins, no other external circuitry should be required
*/
#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 2000
static struct gpio_callback button_cb_data;
volatile u32_t cnt=0;
volatile u32_t o_val=0;
volatile u32_t i_val=0;
#define INPUT_GPIO_PIN 17
#define OUTPUT_GPIO_PIN 16
void button_pressed(struct device *dev, struct gpio_callback *cb,
u32_t pins)
{
cnt++;
}
void main(void)
{
struct device *dev;
int ret;
dev = device_get_binding("GPIO_0");
if (dev == NULL) {
printk("Error: didn't find %s device\n",
"GPIO_0");
return;
}
ret = gpio_pin_configure(dev, INPUT_GPIO_PIN, GPIO_INPUT);
if (ret != 0) {
printk("Error %d: failed to configure pin %d '%s'\n",
ret, INPUT_GPIO_PIN, "INPUT");
return;
}
ret = gpio_pin_interrupt_configure(dev, INPUT_GPIO_PIN, GPIO_INT_EDGE_BOTH);
if (ret != 0) {
printk("Error %d: failed to configure interrupt on pin %d '%s'\n",
ret, INPUT_GPIO_PIN, "INPUT");
return;
}
gpio_init_callback(&button_cb_data, button_pressed, BIT(INPUT_GPIO_PIN));
gpio_add_callback(dev, &button_cb_data);
ret = gpio_pin_configure(dev, OUTPUT_GPIO_PIN, GPIO_OUTPUT_LOW);
if (ret != 0) {
printk("Error %d: failed to configure pin %d '%s'\n",
ret, OUTPUT_GPIO_PIN, "OUTPUT");
return;
}
while (1) {
i_val = gpio_pin_get(dev, INPUT_GPIO_PIN);
gpio_pin_set(dev, OUTPUT_GPIO_PIN, o_val);
o_val = !o_val;
i_val = gpio_pin_get(dev, INPUT_GPIO_PIN);
gpio_pin_set(dev, OUTPUT_GPIO_PIN, o_val);
o_val = !o_val;
i_val = gpio_pin_get(dev, INPUT_GPIO_PIN);
gpio_pin_set(dev, OUTPUT_GPIO_PIN, o_val);
printk("Results: %d\n\n",cnt); /* should be "1: Rising/Falling, 2: BOTH_EDGE" */
cnt=0;
k_sleep(SLEEP_TIME_MS); /* delay for readable serial output */
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment