Skip to content

reading battery status via i2c always reports 0 #20

@wez

Description

@wez

I'm not sure if I'm doing something wrong here, but I can read keyboard data and change the backlight just fine, so my i2c stuff generally works.

I have this rust function to read the battery register; it sends REG_ID_BAT and then reads back 2 bytes:

const KBD_ADDR: u8 = 0x1f;
const REG_ID_BKL: u8 = 0x05;
const REG_ID_FIF: u8 = 0x09;
const REG_ID_BK2: u8 = 0x0a;
const REG_ID_BAT: u8 = 0x0b;
const REG_WRITE: u8 = 1u8 << 7;

async fn read_battery_pct(
    i2c_bus: &mut I2c<'_, I2C1, embassy_rp::i2c::Async>,
) -> Result<u8, embassy_rp::i2c::Error> {
    let mut buf = [0u8; 2];
    i2c_bus
        .write_read_async(KBD_ADDR, [REG_ID_BAT], &mut buf)
        .await?;

    Ok(buf[1])
}

The data I get back is always [REG_ID_BAT, 0], which corresponds to:

case REG_ID_BAT:{
//Serial1.print("REG_ID_BAT getBatteryPercent:");Serial1.print(current_bat_pcnt);Serial1.println("%");
write_buffer[0] = reg;
write_buffer[1] = (uint8_t)current_bat_pcnt;

but with current_bat_pcnt set to 0.

That value always appears to be 0 for me; whether the batteries are installed, removed, charging or when the usb-c cable to the picocalc (not the pico) is attached to the computer or not.

Am I reading the schematic correctly? I think the STM MCU Serial1 is connected to UART1 (pins 8/9) on the pico, so I should be able to read the PMU debugging information by reading from that uart in my pico firmware? I capture and log that data, but I haven't seen anything get printed there so far.

Should I be doing something differently to read the battery level?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions