jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Is Sys() mode broken in wiringPi?

Thu Apr 20, 2017 7:11 am

Using the latest Raspbian on a Raspberry Pi 2+, programming in C++.

I've been trying to read a couple of GPIO pins for hours now, and it just won't do right.

The pins were identified by number on the Adafruit Kippah (the adapter for the 40-pin parallel dotclock displays.)

First, I didn't know whether the numbers identified (23, 25, 27) were BCM or WPi numbers, but I've established they are BCM numbers.

Then, I tried to export them by calling system("gpio -g export 27 in") BUT I can't get this to work. When passing the "-g" option, the "gpio" command says "gpio: Unknown command: export." which is ... confusing. Instead, I have to do the equivalent "gpio export 2 in" to get the pin exported.
However, I can turn on pull-ups using the -g option: system("gpio -g mode 27 up") works just fine!

Then, after calling wiringPiSetupSy(), I'm trying to read this pin, but it returns 0 all the time.
The documentation says the pin numbers are always BCM so I use digitalRead(27) but that returns 0.
Meanwhile, if in another terminal I call "gpio -g read 27" it returns the right value. 1 by default, 0 if I ground the pin, very repeatable.
I've also tried digitalRead(2), on the guess that perhaps the documentation was wrong, but that returns 1 all the time, even when grounding the pin.
So, I can't find a relation between the return value of digitalRead() and the corresponding gpio read command, which is super frustrating.

Reading the value out of the GPIO sysfs plugin also does not work right -- it shows 1 all the time (lke digitalRead on pin 2)

Perhaps the problem is that gpio export always takes BCM pin numbers, and then thinks it's an error if I explicitly tell it to use them with "-g" ?

Color me confused. This seems to not work as documented.

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

Re: Is Sys() mode broken in wiringPi?

Thu Apr 20, 2017 7:17 am

It may be helpful if you give the code and build instructions for a really short example demonstrating the problem. Then others could run the example and check or point out what you are doing wrong.

jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Re: Is Sys() mode broken in wiringPi?

Thu Apr 20, 2017 7:53 am

My conclusion is: digitalWrite() is broken?

I can write with gpio write, but I can't write with digitalWrite(). Doesn't matter if I use the BCM or GPIO number, it still doesn't work. (I presume I'm supposed to use BCM, but I tried both.)

Writing my own replacement works fine:

Code: Select all

void pinwrite(int pin, int val) {
    char buf[123];
    sprintf(buf, "/sys/class/gpio/gpio%d/value", pin);
    int fd = ::open(buf, O_RDWR);
    if (fd >= 0) {
        sprintf(buf, "%d\n", val);
        write(fd, buf, strlen(buf));
        ::close(fd);
    } else {
        perror(buf);
    }
}

jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Re: Is Sys() mode broken in wiringPi?

Thu Apr 20, 2017 5:39 pm

I don't get it. according to docs, gpio takes WiringPi numbers, unless the -g option is used.
This means I need to export "gpio2" to use BCM pin 27.
However, the value of this pin 2 doesn't match the value of the BCM pin 27. (This is on a 2+)

Code: Select all

pi@raspberrypi:~/robot_software/controller $ gpio unexportall
pi@raspberrypi:~/robot_software/controller $ gpio export 2 in
pi@raspberrypi:~/robot_software/controller $ gpio -g mode 27 in
pi@raspberrypi:~/robot_software/controller $ gpio -g mode 27 up
pi@raspberrypi:~/robot_software/controller $ gpio -g read 27
1
pi@raspberrypi:~/robot_software/controller $ cat /sys/class/gpio/gpio2/value
1
pi@raspberrypi:~/robot_software/controller $ gpio -g mode 27 down
pi@raspberrypi:~/robot_software/controller $ gpio -g read 27
0
pi@raspberrypi:~/robot_software/controller $ cat /sys/class/gpio/gpio2/value
1
pi@raspberrypi:~/robot_software/controller $ gpio readall
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 | ALT2 | 0 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 0 |  7 || 8  | 0 | ALT2 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT2 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 | ALT2 | 1 | 11 || 12 | 1 | ALT2 | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 | ALT2 | 0 | 15 || 16 | 0 | OUT  | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | ALT2 | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT2 | 1 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT2 | 1 | 21 || 22 | 0 | ALT2 | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT2 | 0 | 23 || 24 | 1 | ALT2 | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | ALT2 | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 | ALT2 | 1 | 27 || 28 | 0 | ALT2 | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 | ALT2 | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 0 | 31 || 32 | 1 | ALT2 | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 | ALT2 | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 | ALT2 | 1 | 35 || 36 | 1 | ALT2 | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 | ALT2 | 0 | 37 || 38 | 1 | ALT2 | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | ALT2 | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
This is another problem:

Code: Select all

pi@raspberrypi:~/robot_software/controller $ gpio -g export 27 in
gpio: Unknown command: export.
But it seems like the "export" part of the GPIO tool always uses the BCM numbering?

Code: Select all

pi@raspberrypi:~/robot_software/controller $ gpio export 27 in
pi@raspberrypi:~/robot_software/controller $ cat /sys/class/gpio/gpio27/value
0
pi@raspberrypi:~/robot_software/controller $ gpio -g mode 27 up
pi@raspberrypi:~/robot_software/controller $ cat /sys/class/gpio/gpio27/value
1
pi@raspberrypi:~/robot_software/controller $ gpio -g mode 27 down
pi@raspberrypi:~/robot_software/controller $ cat /sys/class/gpio/gpio27/value
0
pi@raspberrypi:~/robot_software/controller $ cat /sys/class/gpio/gpio2/value
1
pi@raspberrypi:~/robot_software/controller $
However, "mode" does not; it requires -g:

Code: Select all

pi@raspberrypi:~/robot_software/controller $ gpio mode 27 up
pi@raspberrypi:~/robot_software/controller $ cat /sys/class/gpio/gpio27/value
0
pi@raspberrypi:~/robot_software/controller $ gpio -g mode 27 up
pi@raspberrypi:~/robot_software/controller $ cat /sys/class/gpio/gpio27/value
1
This matches the expectation that "non-G" GPIO pin numbers:

Code: Select all

pi@raspberrypi:~/robot_software/controller $ gpio mode 2 down
pi@raspberrypi:~/robot_software/controller $ cat /sys/class/gpio/gpio27/value
0
pi@raspberrypi:~/robot_software/controller $ gpio mode 2 up
pi@raspberrypi:~/robot_software/controller $ cat /sys/class/gpio/gpio27/value
1
But, wait -- /sys/class/gpio/gpio2/value uses the wiringPi number, but /sys/class/gpio/gpio27/value uses the BCM number for the same pin!
WHAAAAT!??!?!?

I may need to just give my program root and use the "standard" wiringPi interface instead. I can't make sense of this behavior.

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

Re: Is Sys() mode broken in wiringPi?

Fri Apr 21, 2017 8:28 am

Have you got WiringPi 2.44?

Code: Select all

root@viking ~ # gpio -v
gpio version: 2.44
Copyright (c) 2012-2017 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Model A+, Revision: 01, Memory: 256MB, Maker: Sony
  * Device tree is enabled.
  *--> Raspberry Pi Model A Plus Rev 1.1
  * This Raspberry Pi supports user-level GPIO access.
root@viking ~ #
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 “Troubleshooting”