根据FL2440开发板,内核版别2.6.28
主机渠道:Ubuntu 11.04 内核版别2.6.39
驱动代码
- #include
- #include
- #include
- #include
- #include
- #include
interrupt.h>/*设置中止方法*/ - #include
- #include
- #include
- #include
- #include
- //设备名
- #defineIO_DEVICE_NAME”my_io”
- //主设备号
- #defineIO_DEVICE_MAJOR240
- //次设备号
- #defineIO_DEVICE_SECONDARY32
- //回来一个数x的第y位
- #defineMYBIT(x,y)((x>>y)%2)
- #ifndef_LINUX_IRQRETURN_H
- #define_LINUX_IRQRETURN_H
- typedefintirqreturn_t;
- #defineIRQ_NONE(0)
- #defineIRQ_HANDLED(1)
- #defineIRQ_RETVAL(x)((x)!=0)
- #endif
- /*
- *S3C2410GPIOedgedetectionforIRQs:
- *IRQsaregeneratedonFalling-Edge,Rising-Edge,both,lowlevelorhigglevel.
- *Thismustbecalled*before*thecorrespondingIRQisregistered.
- */
- #defineEXT_LOWLEVEL0
- #defineEXT_HIGHLEVEL1
- #defineEXT_FALLING_EDGE2
- #defineEXT_RISING_EDGE4
- #defineEXT_BOTH_EDGES6
- staticintflag_0,flag_2;//中止转化标志
- staticintcnt;
- intdata;
- DECLARE_WAIT_QUEUE_HEAD(io_wait);//声明等候行列
- voidio_con_set();
- staticirqreturn_tio_interrupt_0(intirq,void*dev_id,structpt_regs*regs)
- {
- if(flag_0==0)
- {
- printk(“**********theinterrupt0works**********\n”);
- cnt=(cnt+1)%2;
- flag_0=1;
- if(cnt==0)
- {
- printk(“IN\n”);
- data=1;
- s3c2410_gpio_setpin(S3C2410_GPB5,0);
- s3c2410_gpio_setpin(S3C2410_GPB6,1);
- }
- wake_up_interruptible(&io_wait);
- }
- returnIRQ_HANDLED;
- }
- staticirqreturn_tio_interrupt_2(intirq,void*dev_id,structpt_regs*regs)
- {
- if(flag_2==0)
- {
- printk(“**********theinterrupt2works**********\n”);
- cnt=(cnt+1)%2;
- flag_2=1;
- if(cnt==0)
- {
- printk(“OUT\n”);
- data=0;
- s3c2410_gpio_setpin(S3C2410_GPB5,1);
- s3c2410_gpio_setpin(S3C2410_GPB6,0);
- }
- wake_up_interruptible(&io_wait);
- }
- returnIRQ_HANDLED;
- }
- staticintio_open(structinode*inode,structfile*file)//翻开设备函数
- {
- intret;
- set_irq_type(IRQ_EINT0,EXT_FALLING_EDGE);//设置中止0触发方法
- set_irq_type(IRQ_EINT2,EXT_FALLING_EDGE);//设置中止2触发方法
- //EXT_LOWLEVEL
- //EXT_HIGHLEVEL
- //EXT_FALLING_EDGE
- //EXT_RISING_EDGE
- //EXT_BOTH_EDGES
- disable_irq(IRQ_EINT0);
- disable_irq(IRQ_EINT2);
- enable_irq(IRQ_EINT0);
- enable_irq(IRQ_EINT2);
- ret=request_irq(IRQ_EINT0,io_interrupt_0,IRQF_SHARED,IO_DEVICE_NAME,1);//注册中止0
- if(ret<0)
- {
- printk(“IRQ%dcannotrequest\n”,IRQ_EINT0);
- returnret;
- }
- ret=request_irq(IRQ_EINT2,io_interrupt_2,IRQF_SHARED,IO_DEVICE_NAME,1);//注册中止2
- if(ret<0)
- {
- printk(“IRQ%dcannotrequest\n”,IRQ_EINT2);
- returnret;
- }
- printk(“thedeviceisopened\n”);
- io_con_set();
- cnt=0;
- return0;
- }
- voidio_con_set()//IO端口操控寄存器初始化
- {
- s3c2410_gpio_cfgpin(S3C2410_GPF0,S3C2410_GPF0_EINT0);
- s3c2410_gpio_cfgpin(S3C2410_GPF2,S3C2410_GPF2_EINT2);
- s3c2410_gpio_cfgpin(S3C2410_GPB5,S3C2410_GPB5_OUTP);
- s3c2410_gpio_cfgpin(S3C2410_GPB6,S3C2410_GPB6_OUTP);
- }
- staticintio_close(structinode*inode,structfile*file)//设备封闭函数
- {
- free_irq(IRQ_EINT0,1);//开释中止
- free_irq(IRQ_EINT2,1);//开释中止
- printk(“thedeviceisclosed\n”);
- return0;
- }
- staticssize_tio_read(structfile*filp,char*buff,size_tcount,loff_t*f_ops)//读取IO端口
- {
- wait_event_interruptible(io_wait,flag_0&flag_2);
- flag_0=0;
- flag_2=0;
- copy_to_user(buff,(char*)&data,sizeof(data));
- }
- staticstructfile_operationsio_device_fops=
- {
- .owner=THIS_MODULE,
- .read=io_read,
- .open=io_open,
- .release=io_close,
- };
- staticint__initio_init(void)//insmod加载驱动时碑文
- {
- intret;
- ret=register_chrdev(IO_DEVICE_MAJOR,IO_DEVICE_NAME,&io_device_fops);
- if(ret<0)
- {
- printk(“Failtoregistthedevice\n”);
- returnret;
- }
- return0;
- }
- staticint__exitio_exit(void)//rmmod卸载驱动时碑文
- {
- unregister_chrdev(IO_DEVICE_MAJOR,IO_DEVICE_NAME);
- printk(“thedevicehasbeenunregisted\n”);
- }
- module_init(io_init);
- module_exit(io_exit);
- MODULE_LICENSE(“GPL”);
Makefile
obj-m := my_io.o
KERNELDIR ?= /arm/linux-2.6.28.7-2440
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -f *.o *.ko *.order *.symvers
调用代码: