When I tried to run software that reads GPIO (P9.15) with BeagleBoneBlack REV.C3 under Debian 7.11, it always reads “0”.
On the other hand, BeagleBoneBlack REV.C2 + Debian7.11 works fine.
The official PCN for rev. C3 states “CUSTOMER ACTION REQUIRED: No actions.”
If anyone has any information on this issue, please give me some information.
The test code I am using is as follows.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
typedef enum GPIO_DIRECTION_t {
IN,
OUT
} GPIO_DIRECTION_t;
typedef enum GPIO_ACTIVE_TRIGGER_t {
RISING,
FALLING,
BOTH
} GPIO_ACTIVE_TRIGGER_t;
typedef struct GPIO_t {
int num;
GPIO_DIRECTION_t dir;
GPIO_ACTIVE_TRIGGER_t trigger;
int value_fd;
} GPIO_t;
int gpio_open(GPIO_t * gpio, int num, GPIO_DIRECTION_t dir)
{
int fd;
FILE *fp;
char file_path[100];
fp = fopen("/sys/class/gpio/export", "w");
if(fp == NULL) {
return -1;
}
fprintf(fp, "%d", num);
gpio->num = num;
fclose(fp);
sprintf(file_path, "/sys/class/gpio/gpio%d/direction", num);
fp = fopen(file_path, "w");
if(fp == NULL) {
return -2;
}
if(dir == IN) {
fprintf(fp, "%s", "in");
}
else {
fprintf(fp, "%s", "out");
}
gpio->dir = dir;
fclose(fp);
sprintf(file_path, "/sys/class/gpio/gpio%d/value", num);
fd = open(file_path, O_RDWR | O_SYNC);
if(fd < 0) {
return -3;
}
gpio->value_fd = fd;
return 0;
}
int gpio_close(GPIO_t * gpio)
{
FILE *fp;
close(gpio->value_fd);
fp = fopen("/sys/class/gpio/unexport", "w");
if(fp == NULL) {
return -1;
}
fprintf(fp, "%d", gpio->num);
fclose(fp);
return 0;
}
int gpio_out_value(GPIO_t * gpio, int value)
{
if(gpio->dir != OUT) {
return -1;
}
if(value == 0) {
write(gpio->value_fd, "0", 1);
}
else {
write(gpio->value_fd, "1", 1);
}
lseek(gpio->value_fd, 0, SEEK_SET);
return 0;
}
int gpio_in_value(GPIO_t * gpio, int * value)
{
char buff[100];
if(gpio->dir != IN) {
return -1;
}
read(gpio->value_fd, buff, 100);
lseek(gpio->value_fd, 0, SEEK_SET);
*value = atoi(buff);
return 0;
}
/****************************************************************
* Main
****************************************************************/
int main(void)
{
int result;
int value;
GPIO_t MYPORT;
result = gpio_open(&MYPORT, 48, IN);
printf("GPIO P9_15 open : %d\n", result);
gpio_in_value(&MYPORT, &value); // <-- *always read "0" in REV.C3*
printf("P9.15 = %d\n", value);
}