字符设备驱动内部实现原理+ioctl控制LED灯

text.c

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

int main(int argc,const char * argv[])
{
    char buf[128] = {};
    int fd;
    int num;
    int ctlnum;
    fd = open("/dev/mycdev1", O_RDWR);      //设备节点名,设备节点实在驱动文件创建的,
        if (fd < 0)                         //fd是用来和驱动文件节点建立联系,这个驱动节点控制了LED灯(自己设置的)
        {
            perror("open dev error");
            exit(-1);
        }
        printf("成功打开设备文件\n");
    while(1)
    {
        printf("请输入要实现的功能:1(开灯) 0(关灯)>");
        scanf("%d",&num);
        printf("请输入要控制的灯:1(LED1) 2(LED2) 3(LED3)>");
        scanf("%d",&ctlnum);
        switch(num)
        {
            case 1:
                ioctl(fd,LED_ON,&ctlnum);   //传指针更灵活,这样可以传字符
                break;
            case 0:
                ioctl(fd,LED_OFF,&ctlnum);
                break;
            default:
                break;
        }
    }  
    close(fd);
    return 0;
}

mycdev.c

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

char kbuf[128] = {0};  // 用来接用户空间传递的数据

// 定义指针接收映射成功的虚拟内存首地址
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;

// 接收struct class/struct device 类型空间的首地址
struct class *cls;
struct device *dev;

struct cdev * cdev;             //字符驱动设备空间地址        
unsigned int major=0;          // 主设备号
unsigned int minor=0;         //次设备号
dev_t devnum;                //设备号

// 封装操作方法,自己封装的函数
int mycdev_open(struct inode *inode, struct file *file)
{
    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__);
    return 0;
}
ssize_t mycdev_write(struct file * file, const char * ubuf, size_t size, loff_t * lof)
{
    printk("%s:%s:%d:%s\n",__FILE__,__func__,__LINE__,kbuf);
    return 0;
}
int mycdev_close(struct inode * inode, 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)    
{   
    //cmd:ioctl第二个参数的值,arg:ioctl第三个参数的值
    //ioctl(fd,LED_ON,&ctlnum)  1:文件描述符,2:要实现的功能的功能码,3:数值的地址
    //_IOW('l',1,int)   1:设备标识,2:要实现的功能,3:是ioctl第三个参数的大小
    
    int which;  //驱动的容器,用来接收控制那个灯功能
    int ret=copy_from_user(&which,(unsigned int *)arg,4);   //1:要给的是谁,2:要获取的对象
    if(ret)
    {
        printk("copy_from_user error");
        return ret;
    }
    //根据用户空间功能码的不同,实现硬件不同控制
    switch(cmd)
    {
        case LED_ON:
        switch(which)
        {
            case 1: //LED1
                vir_led1->ODR |= (0x1 << 10);
                break;
            case 2: //LED2
                vir_led2->ODR |= (0x1 << 10);
                break;
            case 3: //LED3
                vir_led3->ODR |=(0x1 << 8);
                break;
        }
        break;
        case LED_OFF:
        switch(which)
        {
            case 1: //LED1
                vir_led1->ODR &= (~(0x1 << 10));
                break;
            case 2: //LED2
                vir_led2->ODR &= (~(0x1 << 10));
                break;
            case 3: //LED3
                vir_led3->ODR &= (~(0x1 << 8));
                break;
        }
        break;
    }
    return 0;
}
//定义操作方法结构体变量并初始化,让结构体变量指向自己封装的函数名
struct file_operations fops= {
    .open=mycdev_open,
    .read=mycdev_read,
    .write=mycdev_write,
    .release=mycdev_close,
    .unlocked_ioctl =mycdev_ioctl,
};
int led_init(void)
{
    //-----------------------------------完成硬件寄存器物理内存的映射---------------------------
    vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if (vir_led1 == NULL)
    {
        printk("led1:物理内存映射失败__%d__\n", __LINE__);
        return -EFAULT;
    }
    vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    if (vir_led2 == NULL)
    {
        printk("led2:物理内存映射失败__%d__\n", __LINE__);
        return -EFAULT;
    }
    vir_led3 = vir_led1;
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("rcc:物理内存映射失败__%d__\n", __LINE__);
        return -EFAULT;
    }
    printk("led1/led2/led3/rcc:物理内存映射成功\n");

    //----------------------------------------硬件寄存器初始化-------------------------------
    // rcc初始化
    (*vir_rcc) |= (0x3 << 4);
    // led1初始化
    vir_led1->MODER &= (~(0x3 << 20));
    vir_led1->MODER |= (0x1 << 20);
    vir_led1->ODR &= (~(0x1 << 10));
    // led2初始化
    vir_led2->MODER &= (~(0x3 << 20));
    vir_led2->MODER |= (0x1 << 20);
    vir_led2->ODR &= (~(0x1 << 10));
    // led3初始化
    vir_led3->MODER &= (~(0x3 << 16));
    vir_led3->MODER |= (0x1 << 16);
    vir_led3->ODR &= (~(0x1 << 8));
    return 0;
}

