字符设备驱动

目录

demo.c

test.c

led.h

makefile

实验效果


demo.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"
//内核buf
char kbuf[100]="";
//地址映射
//定义RCC虚拟地址
unsigned int *rcc_vir = NULL;
//定义GPIOE虚拟地址
gpio_t *gpioe_vir = NULL;
//定义GPIOF虚拟地址
gpio_t *gpiof_vir = NULL;
//用户将用户空间的数据写到内核空间

#define LED1_ON (gpioe_vir->ODR|=(0x1<<10))
#define LED1_OFF (gpioe_vir->ODR &=(~(0x1<<10)))
#define LED2_ON (gpiof_vir->ODR|=(0x1<<10))
#define LED2_OFF (gpiof_vir->ODR &=(~(0x1<<10)))
#define LED3_ON (gpioe_vir->ODR|=(0x1<<8))
#define LED3_OFF (gpioe_vir->ODR &=(~(0x1<<8)))

//从用户空间读取数据
ssize_t led_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loffs){
    int res;
    printk("%s%s%d\n",__FILE__,__func__,__LINE__);
    //当用户空间写入的数据大小大于kbuf时,size大小为kbuf大小
    size=size>sizeof(kbuf)? sizeof(kbuf):size;
    //将用户空间的数据拷贝的内核空间
    res=copy_from_user(kbuf,ubuf,size);
    //成功返回0,失败返回为拷贝的字节数
    if(res){
        printk("copy from user error\n");
        return -EIO;
    }
    //LED1亮
    if(kbuf[0]==1){
        LED1_ON;
        LED2_OFF;
        LED3_OFF;
    }
    //LED2亮
    else if(kbuf[0]==2){
        LED1_OFF;
        LED2_ON;
        LED3_OFF;
    }
    //LED3亮
    else if(kbuf[0]==3){
        LED1_OFF;
        LED2_OFF;
        LED3_ON;
    }
    return size;
}
int led_open(struct inode *indoe, struct file *file){
    printk("%s%s%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
int led_close(struct inode *indoe, struct file *file){
    printk("%s%s%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
//主设备号
unsigned int major = 0;
//文件操作结构体
const struct file_operations fops = 
{
    .open=led_open,
    .write=led_write,
    .release=led_close,
};

//入口函数
static int __init demo_init(void){
    //注册字符设备驱动,成功返回主设备号,失败返回一个<0
    major=register_chrdev(0,"myled",&fops);
    if(major<0){
        return -EIO;
    }
    printk("major=%d\n",major);

    //ioremap成功返回映射首地址,失败返回NULL
    //rcc地址映射
    rcc_vir=ioremap(RCC_MP_AHB4ENSETR,4);
    if(rcc_vir==NULL){
        printk("ioremap rcc_vir error\n");
        return -EIO;
    }
    //gpioe地址映射
    gpioe_vir=ioremap(GPIO_E,sizeof(gpio_t));
    if(gpioe_vir==NULL){
        printk("ioremap gpioe_moder_vir error\n");
        return -EIO;
    }
    //gpiof地址映射
    gpiof_vir=ioremap(GPIO_F,sizeof(gpio_t));
    if(gpioe_vir==NULL){
        printk("ioremap gpioe_odr_vir error\n");
        return -EIO;
    }

    //led1的初始化PE10
    *rcc_vir |= (0x1<<4);
    gpioe_vir->MODER &= (~(0x3<<20));
    gpioe_vir->MODER |= (0x1<<20);
    gpioe_vir->ODR &= (~(0x1<<10));
    //led2的初始化PF10
    *rcc_vir |= (0x1<<5);
    gpiof_vir->MODER &= (~(0x3<<20));
    gpiof_vir->MODER |= (0x1<<20);
    gpiof_vir->ODR &= (~(0x1<<10));
    //led3的初始化PE8
    gpioe_vir->MODER &= (~(0x3<<16));
    gpioe_vir->MODER |= (0x1<<16);
    gpioe_vir->ODR &= (~(0x1<<8));
    return 0;
}

static void __exit demo_exit(void){
    //注销字符设备驱动
    unregister_chrdev(major,"myled");
    //
    iounmap(rcc_vir);
    iounmap(gpioe_vir);
    iounmap(gpiof_vir);
}

module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");

test.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc,const char * argv[])
{
    int fd=open("/dev/myled",O_RDWR);
    if(fd==-1){
        perror("open error");
        return -1;
    }
    char buf[128]="";
    while(1){
        buf[0]=1;
        write(fd,buf,sizeof(buf));
        sleep(1);
        buf[0]=2;
        write(fd,buf,sizeof(buf));
        sleep(1);
        buf[0]=3;
        write(fd,buf,sizeof(buf));
        sleep(1);
    }
    close(fd);
    return 0;
}

led.h

#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 RCC_MP_AHB4ENSETR 0x50000A28
#define GPIO_E 0x50006000
#define GPIO_F 0x50007000

#endif

makefile

#指定架构变量
ARCH ?=
ifeq ($(ARCH),arm)
#指定arm架构编译内核路径
KERNELDIR := /home/ubuntu/linux-5.10.61/
else
#指定x86架构编译内核路径
KERNELDIR :=/lib/modules/$(shell uname -r)/build
endif
#开启一个终端执行pwd指令
PWD := $(shell pwd)
all:
	@#-C跳到指定的目录下,读取内核源码目录下的顶层Makefile文件
	@#M指定源码所在路径,读取当前目录下的源码文件
	make -C $(KERNELDIR) M=$(PWD) modules 
clean:
	make -C $(KERNELDIR) M=$(PWD) clean
obj-m := demo.o

实验效果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值