APQ8016 GPIO Registers acess using C


Hi all

Can anyone help me accessing GPIO registers using C ?
I’m using the Technical Reference Manual to see the registers, but I can’t write to them, or get several segmentation faults. Any help, or example in C ?

My Code is simply:

#define APQ8016_PERI_BASE 0x01000000
#define CLK_GATE_EN (APQ8016_PERI_BASE + 0x100000)

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

#define BLOCK_SIZE (4*1024)

int mem_fd;
void *gpio_map;

volatile unsigned *gpio;

#define TEST_GPIO *(gpio+(0x1000 * 52) //For GPIO_52

int main(int argc, char **argv)
int g,rep;


TEST_GPIO |= 1<<1 ; //Set bit 1

return 0;

void setup_io()
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf(“can’t open /dev/mem \n”);

/* mmap GPIO */
gpio_map = mmap(
NULL, //Any adddress in our space will do
BLOCK_SIZE, //Map length
PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
MAP_SHARED, //Shared with other processes
mem_fd, //File to map
GPIO_BASE //Offset to GPIO peripheral

close(mem_fd); //No need to keep mem_fd open after mmap

if (gpio_map == MAP_FAILED) {
printf(“mmap error %d\n”, (int)gpio_map);//errno also set!

gpio = (volatile unsigned *)gpio_map;

So I’m just trying to set bit 1 of TLMM_GPIO_CFGn, register on GPIO_52, I’m getting always a segmentation fault.
I’m using Linaro 4.9.56

Paulo Moreira


Ok, one of the errors was the mmap on the size, so I changed the define of BLOCK_SIZE to 4000*1024 (4Mb). Now I don’t get any error running the progam, but no changes to my GPIO_52 :frowning:
Any help would be appreciated.

Paulo Moreira