




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、这段时间都在忙驱动了,几个有代表性的简单的外围接口,写写,让我知道什么是所谓的驱动!完全原创,若有转载,请标记出处,若有什么疑问,请联系我:85469843.三峡大学感谢网上朋友的热心帮助,解决了几个硬件方面的疑问!这几天把外围简单的输入驱动给写了下,尽管简单,但还是花了我不少心思呀!/*eint.c*/*实现功能很简单,就是按下接中断的按键后,LED就亮,中间涉及到中断的注册,中断驱动程序的实现.但还没有加入防抖动的功能,实在太懒了!*/#ifndef _KERNEL_#define _KERNEL_#endif#ifndef MODULE#define MODULE#endif#inclu
2、de #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEVICE_MAJOR 253#define DEV_NAME eintstatic int eint_open(struct inode *inode,struct file *filp); static int eint_release(struct inode *inode, struct file *filp);
3、static ssize_t eint_read(struct file *filp,char *buffer,size_t count,loff_t* ppos); static int _init eint_init(void); static void _exit eint_cleanup(void); /static void (*kbdEvent)(void);/void kbdEvent_dummy(void)/#define KBD_TIMER_DELAY (HZ/10)/#define KBD_TIMER_DELAY1 (HZ/100)/#define MAX_KBD_BUF1
4、6/typedef unsigned char KBD_RET;/*typedef structunsigned int kbdStatus;KBD_RET bufMAX_KBD_BUF;unsigned int head,tail;wait_queue_head_t wq;KBD_DEV;static KBD_DEV kbddev;#define BUF_HEAD (kbddev.bufkbddev.head)#define BUF_TAIL (kbddev.bufkbddev.tail)#define INCBUF(x,mod) (+(x)&(mod)-1)static struct ti
5、mer_list kbd_timer;*/ static struct file_operations eint_fops= owner:THIS_MODULE, open:eint_open, release:eint_release, read:eint_read, ; static void isr_kbd(int irq,void *dev_id,struct pt_regs *reg)printk(Occured key board Interrupt,irq=%dn,irq);/*if(kbddev.kbdStatus=KEYSTATUS_UP )KBD_CLOSE_INT();i
6、f(ISKBD_DOWN()kbddev.kbdStatus=KEYSTATUS_DOWNX;kbd_timer.expires=jiffies+KBD_TIMER_DELAY1;add_timer(&kbd_timer);elseKBD_OPEN_INT();*/*(unsigned char*)0xd1000000)=0x00; static int _init eint_init(void) int ret; set_external_irq(IRQ_EINT2,EXT_LOWLEVEL,GPIO_PULLUP_EN);set_external_irq(IRQ_EINT3,EXT_LOW
7、LEVEL,GPIO_PULLUP_EN);/kbdEvent=kbdEvent_dummy; ret=register_chrdev(DEVICE_MAJOR,DEV_NAME,&eint_fops); if (ret0) printk(DEV_NAME cannot get major number!n); return ret; if( check_region( GPIO_F2,1 ) &check_region(GPIO_F3,1) & check_region(0x10000000,1)printk(the port is used by another modulen);retu
8、rn -1; request_region(GPIO_F2,1,DEV_NAME);request_region(GPIO_F3,1,DEV_NAME);request_region(0x10000000,1,DEV_NAME);ret=request_irq(IRQ_EINT2,isr_kbd,SA_INTERRUPT,DEV_NAME,isr_kbd);if(ret) return ret;ret=request_irq(IRQ_EINT3,isr_kbd,SA_INTERRUPT,DEV_NAME,isr_kbd);if(ret) return ret;/*kbddev.head=kbd
9、dev.tail=0;kbddev.kbdStatus=KEYSTATUS_UP;init_waitqueue_head(&(kbddev.wq);init_timer(&kbd_timer);kbd_timer.function=kbd_timer_handler;*/return 0; static void _exit eint_cleanup(void) *(unsigned char*)0xd1000000)=0xff; release_region(GPIO_F2,1); release_region(GPIO_F3,1);release_region(0x10000000,1);
10、free_irq(IRQ_EINT2,DEV_NAME);free_irq(IRQ_EINT3,DEV_NAME);unregister_chrdev(DEVICE_MAJOR,DEV_NAME); static int eint_open(struct inode *inode,struct file *filp) MOD_INC_USE_COUNT; return 0; static int eint_release(struct inode *inode, struct file *filp) MOD_DEC_USE_COUNT;/del_timer(&kbd_timer); print
11、k(release successn); return 0; static ssize_t eint_read(struct file *filp,char *buffer,size_t count,loff_t* ppos) /*retry:if(ascii_key!=z)printk(prepare to copy to user);copy_to_user(buffer,&ascii_key,sizeof(char);return sizeof(char);else/printk(prepare to sleepn);interruptible_sleep_on(&wq);/printk
12、(sleeping!n);if(signal_pending(current)return -ERESTARTSYS;/printk(retry? n);goto retry ;*/return sizeof(char); module_init(eint_init); module_exit(eint_cleanup); /*eint.c*/*测试程序,什么都不做,打开设备后就是等着你去按按键了*/#include #include #include #include /#include static void delay()int i,j,k;for(i=100;i0;i-)for(j=1
13、00;j0;j-)for(k=100;k0;k-)int main(int argc ,char* argv)int fd;char a=0;printf(prepare to open /dev/keybutton n);fd=open(/dev/eint,O_RDONLY);printf(WYQ fd is %dn,fd);if(fd0)printf(keybutton was opened n);while(1)/read(fd,&a,sizeof(a);/printf(%c was read from keyboardn,a);/delay();close(fd);printf(fd
14、was closedn);return 0;/*keybutton.c*/*4X4键盘的驱动,已经加入了防抖动的功能,参考了光盘上的中断处理程序,感谢同学吕贵蓉一起研究了那段恼人的代码!这里面主要是键盘完全没有使用硬件中断,所以只有在open()后不停的轮询,这样就使用了系统内核定时器,而定时器每add_timer()一次,就完了,不能实现每10ms就调用一次扫描操作,所以不得已之下,在中断处理程序里面做了点手脚,你可以看!我想本来应该还要加入原子操作才对的!然后呢,由于是要读外部设备,自然涉及到了阻塞和非阻塞了,这关系到唤醒与睡眠的问题了.这里说的几个问题相信经常编写驱动的高手一定很熟悉了!
15、感谢涂志波师兄借给我的书设备驱动程序,嵌入式系统接口设计与linux驱动程序设计说心底话,真想买一本,但实在太贵了!以后有的话,要考虑收藏一本了!难道真的是书非借不可读也?*/#ifndef _KERNEL_#define _KERNEL_#endif#ifndef MODULE#define MODULE#endif#include #include #include #include #include #include #include #include #include #include #include #include #include #define DEVICE_MAJOR 25
16、1#define DEV_NAME keybuttonstatic int keybutton_open(struct inode *inode,struct file *filp); static int keybutton_release(struct inode *inode, struct file *filp); static ssize_t keybutton_read(struct file *filp,char *buffer,size_t count,loff_t* ppos); static int _init keybutton_init(void); static vo
17、id _exit keybutton_cleanup(void); static char key_get_char(int row,int col);/*#define MAX_KBD_BUF 16typedef unsigned char KBD_RET;typedef struct/unsigned int kbdStatus;KBD_RET bufMAX_KBD_BUF;unsigned int head,tail;wait_queue_head_t wq;static KBD_DEV kbddev;#define BUF_HEAD(kbddev.bufkbddev.head)#def
18、ine BUF_TAIL(kbddev.bufkbddev.tail)#define INCBUF(x,mod)(+(x) & (mod)-1)*/static wait_queue_head_t wq;enum KEYBOARD_SCAN_STATUSKEYBOARD_SCAN_FIRST,KEYBOARD_SCAN_SECOND,KEYBOARD_SCAN_THIRD,KEYBOARD_SCAN_FOURTH;int row = 0;/extern unsigned char output_0x10000000;static unsigned char ascii_key=z, input
19、_key4, input_key14, key_mask = 0x0F;static unsigned char*keyboard_port_scan = (unsigned char*)0xd1000000;static unsigned char*keyboard_port_value = (unsigned char*)0xd1000002;static int keyboard_scan_status4 = KEYBOARD_SCAN_FIRST, KEYBOARD_SCAN_FIRST, KEYBOARD_SCAN_FIRST, KEYBOARD_SCAN_FIRST;static
20、struct timer_list kbd_timer;static char key_get_char(int row, int col)char key = 0;printk(key_get_charn);switch( row )case 0:if(col & 0x01) = 0) key = 0; else if(col & 0x02) = 0) key =A; else if(col & 0x04) = 0) key = B; else if(col & 0x08) = 0) key = F; break;case 1:if(col & 0x01) = 0) key = 7; els
21、e if(col & 0x02) = 0) key = 8; else if(col & 0x04) = 0) key = 9;else if(col & 0x08) = 0) key = E;break;case 2:if(col & 0x01) = 0) key = 4; else if(col & 0x02) = 0) key = 5; else if(col & 0x04) = 0) key = 6; else if(col & 0x08) = 0) key = D; break;case 3:if(col & 0x01) = 0) key = 1; else if(col & 0x0
22、2) = 0) key = 2; else if(col & 0x04) = 0) key = 3; else if(col & 0x08) = 0) key = C; break;default:break;return key;static void recv_key(unsigned char c)unsigned char cc;cc=c;/printk(recv_key recevied char %c,cc);static void Kbd_Scan(unsigned long data)int loopcnt = row, bexit = 0;int temp;/printk(1
23、0ms was past! begin to scan n);del_timer(&kbd_timer);for( loopcnt = row; loopcnt = 4)temp = loopcnt - 4;elsetemp = loopcnt;/printk(temp is %dn,temp);switch(keyboard_scan_statustemp)case KEYBOARD_SCAN_FIRST:/printk(status is %dn,keyboard_scan_statustemp);*keyboard_port_scan = (0x00000001temp); keyboa
24、rd_scan_statustemp = KEYBOARD_SCAN_SECOND;bexit = 1;break;case KEYBOARD_SCAN_SECOND:/ printk(status is %dn,keyboard_scan_statustemp);input_keytemp = (*keyboard_port_value) & key_mask;if(input_keytemp = key_mask)keyboard_scan_statustemp = KEYBOARD_SCAN_FIRST;elsekeyboard_scan_statustemp=KEYBOARD_SCAN
25、_THIRD;printk(some key was down mainly!n);bexit = 1;break;case KEYBOARD_SCAN_THIRD:/ printk(status is %dn,keyboard_scan_statustemp);if (*keyboard_port_value) & key_mask) != input_keytemp) keyboard_scan_statustemp = KEYBOARD_SCAN_FIRST;elseascii_key = key_get_char(temp, input_keytemp);printk(prepare
26、to wake upn);wake_up_interruptible(&wq);/ascii_key=z;printk(has waken up n);printk(ooooooooooooooooothe char is %cn,ascii_key);keyboard_scan_statustemp = KEYBOARD_SCAN_FOURTH;printk(some key was really down);*keyboard_port_scan = (0x00000001temp); bexit = 1;break;case KEYBOARD_SCAN_FOURTH:/ printk(s
27、tatus is %dn,keyboard_scan_statustemp);input_key1temp = (*keyboard_port_value) & key_mask;if(input_key1temp = key_mask)/ get a keyrecv_key(ascii_key);printk(key was up now!n);keyboard_scan_statustemp = KEYBOARD_SCAN_FIRST;else*keyboard_port_scan = (0x00000001temp); bexit = 1;break;if(bexit)break;row
28、 = temp;kbd_timer.expires=jiffies+HZ/100; add_timer(&kbd_timer); static struct file_operations keybutton_fops= owner:THIS_MODULE, open:keybutton_open, release:keybutton_release, read:keybutton_read, ; /*keybutton_init*/ static int _init keybutton_init(void) int ret; ret=register_chrdev(DEVICE_MAJOR,
29、DEV_NAME,&keybutton_fops); if (ret0) printk(DEV_NAME cannot get major number!n); return ret; if( check_region( 0x10000000,1 ) &check_region(0x10000002,1)printk(the port is used by another modulen);return -1; request_region(0x10000000,1,DEV_NAME);request_region(0x10000002,1,DEV_NAME);/*kbddev.head=kb
30、ddev.tail=0;*/init_waitqueue_head(&wq);printk(success to request_regionn);return 0; /*keybutton_cleanup*/static void _exit keybutton_cleanup(void) release_region(0x10000000,1); release_region(0x10000002,1);unregister_chrdev(DEVICE_MAJOR,DEV_NAME); static int keybutton_open(struct inode *inode,struct
31、 file *filp) init_timer(&kbd_timer);kbd_timer.function=Kbd_Scan; kbd_timer.expires=jiffies+HZ/100; add_timer(&kbd_timer); MOD_INC_USE_COUNT; printk(open device keybuttonn);printk(timer has added into systemn);return 0; static int keybutton_release(struct inode *inode, struct file *filp) MOD_DEC_USE_
32、COUNT;/del_timer(&kbd_timer); printk(release successn); return 0; /* static KBD_RET kbdRead(void)KBD_RET kbd_ret;kbd_ret=BUF_TAIL;kbddev.tail=INCBUF(kbddev.tail,MAX_KBD_BUF);return kbd_ret; */static ssize_t keybutton_read(struct file *filp,char *buffer,size_t count,loff_t* ppos) /add_timer(&kbd_time
33、r);/printk(timer has added into systemn);retry:if(ascii_key!=z)printk(prepare to copy to user);copy_to_user(buffer,&ascii_key,sizeof(char);return sizeof(char);else/printk(prepare to sleepn);interruptible_sleep_on(&wq);/printk(sleeping!n);if(signal_pending(current)return -ERESTARTSYS;/printk(retry? n
34、);goto retry ;return sizeof(char); module_init(keybutton_init); module_exit(keybutton_cleanup); keytest.c*/*已经没有什么新意了,测试程序都是这样的了!*/#include #include #include #include /#include static void delay()int i,j,k;for(i=100;i0;i-)for(j=100;j0;j-)for(k=100;k0;k-)int main(int argc ,char* argv)int fd;char a=0;
35、printf(prepare to open /dev/keybutton n);fd=open(/dev/keybutton,O_RDONLY);printf(WYQ fd is %dn,fd);if(fd0)printf(keybutton was opened n);read(fd,&a,sizeof(a);printf(%c was read from keyboardn,a);delay();close(fd);printf(fd was closedn);return 0;adv.c*/*A/D转换的驱动,终于感受了一把转化!太受不了自己了,一个研究生了,居然一直都没有用转换做过东
36、西!哎,人水了,没有办法!原来转换可以用两种机制来实现的,一种是读转换结束标志位,一种是才用转换结束中断,我这个才用的读转换结束标志位,比较简单的一种。我看网上的一个的驱动,两个都用了,我想应该是他错了吧!希望有网友能批评我的观点!*/#ifndef _KERNEL_#define _KERNEL_#endif#ifndef MODULE#define MODULE#endif#include #include #include #include #include #include #include #include #include #include #include #include #i
37、nclude #define DEVICE_MAJOR 252#define DEV_NAME ADVstatic int ADV_open(struct inode *inode,struct file *filp); static int ADV_release(struct inode *inode, struct file *filp); static ssize_t ADV_read(struct file *filp,char *buffer,size_t count,loff_t* ppos); static int _init ADV_init(void); static void _exit ADV_cleanup(void); /static wait_queue_head_t wq;/static struct timer_list kbd_timer; static struct file_o
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 IEC 60684-2:2025 EN-FR Flexible insulating sleeving - Part 2: Methods of test
- 【正版授权】 IEC 63522-20:2025 EN-FR Electrical relays – Tests and measurements - Part 20: Mechanical endurance
- 2025年学前教育与儿童发展考试试卷及答案
- 2025年全球化与地方文化保护考试题及答案
- 2025年按摩与推拿专业考试题及答案
- 2025年茶艺师职业能力测试试卷及答案
- 万安保安考试题及答案
- 梯级城市基础设施优化补充协议
- 商住两用房产分割与资产重组投资协议
- 网络零售商网店经营权保留及数据分析服务协议
- 家庭教育指导流程
- 生产性服务业集聚对我国制造业全球价值链地位影响的门槛效应研究
- JB T 5528-2005压力表标度及分划
- 图形设计方法同构、替构、解构、重构
- 民法典之婚姻家庭编案例解读课件
- SCA涂胶机内部培训资料课件
- 学校澡堂运营方案
- 门窗展厅培训课件
- 税务会计学(第 14版)习题参考答案
- 海康产品及公司介绍全系列
- 国开电大软件工程形考作业3参考答案
评论
0/150
提交评论