#define SYSFS_GPIO_RST_PIN_113 "113"
#define SYSFS_GPIO_RST_DIR_113 "/sys/class/gpio/gpio113/direction"
#define SYSFS_GPIO_RST_VAL_113 "/sys/class/gpio/gpio113/value"
#define SYSFS_GPIO_RST_PIN_114 "114"
#define SYSFS_GPIO_RST_DIR_114 "/sys/class/gpio/gpio114/direction"
#define SYSFS_GPIO_RST_VAL_114 "/sys/class/gpio/gpio114/value"
#define SYSFS_GPIO_EXPORT "/sys/class/gpio/export"
#define SYSFS_GPIO_RST_DIR_OUT "out"
#define SYSFS_GPIO_RST_DIR_IN "in"
#define SYSFS_GPIO_RST_VAL_H "1"
#define SYSFS_GPIO_RST_VAL_L "0"
int GPIO_Init(void)
{
int fd;
char acStr[3] = {0};
fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);
if(fd == -1)
{
printf("ERR: Radio hard reset pin open error.\n");
return EXIT_FAILURE;
}
write(fd, SYSFS_GPIO_RST_PIN_113 ,sizeof(SYSFS_GPIO_RST_PIN_113));
write(fd, SYSFS_GPIO_RST_PIN_114 ,sizeof(SYSFS_GPIO_RST_PIN_114));
close(fd);
//设置端口方向
fd = open(SYSFS_GPIO_RST_DIR_113, O_RDWR);
if(fd == -1)
{
printf("ERR: Radio hard reset pin direction open error.\n");
return EXIT_FAILURE;
}
write(fd, SYSFS_GPIO_RST_DIR_OUT, sizeof(SYSFS_GPIO_RST_DIR_OUT));
if(read(fd, acStr, 3) < 0)
{
printf("[errno=%d][errno=%s]\n", errno, strerror(errno));
return 0;
}
while(0 != strcmp(acStr, SYSFS_GPIO_RST_DIR_OUT))
{
write(fd, SYSFS_GPIO_RST_DIR_OUT, sizeof(SYSFS_GPIO_RST_DIR_OUT));
sleep(1);
if(read(fd, acStr, 3) < 0)
{
printf("12[errno=%d][errno=%s]\n", errno, strerror(errno));
//return 0;
}
printf("[113acStr=%s].\n",acStr);
sleep(5);
}
close(fd);
//设置端口方向
if(fd = open(SYSFS_GPIO_RST_DIR_114, O_RDWR) == -1)
{
printf("ERR: Radio hard reset pin direction open error.\n");
return EXIT_FAILURE;
}
write(fd, SYSFS_GPIO_RST_DIR_OUT, sizeof(SYSFS_GPIO_RST_DIR_OUT));
if(read(fd, acStr, 3) < 0)
{
printf("[errno=%d][errno=%s]\n", errno, strerror(errno));
return 0;
}
while(0 != strcmp(acStr, SYSFS_GPIO_RST_DIR_OUT))
{
write(fd, SYSFS_GPIO_RST_DIR_OUT, sizeof(SYSFS_GPIO_RST_DIR_OUT));
if(read(fd, acStr, 3) < 0)
{
printf("[errno=%d][errno=%s]\n", errno, strerror(errno));
return 0;
}
printf("[114acStr=%s].\n",acStr);
sleep(5);
}
close(fd);
return 0;
}
int GPIO_SetDirection(char *pcPinType, char *pcDirection)
{
int fd;
if(NULL == pcPinType || NULL == pcDirection)
{
printf("ERR: GPIO_SetDirection para is error.\n");
return 1;
}
if(0 == strcmp(pcPinType, SYSFS_GPIO_RST_DIR_113))
{
//设置端口方向/sys/class/gpio/gpio113
fd = stOperFd.fdDirectoin113;
}
else if(0 == strcmp(pcPinType, SYSFS_GPIO_RST_DIR_114))
{
//设置端口方向/sys/class/gpio/gpio114
fd = stOperFd.fdDirectoin114;
}
else
{
printf("ERR: Radio hard reset pin direction open error.\n");
return 1;
}
if(0 == strcmp(pcDirection, SYSFS_GPIO_RST_DIR_OUT))//echo out > direction
{
write(fd, SYSFS_GPIO_RST_DIR_OUT, sizeof(SYSFS_GPIO_RST_DIR_OUT));
}
else if(0 == strcmp(pcDirection, SYSFS_GPIO_RST_DIR_IN))//echo in > direction
{
write(fd, SYSFS_GPIO_RST_DIR_IN, sizeof(SYSFS_GPIO_RST_DIR_IN));
}
else
{
printf("ERR: GPIO_SetDirection para pcDirection=%s.\n",pcDirection);
return 1;
}
return 0;
}
int GPIO_WRITE_BIT(char *pacType, unsigned char ucVal)
{
int fd;
if(NULL == pacType)
{
printf("Input is NULL\n");
return 1;
}
if(0 == strcmp(pacType, SYSFS_GPIO_RST_VAL_113))
{
GPIO_SetDirection(SYSFS_GPIO_RST_DIR_113, SYSFS_GPIO_RST_DIR_OUT);
fd = stOperFd.fdValue113;
}
else if(0 == strcmp(pacType, SYSFS_GPIO_RST_VAL_114))
{
GPIO_SetDirection(SYSFS_GPIO_RST_DIR_114, SYSFS_GPIO_RST_DIR_OUT);
fd = stOperFd.fdValue114;
}
else
{
printf("ERR: Radio hard reset pin value open error.\n");
return 1;
}
if(1 == ucVal)
{
write(fd, SYSFS_GPIO_RST_VAL_H, 2);
}
else if(0 == ucVal)
{
write(fd, SYSFS_GPIO_RST_VAL_L, 2);
}
else
{
printf("val err is %d\n", ucVal);
}
}
int GPIO_READ_IDB(char *pacType)
{
int fd;
char acStr[3] = {0};
if(NULL == pacType)
{
printf("Input is NULL\n");
return 0;
}
if(0 == strcmp(pacType, SYSFS_GPIO_RST_VAL_113))
{
GPIO_SetDirection(SYSFS_GPIO_RST_DIR_113, SYSFS_GPIO_RST_DIR_IN);
fd = stOperFd.fdValue113;
}
else if(0 == strcmp(pacType, SYSFS_GPIO_RST_VAL_114))
{
GPIO_SetDirection(SYSFS_GPIO_RST_DIR_114, SYSFS_GPIO_RST_DIR_IN);
fd = stOperFd.fdValue114;
}
else
{
printf("ERR: Radio hard reset pin value open error.\n");
return 0;
}
if(read(fd, acStr, 3) < 0)
{
printf("[errno=%d][errno=%s]\n", errno, strerror(errno));
return 0;
}
printf("ERR: [acStr=%s].\n",acStr);
return (atoi(acStr));
}