华清11.19

.要求:
    1)分部实现注册字符设备驱动
    2)自动创建设备节点
    3)通过结构体对led灯地址进行映射
    4)次设备号完成私有数据传参
mydev.c

#include <linux/init.h>
#include <linux/module.h>
#include<linux/device.h>
#include<linux/slab.h>
#include<linux/cdev.h>
#include<linux/fs.h>
#include<linux/io.h>
#include"led.h"
#define CNAME "myled"
struct cdev* cdev;
struct class* cls;
struct device* dev;
volatile int* RCC=NULL;
volatile gpio_t* GPIOE=NULL;
volatile gpio_t* GPIOF=NULL;
#if 1
unsigned int major =0;
#else
unsigned int major = 500;
#endif 
unsigned int minor=0;
dev_t devno;
const int count =3;
int mydev_open(struct inode *inode, struct file *file)
{   
    int node;
    node=MINOR(inode->i_rdev);
    file->private_data=(void*)node;
    return 0;
}
ssize_t mydev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
  
    return 0;
}
ssize_t mydev_write (struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int node;
    int ret;
    char c;
    node=(int)file->private_data;
    ret=copy_from_user(&c,ubuf,sizeof(c));
    if(ret)
    {
        printk("copy from user error\n");
        return -EIO;
    }
    switch(node)
    {
        case LED1:
        if(c=='1')
        {
            GPIOE->ODR|=(0x1<<10);
        }
        else if(c=='0')
        {
            GPIOE->ODR&=(~(0x1<<10));
        }
        break;
        case LED2:
        if(c=='1')
        {
            GPIOF->ODR|=(0x1<<10);
        }
        else if(c=='0')
        {
            GPIOF->ODR&=(~(0x1<<10));
        }
        break;
        case LED3:
        if(c=='1')
        {
            GPIOE->ODR|=(0x1<<8);
        }
        else if(c=='0')
        {
            GPIOE->ODR&=(~(0x1<<8));
        }
        break;
    }
    
    return 0;
}

int  mydev_close (struct inode *inode, struct file *file)
{
    printk("close\n");
    return 0;
}
const struct file_operations fops = {
    .open=mydev_open,
    .read=mydev_read,
    .write=mydev_write,
    .release=mydev_close,
};

//入口
static int __init mycdev_init(void)
{
    int ret;
    int i;
    //1.分配cdev结构体
    cdev=cdev_alloc();
    if(NULL==cdev)
    {
        printk("cdev error\n");
        ret= -EIO;
        goto ERR1;
    }
    //2.初始化结构体
    cdev_init(cdev,&fops);
    //3.申请设备号
    if(major==0)
    {
        ret=alloc_chrdev_region(&devno,0,count,CNAME);
        if(ret)
        {
            printk("alloc chrdev region error\n");
            ret=-ENOMEM;
            goto ERR2;
        }
        major=MAJOR(devno);
        minor=MINOR(devno);
    }
    else{
        ret=register_chrdev_region(MKDEV(major,minor),count,CNAME);
        if(ret)
        {
            printk("register chrdev region error\n");
            ret=-ENOMEM;
            goto ERR2;
        }
    }
    //4.驱动的注册
    ret=cdev_add(cdev,MKDEV(major,minor),count);
    if(ret)
    {
        printk("cdev add error\n");
        ret=-EIO;
        goto ERR3;
    }
    //5.自动创建设备节点
    cls=class_create(THIS_MODULE,CNAME);
    if(IS_ERR(cls))
    {
        printk("class create error \n");
        ret=PTR_ERR(cls);
        goto ERR4;
    }
    for(i=0;i<count;i++)
    {
        dev=device_create(cls,NULL,MKDEV(major,i),NULL,"led%d",i);
        if(IS_ERR(dev))
        {
            printk("device create error\n");
            ret=-EIO;
            goto ERR5;
        }
    }
    RCC=ioremap(WL_RCC,4);
    if(NULL==RCC)
    {
        printk("rcc ioremap error\n");
        return ENOMEM;
    }
    //GPIOE映射
    GPIOE=ioremap(WL_GPIOE,sizeof(gpio_t));
    if(NULL==GPIOE)
    {
        printk("gpioE ioremap error\n");
        return ENOMEM;
    }
    //GPIOF映射
    GPIOF=ioremap(WL_GPIOF,sizeof(gpio_t));
    if(NULL==GPIOF)
    {
        printk("gpioF ioremap error\n");
        return ENOMEM;
    }
    //初始化灯
    *RCC|=(0x1<<4);
    *RCC|=(0x1<<5);
    GPIOE->MODER&=(~(0x3<<20));
    GPIOE->MODER|=(0x1<<20);
    GPIOF->MODER&=(~(0x3<<20));
    GPIOF->MODER|=(0x1<<20);
    GPIOE->MODER&=(~(0x3<<16));
    GPIOE->MODER|=(0x1<<16);
   
    return 0;
    ERR5:
        for(--i;i>=0;i--)
        {
            device_destroy(cls,MKDEV(major,i));
        }
        class_destroy(cls);
    ERR4:
        cdev_del(cdev);
    ERR3:
        unregister_chrdev_region(MKDEV(major,minor),count);
    ERR2:
        kfree(cdev);
    ERR1:
        return -EIO;;
}
//出口
static void __exit mycdev_exit(void)
{
    int i;
    iounmap(GPIOE);
    iounmap(GPIOF); 
    iounmap(RCC);
    //1。销毁设备节点信息
    for(i=0;i<count;i++)
        {
            device_destroy(cls,MKDEV(major,i));
        }
    
    //2.销毁目录信息
    class_destroy(cls);
    //3.驱动的注销
    cdev_del(cdev);
    //4.销毁设备号
    unregister_chrdev_region(MKDEV(major,minor),count);
    //5.释放cdev结构体
    kfree(cdev);    
}
//指定入口地址
module_init(mycdev_init);
module_exit(mycdev_exit);
//指定出口地址
//许可证
MODULE_LICENSE("GPL");

 led.h

#ifndef __LED_H__
#define __LED_H__
typedef struct 
{
    volatile unsigned int MODER;
    volatile unsigned int OTYPER;  
    volatile unsigned int OSPEEDR;
    volatile unsigned int PUPDR;
    volatile unsigned int IDR;
    volatile unsigned int ODR;
}gpio_t;

#define WL_GPIOE 0x50006000
#define WL_GPIOF 0x50007000
#define WL_RCC   0x50000a28

typedef enum
{
    LED1,
    LED2,
    LED3
}led_t;

#endif

 Makefile

ARCH?=x86
NAME?=mydev
ifeq ($(ARCH),x86)
	KERNEDIR:=/lib/modules/$(shell uname -r)/build
else
	KERNEDIR:=/home/ubuntu/linux-5.10.61
endif
PWD:=$(shell pwd)
all:
	make -C $(KERNEDIR) M=$(PWD) modules
clean:
	make -C $(KERNEDIR) M=$(PWD) clean
obj-m:=$(NAME).o

结果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值