/*
 * i2c_ina260.h
 *
 *  Created on: Jul 31, 2024
 *      Author: tm
 */

#ifndef I2C_INA260_H_
#define I2C_INA260_H_

#include <stdint.h>

#define INA260_LAMP_I2C_ADDR 0x4c
#define INA260_SYS_I2C_ADDR  0x4f

// Configuration registers and their addresses
#define INA260_CONFIG      0x00U
#define INA260_CURRENT     0x01U
#define INA260_BUS_VOLTAGE 0x02U
#define INA260_POWER       0x03U
#define INA260_MF_ID       0xFEU
#define INA260_DIE_ID      0xFFU

// INA260 configuration values
#define INA260_DEF_CONFIG_VALUE 0x6127U // Continuous bus and shunt, 1.1ms conversion time, 1 avg

// Calibration constants (replace with your values)
#define CALIBRATION_CURRENT 0.00125f // A/LSB
#define CALIBRATION_VOLTAGE 0.00125f // V/LSB
#define CALIBRATION_POWER   0.010f   // W/LSB

// Poll ina260 repeatedly for self testing
// return 1 on success, <= 0 on failure
int ina260_soak_test(void);
// Poll ina260 for self testing
// return 1 on success, <= 0 on failure
int ina260_dev_test(uint8_t i2cAddr);
void ina260_test(uint8_t i2cAddr);
int ina260_system_voltageCurrent( float *volts, float *amps );
int ina260_lamp_voltageCurrent( float *volts, float *amps );
float ina260_calc_voltage(uint16_t rawData);
float ina260_calc_current(uint16_t rawData);
float ina260_calc_power(uint16_t rawData);

// read mfg id and dev id register to see if this is really a ina260
// return 1 if it is probably a ina260, <= 0 on failure
int ina260_probe(uint8_t i2cAddr);

// probe for a ina260 and set config to INA260_DEF_CONFIG_VALUE if it exists
// return 1 on success, <= 0 on failure
int ina260_init(uint8_t i2cAddr);

// Set ina260 to low power mode, return 0 on success. Does a RMW on config reg
// return 1 on success, <= 0 on failure
int ina260_shutdown(uint8_t i2cAddr);
// Set ina260 to continuous mode, return 0 on success. Does a RMW on config reg
// return 1 on success, <= 0 on failure
int ina260_continuous(uint8_t i2cAddr);

// Write a reg
// return 1 on success, <= 0 on failure
int ina260_write_register(uint8_t i2cAddr, uint8_t reg, uint16_t data);
// Read a reg
// return 1 on success, <= 0 on failure
int ina260_read_register(uint8_t i2cAddr, uint8_t regAddr, uint16_t* out_data);

#endif /* I2C_INA260_H_ */
