系统相关
首页 > 系统相关> > Linux 驱动 操控IO端 编写代码

Linux 驱动 操控IO端 编写代码

作者:互联网

pin4driver.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 device声明
#include <linux/uaccess.h>      //class
#include <linux/types.h>
#include <asm/io.h>                     //ioreamap.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";//模块名

//整数11
//0xb  11 00010001

//对应 真实驱动入口
volatile unsigned int* GPFSEL0 = NULL;//volatile (unsigned int *) 0x3f200000;
volatile unsigned int* GPSET0  = NULL;//volatile (unsigned int *) 0x3f20001C;
volatile unsigned int* GPCLR0  = NULL;//volatile (unsigned int *) 0x3f200028;

//led_read函数
static int pin4_read (struct file *file1, char __user *buf, size_t size,loff_t *ppos)
{

        printk("pin4_read\n");//内核打印函数


        return 0;
}

//led_open函数
static int pin4_open(struct inode *inode,struct file *file)
{

        printk("pin4_open\n");
        //配置pin4 引脚为输出引脚               bit 12 - 14配置成001
        *GPFSEL0 &= ~(0x6 << 12);  //按位与 把bit 14 13 配置成 0
        *GPFSEL0 |=  (0x1 <<12);

        return 0;
}
//led_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口  高电平 或者低电平
                //printk("get value\n");
        if(userCmd == 1){

                printk("set 1\n");
                *GPSET0 |= 0x1 << 4;

        }else if(userCmd == 0){

                printk("set 0\n");
                *GPCLR0 |= 0x1 <<4;

        }else{

                printk("dont do\n");

        }
        return 0;
}
static struct file_operations pin4_fops = {

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

};
int __init pin4_drv_init(void)  //1.真实驱动入口
{
        int ret;
        printk("insmod driver pin4 success\n");

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

        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);//将物理地址转换为虚拟地址,把io口寄存器映射成普通内存单元进行访问
        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);  // 卸载驱动

}

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("reson:");

        }else{
                printf("open success\n");
        }
        printf("input commnd : 1/0  \n 1:set pin4 high\n 0:set pin4 low\n");
        scanf("%d",&cmd);

        if(cmd == 1){
                data = 1;
        }

        if(cmd == 0){
                data = 0;
        }

        printf("data = %d\n",data);
        fd = write(fd,&data,1);

}

 

 

按照这个流程

 

去观察 gpio readall 的引脚状态

这个人写的也还不错https://blog.csdn.net/weixin_49817112/article/details/108849996?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.control

标签:操控,int,unsigned,static,IO,Linux,volatile,include,pin4
来源: https://blog.csdn.net/weixin_46016743/article/details/113195643