4

It's already a while since I got my Pi 4, but never used the gpio readall command until yesterday. I have run into an interesting error.

pi@raspberrypi:~ $ gpio readall
Oops - unable to determine board type... model: 17

Was the gpio command already updated to support the new RPi 4, or is my Pi broken? I could read all the pin states and set the pin states from within Python. Is there an alternative to reading the pin states with gpio readall?

Thanks in advance.

EDIT

I have tried flashing new Raspbian image. Still no luck. :(

1
6

As far as I am aware it should work provided you download and install the latest version of wiringPi.

http://wiringpi.com/wiringpi-updated-to-2-52-for-the-raspberry-pi-4b/

cd /tmp
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
4
  • Is it installable with apt-get? Shouldn't the image include the updated version? – Jakub Bláha Sep 10 '19 at 16:21
  • You tell me. Which version do you get with apt-get? Is it version 2.52? – joan Sep 10 '19 at 16:23
  • Well, I tried your solution and it has worked perfectly. Thanks! I cannot remember which version I got with apt-get. – Jakub Bláha Sep 10 '19 at 16:26
  • ´apt-get´ gets version 2.50, which doesn't seem to work on the 4B. With the 2.52, the problem is solved, though. – PMF Oct 4 '19 at 20:10
1

Now that wiringpi is deprecated (and requires a download from the author's site to work on the Pi4) the future of the gpio readall command is unknown.

I wrote a replacement (which uses pigpiod) - which has the advantage of displaying the ACTUAL programmed GPIO function if changed and can be used remotely (like other pigpiod programs). This is simplified, and does not display the confusing wiringpi pin numbers.

If pigpiod is not running run sudo pigpiod

gpioread by default displays power pins in colour.

To restore non-coloured output uncomment the 2 lines # non-coloured output and comment out # coloured output.

Code can be downloaded from https://github.com/Milliways2/gpioread (which has source to display pull on Pi4)

#! /usr/bin/env python3
# 2020-10-18
# 2020-11-07    400, CM4
# 2021-04-13    Fix Wrong model for Old Style revision codes
"""
Read all GPIO
This version for pigpio daemon; allows remote access
"""
import sys, os, time

__all__ = ['FUNCTION', 'HEADER', 'MODES', 'PiModel', 'print_gpio']

MODES=["IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"]
HEADER = ('3.3v', '5v', 2, '5v', 3, 'GND', 4, 14, 'GND', 15, 17, 18, 27, 'GND', 22, 23, '3.3v', 24, 10, 'GND', 9, 25, 11, 8, 'GND', 7, 0, 1, 5, 'GND', 6, 12, 13, 'GND', 19, 16, 26, 20, 'GND', 21)

FUNCTION = {
'Pull': ('High', 'High', 'High', 'High', 'High', 'High', 'High', 'High', 'High', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low'),
'ALT0': ('SDA0', 'SCL0', 'SDA1', 'SCL1', 'GPCLK0', 'GPCLK1', 'GPCLK2', 'SPI0_CE1_N', 'SPI0_CE0_N', 'SPI0_MISO', 'SPI0_MOSI', 'SPI0_SCLK', 'PWM0', 'PWM1', 'TXD0', 'RXD0', 'FL0', 'FL1', 'PCM_CLK', 'PCM_FS', 'PCM_DIN', 'PCM_DOUT', 'SD0_CLK', 'SD0_XMD', 'SD0_DATO', 'SD0_DAT1', 'SD0_DAT2', 'SD0_DAT3'),
'ALT1': ('SA5', 'SA4', 'SA3', 'SA2', 'SA1', 'SAO', 'SOE_N', 'SWE_N', 'SDO', 'SD1', 'SD2', 'SD3', 'SD4', 'SD5', 'SD6', 'SD7', 'SD8', 'SD9', 'SD10', 'SD11', 'SD12', 'SD13', 'SD14', 'SD15', 'SD16', 'SD17', 'TE0', 'TE1'),
'ALT2': ('PCLK', 'DE', 'LCD_VSYNC', 'LCD_HSYNC', 'DPI_D0', 'DPI_D1', 'DPI_D2', 'DPI_D3', 'DPI_D4', 'DPI_D5', 'DPI_D6', 'DPI_D7', 'DPI_D8', 'DPI_D9', 'DPI_D10', 'DPI_D11', 'DPI_D12', 'DPI_D13', 'DPI_D14', 'DPI_D15', 'DPI_D16', 'DPI_D17', 'DPI_D18', 'DPI_D19', 'DPI_D20', 'DPI_D21', 'DPI_D22', 'DPI_D23'),
'ALT3': ('SPI3_CE0_N', 'SPI3_MISO', 'SPI3_MOSI', 'SPI3_SCLK', 'SPI4_CE0_N', 'SPI4_MISO', 'SPI4_MOSI', 'SPI4_SCLK', '_', '_', '_', '_', 'SPI5_CE0_N', 'SPI5_MISO', 'SPI5_MOSI', 'SPI5_SCLK', 'CTS0', 'RTS0', 'SPI6_CE0_N', 'SPI6_MISO', 'SPI6_MOSI', 'SPI6_SCLK', 'SD1_CLK', 'SD1_CMD', 'SD1_DAT0', 'SD1_DAT1', 'SD1_DAT2', 'SD1_DAT3'),
'ALT4': ('TXD2', 'RXD2', 'CTS2', 'RTS2', 'TXD3', 'RXD3', 'CTS3', 'RTS3', 'TXD4', 'RXD4', 'CTS4', 'RTS4', 'TXD5', 'RXD5', 'CTS5', 'RTS5', 'SPI1_CE2_N', 'SPI1_CE1_N', 'SPI1_CE0_N', 'SPI1_MISO', 'SPIl_MOSI', 'SPI1_SCLK', 'ARM_TRST', 'ARM_RTCK', 'ARM_TDO', 'ARM_TCK', 'ARM_TDI', 'ARM_TMS'),
'ALT5': ('SDA6', 'SCL6', 'SDA3', 'SCL3', 'SDA3', 'SCL3', 'SDA4', 'SCL4', 'SDA4', 'SCL4', 'SDA5', 'SCL5', 'SDA5', 'SCL5', 'TXD1', 'RXD1', 'CTS1', 'RTS1', 'PWM0', 'PWM1', 'GPCLK0', 'GPCLK1', 'SDA6', 'SCL6', 'SPI3_CE1_N', 'SPI4_CE1_N', 'SPI5_CE1_N', 'SPI6_CE1_N')
}

PiModel = {
0: 'A',
1: 'B',
2: 'A+',
3: 'B+',
4: '2B',
6: 'CM1',
8: '3B',
9: 'Zero',
0xa: 'CM3',
0xc: 'ZeroW',
0xd: '3B+',
0xe: '3A+',
0x10: 'CM3+',
0x11: '4B',
0x13: '400',
0x14: 'CM4'
}

RED   = '\033[1;31m'
GREEN = '\033[1;32m'
ORANGE = '\033[1;33m'
BLUE = '\033[1;34m'
LRED = '\033[1;91m'
YELLOW = '\033[1;93m'
RESET = '\033[0;0m'
COL = {
    '3.3v': LRED,
    '5v': RED,
    'GND': GREEN
}

TYPE = 0
rev = 0

def pin_state(g):
    """
    Return "state" of BCM g
    Return is tuple (name, mode, value)
    """
    mode = get_alt(g)
    modename = MODES[mode]
    if(mode<2): # i.e. IN or OUT
        name = 'GPIO{}'.format(g)
    else:
        name = FUNCTION[MODES[mode]][g]
    return name, modename, read_gpio(g)

def print_gpio(pin_state):
    """
    Print listing of Raspberry pins, state & value
    Layout matching Pi 2 row Header
    """
    global TYPE, rev
    GPIOPINS = 40
    try:
        Model = 'Pi ' + PiModel[TYPE]
    except:
        Model = 'Pi ??'
    if rev < 16 :   # older models (pre PiB+)
        GPIOPINS = 26

    print('+-----+------------+------+---+{:^10}+---+------+-----------+-----+'.format(Model) )
    print('| BCM |    Name    | Mode | V |  Board   | V | Mode | Name      | BCM |')
    print('+-----+------------+------+---+----++----+---+------+-----------+-----+')
    for h in range(1, GPIOPINS, 2):
    # odd pin
        hh = HEADER[h-1]
        if(type(hh)==type(1)):
            print('|{0:4} | {1[0]:<10} | {1[1]:<4} | {1[2]} |{2:3} '.format(hh, pin_state(hh), h), end='|| ')
        else:
#             print('|        {:18}   | {:2}'.format(hh, h), end=' || ')    # non-coloured output
            print('|        {}{:18}   | {:2}{}'.format(COL[hh], hh, h, RESET), end=' || ')    # coloured output
    # even pin
        hh = HEADER[h]
        if(type(hh)==type(1)):
            print('{0:2} | {1[2]:<2}| {1[1]:<5}| {1[0]:<10}|{2:4} |'.format(h+1, pin_state(hh), hh))
        else:
#             print('{:2} |             {:9}      |'.format(h+1, hh))    # non-coloured output
            print('{}{:2} |             {:9}{}      |'.format(COL[hh], h+1, hh, RESET))    # coloured output
    print('+-----+------------+------+---+----++----+---+------+-----------+-----+')
    print('| BCM |    Name    | Mode | V |  Board   | V | Mode | Name      | BCM |')
    print('+-----+------------+------+---+{:^10}+---+------+-----------+-----+'.format(Model) )

def main():
    global TYPE, rev
    global get_pullupdn, get_alt, read_gpio
    import pigpio

    if len(sys.argv) > 1:
        pi = pigpio.pi(sys.argv[1])
    else:
        pi = pigpio.pi()

    if not pi.connected:
        sys.exit(1)
    get_alt = pi.get_mode
    read_gpio =  pi.read

    rev = pi.get_hardware_revision()
    TYPE = (rev&0x00000FF0)>>4
    if(rev < 0x15+1):   # Old Style
        MM = [0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 6, 2, 3, 6, 2]
        TYPE = MM[rev]

    print_gpio(pin_state)

if __name__ == '__main__':
    main()

gpioread screen shot

0

I have been playing recently with the gpiochip interface (included in recent kernels) and realised that the raspi-gpio debug tool actually has all the details needed.

raspi-gpio should be included in all recent Raspberry Pi OS, but if not you can install raspi-gpio from the repository

This version of my program (which I call gpioreadall) reformats the output of raspi-gpio into a user-friendly format similar to the WiringPi gpio readall.

This produces output identical to gpioread but is independent of any other libraries/daemons. enter image description here The code is listed below and can be downloaded from GPIOreadall

#! /usr/bin/env python3
# 2021-04-02
# 2021-04-13    Fix Wrong model for Old Style revision codes
"""
Read all GPIO
This version for raspi-gpio debug too
"""
import sys, os, time
import subprocess

MODES=["IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"]
HEADER = ('3.3v', '5v', 2, '5v', 3, 'GND', 4, 14, 'GND', 15, 17, 18, 27, 'GND', 22, 23, '3.3v', 24, 10, 'GND', 9, 25, 11, 8, 'GND', 7, 0, 1, 5, 'GND', 6, 12, 13, 'GND', 19, 16, 26, 20, 'GND', 21)

PiModel = {
0: 'A',
1: 'B',
2: 'A+',
3: 'B+',
4: '2B',
6: 'CM1',
8: '3B',
9: 'Zero',
0xa: 'CM3',
0xc: 'ZeroW',
0xd: '3B+',
0xe: '3A+',
0x10: 'CM3+',
0x11: '4B',
0x13: '400',
0x14: 'CM4'
}

RED   = '\033[1;31m'
GREEN = '\033[1;32m'
ORANGE = '\033[1;33m'
BLUE = '\033[1;34m'
LRED = '\033[1;91m'
YELLOW = '\033[1;93m'
RESET = '\033[0;0m'
COL = {
    '3.3v': LRED,
    '5v': RED,
    'GND': GREEN
}

TYPE = 0
rev = 0

def pin_state(g):
    """
    Return "state" of BCM g
    Return is tuple (name, mode, value)
    """
    result = subprocess.run(['raspi-gpio', 'get', ascii(g)], stdout=subprocess.PIPE).stdout.decode('utf-8')

    D = {}  # Convert output of raspi-gpio get to dict for convenience
    paras = result.split()
    for par in paras[2:] :
        p, v = par.split('=')
        if (v.isdigit()):
            D[p] = int(v)
        else:
            D[p] = v

    if(D['fsel'] < 2): # i.e. IN or OUT
        name = 'GPIO{}'.format(g)
    else:
        name = D['func']

    mode = MODES[D['fsel']]
    if(D['fsel'] == 0 and 'pull' in D):
        if(D['pull'] == 'UP'):
            mode = 'IN ^'
        if(D['pull'] == 'DOWN'):
            mode = 'IN v'

    return name, mode, D['level']

def print_gpio(pin_state):
    """
    Print listing of Raspberry pins, state & value
    Layout matching Pi 2 row Header
    """
    global TYPE, rev
    GPIOPINS = 40
    try:
        Model = 'Pi ' + PiModel[TYPE]
    except:
        Model = 'Pi ??'
    if rev < 16 :   # older models (pre PiB+)
        GPIOPINS = 26

    print('+-----+------------+------+---+{:^10}+---+------+-----------+-----+'.format(Model) )
    print('| BCM |    Name    | Mode | V |  Board   | V | Mode | Name      | BCM |')
    print('+-----+------------+------+---+----++----+---+------+-----------+-----+')

    for h in range(1, GPIOPINS, 2):
    # odd pin
        hh = HEADER[h-1]
        if(type(hh)==type(1)):
            print('|{0:4} | {1[0]:<10} | {1[1]:<4} | {1[2]} |{2:3} '.format(hh, pin_state(hh), h), end='|| ')
        else:
#             print('|        {:18}   | {:2}'.format(hh, h), end=' || ')    # non-coloured output
            print('|        {}{:18}   | {:2}{}'.format(COL[hh], hh, h, RESET), end=' || ')    # coloured output
    # even pin
        hh = HEADER[h]
        if(type(hh)==type(1)):
            print('{0:2} | {1[2]:<2}| {1[1]:<5}| {1[0]:<10}|{2:4} |'.format(h+1, pin_state(hh), hh))
        else:
#             print('{:2} |             {:9}      |'.format(h+1, hh))    # non-coloured output
            print('{}{:2} |             {:9}{}      |'.format(COL[hh], h+1, hh, RESET))    # coloured output
    print('+-----+------------+------+---+----++----+---+------+-----------+-----+')
    print('| BCM |    Name    | Mode | V |  Board   | V | Mode | Name      | BCM |')
    print('+-----+------------+------+---+{:^10}+---+------+-----------+-----+'.format(Model) )

def get_hardware_revision():
    """
    Returns the Pi's hardware revision number.
    """
    with open('/proc/cpuinfo', 'r') as f:
        for line in f.readlines():
            if 'Revision' in line:
                REV = line.split(':')[1]
                REV = REV.strip()   # Revision as string
                return int(REV, base=16)

def main():
    global TYPE, rev
    rev = get_hardware_revision()
    TYPE = (rev&0x00000FF0)>>4
    if(rev < 0x15+1):   # Old Style
        MM = [0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 6, 2, 3, 6, 2]
        TYPE = MM[rev] # Map Old Style revision to TYPE

    print_gpio(pin_state)

if __name__ == '__main__':
    main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.