驱动 作业 day4

编写LED灯的驱动,创建三个设备文件,每个设备文件和一个LED灯绑定,当操作这个设备文件时只能控制设备文件对应的这盏灯。
驱动 作业 day4_第1张图片
此时没有安装led2 和led3的驱动所以会打开设备文件失败
驱动 作业 day4_第2张图片
装完以后就可以正常控制了
以下是设备现象
驱动 作业 day4_第3张图片
驱动 作业 day4_第4张图片
head.h

ubuntu@ubuntu:led$ vi test.c 
ubuntu@ubuntu:led$ cat 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 LED1 _IOW('a',1,int) 
#define LED2 _IOW('b',1,int)
#define LED3 _IOW('c',1,int)
#endif

test.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"
int main(int argc, char const *argv[])
{
	int a,b,c,fd1,fd2,fd3;
	while(1)
	{
		printf("请输入两个字符\n");
		printf("请选择要操作的灯1(LED1) 2(LED2) 3(LED3) 4(EXIT)\n");
		scanf("%d",&a);
		getchar();
		if(a == 4)
			return 0;
		switch(a)
		{
		case 1:
			fd1 = open("/dev/myled1", O_RDWR);
			if (fd1 < 0)
			{
				printf("打开设备文件失败\n");
				exit(-1);
			}  
			break;
		case 2:
			fd2 = open("/dev/myled2", O_RDWR); 
			if (fd2 < 0)
			{
				printf("打开设备文件失败\n");
				exit(-1);
			}  
			break;
		case 3:
			fd3 = open("/dev/myled3", O_RDWR); 
			if (fd3 < 0)
			{
				printf("打开设备文件失败\n");
				exit(-1);
			}
			break;
		}
		//从终端读取
		printf("第二个字符:0(关灯) 1(开灯)\n");
		scanf("%d",&b);
		getchar();
		switch(a)
		{
		case 1:
			ioctl(fd1,LED1,b);
			break;
		case 2:
			ioctl(fd2,LED2,b);
			break;
		case 3:
			ioctl(fd3,LED3,b);
			break;
		}
	}
    close(fd1);
    close(fd2);
    close(fd3);
    return 0;
}

led1.c

#include 
#include 
#include
#include
#include
#include
#include 
#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_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
	unsigned int aaa=MINOR(inode->i_rdev);
	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 (aaa)
	{
	case 0: // LED1
		if(arg == 1)
		vir_led1->ODR |= (0x1 << 10);
		else
		vir_led1->ODR &= (~(0x1 << 10));
		break;
	case 1: // LED2
		if(arg == 1)
		vir_led2->ODR |= (0x1 << 10);
		else
		vir_led2->ODR &= (~(0x1 << 10));
		break;
	case 2: // LED3
		if(arg == 1)
		vir_led3->ODR |= (0x1 << 8);
		vir_led3->ODR &= (~(0x1 << 8));
		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;
}
int all_led_init(void)
{
    //寄存器地址的映射
    vir_led1=ioremap(PHY_LED1_ADDR,sizeof(gpio_t));
    if(vir_led1==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
    vir_rcc=ioremap(PHY_RCC_ADDR,4);
    if(vir_rcc==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //寄存器的初始化
    //rcc
    (*vir_rcc) |= (3<<4);
    //led1
    vir_led1->MODER &= (~(3<<20));
    vir_led1->MODER |= (1<<20);
    vir_led1->ODR &= (~(1<<10));

    printk("寄存器初始化成功\n");

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

    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};
static int __init mycdev_init(void)
{
    int ret;
    //1.分配字符设备驱动对象空间  cdev_alloc
    cdev = cdev_alloc();
    if (!cdev) {
        printk(KERN_ALERT "cdev_alloc failed\n");
        return -ENOMEM;
    }
    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,"myled1");
        if(ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else//动态申请设备号
    {
        ret=alloc_chrdev_region(&devno,minor,1,"myled1");
         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,"myled1");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret=-PTR_ERR(cls);
        goto out4;
	}
	printk("向上提交目录成功\n");
	//6.向上提交设备节点
	dev=device_create(cls,NULL,MKDEV(major,minor),NULL,"myled%d",minor+1);
	if(IS_ERR(dev))
	{
		printk("向上提交节点信息失败\n");
		ret=-PTR_ERR(dev);
		goto out5;
	}
	printk("向上提交设备节点信息成功\n");
	//寄存器映射以及初始化
    all_led_init();
    return 0;
out5:
	//销毁上面提交的设备信息
	device_destroy(cls,MKDEV(major,minor));
	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,minor));
    //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);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

led2.c

