(转)“手把手教你学linux驱动开发”OK6410系列之03---LED字符设备驱动

来源:互联网 发布:curl算法 编辑:程序博客网 时间:2024/04/30 18:27


2011-12-22 09:44:24|  分类:Embeded |字号 订阅

       作者:沧海猎人   出处:http://blog.csdn.net/embedded_hunter 

上一篇文章我们介绍了字符设备驱动程序的框架,本篇文章我们将操作真实的硬件---LED。

 一、实验环境 

开发机环境

          操作系统:ubuntu 9.10

          交叉编译环境:arm-linux-gcc 4.2.2 ,安装位置 /usr/local/arm/4.3.2/

          6410板子内核源码路径:/work/linux-2.6.36.2-v1.05/     

目标板环境:OK6410-A     linux2.6.36

 

二、实验原理

         控制LED是最简单的一件事情,我们学习LED驱动程序,就相当于学习其他编程语言是的“hello world”程序一样,是一个入门的程序。

         学习驱动程序,必须要对硬件有所了解,接下来看几个与硬件相关的材料。

 

    

                                           OK6410  LED原理图

 

    

                                              OK6410  LED原理图

 

从上面的原理图可以得知,LED与CPU引脚的连接方法如下,低电平点亮。

     LED1 -GPM0

     LED2 -GPM1

     LED3 -GPM2

     LED4 -GPM3

    

从数据手册可以找到相应的控制方法。这里我们以LED1为例,介绍一下LED1的操作方法,其他的类似,请大家自行分析。

通过上面可以得知,需要先将GPM0设置为输出方式。将相应的寄存器进行配置。

 

然后将GPMDAT寄存器的第0位置0灯亮,置1灯灭。

 

三、实验步骤

1、编写驱动程序

driver_led.c

view plainprint?
  1. #include <linux/module.h>  
  2.   
  3. #include <linux/kernel.h>  
  4. #include <linux/fs.h>  
  5. #include <asm/uaccess.h> /* copy_to_user,copy_from_user */  
  6. #include <linux/miscdevice.h>    
  7. #include <linux/pci.h>    
  8. #include <mach/map.h>    
  9. #include <mach/regs-gpio.h>    
  10. #include <mach/gpio-bank-m.h>    
  11. #include <plat/gpio-cfg.h>  
  12.   
  13. #define LED_MAJOR 240  
  14.   
  15. int led_open (struct inode *inode,struct file *filp)  
  16.   
  17. {  
  18.     unsigned tmp;     
  19.          tmp = readl(S3C64XX_GPMCON);     
  20.     tmp = (tmp & ~(0x7U<<1))|(0x1U);     
  21.          writel(tmp, S3C64XX_GPMCON);   
  22.     printk("#########open######\n");  
  23.     return 0;  
  24. }  
  25.   
  26. ssize_t led_read (struct file *filp, char __user *buf, size_t count,loff_t *f_pos)  
  27. {  
  28.     printk("#########read######\n");  
  29.     return count;  
  30. }  
  31.   
  32.   
  33. ssize_t led_write (struct file *filp, const char __user *buf, size_t count,loff_t *f_pos)  
  34. {  
  35.     char wbuf[10];  
  36.     unsigned tmp;     
  37.     printk("#########write######\n");  
  38.     copy_from_user(wbuf,buf,count);  
  39.     switch(wbuf[0])  
  40.     {  
  41.         case 0:  //off  
  42.             tmp = readl(S3C64XX_GPMDAT);     
  43.                           tmp |= (0x1U);     
  44.                           writel(tmp, S3C64XX_GPMDAT);  
  45.             break;  
  46.         case 1:  //on  
  47.             tmp = readl(S3C64XX_GPMDAT);     
  48.                           tmp &= ~(0x1U);     
  49.                           writel(tmp, S3C64XX_GPMDAT);  
  50.             break;  
  51.         default :  
  52.             break;  
  53.     }  
  54.     return count;  
  55. }  
  56.   
  57. int led_release (struct inode *inode, struct file *filp)  
  58. {  
  59.     printk("#########release######\n");  
  60.     return 0;  
  61. }  
  62.   
  63. struct file_operations led_fops ={  
  64.     .owner = THIS_MODULE,  
  65.     .open = led_open,  
  66.     .read = led_read,  
  67.     .write = led_write,  
  68.     .release = led_release,  
  69. };  
  70.   
  71. int __init led_init (void)  
  72. {   int rc;  
  73.     printk ("Test led dev\n");  
  74.     rc = register_chrdev(LED_MAJOR,"led",&led_fops);  
  75.     if (rc <0)  
  76.     {  
  77.         printk ("register %s char dev error\n","led");  
  78.         return -1;  
  79.     }  
  80.     printk ("ok!\n");  
  81.     return 0;  
  82. }  
  83.   
  84. void __exit led_exit (void)  
  85. {  
  86.     unregister_chrdev(LED_MAJOR,"led");  
  87.     printk ("module exit\n");  
  88.     return ;  
  89. }  
  90.   
  91. module_init(led_init);  
  92. module_exit(led_exit);  
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <asm/uaccess.h> /* copy_to_user,copy_from_user */ #include <linux/miscdevice.h> #include <linux/pci.h> #include <mach/map.h> #include <mach/regs-gpio.h> #include <mach/gpio-bank-m.h> #include <plat/gpio-cfg.h> #define LED_MAJOR 240 int led_open (struct inode *inode,struct file *filp) { unsigned tmp; tmp = readl(S3C64XX_GPMCON); tmp = (tmp & ~(0x7U<<1))|(0x1U); writel(tmp, S3C64XX_GPMCON); printk("#########open######\n"); return 0; } ssize_t led_read (struct file *filp, char __user *buf, size_t count,loff_t *f_pos) { printk("#########read######\n"); return count; } ssize_t led_write (struct file *filp, const char __user *buf, size_t count,loff_t *f_pos) { char wbuf[10]; unsigned tmp; printk("#########write######\n"); copy_from_user(wbuf,buf,count); switch(wbuf[0]) { case 0: //off tmp = readl(S3C64XX_GPMDAT); tmp |= (0x1U); writel(tmp, S3C64XX_GPMDAT); break; case 1: //on tmp = readl(S3C64XX_GPMDAT); tmp &= ~(0x1U); writel(tmp, S3C64XX_GPMDAT); break; default : break; } return count; } int led_release (struct inode *inode, struct file *filp) { printk("#########release######\n"); return 0; } struct file_operations led_fops ={ .owner = THIS_MODULE, .open = led_open, .read = led_read, .write = led_write, .release = led_release, }; int __init led_init (void) { int rc; printk ("Test led dev\n"); rc = register_chrdev(LED_MAJOR,"led",&led_fops); if (rc <0) { printk ("register %s char dev error\n","led"); return -1; } printk ("ok!\n"); return 0; } void __exit led_exit (void) { unregister_chrdev(LED_MAJOR,"led"); printk ("module exit\n"); return ; } module_init(led_init); module_exit(led_exit);

 

