Chinaunix首页 | 论坛 | 博客
  • 博客访问: 493811
  • 博文数量: 223
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2145
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-01 10:23
个人简介

该坚持的时候坚持,该妥协的时候妥协,该放弃的时候放弃

文章分类

全部博文(223)

文章存档

2017年(56)

2016年(118)

2015年(3)

2014年(46)

我的朋友

分类: 嵌入式

2016-11-06 14:30:00

http://blog.chinaunix.net/uid-29512885-id-5754512.html
http://blog.chinaunix.net/uid-29512885-id-5752440.html

根据上面两篇记录,可以整合成内核驱动的编程写法。
代码中新需要的函数:

实地址转换成虚拟地址,ioremap
void *ioremap(unsinged long phys_addr, unsigned long size)
phys_addr:要映射的起始地IO地址
size:要映射的空间的大小
头文件:linux/io.h

writel() 往内存映射的 I/O 空间上写数据,wirtel()  I/O 上写入 32 位数据 (4字节)。
 原型:
void writel (unsigned char data , unsigned short addr );
data:需要写入的数据
addr:写入数据的地址
头文件:linux/io.h?

led.c:
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/cdev.h>
  4. #include <linux/fs.h>
  5. #include <linux/io.h>
  6. #include <asm/uaccess.h>
  7. #include "led.h"

  8. #define GPBCON 0x56000010
  9. #define GPBDAT 0x56000014

  10. unsigned int *led_config;                   //定义CONTRAL控制器指针
  11. unsigned int *led_data;                     //定义DATA控制器指针

  12. struct cdev led_dev;                        //分配设备号
  13. dev_t led_devno;

  14. int led_open(struct inode *inode, struct file *filp)
  15. {
  16.     led_config = ioremap(GPBCON, 4);
  17.     writel(0x15400, led_config);

  18.     led_data = ioremap(GPBDAT, 4);

  19.     return 0;
  20. }

  21. long led_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  22. {
  23.     switch(cmd) {
  24.         case LED_ON:
  25.             writel(0x6bf, led_data);
  26.             return 0;

  27.         case LED_OFF:
  28.             writel(0x7ff, led_data);
  29.             return 0;

  30.         default:
  31.             return -EINVAL;
  32.     }
  33. }

  34. struct file_operations led_fops = {
  35.     .open = led_open,
  36.     .unlocked_ioctl = led_ioctl,
  37. };

  38. static int led_init()
  39. {
  40.     cdev_init(&led_dev, &led_fops);                          //设备初始化

  41.     alloc_chrdev_region(&led_devno, 0, 1, "myled");          //动态分配设备号
  42.     cdev_add(&led_dev, led_devno, 1);                        //添加设备

  43.     return 0;
  44. }

  45. static void led_exit()
  46. {
  47.     cdev_del(&led_dev);                                       //注销设备
  48.     unregister_chrdev_region(led_devno, 1);                   //释放设备号
  49. }

  50. module_init(led_init);
  51. module_exit(led_exit);
led.h:
  1. #define LED_MAGIC 'L'
  2. #define LED_ON _IO(LED_MAGIC, 0)
  3. #define LED_OFF _IO(LED_MAGIC, 1)
led_app.c:

  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <sys/ioctl.h>
  6. #include "led.h"

  7. int main(int argc, char *argv[])
  8. {
  9.     int fd;
  10.     int cmd;
  11.     if(argc < 2)
  12.     {
  13.         printf("please enter the second para!\n");
  14.     return 0;
  15.     }
  16.     
  17.     cmd = atoi(argv[1]);

  18.     fd = open("/dev/myled", O_RDWR);

  19.     if(cmd == 1)
  20.         ioctl(fd, LED_ON);
  21.     else
  22.         ioctl(fd, LED_OFF);

  23.     return 0;
  24. }
