📄 gpio_irq.c
字号:
#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/init.h>#include <linux/compiler.h>#include <linux/configfs.h>#include <linux/delay.h>#include <linux/errno.h>#include <linux/fcntl.h>#include <linux/fs.h>#include <linux/interrupt.h>#include <linux/ioport.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/slab.h>#include <linux/spinlock.h>#include <linux/timer.h>#include <linux/types.h>#include <linux/version.h>#include <linux/vmalloc.h>#include <asm/hardware.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/segment.h>#include <asm/signal.h>#include <asm/system.h>#include <asm/uaccess.h>#include <asm-arm/arch-mxc/mx27_pins.h>#define PTB_DDIRvalue 0xFA000000#define PTB_OCR2value 0xF0000000#define PTB_SWRvalue 0x00000001#define PTB_DRvalue 0x00000000#define PTB_DDIRadd 0x10015100#define INTCNTLadd 0x10040000unsigned long start1 = 0x10015100;unsigned long lenth1 = 0x501;unsigned long start2 = 0x10040000;unsigned long lenth2 = 0x65;unsigned long size = 0x5;unsigned long PTB_DDIRvtadd;unsigned long INTCNTLvtadd;unsigned int PTB_DDIRread;unsigned int PTB_OCR1read;unsigned int PTB_OCR2read;unsigned int PTB_ICONFA1read;unsigned int PTB_ICONFA2read;unsigned int PTB_ICONFB1read;unsigned int PTB_ICONFB2read;unsigned int PTB_DRread;unsigned int PTB_GIUSread;unsigned int PTB_SSRread;unsigned int PTB_ICR1read;unsigned int PTB_ICR2read;unsigned int PTB_IMRread;unsigned int PTB_ISRread;unsigned int PTB_GPRread;unsigned int PTB_SWRread;unsigned int PTB_PUENread;unsigned int INTCNTLread;unsigned int NIMASKread;unsigned int INTENNUMread;unsigned int INTDISNUMread;unsigned int INTENABLEHread;unsigned int INTENABLELread;unsigned int INTTYPEHread;unsigned int INTTYPELread;unsigned int NIPRIORITY7read;unsigned int NIPRIORITY6read;unsigned int NIPRIORITY5read;unsigned int NIPRIORITY4read;unsigned int NIPRIORITY3read;unsigned int NIPRIORITY2read;unsigned int NIPRIORITY1read;unsigned int NIPRIORITY0read;unsigned int NIVECSRread;unsigned int FIVECSRread;unsigned int INTSRCHread;unsigned int INTSRCLread;unsigned int INTFRCHread;unsigned int INTFRCLread;unsigned int NIPNDHread;unsigned int NIPNDLread;unsigned int FIPNDHread;unsigned int FIPNDLread;unsigned int GPIO_irq_major = 0;unsigned int count = 0;#define GPIO_irq_IRQ IOMUX_TO_IRQ(MX27_PIN_USBH1_RXDP)struct resource *GPIO_mem_resource;struct resource *AITC_mem_resource;static ssize_t test_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos);static ssize_t test_write(struct file *file,const char __user *buffer, size_t count,loff_t *f_pos);static int test_open(struct inode *inode, struct file *file);static int test_release(struct inode *inode,struct file *file);int init_GPIO_irq_module(void);void cleanup_GPIO_irq_module(void);static struct file_operations chr_fops={ .read = test_read, .write = test_write, .open = test_open, .release = test_release,};static int test_open(struct inode *inode, struct file *file){ printk("This is open !\n\n"); GPIO_mem_resource = request_mem_region(start1, lenth1, "GPIO_irq"); AITC_mem_resource = request_mem_region(start2, lenth2, "GPIO_irq"); if (GPIO_mem_resource == NULL || AITC_mem_resource == NULL) { release_mem_region(start2,lenth2); release_mem_region(start1, lenth1); printk("request_mem_resource failure !\n"); } else printk("request_mem_resource success !\n"); PTB_DDIRvtadd = (unsigned long) ioremap_nocache(start1, lenth1); INTCNTLvtadd = (unsigned long) ioremap_nocache(start2, lenth2); printk("ioremap success !\n"); printk("this is the first read and write !\n\n"); (void) iowrite32((u32) PTB_SWRvalue, PTB_DDIRvtadd+60); wmb(); (void) iowrite32((u32) PTB_DDIRvalue, PTB_DDIRvtadd); (void) iowrite32((u32) PTB_OCR2value, PTB_DDIRvtadd+8); wmb(); return 0;}static ssize_t test_write(struct file *file,const char __user *buffer, size_t count,loff_t *f_pos){ int i; printk("this is the second write !\n"); for (i=0; i<100; i++) (void) iowrite32((u32) PTB_DRvalue, PTB_DDIRvtadd+28); return count;}static ssize_t test_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos){ printk("this is the second read !\n\n"); PTB_DDIRread = ioread32(PTB_DDIRvtadd); printk("PTB_DDIRvalue = %x\n",PTB_DDIRread); PTB_OCR2read = ioread32(PTB_DDIRvtadd+8); printk("PTB_OCR2value = %x\n",PTB_OCR2read); PTB_DRread = ioread32(PTB_DDIRvtadd+28); printk("PTB_DRvalue = %x\n",PTB_DRread); PTB_GIUSread = ioread32(PTB_DDIRvtadd+32); printk("PTB_GIUSvalue = %x\n",PTB_GIUSread); PTB_SSRread = ioread32(PTB_DDIRvtadd+36); printk("PTB_SSRvalue = %x\n",PTB_SSRread); PTB_IMRread = ioread32(PTB_DDIRvtadd+48); printk("PTB_IMRvalue = %x\n",PTB_IMRread); PTB_ISRread = ioread32(PTB_DDIRvtadd+52); printk("PTB_ISRvalue = %x\n",PTB_ISRread); PTB_GPRread = ioread32(PTB_DDIRvtadd+56); printk("PTB_GPRvalue = %x\n",PTB_GPRread); PTB_SWRread = ioread32(PTB_DDIRvtadd+60); printk("PTB_SWRvalue = %x\n",PTB_SWRread); PTB_PUENread = ioread32(PTB_DDIRvtadd+64); printk("PTB_PUENvalue = %x\n",PTB_PUENread); rmb(); printk("ioread again success !\n"); return count;}static int test_release(struct inode *inode,struct file *file){ printk("this is released !\n"); return 0;}int init_GPIO_irq_module(void){ int res; printk("hello world!\n"); res=register_chrdev(0,"GPIO_irq",&chr_fops); if(res<0) { printk("can't get major name!\n"); return res; } if(GPIO_irq_major == 0) GPIO_irq_major = res; printk("the device major number is %d\n",GPIO_irq_major); return 0;}void cleanup_GPIO_irq_module(void){ printk("goodbye world!\n"); iounmap((void *)PTB_DDIRvtadd); iounmap((void *)INTCNTLvtadd); printk("iounmap success !\n"); release_mem_region(start1, lenth1); release_mem_region(start2, lenth2); printk("release_mem_region success !\n"); unregister_chrdev(GPIO_irq_major,"GPIO_irq"); printk("free_irq success !\n");}module_init(init_GPIO_irq_module);module_exit(cleanup_GPIO_irq_module);MODULE_LICENSE("Dual BSD/GPL");MODULE_AUTHOR("xing-tao_Wang");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -