#include
#include
#include
#include
#include
#include
#include
#include
#include
#define __TIME_PRINTF__
#define __MULTI_THREAD__
#define __GPIO_OPERATION__
#ifdef __TIME_PRINTF__
#include
#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;
}
#endif
void *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;
}
#endif
int 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;
}