【编写LED驱动,创建三个设备文件,每一个设备文件和一个LED灯绑定,当操作这个设备文件时只能控制对应的这盏灯】

作业:

编写LED驱动,创建三个设备文件,每一个设备文件和一个LED灯绑定,当操作这个设备文件时只能控制对应的这盏灯。
1.将GPIO的相关寄存器封装成结构体 --------> head.h
2.LED相关驱动文件 --------> led.c
led0 ------> LED1
led1 ------> LED2
led2 ------> LED3
3.应用层测试文件 --------> test.c

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;
//定义GPIOE和GPIOF
#define GPIOE 0X50006000 //LED1 PE10  LED3 PE8
#define GPIOF 0X50007000 //LED2 PF10
//#define GPIOB 0X50003000
#define RCC 0X50000A28

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

#endif

test.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"

int main(int argc, char const *argv[])
{
    int a, b;
    char buf[128] = {0};
    int fd_led1 = open("/dev/led0", O_RDWR);
    if (fd_led1 < 0)
    {
        printf("打开LED1设备文件失败\n");
        exit(-1);
    }
    int fd_led2 = open("/dev/led1", O_RDWR);
    if (fd_led2 < 0)
    {
        printf("打开LED2设备文件失败\n");
        exit(-1);
    }
    int fd_led3 = open("/dev/led2", O_RDWR);
    if (fd_led3 < 0)
    {
        printf("打开LED3设备文件失败\n");
        exit(-1);
    }

    while (1)
    {
        // 从终端读取
        printf("请选择LED1灯功能\n");
        printf("0(关) 1(开)>");
        scanf("%d", &a);
        printf("请输入要控制的灯\n");
        printf("0(LED1) 1(LED2) 2(LED3)>");
        scanf("%d", &b);
        if (a == 1) // 开灯
        {
			switch (b)
			{
			case 0:
           	    ioctl(fd_led1, LED_ON, b);
				break;
			case 1:
           	    ioctl(fd_led2, LED_ON, b);
				break;
			case 2:
           	    ioctl(fd_led3, LED_ON, b);
				break;
			}
        }
        else if (a == 0) // 关灯
        {			
			switch (b)
			{
			case 0:
           	    ioctl(fd_led1, LED_OFF, b);
				break;
			case 1:
           	    ioctl(fd_led2, LED_OFF, b);
				break;
			case 2:
           	    ioctl(fd_led3, LED_OFF, b);
				break;
			}
        }
    }
    close(fd_led1);
    close(fd_led2);
    close(fd_led3);
    return 0;
}


led0.c

#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/device.h>
#include<linux/cdev.h>
#include<linux/slab.h>
#include<linux/io.h>
#include "head.h"

struct cdev *cdev; 		//字符设备空间首地址
unsigned int major=500; //静态申请设备号
unsigned int minor=0;//次设备号的起始值
dev_t devno; 		//动态申请设备号
struct class *cls;  //接收注册结构体的地址
struct device *dev; //设备号

gpio_t *vir_gpioe;
gpio_t *vir_gpiof;
unsigned int *vir_rcc;

