0hmX/pcbgolf-routing-checkpoint-20261012
A PCBGolf assembly combining a modeled PCB with copper/component bodies and seven native connector housings.
- Version
- 0.0.3
- License
- unset
- Stars
- 0
firmware-support/test_imon.c
#include "pcbgolf_board.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
static void near(float actual, float expected, float limit) { assert(fabsf(actual-expected) <= limit); }
int main(void) {
pcbgolf_imon_cal_t nominal = {PCBGOLF_IMON_ZERO_V, PCBGOLF_IMON_V_PER_A}, fit;
float out;
/* Quantized known loads, 16-bit ADC1 and alternate 12-bit adapter. */
for (unsigned bits=12;bits<=16;bits+=4) for (unsigned i=0;i<=10;++i) {
float current=(float)i/2.0f;
float volts=nominal.zero_v+current*nominal.volts_per_a;
uint32_t fullscale=(1U<<bits)-1U, raw=(uint32_t)lroundf(volts/3.3f*(float)fullscale);
assert(pcbgolf_imon_current(raw,(uint8_t)bits,3.3f,&nominal,&out));
near(out,current,3.3f/(float)fullscale/nominal.volts_per_a);
}
assert(pcbgolf_imon_current(0,16,3.3f,&nominal,&out)); near(out,0,0);
/* Measured no-load and 2A gain: independently differs from nominal offset/gain. */
assert(pcbgolf_fit_imon(.007f,.507f,2.0f,&fit)); near(fit.volts_per_a,.25f,1e-7f);
assert(pcbgolf_imon_current(10069,16,3.3f,&fit,&out)); near(out,2.0f,.001f);
assert(!pcbgolf_imon_current(65536,16,3.3f,&fit,&out));
assert(!pcbgolf_imon_current(0,10,3.3f,&fit,&out));
assert(!pcbgolf_imon_current(0,16,2.9f,&fit,&out));
assert(!pcbgolf_imon_current(0,16,NAN,&fit,&out));
assert(!pcbgolf_fit_imon(.007f,.006f,2.0f,&fit));
assert(!pcbgolf_fit_imon(.007f,.507f,0.0f,&fit));
assert(!pcbgolf_fit_imon(.007f,.507f,.1f,&fit));
fit.volts_per_a=INFINITY;
assert(!pcbgolf_imon_current(0,16,3.3f,&fit,&out));
assert(!pcbgolf_fit_imon(.007f,.507f,NAN,&fit));
puts("IMON: 22 quantized-load cases, measured 2A calibration, no-load clamp, and invalid-input cases pass.");
return 0;
}