海思Hi3516A在用户态GPIO操作

此操作示例在用户态下实现对GPIO的读写操作。

 

将要操作的GPIO编号export:

        fp = fopen("/sys/class/gpio/export", "w");

       fprintf(fp, "%d", gpio_num);

      fclose(fp);

1)每组GPIO有8个GPIO管脚。

2)参数gpio_num为要操作的GPIO编号,该编号等于GPIO组号 * 8 + 组内偏移号,例如GPIO3_2的编号为3 * 8 + 2 = 26。

 

设置GPIO方向:

                         fp = fopen("/sys/class/gpio/gpio%d/direction", "rb+");

对于输入:fprintf(fp, "in");

对于输出:fprintf(fp, "out");

                          fclose(fp);

 

查看GPIO输入值或设置GPIO输出值:

                         fp = fopen("/sys/class/gpio/gpio%d/value", "rb+");

查看输入值:fread(buf, sizeof(char), sizeof(buf) - 1, fp);

输出低:

                          strcpy(buf,"0");

                         fwrite(buf, sizeof(char), sizeof(buf) - 1, fp);

输出高:

                           strcpy(buf,"1");

                          fwrite(buf, sizeof(char), sizeof(buf) - 1, fp);

 

将操作的GPIO编号unexport:

                          fp = fopen("/sys/class/gpio/unexport", "w");

                           fprintf(fp, "%d", gpio_num);

                           fclose(fp);

 

代码示例:

#include 
#include 
#include 
#include 

int gpio_test_in(unsigned int gpio_chip_num, unsigned int gpio_offset_num)
{
        FILE *fp;
        char file_name[50];
        unsigned char buf[10];
        unsigned int gpio_num;

        gpio_num = gpio_chip_num * 8 + gpio_offset_num;

        sprintf(file_name, "/sys/class/gpio/export");
        fp = fopen(file_name, "w");
        if (fp == NULL) {
                printf("Cannot open %s.\n", file_name);
                return -1;
        }
        fprintf(fp, "%d", gpio_num);
        fclose(fp);

        sprintf(file_name, "/sys/class/gpio/gpio%d/direction", gpio_num);
        fp = fopen(file_name, "rb+");
        if (fp == NULL) {
                printf("Cannot open %s.\n", file_name);
                return -1;
        }
        fprintf(fp, "in");
        fclose(fp);

        sprintf(file_name, "/sys/class/gpio/gpio%d/value", gpio_num);
        fp = fopen(file_name, "rb+");
        if (fp == NULL) {
                printf("Cannot open %s.\n", file_name);
                return -1;
        }
        memset(buf, 0, 10);
        fread(buf, sizeof(char), sizeof(buf) - 1, fp);
        printf("%s: gpio%d_%d = %d\n", __func__,
                        gpio_chip_num, gpio_offset_num, buf[0]-48);
       fclose(fp);
        sprintf(file_name, "/sys/class/gpio/unexport");
        fp = fopen(file_name, "w");
        if (fp == NULL) {
                printf("Cannot open %s.\n", file_name);
                return -1;
        }
        fprintf(fp, "%d", gpio_num);
        fclose(fp);

        return (int)(buf[0]-48);
}

int gpio_test_out(unsigned int gpio_chip_num, unsigned int gpio_offset_num,
                unsigned int gpio_out_val)
{
        FILE *fp;
        char file_name[50];
        unsigned char buf[10];
        unsigned int gpio_num;

        gpio_num = gpio_chip_num * 8 + gpio_offset_num;

        sprintf(file_name, "/sys/class/gpio/export");
        fp = fopen(file_name, "w");
        if (fp == NULL) {
                printf("Cannot open %s.\n", file_name);
                return -1;
        }
        fprintf(fp, "%d", gpio_num);
        fclose(fp);

        sprintf(file_name, "/sys/class/gpio/gpio%d/direction", gpio_num);
        fp = fopen(file_name, "rb+");
        if (fp == NULL) {
                printf("Cannot open %s.\n", file_name);
                return -1;
        }
        fprintf(fp, "out");
        fclose(fp);

        sprintf(file_name, "/sys/class/gpio/gpio%d/value", gpio_num);
        fp = fopen(file_name, "rb+");
        if (fp == NULL) {
                printf("Cannot open %s.\n", file_name);
                return -1;
        }
        if (gpio_out_val)
                strcpy(buf,"1");
        else
                strcpy(buf,"0");

        fwrite(buf, sizeof(char), sizeof(buf) - 1, fp);
        printf("%s: gpio%d_%d = %s\n", __func__,
                        gpio_chip_num, gpio_offset_num, buf);
        fclose(fp);

        sprintf(file_name, "/sys/class/gpio/unexport");
        fp = fopen(file_name, "w");
        if (fp == NULL) {
                printf("Cannot open %s.\n", file_name);
                return -1;
        }
        fprintf(fp, "%d", gpio_num);
        fclose(fp);

        return 0;

}
#if 0
int main(int argc, const char *argv[])
{

	//out test
		int i = 0;
		for(i=0;i<20;i++)
		{
			if(0==i%2)
			{
			gpio_test_out(3,3,1);
			}
			else
			{
			gpio_test_out(3,3,0);
			sleep(1);
			}
		}
		
	printf("test gpio over!\n");
	return 0;
	
}
#endif


#if 0
int main(int argc, const char *argv[])
{

	//in test
	int retval = 0;
	retval = gpio_test_in(3,3);
	printf("retval = %d\n",retval);
		
	printf("test gpio over!\n");
	return 0;
	
}
#endif


#if 1
int main(int argc, const char *argv[])
{
	
	int i = 0;
	int retval = 0;
	for(i=0;i<10;i++)
	{
		printf("This is the %d test!!!\n",i+1);
			if(0==i%2)
			{
			gpio_test_out(3,3,1);
			retval = gpio_test_in(3,1);
			printf("retval = %d\n",retval);
			}
			else
			{
			gpio_test_out(3,3,0);
			retval = gpio_test_in(3,1);
			printf("retval = %d\n",retval);
			sleep(5);
			}
		printf("The %d test is over!!!\n",i+1);
	printf("\n\n");
	}
		
	printf("test gpio over!\n");
	return 0;
	
}
#endif

 

 

你可能感兴趣的:(Hisi)