Разобрался после долгих экспирементов, gpio нумеруются 0 gpio -> это банк 0 пин 0; 32 gpio -> это банк 1 пин 0 и тд.
Если ядро поддерживает gpio ио к ним можно и лучше обращаться через sysfs. Набросал немного кода, работает может кому сгодится.
h
Код
#define SIZENAM 255
enum pinstatus {PIN_OFF,PIN_ON};
enum pindirection{PIN_IN,PIN_OUT};
int gpioexp(int pin_num,pindirection pd,pinstatus st);
int gpioini();
int gpioset(int pin_num,pinstatus ps);
pinstatus gpioget(int pin_num);
cpp
CODE
#include <stdio.h> /* For printf() */
#include <string.h> /* For strerror() */
#include <unistd.h> /* For read(), close() */
#include <fcntl.h> /* For open() */
#include <errno.h> /* For errno */
#include <sys/poll.h> /* For poll() */
#include <time.h>
#include <stdlib.h>
#include "gpio.h"
/*-------------------------------------------------------------------------------------------------------*/
int gpioexp(int pin_num,pindirection pd,pinstatus st){
FILE * fp = NULL;
char gpio_file_name[SIZENAM];
if ((fp = fopen("/sys/class/gpio/export", "ab")) != NULL)
{
rewind(fp);
fprintf(fp,"%d",pin_num);
fclose(fp);
}else{
printf("Cannot open GPIO value for %s\n","/sys/class/gpio/export");
return -1;
}
memset(gpio_file_name,0,SIZENAM);
sprintf(gpio_file_name,"/sys/class/gpio/gpio%d/direction",pin_num);
if ((fp = fopen(gpio_file_name, "rb+")) != NULL)
{
rewind(fp);
switch(pd){
case PIN_IN:{
fprintf(fp,"in");
break;
}//case
case PIN_OUT:{
fprintf(fp,"out");
break;
}//case
default:break;
}//switch
fclose(fp);
if(pd!= PIN_IN){
memset(gpio_file_name,0,SIZENAM);
sprintf(gpio_file_name,"/sys/class/gpio/gpio%d/value",pin_num);
if ((fp = fopen(gpio_file_name, "rb+")) != NULL)
{
switch(st){
case PIN_ON:{
fprintf(fp,"1");
break;
}//case
case PIN_OFF:{
fprintf(fp,"0");
break;
}//case
default:break;
}//switch
fclose(fp);
}
}
}
return 0;
}
/*-------------------------------------------------------------------------------------------------------*/
int gpioini(){
/* pin out */
gpioexp(32,PIN_OUT,PIN_OFF);
gpioexp(39,PIN_OUT,PIN_OFF);
/* pin in */
gpioexp(23,PIN_IN,PIN_OFF);
return 0;
}
/*-------------------------------------------------------------------------------------------------------*/
int gpioset(int pin_num,pinstatus ps){
FILE * fp = NULL;
char gpio_file_name[SIZENAM];
memset(gpio_file_name,0,SIZENAM);
sprintf(gpio_file_name,"/sys/class/gpio/gpio%d/value",pin_num);
if ((fp = fopen(gpio_file_name, "rb+")) != NULL)
{
switch(ps){
case PIN_ON:{
fprintf(fp,"1");
break;
}//case
case PIN_OFF:{
fprintf(fp,"0");
break;
}//case
default:break;
}//switch
fclose(fp);
}
return 0;
}
/*-------------------------------------------------------------------------------------------------------*/
pinstatus gpioget(int pin_num){
FILE * fp = NULL;
char gpio_file_name[SIZENAM];
long lSize;
char * buffer;
size_t result;
int temm =0;
pinstatus ret = PIN_OFF;
memset(gpio_file_name,0,SIZENAM);
sprintf(gpio_file_name,"/sys/class/gpio/gpio%d/value",pin_num);
if ((fp = fopen(gpio_file_name, "rb+")) != NULL)
{
fseek (fp , 0 , SEEK_END);
lSize = ftell (fp);
rewind (fp);
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {
printf("Memory error");
}else{
result = fread (buffer,1,lSize,fp);
temm = atoi(buffer);
switch(temm){
case 0:{
ret = PIN_OFF;;
break;
}//case
case 1:{
ret = PIN_ON;
break;
}//case
default:break;
}//switch
}
free (buffer);
fclose(fp);
}
return ret;
}
/*-------------------------------------------------------------------------------------------------------*/
Есть еще одна проблема, после перезагрузки часы слетают, даже если есть батарея. Может у кого была похожая проблема?