-
Notifications
You must be signed in to change notification settings - Fork 6
/
fifo.c
94 lines (79 loc) · 1.42 KB
/
fifo.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
#include <u.h>
#include "../port/lib.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
static QLock getl, putl;
static Rendez getr, putr;
static int
fifocanput(void*)
{
return (FIFOREG->ctl & FifoTfull) == 0;
}
static int
fifocanget(void*)
{
return (FIFOREG->ctl & FifoRempty) == 0;
}
int
nbfifoput(ulong cmd, ulong data)
{
if(FIFOREG->ctl & FifoTfull)
return 0;
if(data>>Fdatalen){
print("nbfp: fifo msg (%lux|%lux) too big\n", data, cmd);
return 0;
}
FIFOREG->send = (data<<Fcmdlen|cmd);
return 1;
}
void
fifoput(ulong cmd, ulong data)
{
qlock(&putl);
sleep(&putr, fifocanput, nil);
nbfifoput(cmd, data);
qunlock(&putl);
}
ulong
fifoget(ulong *data)
{
ulong r;
qlock(&getl);
sleep(&getr, fifocanget, nil);
r = FIFOREG->recv;
qunlock(&getl);
if(data != nil)
*data = r>>Fcmdlen;
return r&Fcmdlen;
}
extern void fiforecv(ulong);
static void
fiforxintr(Ureg*, void*)
{
ulong v;
while(!(FIFOREG->ctl & FifoRempty)) {
v = FIFOREG->recv;
fiforecv(v);
}
intrclear(FRECVbit, 0);
}
static void
fifotxintr(Ureg*, void*)
{
if(FIFOREG->ctl & FifoTfull)
return;
wakeup(&putr);
intrclear(FSENDbit, 0);
}
static void
fifoinit(void (*rxintr)(Ureg *, void*), void (*txintr)(Ureg *, void*))
{
FIFOREG->ctl = (FifoTirq|FifoRirq|Fifoenable|FifoTflush);
intrenable(0, FSENDbit, txintr, nil, "txintr");
intrenable(0, FRECVbit, rxintr, nil, "rxintr");
}
void
fifolink(void){
fifoinit(fiforxintr, fifotxintr);
}