LED通过字符设备驱动的分步实现编写LED驱动,另外实现特备文件和设备的绑定

head.h

#ifndef __HEAD_H__
#define __HEAD_H__

#define PHY_LED13_MODER 0x50006000 //[20 21]/[16 17] >01 
#define PHY_LED13_ODR 0x50006014 // [10]/[8] 
#define PHY_LED2_MODER 0x50007000 //[20 21] >01
#define PHY_LED2_ODR 0x50007014 //10
#define PHY_RCC 0x50000A28 //4 5位

#define LED_ON _IOW('l',1,int *)
#define LED_OFF _IOW('l',0,int *)


#endif

text.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"

int main(int argc,const char * argv[])
{
    int a;
    int b;
    

    while(1)
    {          
        int fd;
        //从中断获取我们想实现的功能功能
        printf("请选择要控制的灯:0-LED1,1-LED2,2-LED3\n");
        scanf("%d",&a);
        switch(a)
        {   
            case 1:
            fd = open("/dev/mycdev0",O_RDWR);
            break;
            case 2:
            fd = open("/dev/mycdev1",O_RDWR);
            break;
            case 3:
            fd = open("/dev/mycdev2",O_RDWR);
            break;
        }
        if(fd<0)
        {
            printf("打开设备文件失败\n");
            return -1;
        }
        printf("打开设备文件成功\n");

        printf("请输入想要实现的功能 ");
        printf("0(关灯)1(开灯)\n");
        printf("请输入:");
        scanf("%d",&b);

        switch (b)
        {
            case 1:
            ioctl(fd,LED_ON,&b);
            break;
            case 0:
            ioctl(fd,LED_OFF,&b);
            break;
        }
        close(fd);
    }
    

    return 0;
}

demo.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"


struct class *cls;
struct device *dev;

unsigned int *vir_moder13;
unsigned int *vir_moder2;
unsigned int *vir_odr13;
unsigned int *vir_odr2;
unsigned int *vir_rcc;


