博客
关于我
树莓派博通BCM2835芯片的IO口驱动代码调试和测试
阅读量:325 次
发布时间: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. 编译步骤

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

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

你可能感兴趣的文章
Airtest自动化测试 Docs airtest.core.android package
查看>>
11.2.6 时间值的小数秒
查看>>
Redis源码分析(七)--- zipmap压缩图
查看>>
【MySQL】(九)触发器
查看>>
Oracle 11G环境配置
查看>>
【Python】(十二)IO 文件处理
查看>>
【Oozie】(三)Oozie 使用实战教学,带你快速上手!
查看>>
师兄面试遇到这条 SQL 数据分析题,差点含泪而归!
查看>>
C语言的数值溢出问题(上)
查看>>
8051单片机(STC89C52)以定时器中断模式实现两倒计时器异步计时
查看>>
vue项目通过vue.config.js配置文件进行proxy反向代理跨域
查看>>
android:使用audiotrack 类播放wav文件
查看>>
聊聊我的五一小假期
查看>>
数据库三个级别封锁协议
查看>>
ACM/NCPC2016 C Card Hand Sorting(upc 3028)
查看>>
Java求逆波兰表达式的结果(栈)
查看>>
ubuntu学习笔记-常用文件、命令以及作用(hosts、vim、ssh)
查看>>
SLAM学习笔记-求解视觉SLAM问题
查看>>
普歌-允异团队-HashMap面试题
查看>>
还在一个一个手动安装虚拟机吗?Cobbler自动部署装机一键最小化安装打把游戏就好了
查看>>