Accessing GPIO using MMAP

Good day everyone,

I am currently trying to access the GPIO on the beaglebone using MMAP to improve the speed and efficiency on some code I am writing which requires fast toggling of GPIO.

I am attepting to set the GPIO output using the following function I wrote:

#define GPIO1_START 0x4804C000
#define GPIO1_DATAOUT 0x13C

int gpio_mmap_set(int value)

    int gpio_fd = open("/dev/mem", O_RDWR | O_SYNC);

    if (gpio_fd < 0)
       printf("Could not open GPIO memory fd\n");
       return 0;

    volatile ulong *gpio;

    gpio = (ulong*) mmap(NULL, 32, PROT_READ | PROT_WRITE, MAP_SHARED, gpio_fd, (GPIO1_START+GPIO1_DATAOUT));

    if (gpio == MAP_FAILED)
       printf ("GPIO Mapping failed\n");
       return 0;

    gpio[28] = value;


    return EXIT_SUCCESS;


However, I just get that 'Mapping failed!' could anyone tell me where I am going wrong...

To explain the defines I have used the AM335x technical manual which specifies GPIO1 bank as starting at 0x4804C000, then the data output register is at offset 0x13C and it is a 32 bit register where by I assume that each bit corresponds to each GPIO output on that bank, correct?

Many thanks in advance!

If you look back in the history under
Making GPIO pins available on the BeagleBoard XM

for my post of Sept 23, 2011, I have included the complete
code for a TCP server that sets up a bunch of the expansion header
pins for direct control of the GPIO. This works on the original
Beagle, I think it will work pretty much the same on the XM.

I know the comments are a bit sparse, if you need any help seeing
how this works just ask.

OOPS, sorry, I see now you are talking about the BONE, not the XM!
The I/O system there is significantly different, so all the mapping info
will have to change, and you will have to work your way around
whichever GPIO pads are dedicated to other functions. But, the basic
technique of setting up the pads and accessing them should work.
I did all of this in GPIO bank 5 on the old Beagle, that bank doesn’t
even exist on the chip used in the Bone.

I hope the I/O multiplexing runs faster on the bone, I could only update
a pin every 240 ns on the original Beagle.



I could only update
a pin every 240 ns on the original Beagle.

are you sure it updates every 240ns? that’s 4.2Mhz. I know that alot of people, including myself, can only get max gpio toggle around 2Mhz.

You could put the code for example?

You could put the code for example?

I put a link to a message from last year that had the code in the message. At the top
in the Google search bar, enter

“Making GPIO pins available on the BeagleBoard XM”

and it will take you to the message thread that has the code.

Yes, that’s right, if you raise the pin, wait 240 ns, then lower the pin, wait 240 ns, then you get a toggle rate of just over 2 MHz. So, we get the same result.

Good! That’s what i thought.

Also, i saw some beagle initiative projects while ago, and one of the projects was creating a userspace gpio library. I am not sure if anybody ever started on it. It would be great if the community develops a software toolkit for beagle/bone for this sort of things.

I once started a project here: but currently I am running
out of time, and the development stalls a bit. It is usable, though
not very well documented. If you want to try it, and have problems,
just contact me.