-
Notifications
You must be signed in to change notification settings - Fork 87
/
gain_cal.cpp
75 lines (64 loc) · 2.13 KB
/
gain_cal.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
#include "vna_measurement.hpp"
#include "ui.hpp"
#include "fifo.hpp"
#include "main.hpp"
#include "ili9341.hpp"
#include "globals.hpp"
#include <board.hpp>
#include <mculib/printf.hpp>
#include <mculib/message_log.hpp>
#include <mculib/printk.hpp>
#include <libopencm3/cm3/scb.h>
using namespace mculib;
using namespace board;
template<int fifoSize>
static void discardPoints(FIFO<complexf, fifoSize>& dpFIFO, int n) {
dpFIFO.clear();
// skip n data points
for(int i=0; i<n; i++) {
while(!dpFIFO.readable());
dpFIFO.dequeue();
}
}
// measure the attenuation at each gain setting
void performGainCal(VNAMeasurement& vnaMeasurement, float* gainTable, int maxGain) {
int j;
volatile int currGain = 0;
auto old_emitDataPoint = vnaMeasurement.emitDataPoint;
auto old_phaseChanged = vnaMeasurement.phaseChanged;
auto old_avg = current_props._avg;
auto old_pow = current_props._adf4350_txPower;
FIFO<complexf, 32> dpFIFO;
current_props._avg = 40; // Use 40 x avg for bbgain cal
current_props._adf4350_txPower = 0; // Use 0 power for prevent bbgain0 overflow
// override phaseChanged, set bbgain to desired value
vnaMeasurement.phaseChanged = [&](VNAMeasurementPhases ph) {
rfsw(RFSW_REFL, RFSW_REFL_ON);
rfsw(RFSW_RECV, RFSW_RECV_REFL);
rfsw(RFSW_ECAL, RFSW_ECAL_OPEN);
rfsw(RFSW_BBGAIN, RFSW_BBGAIN_GAIN(currGain));
};
// disable ecal during gain cal
vnaMeasurement.ecalIntervalPoints = 10000;
vnaMeasurement.setSweep(DEFAULT_FREQ, 0, 1, 1);
vnaMeasurement.emitDataPoint = [&](int freqIndex, freqHz_t freqHz, const VNAObservation& v, const complexf* ecal) {
dpFIFO.enqueue(v[1]);
};
for(j = 0; j <= maxGain; j++) {
currGain = j;
discardPoints(dpFIFO, 1);
while(!dpFIFO.readable());
gainTable[j] = abs(dpFIFO.read()); // Measure magnitude
}
// normalize first entry to 1.0
float norm = gainTable[0];
gainTable[0] = 1.f; // norm / gainTable[0];
for(j = 1; j <= maxGain; j++) {
gainTable[j] = norm / gainTable[j];
}
current_props._avg = old_avg;
current_props._adf4350_txPower = old_pow;
// reset callbacks
vnaMeasurement.emitDataPoint = old_emitDataPoint;
vnaMeasurement.phaseChanged = old_phaseChanged;
}