# HG changeset patch # User John Tsiombikas # Date 1457153703 0 # Node ID a7543d592206226a1bf97a2b9ecee05c81f3bddb # Parent acfcd119e53248bb582a9a2862f652b502534be2 - interrupt debouncing (crude) - blinking another led when pressing the button - using broadcom custom pullup/down function diff -r acfcd119e532 -r a7543d592206 gpio.c --- a/gpio.c Sat Mar 05 04:25:33 2016 +0000 +++ b/gpio.c Sat Mar 05 04:55:03 2016 +0000 @@ -5,8 +5,11 @@ #include #include #include +#include +#include #define TEST_OUTPUT_PIN 4 +#define TEST_OUTPUT_GREEN 17 #define TEST_INPUT_PIN 22 static int dev_init(void); @@ -43,12 +46,14 @@ printk("gpio module registered (device: %d)\n", major); gpio_direction_output(TEST_OUTPUT_PIN, 0); - gpio_direction_input(TEST_INPUT_PIN); + gpio_direction_output(TEST_OUTPUT_GREEN, 0); if(gpio_request(TEST_INPUT_PIN, "test_input") != 0) { printk("gpio: failed to get input gpio pin\n"); return -1; } + gpio_direction_input(TEST_INPUT_PIN); + bcm2708_gpio_setpull(gpio_to_chip(TEST_INPUT_PIN), TEST_INPUT_PIN, BCM2708_PULL_DOWN); if((intr_num = gpio_to_irq(TEST_INPUT_PIN)) < 0) { printk("gpio: failed to get interrupt line for gpio pin\n"); return -1; @@ -65,12 +70,17 @@ static void dev_exit(void) { printk("gpio exit\n"); + unregister_chrdev(major, "gpio"); if(intr_num != -1) { free_irq(intr_num, "gpio"); } gpio_free(TEST_INPUT_PIN); + + gpio_direction_input(TEST_OUTPUT_PIN); + gpio_direction_input(TEST_OUTPUT_GREEN); + bcm2708_gpio_setpull(gpio_to_chip(TEST_INPUT_PIN), TEST_INPUT_PIN, BCM2708_PULL_OFF); } static int dev_open(struct inode *inode, struct file *file) @@ -89,8 +99,6 @@ { unsigned char val; - printk("dev_write(%d) called\n", sz); - if(sz < 1) { return 0; } @@ -104,6 +112,15 @@ static irqreturn_t intr_handler(int inum, void *devid, struct pt_regs *regs) { + static unsigned long ignore_until; + unsigned long now = jiffies; + + if(time_before(now, ignore_until)) { + return IRQ_HANDLED; + } + ignore_until = now + 128 * HZ / 1000; + printk("presses: %d\n", num_presses++); + gpio_set_value(TEST_OUTPUT_GREEN, num_presses & 1); return IRQ_HANDLED; }