int mycdev_open(struct inode *inode, struct file *file)
{
	unsigned int aaa = MINOR(inode->i_rdev);  	//得到次设备号aaa
	file->private_data = (void *)aaa;

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	unsigned int aaa = (int)file->private_data;    //次设备号
    // 功能控制
    switch (cmd)
    {
    case LED_ON:
        switch (aaa)
        {
        case 0:
            vir_gpioe->odr |= (0x1 << 10);
            break;
        }
        break;
    case LED_OFF:
        switch (aaa)
        {
        case 0:
            vir_gpioe->odr &= (~(0x1 << 10));
            break;
        }
        break;
    }
    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)   //寄存器地址映射和初始化
{
    int ret; 				//ret返回错误码 

	// 映射物理地址
    vir_gpioe = ioremap(GPIOE, sizeof(gpio_t));
    if (vir_gpioe == NULL)
    {
        printk("MODER寄存器映射失败\n");
        return -EFAULT;
    }

    vir_rcc = ioremap(RCC, 4);
    if (vir_rcc == NULL)
    {
        printk("RCC寄存器映射失败\n");
        return -EFAULT;
    }

    vir_gpiof = ioremap(GPIOF, sizeof(gpio_t));
    if (vir_gpiof == NULL)
    {
        printk("MODER寄存器映射失败\n");
        return -EFAULT;
    }
    printk("寄存器映射成功\n");

    //1.分配字符设备驱动对象空间  cdev_alloc
    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=register_chrdev_region(MKDEV(major,minor),1,"led0"); //设备号需要是组合出来的,次设备数量,设备文件名
        if(ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else//动态申请设备号
    {
        ret=alloc_chrdev_region(&devno,minor,1,"led0");   //动态申请设备号,次设备号,设备数量,文件名
         if(ret)
        {
            printk("动态申请设备号失败\n");
            goto out2;
        }
        major=MAJOR(devno); 	//根据设备号得到主设备号
        minor=MINOR(devno); 	//根据设备号得到次设备号
    }
    printk("申请设备号成功\n");

    //4.注册字符设备驱动对象  cdev_add()
    ret=cdev_add(cdev,MKDEV(major,minor),1); //字符设备,设备号,设备数量
    if(ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto out3;
    }
    printk("注册字符设备驱动对象成功\n");

    //5.向上提交目录
    cls=class_create(THIS_MODULE,"led0"); //指向自身的指针,文件名
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret=-PTR_ERR(cls);
        goto out4;
    }
    printk("向上提交目录成功\n");

    //6.向上提交设备节点
    dev=device_create(cls,NULL,MKDEV(major,0),NULL,"led0"); //创建设备节点
    if(IS_ERR(dev))
    {
        printk("向上提交节点信息失败\n");
        ret=-PTR_ERR(dev);
        goto out5;
    }
    printk("向上提交设备节点信息成功\n");

     (*vir_rcc) |= (0x3 << 4);
    // 寄存器初始化led1
    vir_gpioe->moder &= (~(0x3 << 20));
    vir_gpioe->moder |= (0x1 << 20);
    vir_gpioe->odr &= (~(0x1 << 10));
    printk("LED1硬件寄存器初始化成功\n");
    return 0;

out5:
    //销毁上面提交的设备信息
    device_destroy(cls,MKDEV(major,0));
    class_destroy(cls);
out4:
    cdev_del(cdev);
out3:
    unregister_chrdev_region(MKDEV(major,minor),1);
out2:
    kfree(cdev);
out1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    //1.销毁设备信息  device_destroy
    device_destroy(cls,MKDEV(major,0));
    //2.销毁目录  class_destroy
    class_destroy(cls);
    //3.注销对象  cdev_del()
    cdev_del(cdev);
    //4.释放设备号   unregister_chrdev_region()
    unregister_chrdev_region(MKDEV(major,minor),1);
    //5.释放对象空间  kfree()
    kfree(cdev);

	// 取消寄存器地址映射
    iounmap(vir_gpioe);
    iounmap(vir_rcc);
    iounmap(vir_gpiof);
    // 销毁设备节点信息
	device_destroy(cls, MKDEV(major, 0));
	// 销毁字符设备驱动
    class_destroy(cls);
    // 字符设备驱动的注销
    unregister_chrdev(major, "led0");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");


led1.c

#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/device.h>
#include<linux/cdev.h>
#include<linux/slab.h>
#include<linux/io.h>
#include "head.h"

struct cdev *cdev; 		//字符设备空间首地址
unsigned int major=500; //静态申请设备号
unsigned int minor=1;//次设备号的起始值
dev_t devno; 		//动态申请设备号
struct class *cls;  //接收注册结构体的地址
struct device *dev; //设备号

gpio_t *vir_gpioe;
gpio_t *vir_gpiof;
unsigned int *vir_rcc;

int mycdev_open(struct inode *inode, struct file *file)
{
	unsigned int aaa = MINOR(inode->i_rdev);  	//得到次设备号aaa
	file->private_data = (void *)aaa;

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	unsigned int aaa = (int)file->private_data;    //次设备号
    // 功能控制
    switch (cmd)
    {
    case LED_ON:
        switch (aaa)
        {
        case 1:
            vir_gpiof->odr |= (0x1 << 10);
            break;
        }
        break;
    case LED_OFF:
        switch (aaa)
        {
        case 1:
            vir_gpiof->odr &= (~(0x1 << 10));
            break;
        }
        break;
    }
    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)   //寄存器地址映射和初始化
{
    int ret; 				//ret返回错误码

	// 映射物理地址
    vir_gpioe = ioremap(GPIOE, sizeof(gpio_t));
    if (vir_gpioe == NULL)
    {
        printk("MODER寄存器映射失败\n");
        return -EFAULT;
    }

    vir_rcc = ioremap(RCC, 4);
    if (vir_rcc == NULL)
    {
        printk("RCC寄存器映射失败\n");
        return -EFAULT;
    }

    vir_gpiof = ioremap(GPIOF, sizeof(gpio_t));
    if (vir_gpiof == NULL)
    {
        printk("MODER寄存器映射失败\n");
        return -EFAULT;
    }
    printk("寄存器映射成功\n");

    //1.分配字符设备驱动对象空间  cdev_alloc
    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=register_chrdev_region(MKDEV(major,minor),1,"led1"); //设备号需要是组合出来的,次设备数量,设备文件名
        if(ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else//动态申请设备号
    {
        ret=alloc_chrdev_region(&devno,minor,1,"led1");   //动态申请设备号,次设备号,设备数量,文件名
         if(ret)
        {
            printk("动态申请设备号失败\n");
            goto out2;
        }
        major=MAJOR(devno); 	//根据设备号得到主设备号
        minor=MINOR(devno); 	//根据设备号得到次设备号
    }
    printk("申请设备号成功\n");

    //4.注册字符设备驱动对象  cdev_add()
    ret=cdev_add(cdev,MKDEV(major,minor),1); //字符设备,设备号,设备数量
    if(ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto out3;
    }
    printk("注册字符设备驱动对象成功\n");

    //5.向上提交目录
    cls=class_create(THIS_MODULE,"led1"); //指向自身的指针,文件名
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret=-PTR_ERR(cls);
        goto out4;
    }
    printk("向上提交目录成功\n");

    //6.向上提交设备节点
    dev=device_create(cls,NULL,MKDEV(major,1),NULL,"led1"); //创建设备节点
    if(IS_ERR(dev))
    {
        printk("向上提交节点信息失败\n");
        ret=-PTR_ERR(dev);
        goto out5;
    }
    printk("向上提交设备节点信息成功\n");

     (*vir_rcc) |= (0x3 << 4);
    // 寄存器初始化led1
    vir_gpiof->moder &= (~(0x3 << 20));
    vir_gpiof->moder |= (0x1 << 20);
    vir_gpiof->odr &= (~(0x1 << 10));
    printk("LED2硬件寄存器初始化成功\n");
    return 0;

out5:
    //销毁上面提交的设备信息
    device_destroy(cls,MKDEV(major,1));
    class_destroy(cls);
out4:
    cdev_del(cdev);
out3:
    unregister_chrdev_region(MKDEV(major,minor),1);
out2:
    kfree(cdev);
out1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    //1.销毁设备信息  device_destroy
    device_destroy(cls,MKDEV(major,1));
    //2.销毁目录  class_destroy
    class_destroy(cls);
    //3.注销对象  cdev_del()
    cdev_del(cdev);
    //4.释放设备号   unregister_chrdev_region()
    unregister_chrdev_region(MKDEV(major,minor),1);
    //5.释放对象空间  kfree()
    kfree(cdev);

	// 取消寄存器地址映射
    iounmap(vir_gpioe);
    iounmap(vir_rcc);
    iounmap(vir_gpiof);
    // 销毁设备节点信息
	device_destroy(cls, MKDEV(major, 1));
	// 销毁字符设备驱动
    class_destroy(cls);
    // 字符设备驱动的注销
    unregister_chrdev(major, "led1");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");


led2.c

#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/device.h>
#include<linux/cdev.h>
#include<linux/slab.h>
#include<linux/io.h>
#include "head.h"

struct cdev *cdev; 		//字符设备空间首地址
unsigned int major=500; //静态申请设备号
unsigned int minor=2;//次设备号的起始值
dev_t devno; 		//动态申请设备号
struct class *cls;  //接收注册结构体的地址
struct device *dev; //设备号

gpio_t *vir_gpioe;
gpio_t *vir_gpiof;
unsigned int *vir_rcc;

int mycdev_open(struct inode *inode, struct file *file)
{
	unsigned int aaa = MINOR(inode->i_rdev);  	//得到次设备号aaa
	file->private_data = (void *)aaa;

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	unsigned int aaa = (int)file->private_data;    //次设备号
    // 功能控制
    switch (cmd)
    {
    case LED_ON:
        switch (aaa)
        {
        case 2:
            vir_gpioe->odr |= (0x1 << 8);
            break;
        }
        break;
    case LED_OFF:
        switch (aaa)
        {
        case 2:
            vir_gpioe->odr &= (~(0x1 << 8));
            break;
        }
        break;
    }
    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)   //寄存器地址映射和初始化
{
    int ret; 				//ret返回错误码 

	// 映射物理地址
    vir_gpioe = ioremap(GPIOE, sizeof(gpio_t));
    if (vir_gpioe == NULL)
    {
        printk("MODER寄存器映射失败\n");
        return -EFAULT;
    }

    vir_rcc = ioremap(RCC, 4);
    if (vir_rcc == NULL)
    {
        printk("RCC寄存器映射失败\n");
        return -EFAULT;
    }

    vir_gpiof = ioremap(GPIOF, sizeof(gpio_t));
    if (vir_gpiof == NULL)
    {
        printk("MODER寄存器映射失败\n");
        return -EFAULT;
    }
    printk("寄存器映射成功\n");

    //1.分配字符设备驱动对象空间  cdev_alloc
    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=register_chrdev_region(MKDEV(major,minor),1,"led2"); //设备号需要是组合出来的,次设备数量,设备文件名
        if(ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else//动态申请设备号
    {
        ret=alloc_chrdev_region(&devno,minor,1,"led2");   //动态申请设备号,次设备号,设备数量,文件名
         if(ret)
        {
            printk("动态申请设备号失败\n");
            goto out2;
        }
        major=MAJOR(devno); 	//根据设备号得到主设备号
        minor=MINOR(devno); 	//根据设备号得到次设备号
    }
    printk("申请设备号成功\n");

    //4.注册字符设备驱动对象  cdev_add()
    ret=cdev_add(cdev,MKDEV(major,minor),1); //字符设备,设备号,设备数量
    if(ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto out3;
    }
    printk("注册字符设备驱动对象成功\n");

    //5.向上提交目录
    cls=class_create(THIS_MODULE,"led2"); //指向自身的指针,文件名
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret=-PTR_ERR(cls);
        goto out4;
    }
    printk("向上提交目录成功\n");

    //6.向上提交设备节点
    dev=device_create(cls,NULL,MKDEV(major,2),NULL,"led2"); //创建设备节点
    if(IS_ERR(dev))
    {
        printk("向上提交节点信息失败\n");
        ret=-PTR_ERR(dev);
        goto out5;
    }
    printk("向上提交设备节点信息成功\n");

     (*vir_rcc) |= (0x3 << 4);
    // 寄存器初始化led1
    vir_gpioe->moder &= (~(0x3 << 16));
    vir_gpioe->moder |= (0x1 << 16);
    vir_gpioe->odr &= (~(0x1 << 8));
    printk("LED1硬件寄存器初始化成功\n");
    return 0;

out5:
    //销毁上面提交的设备信息
    device_destroy(cls,MKDEV(major,2));
    class_destroy(cls);
out4:
    cdev_del(cdev);
out3:
    unregister_chrdev_region(MKDEV(major,minor),1);
out2:
    kfree(cdev);
out1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    //1.销毁设备信息  device_destroy
    device_destroy(cls,MKDEV(major,2));
    //2.销毁目录  class_destroy
    class_destroy(cls);
    //3.注销对象  cdev_del()
    cdev_del(cdev);
    //4.释放设备号   unregister_chrdev_region()
    unregister_chrdev_region(MKDEV(major,minor),1);
    //5.释放对象空间  kfree()
    kfree(cdev);

	// 取消寄存器地址映射
    iounmap(vir_gpioe);
    iounmap(vir_rcc);
    iounmap(vir_gpiof);
    // 销毁设备节点信息
	device_destroy(cls, MKDEV(major, 2));
	// 销毁字符设备驱动
    class_destroy(cls);
    // 字符设备驱动的注销
    unregister_chrdev(major, "led2");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");


实验结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Linux设备驱动程序,用于点亮LED。该程序使用了GPIO子系统和字符设备框架。 ```c #include <linux/kernel.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/gpio.h> #include <asm/uaccess.h> #define LED_PIN 17 // 在树莓派上,GPIO17对应的针脚可以用来控制LED static int led_value = 0; // LED的状态,0表示关闭,1表示打开 static dev_t dev_num; // 设备号 static struct class *dev_class; // 设备类别 static struct device *dev; // 设备 // 打开设备 static int led_open(struct inode *inode, struct file *file) { return 0; } // 从设备中读取数据 static ssize_t led_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { return 0; } // 向设备中写入数据 static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { char kbuf; // 从用户空间中读取一个字节的数据 if (copy_from_user(&kbuf, buf, 1)) return -EFAULT; // 根据读取的数据来控制LED if (kbuf == '0') { gpio_set_value(LED_PIN, 0); led_value = 0; } else if (kbuf == '1') { gpio_set_value(LED_PIN, 1); led_value = 1; } return 1; } // 关闭设备 static int led_release(struct inode *inode, struct file *file) { return 0; } // 设备操作函数 static struct file_operations led_fops = { .owner = THIS_MODULE, .open = led_open, .read = led_read, .write = led_write, .release = led_release, }; // 初始化设备驱动程序 static int __init led_init(void) { int ret; // 申请GPIO资源 ret = gpio_request(LED_PIN, "LED"); if (ret < 0) { printk(KERN_ERR "Failed to request GPIO %d: %d\n", LED_PIN, ret); return ret; } // 设置GPIO方向为输出 ret = gpio_direction_output(LED_PIN, 0); if (ret < 0) { printk(KERN_ERR "Failed to set GPIO %d direction: %d\n", LED_PIN, ret); gpio_free(LED_PIN); return ret; } // 注册字符设备驱动程序 ret = alloc_chrdev_region(&dev_num, 0, 1, "led"); if (ret < 0) { printk(KERN_ERR "Failed to allocate device number: %d\n", ret); gpio_free(LED_PIN); return ret; } // 创建设备类别 dev_class = class_create(THIS_MODULE, "led"); if (IS_ERR(dev_class)) { printk(KERN_ERR "Failed to create device class\n"); unregister_chrdev_region(dev_num, 1); gpio_free(LED_PIN); return PTR_ERR(dev_class); } // 创建设备 dev = device_create(dev_class, NULL, dev_num, NULL, "led"); if (IS_ERR(dev)) { printk(KERN_ERR "Failed to create device\n"); class_destroy(dev_class); unregister_chrdev_region(dev_num, 1); gpio_free(LED_PIN); return PTR_ERR(dev); } // 注册设备操作函数 cdev_init(&led_cdev, &led_fops); ret = cdev_add(&led_cdev, dev_num, 1); if (ret < 0) { printk(KERN_ERR "Failed to add device to kernel: %d\n", ret); device_destroy(dev_class, dev_num); class_destroy(dev_class); unregister_chrdev_region(dev_num, 1); gpio_free(LED_PIN); return ret; } printk(KERN_INFO "LED device driver initialized\n"); return 0; } // 卸载设备驱动程序 static void __exit led_exit(void) { // 删除字符设备 cdev_del(&led_cdev); // 销毁设备 device_destroy(dev_class, dev_num); // 销毁设备类别 class_destroy(dev_class); // 释放设备号 unregister_chrdev_region(dev_num, 1); // 释放GPIO资源 gpio_free(LED_PIN); printk(KERN_INFO "LED device driver unloaded\n"); } module_init(led_init); module_exit(led_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("LED device driver"); ``` 在上述代码中,我们使用`gpio_request()`函数来申请GPIO资源,并使用`gpio_direction_output()`函数将GPIO设置为输出模式。在`led_write()`函数中,我们根据用户空间中读取的数据来控制LED的状态。在`led_init()`函数中,我们先申请GPIO资源,然后创建字符设备,并将其注册到内核中。在`led_exit()`函数中,我们释放了GPIO资源,并删除了字符设备

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值