I have a board with the npm1300 and a nrf58240. I'm trying to read some status registers out of the npm1300 to debug some trouble we are having with it. It doesn't seem to recover from battery voltages around 2.6V -2.8V so I want to see the errors and status.
I read the doc, i see it's a 16bit addressesing scheme where the first part is the offset and the second part is the register address. So to read BCHGCONFIG for example which is 0x3C and has a offset "I think" of 0x300 based on the datasheet (it's not really clear). To read that I think I need to do a 16bit read from 0x033C.
Today my code looks like this and always returns true, and gives me 0 for my data:
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Function to read a 16-bit register from the nPM1300
bool read_npm1300_register_16bit(uint16_t register_address, uint8_t *destination) {
uint8_t w_addr[2];
// Split the 16-bit register address into two bytes
w_addr[0] = (register_address >> 8) & 0xFF; // High byte
w_addr[1] = register_address & 0xFF; // Low byte
// Write the 16-bit register address
int ret = i2c_write(dev_i2c.bus, w_addr, sizeof(w_addr), NPM1300_ADDR);
if (ret != 0) {
printk("Failed to write register address: %d\n", ret);
return false;
}
// Read the 8-bit data from the register
ret = i2c_read(dev_i2c.bus, destination, 1, NPM1300_ADDR);
if (ret != 0) {
printk("Failed to read data: %d\n", ret);
return false;
}
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define NPM1300_ADDR 0x6B // 7-bit I2C address for nPM1300
and we call in main like this:
// Read from nPM1300
uint16_t reg_addr = 0x033C; // Example register address
uint8_t reg_data;
for (;;) {
//printk("Thump!\n");
bool success = read_npm1300_register_16bit(reg_addr, ®_data);
if (success) {
printk("Read register 0x%04x from nPM1300: 0x%02x\n", reg_addr, reg_data);
} else {
printk("Failed to read register 0x%04x from nPM1300\n", reg_addr);
}
//dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
//dk_set_led(1, (++blink_status) % 2);
k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
}