#include 
#include 
#include
#include
#include
#include
#include 
#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_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
	unsigned int aaa=MINOR(inode->i_rdev);
	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 (aaa)
	{
	case 0: // LED1
		if(arg == 1)
		vir_led1->ODR |= (0x1 << 10);
		else
		vir_led1->ODR &= (~(0x1 << 10));
		break;
	case 1: // LED2
		if(arg == 1)
		vir_led2->ODR |= (0x1 << 10);
		else
		vir_led2->ODR &= (~(0x1 << 10));
		break;
	case 2: // LED3
		if(arg == 1)
		vir_led3->ODR |= (0x1 << 8);
		vir_led3->ODR &= (~(0x1 << 8));
		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;
}
int all_led_init(void)
{
    //寄存器地址的映射
    vir_led2=ioremap(PHY_LED2_ADDR,sizeof(gpio_t));
    if(vir_led2==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
    vir_rcc=ioremap(PHY_RCC_ADDR,4);
    if(vir_rcc==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //寄存器的初始化
    //rcc
    (*vir_rcc) |= (3<<4);
    //led2
    vir_led2->MODER &= (~(3<<20));
    vir_led2->MODER |= (1<<20);
    vir_led2->ODR &= (~(1<<10));
    printk("寄存器初始化成功\n");

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

    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};
static int __init mycdev_init(void)
{
    int ret;
    //1.分配字符设备驱动对象空间  cdev_alloc
    cdev = cdev_alloc();
    if (!cdev) {
        printk(KERN_ALERT "cdev_alloc failed\n");
        return -ENOMEM;
    }
    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,"myled2");
        if(ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else//动态申请设备号
    {
        ret=alloc_chrdev_region(&devno,minor,1,"myled2");
         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,"myled2");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret=-PTR_ERR(cls);
        goto out4;
	}
	printk("向上提交目录成功\n");
	//6.向上提交设备节点
	dev=device_create(cls,NULL,MKDEV(major,minor),NULL,"myled%d",minor+1);
	if(IS_ERR(dev))
	{
		printk("向上提交节点信息失败\n");
		ret=-PTR_ERR(dev);
		goto out5;
	}
	printk("向上提交设备节点信息成功\n");
	//寄存器映射以及初始化
    all_led_init();
    return 0;
out5:
	//销毁上面提交的设备信息
	device_destroy(cls,MKDEV(major,minor));
	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,minor));
    //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);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

led3.c

#include 
#include 
#include
#include
#include
#include
#include 
#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_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
	unsigned int aaa=MINOR(inode->i_rdev);
	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 (aaa)
	{
	case 0: // LED1
		if(arg == 1)
		vir_led1->ODR |= (0x1 << 10);
		else
		vir_led1->ODR &= (~(0x1 << 10));
		break;
	case 1: // LED2
		if(arg == 1)
		vir_led2->ODR |= (0x1 << 10);
		else
		vir_led2->ODR &= (~(0x1 << 10));
		break;
	case 2: // LED3
		if(arg == 1)
		vir_led3->ODR |= (0x1 << 8);
		else
		vir_led3->ODR &= (~(0x1 << 8));
		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;
}
int all_led_init(void)
{
    //寄存器地址的映射
    vir_led3=ioremap(PHY_LED3_ADDR,sizeof(gpio_t));
    if(vir_led3==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
    vir_rcc=ioremap(PHY_RCC_ADDR,4);
    if(vir_rcc==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //寄存器的初始化
    //rcc
    (*vir_rcc) |= (3<<4);
    //led3
    vir_led3->MODER &= (~(3<<16));
    vir_led3->MODER |= (1<<16);
    vir_led3->ODR &= (~(1<<8));
    printk("寄存器初始化成功\n");

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

    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};
static int __init mycdev_init(void)
{
    int ret;
    //1.分配字符设备驱动对象空间  cdev_alloc
    cdev = cdev_alloc();
    if (!cdev) {
        printk(KERN_ALERT "cdev_alloc failed\n");
        return -ENOMEM;
    }
    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,"myled3");
        if(ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else//动态申请设备号
    {
        ret=alloc_chrdev_region(&devno,minor,1,"myled3");
         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,"myled3");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret=-PTR_ERR(cls);
        goto out4;
	}
	printk("向上提交目录成功\n");
	//6.向上提交设备节点
	dev=device_create(cls,NULL,MKDEV(major,minor),NULL,"myled%d",minor+1);
	if(IS_ERR(dev))
	{
		printk("向上提交节点信息失败\n");
		ret=-PTR_ERR(dev);
		goto out5;
	}
	printk("向上提交设备节点信息成功\n");
	//寄存器映射以及初始化
    all_led_init();
    return 0;
out5:
	//销毁上面提交的设备信息
	device_destroy(cls,MKDEV(major,minor));
	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,minor));
    //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);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

Makefile

arch?=arm
modname?=demo
#指定内核顶层目录的路径
ifeq ($(arch),arm)
	KERNELDIR:=/home/ubuntu/FSMP1A/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61  #编译为ARM架构的内核路径
else
	KERNELDIR:=/lib/modules/$(shell uname -r)/build   #编译生成x86架构文件的内核路径
endif
#指定当前源码所在的路径
PWD:=$(shell pwd)  #将shell命令pwd的执行结果赋值给变量PWD
#指定使用的符号表文件的路径
KBUILD_EXTRA_SYMBOLS +=/home/ubuntu/23031_driver/day2/1/Module.symvers
all:
	#make modules表示进行模块化编译
	#make -C $(KERNELDIR)先切换路径到KERNELDIR下,按照这个路径下Makefile的规则进行make
	#M=$(PWD)指定模块化编译的路径
	make -C $(KERNELDIR) M=$(PWD) modules
clean:
	#编译清除
	make -C $(KERNELDIR) M=$(PWD) clean
	#将指定的.o文件独立链接为模块文件
obj-m:=$(modname).o

你可能感兴趣的:(c语言,汇编,开发语言,vim,后端)