=== GPIO 应用编程 === C参考代码如下: #include #include #include #include #include #include #include static char gpio_path[100]; //设置GPIO方向、高低电平 static int gpio_config(const char *file, const char *value) { char config_path[100]; int len; int fd; sprintf(config_path, "%s/%s", gpio_path, file); if (0 > (fd = open(config_path, O_WRONLY))) { perror("open error"); return fd; } len = strlen(value); if (len != write(fd, value, len)) { perror("write error"); close(fd); return -1; } close(fd); return 0; } //获取GPIO的方向、电平 static int gpio_get(const char *file) { char get_path[100]; char buf[10]={"\0"}; int len; int fd; sprintf(get_path, "%s/%s", gpio_path, file); if (0 > (fd = open(get_path, O_RDWR))) { perror("open error"); return fd; } if ( 0 > read(fd,buf,10)) { perror("read error"); return fd; } printf(" %s : %s",file,buf); close(fd); return 0; } int main(int argc, char *argv[]) { if (4 != argc) { if (3 != argc) { fprintf(stderr, "set gpio out : %s \n", argv[0]); fprintf(stderr, "set gpio in : %s \n", argv[0]); exit(-1); } } sprintf(gpio_path, "/sys/class/gpio/gpio%s", argv[1]); if (access(gpio_path, F_OK)) { printf("%s inexistence,export %s... \n",gpio_path,argv[1]); int fd; int len; if (0 > (fd = open("/sys/class/gpio/export", O_WRONLY))) { perror("open error"); exit(-1); } len = strlen(argv[1]); if (len != write(fd, argv[1], len)) { perror("write error"); close(fd); exit(-1); } close(fd); } if (gpio_config("direction", argv[2])) exit(-1); if ( 0 == strcmp("out",argv[2] ) && argc == 4 ) { if(gpio_config("value", argv[3])) exit(-1); } printf("GPIO%s:\n",argv[1]); if (gpio_get("direction")) exit(-1); if (gpio_get("value")) exit(-1); exit(0); } 交叉编译源码: aarch64-linux-gnu-gcc -o gpio gpio.c 将编译好的gpio程序使用scp拷贝到 r36s0 主板上,执行测试: 使用方法: 1:./gpio 56 out 1 0:./gpio 56 out 0 {{:arm:rk3399:linux:36s0_gpio10.png?600|}} === UART 应用编程 === 系统下操作 UART 的测试串口,以 232_RX5\TX5 测试为例: 232_RX5\TX5 设备节点为: /dev/ttyS7 C参考UART高低电平输入代码如下: #include #include #include #include #include #include #include #include #define UART_DEVICE "/dev/ttyS7" //uart设备文件名称 int main(int argc, char *argv[]) { int fd, res; struct termios oldtio, newtio; char ch; char buf[256] = {0}; //-----------打开uart设备文件------------------ fd = open(UART_DEVICE, O_RDWR|O_NOCTTY);//没有设置O_NONBLOCK。所以这里read和write是堵塞操作 if (fd < 0) { perror(UART_DEVICE); exit(1); } else printf("Open %s successfully\n", UART_DEVICE); //-----------设置操作?数----------------------- tcgetattr(fd, &oldtio);//获取当前操作模式?数 memset(&newtio, 0, sizeof(newtio)); //波特率=115200 数据位=8 使能数据接收 newtio.c_cflag = B115200|CS8|CLOCAL|CREAD; newtio.c_iflag = IGNPAR; tcflush(fd, TCIFLUSH);//清空输入缓冲区和输出缓冲区 tcsetattr(fd, TCSANOW, &newtio);//设置新的操作?数 //------------向urat发送数据------------------- res=write(fd, "Begin Uart tx", 16); while(1) { //从控制台终端获取数据,然后通过uart发送出去,直到接收到!字符 while((ch=getchar()) != '!') { buf[0]=ch; res=write(fd, buf, 1); } buf[0]=ch; buf[1]=' '; res = write(fd, buf, 2); break; } //-------------从uart接收数据------------------- while(1) { res = read(fd, buf, 255);//程序将在这里挂起,直到从uart接收到数据(堵塞操作) if (res == 0) continue; buf[res] = ' '; printf("res = %d, buf = %s\n", res, buf);//将uart接收到的字符打印出来 if (buf[0] == '!')//uart接收到!字符后退出while break; } //------------关闭uart设备文件,恢复原先?数-------- close(fd); printf("Close %s\n", UART_DEVICE); tcsetattr(fd, TCSANOW, &oldtio); //恢复原先的设置 return 0; } 交叉编译源码: aarch64-linux-gnu-gcc -o uart uart.c 将编译好的程序使用 scp 拷贝到 r36s0 主板上,执行测试: {{:arm:rk3399:linux:36s0_uart11.png?600|}} === KEY 应用编程 === 参考系统下操作Key的方法,获取到key的设备节点为 /dev/input/event3 C参考代码如下: #include #include #include #include #include #include #define INPUT_DEVICE "/dev/input/event3" int main(int argc, char **argv){ int fd; struct input_event event; ssize_t bytesRead; int ret; fd_set readfds; if ( 0 > (fd = open(INPUT_DEVICE,O_RDONLY))) { perror("open error"); return fd; } while(1){ FD_ZERO(&readfds); FD_SET(fd,&readfds); ret = select(fd + 1, &readfds, NULL, NULL, NULL); if (ret == -1){ fprintf(stderr,"select call on%s : an error ocurred",argv[1]); break; } if(FD_ISSET(fd,&readfds)){ bytesRead = read(fd, &event,sizeof(struct input_event)); if(bytesRead == -1 ) fprintf(stderr,"bytesRead :%ld : an error ocurred",bytesRead); } if(event.type == EV_KEY && (event.value == 0 || event.value == 1)) { printf("key %s\n",(event.value) ? "pressed" : "released"); } } close(fd); return EXIT_SUCCESS; } 交叉编译源码: aarch64-linux-gnu-gcc -o key key.c 将编译好的程序使用 scp 拷贝到 r36e0 主板上,执行测试,按动 key 打印如下: {{:arm:rk3399:linux:36s0_key2.png?600|}} *按下按键时显示:key pressed *松开按键时显示:key released