+ -
当前位置:首页 → 问答吧 → 【转】Linux嵌入式系统开发之Led开发---驱动篇

【转】Linux嵌入式系统开发之Led开发---驱动篇

时间:2010-11-08

来源:互联网

作者:☆&寒 烟☆


“小涛哥,你给我说说昨天那个Led驱动吧,我知道咋用程序点亮它了,可是调用的是驱动,我就是一个命令,感觉不知道究竟怎么弄的..”小王央求道。

“这样啊, 那也行,要不咱们这样以后,就讲一次应用,然后就讲与之相关的驱动开发,趁着应用的热度,顺便把驱动学了…”我想想说。

“好好,那今天就开始昨天的那个Led驱动程序开发吧…”

首先需要说明的是LED设备是个字符设备,这就和咱们本博客的linux设备驱动开发里的字符设备驱动关联起来了,不懂,就自己去看了,开始今天的重点内容.在mini24

40中LED链接线使用引脚GPB5~8外接4个LED,操作方法是:

1)引脚功能设为输出。  2)要点亮LED,令引脚输出为0.      3)要熄灭LED,令引脚输出为1.

好了下边,给出详细的源码注释:
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <asm/irq.h>
  7. #include <mach/regs-gpio.h>
  8. #include <machhardware.h>
  9. #include <linux/gpio.h>

  10. #define DEVICE_NAME     "leds_control"  /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
  11. #define LED_MAJOR       231     /* 主设备号 */
  12. #define IOCTL_LED_ON    0    /* 应用程序执行ioctl(fd, cmd, arg)时的第2个参数 */
  13. #define IOCTL_LED_OFF   1

  14. static unsigned long led_table [] = {   /* 用来指定LED所用的GPIO引脚 */
  15.     S3C2410_GPB(5),
  16.     S3C2410_GPB(6),
  17.     S3C2410_GPB(7),
  18.     S3C2410_GPB(8),
  19. };

  20. static unsigned int led_cfg_table [] = {  /* 用来指定GPIO引脚的功能:输出 */
  21.     S3C2410_GPIO_OUTP,
  22.     S3C2410_GPIO_OUTP,
  23.     S3C2410_GPIO_OUTP,
  24.     S3C2410_GPIO_OUTP,
  25. };

  26. //应用程序对设备文件/dev/leds执行open(...)时,就会调用s3c2410_leds_open函数
  27. static int s3c2410_leds_open(struct inode *inode, struct file *file)
  28. {
  29.     int i;
  30.     for (i = 0; i < 4; i++) {
  31.        // 设置GPIO引脚的功能:本驱动中LED所涉及的GPIO引脚设为输出功能
  32.         s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
  33.     }
  34.     return 0;
  35. }

  36. //应用程序对设备文件/dev/leds执行ioclt(...)时,就会调用s3c2410_leds_ioctl函数
  37. static int s3c2410_leds_ioctl(
  38.     struct inode *inode,
  39.     struct file *file,
  40.     unsigned int cmd,
  41.     unsigned long arg)
  42. {
  43.     if (arg > 4) {
  44.         return -EINVAL;
  45.     }
  46.     switch(cmd) {
  47.     case IOCTL_LED_ON:
  48.         // 设置指定引脚的输出电平为0
  49.         s3c2410_gpio_setpin(led_table[arg], 0);
  50.         return 0;

  51.     case IOCTL_LED_OFF:
  52.         // 设置指定引脚的输出电平为1
  53.         s3c2410_gpio_setpin(led_table[arg], 1);
  54.         return 0;

  55.     default:
  56.         return -EINVAL;
  57.     }
  58. }

  59. //字符设备驱动程序的核心,当应用程序操作设备文件时所调用的open、read、write等函数,最终会调用这个结构中指定的对应函数
  60. static struct file_operations s3c2410_leds_fops = {
  61.     .owner  =   THIS_MODULE,   
  62.     .open   =   s3c2410_leds_open,     
  63.     .ioctl  =   s3c2410_leds_ioctl,
  64. };

  65. //执行“insmod s3c2410_leds.ko”命令时就会调用这个函数
  66. static int __init s3c2410_leds_init(void)
  67. {
  68.     int ret;

  69.     /* 注册字符设备驱动程序
  70.      * 参数为主设备号、设备名字、file_operations结构;
  71.      * 这样,主设备号就和具体的file_operations结构联系起来了,
  72.      * 操作主设备为LED_MAJOR的设备文件时,就会调用s3c24xx_leds_fops中的相关成员函数
  73.      * LED_MAJOR可以设为0,表示由内核自动分配主设备号
  74.      */
  75.     ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c2410_leds_fops);
  76.     if (ret < 0) {
  77.       printk(DEVICE_NAME " can't register major number\n");
  78.       return ret;
  79.     }
  80.     printk(DEVICE_NAME " initialized\n");
  81.     return 0;
  82. }

  83. //执行”rmmod s3c24xx_leds.ko”命令时就会调用这个函数
  84. static void __exit s3c2410_leds_exit(void)
  85. {
  86.         unregister_chrdev(LED_MAJOR, DEVICE_NAME);/* 卸载驱动程序 */
  87. }

  88. module_init(s3c2410_leds_init);   /* 指定驱动程序的初始化函数和卸载函数 */
  89. module_exit(s3c2410_leds_exit);
复制代码
小王,上面的代码看清楚了吗,那么怎么使用呢,使用方法有两种(假设保存为leds_control.c):

方法一:
将代码放到内和drivers/char目录下,在drivers/char/Makefile中增加一行obj-m    += leds_control.o,然后在内核根目录下执行make modules,就可以生成模块drivers/char/leds_control.ko。

方法二:

直接在当前驱动源码目录下,建立Makefile文件,内容如下:

CROSS=arm-linux-

#依赖的内核源代码目录,不一定是当前系统的,要是开发板系统源码的目录
KERNELDIR = /opt/linux-2.6.32.2
PWD := $(shell pwd)
.PHONY:

<tab>modules clean
obj-m += leds_control.o
modules:

<tab>$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
<tab>rm -rf *.o *~ core .depend .*.cmd  *.mod.c .tmp_versions

然后执行make,可以看到在当前目录下也会生成leds_control.ko.



OK,小王,看到没,通过以上两不,生成了leds_control.ko,下面就用ftp下载到开发板上,用:

insmod ./leds_control.ko

mknod /dev/leds_control c 150 0

就建好了驱动模块和设备节点,应用程序调用那就是上一篇的事了…

“嗯,我明白了,你这样一说,我几乎都明白了,led的前前后后的工作模式,不要和你说了,我也抓紧时间调调我的自己LED驱动”小王娇气的说。

作者: cu_Cbear   发布时间: 2010-11-08

有点意思!!!!顶楼主

作者: xuyun1227   发布时间: 2010-11-09



QUOTE:
有点意思!!!!顶楼主
xuyun1227 发表于 2010-11-09 13:15




    写的有意思,我借点光,哈哈

作者: cu_Cbear   发布时间: 2010-11-09

热门下载

更多