was running some test code and now it doesn't work anymore. I can't set the GPIO pins from high to low with bcm2835_gpio_write
It still works if I go through command line with
sudo -s
sudo echo "17" > /sys/class/gpio/export
sudo echo "out" > /sys/class/gpio/gpio17/direction
cat /sys/class/gpio/gpio17/value
echo "1" > /sys/class/gpio/gpio17/value
echo "0" > /sys/class/gpio/gpio17/value
sudo echo "17" > /sys/class/gpio/unexport
Is there any way to reset the pins somehow? Or is something locking them from my app. Here is a test app I am using to set all of the pins to 1 or 0 and display the value.
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bcm2835.h>
#include <unistd.h>
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
int allOn = 1;
if (strcmp(argv[1], "ON") == 0) allOn = 1;
if (strcmp(argv[1], "OFF") == 0) allOn = 0;
int pins[17] = {0,1,4,7,8,9,10,11,14,15,17,18,21,22,23,24,25};
int i;
int val;
for (i=0;i<17;i++)
{
if (allOn)
{
printf("Turning on %i\n", pins[i]);
bcm2835_gpio_write(pins[i], HIGH);
if (bcm2835_gpio_lev(pins[i]) == HIGH)
printf("Pin Value = ON\n");
else
printf("Pin Value = OFF\n");
}
else
{
printf("Turning off %i\n", pins[i]);
bcm2835_gpio_write(pins[i], LOW);
if (bcm2835_gpio_lev(pins[i]) == HIGH)
printf("Pin Value = ON\n");
else
printf("Pin Value = OFF\n");
}
delay(1000);
}
}
GPIO Pins Problem
8 posts
- Posts: 6
- Joined: Thu Oct 18, 2012 1:12 am
It look OK. What output do you get to the terminal?
Some pins were HIGH and some LOW, but the calls didn't change any of them except pin 14 it was the only one that would change. I will post the exact pin values when I get home.
- Posts: 6
- Joined: Thu Oct 18, 2012 1:12 am
Hello,
I think, your pins are not initialized in output mode.
You can try to add in your program a call to bcm2835_gpio_fsel
for each pin to configuret it to output.
Visibly some pins (14, 16, ...) are already configured to output.
Philippe
I think, your pins are not initialized in output mode.
You can try to add in your program a call to bcm2835_gpio_fsel
for each pin to configuret it to output.
Visibly some pins (14, 16, ...) are already configured to output.
Philippe
- Posts: 92
- Joined: Wed Sep 12, 2012 8:10 am
- Location: Paris
I will try that when I get home.
But I noticed it when running the code for this.
http://learn.adafruit.com/dht-humidity- ... cs-logging
It was working and then it stopped. It gets stuck in an infinite loop waiting for the pin to drop.
But I noticed it when running the code for this.
http://learn.adafruit.com/dht-humidity- ... cs-logging
It was working and then it stopped. It gets stuck in an infinite loop waiting for the pin to drop.
- Posts: 6
- Joined: Thu Oct 18, 2012 1:12 am
Fyi,
I've found a great util for viewing the gpio_fsel status : http://www.susa.net/wordpress/2012/07/raspberry-pi-gpfsel-gpio-and-pads-status-viewer/
I've found a great util for viewing the gpio_fsel status : http://www.susa.net/wordpress/2012/07/raspberry-pi-gpfsel-gpio-and-pads-status-viewer/
- Posts: 99
- Joined: Sat Sep 08, 2012 1:59 pm
OK, I added bcm2835_gpio_fsel(pins[i], BCM2835_GPIO_FSEL_OUTP); to my On/Off code and that made it work.
However the temperature check code I was using still gets stuck in an infinite loop. It uses the bcm2835_gpio_fsel but gets stuck in the "wait for pin to drop" section. It was working a few days ago.
int readDHT(int type, int pin, float *retTemp) {
int counter = 0;
int laststate = HIGH;
int j=0;
// Set GPIO pin to output
printf("Set GPIO pin to output\n");
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(pin, HIGH);
usleep(500000); // 500 ms
bcm2835_gpio_write(pin, LOW);
usleep(20000);
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
// wait for pin to drop
printf("wait for pin to drop?\n");
while (bcm2835_gpio_lev(pin) == 1) {
usleep(1);
}
However the temperature check code I was using still gets stuck in an infinite loop. It uses the bcm2835_gpio_fsel but gets stuck in the "wait for pin to drop" section. It was working a few days ago.
int readDHT(int type, int pin, float *retTemp) {
int counter = 0;
int laststate = HIGH;
int j=0;
// Set GPIO pin to output
printf("Set GPIO pin to output\n");
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(pin, HIGH);
usleep(500000); // 500 ms
bcm2835_gpio_write(pin, LOW);
usleep(20000);
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
// wait for pin to drop
printf("wait for pin to drop?\n");
while (bcm2835_gpio_lev(pin) == 1) {
usleep(1);
}
- Posts: 6
- Joined: Thu Oct 18, 2012 1:12 am
GOT IT. Thanks for the help everyone.
I had my wiring a little off, so by calling
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
and setting my pin to the input, my pin was reading a 1 from the wire.
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
and setting my pin to the input, my pin was reading a 1 from the wire.
- Posts: 6
- Joined: Thu Oct 18, 2012 1:12 am