-
Notifications
You must be signed in to change notification settings - Fork 13
/
testPins_pulse.cpp
332 lines (293 loc) · 8.8 KB
/
testPins_pulse.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
/*
* This controls one pin : pull it up, down, etc.. and measure
*
*/
#include "vector"
#include "testPins.h"
#include "dso_adc.h"
#include "MapleFreeRTOS1000_pp.h"
#include "testerGfx.h"
#include "myPwm.h"
#include "testerControl.h"
extern DSOADC *adc;
void xFail(const char *message);
/**
*
* @param samplingFrequency
* @param strength
* @param prescaler
* @param rate
* @param sampleOut
* @param xsamples
* @return
*/
typedef struct ScalerTable
{
float div;
DSOADC::Prescaler scaler;
};
typedef struct RateTable
{
float cycle;
adc_smp_rate rate;
};
const ScalerTable scalerTable[]=
{
{2.0, DSOADC::ADC_PRESCALER_2},
{4.0, DSOADC::ADC_PRESCALER_4},
{6.0, DSOADC::ADC_PRESCALER_6},
{8.0, DSOADC::ADC_PRESCALER_8}
};
#define RATE_MK(x) { 12+x##.5, ADC_SMPR_##x##_5},
const RateTable rateTable[]=
{
RATE_MK(1)
RATE_MK(7)
RATE_MK(13)
RATE_MK(28)
RATE_MK(41)
RATE_MK(55)
RATE_MK(71)
RATE_MK(239)
};
#define INCREMENT 1
/**
*/
class PulseSetting
{
public:
PulseSetting( TestPin &xtestPin, TestPin::PULL_STRENGTH strength) : testPin(xtestPin)
{
switch(strength)
{
case TestPin::PULL_LOW: pin=testPin._pinDriveLowRes;pinState=TestPin::PULLUP_LOW;break;
case TestPin::PULL_MED: pin=testPin._pinDriveMedRes;pinState=TestPin::PULLUP_MED;break;
case TestPin::PULL_HI: pin=testPin._pinDriveHighRes;pinState=TestPin::PULLUP_HI;break;
default:
break;
}
res=testPin.getRes(pinState)+testPin.getRes(TestPin::GND);
}
/**
*
* @param samplingFrequency
* @return
*/
bool init(int samplingFrequency, bool forceHighestSpeed=false)
{
if(forceHighestSpeed)
{ // to minimize effect on noise for very small caps
prescaler= DSOADC::ADC_PRESCALER_2;
rate= ADC_SMPR_1_5;
}else
{
DSOADC::frequencyToRateScale(samplingFrequency,prescaler,rate);
}
Logger("ADC rate=%d scale=%d",prescaler,rate);
int cmp;
pwmGetScaleOverFlowCompare(samplingFrequency,timerScaler,timerOvf,cmp);
return true;
}
/**
* \brief compute the offset so that the ramp up is near the beginning
* increasing the shift makes the curve later
* @param adc
* @param timerScaler
* @param timerOvf
* @param prescaler
* @param rate
* @param offset
* @return
*/
bool createWaveForm( int clockPerSample, int sampleAsked, int &nbSamples, uint16_t **samples)
{
// The apparent sampling frequency is F_CPU/(timerScaler*apprentDivider)
// make it so timerSCaler*apparentDivier=8
// so we end up with a know sampling frequency of F_CPU/8 => 9 Mhz with CPU@72 Mhz whatever timerScale is
// as long as it is 1 2 or 4, from 500 Hz to ~ 10 khz
int apparentDivider=clockPerSample/timerScaler;
if(!apparentDivider) xAssert(0);
// ADC is running X cycles faster than repeat
// Same thing as ~ adc running at X Cycle
if(!adc->prepareTimerSampling(timerScaler,timerOvf+apparentDivider,1,rate,prescaler))
{
xAssert(0);
return false;
}
adc->clearSemaphore();
int before=millis();
nbSamples=0;
pwmQuickRestart quick(pin);
noInterrupts();
adc->startTimerSampling(sampleAsked);
quick.go();
interrupts();
if(!adc->getSamples(samples,nbSamples))
{
xAssert(0);
adc->stopTimeCapture();
return false;
}
// skip 1st sample
nbSamples-=1;
(*samples)+=1;
adc->stopTimeCapture();
pwmPause(pin);
offset=0;
return true;
}
bool createWaveFormDelta(TestPin &otherPin, int &clockPerSample, int sampleAsked, int &nbSamples, uint16_t **samples)
{
// The apparent sampling frequency is F_CPU/(timerScaler*apprentDivider)
// clockPerSample is in F_CPU tick, we know the timerScaler
// so we can compute the apparentDivider i.e. the offset we have to put in the ADC
// to get the correct clockPerSample
int apparentDivider=clockPerSample/timerScaler;
clockPerSample=apparentDivider*timerScaler; // update it in case we have a rounding issue
if(!apparentDivider) xAssert(0);
// ADC is running X cycles faster than repeat
// Same thing as ~ adc running at X Cycle
Logger("Waveform : clockPerSample=%d timerScaler=%d timerOvf=%d apparentDivider=%d",clockPerSample,timerScaler,timerOvf,apparentDivider);
if(!adc->prepareDualTimerSampling(timerScaler,timerOvf+apparentDivider,1,rate,prescaler))
{
xAssert(0);
return false;
}
adc->clearSemaphore();
int before=millis();
nbSamples=0;
pwmQuickRestart quick(pin);
noInterrupts();
adc->startDualTimeSampling(otherPin._pin,sampleAsked,0*(timerOvf-apparentDivider));
quick.go();
interrupts();
//while(1) {}
if(!adc->getSamples(samples,nbSamples))
{
xAssert(0);
adc->stopTimeCapture();
return false;
}
//
adc->stopTimeCapture();
nbSamples=(nbSamples)/2;
// Delta
testPin.dualSimulatenousDelta(nbSamples,*samples);
// skip the 2 first ones
nbSamples-=2;
(*samples)+=2;
pwmPause(pin);
offset=0;
return true;
}
public:
int pin;
int timerScaler;
int timerOvf;
DSOADC::Prescaler prescaler;
adc_smp_rate rate ;
int offset;
TestPin::TESTPIN_STATE pinState;
int res;
TestPin &testPin;
};
/**
*
* @param nbSampleAsked
* @param clockPerSample : We target clockPerSample vs F_CPU as apparent sampling frequency
* internally it is F_CPU/(samplingTime)
* @param samplingFrequency : The input signal is repeated every samplingFrequency
* @param strength : Pull up strength
* @param nbSample
* @param xsamples
* @param res : Load Resistance
* @return
*/
bool TestPin::pulseTime(int clockPerSample,int nbSampleAsked, int samplingFrequency, TestPin::PULL_STRENGTH strength, int &nbSample, uint16_t **xsamples,int &res)
{
disconnectAll();
PulseSetting settings(*this,strength);
pullDown(TestPin::PULL_LOW);
xDelay(10);
settings.init(samplingFrequency);
pwm(strength,samplingFrequency);
pwmPause(settings.pin);
adc->setADCPin(this->_pin);
adc->setupTimerSampling();
//
if(!settings.createWaveForm(clockPerSample,nbSampleAsked,nbSample,xsamples ))
{
return false;
}
pullDown(strength);
xDelay(10);
res=settings.res;
disconnectAll();
return true;
}
/**
*
* @param otherPin
* @param clockPerSample
* @param nbSampleAsked
* @param samplingFrequency
* @param strength
* @param nbSample
* @param xsamples
* @param res
* @return
*/
bool TestPin::pulseTimeDelta(TestPin &otherPin, int &clockPerSample,int nbSampleAsked, int samplingFrequency, TestPin::PULL_STRENGTH strength, int &nbSample, uint16_t **xsamples,int &res,bool highspeed)
{
disconnectAll();
PulseSetting settings(*this,strength);
pullDown(TestPin::PULL_LOW);
xDelay(10);
settings.init(samplingFrequency,highspeed);
pwm(strength,samplingFrequency);
pwmPause(settings.pin);
adc->setADCPin(this->_pin);
adc->setupDualTimerSampling();
//
if(!settings.createWaveFormDelta(otherPin,clockPerSample,nbSampleAsked,nbSample,xsamples ))
{
return false;
}
pullDown(strength);
xDelay(10);
res=settings.res;
disconnectAll();
// Merge
return true;
}
/**
*
* @param nbSamples
* @param samplingFrequency
* @param strength
* @param sampleOut
* @param xsamples
* @return
*/
bool TestPin::pulseDma(int nbSamples, DSOADC::Prescaler prescaler, adc_smp_rate rate, TestPin::PULL_STRENGTH strength, int &sampleOut, uint16_t **xsamples)
{
pullDown(TestPin::PULL_LOW);
xDelay(10);
adc->setADCPin(this->_pin);
adc->setupDmaSampling();
adc->prepareDMASampling(rate,prescaler);
adc->clearSemaphore();
adc->startDMASampling(nbSamples);
pullUp(strength);
bool r=adc->getSamples(xsamples,sampleOut);
adc->stopDmaCapture();
if(!r)
{
xAssert(0);
return false;
}
pullDown(strength);
xDelay(10);
return true;
}
// EOF