cannot mmap I/O registers

Applications, development tools, FPGA, C, WEB
Post Reply
tjrob
Posts: 2
Joined: Wed Jun 09, 2021 3:17 am

cannot mmap I/O registers

Post by tjrob » Wed Jun 09, 2021 3:35 am

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,
                        0,mem,TTC0);
        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;
}

tjrob
Posts: 2
Joined: Wed Jun 09, 2021 3:17 am

Re: cannot mmap I/O registers - SOLVED

Post by tjrob » Wed Jun 09, 2021 3:43 am

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.

Post Reply
jadalnie klasyczne ekskluzywne meble wypoczynkowe do salonu ekskluzywne meble tapicerowane ekskluzywne meble do sypialni ekskluzywne meble włoskie

Who is online

Users browsing this forum: No registered users and 1 guest