看了这么多的资料,现在决定上手了,下面将用两种方式来实现对GPIO 117的控制
1,用直接添加到内核的方式,实现MISC的驱动(misc_register)
2,用手工安装的方式,实现简单字符设备驱动(register_chrdev)

实现前提:当前所用的GPIO没有被其它设备所使用,大家可以用我前面BLOG说的方式查看GPIO的使用情况,当前我所用的GPIO本来是bluetooth的开关,需要屏蔽一个函数。不然后面的驱动申请IO都会失败。
函数为Board-am335xevm.c 中的wl12xx_bluetooth_enable();

一,MISC驱动的实现
1,参考linux SDK for AM335x Ver 1.1.pdf P28,添加kernel 配置选项
  打开/driver/input/misc/Kconfig并添加:

  1. config INPUT_GPIOTEST
  2. bool "Gpio 117 test"
  3. help
  4. Just test the Gpio 117 status

打开/driver/input/misc/Makefile并添加:

  1. obj-$(CONFIG_INPUT_GPIOTEST)+=GpioTestDriver.o

2,实现GpioTestDriver.c

  1. #include <linux/gpio.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/moduleparam.h>
  5. #include <linux/delay.h>
  6. #include <linux/types.h>
  7. #include <linux/miscdevice.h>
  8. #include <linux/device.h>
  9. #include <linux/fs.h>
  10. #include <linux/init.h>
  11. #define TEST_IO_NUM(117)
  12. #define NAME_MISC"GpioTest"
  13. #define NAME_MOUDULE"GpioTest1"
  14. #define USE_MISC_MODE1
  15. static int major = 251;
  16. void GpioTest(void);
  17. static long GpioIOctl(struct file *filp, unsigned cmd, unsigned long arg)
  18. {
  19. GpioTest();
  20. return 1;
  21. }
  22. void GpioTest(void)
  23. {
  24. int iCount = 0;
  25. for(iCount = 0; iCount <=20; iCount++ )
  26. {
  27. if(iCount%2 == 0)
  28. {
  29. gpio_direction_output(TEST_IO_NUM, 1);
  30. printk(KERN_INFO"#######IO117 statu is high.\r\n");
  31. }
  32. else
  33. {
  34. gpio_direction_output(TEST_IO_NUM, 0);
  35. printk(KERN_INFO"#######IO117 statu is low.\r\n");
  36. }
  37. mdelay(3000);
  38. }
  39. printk(KERN_INFO"#######App run over!");
  40. }
  41. static int GpioOpen(struct inode *inode, struct file *file)
  42. {
  43. int iRen = -1;
  44. iRen = gpio_request(TEST_IO_NUM, "IO117");
  45. if(iRen < 0)
  46. {
  47. printk(KERN_INFO"#######Failed to request the IO117!");
  48. }else
  49. {
  50. printk(KERN_INFO"#######Success to request the IO117");
  51. }
  52. return iRen;
  53. }
  54. static int GpioClose(struct inode *inode, struct file *file)
  55. {
  56. printk(KERN_INFO"#######Free the IO117");
  57. gpio_free(TEST_IO_NUM);
  58. return 1;
  59. }
  60. //****entry point for TEST GPIO module
  61. static const struct file_operations gpio_test_driver = {
  62. .owner = THIS_MODULE,
  63. .unlocked_ioctl= GpioIOctl,
  64. .llseek = no_llseek,
  65. .open = GpioOpen,
  66. .release = GpioClose,
  67. };
  68. #if USE_MISC_MODE
  69. static struct miscdevice gpiotest_misc_device = {
  70. .minor    = MISC_DYNAMIC_MINOR,
  71. .name     = NAME_MISC,
  72. .fops     = &gpio_test_driver,
  73. };
  74. #endif
  75. static int __init GpioTestInit(void)
  76. {
  77. int iRet;
  78. printk(KERN_INFO"#######GpioTest modules is install!\r\n");
  79. #if USE_MISC_MODE
  80. iRet = misc_register(&gpiotest_misc_device);
  81. if (iRet) {
  82. printk(KERN_INFO"#######unable to register a misc device\r\n");
  83. return iRet;
  84. }
  85. #else
  86. iRet = register_chrdev(major, NAME_MOUDULE, &gpio_test_driver);
  87. if (iRet < 0) {
  88. printk(KERN_INFO"#######unable to register a chr device\r\n");
  89. return iRet;
  90. }
  91. #endif
  92. return iRet;
  93. }
  94. static void __exit GpioTestExit(void)
  95. {
  96. #if USE_MISC_MODE
  97. misc_deregister(&gpiotest_misc_device);
  98. #else
  99. unregister_chrdev(major, NAME_MOUDULE);
  100. #endif
  101. printk(KERN_INFO"#######GpioTest modules is exit!\r\n");
  102. }
  103. module_init(GpioTestInit);
  104. module_exit(GpioTestExit);
  105. MODULE_AUTHOR("david.hu<343556608@qq.com>");
  106. MODULE_LICENSE("GPL");
  107. MODULE_DESCRIPTION("Gpio117 Test driver");

3,直接编译:
make uImage
拷到小板上升级运行
注意启动的过程有打印:
[    3.730712] #######GpioTest modules is install!
这里表示我们的驱动已经合入NK里去了,当然我们也可以命令:ls /dev,可以看到有GpioTest这个存在
4,写测试APP

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/ioctl.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #include <linux/input.h>
  7. #include <fcntl.h>
  8. int main(int argc, charchar *argv)
  9. {
  10. int fd;
  11. fd = open("/dev/GpioTest", O_RDWR);
  12. if(fd < 0)
  13. {
  14. printf("***Can't open the gpiotest!\r\n");
  15. return -1;
  16. }
  17. ioctl(fd, 0, 0);
  18. close(fd);
  19. printf("***App run over!\r\n");
  20. return 1;
  21. }

将编译的.out文件拷到小机上面运行,看是不是会打印正确的结果。

二,字符设备驱动的实现
1,代码的实现,请将上面MISC的代码里#define USE_MISC_MODE1改成0
2,makefile的实现

  1. KERNEL_DIR := /home/ding/workdir/david/EVMBoard/board-support/linux-3.2
  2. PLATFORM := "am335x-evm"
  3. MACHINE_NAME := "am335x"
  4. # If CROSS_COMPILE is not set by Rules.make then set a sane default
  5. CROSS_COMPILE ?= arm-arago-linux-gnueabi-
  6. export CROSS_COMPILE
  7. obj-m := GpioTestDriver.o
  8. MAKE_ENV = ARCH=arm
  9. PWD := $(shell pwd)
  10. all:
  11. $(MAKE) EXTRA_CFLAGS="$(EXTRA_CFLAGS)" -C $(KERNEL_DIR) $(MAKE_ENV) \
  12. M=$(PWD) modules
  13. clean:
  14. rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.symvers

3,将编译的ko拷入小机,然后命令:

  1. insmod ./GpioTestDriver.ko
  2. lsmod
  3. mknod /dev/GpioTestDriver c 251 0

4,实现APP的代码
将上面MISC的代码作如下修改:

  1. fd = open("/dev/GpioTestDriver", O_RDWR);

5,运行APP查看结果

总结:两个驱动代码实现差不多,但是步骤不一样,主要体现在模块需要安装。MISC会自动创建设备文件,它的主设备号是10,字符设备需要我们来指定。
可安装的字符设备对驱动的编写测试是非常有帮助的。

http://blog.csdn.net/hudaweikevin/article/details/16826995

最新文章

  1. jquery中on/delegate的原理
  2. Security5:Execute AS 和 impersonate 权限
  3. CentOS6.3编译安装Nginx1.4.7 + MySQL5.5.25a + PHP5.3.28
  4. win7双系统安装ubuntu并配置常用软件
  5. js原生方法传参的细节(面试必问)
  6. oracle DBMS_LOCK.SLEEP()的使用
  7. JavaScript基础插曲—元素样式,正则表达式,全局模式,提取数组
  8. Java之强引用、 软引用、 弱引用、虚引用
  9. RMB转换人民币大小金额
  10. 一次Linux系统被攻击的分析过程
  11. 通过ajax提交form表单
  12. Centos下LAMP环境搭建
  13. DOS批处理的字符串功能
  14. XHTML 是以 XML 格式编写的 HTML
  15. 利用instsrv和srvany来手动安装服务
  16. Servlet交互【重定向 与 请求分派】详解
  17. CF908D New Year and Arbitrary Arrangement(期望Dp+数学)
  18. web移动端,需要清楚设备像素比devicePixelRatio的应用
  19. 【linux基础】cuDNN版本查询
  20. shell变量的截取总结

热门文章

  1. 能否通过六面照片构建3D模型?比如人脸,全身的多角度照片,生成3D模型。?
  2. HDU1978How Many Ways 记忆化dfs+dp
  3. RocEDU.阅读.写作《苏菲的世界》书摘(七)
  4. 已知圆上三个点坐标,求圆半径 r 和 圆心坐标
  5. ubuntu18.04下搭建深度学习环境anaconda2+ cuda9.0+cudnn7.0.5+tensorflow1.7【原创】【学习笔记】
  6. pybedtools --bedtools的python包
  7. java 23种设计模式,一般情况下,常用的有哪些? 转载
  8. node中session的管理
  9. 安装完Linux Mint后,发现系统中竟没有中文输入法
  10. pshell远程连接服务器