Raspberry Pi 2 vs. 1 GPIO Benchmark

Raspberry Pi 2 GPIO Benchmark

It’s battle time! Some of you may have heard that Raspberry Pi 2 is out with more punch than ever. Just how much more? Well, apt-get dist-upgrade went about 5 times faster with the new Pi. With 1 GB of RAM and four cores, this will definitely be a boost for my home SSH box ergonomics over the previous version.

But what about hacking? There has been a lot of interest in getting GPIO benchmarks for the Pi 2 similar to my earlier Raspberry Pi GPIO benchmark. Well here it is! Please refer to the earlier article for source code and nice screenshots of square waves, as I’ll concentrate on the performance difference only here. You can also get the code from Github:


Summary of results

All the Pi 1 benchmarks were ran 14th and 15th February 2015 using latest versions of the libraries as stated in my updated benchmark post. Pi 2 benchmarks were all run 25th and 26th March 2015 with the latest versions. If you get significantly different results at a later date, please let me know and I’ll update the table!

Language Library Pi 1 Pi 2 Change
Shell /proc/mem access 2.8 kHz 7.0 kHz 2,5x
Shell / wiringPi WiringPi gpio utility 40 Hz 95 Hz 2,4x
Python RPi.GPIO 70 kHz 243 kHz 2,5x
Python wiringpi2 bindings 28 kHz 103 kHz 3,7x
Ruby wiringpi bindings 21 kHz N/A 3,7x
C Native library 22 MHz 41.7 MHz 1,9x
C BCM2835 5.4 MHz 7.2 MHz 1,3x
C WiringPi normal GPIO wiringPiSetup() 4.1 MHz 9.3 MHz 2,3x
C WiringPi GPIO wiringPiSetupGpio() 4.6 MHz 9.4 MHz 2x
C WiringPi sys wiringPiSetupSys() 120 kHz 185 kHz 1.5x
Perl BCM2835 48 kHz 154 kHz 3.2x

Continue reading Raspberry Pi 2 vs. 1 GPIO Benchmark

Unzip Library for C

Christmas holidays are a wonderful time to invent new projects. I decided I’d do some desktop coding for a change, and try to code an optimized image viewer for my old zipped pocket camera photos. First task of course was to read a zip file.

To my surprise, there wasn’t a “GNU standard library” available for this task like there is zlib for general compression, or libjpeg and libpng for images. Best match for my simple needs seemed to be Minizip, but at 7378 lines of code, and 2125 for just unzip.c (utilizing zlib so basically just file handling), I was not convinced, especially because I knew I had some very specific requirements to cater for (namely uncompressing all JPEGs to memory for fast rendering and thumbnail generation).

Zip File Structure – The Essentials

The ZIP file format turned out to be surprisingly simple, especially since I decided I would be sticking to bare essentials and skipping zip64 support, encryption, multifile zips, and all other compression methods than “store” (no compression) and “deflate” (easily decompressed with zlib, see below). Even with barebones setup, my zip routines would handle about 99.9 % of zips out there just fine.

Drawing on excellent ZIP format documentation from InfoZip’s latest appnote, the file structure I needed to parse seemed to have the following structure:

  • Local file header 1
  • File data 1
  • Data descriptor 1
  • Local file header N
  • File data N
  • Data descriptor N
  • … optional decryption / extra stuff …
  • Central directory
  • … zip64-specific extra stuff …
  • End of central directory record

Continue reading Unzip Library for C

Tutorial: State Machines with C Callbacks

State machine

Many electronics projects involve the device transitioning from one state to another. On a high level, it could be that your project is initially in a state where it awaits input, and once it receives it, it goes to another state where it performs a series of actions, eventually returning back to the initial state. On a lower level, many communications protocols are often simple or increasingly complex state machines. In many cases, an elegantly implemented state machine can simplify your code and make it easier to manage.

There are several methods to implement state machines programmatically starting from simple if-conditions to state variables and switch structures. In this tutorial I’ll cover a slightly more advanced method of using callbacks or “function pointers” as they are implemented in C. This has some performance benefits, makes up for some clean code, and you’ll learn a bit on the way!

State Machines with Conditionals

First, to introduce the idea of a state machine, lets take a simple example: Blinking LEDs. Imagine you want the LED to turn on for a second, and then turn off. We could do it with a simple 2-state machine:

enum states { 

enum states state = LED_OFF;

while(1) {
  if(state == LED_OFF) {
    state = LED_ON;
  } else {
    state = LED_OFF;
  sleep(1); // sleep for a second

If you can understand the code above, you have pretty much grasped the fundamentals of state machines. We have some processing specific to given state, and when we want to go to another state, we use a variable (in this example it’s called state) to do that. And by the way, if you haven’t encountered enum before, you should check out Enumerated type in Wikipedia. It’s a very handful tool to add to your C coding arsenal.
Continue reading Tutorial: State Machines with C Callbacks