main
John Lauer
Publish assumption-based R11 assembly study, locked firmware and new review footage
d61d6cb
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
/* Register-level implementation from TI DACx0501 SBAS794E and INA226 SBOS547.
* Board adapter supplies bounded-time I2C operations and big-endian 16-bit words.
* Keep MCU_ENABLE low throughout initialization and on false / LOAD_INA_ERROR.
* LOAD_INA_WAIT does not refresh sample age; adapter must expire stale data.
*/
#include "instruments.h"
#define DAC_ADDR 0x48
#define INA_ADDR 0x40
#define INA_CONFIG 0x4523
#define INA_CALIBRATION 1024
static bool readable(const load_i2c *b){return b && b->read16;}
static bool writable(const load_i2c *b){return readable(b) && b->write16;}
bool load_dac_write(const load_i2c *b,uint16_t code){
if(!writable(b)||code>4095)return false;
uint16_t status=0,gain=0,readback=0,config=0,sync=0;
/* A reset or ignored transaction must not silently change the current scale.
The adapter must force enable LOW if any of these transactions fails. */
return b->read16(DAC_ADDR,4,&gain) && gain==0x0101 &&
b->read16(DAC_ADDR,3,&config) && config==0 &&
b->read16(DAC_ADDR,2,&sync) && sync==0 &&
b->write16(DAC_ADDR,8,(uint16_t)(code<<4)) &&
b->read16(DAC_ADDR,8,&readback) && readback==(uint16_t)(code<<4) &&
b->read16(DAC_ADDR,7,&status) && !(status&1);
}
bool load_instruments_init(const load_i2c *b){
uint16_t id=0,mfr=0,gain=0,status=0,config=0,calibration=0;
if(!b || !b->write16 || !b->read16 || !b->delay_ms)return false;
if(!b->write16(DAC_ADDR,8,0))return false;
if(!b->read16(DAC_ADDR,1,&id)||id!=0x2115)return false; /* DAC60501Z, not M or 16-bit */
/* At VDD=3.3V the internal 2.5V reference MUST be divided by 2.
Buffer gain 2 restores a 2.5V output span. GAIN bits 8 and 0 = 1. */
if(!b->write16(DAC_ADDR,3,0)||!b->write16(DAC_ADDR,4,0x0101)||!b->write16(DAC_ADDR,2,0))return false;
b->delay_ms(10); /* conservative startup delay; verify on hardware */
if(!b->read16(DAC_ADDR,4,&gain)||gain!=0x0101||!b->read16(DAC_ADDR,7,&status)||(status&1))return false;
if(!b->read16(INA_ADDR,0xFE,&mfr)||mfr!=0x5449||!b->read16(INA_ADDR,0xFF,&id)||(id&0xFFF0)!=0x2260)return false;
/* 16 averages, 1.1ms bus + 1.1ms shunt each, triggered (~35.2ms conversion). */
if(!b->write16(INA_ADDR,0,INA_CONFIG)||!b->write16(INA_ADDR,5,INA_CALIBRATION)||
!b->read16(INA_ADDR,0,&config)||config!=INA_CONFIG||
!b->read16(INA_ADDR,5,&calibration)||calibration!=INA_CALIBRATION)return false;
return load_dac_write(b,0);
}
load_ina_result load_ina_poll(const load_i2c *b,float *v,float *a){
if(!writable(b)||!v||!a||v==a)return LOAD_INA_ERROR;
uint16_t flags=0,shunt=0,bus=0,config=0;
/* Detect device reset/reconfiguration before accepting a new measurement. */
if(!b->read16(INA_ADDR,0,&config)||config!=INA_CONFIG)return LOAD_INA_ERROR;
if(!b->read16(INA_ADDR,6,&flags)||(flags&4))return LOAD_INA_ERROR;
if(!(flags&8))return LOAD_INA_WAIT;
/* Triggered conversions have stopped before the ready flag is observed;
both result registers therefore belong to the same completed conversion. */
if(!b->read16(INA_ADDR,1,&shunt)||!b->read16(INA_ADDR,2,&bus))return LOAD_INA_ERROR;
if(!b->write16(INA_ADDR,0,INA_CONFIG))return LOAD_INA_ERROR; /* start next conversion */
/* The current conversion below uses shunt volts directly, independent of CAL.
Bus measurement is AFTER the input protection diode, not the DUT connector. */
*v=(float)bus*.00125f;*a=(float)(int16_t)shunt*.000125f;
return LOAD_INA_READY;
}