0
点赞
收藏
分享

微信扫一扫

实现驱动开发

瑾谋 2022-02-04 阅读 47
//pin4driver2.c(底层代码)
#include <linux/fs.h>            //file_operations声明
#include <linux/module.h>    //module_init  module_exit声明
#include <linux/init.h>      //__init  __exit 宏定义声明
#include <linux/device.h>        //class  devise声明
#include <linux/uaccess.h>   //copy_from_user 的头文件
#include <linux/types.h>     //设备号  dev_t 类型声明
#include <asm/io.h>          //ioremap iounmap的头文件


static struct class *pin4_class;
static struct device *pin4_class_dev;

static dev_t devno;                //设备号
static int major =231;                     //主设备号
static int minor =0;                       //次设备号
static char *module_name="pin4";   //模块名

volatile unsigned int *GPFSEL0 = NULL;
volatile unsigned int *GPSET0  = NULL;
volatile unsigned int *GPCLR0  = NULL;


static int pin4_read(struct file *file,char __user *buf,size_t count,loff_t *ppos)
{
    printk("pin4_read\n");

    return 0;
}



//pin4_open函数
static int pin4_open(struct inode *inode,struct file *file)
{
    printk("pin4_open\n");  //内核的打印函数和printf类似

    //配置pin4引脚为输出引脚,bit 12~14 配置成001
    *GPFSEL0 &= ~(0x6 << 12); //把 bit14,bit13 配置成0
    *GPFSEL0 |= (0x1 << 12); //把 bit12 配置成1

    return 0;
}

//pin4_write函数
static ssize_t pin4_write(struct file *file,const char __user *buf,size_t count, loff_t *ppos)
{
    int userCmd;

    printk("pin4_write\n");
    //获取上层write函数的值
    copy_from_user(&userCmd,buf,count);
    //根据值来操作io口,高电平或者低电平
    
    if(userCmd == 1){
        printk("set 1\n");
        *GPSET0 |= (0x1 << 4);
    }else if(userCmd == 0){
        printk("set 0\n");
        *GPCLR0 |= (0x1 << 4);
    }else{
        printk("undo\n");
    }

    return 0;
}

static struct file_operations pin4_fops = {   //在keil的编译环境下不允许这样写,在linux下可以这样写

    .owner = THIS_MODULE,
    .open  = pin4_open,
    .write = pin4_write,
    .read  = pin4_read,

};

int __init pin4_drv_init(void)  //真实驱动入口
{

    int ret;
    devno = MKDEV(major,minor);  //创建设备号
    ret   = register_chrdev(major, module_name,&pin4_fops);  //注册驱动  告诉内核,把这个驱动加入到内核驱动的链表中

    pin4_class=class_create(THIS_MODULE,"myfirstdemo");      //让代码在dev生成设备
    pin4_class_dev =device_create(pin4_class,NULL,devno,NULL,module_name);  //创建设备文件

    GPFSEL0 = (volatile unsigned int *)ioremap(0x3f200000,4);//物理地址转化为虚拟地址,4:4个字节
    GPSET0  = (volatile unsigned int *)ioremap(0x3f20001c,4);
    GPCLR0  = (volatile unsigned int *)ioremap(0x3f200028,4);

    return 0;
}

void __exit pin4_drv_exit(void)
{
    iounmap(GPFSEL0);//对地址映射解除绑定
    iounmap(GPSET0);
    iounmap(GPCLR0);


    device_destroy(pin4_class,devno);
    class_destroy(pin4_class);
    unregister_chrdev(major, module_name);  //卸载驱动

}

module_init(pin4_drv_init);  //入口,内核加载该驱动的时候,这个宏会被调用
module_exit(pin4_drv_exit);
MODULE_LICENSE("GPL v2");
//pin4test.c(上层代码)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{
        int fd;
        int cmd;
        int data;

        fd = open("/dev/pin4",O_RDWR);
        if(fd < 0){
                printf("open failed\n");
                perror("reason:");
        }else{
                printf("open success\n");
        }
        printf("input commend : 1/0 \n1:set pin4 high\n0:set pin4 low\n");
        scanf("%d",&cmd);

        if(cmd == 1){
                data = 1;
        }
        if(cmd == 0){
                data = 0;
        }
        printf("data = %d\n",data);//这句没写上层会出现小bug
        fd = write(fd,&data,1);

        return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

举报

相关推荐

0 条评论