#include驱动代码#include int main() { int fd; int cmd; int data; fd = open("/dev/pin4",O_RDWR); if(fd <0){ printf("open failuren"); perror("who"); }else{ printf("open successfuln"); } printf("input 1/0 n"); scanf("%d",&cmd); if(cmd ==1){ data = 1; } if(cmd ==0){ data = 0; } printf("data=%dn",data); write(fd,&data,1); return 0; } }
#include驱动代码中有涉及按位与(&)—按位或(|)//flie_operations声明 #include //module_init module_exit声明 #include //_init _exit宏定义声明 #include //class device声明 #include //copy_form_user的头文件 #include //设备号 dev_t 类型声明 #include //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_openn"); //内核的打印函数类似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_writen"); //copy_from_user函数的作用是把用户空间的参数拷贝拷贝到内核空间 copy_from_user(&userCmd,buf,count); printk("get vlueln"); if(userCmd == 1){ printk("set 1n"); *GPSET0 = 0x1 << 4; }else if(userCmd == 0){ printk("set 0n"); *GPCLR0 = 0x1 << 4; }else{ printk("undon"); } return 0; } static ssize_t pin4_read(struct file *file, char __user *buf,size_t count,loff_t *ppos) { // int userCmd; printk("pin4_readn"); //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



