-
Notifications
You must be signed in to change notification settings - Fork 1
/
esrCap.cpp
349 lines (290 loc) · 7.93 KB
/
esrCap.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
348
349
#include "fpmath.hpp"
#include "clocks.hpp"
#include "esrCap.hpp"
volatile esr::MeterCalData esr::calData;
volatile cap::MeterCalData cap::calData;
std::uint32_t esrcap::autoScaleGetSample(
esrcap::sampleFunc_t sampleFunc,
esrcap::gainFunc_t gainFunc,
std::uint8_t & oldgain,
bool precisemode
)
{
gainFunc(oldgain);
std::uint32_t sample, avgSample;
avgSample = sampleFunc(precisemode, sample);
std::int32_t gain = (sample < fp::to(0.0625)) ? fp::to(16.0) : fp::div(fp::to(1.0), sample);
gain -= fp::to(0.2);
if (gain < fp::to(1.0))
{
// Set gain to 0.5
gain = 0;
}
else
{
gain = fp::fromI(gain);
// Crude logarithmic conversion
std::int32_t bits = 0;
while (gain >>= 1)
{
++bits;
}
// clamp to 5
gain = (bits > 5) ? 5 : bits;
}
if (gain != oldgain)
{
oldgain = gain;
gainFunc(oldgain);
sampleFunc(precisemode, avgSample);
}
if (gain != 1)
{
gainFunc(1);
}
return avgSample;
}
void esr::init(esrcap::sampleFunc_t sampleFunc, esrcap::gainFunc_t gainFunc)
{
assert(sampleFunc != nullptr);
assert(gainFunc != nullptr);
esr::calData.getSample = sampleFunc;
esr::calData.setGain = gainFunc;
pinMode(ESR_OUT_11X, INPUT);
OutMode(ESR_PWM_OUT_LOW);
OutMode(ESR_PWM_OUT_HIGH);
OutClr(ESR_PWM_OUT_LOW);
OutClr(ESR_PWM_OUT_HIGH);
// Initialize timer clocks
std::uint32_t tmrFreq = 96000000U;
if (!clk::isInit(clk::tmr::tTCC2))
{
clk::initTmr(clk::tmr::tTCC2, GCLK_CLKCTRL_GEN_GCLK4_Val, tmrFreq);
}
else
{
tmrFreq = clk::tmrSpeed(clk::tmr::tTCC2);
}
assert(tmrFreq == 96000000U);
// Set up multiplexing
PORT->Group[PGrp(ESR_PWM_OUT_LOW) ].PINCFG[ESR_PWM_OUT_LOW & 0x1F].reg |= PORT_PINCFG_PMUXEN;
PORT->Group[PGrp(ESR_PWM_OUT_HIGH)].PINCFG[ESR_PWM_OUT_HIGH & 0x1F].reg |= PORT_PINCFG_PMUXEN;
auto func = (ESR_PWM_OUT_LOW % 2) ? ESR_PWM_FUNCTION << PORT_PMUX_PMUXO_Pos : ESR_PWM_FUNCTION << PORT_PMUX_PMUXE_Pos;
PORT->Group[PGrp(ESR_PWM_OUT_LOW) ].PMUX[(ESR_PWM_OUT_LOW & 0x1F) / 2].reg |= func;
func = (ESR_PWM_OUT_HIGH % 2) ? ESR_PWM_FUNCTION << PORT_PMUX_PMUXO_Pos : ESR_PWM_FUNCTION << PORT_PMUX_PMUXE_Pos;
PORT->Group[PGrp(ESR_PWM_OUT_HIGH)].PMUX[(ESR_PWM_OUT_HIGH & 0x1F) / 2].reg |= func;
// Set Wave output
constexpr auto secondChannel = (std::uint32_t[4]){ TCC_WAVE_POL0, TCC_WAVE_POL1, TCC_WAVE_POL2, TCC_WAVE_POL3 }[ESR_PWM_HIGH_CHANNEL];
TCC2->WAVE.reg = secondChannel |
TCC_WAVE_WAVEGEN_DSBOTTOM;
while (TCC2->SYNCBUSY.bit.WAVE);
esr::setFrequency(ESR_DEFAULT_FREQUENCY);
}
void esr::setFrequency(std::uint32_t frequency, bool enableOut)
{
// Calculate ticks from frequency
const auto ticks = 48000000U / frequency;
esr::outputEnable(false);
TCC2->PERB.reg = ticks;
while (TCC2->SYNCBUSY.bit.PERB);
const auto ticks2 = ticks / 2U;
TCC2->CCB[ESR_PWM_LOW_CHANNEL].reg = ticks2 + 2U;
while (TCC2->SYNCBUSY.vec.CCB);
TCC2->CCB[ESR_PWM_HIGH_CHANNEL].reg = ticks2 - 2U;
while (TCC2->SYNCBUSY.vec.CCB);
if (enableOut)
{
esr::outputEnable(true);
}
}
void esr::outputEnable(bool enable)
{
if (!enable && TCC2->CTRLA.bit.ENABLE)
{
// Disable timer
TCC2->CTRLA.bit.ENABLE = 0;
while (TCC2->SYNCBUSY.bit.ENABLE);
// Clear output
OutClr(ESR_PWM_OUT_LOW);
OutClr(ESR_PWM_OUT_HIGH);
}
else if (enable && !TCC2->CTRLA.bit.ENABLE)
{
// Enable timer
TCC2->CTRLA.bit.ENABLE = 1;
while (TCC2->SYNCBUSY.bit.ENABLE);
}
}
bool esr::zeroReading()
{
bool overload;
esr::calData.outputOffset_FPD += esr::measureESR_fpd(overload);
return !overload;
}
std::int32_t esr::measureESR_fpd(bool & overload)
{
auto sample = esrcap::autoScaleGetSample(
esr::calData.getSample,
esr::calData.setGain,
const_cast<std::uint8_t &>(esr::calData.gain),
false
);
auto offset = esr::calData.adcOffsetVolts_FPD[esr::calData.gain];
sample = (offset > sample) ? 0 : sample - offset;
overload = sample > fp::to(1.85);
// Remove amplifier offset to the input voltage
sample -= fp::to(ESR_AMP_OFFSET);
overload |= sample > fp::to(1.7);
SerialUSB.print("Voltage: ");
SerialUSB.println(fp::fromD(sample), 4);
if (overload)
{
return INT32_MAX;
}
return esr::calcESR_fpd(sample);
}
float esr::measureESR(bool & overload)
{
return fp::fromD(esr::measureESR_fpd(overload));
}
std::int32_t esr::calcVoltsPreDivider_fpd(std::int32_t voltage)
{
return fp::div(voltage, esr::calData.vdiv_FPD);
}
std::int32_t esr::calcDetectorAmplitude_fpd(std::int32_t preVoltage)
{
std::int32_t ampl = esr::calcVoltsPreDivider_fpd(preVoltage);
ampl = fp::div(ampl, fp::to(ESR_AMP_GAIN));
ampl -= esr::calData.gainOffset_FPD;
return 2 * ampl;
}
std::int32_t esr::calcESRDivider_fpd(std::int32_t voltage)
{
std::int32_t ampl = esr::calcDetectorAmplitude_fpd(voltage);
return (ampl) ? fp::div(fp::to(ESR_CAP_BURDEN_VOLTAGE), ampl) : INT32_MAX;
}
std::int32_t esr::calcESR_fpd(std::int32_t voltage)
{
auto divisor = esr::calcESRDivider_fpd(voltage) - fp::to(1.0);
if (!divisor)
{
return INT32_MAX;
}
std::int32_t r = fp::div(fp::to(ESR_R_OUT), divisor);
r -= esr::calData.outputOffset_FPD;
return r;
}
void cap::init(esrcap::sampleFunc_t sampleFunc, esrcap::gainFunc_t gainFunc)
{
assert(sampleFunc != nullptr);
assert(gainFunc != nullptr);
cap::calData.getSample = sampleFunc;
cap::calData.setGain = gainFunc;
pinMode(CAP_OUT, INPUT);
pinMode(CAP_RC_DETECT, INPUT);
OutMode(CAP_CHARGE_OUT);
OutMode(CAP_DISCHARGE_OUT);
OutClr(CAP_CHARGE_OUT);
OutClr(CAP_DISCHARGE_OUT);
// Configure timer clock source
clk::initTmr(clk::tmr::tTC4, GCLK_CLKCTRL_GEN_GCLK0_Val, 48000000U);
TC4->COUNT32.CTRLA.reg |=
TC_CTRLA_PRESCALER_DIV1 |
TC_CTRLA_MODE_COUNT32;
while (TC4->COUNT32.STATUS.bit.SYNCBUSY);
}
static void capMeasurementISR()
{
// Capture timer ticks
const auto ticks = TC4->COUNT32.COUNT.reg;
cap::stopMeasurement();
cap::calData.measureTicks = ticks;
// Mark as done
cap::calData.measureDone = true;
}
void cap::startMeasureMent_async()
{
// Disable discharge
OutClr(CAP_DISCHARGE_OUT);
cap::calData.measureDone = false;
// Enable RC time constant interrupt
attachInterrupt(digitalPinToInterrupt(CAP_RC_DETECT), &capMeasurementISR, FALLING);
// Start timer
TC4->COUNT32.COUNT.reg = 0;
while (TC4->COUNT32.STATUS.bit.SYNCBUSY);
TC4->COUNT32.READREQ.reg = TC_READREQ_RCONT | TC_READREQ_ADDR(0x10);
while (TC4->COUNT32.STATUS.bit.SYNCBUSY);
TC4->COUNT32.CTRLA.bit.ENABLE = 1;
while (TC4->COUNT32.STATUS.bit.SYNCBUSY);
// Start charging
OutSet(CAP_CHARGE_OUT);
}
std::uint32_t cap::measureTicks(bool discharge, std::uint32_t timeoutTicks)
{
std::uint32_t ticks;
cap::startMeasureMent_async();
while (!cap::measureTicks_async(ticks, discharge))
{
if (TC4->COUNT32.COUNT.reg > timeoutTicks)
{
cap::stopMeasurement();
return UINT32_MAX;
}
}
return ticks;
}
bool cap::measureTicks_async(std::uint32_t & ticks, bool discharge)
{
auto ret = cap::calData.measureDone;
if (ret)
{
ticks = cap::calData.measureTicks;
}
if (ret && discharge)
{
cap::discharge();
}
cap::calData.measureDone = false;
return ret;
}
void cap::stopMeasurement()
{
// Disable timer
TC4->COUNT32.CTRLA.bit.ENABLE = 0;
while (TC4->COUNT32.STATUS.bit.SYNCBUSY);
// Disable RC interrupt
disableInterrupt(digitalPinToInterrupt(CAP_RC_DETECT));
}
bool cap::isDischarged()
{
const auto sample = esrcap::autoScaleGetSample(
cap::calData.getSample,
cap::calData.setGain,
const_cast<std::uint8_t &>(cap::calData.gain),
true
);
return sample < fp::to(CAP_DISCHARGE_THRESHOLD);
}
void cap::discharge()
{
OutClr(CAP_CHARGE_OUT);
OutSet(CAP_DISCHARGE_OUT);
}
void cap::stop()
{
OutClr(CAP_CHARGE_OUT);
OutClr(CAP_DISCHARGE_OUT);
}
std::int32_t cap::calcCapacitance_fpd(std::int32_t ticks)
{
ticks -= cap::calData.offsetTicks;
ticks = fp::mul(ticks, fp::to(CAP_TIME_CONSTANT_COEF));
// C = (ticks * 1e12/10000) / (R * ticks_in_second/10000)
std::int32_t cap = fp::muldiv(ticks, std::int32_t(1000000000000ULL / 10000ULL), std::int32_t(CAP_R_SERIES * (48000000U / 10000U)));
cap -= cap::calData.offsetTicks;
return cap;
}
void cap::zeroReading()
{
cap::calData.offsetTicks = cap::calData.measureTicks;
}