访问树莓派 4 系统定时器

时间:2021-05-04 17:25:45

标签: embedded-linux hardware raspberry-pi4

我在读取 Raspberry Pi 4 系统计时器时遇到问题。 我的理解是 LO 32 位应该在地址 0x7e003004 处。 我的阅读总是返回-1。 这是我正在尝试的方式:

int fd;
unsigned char* start;
uint32_t* t4lo;

fd = open("/dev/mem", O_RDONLY);
if (fd == -1)
{
perror("open /dev/mem");
exit(1);
}
start = (unsigned char*)mmap(0, getpagesize(), PROT_READ, MAP_SHARED,
fd, 0x7e003000);
t4lo = (unsigned int *)(start + 0x04);
...
uint32_t Rpi::readTimer(void)
{
    return *t4lo;
}

我应该检查 start 的值,但 gdb 告诉我这是合理的,所以我认为这不是问题。

(gdb) p t4lo
$4 = (uint32_t *) 0xb6f3a004

并且 gdb 不会让我访问 *t4lo。有什么想法吗?

编辑:clock_gettime() 满足了我的需求,但我仍然很好奇。

1 个答案:

答案 0 :(得分:0)

仔细观察https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2711/rpi_DATA_2711_1p0.pdf 第 5 页上的图 1 显示地址因查看对象的不同而有所不同。如果从左侧的 0x7c00_0000 开始,然后向右移动,很明显它在 0xfc00_0000 处显示给处理器。因此将定时器基地址更改为 0xfe00_3000 解决了问题。

秘密隐藏在第 1.2.4 节中:

So a peripheral described in this document as being at legacy address 0x7Enn_nnnn
is available in the 35-bit address space at 0x4_7Enn_nnnn, and visible to the ARM
at 0x0_FEnn_nnnn if Low Peripheral mode is enabled.