ajain
Posts: 5
Joined: Sat Jul 05, 2014 2:40 pm

Toggling GPIO on Rpi B+ as fast as possible

Thu Feb 15, 2018 7:36 pm

Hello,
I am using this baremetal code to test how fast a GPIO can be toggled.
https://github.com/dwelch67/raspberrypi

I edited the blinker01 example to toggle Pin4 on the GPIO header.
Its basically a while loop with gpioset and gpioclear.
I have only been able to get an ON to OFF width of about ~200ns.

This on to off transition is 30ns when using /dev/mem and the Linux kernel.

Is there anyway this can be reduced in baremetal? The datasheet says GPIOs can ideally be pulsed at 250MHz.
The final aim of this exercise is to figure out uart data from a 2Mbps serial port.

TIA,
Aj

MarkR
Posts: 128
Joined: Fri Jan 25, 2013 1:55 pm

Re: Toggling GPIO on Rpi B+ as fast as possible

Thu Feb 15, 2018 10:20 pm

Have a look at what the pigpio library does. As far as I know, it uses the DMA controller to pulse output pins in a controlled, possibly very fast way.

Also, gpio4 (only, I think) has an alternative function (gpclk0) which can be enabled to pulse very fast indeed, if you can figure out how to do it. There is code floating around which pulses gpio4 at radio frequencies (this is not necessarily a good idea, especially with an antenna attached).

LdB
Posts: 912
Joined: Wed Dec 07, 2016 2:29 pm

Re: Toggling GPIO on Rpi B+ as fast as possible

Fri Feb 16, 2018 3:19 am

The Pi GPIO has a slow AXI bus it can't get anywhere near 250Mhz which you might hope for based on the core CPU speed and GPIO datasheet.

Maximum high/low clock speeds on the CPU access to the GPIO are
Pi3: 65Mhz
Pi2: 41Mhz
Pi1: 21Mhz

DMA transfers to the GPIO are significantly slower than CPU access
https://github.com/hzeller/rpi-gpio-dma-demo

It's not documented anywhere most of us setup simple loops with an oscilloscope to test it.

As MarkR commented above the PWM's are probably the fastest possibly approaching the datasheet values as they don't involve the slow axi bus.

So no there is simply no way to get near 250Mhz you hope for. You can basically treat the whole peripheral memory area like it has a big wait state inserted into it.

I would have thought the hardware UARTS on the Pi could be setup to deal with that speed or is the UART data non standard?

If it is non standard and you want a more practical suggestion, what you are trying to do is around a hundred lines of VHDL code which will be everywhere on the internet as it's a sort of VHDL tutorial example always done. It will easily fit into even the smallest FPGA and if you choose a 3.3V FPGA part then you can directly connect it to the Pi.

ajain
Posts: 5
Joined: Sat Jul 05, 2014 2:40 pm

Re: Toggling GPIO on Rpi B+ as fast as possible

Fri Feb 16, 2018 8:06 am

Thank you Markr and LDB for the response.
It is non standard serial data.
As I have not used FPGAs yet, was a little reluctant to do so. Will see what options there are and take a call.
Thanks for the suggestions,
Aj

Return to “Bare metal, Assembly language”