Page 1 of 1

cannot mmap I/O registers

Posted: Wed Jun 09, 2021 3:35 am
by tjrob
I want to use the triple timer counters, and since there is no device driver for them I thought I could simply map their registers into user memory and configure them. Opening /dev/mem succeeds, and doing mmap() on it succeeds, but the first read gets a Segmentation fault. I have tried the address for GPIOs with the same result. I have tried 8-bit and 16-bit accesses with the same result, but I believe some document said to always reference I/O registers via 32-bit accesses.

What am I missing?

Code: Select all

//      frequencycounter.cpp
//      Attempt to use the triple timer counter 0.

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <cstdint>

const uint32_t TTC0 = 0xF8001000;

int main(int argc, char *argv[])
        int mem = open("/dev/mem",O_RDWR);
        if(!mem) {
                printf("Cannot open /dev/mem\n");
                return 1;

        uint32_t *ttc0 = (uint32_t *)mmap(0,0x84,PROT_READ|PROT_WRITE,
        if(!ttc0) {
                printf("Cannot mmap memory\n");
                return 1;

        for(uint32_t i=0; i<=0x80; i+=sizeof(uint32_t)) {
                printf("%08X %08X\n",i,ttc0[i/sizeof(uint32_t)]);

        return 0;

Re: cannot mmap I/O registers - SOLVED

Posted: Wed Jun 09, 2021 3:43 am
by tjrob
Solved it! -- need to add MAP_SHARED into the call to mmap(). That argument was 0 in my original code.

Note that TTC0 is in use, but TTC1 is not.