bask185
Posts: 120
Joined: Thu Mar 09, 2017 11:39 am
Location: Netherlands

What would the max frequency of a C program be?

Mon Jul 16, 2018 10:08 am

If I would write a C program to controll just the I/O, just like you would program a uController.

What would be the maximum running frequency for such a program?

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24181
Joined: Sat Jul 30, 2011 7:41 pm

Re: What would the max frequency of a C program be?

Mon Jul 16, 2018 10:13 am

Do you mean the maximum frequency you can run a GPIO at? Not the max frequency that the CPU running the C program is going at?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

drgeoff
Posts: 9926
Joined: Wed Jan 25, 2012 6:39 pm

Re: What would the max frequency of a C program be?

Mon Jul 16, 2018 10:14 am

Same answer as for "How long is a piece of string?"

It depends.

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: What would the max frequency of a C program be?

Mon Jul 16, 2018 10:54 am

bask185 wrote:
Mon Jul 16, 2018 10:08 am
If I would write a C program to controll just the I/O, just like you would program a uController.

What would be the maximum running frequency for such a program?



Will the resulting executable be running under the control of an Operating System such as Raspbian? (In which case it won't really be "just like you would program a uController".)

Or will it be a self-contained executable that runs directly on the RPi hardware in place of a conventional Operating System? In which case your query would be better transferred to the "Programming/Bare Metal" sub-forum.


In either case: will the bottleneck be the cpu-performance of the processor in the (unspecified) RPi hardware, or the switching speed of the gpio circuitry? Wouldn't it be a worthwhile experiment to do some first-hand tests to determine that from your own experience, and under your own working conditions?

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

Re: What would the max frequency of a C program be?

Mon Jul 16, 2018 11:28 am

Which I/O?
  • SD card
  • USB
  • Ethernet
  • HDMI
  • Serial
  • I2C
  • SPI
  • GPIO

bask185
Posts: 120
Joined: Thu Mar 09, 2017 11:39 am
Location: Netherlands

Re: What would the max frequency of a C program be?

Tue Jul 31, 2018 8:21 am

what I mean is the following.

I want to write a C program to controll the I/O. Than compile it to a binary and run that binary on a raspberry 3B+ with raspbian or raspian lite

If I want to make one I/O high on a uController it takes 4 clock cycles. An atmega328p at 16MHz would take 1/32^6 *4 = 250ns to change one I/O port. How long would a RPI take to do exactly the same?

I am only interested in the cycle time of a running C program. So the max frequency that the CPU running the C program

jahboater
Posts: 4843
Joined: Wed Feb 04, 2015 6:38 pm

Re: What would the max frequency of a C program be?

Tue Jul 31, 2018 8:36 am

Still not clear what you mean.

See this old post. Note that the new 3B+ model should be quite a bit faster.
http://codeandlife.com/2012/07/03/bench ... pio-speed/

Otherwise if you mean CPU clock speed, then it is 1400MHz for the Pi 3B+.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24181
Joined: Sat Jul 30, 2011 7:41 pm

Re: What would the max frequency of a C program be?

Tue Jul 31, 2018 9:40 am

If you are talking about how fast you can change the state of the GPIO pons, it's a bit more complicated than the CPU speed. It's related to the bus speed, which IIRC is 250Mhz, because the GPIO are attached to the bus. Not only that, but its takes quite a few cycles of that bus for the GPIO's to actually do anything. So probably the limit is 10's of megahertz.

BUT, the bus is a shared resource, so there is no guarantee that it will be the same latency from one run to another- it depends what else is going on.

So in short, its doesn't work like the atmega.

Joan (posted above) has done sterling work in using DMA to work the pins, her libraries are your best option. She also has benchmarks that might be of help.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: What would the max frequency of a C program be?

Tue Jul 31, 2018 5:27 pm

bask185 wrote:
Tue Jul 31, 2018 8:21 am
If I want to make one I/O high on a uController it takes 4 clock cycles. An atmega328p at 16MHz would take 1/32^6 *4 = 250ns to change one I/O port. How long would a RPI take to do exactly the same?

I am only interested in the cycle time of a running C program. So the max frequency that the CPU running the C program
It's not really possible to get a definitive answer to that.

The Pi running a Linux distribution is a multi-user, multi-process system. This means there can be multiple process competing for cpu cycles, the kernel will then schedule the tasks to run for a short period to give the impression that things are happening at the same time. An atmega will only be running your single program so you know how quickly things happen.

There are things you can do to improve the chances of your own code being run more regularly but from userspace (where most software runs) you don't have full control over the kernels scheduling.

hippy
Posts: 6281
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: What would the max frequency of a C program be?

Tue Jul 31, 2018 6:40 pm

bask185 wrote:
Tue Jul 31, 2018 8:21 am
I want to write a C program to controll the I/O. Than compile it to a binary and run that binary on a raspberry 3B+ with raspbian or raspian lite
The maximum frequency you could toggle a GPIO pin at would seem to be about 22MHz according to the link jahboater posted -

http://codeandlife.com/2012/07/03/bench ... gpio-speed

I would expect roughly the same for a Pi 3B+ as for the earlier Pi because I believe the I/O hardware and the connecting bus is the same, only the ARM cores are faster.

The slower examples should get faster because there is more overhead to them which will execute faster on more modern Pi. For native C the bottleneck would be the bus to the GPIO pins, not CPU execution speed, so I would not expect to see much improvement if any.

My guess is that 25MHz might be achievable with assembly language and optimal coding.

The problem is that running code on a Pi with an OS isn't like running code on a micro which doesn't have an OS.

On the Pi things can happen at any time and take control away from you code. It would be like having interrupts handled by you microcontroller code; those interrupts would affect the maximum GPIO pin toggle frequency, extend the high and/or low periods.

There is a maximum theoretical toggling rate but no guarantee that you will achieve that continually.

You could run the Pi without an OS, could even run your code in the GPU rather than on an ARM core, or maybe even put your pin toggling code on a separate core, but once you have anything other than just your code running you will see some drop from maximum frequency.

Added: It seems the bus to GPIO is faster on the later Pi's -

https://github.com/hzeller/rpi-gpio-dma-demo

Shows the (non-DMA) maximum bit-banged pin toggling rate as 22.7MHz for Pi original, 41.7MHz for a Pi 2 and 65.8MHz for a Pi 3.

Also - https://periph.io/news/2017/gpio_perf - shows 82.6MHz on a Pi 3, and 29.3MHz on a Pi Zero W, so I guess it really does come down to how optimal the executable code is.

bask185
Posts: 120
Joined: Thu Mar 09, 2017 11:39 am
Location: Netherlands

Re: What would the max frequency of a C program be?

Mon Aug 06, 2018 6:08 am

jamesh wrote:
Tue Jul 31, 2018 9:40 am
If you are talking about how fast you can change the state of the GPIO pons, it's a bit more complicated than the CPU speed. It's related to the bus speed, which IIRC is 250Mhz, because the GPIO are attached to the bus. Not only that, but its takes quite a few cycles of that bus for the GPIO's to actually do anything. So probably the limit is 10's of megahertz.

BUT, the bus is a shared resource, so there is no guarantee that it will be the same latency from one run to another- it depends what else is going on.

So in short, its doesn't work like the atmega.

Joan (posted above) has done sterling work in using DMA to work the pins, her libraries are your best option. She also has benchmarks that might be of help.
Thank you, most helpfull answer

Return to “Advanced users”