2716|2

76

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

S3C2440按键驱动 [复制链接]

  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. #include
  11. //#include
  12. #include
  13. #include
  14. #include

  15. #define KEYNUM 5
  16. #define KEY_UP 0
  17. #define KEY_DOWN 1
  18. #define KEY_DOWNX 2
  19. #define MAJOR 255
  20. #define KEY_TIMER_DELAY (HZ/50)
  21. #define GPIOG_REG_ADDR 0x56000064//gpio_g data register phys address

  22. struct b_dev
  23. {
  24.   struct cdev b_cdev;
  25.   char key_state_buf[KEYNUM];//up or down
  26.   unsigned int irq[KEYNUM];
  27.   struct timer_list tl;
  28. };

  29. struct b_dev *pb_dev;
  30. int b_open(struct inode * inodep,struct file * filp)
  31. {
  32.   return 0;
  33. }
  34. int b_release(struct inode * inodep,struct file * filp)
  35. {
  36.   return 0;
  37. }
  38. int b_read(struct file * filp,char * buf,size_t size,loff_t offset)
  39. {
  40.   if(!access_ok(VERIFY_WRITE,buf,size))
  41.   {
  42.     printk(KERN_INFO "buf can't write\n");
  43.     return -EFAULT;
  44.   }
  45.   int key_num,count=0;
  46.   for(key_num=0;key_num!=KEYNUM;key_num++)
  47.   {
  48.     if(pb_dev->key_state_buf[key_num]==KEY_DOWNX && count
  49.     {
  50.       put_user('0'+key_num,buf+count);
  51.       pb_dev->key_state_buf[key_num]=KEY_UP;
  52.       count++;
  53.     }
  54.   }
  55.   return count;
  56. }
  57. struct file_operations f_ops=
  58. {
  59.   .open=b_open,
  60.   .read=b_read,
  61.   .release=b_release,
  62.   .owner=THIS_MODULE,
  63. };

  64. void timer_handler(unsigned long sleep_time)
  65. {
  66.   unsigned int *p_gpiog=__va(GPIOG_REG_ADDR);//phys address to virtual address
  67.   unsigned int gpio_g=*p_gpiog;
  68.   if(gpio_g & 0x0001  && pb_dev->key_state_buf[0] == KEY_DOWN){
  69.     //button 1
  70.     pb_dev->key_state_buf[0]=KEY_DOWNX;
  71.   }
  72.   else if(gpio_g & 0x0008 && pb_dev->key_state_buf[1] == KEY_DOWN){
  73.     //button 2
  74.     pb_dev->key_state_buf[1]=KEY_DOWNX;
  75.   }
  76.   else if(gpio_g & 0x0020 && pb_dev->key_state_buf[2] == KEY_DOWN){
  77.     //button 3
  78.     pb_dev->key_state_buf[2]=KEY_DOWNX;
  79.   }
  80.   else if(gpio_g & 0x0040 && pb_dev->key_state_buf[3] == KEY_DOWN){
  81.     //button 4
  82.     pb_dev->key_state_buf[3]=KEY_DOWNX;
  83.   }
  84.   else if(gpio_g & 0x0080 && pb_dev->key_state_buf[4] == KEY_DOWN){
  85.     //button 5
  86.     pb_dev->key_state_buf[4]=KEY_DOWNX;
  87.   }
  88.   else if(gpio_g & 0x0800 && pb_dev->key_state_buf[5] == KEY_DOWN){
  89.     //button 6
  90.     pb_dev->key_state_buf[5]=KEY_DOWNX;
  91.   }
  92.   printk(KERN_INFO "timer\n");
  93. }
  94. void irq_handler(int irq,void *dev_id,struct pt_regs *regs)
  95. {
  96.   pb_dev->tl.expires=jiffies+KEY_TIMER_DELAY;
  97.   add_timer(&pb_dev->tl);
  98.   switch(irq)
  99.   {
  100.     case 16+36:
  101.       pb_dev->key_state_buf[0]=KEY_DOWN;
  102.       break;
  103.     case 16+39:
  104.       pb_dev->key_state_buf[1]=KEY_DOWN;
  105.       break;
  106.     case 16+41:
  107.       pb_dev->key_state_buf[2]=KEY_DOWN;
  108.       break;
  109.     case 16+42:
  110.       pb_dev->key_state_buf[3]=KEY_DOWN;
  111.       break;
  112.     case 16+43:
  113.       pb_dev->key_state_buf[4]=KEY_DOWN;
  114.       break;
  115.     case 16+47:
  116.       pb_dev->key_state_buf[5]=KEY_DOWN;
  117.       break;
  118.     default:
  119.       break;
  120.   }
  121.   printk(KERN_INFO "IRQ\n");
  122. }

  123. void init_irqs()
  124. {
  125.   pb_dev->irq[0]=16+36;
  126.   pb_dev->irq[1]=16+39;
  127.   pb_dev->irq[2]=16+41;
  128.   pb_dev->irq[3]=16+42;
  129.   pb_dev->irq[4]=16+43;
  130.   pb_dev->irq[5]=16+47;
  131.   int key_num;
  132.   for(key_num=0;key_num
  133.   {
  134.     request_irq(pb_dev->irq[key_num],irq_handler,SA_SHIRQ,"buttons",pb_dev);
  135.     set_irq_type(pb_dev->irq[key_num],IRQ_TYPE_EDGE_FALLING);
  136.   }
  137. }
  138. static int b_init(void)
  139. {
  140.   pb_dev=kmalloc(sizeof(struct b_dev),GFP_KERNEL);
  141.   memset(pb_dev,0,sizeof(struct b_dev));
  142.   int key_num;
  143.   for(key_num=0;key_num
  144.   {
  145.     pb_dev->key_state_buf[key_num]=KEY_UP;
  146.   }
  147.   dev_t devno;
  148.   devno=MKDEV(MAJOR,0);
  149.   cdev_init(&pb_dev->b_cdev,&f_ops);
  150.   pb_dev->b_cdev.owner=THIS_MODULE;
  151.   pb_dev->b_cdev.ops=&f_ops;
  152.   int err=cdev_add(&pb_dev->b_cdev,devno,1);
  153.   if(err)
  154.     printk(KERN_NOTICE "Error %d\n",err);
  155.   setup_timer(&pb_dev->tl,timer_handler,20);
  156.   init_irqs();
  157.   printk(KERN_INFO "init over\n");
  158. }

  159. static void b_exit(void)
  160. {
  161.   int key_num;
  162.   for(key_num=0;key_num
  163.   {
  164.     free_irq(pb_dev->irq[key_num],pb_dev);
  165.   }
  166.   cdev_del(&pb_dev->b_cdev);
  167.   kfree(pb_dev);
  168.   unregister_chrdev_region(MKDEV(MAJOR,0),1);
  169. }

  170. MODULE_AUTHOR("scy");
  171. MODULE_LICENSE("Dual BSD/GPL");

  172. module_init(b_init);
  173. module_exit(b_exit);
复制代码

当有按键时,系统就会报错
[root@FriendlyARM /]# IRQ
irq event 59: bogus return value 7
[] (dump_stack+0x0/0x14) from [] (__report_bad_irq+0x28/0x94
)
[] (__report_bad_irq+0x0/0x94) from [] (note_interrupt+0x174
/0x1f0)
r5:c03d5be0 r4:c03d5be0
[] (note_interrupt+0x0/0x1f0) from [] (handle_edge_irq+0xc0/
0x160)
[] (handle_edge_irq+0x0/0x160) from [] (s3c_irq_demux_extint
8+0x9c/0xa8)
r7:c03ff284 r6:00000001 r5:00000000 r4:0000003b
[] (s3c_irq_demux_extint8+0x0/0xa8) from [] (__exception_tex
t_start+0x44/0x70)
r7:c03ff284 r6:00000000 r5:c0425474 r4:00000015
[] (__exception_text_start+0x0/0x70) from [] (__irq_svc+0x24
/0xa0)
Exception stack(0xc03cdf40 to 0xc03cdf88)
df40: f4100000 00000032 f4100000 60000013 c0029edc c03cc000 c0029edc c03ff284
df60: 3001c920 41129200 3001c8ec c03cdf94 c03cdf98 c03cdf88 c002a4f8 c0029f3c
df80: 60000013 ffffffff
r7:c03ff284 r6:00000020 r5:f4000000 r4:ffffffff
[] (default_idle+0x0/0xac) from [] (cpu_idle+0x4c/0x68)
[] (cpu_idle+0x0/0x68) from [] (rest_init+0x5c/0x70)
r7:c03d0440 r6:c001e000 r5:c03ff240 r4:c0423264
[] (rest_init+0x0/0x70) from [] (start_kernel+0x200/0x268)
[] (start_kernel+0x0/0x268) from [<30008034>] (0x30008034)
r6:c001ed34 r5:c03ff35c r4:c0007175
handlers:
[] (irq_handler+0x0/0x130 [my_bdri])
Unable to handle kernel paging request at virtual address e6000064
pgd = c3acc000
[e6000064] *pgd=00000000
Internal error: Oops: 5 [#1]
Modules linked in: my_bdri
CPU: 0    Not tainted  (2.6.29.4-FriendlyARM #2)
PC is at timer_handler+0x10/0x118 [my_bdri]
LR is at run_timer_softirq+0x1b0/0x230
pc : []    lr : []    psr: 20000013
sp : c03cde80  ip : c03cde90  fp : c03cde8c
r10: c04211c8  r9 : c03d4140  r8 : c03cc000
r7 : bf0000bc  r6 : c03cdea0  r5 : 00000100  r4 : c04209c0
r3 : e6000000  r2 : c03cdea0  r1 : c3a612dc  r0 : 00000014
Flags: nzCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment kernel
Control: c000717f  Table: 33acc000  DAC: 00000017
Process swapper (pid: 0, stack limit = 0xc03cc268)
Stack: (0xc03cde80 to 0xc03ce000)
de80: c03cded4 c03cde90 c0044c80 bf0000cc c03cdeb4 c04217c8 c04215c8 c04213c8
dea0: c03cdea0 c03cdea0 c03cdecc 00000004 00000001 00000100 c03cc000 c04207e0
dec0: 00000001 0000000a c03cdf0c c03cded8 c0040204 c0044ae0 c03d5514 c03d2c58
dee0: 0000001e 0000001e c0425474 00000000 c03ff284 3001c920 41129200 3001c8ec
df00: c03cdf1c c03cdf10 c00402cc c0040198 c03cdf3c c03cdf20 c0028048 c0040294
df20: ffffffff f4000000 00004000 c03ff284 c03cdf94 c03cdf40 c0028a44 c0028010
df40: f4100000 00000032 f4100000 60000013 c0029edc c03cc000 c0029edc c03ff284
df60: 3001c920 41129200 3001c8ec c03cdf94 c03cdf98 c03cdf88 c002a4f8 c0029f3c
df80: 60000013 ffffffff c03cdfb4 c03cdf98 c002a4f8 c0029eec c0423264 c03ff240
dfa0: c001e000 c03d0440 c03cdfc4 c03cdfb8 c02d2124 c002a4bc c03cdff4 c03cdfc8
dfc0: c0008930 c02d20d8 c0008470 00000000 00000000 c001ed38 00000000 c0007175
dfe0: c03ff35c c001ed34 00000000 c03cdff8 30008034 c0008740 00000000 00000000
Backtrace:
[] (timer_handler+0x0/0x118 [my_bdri]) from [] (run_timer_so
ftirq+0x1b0/0x230)
[] (run_timer_softirq+0x0/0x230) from [] (__do_softirq+0x7c/
0xfc)
[] (__do_softirq+0x0/0xfc) from [] (irq_exit+0x48/0x50)
[] (irq_exit+0x0/0x50) from [] (__exception_text_start+0x48/
0x70)
[] (__exception_text_start+0x0/0x70) from [] (__irq_svc+0x24
/0xa0)
Exception stack(0xc03cdf40 to 0xc03cdf88)
df40: f4100000 00000032 f4100000 60000013 c0029edc c03cc000 c0029edc c03ff284
df60: 3001c920 41129200 3001c8ec c03cdf94 c03cdf98 c03cdf88 c002a4f8 c0029f3c
df80: 60000013 ffffffff
r7:c03ff284 r6:00004000 r5:f4000000 r4:ffffffff
[] (default_idle+0x0/0xac) from [] (cpu_idle+0x4c/0x68)
[] (cpu_idle+0x0/0x68) from [] (rest_init+0x5c/0x70)
r7:c03d0440 r6:c001e000 r5:c03ff240 r4:c0423264
[] (rest_init+0x0/0x70) from [] (start_kernel+0x200/0x268)
[] (start_kernel+0x0/0x268) from [<30008034>] (0x30008034)
r6:c001ed34 r5:c03ff35c r4:c0007175
Code: e1a0c00d e92dd800 e24cb004 e3a034e6 (e5931064)
Kernel panic - not syncing: Fatal exception in interrupt
==================================================================================
求高手帮助解决。。

最新回复

典型的非法指针错误  详情 回复 发表于 2009-9-7 09:34
点赞 关注

回复
举报

73

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
oops,使用了非法的地址
 
 

回复

76

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
典型的非法指针错误
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表