Skip to content

reading battery status via i2c always reports 0 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wez opened this issue Apr 17, 2025 · 7 comments
Open

reading battery status via i2c always reports 0 #20

wez opened this issue Apr 17, 2025 · 7 comments

Comments

@wez
Copy link
Contributor

wez commented Apr 17, 2025

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?

@wez
Copy link
Contributor Author

wez commented Apr 17, 2025

Compiling and flashing the current version of the master branch fixed the reported battery level for me

@prokrypt
Copy link
Contributor

Yep. Was fixed with 65bf0c6

@wez
Copy link
Contributor Author

wez commented Apr 18, 2025

I found that I also needed #22 to read the charging status, too

@cuu
Copy link
Contributor

cuu commented Apr 19, 2025

#22 did not fix all but helped me to realized that current_bat_pcnt is not a good idea

when I try to fix in 65bf0c6 I totally forgot the reg of REG_ID_BAT

current battery percent can be stored in reg of REG_ID_BAT

So I will remove current_bat_pcnt from code later and add bitSet(percent,7) to indicate charging status just like @wez said

@cuu
Copy link
Contributor

cuu commented Apr 21, 2025

@wez

a1847ee

removed current_bat_pcnt now

I've updated picocalc_helloworld ,added new battery info code with charging status
in 3b84959

I also find out that, when I press the battery into socket for the first time, there is a slight time that axp2101 chip will report battery percent to be zero ,that causes low_bat() to do PMU.shutdown();

So I add a low_bat_count to debounce to avoid

@wez
Copy link
Contributor Author

wez commented Apr 21, 2025

@cuu the code looks good! Regarding #22, I think the issue with the report_bat function still exists. Should I rebase that PR and re-submit, or do you have other plans for this?

@cuu
Copy link
Contributor

cuu commented Apr 21, 2025

@wez sorry I forgot the report_bat issue, now I commented out all report_bat calls, but allow me to leave the code

295f7e7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants