1. /*******************************
  2. *
  3. *杂项设备驱动:miscdevice
  4. *majior=10;
  5. *
  6. * *****************************/
  7.  
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/fs.h>
  11. #include <linux/types.h>
  12. #include <linux/init.h>
  13.  
  14. //#include <linux/moduleparam.h>
  15. //#include <linux/slab.h>//kcalloc,kzalloc等内存分配函数
  16.  
  17. //---------ioctl------------
  18. #include <linux/ioctl.h>
  19.  
  20. //---------misc_register----
  21. #include <linux/miscdevice.h>
  22.  
  23. //----------cdev--------------
  24. #include <linux/cdev.h>
  25.  
  26. //----------delay-------------
  27. #include <linux/delay.h>
  28.  
  29. //----------GPIO---------------
  30. #include <mach/gpio.h>
  31. #include <mach/regs-gpio.h>
  32. #include <plat/gpio-cfg.h>
  33.  
  34. #define DEVICE_NAME "leds"
  35.  
  36. static int led_gpios[] = {
  37. S5PV210_MP04(),
  38. S5PV210_MP04(),
  39. S5PV210_MP04(),
  40. S5PV210_MP04(),
  41. };//4个LED
  42.  
  43. #define LED_NUM ARRAY_SIZE(led_gpios)
  44.  
  45. static long fl210_leds_ioctl(struct file *filp, unsigned int cmd,
  46. unsigned long arg)
  47. {
  48. switch(cmd) {
  49. case :
  50. case :
  51. if (arg > LED_NUM) {
  52. return -EINVAL;
  53. }
  54.  
  55. gpio_set_value(led_gpios[arg], !cmd);//根据cmd设置LED的暗灭
  56. printk(DEVICE_NAME": %ld %d\n", arg, cmd);
  57. break;
  58.  
  59. default:
  60. return -EINVAL;
  61. }
  62.  
  63. return ;
  64. }
  65.  
  66. static struct file_operations fl210_led_dev_fops = {
  67. .owner = THIS_MODULE,
  68. .unlocked_ioctl = fl210_leds_ioctl,
  69. };
  70.  
  71. //----------------miscdevice------------------
  72. static struct miscdevice fl210_led_dev = {
  73. .minor = MISC_DYNAMIC_MINOR,
  74. .name = DEVICE_NAME,
  75. .fops = &fl210_led_dev_fops,
  76. };
  77. //--------------------------------------------
  78.  
  79. static int __init fl210_led_dev_init(void) {
  80. int ret;
  81. int i;
  82.  
  83. for (i = ; i < LED_NUM; i++) {
  84. ret = gpio_request(led_gpios[i], "LED");//申请GPIO口
  85. if (ret) {
  86. printk("%s: request GPIO %d for LED failed, ret = %d\n", DEVICE_NAME,
  87. led_gpios[i], ret);
  88. return ret;
  89. }
  90.  
  91. s3c_gpio_cfgpin(led_gpios[i], S3C_GPIO_OUTPUT);//设置GPIO口为输出
  92. gpio_set_value(led_gpios[i], );//初始化GPIO口的值
  93. }
  94.  
  95. ret = misc_register(&fl210_led_dev);//注册杂项设备
  96.  
  97. printk(DEVICE_NAME"\tinitialized\n");
  98. printk("led num is: %d\n",LED_NUM);
  99. return ret;
  100. }
  101.  
  102. static void __exit fl210_led_dev_exit(void) {
  103. int i;
  104.  
  105. for (i = ; i < LED_NUM; i++) {
  106. gpio_free(led_gpios[i]);//释放GPIO口
  107. }
  108.  
  109. misc_deregister(&fl210_led_dev);//注销设备
  110. }
  111.  
  112. module_init(fl210_led_dev_init);
  113. module_exit(fl210_led_dev_exit);
  114.  
  115. MODULE_LICENSE("GPL");
  116. MODULE_AUTHOR("");

S5PV210_MP04宏定义在linux/arch/arm/mach-s5pv210/include/mach/gpio.h

#define S5PV210_MP04(_nr) (S5PV210_GPIO_MP04_START + (_nr))

S5PV210_GPIO_MP04_START = S5PV210_GPIO_NEXT(S5PV210_GPIO_MP03),

#define S5PV210_GPIO_NEXT(__gpio) \ ((__gpio##_START) + (__gpio##_NR) + CONFIG_S3C_GPIO_SPACE + 1)

//这里的CONFIG_S3C_GPIO_SPAC是内核配置选项,在.config中可以找到,我的配置为:   CONFIG_S3C_GPIO_SPACE = 0

上述代码用到以下几个函数:

gpio_set_value();

s3c_gpio_cfgpin();

gpio_request();

gpio_free();

misc_register();

misc_deregister();

后面会逐个分析。

测试程序如下:

  1. #include <stdio.h>
  2. //#include "sys/types.h"
  3. #include <sys/ioctl.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>//read,write等等
  6. //#include "termios.h"
  7. //#include "sys/stat.h"
  8. #include <fcntl.h>
  9.  
  10. #define LED2_ON 0x1
  11. #define LED2_OFF 0x0
  12.  
  13. main(int argc,char *argv[])
  14. {
  15. int fd;
  16.  
  17. if ((fd=open("/dev/leds",O_RDWR /*| O_NDELAY | O_NOCTTY*/)) < )
  18. {
  19. printf("Open Device failed.\r\n");
  20. exit();
  21. }
  22. else
  23. {
  24. printf("Open Device successed.\r\n");
  25. }
  26. if (argc<)
  27. {
  28. /* code */
  29. printf("Usage: %s <on|off num>\n",argv[]);
  30. exit();
  31. }
  32. if(!strcmp(argv[],"on"))
  33. {
  34. printf("led1 will on!!\n");
  35. if(ioctl(fd,LED2_ON,atoi(argv[]))<)
  36. {
  37. printf("ioctl err!!\n");
  38. }
  39.  
  40. }
  41. if(!strcmp(argv[],"off"))
  42. {
  43. printf("led1 will off!!\n");
  44. if(ioctl(fd,LED2_OFF,atoi(argv[]))<)
  45. {
  46. printf("ioctl err!!\n");
  47. }
  48. }
  49. close(fd);
  50. }

Linux下GPIO驱动(一) ----一个简单的LED驱动的更多相关文章

  1. Linux下实现流水灯等功能的LED驱动代码及测试实例

    驱动代码: #include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> ...

  2. linux设备驱动归纳总结(五):4.写个简单的LED驱动【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-84693.html linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxx ...

  3. 【Linux开发】linux设备驱动归纳总结(五):4.写个简单的LED驱动

    linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  4. 很好的linux下GPIO驱动详解文章

    原文地址  http://blog.csdn.net/llxmedici/article/details/6282372 打算跟着友善之臂的<mini2440 linux移植开发指南>来做 ...

  5. linux下scsi共享磁盘的简单搭建

    linux下scsi共享磁盘的简单搭建 Scsi 共享磁盘需要我先有空余的分区,或者可以在虚拟机里面添加一块磁盘,安装所需的软件我在虚拟机里面添加了一块硬盘,分了一个主分区,sdb1 1G,将这个用s ...

  6. Linux下的GitHub安装与简单配置教程 ~ 转载

    Linux下的GitHub安装与简单配置教程   1.GitHub简介 Git是一个分布式版本控制系统,与其相对的是CVS.SVN等集中式的版本控制系统. 2.Git的安装 1)安装Git a.查看与 ...

  7. [stm32] 一个简单的stm32vet6驱动2.4寸240X320的8位并口tft屏DEMO

    书接上文: 最近在研究用低速.低RAM的单片机来驱动小LCD或TFT彩屏实现动画效果 首先我用一个16MHz晶振的m0内核的8位单片机nRF51822尝试驱动一个1.77寸的4线SPI屏(128X16 ...

  8. [stm32] 一个简单的stm32vet6驱动的天马4线SPI-1.77寸LCD彩屏DEMO

    书接上文<1.一个简单的nRF51822驱动的天马4线SPI-1.77寸LCD彩屏DEMO> 我们发现用16MHz晶振的nRF51822驱动1.77寸的spi速度达不到要求 本节主要采用7 ...

  9. 在Linux下,如何分析一个程序达到性能瓶颈的原因

    0.在Linux下,如何分析一个程序达到性能瓶颈的原因,请分别从CPU.内存.IO.网络的角度判断是谁导致的瓶颈?注意现在的机器CPU是多核 1.用sar -n DEV 1 10 2.用iotop命令 ...

随机推荐

  1. JavaScript网站设计实践(二)实现导航栏当前所选页面的菜单项高亮显示

    一.(一)中的代码还可以修改的地方. 在(一)中,如果是运行在服务器下,如apache等,可以把head和navigation的div抽取出来,放置在另一个html文件里,然后在页面中,include ...

  2. C#_mvc_ajax_return data

    假设cshtml文件中是这样的: <script type="text/javascript"> $(document).ready(function(){ $(&qu ...

  3. Mechanism of Loading Resources

    Mechanism of Loading Resources 1. Distributed strategy 1.1. Developer guilde 1.2. Notes 2. Centraliz ...

  4. Android(java)学习笔记135:Android中assets文件夹资源的访问

    Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.java里面自动生成该资源文件的ID,所以访问这种资源文件 ...

  5. fstat - 读取文件相关信息

    #fstat读取到的信息 ["dev"]=> int(16777220) ["ino"]=> int(66880002) ["mode&q ...

  6. Number of failed login attempts exceeds threshold value

    OEM发出好多告警,Number of failed login attempts exceeds threshold value. profile当前配置10次失败就会锁定user 查看下,dba_ ...

  7. [Doc ID 1590988.1]如何清理E-Business Suite的缓存(Apache/iAS, Cabo, Modplsql, Browser, Jinitiator, Java, Portal, WebADI)?

    文档内容   目标   解决方案   参考 适用于: Oracle Applications Technology Stack - 版本 11.5.9 到 12.2.2 [发行版 11.5 到 12. ...

  8. 深入理解计算机系统第二版习题解答CSAPP 2.17

    假设w=4,我们能给每个可能的十六进制数字赋予一个数值,假设用一个无符号或者补码表示.完成下表: x 无符号(B2U(x)) 补码(B2T(x)) 十六进制 二进制 0xE 1110 14 -2 0x ...

  9. Oracle 返回结果集的 存储过程

    create or replace PROCEDURE SPGETROLELIST ( P_APPCODE IN VARCHAR2 , P_USERROLE IN VARCHAR2 , CUR_RES ...

  10. Gvim使用心得--设置篇[转]

    1.设置自己喜欢的字体? 点“编辑”--“选择字体”, 然后在字体列表中选择一个你喜欢的字体和字号,然后确认. 如果想每次都使用这个这个字体 需要加到启动文件中 比如我的 set guifont=Co ...