signal
Posts: 3
Joined: Tue Dec 25, 2012 10:41 pm

40Mhz GPIO read speed?

Wed Dec 26, 2012 3:47 am

I think this maybe a little over ambitious.

I need to capture 8bit @40Mhz data and not sure the RPi can handle that on the GPIO pins.
I would use the 8 GPIO's on P1 and put the clock on a GPIO on P5 for triggering.

Idea would be to either fill a buffer and save for analysis or stream the data through a socket on the ethernet port for saving by another system. From what I can tell, saving the data to the SD card in realtime won't work as it's too slow.

But first thing first, can the GPIO handle that speed?

signal
Posts: 3
Joined: Tue Dec 25, 2012 10:41 pm

Re: 40Mhz GPIO read speed?

Wed Dec 26, 2012 7:35 pm

Based on the wiringPi speed test example (which tests 3 methods of accessing GPIO) it looks like you can get up to about 14Mhz reliably. Maybe a smidge faster with overclocking.

This would still be well under my 40Mhz need though.

Gordon at wiringPi pointed out that there are 32 GPIO lines to use though. Using something like a shift register to store x4 8bit samples would require 10Mhz to read. Well within the limits but unsure how to handle a read cycle to the RPi while we still have data coming in.

Digging into it a bit more.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: 40Mhz GPIO read speed?

Wed Dec 26, 2012 8:10 pm

Personally at that data rate I would create external hardware or dedicated micro to acquire the data and transfer the data to the Pi as buffers.

How it is acquired depends on the characteristics of the datastream, clock synchronisation , and many other factors, like how bursty the datastream is.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: 40Mhz GPIO read speed?

Thu Dec 27, 2012 1:50 pm

signal wrote:Based on the wiringPi speed test example (which tests 3 methods of accessing GPIO) it looks like you can get up to about 14Mhz reliably. Maybe a smidge faster with overclocking.

This would still be well under my 40Mhz need though.

Gordon at wiringPi pointed out that there are 32 GPIO lines to use though. Using something like a shift register to store x4 8bit samples would require 10Mhz to read. Well within the limits but unsure how to handle a read cycle to the RPi while we still have data coming in.

Digging into it a bit more.
Possibly some confusion - there are 2 x 32-bit GPIO registers, however the 8 (to 21) avalable GPIO pins are all inside one of those registers, but not at consecutive bit locations.

So you could read the 32-bit input register in a single cycle, then shift the bits round to get the 8 bits you need - if you can do this fast enough then you might be in with a chance - but I wasn't clear if it was 40,000,000 bits/sec expressed as 5,000,000 samples of 8 bits a second, or a stream of single bits at 40MHz...

If the samples are coming in at 5MHz then you could possibly read the 32-bits into memory for each sample, then post-process it to extract the 8-bit samples, but it depends on how many samples you're after. (You'll run out of RAM quickly!)

-Gordon
--
Gordons projects: https://projects.drogon.net/

signal
Posts: 3
Joined: Tue Dec 25, 2012 10:41 pm

Re: 40Mhz GPIO read speed?

Fri Dec 28, 2012 11:46 am

Ah.. thanks for the clarification.

I'll be able to hook up a decent logic analyzer to the device this weekend and see what's going on.

User avatar
Grumpy Mike
Posts: 934
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: 40Mhz GPIO read speed?

Sat Dec 29, 2012 1:40 pm

Normally along with the data you will also have a signal of some sorts to tell when the data is valid. This can be called a strobe signal.
When receiving the data the Pi must look at this signal until it see it being valid and then when it is to read in the data. So it is not just a matter of reading the data, you have to read it at the right time.

rgh
Posts: 212
Joined: Fri Nov 25, 2011 3:53 pm

Re: 40Mhz GPIO read speed?

Sat Dec 29, 2012 1:57 pm

Even using DMA to read the GPIO as fast as it can, I don't think you'll get much over 20M reads per second. Then what with contention on the memory and I/O bus, pauses while the DMA controller chains on to another control block, etc, you might well go a couple of microseconds occasionally without GPIO being read. Then, every 500ms, the GPU freezes everything for up to 16us while it adjusts the memory refresh rate based on temperature. You can disable that adjustment, read the Panalyzer thread for details. If you want to poll some strobe signal to see when data is valid then you can't use DMA, and the rate you'll get using the CPU will be much lower - with Panalyzer I concluded that I couldn't sample reliably above 1MHz. Panalyzer loops reading a counter that ticks over once a microsecond. Every time it ticks over, the code reads the GPIO pins, and this approach only works because it disables interrupts while it is sampling.

ci.v.ic
Posts: 5
Joined: Wed Aug 20, 2014 11:37 pm

Re: 40Mhz GPIO read speed?

Fri Aug 29, 2014 12:46 pm