遇到的错误:

  1. # ./led_app 1
  2. Unable to handle kernel paging request at virtual address aaaaaab6
  3. pgd = c39c8000
  4. [aaaaaab6] *pgd=00000000
  5. Internal error: Oops: 3 [#1]
  6. Modules linked in: led(P)
  7. CPU: 0 Tainted: P (2.6.30.4-EmbedSky #5)
  8. PC is at do_vfs_ioctl+0x58/0x540
  9. LR is at sys_ioctl+0x40/0x68
  10. pc : [] lr : [] psr: 80000013
  11. sp : c39a5f08 ip : 00004c00 fp : c39a5f7c
  12. r10: 00000000 r9 : c39a4000 r8 : c0045008
  13. r7 : 00000000 r6 : c4832014 r5 : 00000003 r4 : 00000003
  14. r3 : 00005421 r2 : 00005452 r1 : 00000003 r0 : aaaaaaaa
  15. Flags: Nzcv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user
  16. Control: c000717f Table: 339c8000 DAC: 00000015
  17. Process led_app (pid: 618, stack limit = 0xc39a4268)
  18. Stack: (0xc39a5f08 to 0xc39a6000)
  19. 5f00: 000a7000 c3955300 c39a5f7c c39a5f20 00000003 00000003
  20. 5f20: 00000000 c39a7700 00000000 c39a4000 c39a5f64 c39a5f40 c00ba660 00000003
  21. 5f40: 00000003 c380c000 c4832014 00000000 ffffff9c 00000003 00000000 00004c00
  22. 5f60: c4832014 c0045008 c39a4000 00000000 c39a5fa4 c39a5f80 c00b3174 c00b2c04
  23. 5f80: 00000005 00000000 00008984 00000000 0000893c 00000036 00000000 c39a5fa8
  24. 5fa0: c0044e60 c00b3144 00008984 00000000 00000003 00004c00 00000000 00000001
  25. 5fc0: 00008984 00000000 0000893c 00000036 00000000 00000000 00000000 beda7d64
  26. 5fe0: 00000000 beda7d48 000082a4 0001315c 60000010 00000003 0c080022 21840000
  27. Backtrace:
  28. [] (do_vfs_ioctl+0x0/0x540) from [] (sys_ioctl+0x40/0x68)
  29. [] (sys_ioctl+0x0/0x68) from [] (ret_fast_syscall+0x0/0x2c)
  30. r7:00000036 r6:0000893c r5:00000000 r4:00008984
  31. Code: e59f34dc e15c0003 0a000079 e596000c (e590200c)
  32. ---[ end trace 96eede84e19fea3c ]---
  33. Unable to handle kernel paging request at virtual address 00001052
  34. pgd = c39c8000
  35. [00001052] *pgd=339c0031, *pte=00000000, *ppte=00000000
  36. Internal error: Oops: 13 [#2]
  37. Modules linked in: led(P)
  38. CPU: 0 Tainted: P D (2.6.30.4-EmbedSky #5)
  39. PC is at filp_close+0x2c/0x7c
  40. LR is at put_files_struct+0xbc/0xe0
  41. pc : [] lr : [] psr: 20000013
  42. sp : c39a5c88 ip : c39a5ca8 fp : c39a5ca4
  43. r10: 00000000 r9 : aaaaaab6 r8 : c38d9780
  44. r7 : 0000000c r6 : c38d9780 r5 : 00000001 r4 : c4832014
  45. r3 : 0000101e r2 : c39a4000 r1 : c38d9780 r0 : c4832014
  46. Flags: nzCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user
  47. Control: c000717f Table: 339c8000 DAC: 00000015
  48. Process led_app (pid: 618, stack limit = 0xc39a4268)
  49. Stack: (0xc39a5c88 to 0xc39a6000)
  50. 5c80: 00000000 00000001 c38d9788 0000000c c39a5ccc c39a5ca8
  51. 5ca0: c0058e28 c00a4298 0000000b c00b2c4c c39a4000 c00b2c50 c383a900 bf000000
  52. 5cc0: c39a5cdc c39a5cd0 c0058e74 c0058d7c c39a5cfc c39a5ce0 c005a794 c0058e5c
  53. 5ce0: c39a4000 00000001 e19fea3c 96eede84 c39a5d0c c39a5d00 c004932c c005a2f8
  54. 5d00: c39a5d2c c39a5d10 c004b5e4 c0049164 c39a5ec0 00000003 c39a5ec0 c04ee808
  55. 5d20: c39a5dc4 c39a5d30 c004b898 c004b588 00002000 00002000 00000001 ffffffff
  56. 5d40: c39a5d98 c39a5ec0 c04ee808 c39a5ec0 c00b2c4c c39a5dc4 c39a5d64 c0044a00
  57. 5d60: c00441b8 aaaaaab6 00000001 c39a5ec0 00000001 e590200c e590200c c39a5ec0
  58. 5d80: c04ee808 c39a5ec0 aaaaaab6 c00b2c4c c39a5dc4 00000002 c39a5dac c004d174
  59. 5da0: c0377300 00000013 ffffffff 0000000c 0000000c e590200c c39a5e0c c39a5dc8
  60. 5dc0: c004cfe0 c004b860 c0007120 c0007120 56000000 c4832fff c4833000 00000003
  61. 5de0: 56000000 c04b670c c39a5ef4 00000003 c04b673c c39a5ec0 80000013 aaaaaab6
  62. 5e00: c39a5ebc c39a5e10 c00441e4 c004cf4c c004c628 00000000 c39a5e3c c39a5e28
  63. 5e20: c00a90d4 c007a000 c4832014 bf0006a8 c39a5e64 00000005 c39a5e64 c39a5e48
  64. 5e40: c00bbf3c c014ca24 c4832014 c39a7700 c39c24b0 c00a90d8 c39a5e8c c39a5e68
  65. 5e60: c00a4734 c00bbf28 c39a7700 c39523c0 00000003 c380c000 00000026 c39a5ed8
  66. 5e80: c39a5eac c39a5e90 c00a48e0 c00a4588 00000000 c39523c0 ffffffff c39a5ef4
  67. 5ea0: c4832014 00000000 c0045008 00000000 c39a5f7c c39a5ec0 c0044a00 c00441b8
  68. 5ec0: aaaaaaaa 00000003 00005452 00005421 00000003 00000003 c4832014 00000000
  69. 5ee0: c0045008 c39a4000 00000000 c39a5f7c 00004c00 c39a5f08 c00b3174 c00b2c4c
  70. 5f00: 80000013 ffffffff 000a7000 c3955300 c39a5f7c c39a5f20 00000003 00000003
  71. 5f20: 00000000 c39a7700 00000000 c39a4000 c39a5f64 c39a5f40 c00ba660 00000003
  72. 5f40: 00000003 c380c000 c4832014 00000000 ffffff9c 00000003 00000000 00004c00
  73. 5f60: c4832014 c0045008 c39a4000 00000000 c39a5fa4 c39a5f80 c00b3174 c00b2c04
  74. 5f80: 00000005 00000000 00008984 00000000 0000893c 00000036 00000000 c39a5fa8
  75. 5fa0: c0044e60 c00b3144 00008984 00000000 00000003 00004c00 00000000 00000001
  76. 5fc0: 00008984 00000000 0000893c 00000036 00000000 00000000 00000000 beda7d64
  77. 5fe0: 00000000 beda7d48 000082a4 0001315c 60000010 00000003 0c080022 21840000
  78. Backtrace:
  79. [] (filp_close+0x0/0x7c) from [] (put_files_struct+0xbc/0xe0)
  80. r7:0000000c r6:c38d9788 r5:00000001 r4:00000000
  81. [] (put_files_struct+0x0/0xe0) from [] (exit_files+0x28/0x2c)
  82. [] (exit_files+0x0/0x2c) from [] (do_exit+0x4ac/0x620)
  83. [] (do_exit+0x0/0x620) from [] (die+0x1d8/0x214)
  84. [] (die+0x0/0x214) from [] (__do_kernel_fault+0x6c/0x7c)
  85. [] (__do_kernel_fault+0x0/0x7c) from [] (do_bad_area+0x48/0x90)
  86. r7:c04ee808 r6:c39a5ec0 r5:00000003 r4:c39a5ec0
  87. [] (do_bad_area+0x0/0x90) from [] (do_alignment+0xa4/0x354)
  88. r5:e590200c r4:0000000c
  89. [] (do_alignment+0x0/0x354) from [] (do_DataAbort+0x3c/0xa0)
  90. [] (do_DataAbort+0x0/0xa0) from [] (__dabt_svc+0x40/0x60)
  91. Exception stack(0xc39a5ec0 to 0xc39a5f08)
  92. 5ec0: aaaaaaaa 00000003 00005452 00005421 00000003 00000003 c4832014 00000000
  93. 5ee0: c0045008 c39a4000 00000000 c39a5f7c 00004c00 c39a5f08 c00b3174 c00b2c4c
  94. 5f00: 80000013 ffffffff
  95. [] (do_vfs_ioctl+0x0/0x540) from [] (sys_ioctl+0x40/0x68)
  96. [] (sys_ioctl+0x0/0x68) from [] (ret_fast_syscall+0x0/0x2c)
  97. r7:00000036 r6:0000893c r5:00000000 r4:00008984
  98. Code: 0a000011 e5903010 e3530000 0a00000c (e5933034)
  99. ---[ end trace 96eede84e19fea3d ]---
  100. Fixing recursive fault but reboot is needed!

open实现的函数没有return 0;

阅读(640) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~