[Banana Pi] AXP209 battery power monitoring

The AXP209 chip present on the Banana Pi is managing the board powering. The free pads just behind the power micro-USB allows for a 3.7v li-po / li-ion battery to be installed.

20141124_212653

2400 mAh tablet battery and 2 pin header

So we need some monitoring tools.

First, install bc arbitrary precision numeric processing language :

sudo apt-get install bc

Now there are two scripts for monitoring AXP209 :

battery_info.sh
#!/bin/sh
# This program gets the battery info from PMU
# Voltage and current charging/discharging
#
# Nota : temperature can be more than real because of self heating
#######################################################################
# Copyright (c) 2014 by RzBo, Bellesserre, France
#
# Permission is granted to use the source code within this
# file in whole or in part for any use, personal or commercial,
# without restriction or limitation.
#
# No warranties, either explicit or implied, are made as to the
# suitability of this code for any purpose. Use at your own risk.
#######################################################################

# force ADC enable for battery voltage and current
i2cset -y -f 0 0x34 0x82 0xC3

################################
#read Power status register @00h
POWER_STATUS=$(i2cget -y -f 0 0x34 0x00)
#echo $POWER_STATUS

BAT_STATUS=$(($(($POWER_STATUS&0x02))/2))  # divide by 2 is like shifting rigth 1 times
#echo $(($POWER_STATUS&0x02))
echo « BAT_STATUS= »$BAT_STATUS
# echo $BAT_STATUS

################################
#read Power OPERATING MODE register @01h
POWER_OP_MODE=$(i2cget -y -f 0 0x34 0x01)
#echo $POWER_OP_MODE

CHARG_IND=$(($(($POWER_OP_MODE&0x40))/64))  # divide by 64 is like shifting rigth 6 times
#echo $(($POWER_OP_MODE&0x40))
echo « CHARG_IND= »$CHARG_IND
# echo $CHARG_IND

BAT_EXIST=$(($(($POWER_OP_MODE&0x20))/32))  # divide by 32 is like shifting rigth 5 times
#echo $(($POWER_OP_MODE&0x20))
echo « BAT_EXIST= »$BAT_EXIST
# echo $BAT_EXIST

################################
#read Charge control register @33h
CHARGE_CTL=$(i2cget -y -f 0 0x34 0x33)
echo « CHARGE_CTL= »$CHARGE_CTL
# echo $CHARGE_CTL

################################
#read Charge control register @34h
CHARGE_CTL2=$(i2cget -y -f 0 0x34 0x34)
echo « CHARGE_CTL2= »$CHARGE_CTL2
# echo $CHARGE_CTL2

################################
#read battery voltage    79h, 78h    0 mV -> 000h,    1.1 mV/bit    FFFh -> 4.5045 V
BAT_VOLT_LSB=$(i2cget -y -f 0 0x34 0x79)
BAT_VOLT_MSB=$(i2cget -y -f 0 0x34 0x78)

#echo $BAT_VOLT_MSB $BAT_VOLT_LSB

BAT_BIN=$(( $(($BAT_VOLT_MSB << 4)) | $(($(($BAT_VOLT_LSB & 0xF0)) >> 4)) ))

BAT_VOLT=$(echo « ($BAT_BIN*1.1) »|bc)
echo « Battery voltage = « $BAT_VOLT »mV »

###################
#read Battery Discharge Current    7Ah, 7Bh    0 mV -> 000h,    0.5 mA/bit    FFFh -> 4.095 V
BAT_IDISCHG_LSB=$(i2cget -y -f 0 0x34 0x7B)
BAT_IDISCHG_MSB=$(i2cget -y -f 0 0x34 0x7A)

#echo $BAT_IDISCHG_MSB $BAT_IDISCHG_LSB

BAT_IDISCHG_BIN=$(( $(($BAT_IDISCHG_MSB << 4)) | $(($(($BAT_IDISCHG_LSB & 0xF0)) >> 4)) ))

BAT_IDISCHG=$(echo « ($BAT_IDISCHG_BIN*0.5) »|bc)
echo « Battery discharge current = « $BAT_IDISCHG »mA »

###################
#read Battery Charge Current    7Ch, 7Dh    0 mV -> 000h,    0.5 mA/bit    FFFh -> 4.095 V
BAT_ICHG_LSB=$(i2cget -y -f 0 0x34 0x7D)
BAT_ICHG_MSB=$(i2cget -y -f 0 0x34 0x7C)

#echo $BAT_ICHG_MSB $BAT_ICHG_LSB

