Linux GPIO控制



#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));
}























 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值