驱动点亮LED小灯

本次实验字符设备驱动分步实现

/dev/led1    /dev/led2    /dev/led3        ------> 三个设备节点

---------------------------------------------------

                myled(驱动)                       --------->一个驱动

-------------------------------------------------

LED1         LED2         LED3            ----------->分别控制三盏灯

头文件:

#ifndef __MYLED_H__
#define __MYLED_H__

typedef struct {
	volatile unsigned int MODER;   // 0x00
	volatile unsigned int OTYPER;  // 0x04
	volatile unsigned int OSPEEDR; // 0x08
	volatile unsigned int PUPDR;   // 0x0C
	volatile unsigned int IDR;     // 0x10
	volatile unsigned int ODR;     // 0x14
	volatile unsigned int BSRR;    // 0x18
	volatile unsigned int LCKR;    // 0x1C 
	volatile unsigned int AFRL;    // 0x20 
	volatile unsigned int AFRH;    // 0x24
	volatile unsigned int BRR;     // 0x28
	volatile unsigned int res;
	volatile unsigned int SECCFGR; // 0x30

}gpio_t;

#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR  0X50000A28

enum LED
{
    LED1=1,
    LED2,
    LED3
};

#define LED_ON   _IO('a', 1)
#define LED_OFF  _IO('a', 0)
#define LedON    _IOW('a', 1, int)
#define LedOFF   _IOW('a', 0, int)
 
#endif

驱动文件:demo.c

编译指令:make arch=arm modname=demo


demo:你写的驱动的文件名,不要带‘.c’
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include "myled.h"

int minor = 0;//次设备号从0开始
#if 0
unsigned int major = 0;//动态申请
#else
unsigned int major = 500;//静态指定设备号
#endif
char kbuf[128] = {};//定义数组用于存放和用户之间拷贝的数据
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int * vir_rcc;
struct cdev *cdev;  //字符设备驱动结构体指针
struct class *cls;  //设备类结构体指针
struct device *dev; //保存设备基本信息结构体的指针
const int count=3;//指定设备节点的个数为3


//对应的是open()
int mycdev_open(struct inode *inode, struct file *file)
{
      printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
      return 0;
}

//read()
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    //size参数是用户期待读到的字节长度
    int ret;
    if(size > sizeof(kbuf))
    size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);
    if( ret )
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
     
       return size;
}
//write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if(size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("数据从用户向内核拷贝失败\n");
        return -EIO;
    }

    return size;
}
//close()
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret, which;
    switch (cmd)
    {
        case LedON:
            ret = copy_from_user(&which, (void*)arg, sizeof(int));
            if(ret)
            {
                printk("用户向内核xxx失败\n");
                return -EIO;
            }
            switch (which)
            {
                case LED1:
                    vir_led1->ODR |= (1<<10);
                    break;
                case LED2:
                    vir_led2->ODR |= (1<<10);
                    break;
                case LED3:
                    vir_led3->ODR |= (1<<8);
                    break;
            }
            break;
        case LedOFF:
            ret=copy_from_user(&which, (void *)arg, sizeof(int));
            if(ret)
            {
                printk("用户向内核拷贝数据失败\n");
                return -EIO;
            }
            switch(which)
            {
                case LED1:
                    vir_led1->ODR &= ~(1<<10);
                    break;
                case LED2:
                    vir_led2->ODR &= ~(1<<10);
                    break;
                case LED3:
                    vir_led3->ODR &= ~(1<<8);
                    break;
            }
            break;
        default:
            printk("功能码错误\n");
            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,
    .read = mycdev_read,
    .write = mycdev_write,
    .unlocked_ioctl = ioctl,
    .release = mycdev_close,
};


//函数入口
static int __init mycdev_init(void)
{
    int ret,i;
    //分配对象
    dev_t devno;
    cdev=cdev_alloc();
    if(cdev==NULL)
    {
        printk("cdev alloc memory err\n");
        
        ret = -ENOMEM;
        goto ERR1;
    }
    printk("对象分配成功\n");
    //对象的初始化
    cdev_init(cdev,&fops);
    //设备号的申请
 
    if(major==0)//动态申请
    {
        ret=alloc_chrdev_region(&devno,minor,count,"my_led");
        if(ret)
        {
            printk("动态申请设备号失败\n");
            goto ERR2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
        printk("动态申请设备号成功\n");
    }
    else
    {
        ret=register_chrdev_region(MKDEV(major,minor),count,"my_led");
        if(ret)
        {
            printk("静态申请设备号失败\n");
            goto ERR2;
        }
        printk("静态申请设备号成功\n");
    }
    //注册字符设备驱动
    ret=cdev_add(cdev,MKDEV(major,minor),count);
    if(ret)
    {
        printk("字符设备驱动注册失败\n");
        goto ERR3;
    }
    printk("注册字符设备驱动成功\n");
   
    //自动创建设备节点
    cls = class_create(THIS_MODULE,"led");
    if(IS_ERR(cls))
    {
        printk("创建逻辑节点目录失败\n");
        ret=PTR_ERR(cls);
        goto ERR4;
    }
    printk("创建逻辑节点目录成功\n");
    //向上提交节点信息
    for(i=0;i<3;i++)
    {
      dev = device_create(cls,NULL,MKDEV(major,i),NULL,"my_led%d",i);
        if(IS_ERR(dev))
        {
            printk("创建逻辑节点失败\n");
            ret = PTR_ERR(dev);
            goto ERR5;
        }
    }
    printk("创建逻辑节点成功\n");
    //寄存器的初始化
    all_led_init();
    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 ret;
}
static void __exit mycdev_exit(void)
{
    //1.销毁设备节点
    int i;
    for(i=0;i<count;i++)
    {
         device_destroy(cls,MKDEV(major,i));
    }
     class_destroy(cls);
    //2.注销字符设备驱动
    cdev_del(cdev);
    //3.释放设备号
    unregister_chrdev_region(MKDEV(major,minor),count);
    //4.释放动态申请的空间
    kfree(cdev);


}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

主函数:test.c     

编译指令(需要交叉编译工具链):arm-linux-gnueabihf-gcc test.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/ioctl.h>
#include"myled.h"

int main(int argc,char const *argv[])
{
    
    char buf[128]={};
    int fd;
    int which;
    printf("请输入想要控制的灯 1:LED1 2:LED2 3:LED3\n");
    scanf("%d",&which);
    getchar();
    switch (which)
    {
        case 1:
            fd=open("/dev/myled0",O_RDWR);
            break;
        case 2:
            fd=open("/dev/myled1",O_RDWR);
            break;
        case 3:
            fd=open("/dev/myled2",O_RDWR);
            break;
        default:
            printf("输入有误,程序退出\n");
            exit(-1);
    }
    if(fd<0)
    {
        printf("打开文件失败\n");
        exit(-1);
    }
    printf("打开文件成功\n");
    //在终端中输入
    while(1)
    {
        printf("请输入控制命令 1:开灯 0:关灯 9:退出程序\n");
        fgets(buf,sizeof(buf),stdin);
        if(buf[0]=='9')
        {
            break;
        }
        else if(buf[0]=='1')//对LED灯进行操作
            ioctl(fd,LedON,&which);
        else if(buf[0]=='0') 
            ioctl(fd,LedOFF,&which);
    }
    close(fd);
    return 0;
}

Makefile

modname ?= demo
arch ?= arm

KBUILD_EXTRA_SYMBOLS+=/home/ubuntu/lk/驱动开发/day2/1/Module.symvers

ifeq ($(arch),arm)#arm架构
KERNELDIR := /home/ubuntu/fsmp1a/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61
else
KERNELDIR:= /lib/modules/$(shell uname -r)/build #当前x86架构格式路径
#KERNELDIR是一个变量,指向内核源码目录
endif
PWD:=$(shell pwd)
#PWD指向当前驱动目录的一个变量


all:
	make -C $(KERNELDIR) M=$(PWD) modules
#make -C $(KERNELDIR)
#进入内核顶层目录下,读取对应的Makefile文件,然后执行make
#M=$(PWD)  		:指定边沿模块的路径为当前驱动路径
#make modules	:模块化编译
#进入内核顶层目录使用其中的Makefile对当前文件进行模块化编译
clean:
	make -C $(KERNELDIR) M=$(PWD) clean
#清除编译

obj-m:=$(modname).o
#指定当前编译生成的模块名字为demo  demo.c ==> demo.ko

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老K殿下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值