Chinaunix首页 | 论坛 | 博客
  • 博客访问: 301003
  • 博文数量: 76
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-20 20:38
文章分类
文章存档

2016年(20)

2015年(56)

分类: 嵌入式

2015-05-25 20:18:45

P { margin-bottom: 0.21cm; }

second_drv 查询方式 按键驱动程序

15年4月4月27月27日27日22:07:06

1)先写出来框架:

*********************************************************************

1 #include

2 #include

3 #include

4 #include

5 #include

6 #include

7 #include

8 #include

9 #include

10 #include

11

12 static struct class *second_drv_class;

13 static struct class_device *second_drv_class_dev;

14

15 static int second_drv_open(struct inode *inode, struct file *file){

16 return 0;

17 }

18

19 static ssize_t second_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos){

20 return 0;

21 }

22

23 ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos){

24

25 return 0;

26 }

27

28 static struct file_operations second_drv_fops = {

29 .owner = THIS_MODULE,

30 .open = second_drv_open,

31 .write = second_drv_write,

32 .read = second_drv_read,

33 };

34

35 int major;

36 static int __init second_drv_init(void){

37 major = register_chrdev(0, "second_drv", &second_drv_fops);

38

39 second_drv_class = class_create(THIS_MODULE, "second_drv");

40 second_drv_class_dev = class_device_create(second_drv_class, NULL, MKDEV(major, 0), NULL, "buttons"); //创建类和类里面的元素。

41

42 return 0;

43 }

44

45 static void __exit second_drv_exit(void){

46 unregister_chrdev(major, "second_drv");

47

48 class_device_unregister(second_drv_class_dev);

49 class_destroy(second_drv_class); //在出口函数里面注销类。

50

51 return 0;

52 }

53

54

55

56 module_init(second_drv_init);

57 module_exit(second_drv_exit);

58

59 MODULE_LICENSE("GPL");

********************************************************************

写驱动的时候,先写出来框架,然后编译以后,试试看看能用不,如果没问题的话,再继续往下做,如果等驱动写完以后再编译,如果出问题的话,就不能确定是哪的问题,不好调试。


2)写一个驱动

**********************************************************************

1 #include

2 #include

3 #include

4 #include

5 #include

6 #include

7 #include

8 #include

9 #include

10 #include

11

12 static struct class *second_drv_class;

13 static struct class_device *second_drv_class_dev;

14

15 volatile unsigned long *gpfcon;

16 volatile unsigned long *gpfdat;

17

18 volatile unsigned long *gpgcon;

19 volatile unsigned long *gpgdat;

20

21 static int second_drv_open(struct inode *inode, struct file *file){

22

23 *gpfcon &= ~((0x3 << (0*2)) | (0x3 << (2*2)));

24 *gpgcon &= ~((0x3 << (3*2)) | (0x3 << (11*2))); //配置引脚。

25 return 0;

26 }

27

28 static ssize_t second_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos){

29 return 0;

30 }

31

32 ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos){

33 unsigned char key_val[4];

34 int regval;

35

36 if(size != sizeof(key_val))

37 return -EINVAL;

38

39 regval = *gpfdat;

40 key_val[0] = (regval & (1 << 0)) ? 1: 0;

41 key_val[1] = (regval & (1 << 2)) ? 1: 0;

42

43 regval = *gpgdat;

44 key_val[2] = (regval & (1 << 3)) ? 1: 0;

45 key_val[3] = (regval & (1 << 11)) ? 1: 0;

46

47 copy_to_user(buf, key_val, sizeof(key_val));

48

49 return sizeof(key_val);

50 }

51

52 static struct file_operations second_drv_fops = {

53 .owner = THIS_MODULE,

54 .open = second_drv_open,

55 .write = second_drv_write,

56 .read = second_drv_read,

57 };

58

59 int major;

60 static int __init second_drv_init(void){

61 major = register_chrdev(0, "second_drv", &second_drv_fops);

62

63 second_drv_class = class_create(THIS_MODULE, "second_drv");

64 second_drv_class_dev = class_device_create(second_drv_class, NULL, MKDEV(major, 0), NULL, "buttons");

65

66 gpfcon = (unsigned long *)ioremap(0x56000050, 16);

67 gpfdat = gpfcon + 1;

68

69 gpgcon = (unsigned long *)ioremap(0x56000060, 16);

70 gpgdat = gpgcon + 1;

71 return 0;

72 }

73

74 static void __exit second_drv_exit(void){

75 unregister_chrdev(major, "second_drv");

76

77 class_device_unregister(second_drv_class_dev);

78 class_destroy(second_drv_class);

79

80 return 0;

81 }

82

83

84

85 module_init(second_drv_init);

86 module_exit(second_drv_exit);

87

88 MODULE_LICENSE("GPL");

**********************************************************************

测试程序为:

*******************************************************************

1 #include

2 #include

3 #include

4 #include

5

6 int main (int argc, char **argv){

7 int fd;

8 unsigned char key_val[4];

9 int cnt = 0;

10

11 fd = open("/dev/buttons",O_RDWR);

12

13 if (fd < 0)

14 printf("cannot open!\n");

15

16 while (1)

17 {

18 read(fd, key_val, sizeof(key_val));

19 if(!key_val[0] || !key_val[1] || !key_val[2] || !key_val[3])

20 {

21 printf("%04d key pressed: %d %d %d %d\n", cnt++, key_val[0], key_val[1], key_val[2], key_val[3]);

22 }

23 }

24 return 0;

25 }

*********************************************************************

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