基于框架编写驱动代码

应用层代码

#include <unistd.h>
#include <stdio.h>

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

       fd = open("/dev/pin4",O_RDWR);
       if(fd <0){

              printf("open failure\n");
              perror("who");
       }else{
              printf("open successful\n");
       }

       printf("input 1/0 \n");
       scanf("%d",&cmd);
       
       if(cmd ==1){
              data = 1;
       }
       if(cmd ==0){
              data = 0;
       }

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

       return 0;
}
}

驱动代码

#include <linux/fs.h>			//flie_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>		//copy_form_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 ssize_t pin4_open(struct inode *innode,struct file *file)
{
	printk("pin4_open\n");	//内核的打印函数类似printf

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

	return 0;
}	

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

	printk("pin4_write\n");

	//copy_from_user函数的作用是把用户空间的参数拷贝拷贝到内核空间
	
	copy_from_user(&userCmd,buf,count);
	printk("get vluel\n");

	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 ssize_t pin4_read(struct file *file, char __user *buf,size_t count,loff_t *ppos)
{
//	int userCmd;

	printk("pin4_read\n");
	//copy_to_user函数的作用是把内核空间的参数拷贝拷贝到用户空间
//	copy_to_user(buf,&userCmd,count);

	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)	//真实驱动入口
{
	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);	//创建设备文件

	//用函数ioremap将物理地址转换为虚拟地址,io口寄存器映射成普通内存单元访问
	GPFSEL0 = (volatile unsigned int*)ioremap(0x3f200000,4);	//初始化设置引脚功能的寄存器的地址
	GPSET0  = (volatile unsigned int*)ioremap(0x3f20001c,4);	// 初始化控制引脚输出‘1’的寄存器地址
	GPCLR0  = (volatile unsigned int*)ioremap(0x3f200028,4);	//初始化控制引脚输出‘0’的寄存器的地址

	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");

驱动代码中有涉及按位与(&)—按位或(|)

按位与(&):参加运算的两个数,换算为二进制(0、1)后,进行与运算。只有当 相应位上全部为1时取1, 存在0时为0。
011 & 110

011
110
---
010
按位或(|):参加运算的两个数,换算为二进制(0、1)后,进行或运算。只要当 相应位上存在1时取1, 全部为0时为0。
011 | 110

011
110
---
111

让编写的驱动生效

  • 打开内核源码树目录,在驱动目录的字符设备驱动目录中,将代码pin4driver.c拷贝到里面
cd SYSTEM/linux-rpi-4.14.y/drivers/char/

在这里插入图片描述

  • 编辑字符设备驱动目录中的Makefile
pl@ubuntu:~/SYSTEM/linux-rpi-4.14.y/drivers/char$ vi Makefile 

添加obj-m …o代码
在这里插入图片描述

  • 在内核源码树目录下使用交叉编译工具进行内核编译
    输入以下指令,生成pin4driver2.ko文件
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7  make -j4 modules
  • 将之前生成的pin4driver2.ko文件拷贝到树莓派的工作目录
scp ./drivers/char/pin4driver2.ko  pi@192.168.1.6:/home/pi
  • 将应用代码交叉编译后拷贝到树莓派的工作目录
arm-linux-gnueabihf-gcc pin4test.c -o pin4test
scp pin4test  pi@192.168.1.6:/home/pi

打开SecureCRT,连接树莓派

可以看到两个文件都已拷贝到工作目录
在这里插入图片描述

  • 输入以下代码,加载内核驱动
 sudo insmod pin4driver2.ko
Linux命令:
			lsmod——显示已载入系统的模块
			rmmod——用于从当前运行的内核中移除指定的内核模块

加载前
在这里插入图片描述
加载后,可以看到多了pin4driver2的设备
在这里插入图片描述

  • 为/dev/pin4添加权限,运行pin4test
sudo chmod 666 /dev/pin4
  • 运行后输入1,可看到4引脚为out,置为高电平1

在这里插入图片描述

  • 再次运行后输入0,可看到4引脚为out,置为低电平0
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值