📄 testgpio.c
字号:
/*THIS file is to test the gpio state and gpio interruptwe set the gpio mod in GPIO_init,request irq in simple_init,in the irq handler--simple_irq,we write_GPIO then we read_GPIO_state*/#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/miscdevice.h>#include <linux/sched.h>#include <linux/delay.h>#include <linux/poll.h>#include <linux/spinlock.h>#include <asm/irq.h>#include <linux/delay.h>#include <asm/hardware.h>#define GPIO_12 12#define GPIO_11 11#define GPIO_22 22#define GPIO_17 17#define GPIO_91 91#define SIMPLE_MAJOR 235#define SIMPLE_DEVICE_NAME "test_gpio"#define SIMPLE_IRQ IRQ_GPIO(GPIO_12) //the no. of the interrupt of gpio12 is dirived from the macro IRQ_GPIO()//--------------------------------------------------------------------------------static void write_GPIO(int gpio_nr);static void read_GPIO_state(int gpio_port);//-----------------------------------------------------------------------static ssize_t simple_read (struct file *file, char * buf, size_t count,loff_t * offset){ printk("simple_read!\n"); return 0;}static ssize_t simple_write (struct file *file, const char * buf, size_t count,loff_t * offset){ printk("simple_write has been called\n"); write_GPIO(GPIO_11); write_GPIO(GPIO_91); return 0;}int simple_ioctl (struct inode *inode, struct file *file, unsigned int cmd,unsigned long arg){ return 0;}static int simple_open(struct inode *inode, struct file *file){ printk("simple_open has been called \n"); return 0;}static int simple_release(struct inode *inode, struct file *file){ printk("simple_release has been called\n"); return 0;}static struct file_operations simple_fops = { .owner = THIS_MODULE, .read = simple_read, .write = simple_write, .ioctl = simple_ioctl, .open = simple_open, .release = simple_release,};//----------------------------------------------------------------------------//set the gpio modestatic void GPIO_init(void){ set_GPIO_IRQ_edge(GPIO_12, GPIO_RISING_EDGE); //set gpio12 be irq set_GPIO_mode(GPIO_11 | GPIO_OUT); //set gpio 16 be alternate function 1,out set_GPIO_mode(GPIO_22 | GPIO_OUT); //set gpio 17 be in mode set_GPIO_mode(GPIO_91 | GPIO_OUT); //set gpio 91 be out mod }//let the pin first be high then be low,after each set,there is a 2 second delaystatic void write_GPIO(int gpio_nr){ int i; printk("\nwriting to gpio... ...%d\n",gpio_nr); GPSR(gpio_nr)|=GPIO_bit(gpio_nr); printk("\nreading the gpio%d state after set ... ...\n",gpio_nr); read_GPIO_state(gpio_nr); for(i=1;i<200;i++) { mdelay(20); } GPCR(gpio_nr)|=GPIO_bit(gpio_nr); printk("\nreading the gpio state after clear... ...\n"); read_GPIO_state(gpio_nr); for(i=1;i<200;i++) { mdelay(20); }}//irq handler,we write GPIO_91 for twicestatic void simple_irq(int irq, void *dev_id, struct pt_regs *reg){ printk("Now gpio interrupt occur!!!\n"); write_GPIO(GPIO_11); write_GPIO(GPIO_11); write_GPIO(GPIO_11); //write_GPIO(GPIO_22); //write_GPIO(GPIO_91); }//read the gpio's state by reading the relative bits in GPIO control registersstatic void read_GPIO_state(int gpio_port){ unsigned long gafr_mask,tmp; if((GPDR(gpio_port)&GPIO_bit(gpio_port))) printk("GPIO%d pin's direction is out\n",gpio_port); else printk("GPIO%d pin's direction is in\n",gpio_port); if((GPLR(gpio_port)&GPIO_bit(gpio_port))) printk("GPIO%d pin's voltage is high\n",gpio_port); else printk("GPIO%d pin's voltage is low\n",gpio_port); if((GRER(gpio_port)&GPIO_bit(gpio_port))) printk("GPIO%d pin's interrupt is rising edge\n",gpio_port); if((GEDR(gpio_port)&GPIO_bit(gpio_port))) printk("GPIO%d pin's rising edge interrupt has occured\n",gpio_port); gafr_mask=GPIO_bit(gpio_port*2)|GPIO_bit(gpio_port*2+1); tmp=(~GAFR(gpio_port))&gafr_mask; if(gafr_mask==tmp) printk("GPIO%d hasn't been set to alternate function yet\n",gpio_port); else printk("GPIO%d has already been set to alternate function!!\n",gpio_port);}static int __init simple_init(void){ int ret,i; int t[6]={11,17,33,65,97,118}; int j,x; unsigned long m[6]; ret=register_chrdev(SIMPLE_MAJOR, SIMPLE_DEVICE_NAME, &simple_fops); if(ret) { printk(SIMPLE_DEVICE_NAME"can't register the device keypad"); } printk("reading the gpio state befor gpio_init... ...\n"); //read_GPIO_state(GPIO_12); read_GPIO_state(GPIO_11); //read_GPIO_state(GPIO_17); //read_GPIO_state(GPIO_91); GPIO_init(); printk("\nreading the gpio state after gpio_init... ...\n"); //read_GPIO_state(GPIO_12); read_GPIO_state(GPIO_11); //read_GPIO_state(GPIO_17); //read_GPIO_state(GPIO_91); ret=request_irq( SIMPLE_IRQ, &simple_irq, SA_INTERRUPT, SIMPLE_DEVICE_NAME, &simple_irq); printk("\nthe value irq return is %d\n",ret); if(ret) { unregister_chrdev(SIMPLE_MAJOR, SIMPLE_DEVICE_NAME); printk(SIMPLE_DEVICE_NAME " can't request irqs\n"); return ret; } else printk("irq successful!!!!\n\n"); return 0;}static void __exit simple_exit(void){ free_irq(SIMPLE_IRQ,&simple_irq); unregister_chrdev(SIMPLE_MAJOR, SIMPLE_DEVICE_NAME); }MODULE_LICENSE("GPL");module_init(simple_init);module_exit(simple_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -