Crashing accessing mmap location

I’ve been working to get the access to the GPIO registers to replace the existing sys access we are using now. However, I’m crashing accessing both GPIO 2 and 3 (1 and 2 appear fine). I’m sure I’m just doing something stupid. Here is some basic setup:

__off_t MUX_OFFSET[] = {
0x44E07000, // GPIO 0
0x4804C000, // GPIO 1
0x481AC000, // GPIO 2
0x481AE000 // GPIO 3

uint8_t *gpio_addr;
uint32_t *gpio_oe_addr;
uint32_t *gpio_setdataout_addr;
uint32_t *gpio_cleardataout_addr;
uint32_t *gpio_dataout_addr;
uint32_t *gpio_datain_addr;


#define GPIO_MEM_LENGTH 0xfff
#define GPIO_OE 0x134
#define GPIO_SETDATAOUT 0x194
#define GPIO_DATAOUT 0x13C
#define GPIO_DATAIN 0x138``

Then here is example code that will always crash on accessing GPIO 2:

fd = open("/dev/mem", O_RDWR);

for( size_t gpio = 0; gpio < 4; ++gpio)
MUX_LAYOUT& mux = MUXes[gpio];

mux.gpio_addr = (uint8_t*)mmap(0, GPIO_MEM_LENGTH, PROT_READ | PROT_WRITE, MAP_SHARED, fd, MUX_OFFSET[gpio]);

if(mux.gpio_addr == MAP_FAILED)
throw MSC_EXCEPTION(“Unable to map texpr PIO”);

mux.gpio_oe_addr = (uint32_t *)(mux.gpio_addr + GPIO_OE);
mux.gpio_setdataout_addr = (uint32_t *)(mux.gpio_addr + GPIO_SETDATAOUT);
mux.gpio_cleardataout_addr = (uint32_t *)(mux.gpio_addr + GPIO_CLEARDATAOUT);
mux.gpio_dataout_addr = (uint32_t *)(mux.gpio_addr + GPIO_DATAOUT);
mux.gpio_datain_addr = (uint32_t *)(mux.gpio_addr + GPIO_DATAIN);

// Crashes here when gpio == 2
uint32_t temp = *mux.gpio_datain_addr;


In this case I’m crashing dereferencing gpio_datain_addr, but it will crash accessing any of the pointers created when setting up GPIO 2 and 3. Any thoughts? I'm running a relatively current debian build.

Thanks for the help,


Clocks for some of the GPIO modules are not enabled by default, and
accessing them will cause BUS error. The easiest workaround is to
export one pin from each of the modules using /sys interface:
Here is a short script which does the job:


You can try only map 4Bytes