I was able to read and write a 5V SRAM with 25 MHz max. I used assembler code to maximize the bit banging method, however, 40 MHz seems not to be possible. Details here: http://d-fence.sytes.net/raspberry-pis-gpio-speed/

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: 40Mhz GPIO read speed?

Fri Aug 29, 2014 12:54 pm

ci.v.ic wrote:I was able to read and write a 5V SRAM with 25 MHz max. I used assembler code to maximize the bit banging method, however, 40 MHz seems not to be possible. Details here: http://d-fence.sytes.net/raspberry-pis-gpio-speed/
What code did you use to read data? Or was this just a case of toggling gpios as fast as possible?

ci.v.ic
Posts: 5
Joined: Wed Aug 20, 2014 11:37 pm

Re: 40Mhz GPIO read speed?

Fri Aug 29, 2014 1:16 pm

I'm sorry it was only bit banging (toggling pins). There is second memory interface but I'm trying to find a documentation about that interface for months. Probably it is only for sd cards but maybe it is really for a second memory chip.

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: 40Mhz GPIO read speed?

Fri Aug 29, 2014 1:20 pm

ci.v.ic wrote:I'm sorry it was only bit banging (toggling pins). There is second memory interface but I'm trying to find a documentation about that interface for months. Probably it is only for sd cards but maybe it is really for a second memory chip.
You may have more joy using SPI. If I get a chance I'll connect two Pi's and see how fast I can push data between them. Might also try the same experiment with bit bang gpios.

User avatar
mikronauts
Posts: 2771
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: 40Mhz GPIO read speed?

Fri Aug 29, 2014 5:34 pm

How many consecutive samples must you read before you can have a break, and how long would the break be?
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: 40Mhz GPIO read speed?

Fri Aug 29, 2014 5:45 pm

mikronauts wrote:How many consecutive samples must you read before you can have a break, and how long would the break be?
I'm not sure that ci.v.ic had a particular task in mind, the tone seems to be more one of experiment.

User avatar
mikronauts
Posts: 2771
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: 40Mhz GPIO read speed?

Fri Aug 29, 2014 7:35 pm

ah I thought he had a particular application, and I was trying to see if there was a large enough off the shelf FIFO he could use as a buffer, or if another microcontroller could help.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

jdb
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2299
Joined: Thu Jul 11, 2013 2:37 pm

Re: 40Mhz GPIO read speed?

Fri Aug 29, 2014 9:14 pm

At 40MHz your prime concern will not be one of data acquisition rate.

The GPIO pins and trace routing are not designed to carry signals that fast with acceptable integrity. 25MHz is about as good as you'll get on the GPIO header.

If you try to sample an 8-bit bus at 40 meg then don't be surprised when you get data glitches, bit flips and other such nasties. The pins were never meant to go that fast and still transmit/receive valid logic levels.
Rockets are loud.
https://astro-pi.org

User avatar
mikronauts
Posts: 2771
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: 40Mhz GPIO read speed?

Fri Aug 29, 2014 9:57 pm

Thanks jdb, that is good to know.

One of these days I'll have to 'deep dive' the BCM docs, however unlike the OP I have not had any need for more than 1MHz from the GPIO header for a serial port.

Mind you, one of these days I intend to find out how fast I can get /dev/ttyAMA0 to go reliably, for maximum comm rate with RoboPi.
jdb wrote:At 40MHz your prime concern will not be one of data acquisition rate.

The GPIO pins and trace routing are not designed to carry signals that fast with acceptable integrity. 25MHz is about as good as you'll get on the GPIO header.

If you try to sample an 8-bit bus at 40 meg then don't be surprised when you get data glitches, bit flips and other such nasties. The pins were never meant to go that fast and still transmit/receive valid logic levels.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

danjperron
Posts: 3487
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: 40Mhz GPIO read speed?

Sat Aug 30, 2014 2:31 am

Hi ci.v.ic

is 25Mhz the frequency you get from toggle on/off? if yes than you got 50M change per second.

Also you assembly will have been faster if you didn't recalculate the bit all the time.

Code: Select all

               __asm__(
            "mov r1,#1\n\t"
            "lsl r1,#17\n\t"
            "loop:\n\t"

            "str r1,[%0,#28]\n\t"     
            "str r1,[%0,#40]\n\t"     

            "b loop\n\t"
            :
            :"r"((uint32_t)gpio)
            : "r0", "r1"
            );
Daniel

ci.v.ic
Posts: 5
Joined: Wed Aug 20, 2014 11:37 pm

Re: 40Mhz GPIO read speed?

Thu Sep 04, 2014 9:14 am

danjperron wrote: is 25Mhz the frequency you get from toggle on/off? if yes than you got 50M change per second.

Also you assembly will have been faster if you didn't recalculate the bit all the time.
Hi Daniel,

