|
| 1 | +#include <stdio.h> |
| 2 | +#include "pico/stdlib.h" |
| 3 | +#include "hardware/i2c.h" |
| 4 | +#include <math.h> |
| 5 | + |
| 6 | +// LED GPIO |
| 7 | +#define EXT_LED_GPIO 2 |
| 8 | + |
| 9 | +// INA228 breakout board shunt resistance in Ohms |
| 10 | +#define R_SHUNT 0.015 |
| 11 | + |
| 12 | +// Max expected current through shunt resistor in Amps |
| 13 | +#define MAX_EXPECTED_CURRENT 0.1 |
| 14 | +#define I2C_ADDR 0x40 |
| 15 | + |
| 16 | +// Macros for converting arrays of bytes to decimal integer |
| 17 | +#define COALESCE_2_BYTES(a2) ((a2[0] << 8) | a2[1]) |
| 18 | +#define COALESCE_3_BYTES(a3) ((a3[0] << 16) | (a3[1] << 8) | a3[2]) |
| 19 | +#define COALESCE_RESERVED_3_BYTES(a3) ((a3[0] << 12) | (a3[1] << 4) | (a3[2] >> 4)) |
| 20 | +#define COALESCE_5_BYTES(a5) (((uint64_t)a5[0] << 32) | (a5[1] << 24) | (a5[2] << 16) | (a5[3] << 8) | a5[4]) |
| 21 | + |
| 22 | +// ina228 registers (see datasheet) |
| 23 | +uint8_t VSHUNT_REG = 0x04; |
| 24 | +uint8_t VBUS_REG = 0x05; |
| 25 | +uint8_t DIETEMP_REG = 0x06; |
| 26 | +uint8_t CURRENT_REG = 0x07; |
| 27 | +uint8_t POWER_REG = 0x08; |
| 28 | +uint8_t ENERGY_REG = 0x09; |
| 29 | +uint8_t CHARGE_REG = 0x0A; |
| 30 | + |
| 31 | +uint8_t ADC_CONFIG_REG = 0x01; |
| 32 | +uint8_t SHUNT_CAL_REG = 0x02; |
| 33 | + |
| 34 | +// conversion factors (see datasheet) |
| 35 | +// note: VSHUNT_FACTOR assumes ADCRANGE = 0 |
| 36 | +const double VSHUNT_FACTOR = 312.5 * 1e-9; |
| 37 | +const double VBUS_FACTOR = 195.3125 * 1e-6; |
| 38 | +const double DIETEMP_FACTOR = 7.8125 * 1e-3; |
| 39 | +const double CURRENT_FACTOR = MAX_EXPECTED_CURRENT / (1<<19); |
| 40 | +const double POWER_FACTOR = 3.2 * CURRENT_FACTOR ; |
| 41 | +const double ENERGY_FACTOR = 16 * 3.2 * CURRENT_FACTOR; |
| 42 | +const double CHARGE_FACTOR = CURRENT_FACTOR; |
| 43 | + |
| 44 | +const uint16_t SHUNT_CAL = 13107.2 * 1e6 * CURRENT_FACTOR * R_SHUNT; |
| 45 | + |
| 46 | +float vshunt, vbus, dietemp, current, power, energy, charge; |
| 47 | + |
| 48 | +static void ina228_init() { |
| 49 | + // Write to shunt register |
| 50 | + uint8_t shunt_msb = (SHUNT_CAL >> 8) & 0xFF; |
| 51 | + uint8_t shunt_lsb = SHUNT_CAL & 0xFF; |
| 52 | + uint8_t shunt_buf[3] = {SHUNT_CAL_REG, shunt_msb, shunt_lsb}; |
| 53 | + i2c_write_blocking(i2c_default, I2C_ADDR, shunt_buf, 3, false); |
| 54 | + |
| 55 | + // Write to ADC config register (need to enable continuous mode to access accumulation variables energy and charge) |
| 56 | + uint8_t adc_msb = 0xF0; |
| 57 | + uint8_t adc_lsb = 0x00; |
| 58 | + uint8_t adc_buf[3] = {ADC_CONFIG_REG, adc_msb, adc_lsb}; |
| 59 | + i2c_write_blocking(i2c_default, I2C_ADDR, adc_buf, 3, false); |
| 60 | +} |
| 61 | + |
| 62 | +static void ina228_read(float *vshunt, float *vbus, float *dietemp, float *current, float *power, float *energy, float *charge) { |
| 63 | + // Buffers for writing register measurements to |
| 64 | + // Some registers are 2 bytes, some are 3 bytes, the accumulation registers are 5 bytes |
| 65 | + uint8_t buffer_2[2]; |
| 66 | + uint8_t buffer_3[3]; |
| 67 | + uint8_t buffer_5[5]; |
| 68 | + |
| 69 | + // For the vshunt, vbus and current registers the 4 least significant bits are reserved and always read zero, so use variant of coalesce macro to extract value |
| 70 | + i2c_write_blocking(i2c_default, I2C_ADDR, &VSHUNT_REG, 1, true); |
| 71 | + i2c_read_blocking(i2c_default, I2C_ADDR, buffer_3, 3, false); |
| 72 | + *vshunt = COALESCE_RESERVED_3_BYTES(buffer_3) * VSHUNT_FACTOR; |
| 73 | + |
| 74 | + i2c_write_blocking(i2c_default, I2C_ADDR, &VBUS_REG, 1, true); |
| 75 | + i2c_read_blocking(i2c_default, I2C_ADDR, buffer_3, 3, false); |
| 76 | + *vbus = COALESCE_RESERVED_3_BYTES(buffer_3) * VBUS_FACTOR; |
| 77 | + |
| 78 | + i2c_write_blocking(i2c_default, I2C_ADDR, &DIETEMP_REG, 1, true); |
| 79 | + i2c_read_blocking(i2c_default, I2C_ADDR, buffer_2, 2, false); |
| 80 | + *dietemp = COALESCE_2_BYTES(buffer_2) * DIETEMP_FACTOR; |
| 81 | + |
| 82 | + i2c_write_blocking(i2c_default, I2C_ADDR, &CURRENT_REG, 1, true); |
| 83 | + i2c_read_blocking(i2c_default, I2C_ADDR, buffer_3, 3, false); |
| 84 | + *current = COALESCE_RESERVED_3_BYTES(buffer_3) * CURRENT_FACTOR; |
| 85 | + |
| 86 | + i2c_write_blocking(i2c_default, I2C_ADDR, &POWER_REG, 1, true); |
| 87 | + i2c_read_blocking(i2c_default, I2C_ADDR, buffer_3, 3, false); |
| 88 | + *power = COALESCE_3_BYTES(buffer_3) * POWER_FACTOR; |
| 89 | + |
| 90 | + i2c_write_blocking(i2c_default, I2C_ADDR, &ENERGY_REG, 1, true); |
| 91 | + i2c_read_blocking(i2c_default, I2C_ADDR, buffer_5, 5, false); |
| 92 | + *energy = COALESCE_5_BYTES(buffer_5) * ENERGY_FACTOR; |
| 93 | + |
| 94 | + i2c_write_blocking(i2c_default, I2C_ADDR, &CHARGE_REG, 1, true); |
| 95 | + i2c_read_blocking(i2c_default, I2C_ADDR, buffer_5, 5, false); |
| 96 | + *charge = COALESCE_5_BYTES(buffer_5) * CHARGE_FACTOR; |
| 97 | +} |
| 98 | + |
| 99 | +int main() |
| 100 | +{ |
| 101 | + stdio_init_all(); |
| 102 | + |
| 103 | + // Initialise external LED and turn it on (so we can measure some current) |
| 104 | + gpio_init(EXT_LED_GPIO); |
| 105 | + gpio_set_dir(EXT_LED_GPIO, GPIO_OUT); |
| 106 | + gpio_put(EXT_LED_GPIO, true); |
| 107 | + |
| 108 | + // I2C initialisation |
| 109 | + i2c_init(i2c_default, 400*1000); |
| 110 | + |
| 111 | + // GPIO initialisation |
| 112 | + gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C); |
| 113 | + gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C); |
| 114 | + gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN); |
| 115 | + gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN); |
| 116 | + |
| 117 | + // Initialise ina228 |
| 118 | + ina228_init(); |
| 119 | + |
| 120 | + while (true) { |
| 121 | + ina228_read(&vshunt, &vbus, &dietemp, ¤t, &power, &energy, &charge); |
| 122 | + printf("INA228 Measurements:\nVSHUNT: %f V\nVBUS: %f V\nDIETEMP: %f °C\nCURRENT: %f A\nPOWER: %f W\nENERGY: %f J\nCHARGE: %f C\n-----------------\n", vshunt, vbus, dietemp, current, power, energy, charge); |
| 123 | + sleep_ms(1000); |
| 124 | + } |
| 125 | +} |
0 commit comments