hi3519a gpio


原文链接: hi3519a gpio

海思提供一在应用层测试GPIO的函数,只需要将其使用,即可完成GPIO的输入输出等功能测试,本文是测试一下海思GPIO的输入功能。

 
gpio11_5 93
1、测试源码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
 
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;
 
}
				
int main(int argc, const char *argv[])
{
 
		if (argc != 3)
			{
			printf("Usage %s : gpio_chip_num : gpio_offset_num!\n",argv[0]);
			return -1;
			}
		int gpio_chip_num = atoi(argv[1]);
		int gpio_offset_num = atoi(argv[2]);
		int i = 20;
 
		while(i > 0)
		{
			gpio_test_in(gpio_chip_num,gpio_offset_num);
			sleep(1);
			i--;
		}
		
	printf("test_gpio over!\n");
return 0;
	
}

2、我使用的仍是传参方式,下面的测试我使用的是是GPIO3_1,所以传参为 3 1。将上面的源码编译成ELF文件,拷贝至开发板下。

 

3、运行该程序。

 

 

 

4、我在外部手动给其一个高地电平,当给高时,对用的打印信息为1,当给低时,对用的打印信息为0。

 

至此即完成GPIO的输入测试,可以在此基础上修改代码,完成自己想要的功能
————————————————
版权声明:本文为CSDN博主「zxy131072」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zxy131072/article/details/86621072

`