It all started in September 2012. The Raspberry Pi Foundation went and made some improvements to the Pi. How dare they!! ;) Seriously, though, there were some significant improvements to the Pi and a Rev 2 version was launched with double the RAM and some other changes/additions/improvements.
This threw up some minor headaches for developers because some of the GPIO pinouts were changed and some new GPIO ports were made available on a brand new “solder it yourself if you want it” header called P5 (see the leaning header of Pi5a)
GPIO 0 became GPIO 2 (P1 pin 3)
GPIO 1 became GPIO 3 (P1 pin 5)
GPIO 21 became GPIO 27 (P1 pin 13)
The two i2c buses were swapped around:
SDA0 became SDA1 (P1 pin 3 alternative function)
SCL0 became SCL1 (P1 pin 5 alternative function)
Some new GPIO ports 28, 29, 30, 31 were made available if you solder on P5 header.
People whose programs no longer work with all Raspberry Pis – that’s who!
For example, the Gertboard LEDs program uses GPIO21. If using a Rev 1 Pi this worked fine, but on a Rev 2 Pi (including model A) the fifth LED wouldn’t light because GPIO21 is now GPIO27. To work properly, the program needs to be able to know whether it’s running on a Rev 1 or Rev 2 Pi, so it can use the correct ports.
How to check your Raspberry Pi Revision number?
You can see this gives lots of info. Near the bottom is “Revision : 000f”
This tells me I have a Rev 2 Pi, but it’s a bit clunky. There are several different rev. codes for different Pi models and manufacturers (here’s a list of them). We could write some code to check the cpuinfo and extract the bit we want, compare it with known revision codes etc. But we don’t need any of that because, from RPi.GPIO 0.4.0a onwards (September 2012) we can use a built-in RPi.GPIO variable which does it all for us. (If you have older than 0.4.0a, see yesterday’s post for updating instructions.)
It’s called GPIO.RPI_REVISION
As before, we can try this out in a live python session…
Mine returns a 2 because it’s a Rev 2 Pi.
Or you can incorporate the code into a program as we did in yesterday’s post …
import RPi.GPIO as GPIO print "Your Pi is a Revision %s, so port 21 becomes port 27 etc..." % GPIO.RPI_REVISION
So then, when you’re doing some GPIO work that uses any of the ports 0, 1, 2, 3, 21, 27, 28, 29, 30, 31, you can be sure to control the correct ports. See the example below…
import RPi.GPIO as GPIO if GPIO.RPI_REVISION == 1: ports = [0, 1, 21] else: ports = [2, 3, 27] print "Your Pi is a Revision %s, so your ports are: %s" % (GPIO.RPI_REVISION, ports)
As long as no further changes to GPIO pin allocations are made, the above should work with all future Pi revisions. If further changes are made, they will need to be incorporated like this (and Ben will have to update RPi.GPIO again)…
import RPi.GPIO as GPIO if GPIO.RPI_REVISION == 1: ports = [0, 1, 21] elif GPIO.RPI_REVISION == 2: ports = [2, 3, 27] else: ports = ["whatever the new changes will be"] print "Your Pi is a Revision %s, so your ports are: %s" % (GPIO.RPI_REVISION, ports)
That was part 2: checking your Pi board revision number. Tomorrow, in part 3 we’ll cover the best way to handle a clean CTRL+C exit, protect your Pi and avoid all those nasty, horrid, “port already in use by another program” messages.