-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogicData.cpp
347 lines (287 loc) · 6.68 KB
/
LogicData.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "Arduino.h"
#include "LogicData.h"
// Expect 1 bit per millisecond
#define SAMPLE_RATE 1000
// Push to head; pop from tail
index_t mque::next(index_t x)
{
return ( x + 1 ) % Q_MAX;
}
bool mque::empty() {
return tail == head;
}
bool mque::full() {
return next(tail) == head;
}
index_t mque::size()
{
return (Q_MAX + head - tail) % Q_MAX;
}
// destructive push; pushes even if full
void mque::push(micros_t t)
{
// NOTE: Caller should have disabled interrupts
trace[head] = t;
head = next(head);
if (tail == head) {
tail = next(tail);
}
}
// destructive pop; no-op if empty
bool mque::pop(micros_t * t)
{
lock _;
if (empty()) return false;
*t = trace[tail];
tail = next(tail);
return true;
}
// drop elements from the tail of the queue; no range-checking!
void mque::drop(index_t n)
{
lock _;
tail += n;
tail %= Q_MAX; // TODO: Check if this is expensive
}
// non-destructive indexed peek
bool mque::peek(index_t index, micros_t * t)
{
lock _;
if (index >= size()) return false;
index += tail;
index %= Q_MAX;
*t = trace[index];
return true;
}
//------------------------------------------------------
void LogicData::Begin() {
pinMode(tx_pin, OUTPUT);
SendBit(MARK); // IDLE-CLOSED
}
void LogicData::PinChange(bool level) {
// Assumes interrupts disabled
// Expect HIGH level on even queue steps
bool sync = q.head & 1;
if (level == sync) {
micros_t now = micros();
if (pin_idle) {
q.push(BIG_IDLE);
pin_idle = false;
} else {
q.push(now-prev_bit);
}
prev_bit = now;
}
}
void LogicData::Service() {
micros_t idle_time = micros() - prev_bit;
if (!pin_idle && idle_time >= IDLE_TIME) {
lock _;
if (micros() - prev_bit >= IDLE_TIME) {
pin_idle = true;
}
}
}
// Calculate parity and set in lsb of message
uint32_t LogicData::Parity(uint32_t msg) {
unsigned par_count = 0;
for (uint32_t mask = 2; mask ; mask <<= 2) {
par_count += msg & mask;
}
msg |= par_count & 1;
return msg;
}
bool LogicData::CheckParity(uint32_t msg) {
return Parity(msg) == msg;
}
//void LogicData::DumpQueue() {
// lock _;
// fini=q.tail;
// bool level = ((q.size() & 1)==0) ^ !prev_level;
//}
uint32_t LogicData::ReadTrace() {
index_t fini;
{
lock _;
fini=q.tail;
}
bool level = !(fini & 1);
micros_t t;
index_t i=0;
//-- Find start-bit (idle-low followed by high-pulse shorter than 2-bits)
for (; q.peek(i, &t); i++) {
if (!level && t > static_cast<micros_t>(40) * SAMPLE_RATE) {
micros_t t1;
if (q.peek(i+1, &t1) && t1 < SAMPLE_RATE*2) break;
}
level = !level;
}
//-- Sample signals at mid-point of data rate
uint32_t mask = 1ULL<<31;
uint32_t acc = 0;
acc = 0;
micros_t t_meas = SAMPLE_RATE/2;
for (t=0; mask; mask >>= 1) {
if (t_meas < SAMPLE_RATE) {
if (!q.peek(++i, &t)) break;
level = !level;
t_meas += t;
}
acc += !level ? mask : 0;
t_meas -= SAMPLE_RATE;
}
// ran out of signal before we got whole word
if (mask) return 0;
// We decoded a word and it consumed i samples
lock _;
if (fini == q.tail) {
q.drop(i-1);
} else {
// race fail; return 0 and let the caller try again later
acc = 0;
}
return acc;
}
bool LogicData::IsValid(uint32_t msg) {
if ((msg & 0xFFF00000) != 0x40600000) {
return false;
}
return CheckParity(msg);
}
bool LogicData::IsNumber(uint32_t msg) {
return IsValid(msg) && (msg & 0xFFE00) == 0x00400;
}
const char * LogicData::MsgType(uint32_t msg) {
if ((msg & 0xFFF00000) != 0x40600000) {
return "INVAL";
}
if (!CheckParity(msg)) {
return "PARIT";
}
// Display number
if ((msg & 0xFFE00) == 0x00400) {
return "NUMBR";
}
// Display command
if ((msg & 0xFFFF) == 0x1400) {
return "DISPL";
}
return "UKNWN";
}
static uint8_t ReverseNibble(uint8_t in) {
uint8_t ret = 0;
ret |= (in << 3) & 8;
ret |= (in << 1) & 4;
ret |= (in >> 1) & 2;
ret |= (in >> 3) & 1;
return ret;
}
static uint8_t ReverseByte(uint8_t in) {
return (ReverseNibble(in) << 4) + ReverseNibble(in>>4);
}
static uint16_t ReverseWord(uint16_t in) {
return (ReverseByte(in) << 8) + ReverseByte(in>>8);
}
uint8_t LogicData::GetNumber(uint32_t msg) {
if (IsNumber(msg)) {
return ReverseByte(msg>>1);
}
return 0;
}
const char * LogicData::Decode(uint32_t msg) {
static char buf[20];
// 0x40600400
// 0x20300200
// ^^ display byte
// ^^^ command
uint16_t w = ReverseWord(msg>>9)>>4;
uint8_t b = ReverseByte(msg>>1);
if ((msg & 0xFFF00000) != 0x40600000) {
sprintf(buf, "%08lx ??", msg);
} else if (!CheckParity(msg)) {
sprintf(buf, "%08lx !", msg);
} else if (w == 0x400) {
// Display number
sprintf(buf, "%03u", b);
} else if (msg == 0x40611400) {
sprintf(buf, "Display ON");
} else if (msg == 0x406e1400) {
sprintf(buf, "Display OFF");
} else {
sprintf(buf, "%08lx %03x %02x", msg, w, b);
}
return buf;
}
// Transmit
void LogicData::SendBit(bool bit) {
digitalWrite(tx_pin, bit);
}
void LogicData::MicroDelay(micros_t us) {
// spin-wait until timer advances
while (true) {
Service();
micros_t now = micros();
now -= timer;
if (now >= us) break;
// TODO: if (t-now > 50) usleep(t-now-50);
}
timer += us;
}
void LogicData::Delay(uint16_t ms) {
MicroDelay(micros_t(ms)*1000);
}
void LogicData::SendBit(bool bit, uint16_t ms) {
SendBit(bit);
Delay(ms);
}
void LogicData::Space(uint16_t ms) {
SendBit(SPACE, ms);
}
void LogicData::SendStartBit() {
Space(LOGICDATA_MIN_START_BIT);
}
void LogicData::LogicData::Stop() {
SendBit(MARK); // IDLE-CLOSED
}
void LogicData::Send(uint32_t data) {
SendStartBit();
for (uint32_t i = 0x80000000 ; i; i/=2) {
SendBit((i&data)?SPACE:MARK, 1);
}
SendBit(SPACE); // IDLE-OPEN
}
void LogicData::OpenChannel() {
if (active) return;
active = true;
start = timer = micros();
SendBit(SPACE);
}
void LogicData::CloseChannel() {
// Send a SPACE at least long enough so our command-channel was open for 500ms, or
// just start-bit length if we've already been open that long.
if (!active) return;
// Workaround for stuck timers
Stop();
return;
// BUG: Following timers never expire!
micros_t delta = timer-start;
if (delta/1000 + LOGICDATA_MIN_START_BIT < LOGICDATA_MIN_WINDOW_MS) {
timer=start;
Space(LOGICDATA_MIN_WINDOW_MS);
} else {
Space(LOGICDATA_MIN_START_BIT);
}
Stop();
active = false;
}
void LogicData::Send(uint32_t * data, unsigned count) {
if (!count) return;
OpenChannel();
for (unsigned i = 0; i != count; i++) {
Send(data[i]);
}
CloseChannel();
}
//
// LOGICDATA protocol
//////////////////////////////////////////////////////////