|
rcbench
Servo and ESC test bench: panel, IOmcu and shared C
|
The INA228. More...
Macros | |
| #define | CODE20_MAX 524287 |
| Ends of a 20-bit two's complement register. | |
| #define | CODE20_MIN (-524288) |
| #define | FS_NARROW_NV 40960000u |
| Full-scale shunt voltages as max mA × µΩ, which is nV (Table 8-1). | |
| #define | FS_WIDE_NV 163840000u |
Functions | |
| uint16_t | ina228_adc_config (uint8_t mode, uint8_t vbusct, uint8_t vshct, uint8_t vtct, uint8_t avg) |
| ADC_CONFIG from its fields; each is masked to its width. | |
| static uint32_t | ina228_ct_us (uint8_t code) |
| Conversion time of a VBUSCT, VSHCT or VTCT code, µs (Table 7-6). | |
| uint32_t | ina228_averages (uint8_t code) |
| Samples averaged for an AVG code (Table 7-6). | |
| uint32_t | ina228_cycle_us (uint16_t adc_config) |
| Time from one result to the next for an ADC_CONFIG, µs: the enabled inputs' conversion times, one after another, times the averages (§7.3.4). | |
| bool | ina228_identity_ok (uint16_t maker, uint16_t device) |
| Whether MANUFACTURER_ID and DEVICE_ID name an INA228. | |
| ina228_setup_err_t | ina228_calibrate (uint32_t shunt_uohm, uint32_t max_ma, ina228_cal_t *out) |
SHUNT_CAL and ADCRANGE for a shunt and a maximum current. out is filled only on INA228_SETUP_OK. | |
| static uint64_t | r_k (const ina228_cal_t *cal) |
| R × 4 at ADCRANGE 1: the denominator every quantity scaled by CURRENT_LSB = SHUNT_CAL / (13107.2 × R[µΩ] × (4 at ADCRANGE 1)) shares. | |
| int32_t | ina228_code20 (uint32_t raw24) |
| The 20-bit value in bits 23-4 of a 24-bit register (VSHUNT, VBUS, CURRENT), as raw: -2^19 to 2^19 - 1. | |
| static sense_clip_t | clip20 (int32_t code) |
| Classify a 20-bit code: at either end of its range it is a clip. | |
| sense_value_t | ina228_current_ua (const ina228_cal_t *cal, int32_t code) |
| CURRENT in µA; a code at either end is a clip. | |
| sense_value_t | ina228_vshunt_nv (const ina228_cal_t *cal, int32_t code) |
| VSHUNT in nV: 312.5 nV a step at ADCRANGE 0, 78.125 nV at 1 (Table 7-9); a code at either end is a clip. | |
| int32_t | ina228_vbus_uv (int32_t code) |
| VBUS in µV, 195.3125µV a step (Table 7-10). | |
| int32_t | ina228_dietemp_mdegc (uint16_t raw) |
| DIETEMP in m°C, 7.8125m°C a step (Table 7-11). | |
| uint64_t | ina228_power_uw (const ina228_cal_t *cal, uint32_t raw24) |
| POWER (24 bit, unsigned) in µW: 3.2 × CURRENT_LSB a step (Eq. | |
| uint64_t | ina228_energy_mj (const ina228_cal_t *cal, uint64_t raw40) |
| ENERGY (40 bit, unsigned) in mJ: 16 × 3.2 × CURRENT_LSB a step (Eq. | |
| int64_t | ina228_charge_uc (const ina228_cal_t *cal, uint64_t raw40) |
| CHARGE (40 bit, two's complement) in µC: CURRENT_LSB a step (Eq. | |
| ina228_setup_err_t | ina228_init (ina228_t *d, sense_bus_t *bus, uint8_t addr, uint32_t shunt_uohm, uint32_t max_ma, uint16_t adc_config) |
A part at addr with a shunt, a maximum current and an ADC_CONFIG (INA228_ADC_BENCH for this bench). | |
| static bool | read16 (ina228_t *d, uint8_t reg, uint16_t *v) |
| Read a 16-bit register. | |
| static bool | write16 (ina228_t *d, uint8_t reg, uint16_t v) |
| Write a 16-bit register. | |
| static sense_state_t | probe (ina228_t *d) |
| Probe the part: identity, then the set-up written and read back. | |
| bool | ina228_step (ina228_t *d, uint32_t now_ms) |
| Every tick: probes the part when it is due – at the first call, and SENSE_RETRY_MS after it was found absent, wrong, refused or offline. | |
| sense_state_t | ina228_state (const ina228_t *d) |
| The part's state as of the last probe. | |
| static sense_err_t | read24 (ina228_t *d, uint8_t reg, uint32_t *raw) |
| Read a 24-bit register value, 3 bytes. | |
| static sense_err_t | read40 (ina228_t *d, uint8_t reg, uint64_t *raw) |
| Read a 40-bit register value, 5 bytes. | |
| static sense_err_t | read_reg16 (ina228_t *d, uint8_t reg, uint16_t *raw) |
| Read a 16-bit register value, 2 bytes. | |
| sense_err_t | ina228_read_current (ina228_t *d, sense_value_t *ua) |
| Readings. | |
| sense_err_t | ina228_read_vshunt (ina228_t *d, sense_value_t *nv) |
| Read the shunt voltage. | |
| sense_err_t | ina228_read_vbus (ina228_t *d, int32_t *uv) |
| Read the bus voltage. | |
| sense_err_t | ina228_read_power (ina228_t *d, uint64_t *uw) |
| Read the power. | |
| sense_err_t | ina228_read_dietemp (ina228_t *d, int32_t *mdegc) |
| Read the die temperature. | |
| sense_err_t | ina228_read_diag (ina228_t *d, uint16_t *diag) |
| DIAG_ALRT as read; the read clears the latched flags (Table 7-16). | |
| sense_err_t | ina228_read_energy (ina228_t *d, uint64_t *mj) |
| Read the energy. | |
| sense_err_t | ina228_read_charge (ina228_t *d, int64_t *uc) |
| Read the charge. | |
| sense_err_t | ina228_clear_totals (ina228_t *d) |
| ENERGY and CHARGE to 0: CONFIG with RSTACC, then without. | |
| sense_err_t | ina228_verify (ina228_t *d, bool *lost) |
| ADC_CONFIG read back against the value written. | |
| #define FS_NARROW_NV 40960000u |
Full-scale shunt voltages as max mA × µΩ, which is nV (Table 8-1).
±40.96mV, ADCRANGE 1
| #define FS_WIDE_NV 163840000u |
±163.84mV, ADCRANGE 0
|
static |
Classify a 20-bit code: at either end of its range it is a clip.
| code | The 20-bit code, -524288 to 524287. |
| uint16_t ina228_adc_config | ( | uint8_t | mode, |
| uint8_t | vbusct, | ||
| uint8_t | vshct, | ||
| uint8_t | vtct, | ||
| uint8_t | avg ) |
ADC_CONFIG from its fields; each is masked to its width.
| mode | Operating mode. |
| vbusct | Bus voltage conversion time code. |
| vshct | Shunt voltage conversion time code. |
| vtct | Temperature conversion time code. |
| avg | Averaging code. |
| uint32_t ina228_averages | ( | uint8_t | code | ) |
Samples averaged for an AVG code (Table 7-6).
| code | The AVG code. |
| ina228_setup_err_t ina228_calibrate | ( | uint32_t | shunt_uohm, |
| uint32_t | max_ma, | ||
| ina228_cal_t * | out ) |
SHUNT_CAL and ADCRANGE for a shunt and a maximum current. out is filled only on INA228_SETUP_OK.
| shunt_uohm | Shunt resistance, in µΩ. |
| max_ma | Maximum current, in mA. |
| out | Receives the calibration. |
| int64_t ina228_charge_uc | ( | const ina228_cal_t * | cal, |
| uint64_t | raw40 ) |
CHARGE (40 bit, two's complement) in µC: CURRENT_LSB a step (Eq.
7).
| cal | The calibration. |
| raw40 | The 40-bit CHARGE register value. |
| sense_err_t ina228_clear_totals | ( | ina228_t * | d | ) |
ENERGY and CHARGE to 0: CONFIG with RSTACC, then without.
A failure of the second write leaves RSTACC set; the call is to be repeated.
| d | The driver. |
| int32_t ina228_code20 | ( | uint32_t | raw24 | ) |
The 20-bit value in bits 23-4 of a 24-bit register (VSHUNT, VBUS, CURRENT), as raw: -2^19 to 2^19 - 1.
| raw24 | The 24-bit register value. |
|
static |
Conversion time of a VBUSCT, VSHCT or VTCT code, µs (Table 7-6).
| code | The conversion time code. |
| sense_value_t ina228_current_ua | ( | const ina228_cal_t * | cal, |
| int32_t | code ) |
CURRENT in µA; a code at either end is a clip.
| cal | The calibration. |
| code | The CURRENT code. |
| uint32_t ina228_cycle_us | ( | uint16_t | adc_config | ) |
Time from one result to the next for an ADC_CONFIG, µs: the enabled inputs' conversion times, one after another, times the averages (§7.3.4).
0 for a mode that does not convert continuously.
| adc_config | The ADC_CONFIG register value. |
| int32_t ina228_dietemp_mdegc | ( | uint16_t | raw | ) |
DIETEMP in m°C, 7.8125m°C a step (Table 7-11).
| raw | The DIETEMP register value. |
| uint64_t ina228_energy_mj | ( | const ina228_cal_t * | cal, |
| uint64_t | raw40 ) |
ENERGY (40 bit, unsigned) in mJ: 16 × 3.2 × CURRENT_LSB a step (Eq.
6).
| cal | The calibration. |
| raw40 | The 40-bit ENERGY register value. |
| bool ina228_identity_ok | ( | uint16_t | maker, |
| uint16_t | device ) |
Whether MANUFACTURER_ID and DEVICE_ID name an INA228.
| maker | The MANUFACTURER_ID register value. |
| device | The DEVICE_ID register value. |
| ina228_setup_err_t ina228_init | ( | ina228_t * | d, |
| sense_bus_t * | bus, | ||
| uint8_t | addr, | ||
| uint32_t | shunt_uohm, | ||
| uint32_t | max_ma, | ||
| uint16_t | adc_config ) |
A part at addr with a shunt, a maximum current and an ADC_CONFIG (INA228_ADC_BENCH for this bench).
On anything but INA228_SETUP_OK the part is left SENSE_PART_UNSET and nothing is ever sent to it.
| d | The driver to set up. |
| bus | The sensor bus the part sits on. |
| addr | I2C address of the part. |
| shunt_uohm | Shunt resistance, in µΩ. |
| max_ma | Maximum current, in mA. |
| adc_config | The ADC_CONFIG register value. |
| uint64_t ina228_power_uw | ( | const ina228_cal_t * | cal, |
| uint32_t | raw24 ) |
POWER (24 bit, unsigned) in µW: 3.2 × CURRENT_LSB a step (Eq.
5).
| cal | The calibration. |
| raw24 | The 24-bit POWER register value. |
| sense_err_t ina228_read_charge | ( | ina228_t * | d, |
| int64_t * | uc ) |
Read the charge.
| d | The driver. |
| uc | Receives the charge, in µC. |
| sense_err_t ina228_read_current | ( | ina228_t * | d, |
| sense_value_t * | ua ) |
Readings.
Each is one transaction; on anything but SENSE_OK the output is left as it was.
| d | The driver. |
| ua | Receives the current, in µA. |
| sense_err_t ina228_read_diag | ( | ina228_t * | d, |
| uint16_t * | diag ) |
DIAG_ALRT as read; the read clears the latched flags (Table 7-16).
| d | The driver. |
| diag | Receives the DIAG_ALRT value. |
| sense_err_t ina228_read_dietemp | ( | ina228_t * | d, |
| int32_t * | mdegc ) |
Read the die temperature.
| d | The driver. |
| mdegc | Receives the die temperature, in m°C. |
| sense_err_t ina228_read_energy | ( | ina228_t * | d, |
| uint64_t * | mj ) |
Read the energy.
| d | The driver. |
| mj | Receives the energy, in mJ. |
| sense_err_t ina228_read_power | ( | ina228_t * | d, |
| uint64_t * | uw ) |
Read the power.
| d | The driver. |
| uw | Receives the power, in µW. |
| sense_err_t ina228_read_vbus | ( | ina228_t * | d, |
| int32_t * | uv ) |
Read the bus voltage.
| d | The driver. |
| uv | Receives the bus voltage, in µV. |
| sense_err_t ina228_read_vshunt | ( | ina228_t * | d, |
| sense_value_t * | nv ) |
Read the shunt voltage.
| d | The driver. |
| nv | Receives the shunt voltage, in nV. |
| sense_state_t ina228_state | ( | const ina228_t * | d | ) |
The part's state as of the last probe.
| d | The driver. |
| bool ina228_step | ( | ina228_t * | d, |
| uint32_t | now_ms ) |
Every tick: probes the part when it is due – at the first call, and SENSE_RETRY_MS after it was found absent, wrong, refused or offline.
A probe is 10 transactions, 1075µs of bus time at 400kHz without the controller's own time between them. Returns whether the part is online.
| d | The driver. |
| now_ms | The current time, in ms. |
| int32_t ina228_vbus_uv | ( | int32_t | code | ) |
VBUS in µV, 195.3125µV a step (Table 7-10).
| code | The VBUS code. |
| sense_err_t ina228_verify | ( | ina228_t * | d, |
| bool * | lost ) |
ADC_CONFIG read back against the value written.
A part that resets itself answers with INA228_ADC_RESET at either range, and CONFIG with 0, which is also what ADCRANGE 0 writes: ADC_CONFIG is the register that tells. Such a part computes CURRENT at ADCRANGE 0, a quarter of the truth for a set-up at ADCRANGE 1. Another value than the one written is read a second time at once, so one corrupted read is no reset; when both differ the part is taken offline (sense_part_lost()), lost is set and SENSE_OK returned. A read of FFFFh, the bus that no part drives, takes it offline on that read without a second. lost is false otherwise. The readings are not checked for all ones: CURRENT FFFFFFh is code -1 and taken. One transaction, two on a first mismatch other than FFFFh: 120µs of bus time each at 400kHz.
| d | The driver. |
| lost | Set true when the part was taken offline. |
| sense_value_t ina228_vshunt_nv | ( | const ina228_cal_t * | cal, |
| int32_t | code ) |
VSHUNT in nV: 312.5 nV a step at ADCRANGE 0, 78.125 nV at 1 (Table 7-9); a code at either end is a clip.
| cal | The calibration. |
| code | The VSHUNT code. |
|
static |
Probe the part: identity, then the set-up written and read back.
| d | The driver. |
|
static |
R × 4 at ADCRANGE 1: the denominator every quantity scaled by CURRENT_LSB = SHUNT_CAL / (13107.2 × R[µΩ] × (4 at ADCRANGE 1)) shares.
| cal | The calibration. |
|
static |
Read a 16-bit register.
| d | The driver. |
| reg | Register address. |
| v | Receives the value. |
|
static |
Read a 24-bit register value, 3 bytes.
| d | The driver. |
| reg | Register address. |
| raw | Receives the value. |
|
static |
Read a 40-bit register value, 5 bytes.
| d | The driver. |
| reg | Register address. |
| raw | Receives the value. |
|
static |
Read a 16-bit register value, 2 bytes.
| d | The driver. |
| reg | Register address. |
| raw | Receives the value. |
|
static |
Write a 16-bit register.
| d | The driver. |
| reg | Register address. |
| v | The value to write. |