tags:

views:

679

answers:

1

Im struggling to read(and write) to HW registers from Linux user space. The goal is to configure some GPIO pins from and be able to set and read this pins.

According to the spec for the processor(imx27 from Freescale) the physical address for the register bank controlling GPIO this is 0x10015000

My assumption was that I could use something like this: unsigned long *gpio; fd = open("/dev/mem", O_RDWR); gpio = (unsigned long *) mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x10015000);

I now expected to be able to read and set data to the registers in the processor. The proble is that it does not matter which location i read, I always get 0.

For example register in physical location 0x10015220 contains a register showing which pins are in us as GPIO. This Defaults to 0xFFFFFFFF. Reading this register I expected to get something different than 0:

printf("PTC_GIUS: 0x%08lX\n", gpio[0x220]); gives PTC_GIUS: 0x00000000

Where am I going wrong ?

+1  A: 

The mmap on /dev/mem should work. Have you tried running your code as root? Maybe some security is preventing your program from accessing the address-space. Also make sure you've passed the correct physical address of your GPIO-Space.

The approach you've used works on my Cortex-A8 ARM-board running linux without problems.

If you can't get it working there is not much you can do except finding or writing a device-driver for the gpio (writing one is not that hard btw.).

With a bit of luck someone already did that for you. Does a node named /dev/gpio exist in your filesystem? If so you already have a driver.

A google search on /dev/gpio will give you all details on how to use it. You may also find the source for a simple gpio driver that you can modify to suit your needs.

Good luck, Nils

Nils Pipenbrinck
Yeah, it's often best to write a device driver, at least once past the prototyping stages.
Adam Goode