-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrecord_high_speed_rle_data.ino
620 lines (522 loc) · 17.7 KB
/
record_high_speed_rle_data.ino
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* Teensy Logic Analyzer
* Copyright (c) 2018 LAtimes2
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
//
// This function records data using Run Length Encoding (RLE).
// This is useful when the data does not change very often. If the
// value does not change from the previous value, it just increments
// a count. When the value changes again, it stores an extra sample
// with the count, along with the highest channel set to 1 (e.g.
// channel 7 for 8 channel mode, channel 3 for 4 channel mode). This
// means that the highest channel cannot be used for data.
//
void recordHighSpeedRLEData (sumpSetupVariableStruct &sv,
sumpDynamicVariableStruct &dynamic)
{
// hard-code values since always 8 channels
const byte samplesPerElement = 4;
const byte samplesPerElementMinusOne = 3;
const uint32_t sampleMask = 0x7F;
const uint32_t sampleShift = 8;
const uint32_t rleCountIndicator = 0x80;
const uint32_t anyDataMask = 0x80808080;
// packed booleans can be written/read as fast as regular booleans
struct Packed_Bools {
bool bufferHasWrapped : 1;
bool doneRecording : 1;
bool rleInProgress : 1;
bool simpleTrigger : 1;
bool skipWait : 1;
};
union Packed_Bools_Union {
Packed_Bools b;
int32_t integer;
};
// pack into a single int to fit in 1 register
struct Packed_Type {
uint8_t triggerMask : 8;
uint8_t triggerValue : 8;
uint16_t triggerDelay : 16;
};
union Packed_Union {
Packed_Type p;
int32_t integer;
};
Packed_Bools_Union bools;
Packed_Union packed;
Packed_Union triggerArray[4];
//// bool bufferHasWrapped = false;
bools.b.bufferHasWrapped = false;
int elementsToRecord = sv.samplesToRecord / sv.samplesPerElement;
register uint32_t *inputPtr = (uint32_t *)sv.startOfBuffer;
uint32_t *endOfBuffer = (uint32_t *)sv.endOfBuffer;
uint32_t *startOfBuffer = (uint32_t *)sv.startOfBuffer;
uint32_t *startPtr = (uint32_t *)sv.startOfBuffer;
//// byte samplesPerElement = sv.samplesPerElement;
//// byte samplesPerElementMinusOne = samplesPerElement - 1;
// shift right 1 to mask off upper channel, which is used for RLE
//// uint32_t sampleMask = sv.sampleMask >> 1;
//// uint32_t anyDataMask = sv.anyDataMask;;
// this is to set the highest channel for an RLE count
//// uint32_t rleCountIndicator = sv.rleCountIndicator;
//// uint32_t sampleShift = sv.sampleShift;
int sampleValue = -1;
// int previousSampleValue = -1;
uint32_t previousFirstValue = 0;
//// bool rleInProgress = false;
bools.b.rleInProgress = false;
// int triggerCount = samplesPerElementMinusOne;
register int workingCount = samplesPerElementMinusOne + 1;
register uint32_t workingValue = 0;
bools.b.simpleTrigger = false;
bools.b.skipWait = false;
int currentTriggerLevel = 0;
//// uint32_t triggerMask = sv.triggerMask[0];
//// uint32_t triggerValue = sv.triggerValue[0];
//// uint32_t triggerDelay = sv.triggerDelay[0];
packed.p.triggerMask = sv.triggerMask[0];
packed.p.triggerValue = sv.triggerValue[0];
packed.p.triggerDelay = sv.triggerDelay[0];
// state is not used except to make a switch context for ptr
stateType state;
// ptr points to a label inside the switch statement to speed it up,
// since it doesn't have to calculate the jump table each time through.
// Label names are the case names with '_Label' added
register void *switch_ptr;
// set up trigger array
if (sv.lastTriggerLevel == 0)
{
currentTriggerLevel = 0;
if (sv.triggerDelay[0] == 0)
{
bools.b.simpleTrigger = true;
}
DEBUG_SERIAL (print("Packed int:"));
DEBUG_SERIAL (println(packed.integer, HEX));
}
else if (sv.lastTriggerLevel == 1)
{
currentTriggerLevel = 1;
triggerArray[0].integer = sv.triggerMask[1] + (sv.triggerValue[1] << 8) + (sv.triggerDelay[1] << 16);
}
else if (sv.lastTriggerLevel == 2)
{
currentTriggerLevel = 2;
triggerArray[1].integer = sv.triggerMask[1] + (sv.triggerValue[1] << 8) + (sv.triggerDelay[1] << 16);
triggerArray[0].integer = sv.triggerMask[2] + (sv.triggerValue[2] << 8) + (sv.triggerDelay[2] << 16);
}
else if (sv.lastTriggerLevel == 3)
{
currentTriggerLevel = 3;
triggerArray[2].integer = sv.triggerMask[1] + (sv.triggerValue[1] << 8) + (sv.triggerDelay[1] << 16);
triggerArray[1].integer = sv.triggerMask[2] + (sv.triggerValue[2] << 8) + (sv.triggerDelay[2] << 16);
triggerArray[0].integer = sv.triggerMask[3] + (sv.triggerValue[3] << 8) + (sv.triggerDelay[3] << 16);
}
// if using a trigger
if (sv.triggerMask[0])
{
switch_ptr = &&Buffering_Label;
// position to arm the trigger
startPtr = inputPtr + sv.delaySizeInElements;
}
else
{
switch_ptr = &&Triggered_Second_Pass_Label;
startPtr = endOfBuffer;
}
// 100% causes a problem with circular buffer - never stops
if (startPtr >= endOfBuffer)
{
startPtr = endOfBuffer - 1;
}
maskInterrupts ();
// read enough samples prior to arming to meet the pre-trigger request
// (for speed, use while (1) and break instead of while (inputPtr != startPtr))
while (1)
{
/*
if (bools.b.skipWait)
{
bools.b.skipWait = false;
clearTimerFlag ();
}
else
*/
{
//// waitForTimeout ();
waitForTimeout3 ();
}
////Skip_Wait_Label:
//// digitalWriteFast (TIMING_PIN_1, LOW);
// read a sample
// sampleValue = PORT_DATA_INPUT_REGISTER & sampleMask;
if ((PORT_DATA_INPUT_REGISTER & sampleMask) != (uint32_t)sampleValue)
////if ((temp = (PORT_DATA_INPUT_REGISTER & sampleMask)) != sampleValue)
// if (sampleValue != previousSampleValue)
{
sampleValue = PORT_DATA_INPUT_REGISTER & sampleMask;
////sampleValue = temp;
#ifdef TIMING_DISCRETES
toggleTimingPin1 ();
#endif
// bools.b.skipWait = true;
// if previous rle count has not been written
if (workingCount == 0)
{
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_3, HIGH);
#endif
*inputPtr = workingValue;
++inputPtr;
// adjust for circular buffer wraparound at the end
if (inputPtr >= endOfBuffer)
{
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_5, HIGH);
#endif
inputPtr = startOfBuffer;
bools.b.bufferHasWrapped = true;
// if any data is received from PC, then stop (assume it is a reset)
if (usbInterruptPending ())
{
DEBUG_SERIAL(print(" Halt due to USB interrupt"));
set_led_off ();
SUMPreset();
break;
}
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_5, LOW);
#endif
}
// save new value (no need to shift since just saved)
workingValue = sampleValue;
workingCount = samplesPerElementMinusOne;
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_3, LOW);
#endif
}
else
{
// save new value
workingValue = (workingValue << sampleShift) + sampleValue;
--workingCount;
}
// previousSampleValue = sampleValue;
bools.b.rleInProgress = false;
}
else // same
{
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_2, HIGH);
#endif
if (bools.b.rleInProgress == false)
{
// save count for previous value
workingValue = (workingValue << sampleShift) + rleCountIndicator + 1;
--workingCount;
bools.b.rleInProgress = true;
}
else
{
// number of RLE instances is stored in the working Value
workingValue++;
// if RLE count is at the maximum value
if ((workingValue & sampleMask) == sampleMask)
{
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_4, HIGH);
#endif
// force current count to be written and new count started
bools.b.rleInProgress = false;
// not enough time to check if also going to write working value
if (workingCount != 0)
{
// if any data is received from PC, then stop (assume it is a reset)
if (usbInterruptPending ())
{
DEBUG_SERIAL(print(" Halt due to USB interrupt"));
set_led_off ();
SUMPreset();
break;
}
}
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_4, LOW);
digitalWriteFast (TIMING_PIN_4, HIGH);
digitalWriteFast (TIMING_PIN_4, LOW);
#endif
}
}
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_2, LOW);
#endif
}
// save the working value when it is full
if (workingCount == 0 && bools.b.rleInProgress == false)
{
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_3, HIGH);
#endif
*inputPtr = workingValue;
++inputPtr;
// adjust for circular buffer wraparound at the end
if (inputPtr >= endOfBuffer)
{
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_5, HIGH);
#endif
inputPtr = startOfBuffer;
bools.b.bufferHasWrapped = true;
// if any data is received from PC, then stop (assume it is a reset)
if (usbInterruptPending ())
{
DEBUG_SERIAL(print(" Halt due to USB interrupt"));
set_led_off ();
SUMPreset();
break;
}
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_5, LOW);
#endif
}
workingCount = samplesPerElement;
workingValue = 0;
#ifdef TIMING_DISCRETES_2
digitalWriteFast (TIMING_PIN_3, LOW);
#endif
} // if workingCount == 0
else if (workingCount == 1)
{
// just before overwriting old data, check if it has data in it.
// This is to prevent having just RLE counts at the start of the
// buffer because it wrapped arn overwrote the original first value.
// if any data (i.e. not just RLE counts) in this value, save it
if ((*inputPtr & anyDataMask) != anyDataMask)
{
previousFirstValue = *inputPtr;
}
}
//
// For speed, perform the switch statement using a pointer to
// the current state and a goto. The switch statement has to
// be in the code so the compiler sets it up properly, but
// the goto uses the duplicate labels for each case.
//
goto *switch_ptr;
switch (state) {
case LookingForSimpleTrigger :
LookingForSimpleTrigger_Label:
// if trigger has occurred
if ((sampleValue & packed.p.triggerMask) == packed.p.triggerValue)
{
// last location to save
startPtr = inputPtr - sv.delaySizeInElements;
// move to triggered state
// state = Triggered_First_Pass;
switch_ptr = &&Triggered_First_Pass_Label;
#ifdef TIMING_DISCRETES
digitalWriteFast (TIMING_PIN_1, LOW);
#endif
}
break;
case LookingForTrigger :
LookingForTrigger_Label:
// if trigger has occurred
if ((sampleValue & packed.p.triggerMask) == packed.p.triggerValue)
{
if (packed.p.triggerDelay > 0) {
// state = TriggerDelay;
switch_ptr = &&TriggerDelay_Label;
} else {
// if last trigger level
if (currentTriggerLevel == 0)
{
// last location to save
startPtr = inputPtr - sv.delaySizeInElements;
// move to triggered state
// state = Triggered_First_Pass;
switch_ptr = &&Triggered_First_Pass_Label;
#ifdef TIMING_DISCRETES
digitalWriteFast (TIMING_PIN_1, LOW);
#endif
} else {
#ifdef TIMING_DISCRETES
digitalWriteFast (TIMING_PIN_1, LOW);
#endif
// advance to next trigger level
--currentTriggerLevel;
packed.integer = triggerArray[currentTriggerLevel].integer;
#ifdef TIMING_DISCRETES
digitalWriteFast (TIMING_PIN_1, HIGH);
#endif
}
}
}
break;
case TriggerDelay :
TriggerDelay_Label:
--packed.p.triggerDelay;
if (packed.p.triggerDelay == 0) {
// if last trigger level
if (currentTriggerLevel == 0) {
// last location to save
startPtr = inputPtr - sv.delaySizeInElements;
// move to triggered state
// state = Triggered_First_Pass;
switch_ptr = &&Triggered_First_Pass_Label;
} else {
--currentTriggerLevel;
packed.integer = triggerArray[currentTriggerLevel].integer;
// state = LookingForTrigger;
switch_ptr = &&LookingForTrigger_Label;
}
}
break;
case Triggered:
Triggered_Label:
if (inputPtr == startPtr) {
// done recording. Use a goto for speed so that
// no 'if' needed to check for done in the main loop
goto DoneRecording;
}
break;
case Buffering:
Buffering_Label:
// if enough data is buffered
if (inputPtr >= startPtr)
{
// move to armed state
// state = LookingForTrigger;
switch_ptr = &&LookingForTrigger_Label;
set_led_on ();
#ifdef TIMING_DISCRETES
digitalWriteFast (TIMING_PIN_1, HIGH);
#endif
}
break;
case Triggered_Second_Pass:
Triggered_Second_Pass_Label:
// adjust for circular buffer wraparound at the end.
if (startPtr < startOfBuffer)
{
startPtr = startPtr + elementsToRecord;
}
// move to triggered state
// state = Triggered;
switch_ptr = &&Triggered_Label;
#ifdef TIMING_DISCRETES
digitalWriteFast (TIMING_PIN_1, LOW);
#endif
break;
case Triggered_First_Pass:
Triggered_First_Pass_Label:
// go as fast as possible to try to catch up from Triggered state
// state = Triggered_Second_Pass;
switch_ptr = &&Triggered_Second_Pass_Label;
set_led_off (); // TRIGGERED, turn off LED
break;
}
// } // if state == LookingForTrigger
} // while (1)
DoneRecording:
// cleanup
unmaskInterrupts ();
#ifdef TIMING_DISCRETES
digitalWriteFast (TIMING_PIN_0, LOW);
#endif
// save the first value in case it was overwritten due to buffer overflow
// (i.e. first sample is an RLE count, but what was the value it is counting?)
sv.firstRLEValue = previousFirstValue;
// adjust trigger count
dynamic.triggerSampleIndex = (startPtr + sv.delaySizeInElements - startOfBuffer) * samplesPerElement + samplesPerElementMinusOne;
dynamic.bufferHasWrapped = bools.b.bufferHasWrapped;
// adjust for circular buffer wraparound at the end.
if (dynamic.triggerSampleIndex >= (uint32_t)sv.samplesToRecord)
{
dynamic.triggerSampleIndex = dynamic.triggerSampleIndex - sv.samplesToRecord;
}
if (inputPtr != startPtr)
{
int deltaElements = inputPtr - startOfBuffer;
if (deltaElements < 0)
{
deltaElements += elementsToRecord;
}
dynamic.interruptedIndex = deltaElements * samplesPerElement;
}
}
inline void toggleTimingPin0 () {
*portToggleRegister (TIMING_PIN_0) = 1;
}
inline void toggleTimingPin1 () {
//// *portToggleRegister (TIMING_PIN_1) = 1;
*portToggleRegister (TIMING_PIN_3) = 1;
}
inline void waitForTimeout2 (void)
{
#ifdef TIMING_DISCRETES
toggleTimingPin0 ();
#endif
// for speed, to reduce jitter
if (TIMER_FLAG_REGISTER)
{
clearTimerFlag ();
} else if (TIMER_FLAG_REGISTER) {
clearTimerFlag ();
} else {
// digitalWriteFast (TIMING_PIN_0, LOW);
// while (!TIMER_FLAG_REGISTER);
// digitalWriteFast (TIMING_PIN_0, HIGH);
// clearTimerFlag ();
// }
waitStart:
if (TIMER_FLAG_REGISTER) goto waitEnd;
if (TIMER_FLAG_REGISTER) goto waitEnd;
if (TIMER_FLAG_REGISTER) goto waitEnd;
if (TIMER_FLAG_REGISTER) goto waitEnd;
if (TIMER_FLAG_REGISTER) goto waitEnd;
if (TIMER_FLAG_REGISTER) goto waitEnd;
if (TIMER_FLAG_REGISTER) goto waitEnd;
if (!TIMER_FLAG_REGISTER) goto waitStart;
waitEnd:
clearTimerFlag ();
}
#ifdef TIMING_DISCRETES
toggleTimingPin0 ();
#endif
}
inline void waitForTimeout3 (void)
{
#ifdef TIMING_DISCRETES
// digitalWriteFast (TIMING_PIN_0, HIGH);
toggleTimingPin0 ();
#endif
// WaitCount has to be less than cpu cycles in the shortest
// loop (Do_Triggered?), so that it doesn't start too early
const int WaitCount = 24 / (F_CPU / F_BUS);
while (PIT_CVAL0 > WaitCount);
#ifdef TIMING_DISCRETES
// digitalWriteFast (TIMING_PIN_0, LOW);
toggleTimingPin0 ();
#endif
}