这篇博客记录了我在用户程序中将物理地址映射到虚拟地址,然后使用虚拟地址控制树莓派3B的GPIO的过程。以下是整个过程的记录:
1、下载数据手册
和控制单片机IO口相似,如果用户想控制树莓派的GPIO,就得先知道GPIO相关寄存器的地址和设置的方法。树莓派的网站上提供了外设说明手册(Peripheral specification),这个手册对芯片上的外设怎么使用进行了描述。不过,Pi 3 的处理器是BCM2837,官网只提供了BCM2835(Pi 1 处理器)的外设说明手册。由于两个芯片外设上区别不大,我直接下载了BCM2835的手册来参考。下载手册的网址:https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/README.md
2、查阅GPIO相关寄存器地址和设置方法
翻到第5页,可以看到下图这个关于ARM地址映射的描述。
中间的部分为ARM的物理地址分配方式。IO外设(IO Peripherals)的物理地址分配在0x20000000(这是BCM2835的)。由于芯片不同,BCM2837的IO设备地址已经改为 0x3F000000,这一点,在官网提供的文档中也有说到