forked from vsamtuc/tinyos3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bios_example5.c
128 lines (95 loc) · 2.2 KB
/
bios_example5.c
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <error.h>
#include <unistd.h>
#include "bios.h"
/*
A simple program to demonstrate using serial devices.
To run this program, you must execute two terminals
in two different windows first. Otherwise, the program
will block.
*/
ssize_t read_serial(void *cookie, char *buf, size_t size)
{
uint core = cpu_core_id;
ssize_t ret = 0;
char c = '\0';
while(size>0 && c!='\n') {
/* get a character */
if(! bios_read_serial(core, &c))
{
cpu_disable_interrupts();
/*
Wait in a busy loop, polling the driver.
*/
while(! bios_read_serial(core, &c))
;
cpu_enable_interrupts();
}
*buf = c;
buf++;
ret++;
size--;
}
return ret;
}
ssize_t write_serial(void* cookie, const char* buf, size_t size)
{
uint core = cpu_core_id;
ssize_t ret = 0;
while(size > 0) {
char c = *buf;
if(bios_write_serial(core, c))
{
size --;
buf ++;
ret ++;
}
}
return ret;
}
cookie_io_functions_t bios_serial_funcs = {
read_serial, write_serial, NULL, NULL
};
void bootfunc()
{
uint mycore = cpu_core_id;
bios_serial_interrupt_core(mycore, SERIAL_TX_READY, mycore);
bios_serial_interrupt_core(mycore, SERIAL_RX_READY, mycore);
/*
open a buffered stream for convenience
*/
FILE* fterm = fopencookie(NULL, "r+", bios_serial_funcs);
char* linebuf = NULL;
size_t linebuf_len;
int n=3;
while(n--) {
fprintf(fterm, "Type a line: "); fflush(fterm);
int rc = getline(&linebuf, &linebuf_len, fterm);
if(rc==-1) {
char msg[1024];
char* message = strerror_r(errno, msg, 1024);
fprintf(fterm, "error in reading input: %s\n", message);
fflush(fterm);
} else
fprintf(fterm, "You typed: %s", linebuf);
fflush(fterm);
if(strcmp(linebuf, "exit")==0)
break;
}
fprintf(fterm, "Bye bye\n");
fflush(fterm);
if(linebuf)
free(linebuf);
fclose(fterm);
}
int main()
{
fprintf(stderr, "Please make sure that you have 2 terminals running, or else"
" this process will be stuck.\nIf you do not see a message to tell you to"
" 'Type a line', please either start terminals 0 and 1 or kill this process"
" (by hitting <Control>-C).\n");
vm_boot(bootfunc, 2, 2);
}