要编写对io口进行操控,我们首先需要去阅读芯片手册,这边使用的是树莓派 3B所以查看的手册是BCM2835,查看cpu型号可以用这个指令来查看:
cat /proc/cpuinfo
IO口介绍(只介绍这边使用到的)
我们可以通过gpio readall
在树莓查看我们的bcm引脚所对应的io口是哪个
输出配置:
清零配置:
现在就向大家介绍一下简单的代码示例:
底层代码pin4driver.c
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <asm/io.h>
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的作用是作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值。
volatile unsigned int *GPSET0 = NULL;
volatile unsigned int *GPCLR0 = NULL;
//led open 函数
static int pin4_open(struct inode *inode,struct file *file)
{
printk("pin4_open\n");
*GPFSEL0 &= ~(0x6 << 12);//0x6用二进制表示是110 想左移12位在取反就可以保证 14-12位为001在用&(与运算)运算我们就可以保证 14,13位一定为0
*GPFSEL0 |= (0x1 <<12);//0x1 用二进制表示是1 想左移12位在用|(或运算)就可以保证第12为一定为1
return 0;
}
static int pin4_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
{
int userCmd;
printk("pin4_write\n");
copy_form_user(&userCmd,buf,size);//这个函数是用于接收上层代码的,在内核源码中可以找到这个函数用法
if (userCmd==1){
printk("set 1\n");
*GPSET0 |= (0x1 << 4);//0x1 左移四位就可以将第4引脚置1并且不影响其他位,并且将pin4设置为高电平
}else if(userCmd==0){
printk("set 0\n");
*GPCLR0 |= (0x1 << 4);//原理上,但这边设置的是低电平
}else{
printk("undo\n");
}
return 0;
}
static int pin4_read(struct file *file1, char __user *buf, size_t size, loff_t *ppos)
{
printk("pin4_read\n");
return 0;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = pin4_open,
.write = pin4_write,
.read = pin4_read,
};
int __init pin4_drv_init(void)
{
int ret;
printk("pin4 insmod success\n");
devno = MKDEV(major,minor);
ret = register_chrdev(major,module_name, &led_fops);//注册驱动 告诉内核
pin4_class = class_create( THIS_MODULE, "myfirstdemo");
pin4_class_dev = device_create(pin4_class,NULL,devno,NULL,module_name);
GPFSEL0 = (volatile unsigned int *)ioremap(0x3f200000,4);//ioremap():物理地址转换成虚拟地址,io口寄存器映射成普通内存单元进行访问,0x3f200000 GPFSL0的物理地址,在下面为大家介绍为什么这个
//第二个参数表示映射的大小
GPSET0 = (volatile unsigned int *)ioremap(0x3f20001c,4);//物理地址的映射要在创建之后
GPCLR0 = (volatile unsigned int *)ioremap(0x3f200028,4);
return 0;
}
void __exit pin4_drv_exit(void)
{
iounmap(GPFSEL0 );//取消ioremap所映射的IO地址
iounmap(GPSET0 );//取消隐射要在摧毁前
iounmap(GPSET0);
device_destroy(pin4_class,devno);
class_destroy(pin4_class);
unregister_chrdev(major,module_name);//卸载驱动
printk("pin4 destroyed\n");
}
module_init(pin4_drv_init);
module_exit(pin4_drv_exit);
MODULE_LICENSE("GPL v2");
GPFSEL0 = (volatile unsigned int *)ioremap(0x3f200000,4)
为什么GPFSEL0的物理地址是这个0x3f200000:我们在编写驱动程序的时候,IO空间的起始地址是0x3f000000,加上GPIO的偏移量0x2000000,所以GPIO的物理地址应该是从0x3f200000开始的,然后在这个基础上进行Linux系统的MMU内存虚拟化管理,映射到虚拟地址上。
这边要注意一个偏移值,偏移值可以在文档中直接看到,比如GPSET0:
这边来自这篇博文的介绍:树莓派LED驱动编写
上层代码介绍:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int fd;
int cmd;
fd = open("/dev/pin4",O_RDWR);
if(fd<0){
printf("open pin4 failure\n");
perror("reason: ");
}else{
printf("open pin4 success \n");
}
printf("please input cmd 1/0\n1--set pin4 high\n0--set pin4 low\n");
scanf("%d",&cmd);
if(cmd == 1){
printf("cmd = &d\n",cmd)
}else if(cmd ==0){
printf("cmd = &d\n",cmd)
}
write(fd,&cmd,1);
return 0;
}
结果示例(如何编译代码和测试代码可以参考这篇文章——linux内核第一次编程)
这边有一篇博文写的比我更加详细,感兴趣的小伙伴可以去浏览学习:详细到吐血 —— 树莓派驱动开发入门:从读懂框架到自己写驱动