posix_mod.c

来自「rtlinux3.0 的源代码」· C语言 代码 · 共 64 行

C
64
字号
#include <rtl.h>#include <rtl_fifo.h>#include <time.h>#include <rtl_sched.h>#include <rtl_sync.h>#include <pthread.h>#include <posix/unistd.h>pthread_t T;void *my_code(void *);int fd;int stop = 0;static void copy_device_data(unsigned int *);#define DELAY_NS 500000 // 500 microsecondsint init_module(void){		if ( (fd = open("/dev/rtf0",O_WRONLY | O_NONBLOCK )) < 0)	{		rtl_printf("Example cannot open fifo\n");		rtl_printf("Error number is %d\n",errno);		return -1;	}	if( pthread_create(&T,NULL,my_code,NULL))	{		close(fd);		rtl_printf("Cannot create thread\n");		return -1;	}	return 0;}void cleanup_module(void){ 	stop = 1;	pthread_join(T,NULL);	close(fd);}void *my_code(void *arg){	struct timespec t;	struct {int i; unsigned int d; }D = {0,0};	clock_gettime(CLOCK_RTL_SCHED,&t);	while(!stop){		copy_device_data(&D.d);		D.i++;		/* ignore write fails, we just drop the data */		write(fd,&D,sizeof(D));		timespec_add_ns(&t,DELAY_NS);		clock_nanosleep(CLOCK_RTL_SCHED, TIMER_ABSTIME, &t, NULL);	}	return (void *)stop;}static void copy_device_data(unsigned int *x){	static int last=0;	int d;	rdtscl(d);	*x= (d - last);	last = d;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?