by John Lauer
Two-board Molecule interferometer with onboard RP2040, USB-C data, 15 V USB-C PD power and a JST stage interface.
main
John Lauer
software/: Pico firmware (500 ksps two-channel ADC over DMA, SYNC on GPIO15, framed delta-packed USB stream), smi-dash…
e09b832
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
// SPDX-License-Identifier: MIT
// Host-side round-trip test of the block packer and frame CRC, and a writer of
// a reference stream (test/vectors.bin) that the dashboard's Rust decoder
// checks against in its own unit tests.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "smi_frame.h"
static unsigned rng = 12345;
static unsigned rnd(void) { rng = rng * 1103515245u + 12345u; return (rng >> 16) & 0x7fff; }
int main(int argc, char **argv) {
const char *out = argc > 1 ? argv[1] : "vectors.bin";
int fails = 0;
uint16_t s[2 * SMI_BLOCK], back[SMI_BLOCK];
uint8_t buf[SMI_MAX_BLOCK_BYTES];
size_t bytes_total = 0, samples_total = 0;
for (int t = 0; t < 20000; t++) {
int kind = t % 4;
for (int i = 0; i < 2 * SMI_BLOCK; i++) {
double x = 2048 + 1500 * sin((t * 128 + i) * 0.01 * (1 + kind));
if (kind == 0) s[i] = (uint16_t)(x + rnd() % 8); // slow signal plus noise
else if (kind == 1) s[i] = (uint16_t)(rnd() & 0x0FFF); // white noise: forces raw
else if (kind == 2) s[i] = 1000; // constant: width 0
else s[i] = (uint16_t)((i * 37 + t) & 0x0FFF); // sawtooth with wraps
}
for (int ch = 0; ch < 2; ch++) {
size_t n = smi_pack_block(s + ch, 2, buf);
size_t m = smi_unpack_block(buf, n, back);
bytes_total += n; samples_total += SMI_BLOCK;
if (m != n) { fails++; continue; }
for (int i = 0; i < SMI_BLOCK; i++) if (back[i] != s[ch + 2 * i]) { fails++; break; }
}
}
// CRC-16/CCITT-FALSE check value for "123456789" is 0x29B1.
uint16_t c = smi_crc16((const uint8_t *)"123456789", 9, 0xFFFF);
if (c != 0x29B1) { printf("ERROR: crc16 check value %04X, expected 29B1\n", c); fails++; }
// Reference stream: 4 DATA frames of 1024 pairs with a known ramp signal.
FILE *f = fopen(out, "wb");
if (!f) { printf("ERROR: cannot write %s\n", out); return 1; }
static uint8_t frame[8 + 13 + 32 * SMI_MAX_BLOCK_BYTES + 2];
for (int fr = 0; fr < 4; fr++) {
static uint16_t pairs[2048];
for (int i = 0; i < 1024; i++) {
long n = fr * 1024L + i;
pairs[2 * i] = (uint16_t)(2048 + 1200 * sin(n * 0.05));
pairs[2 * i + 1] = (uint16_t)((n * 7) % 4096);
}
uint8_t *pl = frame + 8;
smi_put_u64(pl, (uint64_t)fr * 1024);
smi_put_u32(pl + 8, 1000u + 2048u * (unsigned)fr);
pl[12] = 16;
size_t o = 13;
for (int g = 0; g < 16; g++) {
o += smi_pack_block(pairs + g * 128, 2, pl + o);
o += smi_pack_block(pairs + g * 128 + 1, 2, pl + o);
}
smi_begin_frame(frame, SMI_T_DATA, (uint16_t)o, (uint16_t)fr);
fwrite(frame, 1, smi_finish_frame(frame, (uint16_t)o), f);
}
fclose(f);
if (fails) { printf("ERROR: %d round-trip failures\n", fails); return 1; }
printf("OK: 40000 blocks round-trip exactly; crc16 check 29B1; mean %.2f bits/sample incl. headers; wrote %s\n",
8.0 * bytes_total / samples_total, out);
return 0;
}