BAT_ICHG_BIN=$(( $(($BAT_ICHG_MSB << 4)) | $(($(($BAT_ICHG_LSB & 0xF0)) >> 4)) ))

BAT_ICHG=$(echo « ($BAT_ICHG_BIN*0.5) »|bc)
echo « Battery charge current = « $BAT_ICHG »mA »

power_status.sh
#!/bin/sh
# This program gets the power status (AC IN or BAT)
# I2C interface with AXP209
#
#######################################################################
# Copyright (c) 2014 by RzBo, Bellesserre, France
#
# Permission is granted to use the source code within this
# file in whole or in part for any use, personal or commercial,
# without restriction or limitation.
#
# No warranties, either explicit or implied, are made as to the
# suitability of this code for any purpose. Use at your own risk.
#######################################################################

#read Power status register @00h
POWER_STATUS=$(i2cget -y -f 0 0x34 0x00)
#echo $POWER_STATUS

# bit 7 : Indicates ACIN presence 0: ACIN does not exist; 1: ACIN present
#echo « bit 7 : Indicates ACIN presence 0: ACIN does not exist; 1: ACIN present »
#echo « bit 2 : Indicates that the battery current direction 0: battery discharge; 1: The battery is charged »

AC_STATUS=$(($(($POWER_STATUS&0x80))/128))  # divide by 128 is like shifting rigth 8 times
#echo $(($POWER_STATUS&0x80))
echo « AC_STATUS= »$AC_STATUS
# echo $AC_STATUS

Examples :

On AC, not charging :

AC_STATUS=1

BAT_STATUS=0
CHARG_IND=0
BAT_EXIST=1
CHARGE_CTL=0xd0
CHARGE_CTL2=0x47
Battery voltage = 4176.7mV
Battery discharge current = 0mA
Battery charge current = 0mA

On AC, charging :

AC_STATUS=1

BAT_STATUS=0
CHARG_IND=1
BAT_EXIST=1
CHARGE_CTL=0xd0
CHARGE_CTL2=0x47
Battery voltage = 4188.8mV
Battery discharge current = 200.0mA
Battery charge current = 0mA

On battery :

AC_STATUS=0

BAT_STATUS=0
CHARG_IND=0
BAT_EXIST=1
CHARGE_CTL=0xd0
CHARGE_CTL2=0x47
Battery voltage = 4030.4mV
Battery discharge current = 0mA
Battery charge current = 160.0mA

Taken from http://pcduino.com/forum/index.php?topic=4762.0

About Captain Stouf

Spécialiste en systèmes informatiques, Développeur matériel et logiciel, Inventeur, Maker : électronique, Systems on Chip, micro-controlleurs, Internet of Things, Modélisation / Scan / Impression 3D, Imagerie...

10 thoughts on “[Banana Pi] AXP209 battery power monitoring

    1. Fabio
      23/01/2015 at 18 h 43 min

      Hi, very interesting article! Can you tell me exactly what type of battery can be connected to the banana pi (I have a banana pros lemaker)? Thank you
      Fabio

      1. Captain Stouf
        02/02/2015 at 15 h 17 min

        Hi,
        I’m currently using a 3.7v, 2400mAh Lipo battery salvaged from a lowcost tablet. It should also work with 3.7v Li-ion batteries without modification.
        The AXP209 registers could also be adjusted for various batteries types, voltage and amperages. See details on

  1. 25/12/2014 at 3 h 49 min

    Thanks for this!

    It might be really obvious, but to get this working I had to install the i2c tools with this command:

    apt-get install i2c-tools

    I also had to change each « and » in the above code to a plain  » (Thanks, WordPress…).

    I’m thought I’d leave this here in case it helps someone else!

    1. 25/12/2014 at 3 h 52 min

      … that is, I had to change them to a plain double-quotation mark.

      None of this <> business, just a $quot;

      1. 25/12/2014 at 3 h 57 min

        I am officially giving up on trying to type the symbol I’m talking about! Je suis désolée!

  2. P.
    15/01/2015 at 19 h 08 min

    On the banana you could use : cat /sys/class/power_supply/battery/uevent. It’s easier … Isn’t it ?

    1. Captain Stouf
      02/02/2015 at 15 h 19 min

      Hi,
      You’re right, there are easier methods to read the AXP209 status, including reading a few files. I will update the blog article when I have a new script to share.
      Thank you

  3. ja z domu
    22/02/2015 at 18 h 49 min

    Battery charge and discharge current switched when on battery it shown battery charge current 😉

Laisser un commentaire