char kbuf[128]={};
//封装操作方法
int mycdev_open(struct inode * inode,struct file* file)
{
    unsigned int min=MINOR(inode->i_rdev);//获取打开的文件的次设备号
    file->private_data=(void *)min;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
/*
ssize_t mycdev_read(struct file * file,char *ubuf,size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    unsigned long ret;
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy to user filed\n");
        return -EIO;
    }
    return 0;
}
ssize_t mycdev_write(struct file * file,const char *ubuf,size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    unsigned long ret;
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy from user filed\n");
        return -EIO;
    }
    //控制LED灯
    if(kbuf[0]=='0')//关灯
    {
        (*vir_odr13)&=~(0x1<<10);
        (*vir_odr13)&=~(0x1<<8);
        (*vir_odr2)&=~(0x1<<10);
    }
    else if(kbuf[0]=='1')//开LED1
    {
        (*vir_odr13)|=(0x1<<10);
    }
    else if(kbuf[0]=='2')//开LED2
    {
        (*vir_odr2)|=(0x1<<10);
    }
    else if(kbuf[0]=='3')//开LED3
    {
        (*vir_odr13)|=(0x1<<8);
    }

    return 0;
}
*/
int mycdev_close(struct inode * indode,struct file* file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
long mycdev_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
    int min=(int)file->private_data;//获取文件的次设备号
    
    switch(min)
    {
        case 0://LED1
        switch(cmd)
        {
            case LED_ON://开灯
            (*vir_odr13) |=(0x1<<10);
            break;
            case LED_OFF://关灯
            (*vir_odr13) &=~(0x1<<10);
            break;
        }
        break;
        case 1://LED2
        switch(cmd)
        {
            case LED_ON://开灯
            (*vir_odr2) |=(0x1<<10);
            break;
            case LED_OFF://关灯
            (*vir_odr2) &=~(0x1<<10);
            break;
        }
        break;
        case 2://LED3
        switch(cmd)
        {
            case LED_ON://开灯
            (*vir_odr13) |=(0x1<<8);
            break;
            case LED_OFF://关灯
            (*vir_odr13) &=~(0x1<<8);
            break;
        }
        break;
    }
    return 0;

}

struct file_operations fops = {
    .open=mycdev_open,
    //.read=mycdev_read,
    //.write=mycdev_write,
    .unlocked_ioctl = mycdev_ioctl,
    .release=mycdev_close,
};

struct cdev* cdev;
unsigned int major=0;
unsigned int minor=0;
dev_t devno;
static int __init mycdev_init(void)
{
    int i;
    //注册字符设备驱动
     //1.申请一个对象空间cdev_alloc
    int ret;
    cdev=cdev_alloc();
    if(cdev==NULL)
    {
        printk("申请字符设备驱动对象空间失败\n");
        ret =-EFAULT;
        goto out1;
    }
    printk("申请字符设备驱动对象空间成功\n");
    //2.初始化对象cdev_init
    cdev_init(cdev,&fops);
    //3.申请设备号  register_chrdev_region()/alloc_chrdev_region()
    if(major==0)//动态申请
    {
        ret=alloc_chrdev_region(&devno,minor,3,"mychrdev");
        if(ret)
        {
            printk("动态申请设备号失败\n");
            goto out2;
        }
        major=MAJOR(devno);//根据设备号获取主设备号
        minor=MINOR(devno);//根据设备号获取次设备号
    }
    else//静态申请
    {
        ret=register_chrdev_region(MKDEV(major,minor),3,"mychrdev");
        if(ret)
        {
            printk("静态申请设备号失败\n");
            goto out2;
        }
    }
    printk("设备号申请成功\n");
    //4.注册驱动对象  cdev_add
    ret=cdev_add(cdev,MKDEV(major,minor),3);
    if(ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto out3;
    }
    printk("注册字符设备驱动对象成功\n");

    if(major<0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动成功major=%d\n",major);

    //5.向上提交目录  class_create
    cls=class_create(THIS_MODULE,"mychrdev");
    if(IS_ERR(cls))
    {
        printk("想上提交目录失败\n");
        goto out4;
    }
    printk("想上提交目录成功\n");
    //6.向上提交设备节点信息  device_create
    
    for(i=0;i<3;i++)
    {
        dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
        if(IS_ERR(dev))
        {
            printk("想上提交设备节点失败\n");
            goto out5;
        }
    }
    printk("想上提交设备节点成功\n");

    //进行寄存器的地址映射
    vir_moder13 = ioremap(PHY_LED13_MODER,4);
    if(vir_moder13 == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_moder2 = ioremap(PHY_LED2_MODER,4);
    if(vir_moder13 == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_odr13 = ioremap(PHY_LED13_ODR,4);
    if(vir_odr13 == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_odr2 = ioremap(PHY_LED2_ODR,4);
    if(vir_odr2 == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_rcc = ioremap(PHY_RCC,4);
    if(vir_rcc == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    printk("寄存器内存映射成功\n");

    //寄存器初始化
    //rcc初始化
    (*vir_rcc) |= (0x3<<4);//GPIOEF控制器时钟使能
    (*vir_moder13) &= ~(0x3<<20);
    (*vir_moder13) |= (0x1<<20);//LED1
    (*vir_moder13) &= ~(0x3<<16);
    (*vir_moder13) |= (0x1<<16);//LED3
    (*vir_moder2) &= ~(0x3<<20);
    (*vir_moder2) |= (0x1<<20);//LED2
    (*vir_odr13) &=~(0x1<<10);//LED1
    (*vir_odr13) &=~(0x1<<8);//LED3
    (*vir_odr2) &=~(0x1<<10);//LED2

    return 0;
    printk("初始化LED成功");
out5:
    //当提交设备节点中途失败时,将提交成功的节点释放
    for(i--;i>=0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //销毁目录
    class_destroy(cls);
out4:
    cdev_del(cdev);
out3:
    unregister_chrdev_region(MKDEV(major,minor),3);
out2:
    kfree(cdev);
out1:
    return ret;
}

static void __exit mycdev_exit(void)
{
    int i;
    //取消内存映射
    iounmap(vir_moder13);
    iounmap(vir_moder2);
    iounmap(vir_odr13);
    iounmap(vir_odr2);
    iounmap(vir_rcc);
    
    //1.销毁设备节点信息
    for(i=0;i<3;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //2.销毁目录
    class_destroy(cls);
    //3.注销字符设备驱动对象
    cdev_del(cdev);
    //4.释放设备号
    unregister_chrdev_region(MKDEV(major,minor),3);
    //5.释放申请到的字符设备驱动对象空间
    kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

你可能感兴趣的:(linux,c语言,驱动开发)