Gumboil
Posts: 44
Joined: Thu Dec 25, 2014 10:20 pm

Find RaspberryPi model and GPIO mode

Fri Jan 02, 2015 12:20 pm

I am writing objects for general use. Are there any functions in Python I can call which will return the Pi model that is being used and the GPIO mode (BOARD or BCM) that has been specified?

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

Re: Find RaspberryPi model and GPIO mode

Fri Jan 02, 2015 12:31 pm

Depends on the Python module you are using.

pigpio has http://abyz.co.uk/rpi/pigpio/python.htm ... e_revision

I have no idea if the modules which use alternative mappings of gpio names support a method of asking which is currently in use (it's meaningless outside such software). For instance pigpio only use Broadcom gpio numbers.

Gumboil
Posts: 44
Joined: Thu Dec 25, 2014 10:20 pm

Re: Find RaspberryPi model and GPIO mode

Fri Jan 02, 2015 12:55 pm

Thank you Joan - that is helpful

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Find RaspberryPi model and GPIO mode

Fri Jan 02, 2015 1:04 pm

Code: Select all

#!/usr/bin/python3

def getRevision():
  # Extract revision from cpuinfo file
  revision = "ERROR"
  try:
    f = open('/proc/cpuinfo','r')
    for line in f:
      if line[0:8]=='Revision':
        revision = line[11:15]
    f.close()
  except:
    revision = "ERROR"

  return revision

def revToModel():
  rev = getRevision()
  model = [
    "0002", ["Model B Rev 1.0", "256MB"],
    "0003", ["Model B Rev 1.0 (no fuses,D14)", "256MB"],
    "0004", ["Model B Rev 2.0 (mounting holes,Sony)", "256MB"],
    "0005", ["Model B Rev 2.0 (mounting holes,Qisda)", "256MB"],
    "0006", ["Model B Rev 2.0 (mounting holes,Egoman)", "256MB"],
    "0007", ["Model A (Egoman)", "256MB"],
    "0008", ["Model A (Sony)", "256MB"],
    "0009", ["Model A (Qisda)", "256MB"],
    "000d", ["Model B Rev 2.0 (mounting holes,Egoman)", "512MB"],
    "000e", ["Model B Rev 2.0 (mounting holes,Sony)", "512MB"],
    "000f", ["Model B Rev 2.0 (mounting holes,Qisda)", "512MB"],
    "0010", ["Model B+", "512MB"],
    "0011", ["Compute Module", "512MB"],
    "0012", ["Model A+", "256MB"]
  ]
  ix = model.index(rev)
  board, memory = model[ix+1]
  return (rev, board, memory)

rev, board, memory = revToModel()
print ("Revision: %s, Board: %s, Memory: %s" % (rev,board,memory))
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Gumboil
Posts: 44
Joined: Thu Dec 25, 2014 10:20 pm

Re: Find RaspberryPi model and GPIO mode

Fri Jan 02, 2015 4:15 pm

Excellent - thank you Dougie

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Find RaspberryPi model and GPIO mode

Fri Jan 02, 2015 4:27 pm

Gumboil wrote:Excellent - thank you Dougie
It's not future proof.

If the foundation create a model C, C+, B++ or model Q or anything with a new revision value my code will barf. Also the way I'm parsing the data from /proc/cpuinfo leaves a lot to be desired since I should probably be using a regex rather than a simple string compare and relying on positional data/length.

But it works for now.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

RobHenry
Posts: 452
Joined: Fri Sep 21, 2012 9:04 pm
Location: UK

Re: Find RaspberryPi model and GPIO mode

Fri Jan 02, 2015 5:03 pm

DougieLawson wrote: But it works for now.
It doesn't work if the warranty bit is set:

Code: Select all

rob@raspberrypi ~ $ cat /proc/cpuinfo |grep Revision
Revision	: 100000e
Could be fixed in a number of ways, I don't have a pi without the warranty bit set so can't regression test.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Find RaspberryPi model and GPIO mode

Fri Jan 02, 2015 5:16 pm

RobHenry wrote:
DougieLawson wrote: But it works for now.
It doesn't work if the warranty bit is set:

Code: Select all

rob@raspberrypi ~ $ cat /proc/cpuinfo |grep Revision
Revision	: 100000e
Could be fixed in a number of ways, I don't have a pi without the warranty bit set so can't regression test.
Your problem, you can code round it.

hint: change revision = line[-4:] to just work with the last 4 characters on the line.

I've never had any desire to overclock any of my three Raspberry Pis so all of my warranty bits are intact. You can also get revision directly from /sys/module/bcm2708/parameters/boardrev as a decimal value (if boardrev > 65535 then subtract 16,777,216 to lose the warranty bit).

Revision (as a hex value) is also available by parsing /proc/cmdline ("bcm2708.boardrev=0x??").
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Python”