forked from LAtimes2/TeensyLogicAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_data.ino
352 lines (290 loc) · 10.8 KB
/
send_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
/* Teensy Logic Analyzer
* Copyright (c) 2016 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.
*/
//
// These functions are used to send the data back to the PC after recording
//
// Definitions:
// sample - one sample of data. May be 1 bit, 2 bits, 4 bits, or 8 bits,
// as defined by sv.samplesPerElement, sampleMask, and sampleShift
// element - collection of samples stored as one element. Currently 32 bit integer.
// Oldest sample is in msb of element, newest sample is lsb
// data - all the data that was recorded. An array of elements.
// arrayIndex - index of elements in data (0-based)
// elementIndex - index into an element. 0 is newest sample.
// sampleIndex - index of samples in data. 0 is first address. 0 is not
// necessarily the oldest sample, since the buffer can wrap
// while waiting for a trigger.
byte getSample (sumpSetupVariableStruct setup, int sampleIndex)
{
uint32_t sample;
int arrayIndex = sampleIndex / setup.samplesPerElement;
int elementIndex = setup.samplesPerElement - (sampleIndex % setup.samplesPerElement) - 1;
sample = (setup.startOfBuffer[arrayIndex] >> (setup.sampleShift * elementIndex)) & setup.sampleMask;
// if RLE is selected but it wasn't used, clear top channel
if (setup.rleSelected && !setup.rleUsed)
{
// clear rleCountIndicator for this number of channels
sample = sample & ~setup.rleCountIndicator;
}
return sample;
}
bool sampleIsRleCount (sumpSetupVariableStruct setup, int sampleIndex)
{
bool returnValue = false;
uint32_t sample;
sample = getSample (setup, sampleIndex);
if ((sample & setup.rleCountIndicator) == setup.rleCountIndicator)
{
returnValue = true;
}
return (returnValue);
}
// adjustTrigger
//
// Certain of the fastest modes don't have time to set the trigger precisely. This function
// goes back and looks at the data around the nominal trigger point to find the actual
// trigger location and sets it accordingly.
//
void adjustTrigger (
sumpSetupVariableStruct setup,
sumpDynamicVariableStruct &dynamic)
{
#if HARDWARE_CONFIGURATION
// look from 14 before to 16 after the current trigger index for actual trigger change
// (hardware checks 32 bits at a time)
const int startIndex = -14;
const int endIndex = 16;
#else
// assembly checks every 8 samples
const int startIndex = -7;
const int endIndex = 0;
#endif
int currentStartIndex = startIndex; // can get adjusted at each stage
uint32_t triggerSampleIndex = 0xFFFFFFFF; // set to large invalid value
bool continue_checks = true;
// for each trigger level
for (int level = 0; level <= setup.lastTriggerLevel; ++level) {
for (int index = currentStartIndex; index <= endIndex; ++index) {
// since unsigned, check for wraparound to a very large number (i.e. negative)
if (dynamic.triggerSampleIndex + index > 0xF0000000) {
continue_checks = false;
}
// if an RLE count, don't use it
if (setup.rleSelected && (getSample (setup, dynamic.triggerSampleIndex + index) & setup.rleCountIndicator)) {
continue_checks = false;
}
if (continue_checks) {
// if it is the trigger
if ((getSample (setup, dynamic.triggerSampleIndex + index) & setup.triggerMask[level]) == setup.triggerValue[level])
{
if (level == setup.lastTriggerLevel) {
triggerSampleIndex = dynamic.triggerSampleIndex + index;
// add trigger delay if not at start of search area (may have been true earlier)
if (index != startIndex) {
// add any delay after the trigger
triggerSampleIndex += setup.triggerDelay[level];
}
// don't know why it needs this, but get it to align
if (setup.numberOfChannels == 2) {
dynamic.triggerSampleIndex -= 1;
}
} else {
// not last level - continue with next stage
currentStartIndex = index;
// add trigger delay if not at start of search area (may have been true earlier)
if (index != startIndex) {
currentStartIndex += setup.triggerDelay[level];
// if delay goes past end of search area, start searching at beginning again
// (may have been a false trigger at this level, it was really before search area
// due to large delay)
if (currentStartIndex > endIndex) {
currentStartIndex = startIndex;
}
}
}
break;
} // if trigger
} // if continue_checks
} // for index ...
} // for level ...
// do not go outside of search area
if (triggerSampleIndex <= dynamic.triggerSampleIndex + endIndex &&
(dynamic.bufferHasWrapped || triggerSampleIndex >= setup.delaySamples))
{
dynamic.triggerSampleIndex = triggerSampleIndex;
}
}
void sendData (
sumpSetupVariableStruct sumpSetup,
sumpDynamicVariableStruct dynamic)
{
const byte RLE_MASK = 0x80;
int firstSampleIndex;
uint32_t lastSampleIndex;
uint32_t triggerSampleIndex;
uint32_t firstRLEValue = 0;
int missingSampleCount = 0;
byte sample;
int samplesSentCount = 0;
int samplesToSend = sumpSetup.samplesToSend;
bool wrappedBuffer = false;
// if using trigger
if (sumpSetup.triggerMask[0])
{
adjustTrigger (sumpSetup, dynamic);
}
triggerSampleIndex = dynamic.triggerSampleIndex;
// set unused channels to alternating 1's and 0's
byte unusedValue = 0x55;
// get first sampleIndex to send
firstSampleIndex = triggerSampleIndex - sumpSetup.delaySamples;
if (firstSampleIndex < 0)
{
firstSampleIndex += sumpSetup.samplesToSend;
}
// if using trigger with RLE
if (sumpSetup.triggerMask[0] && sumpSetup.rleSelected)
{
// compute the first sample value in case first sample was an RLE count
for (int32_t elementIndex = sumpSetup.samplesPerElement - 1; elementIndex >= 0; --elementIndex)
{
sample = (sumpSetup.firstRLEValue >> (sumpSetup.sampleShift * elementIndex)) & sumpSetup.sampleMask;
// if it is not an RLE count
if ((sample & sumpSetup.rleCountIndicator) == 0)
{
// save it as the first value to insert in the data stream
firstRLEValue = sample;
}
}
}
// if halted before recording all the data
if (dynamic.interruptedIndex >= 0)
{
// get last sample index to send if it hadn't been interrupted
lastSampleIndex = firstSampleIndex + samplesToSend - 1;
missingSampleCount = lastSampleIndex - dynamic.interruptedIndex;
// if buffer wrapped around
if (missingSampleCount < 0)
{
missingSampleCount += sumpSetup.samplesToRecord;
}
if ((uint32_t)missingSampleCount >= sumpSetup.samplesToRecord)
{
missingSampleCount -= sumpSetup.samplesToRecord;
}
// adjust samples available to be sent
samplesToSend -= missingSampleCount;
}
// get last sampleIndex to send
lastSampleIndex = firstSampleIndex + samplesToSend - 1;
// if buffer wrapped around
if (lastSampleIndex >= sumpSetup.samplesToRecord)
{
wrappedBuffer = true;
lastSampleIndex = lastSampleIndex - sumpSetup.samplesToRecord;
}
// if samples were limited, send bogus data to indicate it is done.
// Send these first since sent backwards in time
for (uint32_t index = samplesToSend; index < sumpSetup.samplesRequested; index = index + 2)
{
// send alternating 1's and 0's
Serial.write(0x55);
Serial.write(0x2A); // keep MSB off for RLE
samplesSentCount = samplesSentCount + 2;
}
// send the data up to end of buffer
int finalIndex;
if (wrappedBuffer)
{
finalIndex = sumpSetup.samplesToRecord - 1;
}
else
{
finalIndex = lastSampleIndex;
}
// data is sent from last to first
// if buffer wrapped, send the last part of the data
if (wrappedBuffer)
{
for (int index = lastSampleIndex; index >= 0; --index)
{
sample = getSample (sumpSetup, index);
// if it is an RLE count, move count indicator to correct bit
if (sumpSetup.rleUsed && (sample & sumpSetup.rleCountIndicator))
{
// clear rleCountIndicator for this number of channels
sample = sample & ~sumpSetup.rleCountIndicator;
// set RLE count indicator to GUI
sample = sample | RLE_MASK;
}
else if (!sumpSetup.rleSelected)
{
sample = (unusedValue & ~sumpSetup.sampleMask) + (sample & sumpSetup.sampleMask);
}
Serial.write(sample);
++samplesSentCount;
unusedValue = ~unusedValue; // toggle 1's and 0's
if (sumpSetup.rleSelected)
{
// RLE must have MSB of unused samples set to 0
unusedValue = unusedValue & 0x7F;
}
}
}
for (int index = finalIndex; index >= firstSampleIndex; --index)
{
sample = getSample (sumpSetup, index);
// if first sample index is an RLE count, set it to a value
if (sumpSetup.rleSelected &&
(index == firstSampleIndex) &&
((sample & RLE_MASK) == RLE_MASK))
{
sample = firstRLEValue;
}
else
{
sample = getSample (sumpSetup, index);
}
// if it is an RLE count
if (sumpSetup.rleUsed && (sample & sumpSetup.rleCountIndicator))
{
// clear rleCountIndicator for this number of channels
sample = sample & ~sumpSetup.rleCountIndicator;
// set RLE count indicator to GUI
sample = sample | RLE_MASK;
}
else if (!sumpSetup.rleSelected)
{
sample = (unusedValue & ~sumpSetup.sampleMask) + (sample & sumpSetup.sampleMask);
}
Serial.write(sample);
++samplesSentCount;
unusedValue = ~unusedValue; // toggle 1's and 0's
if (sumpSetup.rleSelected)
{
// RLE must have MSB of unused samples set to 0
unusedValue = unusedValue & 0x7F;
}
} // for index =
}