博客
关于我
树莓派博通BCM2835芯片的IO口驱动代码调试和测试
阅读量:329 次
发布时间:2019-03-04

本文共 3582 字,大约阅读时间需要 11 分钟。

树莓派GPIO配置详解

树莓派作为一个功能强大的开发板,拥有丰富的GPIO接口,可供开发者进行各种输入输出操作。本文将详细介绍树莓派的GPIO寄存器及其配置方法,帮助开发者充分利用GPIO功能。

1. 树莓派寄存器介绍

树莓派的GPIO模块由多个寄存器组成,每个寄存器管理多个引脚的输入输出功能。树莓派共有54个引脚,分布在六个不同的功能寄存器中:

  • GPFEL0-GPFEL5:每个寄存器管理8个引脚的输入输出功能。
  • GPFSEL0-GPFSEL5:每个寄存器管理8个引脚的功能配置。

这些寄存器通过特定的位模式控制引脚的功能,如输入、高度电平、低电平、推挽输出等。

2. 引脚4配置

为了配置树莓派引脚4为输出,需要使用GPFSEL0寄存器。该寄存器的功能位如下:

  • 位12-14:控制引脚4的功能。
  • 要将引脚4设置为输出,需将这三个位设置为001

通过芯片手册P95可知,GPFSEL0寄存器的操作可以通过以下方式实现:

*GPFSEL0 &= ~(0x6 << 12);*GPFSEL0 |= (0x1 << 12);

这样,引脚4将被配置为输出。

3. 设置pin4为1和清零

要将引脚4设置为高电平(1)或低电平(0),需要使用GPSET0和GPCLR0寄存器:

  • GPSET0:用于设置引脚状态为高电平。

    *GPSET0 |= (1 << 4);
  • GPCLR0:用于清除引脚状态。

    *GPCLR0 |= (1 << 4);

4. 寄存器地址管理

树莓派的GPIO寄存器地址空间从0x3f200000开始,Linux通过ioremap函数进行内存虚拟化。需要注意的是,开发者应使用虚拟地址进行操作,而非物理地址。

5. 底层驱动开发

以下是树莓派引脚4的驱动代码示例:

#include 
#include
#include
static struct file_operations pin4_fops = { .open = pin4_open, .write = pin4_write, .read = pin4_read,};static ssize_t pin4_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { int userCmd; copy_from_user(&userCmd, buf, count); if (userCmd == 1) { *GPSET0 |= 0x1 << 4; } else if (userCmd == 0) { *GPCLR0 |= 0x1 << 4; } return count;}static int pin4_open(struct inode *inode, struct file *file) { *GPFSEL0 &= ~(0x6 << 12); *GPFSEL0 |= (0x1 << 12); return 0;}static ssize_t pin4_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { return 0;}device_t devno;static dev_t dev;static struct class *pin4_class;static struct device *pin4_dev;static ssize_t pin4_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { int userCmd; copy_from_user(&userCmd, buf, count); 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 count;}static int pin4_open(struct inode *inode, struct file *file) { printk("pin4_open\n"); *GPFSEL0 &= ~(0x6 << 12); *GPFSEL0 |= (0x1 << 12); return 0;}static ssize_t pin4_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { printk("pin4_read\n"); return 0;}static struct file_operations pin4_fops = { .owner = THIS_MODULE, .open = pin4_open, .write = pin4_write, .read = pin4_read,};static int pin4_drv_init(void) { printk("insmod driver pin4 success\n"); devno = MKDEV(231, 0); register_chrdev(231, "pin4", &pin4_fops); pin4_class = class_create(THIS_MODULE, "pin4"); pin4_dev = device_create(pin4_class, NULL, devno, NULL, "pin4"); GPSEL0 = ioremap(0x3f200000, 4); GPSET0 = ioremap(0x3f20001C, 4); GPCLR0 = ioremap(0x3f200028, 4); return 0;}void pin4_drv_exit(void) { iounmap(GPSEL0); iounmap(GPSET0); iounmap(GPCLR0); device_destroy(pin4_class, devno); unregister_chrdev(231, "pin4"); class_destroy(pin4_class);}module_init(pin4_drv_init);module_exit(pin4_drv_exit);

6. 应用层代码

应用层代码示例:

#include 
#include
#include
#include
int main() { int fd; int cmd; fd = open("/dev/pin4", O_RDWR); if (fd < 0) { printf("open pin4 fail\n"); exit(-1); } else { printf("open pin4 success\n"); } printf("Please input: "); scanf("%d", &cmd); printf("cmd=%d\n", cmd); if (cmd == 1) { printf("set 1\n"); write(fd, &cmd, 1); } else if (cmd == 0) { printf("set 0\n"); write(fd, &cmd, 1); } return 0;}

7. 编译步骤

请参考以下链接进行编译:编译步骤

通过以上配置,您可以轻松控制树莓派引脚的输入输出状态。

你可能感兴趣的文章
MySQL:索引失效场景总结
查看>>
Mysql:避免重复的插入数据方法汇总
查看>>
MyS中的IF
查看>>
M_Map工具箱简介及地理图形绘制
查看>>
m_Orchestrate learning system---二十二、html代码如何变的容易
查看>>
M×N 形状 numpy.ndarray 的滑动窗口
查看>>
m个苹果放入n个盘子问题
查看>>
n = 3 , while n , continue
查看>>
n 叉树后序遍历转换为链表问题的深入探讨
查看>>
N!
查看>>
N-Gram的基本原理
查看>>
n1 c语言程序,全国青少年软件编程等级考试C语言经典程序题10道七
查看>>
Nacos Client常用配置
查看>>
nacos config
查看>>
Nacos Config--服务配置
查看>>
Nacos Derby 远程命令执行漏洞(QVD-2024-26473)
查看>>
Nacos 与 Eureka、Zookeeper 和 Consul 等其他注册中心的区别
查看>>
Nacos 单机集群搭建及常用生产环境配置 | Spring Cloud 3
查看>>
Nacos 启动报错[db-load-error]load jdbc.properties error
查看>>
Nacos 报Statement cancelled due to timeout or client request
查看>>