Compare commits
14 Commits
00dc0e21b7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8175d1cba0 | |||
| 70b5557be9 | |||
| 4a0740539b | |||
| bee128dec7 | |||
| 94df442395 | |||
| 38a76ead58 | |||
| deb21dc681 | |||
| 61ac134424 | |||
| 4aded6604a | |||
| d36fb23470 | |||
| 9b9b7a79ad | |||
| 4e69bb67e2 | |||
| 71bf90f003 | |||
| f462e65cc2 |
371
main.cpp
371
main.cpp
@ -113,6 +113,9 @@ struct Config {
|
||||
std::string live_json_path = "live_plot.json";
|
||||
std::optional<std::string> tty_path;
|
||||
bool di1_group_average = false;
|
||||
bool do1_toggle_per_frame = false;
|
||||
bool do1_noise_subtract = false;
|
||||
std::optional<uint32_t> noise_avg_steps;
|
||||
};
|
||||
|
||||
[[noreturn]] void fail(const std::string& message) {
|
||||
@ -470,6 +473,7 @@ void print_help(const char* exe_name) {
|
||||
<< " [di1:zero|trace|ignore]\n"
|
||||
<< " [duration_ms:100] [packet_limit:0] [csv:capture.csv] [svg:capture.svg]\n"
|
||||
<< " [live_html:live_plot.html] [live_json:live_plot.json] [tty:/tmp/ttyADC_data] [di1_group_avg]\n"
|
||||
<< " [do1_toggle_per_frame] [do1_noise_subtract] [noise_avg_steps:N]\n"
|
||||
<< " [recv_block:32768] [stats_period_ms:1000] [live_update_period_ms:1000] [svg_history_packets:50] [start_wait_ms:10000]\n"
|
||||
<< " [buffer_words:8388608] [step_words:32768]\n"
|
||||
<< " [pullup_syn1] [pullup_syn2] [pulldown_conv_in] [pulldown_start_in]\n"
|
||||
@ -494,17 +498,26 @@ void print_help(const char* exe_name) {
|
||||
<< " stats_period_ms:1000 -> print online transfer/capture statistics every 1000 ms (0 disables)\n"
|
||||
<< " recv_block:32768 -> request up to 32768 raw 32-bit words per X502_Recv() call\n"
|
||||
<< " live_update_period_ms:1000 -> refresh live HTML/JSON no more than once per second"
|
||||
<< " (ignored in tty+di1_group_avg fast mode)\n"
|
||||
<< " (ignored in tty fast stream-only modes)\n"
|
||||
<< " svg_history_packets:50 -> keep last 50 packets in RAM for final SVG (0 keeps all, risky)"
|
||||
<< " (ignored in tty+di1_group_avg fast mode)\n"
|
||||
<< " (ignored in tty fast stream-only modes)\n"
|
||||
<< " duration_ms:100 -> max packet length if stop edge does not arrive\n"
|
||||
<< " packet_limit:0 -> 0 means continuous until Ctrl+C, N means stop after N packets\n"
|
||||
<< " buffer_words:8388608 -> input stream buffer size in 32-bit words\n"
|
||||
<< " step_words:32768 -> input stream transfer step in 32-bit words\n"
|
||||
<< " live_html/live_json -> live graph files updated as packets arrive outside tty+di1_group_avg fast mode\n"
|
||||
<< " live_html/live_json -> live graph files updated as packets arrive outside tty fast stream-only modes\n"
|
||||
<< " tty:/tmp/ttyADC_data -> write a continuous legacy 4-word CH1/CH2 stream; with channels:1, CH2 is 0\n"
|
||||
<< " di1_group_avg -> with tty + di1:trace, emit one averaged 4-word step per constant DI1 run\n"
|
||||
<< " tty+di1_group_avg -> fast stream-only mode: skip CSV/SVG/live outputs and send only averaged tty data\n"
|
||||
<< " do1_toggle_per_frame -> hardware cyclic DO1 pattern in module memory:\n"
|
||||
<< " DO1 outputs 00110011... continuously (toggle every 2 ADC ticks)\n"
|
||||
<< " without per-tick DOUT updates from PC\n"
|
||||
<< " do1_noise_subtract -> with tty + do1_toggle_per_frame, use DI1 state from synchronous DIN\n"
|
||||
<< " (expected DO1->DI1 loopback) to average recent noise steps and subtract it\n"
|
||||
<< " from useful steps before tty output\n"
|
||||
<< " (only corrected DO1=LOW steps are sent to tty)\n"
|
||||
<< " noise_avg_steps:N -> number of recent DO1=HIGH noise steps per channel used as subtraction baseline\n"
|
||||
<< " (required when do1_noise_subtract is enabled)\n"
|
||||
<< " tty fast stream-only modes -> di1_group_avg or do1_noise_subtract; skip CSV/SVG/live outputs\n"
|
||||
<< " If sample_clock_hz is omitted together with clock:internal, the maximum ADC speed is used\n"
|
||||
<< "\n"
|
||||
<< "Profiles:\n"
|
||||
@ -527,10 +540,12 @@ void print_help(const char* exe_name) {
|
||||
<< "This build enables synchronous DIN together with ADC. DI_SYN2 stop edges are detected\n"
|
||||
<< "inside the same input stream, packets are split continuously by DI_SYN2 edges, and DI1\n"
|
||||
<< "can either zero ADC samples on change, be exported as a separate synchronous trace, or be ignored.\n"
|
||||
<< "Outside tty+di1_group_avg fast mode, open live_plot.html in a browser to see the live graph update over time.\n"
|
||||
<< "Outside tty fast stream-only modes, open live_plot.html in a browser to see the live graph update over time.\n"
|
||||
<< "The live HTML supports X/Y zoom buttons, mouse-wheel zoom, and reset.\n"
|
||||
<< "For tty output, use di1_group_avg together with di1:trace to emit one averaged 4-word step\n"
|
||||
<< "per constant DI1 run while keeping the current ring-buffered binary frame format.\n"
|
||||
<< "For DO1 subtraction with loopback, use do1_toggle_per_frame + do1_noise_subtract + noise_avg_steps:N;\n"
|
||||
<< "DI1 from synchronous DIN is used as the actual DO1 state marker.\n"
|
||||
<< "Amplitude mode keeps the raw AD8317 voltage as captured on X1; no inversion is applied.\n"
|
||||
<< "\n"
|
||||
<< "Phase profile example:\n"
|
||||
@ -547,7 +562,13 @@ void print_help(const char* exe_name) {
|
||||
<< " " << exe_name
|
||||
<< " profile:amplitude clock:internal internal_ref_hz:2000000 start:di_syn2_rise stop:di_syn2_fall"
|
||||
<< " sample_clock_hz:max range:0.2 di1:trace di1_group_avg duration_ms:100 packet_limit:0"
|
||||
<< " tty:/tmp/ttyADC_data\n";
|
||||
<< " tty:/tmp/ttyADC_data\n"
|
||||
<< "\n"
|
||||
<< "Amplitude TTY DO1 noise subtract example:\n"
|
||||
<< " " << exe_name
|
||||
<< " profile:amplitude clock:internal internal_ref_hz:2000000 start:di_syn2_rise stop:di_syn2_fall"
|
||||
<< " sample_clock_hz:max range:0.2 do1_toggle_per_frame do1_noise_subtract noise_avg_steps:16"
|
||||
<< " duration_ms:100 packet_limit:0 tty:/tmp/ttyADC_data\n";
|
||||
}
|
||||
|
||||
Config parse_args(int argc, char** argv) {
|
||||
@ -631,6 +652,18 @@ Config parse_args(int argc, char** argv) {
|
||||
cfg.di1_group_average = true;
|
||||
continue;
|
||||
}
|
||||
if (arg == "do1_noise_subtract") {
|
||||
cfg.do1_noise_subtract = true;
|
||||
continue;
|
||||
}
|
||||
if (arg == "do1_toggle_per_frame") {
|
||||
cfg.do1_toggle_per_frame = true;
|
||||
continue;
|
||||
}
|
||||
if (starts_with(arg, "noise_avg_steps:")) {
|
||||
cfg.noise_avg_steps = parse_u32(arg.substr(16), "noise_avg_steps");
|
||||
continue;
|
||||
}
|
||||
if (starts_with(arg, "sample_clock_hz:")) {
|
||||
const std::string value = trim_copy(arg.substr(16));
|
||||
cfg.sample_clock_specified = true;
|
||||
@ -742,7 +775,7 @@ Config parse_args(int argc, char** argv) {
|
||||
cfg.live_update_period_ms = std::max<uint32_t>(cfg.live_update_period_ms, 1000U);
|
||||
}
|
||||
}
|
||||
if (cfg.tty_path && cfg.di1_group_average) {
|
||||
if (cfg.tty_path && (cfg.di1_group_average || cfg.do1_noise_subtract)) {
|
||||
if (!cfg.recv_block_specified) {
|
||||
cfg.recv_block_words = std::max<uint32_t>(cfg.recv_block_words, 65536U);
|
||||
}
|
||||
@ -765,6 +798,25 @@ Config parse_args(int argc, char** argv) {
|
||||
if (cfg.di1_group_average && (cfg.di1_mode != Di1Mode::Trace)) {
|
||||
fail("di1_group_avg requires di1:trace");
|
||||
}
|
||||
if (cfg.do1_noise_subtract) {
|
||||
if (!cfg.tty_path.has_value()) {
|
||||
fail("do1_noise_subtract requires tty:<path>");
|
||||
}
|
||||
if (!cfg.do1_toggle_per_frame) {
|
||||
fail("do1_noise_subtract requires do1_toggle_per_frame");
|
||||
}
|
||||
if (cfg.di1_group_average) {
|
||||
fail("do1_noise_subtract is incompatible with di1_group_avg");
|
||||
}
|
||||
if (!cfg.noise_avg_steps.has_value()) {
|
||||
fail("do1_noise_subtract requires noise_avg_steps:N");
|
||||
}
|
||||
if (*cfg.noise_avg_steps == 0U) {
|
||||
fail("noise_avg_steps must be > 0");
|
||||
}
|
||||
} else if (cfg.noise_avg_steps.has_value()) {
|
||||
fail("noise_avg_steps:N can only be used together with do1_noise_subtract");
|
||||
}
|
||||
if (sync_uses_di_syn1(cfg.sync_mode) && sync_uses_di_syn1(cfg.sync_start_mode)) {
|
||||
fail("clock and start cannot both use DI_SYN1; use start_in or immediate start");
|
||||
}
|
||||
@ -914,6 +966,8 @@ struct Api {
|
||||
decltype(&X502_SetAdcFreq) SetAdcFreq = nullptr;
|
||||
decltype(&X502_GetAdcFreq) GetAdcFreq = nullptr;
|
||||
decltype(&X502_SetDinFreq) SetDinFreq = nullptr;
|
||||
decltype(&X502_SetOutFreq) SetOutFreq = nullptr;
|
||||
decltype(&X502_AsyncOutDig) AsyncOutDig = nullptr;
|
||||
decltype(&X502_SetRefFreq) SetRefFreq = nullptr;
|
||||
decltype(&X502_SetStreamBufSize) SetStreamBufSize = nullptr;
|
||||
decltype(&X502_SetStreamStep) SetStreamStep = nullptr;
|
||||
@ -924,6 +978,11 @@ struct Api {
|
||||
decltype(&X502_StreamsStart) StreamsStart = nullptr;
|
||||
decltype(&X502_GetRecvReadyCount) GetRecvReadyCount = nullptr;
|
||||
decltype(&X502_Recv) Recv = nullptr;
|
||||
decltype(&X502_OutCycleLoadStart) OutCycleLoadStart = nullptr;
|
||||
decltype(&X502_OutCycleSetup) OutCycleSetup = nullptr;
|
||||
decltype(&X502_OutCycleStop) OutCycleStop = nullptr;
|
||||
decltype(&X502_Send) Send = nullptr;
|
||||
decltype(&X502_PrepareData) PrepareData = nullptr;
|
||||
decltype(&X502_ProcessData) ProcessData = nullptr;
|
||||
|
||||
decltype(&E502_OpenUsb) OpenUsb = nullptr;
|
||||
@ -967,6 +1026,8 @@ struct Api {
|
||||
SetAdcFreq = load_symbol<decltype(SetAdcFreq)>(x502_module, "X502_SetAdcFreq");
|
||||
GetAdcFreq = load_symbol<decltype(GetAdcFreq)>(x502_module, "X502_GetAdcFreq");
|
||||
SetDinFreq = load_symbol<decltype(SetDinFreq)>(x502_module, "X502_SetDinFreq");
|
||||
SetOutFreq = load_symbol<decltype(SetOutFreq)>(x502_module, "X502_SetOutFreq");
|
||||
AsyncOutDig = load_symbol<decltype(AsyncOutDig)>(x502_module, "X502_AsyncOutDig");
|
||||
SetRefFreq = load_symbol<decltype(SetRefFreq)>(x502_module, "X502_SetRefFreq");
|
||||
SetStreamBufSize = load_symbol<decltype(SetStreamBufSize)>(x502_module, "X502_SetStreamBufSize");
|
||||
SetStreamStep = load_symbol<decltype(SetStreamStep)>(x502_module, "X502_SetStreamStep");
|
||||
@ -977,6 +1038,11 @@ struct Api {
|
||||
StreamsStart = load_symbol<decltype(StreamsStart)>(x502_module, "X502_StreamsStart");
|
||||
GetRecvReadyCount = load_symbol<decltype(GetRecvReadyCount)>(x502_module, "X502_GetRecvReadyCount");
|
||||
Recv = load_symbol<decltype(Recv)>(x502_module, "X502_Recv");
|
||||
OutCycleLoadStart = try_load_symbol<decltype(OutCycleLoadStart)>(x502_module, "X502_OutCycleLoadStart");
|
||||
OutCycleSetup = try_load_symbol<decltype(OutCycleSetup)>(x502_module, "X502_OutCycleSetup");
|
||||
OutCycleStop = try_load_symbol<decltype(OutCycleStop)>(x502_module, "X502_OutCycleStop");
|
||||
Send = load_symbol<decltype(Send)>(x502_module, "X502_Send");
|
||||
PrepareData = load_symbol<decltype(PrepareData)>(x502_module, "X502_PrepareData");
|
||||
ProcessData = load_symbol<decltype(ProcessData)>(x502_module, "X502_ProcessData");
|
||||
|
||||
OpenUsb = load_symbol<decltype(OpenUsb)>(e502_module, "E502_OpenUsb");
|
||||
@ -1009,6 +1075,9 @@ constexpr uint32_t kE502DiSyn2Mask =
|
||||
(static_cast<uint32_t>(1U) << 13U) | (static_cast<uint32_t>(1U) << 17U);
|
||||
constexpr uint32_t kE502Digital1Mask = (static_cast<uint32_t>(1U) << 0U);
|
||||
constexpr uint32_t kE502Digital2Mask = (static_cast<uint32_t>(1U) << 1U);
|
||||
constexpr uint32_t kE502Do1Mask = (static_cast<uint32_t>(1U) << 0U);
|
||||
constexpr uint32_t kDo1TogglePeriodTicks = 2U;
|
||||
constexpr uint32_t kDo1CyclePatternWords = kDo1TogglePeriodTicks * 2U;
|
||||
constexpr uint32_t kStreamInputAdcFlag = 0x80000000U;
|
||||
constexpr uint32_t kStreamInputCalibratedAdcFlag = 0x40000000U;
|
||||
|
||||
@ -1162,6 +1231,103 @@ struct TtyGroupAverageState {
|
||||
}
|
||||
};
|
||||
|
||||
struct Do1NoiseSubtractState {
|
||||
uint32_t noise_avg_steps = 0;
|
||||
std::array<std::vector<double>, 2> noise_ring;
|
||||
std::array<std::size_t, 2> noise_ring_head {};
|
||||
std::array<std::size_t, 2> noise_ring_size {};
|
||||
std::array<double, 2> noise_ring_sum {};
|
||||
std::array<double, 2> step_sum {};
|
||||
std::array<uint32_t, 2> step_count {};
|
||||
bool step_level_initialized = false;
|
||||
bool step_di1_high = false;
|
||||
uint16_t next_index = 1;
|
||||
|
||||
void configure(uint32_t history_steps) {
|
||||
noise_avg_steps = history_steps;
|
||||
for (auto& ring : noise_ring) {
|
||||
ring.assign(noise_avg_steps, 0.0);
|
||||
}
|
||||
reset_packet();
|
||||
}
|
||||
|
||||
void clear_step() {
|
||||
step_sum = {};
|
||||
step_count = {};
|
||||
}
|
||||
|
||||
void reset_packet() {
|
||||
clear_step();
|
||||
step_level_initialized = false;
|
||||
step_di1_high = false;
|
||||
next_index = 1;
|
||||
noise_ring_head = {};
|
||||
noise_ring_size = {};
|
||||
noise_ring_sum = {};
|
||||
}
|
||||
|
||||
void start_new_step(bool di1_high) {
|
||||
clear_step();
|
||||
step_di1_high = di1_high;
|
||||
step_level_initialized = true;
|
||||
}
|
||||
|
||||
void add_sample(uint32_t lch, double raw_code) {
|
||||
if (lch >= step_sum.size()) {
|
||||
return;
|
||||
}
|
||||
step_sum[lch] += raw_code;
|
||||
++step_count[lch];
|
||||
}
|
||||
|
||||
bool has_complete_step(uint32_t channel_count) const {
|
||||
return (step_count[0] != 0U) && ((channel_count <= 1U) || (step_count[1] != 0U));
|
||||
}
|
||||
|
||||
double step_average(uint32_t lch) const {
|
||||
if ((lch >= step_sum.size()) || (step_count[lch] == 0U)) {
|
||||
return 0.0;
|
||||
}
|
||||
return step_sum[lch] / static_cast<double>(step_count[lch]);
|
||||
}
|
||||
|
||||
void push_noise_average(uint32_t lch, double avg_value) {
|
||||
if ((noise_avg_steps == 0U) || (lch >= noise_ring.size())) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto& ring = noise_ring[lch];
|
||||
auto& head = noise_ring_head[lch];
|
||||
auto& size = noise_ring_size[lch];
|
||||
auto& sum = noise_ring_sum[lch];
|
||||
|
||||
if (size < noise_avg_steps) {
|
||||
ring[size] = avg_value;
|
||||
sum += avg_value;
|
||||
++size;
|
||||
return;
|
||||
}
|
||||
|
||||
sum -= ring[head];
|
||||
ring[head] = avg_value;
|
||||
sum += avg_value;
|
||||
head = (head + 1U) % noise_avg_steps;
|
||||
}
|
||||
|
||||
double noise_baseline(uint32_t lch) const {
|
||||
if (lch >= noise_ring_size.size()) {
|
||||
return 0.0;
|
||||
}
|
||||
const std::size_t size = noise_ring_size[lch];
|
||||
if (size == 0U) {
|
||||
return 0.0;
|
||||
}
|
||||
return noise_ring_sum[lch] / static_cast<double>(size);
|
||||
}
|
||||
|
||||
void finish_step() { clear_step(); }
|
||||
};
|
||||
|
||||
int16_t pack_raw_code_to_int16(double avg_raw_code) {
|
||||
const double scaled =
|
||||
avg_raw_code * 32767.0 / static_cast<double>(X502_ADC_SCALE_CODE_MAX);
|
||||
@ -1361,9 +1527,21 @@ int run(const Config& cfg) {
|
||||
<< " words/s, while the documented Ethernet input-only limit is about 2500000 words/s.";
|
||||
fail(message.str());
|
||||
}
|
||||
double actual_dout_freq_hz = actual_frame_freq_hz;
|
||||
if (cfg.do1_toggle_per_frame) {
|
||||
if (actual_sample_clock_hz <= 0.0) {
|
||||
fail("do1_toggle_per_frame requires a positive ADC sample rate");
|
||||
}
|
||||
actual_dout_freq_hz = actual_sample_clock_hz;
|
||||
expect_ok(api, api.SetOutFreq(device.hnd, &actual_dout_freq_hz), "Set DOUT frequency");
|
||||
}
|
||||
|
||||
expect_ok(api, api.SetStreamBufSize(device.hnd, X502_STREAM_CH_IN, cfg.input_buffer_words), "Set input buffer size");
|
||||
expect_ok(api, api.SetStreamStep(device.hnd, X502_STREAM_CH_IN, cfg.input_step_words), "Set input stream step");
|
||||
if (cfg.do1_toggle_per_frame) {
|
||||
expect_ok(api, api.SetStreamBufSize(device.hnd, X502_STREAM_CH_OUT, cfg.input_buffer_words), "Set output buffer size");
|
||||
expect_ok(api, api.SetStreamStep(device.hnd, X502_STREAM_CH_OUT, cfg.input_step_words), "Set output stream step");
|
||||
}
|
||||
|
||||
uint32_t pullups = 0;
|
||||
if (cfg.pullup_syn1) {
|
||||
@ -1381,7 +1559,11 @@ int run(const Config& cfg) {
|
||||
expect_ok(api, api.SetDigInPullup(device.hnd, pullups), "Set digital input pullups/pulldowns");
|
||||
|
||||
expect_ok(api, api.Configure(device.hnd, 0), "Configure device");
|
||||
expect_ok(api, api.StreamsEnable(device.hnd, X502_STREAM_ADC | X502_STREAM_DIN), "Enable ADC+DIN stream");
|
||||
uint32_t enabled_streams = X502_STREAM_ADC | X502_STREAM_DIN;
|
||||
if (cfg.do1_toggle_per_frame) {
|
||||
enabled_streams |= X502_STREAM_DOUT;
|
||||
}
|
||||
expect_ok(api, api.StreamsEnable(device.hnd, enabled_streams), "Enable data streams");
|
||||
|
||||
const std::size_t target_frames = std::max<std::size_t>(
|
||||
1, static_cast<std::size_t>(std::llround((cfg.duration_ms / 1000.0) * actual_frame_freq_hz)));
|
||||
@ -1413,6 +1595,11 @@ int run(const Config& cfg) {
|
||||
<< " channel 2: "
|
||||
<< ((cfg.channel_count >= 2U) ? phy_channel_name(cfg.mode, cfg.ch2) : std::string("disabled")) << "\n"
|
||||
<< " DI1 handling: " << di1_mode_to_string(cfg.di1_mode) << "\n"
|
||||
<< " DO1 toggle per frame: "
|
||||
<< (cfg.do1_toggle_per_frame ? std::string("enabled (hardware cyclic 00110011..., toggle every 2 ADC ticks)")
|
||||
: std::string("disabled")) << "\n"
|
||||
<< " DOUT rate: "
|
||||
<< (cfg.do1_toggle_per_frame ? std::to_string(actual_dout_freq_hz) + " Hz" : std::string("disabled")) << "\n"
|
||||
<< " ADC range: +/-" << range_to_volts(cfg.range) << " V\n"
|
||||
<< " max frames per packet per channel: " << target_frames << "\n";
|
||||
|
||||
@ -1420,9 +1607,14 @@ int run(const Config& cfg) {
|
||||
std::size_t tty_ring_capacity_bytes = 0;
|
||||
std::unique_ptr<TtyProtocolWriter> tty_writer;
|
||||
const bool tty_di1_group_average = cfg.tty_path.has_value() && cfg.di1_group_average;
|
||||
const bool fast_tty_avg_stream_mode = tty_di1_group_average;
|
||||
const bool tty_do1_noise_subtract = cfg.tty_path.has_value() && cfg.do1_noise_subtract;
|
||||
const bool fast_tty_avg_stream_mode = tty_di1_group_average || tty_do1_noise_subtract;
|
||||
const uint16_t tty_packet_start_marker =
|
||||
(cfg.profile == CaptureProfile::Amplitude) ? 0x001AU : 0x000AU;
|
||||
const std::string fast_tty_mode_name =
|
||||
tty_do1_noise_subtract ? std::string("do1_noise_subtract")
|
||||
: (tty_di1_group_average ? std::string("di1_group_avg")
|
||||
: std::string("none"));
|
||||
if (cfg.tty_path) {
|
||||
const uint64_t typical_tty_batch_frames = (static_cast<uint64_t>(read_capacity_words) + 1U) / 2U;
|
||||
const uint64_t typical_tty_batch_bytes = typical_tty_batch_frames * sizeof(uint16_t) * 4U;
|
||||
@ -1433,7 +1625,7 @@ int run(const Config& cfg) {
|
||||
}
|
||||
tty_ring_capacity_bytes = static_cast<std::size_t>(tty_ring_bytes_u64);
|
||||
tty_writer = std::make_unique<TtyProtocolWriter>(*cfg.tty_path, tty_ring_capacity_bytes);
|
||||
if (!tty_di1_group_average) {
|
||||
if (!tty_di1_group_average && !tty_do1_noise_subtract) {
|
||||
tty_writer->emit_packet_start(tty_packet_start_marker);
|
||||
}
|
||||
}
|
||||
@ -1444,13 +1636,18 @@ int run(const Config& cfg) {
|
||||
writer->initialize_csv(cfg.channel_count, cfg.di1_mode == Di1Mode::Trace);
|
||||
}
|
||||
if (fast_tty_avg_stream_mode) {
|
||||
std::cout << " tty avg stream-only mode: enabled\n"
|
||||
std::cout << " tty fast stream-only mode: enabled (" << fast_tty_mode_name << ")\n"
|
||||
<< " tty stream output: " << *cfg.tty_path << "\n"
|
||||
<< " tty ring buffer bytes: " << tty_ring_capacity_bytes << "\n"
|
||||
<< " recv block words: " << cfg.recv_block_words << "\n"
|
||||
<< " input step words: " << cfg.input_step_words << "\n"
|
||||
<< " input buffer words: " << cfg.input_buffer_words << "\n"
|
||||
<< " tty di1_group_avg: enabled\n"
|
||||
<< " tty di1_group_avg: " << (tty_di1_group_average ? "enabled" : "disabled") << "\n"
|
||||
<< " tty do1_noise_subtract: " << (tty_do1_noise_subtract ? "enabled" : "disabled") << "\n"
|
||||
<< " do1_noise_subtract marker: "
|
||||
<< (tty_do1_noise_subtract ? "DI1 from DIN (DO1->DI1 loopback expected)" : "n/a") << "\n"
|
||||
<< " noise_avg_steps: "
|
||||
<< (tty_do1_noise_subtract ? std::to_string(*cfg.noise_avg_steps) : std::string("n/a")) << "\n"
|
||||
<< " stream-only note: ignoring csv/svg/live_html/live_json/live_update_period_ms/svg_history_packets\n";
|
||||
} else {
|
||||
std::cout << " live plot html: " << writer->live_html_path() << "\n"
|
||||
@ -1466,6 +1663,11 @@ int run(const Config& cfg) {
|
||||
: (cfg.di1_group_average ? std::string("requested, tty disabled")
|
||||
: std::string("disabled")))
|
||||
<< "\n"
|
||||
<< " tty do1_noise_subtract: "
|
||||
<< (tty_do1_noise_subtract ? std::string("enabled")
|
||||
: (cfg.do1_noise_subtract ? std::string("requested, tty disabled")
|
||||
: std::string("disabled")))
|
||||
<< "\n"
|
||||
<< " SVG history packets kept in RAM: "
|
||||
<< ((cfg.svg_history_packets == 0U) ? std::string("all (unbounded)") : std::to_string(cfg.svg_history_packets))
|
||||
<< "\n";
|
||||
@ -1476,6 +1678,48 @@ int run(const Config& cfg) {
|
||||
std::cerr << "Warning: Ctrl+C handler could not be installed; continuous mode may stop abruptly.\n";
|
||||
}
|
||||
|
||||
std::array<uint32_t, kDo1CyclePatternWords> do1_cycle_pattern {};
|
||||
std::array<uint32_t, kDo1CyclePatternWords> do1_cycle_encoded {};
|
||||
if (cfg.do1_toggle_per_frame) {
|
||||
if ((api.OutCycleLoadStart == nullptr) || (api.OutCycleSetup == nullptr) || (api.OutCycleStop == nullptr)) {
|
||||
fail("do1_toggle_per_frame requires X502_OutCycle* API support (cyclic output mode)");
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < do1_cycle_pattern.size(); ++i) {
|
||||
do1_cycle_pattern[i] = (i < kDo1TogglePeriodTicks) ? 0U : kE502Do1Mask;
|
||||
}
|
||||
|
||||
expect_ok(api,
|
||||
api.OutCycleLoadStart(device.hnd, static_cast<uint32_t>(do1_cycle_pattern.size())),
|
||||
"Allocate cyclic DOUT buffer");
|
||||
expect_ok(api,
|
||||
api.PrepareData(device.hnd,
|
||||
nullptr,
|
||||
nullptr,
|
||||
do1_cycle_pattern.data(),
|
||||
static_cast<uint32_t>(do1_cycle_pattern.size()),
|
||||
0,
|
||||
do1_cycle_encoded.data()),
|
||||
"Prepare cyclic DOUT pattern");
|
||||
|
||||
std::size_t sent_cycle_words = 0;
|
||||
while (sent_cycle_words < do1_cycle_encoded.size()) {
|
||||
const int32_t sent = api.Send(device.hnd,
|
||||
do1_cycle_encoded.data() + sent_cycle_words,
|
||||
static_cast<uint32_t>(do1_cycle_encoded.size() - sent_cycle_words),
|
||||
cfg.recv_timeout_ms);
|
||||
if (sent < 0) {
|
||||
fail("Send cyclic DOUT pattern: " + x502_error(api, sent));
|
||||
}
|
||||
if (sent == 0) {
|
||||
fail("Send cyclic DOUT pattern timed out before all words were queued");
|
||||
}
|
||||
sent_cycle_words += static_cast<std::size_t>(sent);
|
||||
}
|
||||
|
||||
expect_ok(api, api.OutCycleSetup(device.hnd, X502_OUT_CYCLE_FLAGS_WAIT_DONE), "Activate cyclic DOUT pattern");
|
||||
}
|
||||
|
||||
expect_ok(api, api.StreamsStart(device.hnd), "Start streams");
|
||||
device.streams_started = true;
|
||||
|
||||
@ -1490,8 +1734,12 @@ int run(const Config& cfg) {
|
||||
PacketAccumulator current_packet;
|
||||
TtyContinuousState tty_state;
|
||||
TtyGroupAverageState tty_group_state;
|
||||
Do1NoiseSubtractState tty_do1_noise_state;
|
||||
std::vector<uint16_t> tty_frame_words;
|
||||
tty_frame_words.reserve(static_cast<std::size_t>(read_capacity_words) * 2U + 16U);
|
||||
if (tty_do1_noise_subtract) {
|
||||
tty_do1_noise_state.configure(*cfg.noise_avg_steps);
|
||||
}
|
||||
if (!fast_tty_avg_stream_mode) {
|
||||
current_packet.reset(target_frames, cfg.channel_count);
|
||||
}
|
||||
@ -1558,12 +1806,14 @@ int run(const Config& cfg) {
|
||||
<< ", DIN samples/s=" << din_samples_per_s
|
||||
<< ", frames/s per channel=" << frames_per_ch_per_s
|
||||
<< ", packets/s=" << packets_per_s;
|
||||
if (!fast_tty_avg_stream_mode) {
|
||||
if (cfg.di1_mode == Di1Mode::ZeroOnChange) {
|
||||
std::cout << ", zeroed on DI1 change=" << zeroed_fraction << "% ("
|
||||
<< stats_zeroed_samples << "/" << stats_stored_adc_samples << ")";
|
||||
} else if (cfg.di1_mode == Di1Mode::Trace) {
|
||||
std::cout << ", DI1 trace=enabled";
|
||||
}
|
||||
}
|
||||
if (tty_writer) {
|
||||
std::cout << ", tty_frames_written=" << tty_frames_written
|
||||
<< ", tty_frames_dropped=" << tty_frames_dropped
|
||||
@ -1617,6 +1867,49 @@ int run(const Config& cfg) {
|
||||
++packet_avg_steps;
|
||||
};
|
||||
|
||||
auto append_tty_do1_subtracted_step = [&]() {
|
||||
if (!tty_do1_noise_subtract) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!tty_do1_noise_state.step_level_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!tty_do1_noise_state.has_complete_step(cfg.channel_count)) {
|
||||
tty_do1_noise_state.finish_step();
|
||||
return;
|
||||
}
|
||||
|
||||
const double ch1_avg = tty_do1_noise_state.step_average(0U);
|
||||
const double ch2_avg = (cfg.channel_count <= 1U) ? 0.0 : tty_do1_noise_state.step_average(1U);
|
||||
|
||||
if (tty_do1_noise_state.step_di1_high) {
|
||||
tty_do1_noise_state.push_noise_average(0U, ch1_avg);
|
||||
if (cfg.channel_count > 1U) {
|
||||
tty_do1_noise_state.push_noise_average(1U, ch2_avg);
|
||||
}
|
||||
tty_do1_noise_state.finish_step();
|
||||
return;
|
||||
}
|
||||
|
||||
if (tty_do1_noise_state.next_index >= 0xFFFFU) {
|
||||
fail("TTY protocol step index overflow within packet");
|
||||
}
|
||||
|
||||
const double ch1_corrected = ch1_avg - tty_do1_noise_state.noise_baseline(0U);
|
||||
const double ch2_corrected =
|
||||
(cfg.channel_count <= 1U) ? 0.0 : (ch2_avg - tty_do1_noise_state.noise_baseline(1U));
|
||||
|
||||
append_tty_frame((cfg.profile == CaptureProfile::Amplitude) ? 0x001AU : 0x000AU,
|
||||
tty_do1_noise_state.next_index,
|
||||
static_cast<uint16_t>(pack_raw_code_to_int16(ch1_corrected)),
|
||||
static_cast<uint16_t>(pack_raw_code_to_int16(ch2_corrected)));
|
||||
++tty_do1_noise_state.next_index;
|
||||
++packet_avg_steps;
|
||||
tty_do1_noise_state.finish_step();
|
||||
};
|
||||
|
||||
auto flush_tty_frames = [&]() {
|
||||
if (!tty_writer || tty_frame_words.empty()) {
|
||||
return;
|
||||
@ -1647,6 +1940,9 @@ int run(const Config& cfg) {
|
||||
if (tty_di1_group_average) {
|
||||
tty_group_state.reset_packet();
|
||||
append_tty_packet_start();
|
||||
} else if (tty_do1_noise_subtract) {
|
||||
tty_do1_noise_state.reset_packet();
|
||||
append_tty_packet_start();
|
||||
}
|
||||
packet_active = true;
|
||||
};
|
||||
@ -1663,6 +1959,10 @@ int run(const Config& cfg) {
|
||||
if (tty_di1_group_average) {
|
||||
append_tty_group_step();
|
||||
tty_group_state.clear_step();
|
||||
} else if (tty_do1_noise_subtract) {
|
||||
// Finalize the last DI1 run in packet, drop only incomplete channel tuples.
|
||||
append_tty_do1_subtracted_step();
|
||||
tty_do1_noise_state.clear_step();
|
||||
}
|
||||
|
||||
const std::size_t frames = fast_tty_avg_stream_mode
|
||||
@ -1781,6 +2081,8 @@ int run(const Config& cfg) {
|
||||
}
|
||||
if (tty_di1_group_average) {
|
||||
tty_group_state.clear_step();
|
||||
} else if (tty_do1_noise_subtract) {
|
||||
tty_do1_noise_state.clear_step();
|
||||
}
|
||||
};
|
||||
|
||||
@ -2005,7 +2307,6 @@ int run(const Config& cfg) {
|
||||
}
|
||||
trigger_prev_level = di_syn2_level;
|
||||
}
|
||||
|
||||
if (!packet_active && (cfg.sync_start_mode == X502_SYNC_INTERNAL)) {
|
||||
start_packet();
|
||||
}
|
||||
@ -2043,6 +2344,13 @@ int run(const Config& cfg) {
|
||||
if (tty_di1_group_average && di1_changed) {
|
||||
append_tty_group_step();
|
||||
tty_group_state.clear_step();
|
||||
} else if (tty_do1_noise_subtract) {
|
||||
if (!tty_do1_noise_state.step_level_initialized) {
|
||||
tty_do1_noise_state.start_new_step(di1_level);
|
||||
} else if (di1_changed) {
|
||||
append_tty_do1_subtracted_step();
|
||||
tty_do1_noise_state.start_new_step(di1_level);
|
||||
}
|
||||
}
|
||||
|
||||
if (!fast_tty_avg_stream_mode &&
|
||||
@ -2054,7 +2362,11 @@ int run(const Config& cfg) {
|
||||
|
||||
if (fast_tty_avg_stream_mode) {
|
||||
if (fast_packet_frames < target_frames) {
|
||||
if (tty_do1_noise_subtract) {
|
||||
tty_do1_noise_state.add_sample(lch, adc_raw_value);
|
||||
} else {
|
||||
tty_group_state.add_sample(lch, adc_raw_value);
|
||||
}
|
||||
if (lch == (cfg.channel_count - 1U)) {
|
||||
++fast_packet_frames;
|
||||
++total_completed_frames;
|
||||
@ -2114,8 +2426,15 @@ int run(const Config& cfg) {
|
||||
}
|
||||
}
|
||||
|
||||
if (cfg.do1_toggle_per_frame) {
|
||||
expect_ok(api, api.OutCycleStop(device.hnd, X502_OUT_CYCLE_FLAGS_WAIT_DONE), "Stop cyclic DOUT pattern");
|
||||
}
|
||||
|
||||
expect_ok(api, api.StreamsStop(device.hnd), "Stop streams");
|
||||
device.streams_started = false;
|
||||
if (cfg.do1_toggle_per_frame) {
|
||||
expect_ok(api, api.AsyncOutDig(device.hnd, 0U, ~kE502Do1Mask), "Force DO1 LOW after cyclic DOUT stop");
|
||||
}
|
||||
|
||||
if ((cfg.stats_period_ms != 0U) && ((stats_raw_words != 0U) || (stats_adc_samples != 0U) ||
|
||||
(stats_din_samples != 0U) || (stats_stored_adc_samples != 0U) || (stats_zeroed_samples != 0U) ||
|
||||
@ -2131,26 +2450,15 @@ int run(const Config& cfg) {
|
||||
}
|
||||
|
||||
if (!fast_tty_avg_stream_mode) {
|
||||
const std::vector<CapturePacket> svg_packets(packets.begin(), packets.end());
|
||||
writer->write(svg_packets, actual_frame_freq_hz, range_to_volts(cfg.range));
|
||||
|
||||
std::size_t total_packet_frames = 0;
|
||||
for (const auto& packet : svg_packets) {
|
||||
total_packet_frames += (packet.channel_count <= 1U)
|
||||
? packet.ch1.size()
|
||||
: std::min(packet.ch1.size(), packet.ch2.size());
|
||||
}
|
||||
|
||||
std::cout << "Captured " << total_completed_packets << " packet(s), "
|
||||
<< total_packet_frames << " total frames per channel kept for final SVG\n";
|
||||
std::cout << "Captured " << total_completed_packets << " packet(s) in capture mode\n";
|
||||
if (cfg.di1_mode == Di1Mode::ZeroOnChange) {
|
||||
std::cout << "ADC samples forced to 0 on DI1 change: " << total_zeroed_samples << "\n";
|
||||
} else if (cfg.di1_mode == Di1Mode::Trace) {
|
||||
std::cout << "DI1 synchronous trace exported to CSV/live plot/SVG\n";
|
||||
std::cout << "DI1 synchronous trace exported to live plot\n";
|
||||
}
|
||||
} else {
|
||||
std::cout << "Captured " << total_completed_packets
|
||||
<< " packet(s) in tty avg stream-only mode\n";
|
||||
<< " packet(s) in tty fast stream-only mode (" << fast_tty_mode_name << ")\n";
|
||||
}
|
||||
std::cout << "Average stats: "
|
||||
<< "MB/s=" << std::fixed << std::setprecision(3)
|
||||
@ -2170,6 +2478,7 @@ int run(const Config& cfg) {
|
||||
<< (static_cast<double>(total_completed_packets) /
|
||||
std::max(1e-9, static_cast<double>(tick_count_ms() - capture_loop_start) / 1000.0))
|
||||
<< ", packets captured=" << total_completed_packets;
|
||||
if (!fast_tty_avg_stream_mode) {
|
||||
if (cfg.di1_mode == Di1Mode::ZeroOnChange) {
|
||||
std::cout << ", zeroed on DI1 change="
|
||||
<< ((total_stored_adc_samples == 0U)
|
||||
@ -2179,20 +2488,16 @@ int run(const Config& cfg) {
|
||||
} else if (cfg.di1_mode == Di1Mode::Trace) {
|
||||
std::cout << ", DI1 trace=enabled";
|
||||
}
|
||||
}
|
||||
if (tty_writer) {
|
||||
std::cout << ", tty_frames_written=" << tty_final_stats.frames_written
|
||||
<< ", tty_frames_dropped=" << tty_final_stats.frames_dropped
|
||||
<< ", tty_ring_overflows=" << tty_final_stats.ring_overflows;
|
||||
}
|
||||
if (fast_tty_avg_stream_mode) {
|
||||
std::cout << ", tty avg stream-only mode=enabled";
|
||||
std::cout << ", tty fast stream-only mode=" << fast_tty_mode_name;
|
||||
}
|
||||
std::cout << "\n";
|
||||
if (!fast_tty_avg_stream_mode) {
|
||||
std::cout << "Final SVG packets retained in memory: " << packets.size() << "\n"
|
||||
<< "CSV: " << cfg.csv_path << "\n"
|
||||
<< "SVG: " << cfg.svg_path << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -28,6 +28,7 @@ exec "$BIN" \
|
||||
di1_group_avg \
|
||||
duration_ms:100 \
|
||||
packet_limit:0 \
|
||||
do1_toggle_per_frame \
|
||||
profile:amplitude \
|
||||
"tty:${TTY_PATH}" \
|
||||
"$@"
|
||||
|
||||
45
run_do1_noise_subtract.sh
Executable file
45
run_do1_noise_subtract.sh
Executable file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
BIN="${BIN:-./main.exe}"
|
||||
TTY_PATH="${TTY_PATH:-/tmp/ttyADC_data}"
|
||||
LIB_DIR="${LIB_DIR:-$HOME/.local/lib}"
|
||||
NOISE_AVG_STEPS="${NOISE_AVG_STEPS:-}"
|
||||
|
||||
if [[ ! -x "$BIN" ]]; then
|
||||
echo "Binary '$BIN' not found or not executable. Run ./build_main.sh first." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -z "$NOISE_AVG_STEPS" ]]; then
|
||||
echo "Set NOISE_AVG_STEPS to a positive integer, for example: NOISE_AVG_STEPS=16" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! [[ "$NOISE_AVG_STEPS" =~ ^[0-9]+$ ]] || [[ "$NOISE_AVG_STEPS" -eq 0 ]]; then
|
||||
echo "NOISE_AVG_STEPS must be a positive integer, got '$NOISE_AVG_STEPS'" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -d "$LIB_DIR" ]]; then
|
||||
export LD_LIBRARY_PATH="${LIB_DIR}${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
|
||||
fi
|
||||
|
||||
exec "$BIN" \
|
||||
clock:internal \
|
||||
internal_ref_hz:2000000 \
|
||||
start:di_syn2_rise \
|
||||
stop:di_syn2_fall \
|
||||
sample_clock_hz:max \
|
||||
range:5 \
|
||||
duration_ms:100 \
|
||||
packet_limit:0 \
|
||||
do1_toggle_per_frame \
|
||||
do1_noise_subtract \
|
||||
"noise_avg_steps:${NOISE_AVG_STEPS}" \
|
||||
profile:phase \
|
||||
"tty:${TTY_PATH}" \
|
||||
"$@"
|
||||
Reference in New Issue
Block a user