bug fix for REG_ID_BAT

The correct power-on sequence for obtaining the battery percentage is to first power on the PicoCalc with the battery inserted
Then connect the USB Type-C cable to read the battery percentage.
The battery percentage also can be retrieved via I2C communication within the UF2 program.

PicoCalc keyboard firmware updating required.
This commit is contained in:
cuu
2025-04-02 00:11:08 +08:00
parent 57aa179b2e
commit 65bf0c65a7
4 changed files with 62 additions and 17 deletions

View File

@@ -26,13 +26,14 @@ int read_i2c_kbd() {
retval = i2c_write_timeout_us(I2C_KBD_MOD, I2C_KBD_ADDR, msg, 1, false, 500000);
if (retval == PICO_ERROR_GENERIC || retval == PICO_ERROR_TIMEOUT) {
printf("i2c write error\n");
printf("read_i2c_kbd i2c write error\n");
return -1;
}
sleep_ms(16);
retval = i2c_read_timeout_us(I2C_KBD_MOD, I2C_KBD_ADDR, (unsigned char *) &buff, 2, false, 500000);
if (retval == PICO_ERROR_GENERIC || retval == PICO_ERROR_TIMEOUT) {
printf("i2c read error read\n");
printf("read_i2c_kbd i2c read error read\n");
return -1;
}
@@ -54,4 +55,30 @@ int read_i2c_kbd() {
return c;
}
return -1;
}
int read_battery() {
int retval;
uint16_t buff = 0;
unsigned char msg[2];
msg[0] = 0x0b;
if (i2c_inited == 0) return -1;
retval = i2c_write_timeout_us(I2C_KBD_MOD, I2C_KBD_ADDR, msg, 1, false, 500000);
if (retval == PICO_ERROR_GENERIC || retval == PICO_ERROR_TIMEOUT) {
printf("read_battery i2c write error\n");
return -1;
}
sleep_ms(16);
retval = i2c_read_timeout_us(I2C_KBD_MOD, I2C_KBD_ADDR, (unsigned char *) &buff, 2, false, 500000);
if (retval == PICO_ERROR_GENERIC || retval == PICO_ERROR_TIMEOUT) {
printf("read_battery i2c read error read\n");
return -1;
}
if (buff != 0) {
return buff;
}
return -1;
}

View File

@@ -9,11 +9,12 @@
#define I2C_KBD_SDA 6
#define I2C_KBD_SCL 7
#define I2C_KBD_SPEED 400000 // if dual i2c, then the speed of keyboard i2c should be 10khz
#define I2C_KBD_SPEED 10000 // if dual i2c, then the speed of keyboard i2c should be 10khz
#define I2C_KBD_ADDR 0x1F
void init_i2c_kbd();
int read_i2c_kbd();
int read_battery();
#endif

View File

@@ -204,10 +204,14 @@ void pwm_interrupt_handler() {
}
int main() {
stdio_init_all();
char buf[64];
set_sys_clock_khz(133000, true);
stdio_init_all();
uart_init(uart0, 115200);
uart_set_format(uart0, 8, 1, UART_PARITY_NONE); // 8-N-1
uart_set_fifo_enabled(uart0, false);
init_i2c_kbd();
lcd_init();
@@ -225,7 +229,17 @@ int main() {
psram_spi_inst_t psram_spi = psram_spi_init_clkdiv(pio1, -1,1.0f,true);
psram_test(&psram_spi);
// if we need battery information
// ** we should power on the picocalc only with batteries
// ** then plug the type c cable
// otherwise we won't get working battery infos
sleep_ms(2000);
int bat_pcnt = read_battery();
sprintf(buf,"battery percent is %d\n",bat_pcnt>>8);
printf(buf);
lcd_print_string(buf);
while (1) {
int c = lcd_getc(0);