Makefile文件

view plainprint?
  1. obj-m := driver_led.o  
  2. KDIR :=/work/linux-2.6.36.2-v1.05/  
  3. all:  
  4.     make -C $(KDIR) M=$(shell pwd) modules  
  5. install:  
  6.     cp driver_led.ko /tftpboot/  
  7. clean:  
  8.     make -C $(KDIR) M=$(shell pwd) clean  
obj-m := driver_led.o KDIR :=/work/linux-2.6.36.2-v1.05/ all: make -C $(KDIR) M=$(shell pwd) modules install: cp driver_led.ko /tftpboot/ clean: make -C $(KDIR) M=$(shell pwd) clean

 

2、编写测试程序

test.c

view plainprint?
  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/stat.h>  
  4. #include <fcntl.h>  
  5. int main (void)  
  6. {  
  7.     int fd;  
  8.     char buf[10]={0,1};  
  9.     fd = open("/dev/my_led",O_RDWR);  
  10.     if (fd < 0)  
  11.     {  
  12.         printf ("Open /dev/my_led file error\n");  
  13.         return -1;  
  14.     }     
  15.     while(1)  
  16.     {  
  17.         write(fd,&buf[0],1);  
  18.         sleep(1);  
  19.         write(fd,&buf[1],1);  
  20.         sleep(1);  
  21.     }  
  22.     close (fd);  
  23.     return 0;  
  24.   
  25. }  
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main (void) { int fd; char buf[10]={0,1}; fd = open("/dev/my_led",O_RDWR); if (fd < 0) { printf ("Open /dev/my_led file error\n"); return -1; } while(1) { write(fd,&buf[0],1); sleep(1); write(fd,&buf[1],1); sleep(1); } close (fd); return 0; }

 

 

3、编译驱动程序与测试程序

      编译驱动程序

      #make

      将驱动程序放到tftp的工作目录 /tftpboot

      #make install

      编译测试程序

      #arm-linux-gcc  test.c  -o  test

      将测试程序放到tftp的工作目录 /tftpboot

       #cp  test  /tftpboot

 

4、将程序下载到开发板

       将开发板的IP地址修改,与主机在同一个网段。确保PC的tftp服务开启。

      下载程序到开发板

        SMDK6410#   tftp -l /lib/modules/2.6.36.2/driver_led.ko -r driver_led.ko  -g  192.168.1.111        192.168.1.111为服务器IP

        SMDK6410#   tftp -l test  -r test  -g  192.168.1.111        

 

5、测试

        加载驱动   #insmod  /lib/modules/2.6.36.2/driver_led.ko

        创建设备文件   #mknod  /dev/my_led  c   240  0

        测试  ./test

        [root@FORLINX6410]# ./test
      此时可以看到OK6410板子上的LED0在闪烁。

       卸载驱动  #rmmod   driver_led

       从上面的结果我们可以看到,当用户调用相应的文件操作函数时,驱动程序中的相应的函数也会被调用。

        大家可以修改相应程序,测试一下其他的情况。


原创粉丝点击