BeagleBone Black GPIO through /dev/mem

One of my projects involved interfacing the BeagleBone Black to an Quadrarture Encoder. Before beginning with it, I thought the task would be simple and easy, I would use adafruit PyBBIO library to do it. However, I soon realised that PyBBIO is just a Python wrapper of the /sysfs interface! I wrote a script that uses /sysfs and toggles P9_12, and I then probed the pin on my oscilloscope. To my horror this is what I found:

Upon googling a bit, I stumbled upon this and then, this. The code was for BeagleBone White, but I decided to try it out on the Black too see if it works. And yes, it did, I could clearly see USR1_LED blink while running the code.

This meant that the address range for the GPIO registers had not changed from the Vanilla BeagleBone to the BeagleBone Black. I then decided to use the same code to toggle one of the GPIO pins. I decided upon P9_12, thats at offset 28 in the GPIO0 register bank(same as the leds). Ofcourse, on simply modifying the code, I did not get any result. I had to MUX the pin, by using a custom device tree overlay. It is then that I referred to this excellent tutorial.

After performing the steps mentioned above, I again tried to run the code to toggle P9_12. No Luck.

I figured that I first needed to export the pin, set the pin direction, and its initial value, while this could too be done using /dev/mem, however to simplify things I decided to go ahead with the sysfs method to export the pin and set its direction.

After that, I again tried to run the code that uses /dev/mem. VOILA! It finally worked. On probing with the oscilloscope this is what I got:

A toggle rate of 2.78 MHz! thats nearly 1000 times what I got earlier! Though one would expect a much higher toggle rate from a 1GHz processor, however 2Mhz+ was sufficient for my application! I plan to create my own library to use the mmap soon, do email me incase you found this to be useful, or incase you need some more assistance, I'll be glad to help!

You can download the example code from my GitHub page. Here's the link

Featured on: