-
Notifications
You must be signed in to change notification settings - Fork 87
/
common.hpp
308 lines (244 loc) · 7.02 KB
/
common.hpp
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
#pragma once
#include <stdint.h>
#include <complex>
#include <array>
using namespace std;
// common data structures, constants, and convenience functions.
// DO NOT ADD NON-CONSTANT VARIABLES OR STATEFUL FUNCTIONS HERE.
typedef complex<float> complexf;
// port 1 in, port 1 out, port 2 in
typedef array<complexf, 3> VNAObservation;
// a complete observation set for one frequency point
// (measured waves for excitations at each port).
// currently a single VNAObservation, but will be
// an array of 2 observations for a full two port VNA.
typedef VNAObservation VNAObservationSet;
// S11, S21
typedef array<complexf, 2> VNARawValue;
typedef int64_t freqHz_t;
// constants and data types used by all modules.
// DO NOT DECLARE EXTERNAL FUNCTIONS HERE!!!
#define VERSION "git"
#define FIRMWARE_MAJOR_VERSION 1
#define FIRMWARE_MINOR_VERSION 3
#define CH_KERNEL_VERSION "None"
#define PORT_COMPILER_NAME "gcc"
#define PORT_ARCHITECTURE_NAME "arm"
#define PORT_INFO "GD32F303"
#define PORT_CORE_VARIANT_NAME "N/A"
#define PLATFORM_NAME "BARE METAL"
// Enable L/C math code
#define __USE_LC_MATCHING__
#define FREQUENCY_MIN 10000
#define FREQUENCY_MAX 4400000000
// Frequencies below this are generated by the Si5351, above by the ADF4350
// The ADF4350 lower limit is 137Mhz, so it must be above 137Mhz
static constexpr uint32_t FREQUENCY_CHANGE_OVER = 140000000;
#define SWEEP_POINTS_MIN 2
#ifndef SWEEP_POINTS_MAX
#define SWEEP_POINTS_MAX 201
#endif
#define TRACES_MAX 4
#define MARKERS_MAX 4
// Set FFT size depend from max points count
#if SWEEP_POINTS_MAX < 256
#define FFT_SIZE 256
#elif SWEEP_POINTS_MAX < 512
#define FFT_SIZE 512
#else
#error "Need update FFT table for more points size"
#endif
#define ECAL_PARTIAL
#ifdef ECAL_PARTIAL
#define ECAL_CHANNELS 1
#else
#define ECAL_CHANNELS 3
#endif
#define CAL_ENTRIES 7
#define CAL_LOAD 0
#define CAL_OPEN 1
#define CAL_SHORT 2
#define CAL_THRU 3
#define CAL_ISOLN_OPEN 4
#define CAL_ISOLN_SHORT 5
#define CAL_THRU_REFL 6
#define CALSTAT_LOAD (1<<0)
#define CALSTAT_OPEN (1<<1)
#define CALSTAT_SHORT (1<<2)
#define CALSTAT_THRU (1<<3)
#define CALSTAT_ISOLN (1<<4)
#define CALSTAT_ES (1<<5)
#define CALSTAT_ER (1<<6)
#define CALSTAT_ET (1<<7)
#define CALSTAT_ED CALSTAT_LOAD
#define CALSTAT_EX CALSTAT_ISOLN
#define CALSTAT_APPLY (1<<8)
#define CALSTAT_INTERPOLATED (1<<9)
#define CALSTAT_ENHANCED_RESPONSE (1<<10)
#define ETERM_ED 0 /* error term directivity */
#define ETERM_ES 1 /* error term source match */
#define ETERM_ER 2 /* error term refrection tracking */
#define ETERM_ET 3 /* error term transmission tracking */
#define ETERM_EX 4 /* error term isolation */
#define DOMAIN_MODE (1<<0)
#define DOMAIN_FREQ (0<<0)
#define DOMAIN_TIME (1<<0)
#define TD_FUNC (0b11<<1)
#define TD_FUNC_BANDPASS (0b00<<1)
#define TD_FUNC_LOWPASS_IMPULSE (0b01<<1)
#define TD_FUNC_LOWPASS_STEP (0b10<<1)
#define TD_WINDOW (0b11<<3)
#define TD_WINDOW_NORMAL (0b00<<3)
#define TD_WINDOW_MINIMUM (0b01<<3)
#define TD_WINDOW_MAXIMUM (0b10<<3)
// L/C match enable option
#define TD_LC_MATH (1<<5)
#define REDRAW_CELLS (1<<0)
#define REDRAW_FREQUENCY (1<<1)
#define REDRAW_CAL_STATUS (1<<2)
#define REDRAW_MARKER (1<<3)
#define REDRAW_AREA (1<<4)
/* Determines the measurements the ADC will do. */
enum MeasurementMode {
MEASURE_MODE_FULL, //Including ECAL, slowest
MEASURE_MODE_REFL_THRU, //Does not switch the output, use for CW mode
MEASURE_MODE_REFL_THRU_REFRENCE, //No ecal
};
constexpr uint32_t BOOTLOADER_BOOTLOAD_MAGIC = 0xdeadbabe;
static volatile uint32_t& bootloaderBootloadIndicator = *(uint32_t*)(0x20000000 + 48*1024 - 4);
// TODO: name all enums and refer to them by name
enum {
TRC_LOGMAG, TRC_PHASE, TRC_DELAY, TRC_SMITH, TRC_POLAR, TRC_LINEAR, TRC_SWR, TRC_REAL, TRC_IMAG, TRC_R, TRC_X, TRC_Q, TRC_OFF
};
enum SweepParameter {
ST_START, ST_STOP, ST_CENTER, ST_SPAN, ST_CW
};
// lever_mode
enum {
LM_MARKER, LM_SEARCH, LM_CENTER, LM_SPAN
};
// marker smith value format
enum {
MS_LIN, MS_LOG, MS_REIM, MS_RX, MS_RLC
};
enum MarkerSearchModes: uint8_t {
Min,
Max
};
#define MK_SEARCH_LEFT -1
#define MK_SEARCH_RIGHT 1
enum {
UI_OPTIONS_FLIP = 1
};
typedef struct {
uint8_t enabled;
uint8_t type;
uint8_t channel;
uint8_t polar;
float scale;
float refpos;
} trace_t;
typedef struct {
int8_t enabled;
int16_t index;
freqHz_t frequency;
} marker_t;
struct alignas(4) properties_t {
uint32_t magic;
freqHz_t _frequency0; // start
freqHz_t _frequency1; // stop
int16_t _sweep_points;
uint16_t _cal_status;
complexf _cal_data[CAL_ENTRIES][SWEEP_POINTS_MAX];
float _electrical_delay; // picoseconds
trace_t _trace[TRACES_MAX];
marker_t _markers[MARKERS_MAX];
float _velocity_factor; // %
int _active_marker;
uint8_t _domain_mode; /* 0bxxxxxffm : where ff: TD_FUNC m: DOMAIN_MODE */
uint8_t _marker_smith_format;
uint8_t _avg;
uint8_t _adf4350_txPower; // 0 to 3
uint8_t _si5351_txPower; // 0 to 3
uint8_t _measurement_mode; //See enum MeasurementMode.
uint32_t checksum;
// overwrite all fields of this instance with factory default values
void setFieldsToDefault();
// clear calibration data
void setCalDataToDefault();
properties_t() { setFieldsToDefault(); }
freqHz_t startFreqHz() const {
if(_frequency1 > 0) return _frequency0;
return _frequency0 + _frequency1/2;
}
freqHz_t stopFreqHz() const {
if(_frequency1 > 0) return _frequency1;
return _frequency0 - _frequency1/2;
}
freqHz_t stepFreqHz() const {
if(_sweep_points > 0)
return (stopFreqHz() - startFreqHz()) / (_sweep_points - 1);
return 0;
}
void do_cal_reset(int calType, complexf val);
};
typedef struct {
uint32_t magic;
uint16_t dac_value; // unused
uint16_t grid_color;
uint16_t menu_normal_color;
uint16_t menu_active_color;
uint16_t trace_color[TRACES_MAX];
int16_t touch_cal[4];
int8_t default_loadcal;
uint32_t harmonic_freq_threshold; // unused
int8_t ui_options;
uint32_t checksum;
} config_t;
struct uistat_t {
int8_t digit; /* 0~5 */
int8_t digit_mode;
int8_t current_trace; /* 0..3 */
int64_t value; // for editing at numeric input area
uint8_t lever_mode;
int8_t previous_marker;
MarkerSearchModes marker_search_mode;
bool marker_tracking;
bool marker_delta;
};
#define CONFIG_MAGIC 0x8008123c
static inline bool is_freq_for_adf4350(freqHz_t freq)
{
return freq > FREQUENCY_CHANGE_OVER;
}
// convert vbat [mV] to battery indicator
static inline uint8_t vbat2bati(int16_t vbat)
{
if (vbat < 3200) return 0;
if (vbat < 3450) return 25;
if (vbat < 3700) return 50;
if (vbat < 4100) return 75;
return 100;
}
static const struct {
const char *name;
uint16_t refpos;
float scale_unit;
} trace_info[] = {
{ "LOGMAG", 7, 10 },
{ "PHASE", 4, 90 },
{ "DELAY", 4, 1 },
{ "SMITH", 0, 1 },
{ "POLAR", 0, 1 },
{ "LINEAR", 0, 0.125 },
{ "SWR", 0, 1 },
{ "REAL", 4, 0.25 },
{ "IMAG", 4, 0.25 },
{ "R", 0, 100 },
{ "X", 4, 100 },
{ "Q", 0, 10.0 }
};
static const char * const trc_channel_name[] = {
"CH0", "CH1"
};
float my_atof(const char *p);