搭建虚拟机,创建共享文件夹,在共享文件夹中用vscode编写。搞了个sh文件出来编译。用sfpt工具下载编译的程序到设备板子。
#include<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc,char **argv[])
{
printf("--- hello gpio 203---! \n");
//control gpio203
FILE *export_f =fopen("/sys/class/gpio/export","w");
if(export_f == NULL){
perror("Error to open GPIO export file");
return -1;
}
fprintf(export_f,"203");
fclose(export_f);
char direction_p[50];
snprintf(direction_p,sizeof(direction_p),"/sys/class/gpio/gpio203/direction");
FILE *d_f =fopen(direction_p,"w");
if(d_f == NULL){
perror("Error to open gpio203 direction file");
return -1;
}
fprintf(d_f,"out");
fclose(d_f);
char value_p[50];
char cat_command[100];
snprintf(value_p,sizeof(value_p),"/sys/class/gpio/gpio203/value");
snprintf(cat_command,sizeof(cat_command),"cat %s",value_p);
FILE *value_f =fopen(value_p,"w");
if((value_f ==NULL)){
perror("Error to open gpio203 value file");
return -1;
}
for(int i=0;i<6;i++){
fprintf(value_f,"1");
fflush(value_f);
system(cat_command);
sleep(2);
fprintf(value_f,"0");
fflush(value_f);
system(cat_command);
sleep(2);
}
fclose(value_f);
FILE *unexport_f = fopen("/sys/class/gpio/unexport","w");
if(unexport_f == NULL){
perror("Error to open gpio unexport file");
return -1;
}
fprintf(unexport_f,"203");
//control heartbeep
printf("--- hello gpio heartbeat leds---! \\\\n");
char brightness_p[50];
char cat_command2[100];
snprintf(brightness_p,sizeof(brightness_p),"/sys/class/leds/heartbeat/brightness");
snprintf(cat_command2,sizeof(cat_command2),"cat %s",brightness_p);
FILE *v_f =fopen(brightness_p,"w");
if((v_f ==NULL)){
perror("Error to open heartbeat leds brightness file");
return -1;
}
for(int i=0;i<6;i++){
fprintf(v_f,"1");
fflush(v_f);
system(cat_command2);
sleep(2);
fprintf(v_f,"0");
fflush(v_f);
system(cat_command2);
sleep(2);
}
fclose(value_f);
return 0;
}
更多回帖