控制IO端口 s3c2410_gpio_setpin()的使用
时间:11-20
来源:互联网
点击:
本文基于FL2440 ARM开发板
Linux内核版本 2.6.28.7
arm-linux-gcc 3.4.1
- #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_EINT00
- #defineIRQ_EINT22
- #defineIRQ_EINT33
- #defineIRQ_EINT432
- #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;
- DECLARE_WAIT_QUEUE_HEAD(io_wait);//声明等待队列
- voidio_con_set();
- staticirqreturn_tio_interrupt_0(intirq,void*dev_id,structpt_regs*regs)
- {
- printk("**********theinterrupt0works**********/n");
- if(flag_0==0)
- {
- cnt=(cnt+1)%2;
- flag_0=1;
- if(cnt==0)
- {
- printk("IN/n");
- 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)
- {
- printk("**********theinterrupt2works**********/n");
- if(flag_2==0)
- {
- cnt=(cnt+1)%2;
- flag_2=1;
- if(cnt==0)
- {
- printk("OUT/n");
- 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);
- wait_event_interruptible(io_wait,flag_0);
- flag_0=0;
- flag_2=0;
- //printk("thevalueis%d/n",io_data);
- copy_to_user(buff,(char*)&cnt,sizeof(cnt));
- }
- 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同上一篇的Makefile
obj-m := my_io.o
KERNELDIR ?= /arm/linux-2.6.28.7-2440
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD)
控制IO端口s3c241 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)