Init
This commit is contained in:
53
main/hw/ina260.c
Normal file
53
main/hw/ina260.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "ina260.h"
|
||||
|
||||
i2c_device_config_t INA260_DEV_CFG = {
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = 0x40,
|
||||
.scl_speed_hz = 100000,
|
||||
};
|
||||
|
||||
i2c_master_dev_handle_t INA260_DEV_HANDLE;
|
||||
|
||||
void ina260_init()
|
||||
{
|
||||
ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c0_bus_hdl, &INA260_DEV_CFG, &INA260_DEV_HANDLE));
|
||||
i2c_write_register_16(INA260_DEV_HANDLE, 0x00, 0x0FFF); // set ina max averaging and max time
|
||||
}
|
||||
|
||||
void ina260_readParams(uint32_t *volt, uint32_t *cur, uint32_t *pow)
|
||||
{
|
||||
for (uint8_t reg_addr = 1; reg_addr <= 3; reg_addr++)
|
||||
{
|
||||
uint8_t reg_value[2] = {0}; // Buffer for storing register data
|
||||
|
||||
// Perform the register read
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(INA260_DEV_HANDLE, ®_addr, 1, reg_value, sizeof(reg_value), I2C_TIMEOUT_MS_VALUE));
|
||||
switch (reg_addr)
|
||||
{
|
||||
case 1:
|
||||
*cur = *((uint16_t *)reg_value);
|
||||
|
||||
break;
|
||||
case 2:
|
||||
*volt = *((uint16_t *)reg_value);
|
||||
|
||||
break;
|
||||
case 3:
|
||||
*pow = *((uint16_t *)reg_value);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ina260_printParams(uint32_t volt, uint32_t cur, uint32_t pow)
|
||||
{
|
||||
cur *= 125;
|
||||
ESP_LOGI(TAG_INA, "Current: %ld.%ld mA", cur / 10000, cur % 10000);
|
||||
cur *= 125;
|
||||
ESP_LOGI(TAG_INA, "Voltage: %ld.%ld V", volt / 10000, volt % 10000);
|
||||
|
||||
ESP_LOGI(TAG_INA, "Power: %ld.%ld W", pow / 10000, pow % 10000);
|
||||
}
|
Reference in New Issue
Block a user