-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathCurieMailbox.cpp
87 lines (71 loc) · 1.79 KB
/
CurieMailbox.cpp
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
#include "interrupt.h"
#include "scss_registers.h"
#include "CurieMailbox.h"
#define BUFSIZE 33
#define CAP_CHAN(chan) chan = (chan >= CurieMailbox.numChannels) ?\
CurieMailbox.numChannels - 1 : ((chan < 0) \
? 0 : chan)
static CurieMailboxMsg buf[BUFSIZE];
static volatile unsigned int head;
static unsigned int tail;
CurieMailboxClass::CurieMailboxClass (void)
{
head = 0;
tail = 0;
}
CurieMailboxClass CurieMailbox = CurieMailboxClass();
static void buf_put (CurieMailboxMsg msg)
{
if (CurieMailbox.available() == (BUFSIZE - 1)) {
/* Full- drop the new message */
return;
}
buf[head] = msg;
head = (head + 1) % BUFSIZE;
}
int CurieMailboxClass::available (void)
{
return ((head + BUFSIZE) - tail) % BUFSIZE;
}
void CurieMailboxClass::enableReceive (int channel)
{
CAP_CHAN(channel);
mailbox_enable_receive(channel);
}
void CurieMailboxClass::disableReceive (int channel)
{
CAP_CHAN(channel);
mailbox_disable_receive(channel);
}
static void mbox_isr (CurieMailboxMsg msg)
{
buf_put(msg);
}
void CurieMailboxClass::begin (void)
{
/* Channel 7 is reserved for Serial */
for (int i = 0; i < NUM_MAILBOX_CHANNELS - 1; ++i) {
mailbox_register(i, mbox_isr);
}
}
void CurieMailboxClass::end (void)
{
/* Channel 7 is reserved for Serial */
for (int i = 0; i < NUM_MAILBOX_CHANNELS - 1; ++i) {
mailbox_register(i, 0);
}
}
void CurieMailboxClass::put (CurieMailboxMsg msg)
{
CAP_CHAN(msg.channel);
mailbox_write(msg);
}
CurieMailboxMsg CurieMailboxClass::get (void)
{
CurieMailboxMsg msg;
if (head != tail) {
msg = buf[tail];
tail = (tail + 1) % BUFSIZE;
}
return msg;
}