I have written a program in C to take over the Pi pins and reset their mode input / output on a Pi4. I had to update the wiringPi library ( details in the code comments).
I was able to set the selected pins to input or output as required and when setting the pins to output was able to set high or low. However when setting as an input pin, I was not able to set the mode for pull up or pull down or tristate.
I tried using the command line interface with commands of:
gpio mode 8 out
gpio mode 8 in
Which worked. Pin 8 was always in pull up mode when configured as input.
I tried:
gpio mode 8 down,
gpio mode 8 up
gpio mode tri
none of these made any difference
Please advise why this is not working and what I need to do to be able to set the pull up/down/tristate mode.
Code is:
/*
wiringpi for Pi 4:
download latest version:
cd /tmp
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
display wiring table on pi:
gpio readall
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
| 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 | IN | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | IN | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | IN | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | IN | 1 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | ALT0 | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | ALT0 | 0 | 21 || 22 | 1 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | OUT | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
PinMode:
http://wiringpi.com/reference/core-functions/
void pinMode (int pin, int mode) ; mode: INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK
NOTE: only pin 1 // 12, supports PWM
pin 7 // 7, supports CLOCK output
This function has no effect when in Sys mode
void pullUpDnControl (int pin, int pud) ; should be set as input
pud:
PUD_OFF, (no pull up/down)
PUD_DOWN (pull to ground) approximately 50KO
PUD_UP (pull to 3.3v) approximately 50KO
This function has no effect on the Raspberry Pi’s GPIO pins when in Sys mode.
If you need to activate a pull-up/pull-down, then you can do it with the gpio
program in a script before you start your program.
void digitalWrite (int pin, int value) ;
Writes the value HIGH or LOW (1 or 0) to the given pin which must have been previously set as an output.
WiringPi treats any non-zero number as HIGH, however 0 is the only representation of LOW.
void pwmWrite (int pin, int value) ;
Writes the value to the PWM register for the given pin. The Raspberry Pi has
one on-board PWM pin, pin 1 (BMC_GPIO 18, Phys 12) and the range is 0-1024.
This function is not able to control the Pi’s on-board PWM when in Sys mode.
int digitalRead (int pin) ;
This function returns the value read at the given pin. It will be HIGH or LOW (1 or 0)
depending on the logic level at the pin.
See also:
http://wiringpi.com/the-gpio-utility/
https://pinout.xyz/pinout/gpclk
https://www.raspberrypi.org/forums/viewtopic.php?t=22274
*/
#include <wiringPi.h>
// Pi Pin definitions:
#define S91 8 // 3
#define S92 9 // 5
#define S2 30 // 27
#define S3 31 // 28
int main( void) {
wiringPiSetup();
pinMode(S91, INPUT);
pinMode(S92, INPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pullUpDnControl (S91, PUD_DOWN) ;
pullUpDnControl (S92, PUD_DOWN) ;
while (1) {
if ( digitalRead(S91 )) {
digitalWrite(S3, HIGH);
delay(100);
digitalWrite(S3, LOW);
delay(100);
} else {
digitalWrite(S3, LOW);
delay(100);
}
}
return 0;
}
/*
gcc toggle2.c -o toggle2 -lwringPi
sudo ./toggle
*/