File: com_posix.c

package info (click to toggle)
rtlinux 3.1pre3-3
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 4,896 kB
  • ctags: 4,228
  • sloc: ansic: 26,204; sh: 2,069; makefile: 1,414; perl: 855; tcl: 489; asm: 380; cpp: 42
file content (80 lines) | stat: -rw-r--r-- 1,571 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * rt_com usage example
 *
 * Michael Barabanov <baraban@fsmlabs.com>, 1999
 *  (C) FSMLabs  1999. baraban@fsmlabs.com
 *  Released under the GNU GENERAL PUBLIC LICENSE Version 2, June 1991
 *  Any use of this code must include this notice.
 *
 * This example uses the RTL POSIX IO layer
 */

#include <rtl_fifo.h>
#include <pthread.h>
#include <time.h>
#include <asm/io.h>
#include <rtl.h>
#include <unistd.h>
#include "rt_com.h"

int period=100000000;
pthread_t thread;
int fd;

void *thread_code(void *t) {

	pthread_make_periodic_np (thread, gethrtime(), period);

	do {
		int n;
		char buf[210];
		pthread_wait_np();
		n = read (fd, buf, sizeof(buf));
		if (n > 0) {
			buf[n] = 0;
			rtl_printf("%s", buf);
		}
		write (fd, "test\n", 5);
	} while (1);

	return 0;
}



int init_module(void)
{
	pthread_attr_t attr;
	struct sched_param sched_param;
	int thread_status;

	fd = open("/dev/ttyS0", O_NONBLOCK);
	if (fd < 0) {
		rtl_printf("/dev/ttyS0 open returned %d\n", fd);
		return -1;
	}
	rt_com_setup(0, 38400, RT_COM_PARITY_NONE, 1, 8);

	pthread_attr_init (&attr);
	pthread_attr_setcpu_np(&attr, 0);
	sched_param.sched_priority = 1;
	pthread_attr_setschedparam (&attr, &sched_param);
	thread_status = pthread_create (&thread,  &attr, thread_code, (void *)1);
	if (thread_status != 0) {
		printk("failed to create RT-thread\n");
		return -1;
	} else {
		printk("created RT-thread\n");
	}

	return 0;
}


void cleanup_module(void)
{
        printk ("Removing module on CPU %d\n", rtl_getcpuid());
	pthread_delete_np (thread);
	rt_com_setup(0, -1, 0, 0, 0);
}