-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrecord_hardware.h
512 lines (422 loc) · 12.7 KB
/
record_hardware.h
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
/* 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 file is designed to be included multiple times with different
// preprocessor directives. This will provide maximum speed at the
// expense of extra code memory, which is currently not an issue.
//
bool checkTrigger (
TriggerType triggerType,
uint16_t sampleChan0,
uint16_t sampleChan1);
// various function names
#if MULTIPLE_CHANNELS
#if USE_TRIGGER
void recordSPIData_MultiChannel_Trigger (sumpSetupVariableStruct &sv,
sumpDynamicVariableStruct &dynamic)
#elif USE_PRE_TRIGGER
void recordSPIData_MultiChannel_Pretrigger (sumpSetupVariableStruct &sv,
sumpDynamicVariableStruct &dynamic)
#else
void recordSPIData_MultiChannel (sumpSetupVariableStruct &sv,
sumpDynamicVariableStruct &dynamic)
#endif
#else
#if USE_TRIGGER
void recordSPIData_SingleChannel_Trigger (sumpSetupVariableStruct &sv,
sumpDynamicVariableStruct &dynamic)
#elif USE_PRE_TRIGGER
void recordSPIData_SingleChannel_Pretrigger (sumpSetupVariableStruct &sv,
sumpDynamicVariableStruct &dynamic)
#else
void recordSPIData_SingleChannel (sumpSetupVariableStruct &sv,
sumpDynamicVariableStruct &dynamic)
#endif
#endif
{
#define SPI_SR_TXCTR_MASK 0x0000F000
#define SPI_SR_RXCTR_MASK 0x000000F0
uint32_t *startOfBuffer = (uint32_t *)sv.startOfBuffer;
register uint32_t *inputPtr = (uint32_t *)sv.startOfBuffer;
register uint32_t *endOfBuffer = (uint32_t *)sv.endOfBuffer;
register uint32_t *startPtr = startOfBuffer;
register uint32_t sampleChan0;
bool bufferHasWrapped = false;
byte triggerMask = sv.triggerMask[0];
byte triggerValue = sv.triggerValue[0];
#if MULTIPLE_CHANNELS
register uint32_t sampleChan1;
// ignore unused channels
triggerMask &= 0x03;
triggerValue &= 0x03;
#else
// ignore unused channels
triggerMask &= 0x01;
triggerValue &= 0x01;
#endif
#if USE_TRIGGER || USE_PRE_TRIGGER
TriggerType triggerType = Channel0Low;
#if USE_TRIGGER
int elementsToRecord = sv.samplesToRecord / sv.samplesPerElement;
byte samplesPerElementMinusOne = sv.samplesPerElement - 1;
register stateType state = Buffering;
bool triggered = false;
#endif
// if using a trigger
if (triggerMask)
{
#if USE_TRIGGER
state = Buffering;
// position to arm the trigger (add 6 to clear FIFO at start)
startPtr = inputPtr + sv.delaySizeInElements + 6;
#else // pre-trigger
// set delay to 0
sv.delaySamples = 0;
sv.delaySizeInElements = 0;
// position to stop recording
// (assumes multiple of 2 ints')
startPtr = endOfBuffer;
#endif
if (triggerMask == 1 && triggerValue == 0) triggerType = Channel0Low;
if (triggerMask == 1 && triggerValue == 1) triggerType = Channel0High;
if (triggerMask == 2 && triggerValue == 0) triggerType = Channel1Low;
if (triggerMask == 2 && triggerValue == 2) triggerType = Channel1High;
if (triggerMask == 3 && triggerValue == 0) triggerType = BothChannelsLow;
if (triggerMask == 3 && triggerValue == 1) triggerType = HighLow;
if (triggerMask == 3 && triggerValue == 2) triggerType = LowHigh;
if (triggerMask == 3 && triggerValue == 3) triggerType = BothChannelsHigh;
}
else
{
#if USE_TRIGGER
state = Triggered_First_Pass;
#endif
startPtr = endOfBuffer;
}
#else // no trigger
startPtr = endOfBuffer;
#endif
// 100% causes a problem with circular buffer - never stops
if (startPtr >= endOfBuffer)
{
startPtr = endOfBuffer - 2;
}
spi1Initialize ();
spi1Setup (sv.clockFrequency);
#if MULTIPLE_CHANNELS
{
spi0Initialize ();
spi0Setup (sv.clockFrequency);
}
#endif
maskInterrupts ();
#if USE_PRE_TRIGGER
//////////////////////
//
// pre-trigger
//
//////////////////////
uint16_t sample_chan0;
uint16_t sample_chan1;
bool done = false;
// if using a trigger
if (sv.triggerMask[0])
{
while (!done)
{
sample_chan0 = (digitalReadFast (CHAN0) ? 0xFFFF : 0);
sample_chan1 = (digitalReadFast (CHAN1) ? 0xFFFF : 0);
done = checkTrigger (
triggerType,
sample_chan0,
sample_chan1);
// 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();
unmaskInterrupts ();
return;
}
}
}
#endif
#if MULTIPLE_CHANNELS
startSPIClock (true, sv.cpuClockTicks);
#else
startSPIClock (false, sv.cpuClockTicks);
#endif
//////////////////////
//
// main loop
//
//////////////////////
while (1)
{
#if Teensy_LC
// if data is ready to read
if (SPI1_S & SPI_S_SPRF) {
sampleChan0 = SPI1_DL | (SPI1_DH << 8);
spi1StartTransfer ();
#if MULTIPLE_CHANNELS
{
// clear status
SPI0_S;
sampleChan1 = SPI0_DL | (SPI0_DH << 8);
spi0StartTransfer ();
}
#endif
// wait until data is ready to read
while (!(SPI1_S & SPI_S_SPRF));
*(inputPtr) = sampleChan0 = (sampleChan0 << 16) + SPI1_DL + (SPI1_DH << 8);
++inputPtr;
spi1StartTransfer ();
#if MULTIPLE_CHANNELS
{
// clear status
SPI0_S;
*(inputPtr) = sampleChan1 = (sampleChan1 << 16) + SPI0_DL + (SPI0_DH << 8);
++inputPtr;
spi0StartTransfer ();
}
#endif
#elif Teensy_3_5 || Teensy_3_6
// if data is ready to read
if (SPI1_SR & SPI_SR_RXCTR_MASK)
{
sampleChan0 = SPI1_POPR;
// start next transfer
SPI1_PUSHR = SPI_PUSHR_CONT;
#if MULTIPLE_CHANNELS
{
sampleChan1 = SPI0_POPR;
SPI0_PUSHR = SPI_PUSHR_CONT;
}
#endif
// wait until data is ready to read
while (!(SPI1_SR & SPI_SR_RXCTR_MASK));
*(inputPtr) = sampleChan0 = (sampleChan0 << 16) + SPI1_POPR;
++inputPtr;
// start next transfer
SPI1_PUSHR = SPI_PUSHR_CONT;
#if MULTIPLE_CHANNELS
{
*(inputPtr) = sampleChan1 = (sampleChan1 << 16) + SPI0_POPR;
++inputPtr;
// start next transfer
SPI0_PUSHR = SPI_PUSHR_CONT;
}
#endif
#else
// if at least 2 data values are ready to read
// (for speed, want constant mask of 0E0, so 'and' 0F0 and 1E0)
if (SPI1_SR & SPI_SR_RXCTR_MASK & (SPI_SR_RXCTR_MASK << 1))
{
*(inputPtr) = sampleChan0 = (SPI1_POPR << 16) + SPI1_POPR;
++inputPtr;
// start next transfer
SPI1_PUSHR = SPI_PUSHR_CONT;
SPI1_PUSHR = SPI_PUSHR_CONT;
#if MULTIPLE_CHANNELS
{
*(inputPtr) = sampleChan1 = (SPI0_POPR << 16) + SPI0_POPR;
++inputPtr;
SPI0_PUSHR = SPI_PUSHR_CONT;
SPI0_PUSHR = SPI_PUSHR_CONT;
}
#endif
#endif
#if !USE_TRIGGER
if (inputPtr == startPtr) {
// done recording
break;
}
#else
// adjust for circular buffer wraparound at the end
if (inputPtr >= endOfBuffer)
{
inputPtr = sv.startOfBuffer;
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;
}
}
switch (state)
{
case LookingForTrigger :
// if trigger has occurred
switch (triggerType)
{
case Channel0High:
triggered = (sampleChan0 != 0);
break;
case Channel0Low:
triggered = sampleChan0 != 0xFFFFFFFF;
break;
#if MULTIPLE_CHANNELS
case Channel1High:
triggered = sampleChan1 != 0;
break;
case Channel1Low:
triggered = sampleChan1 != 0xFFFFFFFF;
break;
case BothChannelsHigh:
triggered = (sampleChan0 & sampleChan1) != 0;
break;
case BothChannelsLow:
triggered = (sampleChan0 | sampleChan1) != 0xFFFFFFFF;
break;
case HighLow:
triggered = (sampleChan0 & ~sampleChan1) != 0;
break;
case LowHigh:
triggered = (sampleChan0 | ~sampleChan1) != 0xFFFFFFFF;
break;
#endif
default:
break;
}
if (triggered)
{
// subtract 2 since inputPtr has already been incremented
startPtr = inputPtr - 2 - sv.delaySizeInElements;
// move to triggered state
state = Triggered_First_Pass;
}
break;
case TriggerDelay :
break;
case Triggered :
if (inputPtr == startPtr)
{
// done recording
goto DoneRecording;
}
break;
case Buffering :
// if enough data is buffered
if (inputPtr >= startPtr)
{
// move to armed state
state = LookingForTrigger;
set_led_on ();
#ifdef TIMING_DISCRETES
digitalWriteFast (TIMING_PIN_1, HIGH);
#endif
}
break;
case Triggered_First_Pass :
// go as fast as possible to try to catch up from Triggered state
state = Triggered_Second_Pass;
break;
case Triggered_Second_Pass :
// adjust for circular buffer wraparound at the end.
if (startPtr < startOfBuffer)
{
startPtr = startPtr + elementsToRecord;
}
// move to triggered state
state = Triggered;
set_led_off (); // TRIGGERED, turn off LED
#ifdef TIMING_DISCRETES
digitalWriteFast (TIMING_PIN_1, LOW);
#endif
break;
default:
break;
} // end switch
#endif
} // if data is ready to be read
} // while (1)
#if USE_TRIGGER
DoneRecording:
#endif
unmaskInterrupts ();
#if USE_TRIGGER
// set trigger count
dynamic.triggerSampleIndex = (startPtr + sv.delaySizeInElements - startOfBuffer) * sv.samplesPerElement + samplesPerElementMinusOne;
// adjust for circular buffer wraparound at the end.
if (dynamic.triggerSampleIndex >= (uint32_t)sv.samplesToRecord)
{
dynamic.triggerSampleIndex = dynamic.triggerSampleIndex - sv.samplesToRecord;
}
#else
dynamic.triggerSampleIndex = sv.samplesPerElement;
#endif
dynamic.bufferHasWrapped = bufferHasWrapped;
// turn off SPI module
#if MULTIPLE_CHANNELS
spiDisable (true);
#else
spiDisable (false);
#endif
}
// only need to include this routine once
#ifndef CHECKTRIGGER_DEFINED
#define CHECKTRIGGER_DEFINED
inline bool checkTrigger (
TriggerType triggerType,
uint16_t sampleChan0,
uint16_t sampleChan1)
{
bool triggered = false;
// if trigger has occurred
switch (triggerType)
{
case Channel0High:
triggered = sampleChan0 != 0;
break;
case Channel0Low:
triggered = sampleChan0 != 0xFFFF;
break;
case Channel1High:
triggered = sampleChan1 != 0;
break;
case Channel1Low:
triggered = sampleChan1 != 0xFFFF;
break;
case BothChannelsHigh:
triggered = (sampleChan0 & sampleChan1) != 0;
break;
case BothChannelsLow:
triggered = (sampleChan0 | sampleChan1) != 0xFFFF;
break;
case HighLow:
triggered = (sampleChan0 & ~sampleChan1) != 0;
break;
case LowHigh:
triggered = (sampleChan0 | ~sampleChan1) != 0xFFFF;
break;
default:
break;
}
return triggered;
}
#endif