4

As I understand it, the mode of a GPIO pin can be INPUT or OUTPUT (and I think there may be other modes such as PWM and I2C). I believe that the mode of a pin is a global and that if two applications attempt to set the mode, the last one called will be the actual final value.

My goal is to determine a pin's mode before attempting to set it or use it. This will allow me to detect potential conflicts.

Is there a way to determine the mode for a given pin? My programming library of choice is wiringPi but I am interested in any solution in this area just now.

1
  • 1
    fwiw, @vir down below stated getAlt(int pin), which I just found that most definitely is essentially getMode(), just named poorly
    – stevieb
    Aug 11, 2016 at 0:26

3 Answers 3

2

I haven't used wiringPi but glancing through the core reference, it bizarrely seems there is no such method, and looking at the wiringPi.h header perhaps confirms that. [Edit: See Vir's answer]

If you use the kernel interface in /sys/class, you can get the state with:

cat /sys/class/gpio/gpioN/direction

Which will be either "in" or "out". I've done this in C with open()/read(), obviously it is very straightforward, but I believe it does require you to first export the pin using that interface spec. I am sure it is not nearly as efficient as the pi-specific mmap() libraries such wiringPi, pigpio, or libbcm2835, but it almost certainly could be used in combination with them if you need that kind of efficiency.

4

Yes you can. Use getAlt(int pin) method for that from libwiringPi.so (or wiringPi.h). It will give you direction of that pin.

1
  • This is absolutely correct. I'm writing a Perl interface for wiringPi, and struggled to find out how to do this. getAlt() does so. Why it's not called getMode() is beyond me ;)
    – stevieb
    Aug 11, 2016 at 0:25
3

Yes, you can determine the current mode of a gpio.

You'd have to search the wiringPi documentation and source code to see if a method is implemented by wiringPi.

My pigpio library provides the C I/F gpioGetMode function as well as Python and command line methods.

For example code see Minimal gpio access.

int gpioGetMode(unsigned gpio)
{
   int reg, shift;

   reg   =  gpio/10;
   shift = (gpio%10) * 3;

   return (*(gpioReg + reg) >> shift) & 7;
}

EDITED TO ADD

Each gpio may be independently set to one of eight modes. Not all modes perform a useful function. Some modes are used for debugging and may generate very high frequency signals on the gpio.

The modes are named INPUT, OUTPUT, ALT0, ALT1, ALT2, ALT3, ALT4, and ALT5.

The machine values to set the mode (and the value returned by get mode) are

INPUT  0
OUTPUT 1
ALT0   4
ALT1   5
ALT2   6
ALT3   7
ALT4   3
ALT5   2

Note that there is no natural connection between mode and value.

For details of the permissible modes see BCM2835 ARM Peripherals page 102.

1
  • 1
    Joan ... the MOST important thing I got out of this post was the knowledge of the existence of pigpio. I'm still early on the learning curve here and will now be pouring over your docs in detail.
    – Kolban
    Apr 18, 2015 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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