差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
template:temp_linux_sw [2022/12/27 23:34] ljy 移除 |
template:temp_linux_sw [2023/03/03 01:49] (当前版本) wjj 创建 |
||
---|---|---|---|
行 1: | 行 1: | ||
+ | ==== Linux 编程指南 ==== | ||
+ | |||
+ | === GPIO 应用编程 === | ||
+ | |||
C参考代码如下: | C参考代码如下: | ||
<code C> | <code C> | ||
行 10: | 行 14: | ||
static char gpio_path[100]; | static char gpio_path[100]; | ||
// | // | ||
- | static int gpio_config(const char *value) | + | static int gpio_config(const char *file, |
{ | { | ||
char config_path[100]; | char config_path[100]; | ||
int len; | int len; | ||
int fd; | int fd; | ||
- | sprintf(config_path, | + | sprintf(config_path, |
if (0 > (fd = open(config_path, | if (0 > (fd = open(config_path, | ||
{ | { | ||
行 31: | 行 35: | ||
return 0; | return 0; | ||
} | } | ||
+ | // | ||
+ | static int gpio_get(const char *file) | ||
+ | { | ||
+ | char get_path[100]; | ||
+ | char buf[10]={" | ||
+ | int len; | ||
+ | int fd; | ||
+ | sprintf(get_path, | ||
+ | if (0 > (fd = open(get_path, | ||
+ | { | ||
+ | perror(" | ||
+ | return fd; | ||
+ | } | ||
+ | if ( 0 > read(fd, | ||
+ | { | ||
+ | perror(" | ||
+ | return fd; | ||
+ | } | ||
+ | printf(" | ||
+ | close(fd); | ||
+ | return 0; | ||
+ | } | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | { | ||
if (4 != argc) | if (4 != argc) | ||
{ | { | ||
- | if (3 != argc) | + | if (3 != argc) |
{ | { | ||
fprintf(stderr, | fprintf(stderr, | ||
行 42: | 行 68: | ||
} | } | ||
} | } | ||
- | sprintf(gpio_path, | + | sprintf(gpio_path, |
if (access(gpio_path, | if (access(gpio_path, | ||
{ | { | ||
printf(" | printf(" | ||
- | } | + | int fd; |
+ | int len; | ||
+ | if (0 > (fd = open("/ | ||
+ | { | ||
+ | perror(" | ||
+ | } | ||
+ | len = strlen(argv[1]); | ||
+ | if (len != write(fd, argv[1], len)) | ||
+ | { | ||
+ | perror(" | ||
+ | close(fd); | ||
+ | exit(-1); | ||
+ | } | ||
+ | close(fd); | ||
+ | } | ||
+ | if (gpio_config(" | ||
+ | exit(-1); | ||
if ( 0 == strcmp(" | if ( 0 == strcmp(" | ||
{ | { | ||
- | if(gpio_config(argv[3])) | + | if(gpio_config(" |
exit(-1); | exit(-1); | ||
} | } | ||
- | printf(" | + | printf(" |
- | printf(" | + | if (gpio_get(" |
- | printf(" | + | exit(-1); |
+ | if (gpio_get(" | ||
+ | exit(-1); | ||
exit(0); | exit(0); | ||
} | } | ||
行 62: | 行 106: | ||
aarch64-linux-gnu-gcc -o gpio gpio.c | aarch64-linux-gnu-gcc -o gpio gpio.c | ||
- | 将编译好的gpio程序使用scp拷贝到 | + | 将编译好的gpio程序使用scp拷贝到 |
使用方法: | 使用方法: | ||
- | set gpio out : ./ | + | 0:./ |
- | set gpio in | + | |
+ | 1: | ||
| | ||
- | {{: | + | {{: |
+ | |||
+ | |||
+ | === UART 应用编程 | ||
+ | |||
+ | 系统下操作 UART 的测试串口,以 J_RS232 TX3\RX3 测试为例: | ||
+ | |||
+ | J_RS232 TX3\RX3 设备节点为: | ||
+ | |||
+ | / | ||
+ | |||
+ | C参考UART高低电平输入代码如下: | ||
+ | |||
+ | <code C> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | int set_opt(int, | ||
+ | |||
+ | void main(){ | ||
+ | int fd,nByte; | ||
+ | char *uart3 = "/ | ||
+ | char bufferR[2]; | ||
+ | char bufferW[2]; | ||
+ | |||
+ | memset(bufferR, | ||
+ | memset(bufferW, | ||
+ | |||
+ | if((fd=open(uart3, | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | else{ | ||
+ | printf(" | ||
+ | set_opt(fd, | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | while(1){ | ||
+ | |||
+ | nByte = 0; | ||
+ | |||
+ | memset(bufferR, | ||
+ | memset(bufferW, | ||
+ | |||
+ | // printf(" | ||
+ | if((nByte = read(fd, bufferR, 1)) == 1){ // | ||
+ | |||
+ | |||
+ | printf(" | ||
+ | |||
+ | bufferW[0] = ' | ||
+ | |||
+ | write(fd, | ||
+ | |||
+ | |||
+ | memset(bufferR, | ||
+ | memset(bufferW, | ||
+ | |||
+ | nByte = 0; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | close(fd); | ||
+ | |||
+ | } | ||
+ | |||
+ | // | ||
+ | int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop) | ||
+ | { | ||
+ | struct termios newtio, | ||
+ | // | ||
+ | if ( tcgetattr( fd,& | ||
+ | perror(" | ||
+ | return | ||
+ | } | ||
+ | // | ||
+ | bzero( & | ||
+ | newtio.c_cflag | ||
+ | newtio.c_cflag &= ~CSIZE; | ||
+ | // | ||
+ | switch( nBits ) | ||
+ | { | ||
+ | case 7: | ||
+ | newtio.c_cflag |= CS7; | ||
+ | break; | ||
+ | case 8: | ||
+ | newtio.c_cflag |= CS8; | ||
+ | break; | ||
+ | } | ||
+ | // | ||
+ | switch( nEvent ) | ||
+ | { | ||
+ | // | ||
+ | case ' | ||
+ | newtio.c_cflag |= PARENB;// | ||
+ | newtio.c_cflag |= PARODD;// | ||
+ | newtio.c_iflag |= (INPCK | ISTRIP);// | ||
+ | break; | ||
+ | case ' | ||
+ | newtio.c_iflag |= (INPCK | ISTRIP); | ||
+ | newtio.c_cflag |= PARENB; | ||
+ | newtio.c_cflag &= ~PARODD;// | ||
+ | break; | ||
+ | case ' | ||
+ | newtio.c_cflag &= ~PARENB;// | ||
+ | break; | ||
+ | } | ||
+ | // | ||
+ | switch( nSpeed ) | ||
+ | { | ||
+ | case 2400: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | case 4800: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | case 9600: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | case 115200: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | case 460800: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | default: | ||
+ | cfsetispeed(& | ||
+ | cfsetospeed(& | ||
+ | break; | ||
+ | } | ||
+ | // | ||
+ | if( nStop == 1 ) | ||
+ | newtio.c_cflag & | ||
+ | else if ( nStop == 2 ) | ||
+ | newtio.c_cflag |= CSTOPB;// | ||
+ | |||
+ | newtio.c_cc[VTIME] | ||
+ | newtio.c_cc[VMIN] = 0;// | ||
+ | |||
+ | tcflush(fd, | ||
+ | |||
+ | // | ||
+ | if((tcsetattr(fd, | ||
+ | { | ||
+ | perror(" | ||
+ | return -1; | ||
+ | } | ||
+ | // | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | 交叉编译源码: | ||
+ | |||
+ | aarch64-linux-gnu-gcc -o uart uart.c | ||
+ | |||
+ | 将编译好的程序使用 scp 拷贝到 3399 主板上,执行测试: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | |||
+ | === KEY 应用编程 === | ||
+ | |||
+ | 参考系统下操作Key的方法,获取到key的设备节点为 | ||
+ | < | ||
+ | / | ||
+ | </ | ||
+ | C参考代码如下: | ||
+ | |||
+ | <code C> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #define INPUT_DEVICE "/ | ||
+ | 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, | ||
+ | { | ||
+ | perror(" | ||
+ | return fd; | ||
+ | } | ||
+ | while(1){ | ||
+ | FD_ZERO(& | ||
+ | FD_SET(fd,& | ||
+ | ret = select(fd + 1, & | ||
+ | if (ret == -1){ | ||
+ | fprintf(stderr," | ||
+ | break; | ||
+ | } | ||
+ | if(FD_ISSET(fd,& | ||
+ | bytesRead = read(fd, & | ||
+ | if(bytesRead == -1 ) | ||
+ | fprintf(stderr," | ||
+ | } | ||
+ | if(event.type == EV_KEY && (event.value == 0 || event.value == 1)) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | close(fd); | ||
+ | return EXIT_SUCCESS; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 交叉编译源码: | ||
+ | |||
+ | < | ||
+ | aarch64-linux-gnu-gcc -o key key.c | ||
+ | </ | ||
+ | 将编译好的程序使用 scp 拷贝到 r39s2 主板上,执行测试,按动 key 打印如下: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | *按下按键时显示:key pressed | ||
+ | *松开按键时显示:key released |