according to the photo of the oscilloscope it was indeed 25 MHz. And you are right, my code is not optimal. But as far as I can remember, a few instruction more or less didn't make it faster or slower.

Hi mikronauts,
In fact I would like to add RAM that is shared with other components but doing this with bit banging is not the optimal way, so it was just an experiment. What do you mean by "break"?

User avatar
mikronauts
Posts: 2771
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: 40Mhz GPIO read speed?

Thu Sep 04, 2014 5:58 pm

I was wondering of you need to continiously sample, or had a number of samples before you could do some processing.
ci.v.ic wrote: Hi mikronauts,
In fact I would like to add RAM that is shared with other components but doing this with bit banging is not the optimal way, so it was just an experiment. What do you mean by "break"?
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

ci.v.ic
Posts: 5
Joined: Wed Aug 20, 2014 11:37 pm

Re: 40Mhz GPIO read speed?

Thu Sep 04, 2014 10:07 pm

A bit processing is possible between I/O commands without lowering the frequency, seems that the I/O commands block the execution until the pin can be changed. But that's just an observation.

sibiquin
Posts: 21
Joined: Thu Nov 01, 2012 4:45 pm

Re: 40Mhz GPIO read speed?

Wed Dec 03, 2014 1:35 am

I am left a bit confused by this discussion. One line indicates about 1MHz sampling on GPIO is the best that can be expected, other lines talk about 25MHz. What am I missing?

In my application I want to read 4 bits at 3MHz continuously - data is real time and fixed rate (e.g. no pause or gaps). With a bit of external hardware I can read 8 bits at 1.5MHz or 16 bits at 750KHz. There needs to be enough CPU cycles to write this data to a file without falling behind.

So what is the best approach? I have a bit clock (3MHz), 4 data lines, and a word clock (identifies 32-bit word boundaries in the data streams). I understand the software approach of sampling the bit clock and reading the 4 data lines on a clock change. Seems like very high CPU overhead. Next best would be the bit clock causes an interrupt and the interrupt routine reads the 4 data lines (can the interrupt be serviced reliably in the bit-time?). Is there a DMA solution?... I have no idea what the software side of that would look like...

Thanks for any ideas.
-Mark

jdb
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2299
Joined: Thu Jul 11, 2013 2:37 pm

Re: 40Mhz GPIO read speed?

Wed Dec 03, 2014 8:42 pm

sibiquin wrote:I am left a bit confused by this discussion. One line indicates about 1MHz sampling on GPIO is the best that can be expected, other lines talk about 25MHz. What am I missing?

In my application I want to read 4 bits at 3MHz continuously - data is real time and fixed rate (e.g. no pause or gaps). With a bit of external hardware I can read 8 bits at 1.5MHz or 16 bits at 750KHz. There needs to be enough CPU cycles to write this data to a file without falling behind.

So what is the best approach? I have a bit clock (3MHz), 4 data lines, and a word clock (identifies 32-bit word boundaries in the data streams). I understand the software approach of sampling the bit clock and reading the 4 data lines on a clock change. Seems like very high CPU overhead. Next best would be the bit clock causes an interrupt and the interrupt routine reads the 4 data lines (can the interrupt be serviced reliably in the bit-time?). Is there a DMA solution?... I have no idea what the software side of that would look like...

Thanks for any ideas.
-Mark
The hardware constraint is likely to be around the 25MHz point, i.e. signals transmitted to GPIO inputs are likely to be corrupted if they are over 25MHz.

If you ran the ARM processor (and core clock speed) as fast as they could go, with an assembly-optimised reading loop then you can achieve some very fast sample rates, but ultimately the latencies within the chip limit you. I profiled how fast it took to a) reset the USB interrupt and b) flip a GPIO up and down and the answer is ~390nS, which includes FIQ entry time and a single function call from the vector table. Therefore 3MHz sample rate is probably out of bounds from software.

The correct way to do this would be to use a microcontroller that could read the data and buffer it internally. 4 bits at 3MHz translates to 12MHz single-bit, and there is dedicated hardware within the SoC to transfer data serially - SPI. If you have sufficient internal memory in the micro, then a simple FIFO interface exposed over SPI would give you the mechanism to transport data to the SoC.

If you want offline analysis then you just have to write that to a file. 1.5MB/s is well within the capabilities of the CPU.
Rockets are loud.
https://astro-pi.org

Sebaxus
Posts: 1
Joined: Sat Feb 03, 2018 3:18 pm

Re: 40Mhz GPIO read speed?

Sat Feb 03, 2018 3:20 pm

Just for anyone wondering here is a quick guide on how to change the speed.
Link: https://michaelfranzl.com/2016/11/10/se ... pberry-pi/

Return to “Interfacing (DSI, CSI, I2C, etc.)”