Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions i2c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if (TARGET hardware_i2c)
add_subdirectory_exclude_platforms(pcf8523_i2c)
add_subdirectory_exclude_platforms(ht16k33_i2c)
add_subdirectory_exclude_platforms(slave_mem_i2c)
add_subdirectory_exclude_platforms(ina260_i2c)
else()
message("Skipping I2C examples as hardware_i2c is unavailable on this platform")
endif()
8 changes: 8 additions & 0 deletions i2c/ina260_i2c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_executable(ina260_i2c
ina260_i2c.c
)
target_link_libraries(ina260_i2c
pico_stdlib
hardware_i2c
)
pico_add_extra_outputs(ina260_i2c)
52 changes: 52 additions & 0 deletions i2c/ina260_i2c/ina260_i2c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"

#define CURRENT_REGISTER 0x01
#define VOLTAGE_REGISTER 0x02
#define POWER_REGISTER 0x03
#define I2C_ADDRESS 0x40

#define ByteSwap(u) (uint16_t)((u << 8)|(u >> 8))

// Read register value
static uint16_t read_reg(uint8_t reg) {
// Set the register address
int ret = i2c_write_blocking(i2c_default, I2C_ADDRESS, &reg, 1, true); // no stop is set
assert(ret == 1);
if (ret != 1) return 0;
// Read the value
uint16_t data;
ret = i2c_read_blocking(i2c_default, I2C_ADDRESS, (uint8_t*)&data, 2, false);
assert(ret == 2);
if (ret != 2) return 0;
return ByteSwap(data);
}

int main() {
setup_default_uart();
printf("ina260 test\n");

// Initialise i2c
i2c_init(i2c_default, 100 * 1000);
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
Comment on lines +33 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice (for completeness) if this did the bi_pins thing 🙂

gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);

while(true) {

// Read current and convert to mA
float ma = read_reg(CURRENT_REGISTER) * 1.250f;
if (ma > 15000) ma = 0;
// Read the voltage
float v = read_reg(VOLTAGE_REGISTER) * 0.00125f;
// Read power and convert to mW
uint16_t mw = read_reg(POWER_REGISTER) * 10;

// Display results
printf("current: %.2f mA voltage: %.2f v power: %u mW\n", ma, v, mw);
sleep_ms(1000);
}
return 0;
}