编译源代码
-
点击test-gpio.c下载测试源代码文件,保存到 Ubuntu 下的目标目录。例如, 应用程序目录:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <sys/stat.h> #define GPIO44 492 #define GPIO22 470 #define GPIO_KEY1 GPIO44 #define FISCV_GPIO GPIO22 #define MAX_BUF 128 //Define array size #define StarF_Gpio_Dir "/sys/class/gpio" //GPIO control paths /********************************************************************** * Function Name: StarF_gpio_export * Description: Set the pin number * return value: 0 Success; Others: fail * Data version Author Application Name * ---------------------------------------------------------------------- * 2021/12/08 V1.0 zheng.xu test gpio ***********************************************************************/ int StarF_gpio_export(unsigned int gpio) { int fd, len; char buf[MAX_BUF]; // /sys/class/gpio/export fd = open( "/sys/class/gpio/export", O_WRONLY); if (fd < 0) { perror("gpio/export"); return fd; } len = snprintf(buf, sizeof(buf), "%d", gpio); write(fd, buf, len); close(fd); return 0; } /********************************************************************** * Function Name: StarF_gpio_unexport * Description: Cancel the GPIO pin number * return value: 0 Success; Others: fail * Data version Author Application Name * ---------------------------------------------------------------------- * 2021/12/08 V1.0 zheng.xu test gpio ***********************************************************************/ int StarF_gpio_unexport(unsigned int gpio) { int fd, len; char buf[MAX_BUF]; // /sys/class/gpio/unexport fd = open("/sys/class/gpio/unexport", O_WRONLY); if (fd < 0) { perror("gpio/export"); return fd; } len = snprintf(buf, sizeof(buf), "%d", gpio); write(fd, buf, len); close(fd); return 0; } /********************************************************************** * Function Name: StarF_gpio_set_dir * Description: Set GPIO pin I/O * return value: 0 Success; Others: fail * Data version Author Application Name * ---------------------------------------------------------------------- * 2021/12/08 V1.0 zheng.xu test gpio ***********************************************************************/ int StarF_gpio_set_dir(unsigned int gpio, unsigned int out_flag) { int fd, len; char buf[MAX_BUF]; // /sys/class/gpio/gpioN/direction len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/direction", gpio); fd = open(buf, O_WRONLY); if (fd < 0) { perror(buf); return fd; } if (out_flag) //'1' set to output write(fd, "out", 4); else //‘0’ set input write(fd, "in", 3); close(fd); return 0; } /********************************************************************** * Function Name: StarF_gpio_set_dir * Description: Set GPIO high & low levels * function parameters:@GPIO Set GPIO number for the output level value 1: Set gpio output to high level; 0: Set gpio output to low level. * return value: 0 Success; Others: fail * Data version Author Application Name * ---------------------------------------------------------------------- * 2021/12/08 V1.0 zheng.xu test gpio ***********************************************************************/ int StarF_gpio_set_value(unsigned int gpio, unsigned int value) { int fd, len; char buf[MAX_BUF]; // /sys/class/gpio/gpioN/value len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/value", gpio); fd = open(buf, O_WRONLY); if (fd < 0) { perror(buf); return fd; } if (value) //'1' output is high level write(fd, "1", 2); else //'0' output is Low level write(fd, "0", 2); close(fd); return 0; } /********************************************************************** * Function Name: StarF_gpio_get_value * Description: Read GPIO high & low levels * function parameters:@GPIO Set GPIO number for the output level value 1: Set gpio output to high level; 0: Set gpio output to low level. * return value: 0 Success; Others: fail * Data version Author Application Name * ---------------------------------------------------------------------- * 2021/12/08 V1.0 zheng.xu test gpio ***********************************************************************/ int StarF_gpio_get_value(unsigned int gpio, unsigned int *value) { int fd, len; char buf[MAX_BUF]; char ch; // /sys/class/gpio/gpioN/value len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/value", gpio); fd = open(buf, O_RDONLY); if (fd < 0) { perror("gpio/get-value"); return fd; } read(fd, &ch, 1); //Read the external input level if (ch != '0') { //'1' Input is high level *value = 1; } else { //'0' Input is Low level *value = 0; } close(fd); return 0; } /********************************************************************** * FunctionName: main * Description: * function parameters: * return value: 0 Success; Others: fail * Data version Author Application Name * ---------------------------------------------------------------------- * 2021/12/08 V1.0 zheng.xu test gpio ***********************************************************************/ int main(int argc, char **argv) { unsigned int i; unsigned int value1,value2; printf("\t********************************************\n"); printf("\t******** StarF_GPIO_TEST_DEMO ************\n"); printf("\t******** Version date: 2021/12 ************\n"); printf("\t********************************************\n"); printf("Gpio begin to init\r\n"); StarF_gpio_export(FISCV_GPIO); //export gpio Gpio StarF_gpio_set_dir(FISCV_GPIO, 1); //set as output printf("Gpio init ok\r\n"); /* Confirm INIT_B Pin as High */ while(1) { StarF_gpio_set_value(FISCV_GPIO, 1); //output high printf("Gpio off\r\n"); usleep(500000); //delay StarF_gpio_set_value(FISCV_GPIO, 0); //output low printf("Gpio on\r\n"); usleep(500000); //delay } StarF_gpio_unexport(FISCV_GPIO); //unexport gpio Gpio return 0; }
-
(可选)安装编译工具。以下是安装示例:
sudo apt-get install gcc-riscv64-linux-gnu
Information:
- 如已安装该工具,则可跳过此步。
- 安装完成后,运行以下代码,以确认版本是否已更新:
linus@starfive$ riscv64-linux-gnu-gcc -v
:图 1. 输出示例
结果:系统在当前目录生成了名为
test-gpio
的可执行文件。 -
执行以下命令,以编译源代码:
riscv64-linux-gnu-gcc -o test-gpio test-gpio.c
-
执行以下命令,以测试编译是否成功:
file test-gpio
Result: 在输出结果中出现
UCB RISC-V
,则表示编译成功:Riscv@starfive:~/work/app$ file test-gpio test-gpio: ELF 64-bit LSB executable, UCB RISC-V, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-riscv64-lp64d.so.1, for GNU/Linux 4.15.0, BuildID[sha1]=476d5a99c84f995d03227a18285222ac25e2cd0d, not stripped c-v2x@starfive:~/work/app$