static int __init mycdev_init(void)
{
    int i;  //设备节点
    int ret;
    //1.分配字符设备驱动对象
    //申请一个字符设备对象驱动空间
    cdev=cdev_alloc();
    if(cdev == NULL)
    {
        printk("cdev_alloc error\n");
        ret = -EFAULT;
        goto OUT1;
    }
    printk("申请字符设备驱动成功\n");
    //2.初始化字符设备驱动对象
    cdev_init(cdev,&fops);
    //3.申请设备号
    if(major > 0)   //静态申请设备号
    {
        ret=register_chrdev_region(MKDEV(major,minor),3,"mychrdev");
        if(ret)
        {
            printk("静态申请设备号失败\n");
            goto OUT2;
        }
        printk("静态申请设备号成功\n");
    }
    else if(major == 0)
    {
        ret=alloc_chrdev_region(&devnum,minor,3,"mychrdev");
        if(ret)
        {
            printk("动态申请设备号失败\n");
            goto OUT2;
        }
        printk("动态申请设备号成功\n");
        minor=MINOR(devnum);    //根据设备号获取次设备号
        major=MAJOR(devnum);    //根据设备号获取主设备号
    }
    //4.注册字符设备驱动对象
    ret=cdev_add(cdev,MKDEV(major,minor),3);
    if(ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto OUT3;
    }
    printk("注册字符设备驱动对象成功\n");
    //5.向上提交目录信息
    cls=class_create(THIS_MODULE,"mycdev");
    if(IS_ERR(cls))
    {
        printk("向上提交目录信息失败\n");
        ret=-PTR_ERR(cls);
        goto OUT4;
    }
    printk("向上提交目录信息成功\n");
    //6.向上提交设备节点信息
    for(i=0;i<3;i++)
    {
        dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
        if(IS_ERR(dev))
        {
            printk("向上提交设备节点信息失败\n");
            ret= -PTR_ERR(dev);
            goto OUT5;
        }
    }
    printk("向上提交设备节点信息成功\n");
    led_init();
    return 0;
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)
{
    unsigned int i;
    //----------------------------------------取消物理内存映射-------------------------------
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_led3);
    //----------------------------------------销毁目录/设备信息-------------------------------
    //<1.设备销毁,现有目录后有设备,所以先销设备,后销目录
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    //<2.目录销毁
    class_destroy(cls);
    //注销字符设备驱动对象
    cdev_del(cdev);
    //释放设备号
    unregister_chrdev_region(MKDEV(major,minor),3);
    //释放对象空间
    kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

head.h

#ifndef __HEAD_H__
#define __HEAD_H__

typedef struct
{
    unsigned int MODER;
    unsigned int OTYPER;
    unsigned int OSPEEDR;
    unsigned int PUPDR;
    unsigned int IDR;
    unsigned int ODR;
}gpio_t;
#define PHY_LED1_ADDR 0x50006000
#define PHY_LED2_ADDR 0x50007000
#define PHY_LED3_ADDR 0x50006000
#define PHY_RCC_ADDR  0x50000A28

#define LED_ON _IOW('l',1,int)  //开灯,1控制灯亮,int是ioctl第三的参数的大小,传个int系统自己计算大小4
#define LED_OFF _IOW('l',0,int)  //关灯

#endif

你可能感兴趣的:(汇编,arm开发,驱动开发)