linux使用gpio开一个线程,gpio和多线程

#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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值