驱动开发day8

head.h

#ifndef __HEAD_H__
#define __HEAD_H__ 

// 构建开灯关灯的功能码
#define LED_ON _IO('l', 1)
#define LED_OFF _IO('l', 0)
#endif 

demo.c

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

struct device_node *dnode;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;

char kbuf[128]={0};

struct cdev *cdev;
unsigned int major=0;
unsigned int minor=0;
dev_t devno;
struct class *cls;
struct device *dev;

int mycdev_open(struct inode *inode, struct file *file)
{
    int min=MINOR(inode->i_rdev);//获取打开的的文件的次设备号
    file->private_data= (void *)min;
    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:    //开灯
                    gpiod_set_value(gpiono1,1);
                    break;
                case LED_OFF:   //关灯
                    gpiod_set_value(gpiono1,0);
                    break;
            }
            break;
         case 1:    //操作LED2
            switch(cmd)
            {
                case LED_ON:    //开灯
                    gpiod_set_value(gpiono2,1);
                    break;
                case LED_OFF:   //关灯
                    gpiod_set_value(gpiono2,0);
                    break;
            }
            break;
         case 2:    //操作LED3
            switch(cmd)
            {
                case LED_ON:    //开灯
                    gpiod_set_value(gpiono3,1);
                    break;
                case LED_OFF:
                    gpiod_set_value(gpiono3,0);
                    break;
            }
            break;
    }
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

// 定义操作方法结构体变量并赋值
struct file_operations fops = {

    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};


static int __init mycdev_init(void)
{
    //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");
    //5.向上提交目录  class_create
    cls=class_create(THIS_MODULE,"mychrdev");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        goto out4;
    }
    printk("向上提交目录成功\n");
    //6.向上提交设备节点信息  device_create
    int i;
    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");



	//解析设备树节点信息
		dnode = of_find_node_by_path("/myled");
		if(dnode == NULL)
		{
			printk("解析设备树节点失败\n");
			return -ENXIO;
		}

		//获取LED2 GPIO编号
		gpiono2 = gpiod_get_from_of_node(dnode, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
		if(IS_ERR(gpiono2))
		{
			printk("申请gpio信息失败\n");
			return -PTR_ERR(gpiono2);
		}

		//获取LED1 GPIO编号
		gpiono1 = gpiod_get_from_of_node(dnode, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
		if(IS_ERR(gpiono1))
		{
			printk("申请gpio信息失败\n");
			return -PTR_ERR(gpiono1);
		}

		//获取LED3 GPIO编号
		gpiono3 = gpiod_get_from_of_node(dnode, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
		if(IS_ERR(gpiono3))
		{
			printk("申请gpio信息失败\n");
			return -PTR_ERR(gpiono3);
		}


    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)
{
	gpiod_set_value(gpiono1,0);
	gpiod_set_value(gpiono2,0);
	gpiod_set_value(gpiono3,0);
	//释放GPIO编号
	gpiod_put(gpiono1);
	gpiod_put(gpiono2);
	gpiod_put(gpiono3);


    //1.销毁设备节点信息
    int i;
    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");

test.c

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


int main(int argc, char const *argv[])
{ 
    int a,b;
    while(1)
    {
        char buf[128]="/dev/mycdev";
        //从终端读取
        printf("请输入要实现的LED灯:");
        printf("0(LED1) 1(LED2) 2(LED3)\n");
        printf("请输入>");
        scanf("%d",&b);
        sprintf(buf,"%s%d",buf,b);

        int fd=open(buf,O_RDWR);
        if(fd<0)
        {
        printf("打开设备文件失败\n");
        exit(-1);
        }
    
        //从终端读取
        printf("请输入要实现的功能 ");
        printf("0(关灯) 1(开灯)\n");
        printf("请输入>");
        scanf("%d",&a);
        switch(a)
        {
            case 1:
                ioctl(fd,LED_ON);
                break;
            case 0:
                ioctl(fd,LED_OFF);
                break;
        }
        close(fd);
    }
    return 0;

计时器

#include 
#include 
#include 
#include 
#include 
#include 

struct timer_list hw;

void mytime_hello(struct timer_list *timer)
{
	printk("hello world\n");

	mod_timer(timer, jiffies + 5*HZ);
}

//入口函数,安装内核模块时执行
static int __init mycdev_init(void)
{	
	timer_setup(&hw, mytime_hello, 0);
	hw.expires = jiffies + 5*HZ;
	add_timer(&hw);
	return 0;
}

//出口函数,卸载内核模块时执行
static void __exit mycdev_exit(void)
{	
	del_timer(&hw);
}

//用于声明入口函数
module_init(mycdev_init);
//用于声明出口函数
module_exit(mycdev_exit);
//声明当前内核模块遵循GPL协议
MODULE_LICENSE("GPL");

你可能感兴趣的:(驱动开发)