博客
关于我
树莓派博通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 Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>