gpio和多线程

来源:互联网 发布:镜子软件手机版 编辑:程序博客网 时间:2024/05/19 05:05
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <unistd.h>#include <termios.h>#include <errno.h>#include <pthread.h>#define __TIME_PRINTF__#define __MULTI_THREAD__#define __GPIO_OPERATION__#ifdef __TIME_PRINTF__    #include <time.h>#endif#ifdef __GPIO_OPERATION__#include "include/gpio.h"#define PATH_DEV "/dev/gpio"int fd_dev;int read_gpio(int fd/*, char *buf*/){    int count, i;     user_gpio_set_t gpio[20];    memset(gpio, '0', 20*sizeof(user_gpio_set_t));    count = read(fd, gpio, 20*sizeof(user_gpio_set_t));    printf("read character number = %d\n", count);    for (i = 0; i < 20; i++) {        printf("[%d]: name = %s, data = %d.\n", i, gpio[i].gpio_name, gpio[i].data);    }    return 0;}#endifvoid *pthread_read_fn_0(void *arg){    time_t nowtime;    struct tm *p_time;                    while (1) {                //sleep(1);    #ifdef __TIME_PRINTF__        time(&nowtime);        p_time = localtime(&nowtime);    #endif        printf("pthread#0 loop:\n");    #ifdef __TIME_PRINTF__        printf("\tlocaltime = %s\n", asctime(p_time));    #endif    #ifdef __GPIO_OPERATION__        read_gpio(fd_dev);    #endif    }                        return NULL;}#ifdef __MULTI_THREAD__void *pthread_read_fn_1(void *arg){    time_t nowtime;    struct tm *p_time;                    while (1) {                //sleep(1);    #ifdef __TIME_PRINTF__        time(&nowtime);        p_time = localtime(&nowtime);    #endif        printf("pthread#1 loop:\n");    #ifdef __TIME_PRINTF__        printf("\tlocaltime = %s\n", asctime(p_time));    #endif    #ifdef __GPIO_OPERATION__        read_gpio(fd_dev);    #endif    }                        return NULL;}#endifint main(int argc, char **argv){#ifdef __GPIO_OPERATION__    //open device    printf("open A10 gpio character device.\n");    fd_dev = open(PATH_DEV, O_RDWR);    if (fd_dev < 0) {        printf("gpio fail to open.\n");        return -1;    }    printf("device open; fd = %d.\n", fd_dev);#endif    printf("start to read pthread.\n");    pthread_t pthread_read_0;    pthread_create(&pthread_read_0, NULL, pthread_read_fn_0, NULL);#ifdef __MULTI_THREAD__    pthread_t pthread_read_1;    pthread_create(&pthread_read_1, NULL, pthread_read_fn_1, NULL);#endif    pthread_join(pthread_read_0, NULL);#ifdef __MULTI_THREAD__    pthread_join(pthread_read_1, NULL);#endif                                            #ifdef __GPIO_OPERATION__    //close device    close(fd_dev);    printf("device close.\n");#endif    return 0;}


0 0