OK6410 linux第一个字符型设备驱动:LED驱动

来源:互联网 发布:魔兽争霸同步数据失败 编辑:程序博客网 时间:2024/05/11 18:29
在OK6410开发板上实现linux环境下的第一个字符设备驱动:LED驱动
下面是驱动代码,内核是linux-3.0.1版本。
        #include <linux/kernel.h>        #include <linux/module.h>        #include <linux/init.h>        #include <linux/fs.h>        #include <linux/pci.h>        #include <linux/miscdevice.h>        #include <asm/uaccess.h>/*copy_to_user,copy_from_user*/        #include <mach/map.h>        #include <mach/regs-gpio.h>        #include <mach/gpio-bank-m.h>        #include <plat/gpio-cfg.h>        #include <asm/io.h>        #include <linux/delay.h>        #define LED_MAJOR 0        volatile unsigned long *GPM_CON = NULL;        volatile unsigned long *GPM_DAT = NULL;        static struct class *LED_Class;        static struct class_device *LED_Class_Devs;        int led_open (struct inode *inode,struct file *filp)         {          printk("LED_drv_open\n");        /* 配置GPM0,1,2,3输出*/        *GPM_CON &= 0xffff1111;//~((0xf<<(0*4))|(0xf<<(1*4))|(0xf<<(2*4))|(0xf<<(3*4)));        //*GPM_CON |= ((0x1<<(0*4))|(0x1<<(1*4))|(0x1<<(2*4))|(0x1<<(3*4)));        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)          {              int val;            printk("LED_drv_write\n");            copy_from_user(&val,buf,count);            if(1==val)            {                *GPM_DAT &=0x0;  //关灯            }            else            {                *GPM_DAT |= 0xf;//开灯            }            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 major;          int __init led_init (void)          {              int major;              printk ("Test led dev\n");              major = register_chrdev(0,"led",&led_fops);  //向内核注册驱动            if (major <0)              {                  printk ("register %s char dev error\n","led");                  return -1;              }              LED_Class=class_create(THIS_MODULE,"LED");            if(IS_ERR(LED_Class))                return PTR_ERR(LED_Class);            LED_Class_Devs = device_create(LED_Class,NULL,MKDEV(major,0),NULL,"LED");            if(unlikely(IS_ERR(LED_Class_Devs)))                return PTR_ERR(LED_Class_Devs);                GPM_CON = (volatile unsigned long *)ioremap(0x7F008820,4);            GPM_DAT=GPM_CON+1;                  return 0;          }          void __exit led_exit (void)          {              unregister_chrdev(major,"led");        //  device_destroy(LED_Class_Devs);        /*  class_device_unregister(LED_Class_Devs);*/            class_destroy(LED_Class);            iounmap(GPM_CON);            printk ("module exit\n");              return ;          }             module_init(led_init);          module_exit(led_exit);          MODULE_AUTHOR("liqiuhua liqiuhua2016@gmail.com");        MODULE_DESCRIPTION("S3C6410 LED driver");        MODULE_LICENSE("GPL");        MODULE_ALIAS("platform:s3c6410_led");

下面是测试代码:

    #include <stdio.h>      #include <sys/types.h>      #include <sys/stat.h>      #include <fcntl.h>      int main (int argc, char **argv)      {          int fd;          char buf[10]={0,1};          int val;        fd = open("/dev/LED",O_RDWR);          if (fd < 0)          {              printf ("Open /dev/LED file error\n");              return -1;          }             if(argc != 2)         {            printf ("Usage: \n");            printf ("%s <on | off>\n",argv[0]);            return -1;        }        if(strcmp(argv[1],"on")==0)            val=1;        else            val=0;        write(fd,&val,4);        close (fd);          return 0;      } 
0 0
原创粉丝点击