raspi_gpio_driver
changeset 2:a7543d592206 tip
- interrupt debouncing (crude)
- blinking another led when pressing the button
- using broadcom custom pullup/down function
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 05 Mar 2016 04:55:03 +0000 |
parents | acfcd119e532 |
children | |
files | gpio.c |
diffstat | 1 files changed, 20 insertions(+), 3 deletions(-) [+] |
line diff
1.1 --- a/gpio.c Sat Mar 05 04:25:33 2016 +0000 1.2 +++ b/gpio.c Sat Mar 05 04:55:03 2016 +0000 1.3 @@ -5,8 +5,11 @@ 1.4 #include <linux/interrupt.h> 1.5 #include <asm/uaccess.h> 1.6 #include <linux/gpio.h> 1.7 +#include <linux/jiffies.h> 1.8 +#include <linux/platform_data/bcm2708.h> 1.9 1.10 #define TEST_OUTPUT_PIN 4 1.11 +#define TEST_OUTPUT_GREEN 17 1.12 #define TEST_INPUT_PIN 22 1.13 1.14 static int dev_init(void); 1.15 @@ -43,12 +46,14 @@ 1.16 printk("gpio module registered (device: %d)\n", major); 1.17 1.18 gpio_direction_output(TEST_OUTPUT_PIN, 0); 1.19 - gpio_direction_input(TEST_INPUT_PIN); 1.20 + gpio_direction_output(TEST_OUTPUT_GREEN, 0); 1.21 1.22 if(gpio_request(TEST_INPUT_PIN, "test_input") != 0) { 1.23 printk("gpio: failed to get input gpio pin\n"); 1.24 return -1; 1.25 } 1.26 + gpio_direction_input(TEST_INPUT_PIN); 1.27 + bcm2708_gpio_setpull(gpio_to_chip(TEST_INPUT_PIN), TEST_INPUT_PIN, BCM2708_PULL_DOWN); 1.28 if((intr_num = gpio_to_irq(TEST_INPUT_PIN)) < 0) { 1.29 printk("gpio: failed to get interrupt line for gpio pin\n"); 1.30 return -1; 1.31 @@ -65,12 +70,17 @@ 1.32 static void dev_exit(void) 1.33 { 1.34 printk("gpio exit\n"); 1.35 + 1.36 unregister_chrdev(major, "gpio"); 1.37 1.38 if(intr_num != -1) { 1.39 free_irq(intr_num, "gpio"); 1.40 } 1.41 gpio_free(TEST_INPUT_PIN); 1.42 + 1.43 + gpio_direction_input(TEST_OUTPUT_PIN); 1.44 + gpio_direction_input(TEST_OUTPUT_GREEN); 1.45 + bcm2708_gpio_setpull(gpio_to_chip(TEST_INPUT_PIN), TEST_INPUT_PIN, BCM2708_PULL_OFF); 1.46 } 1.47 1.48 static int dev_open(struct inode *inode, struct file *file) 1.49 @@ -89,8 +99,6 @@ 1.50 { 1.51 unsigned char val; 1.52 1.53 - printk("dev_write(%d) called\n", sz); 1.54 - 1.55 if(sz < 1) { 1.56 return 0; 1.57 } 1.58 @@ -104,6 +112,15 @@ 1.59 1.60 static irqreturn_t intr_handler(int inum, void *devid, struct pt_regs *regs) 1.61 { 1.62 + static unsigned long ignore_until; 1.63 + unsigned long now = jiffies; 1.64 + 1.65 + if(time_before(now, ignore_until)) { 1.66 + return IRQ_HANDLED; 1.67 + } 1.68 + ignore_until = now + 128 * HZ / 1000; 1.69 + 1.70 printk("presses: %d\n", num_presses++); 1.71 + gpio_set_value(TEST_OUTPUT_GREEN, num_presses & 1); 1.72 return IRQ_HANDLED; 1.73 }