{"id":609,"date":"2017-12-22T22:02:44","date_gmt":"2017-12-22T21:02:44","guid":{"rendered":"http:\/\/snakenet.eu\/blog\/?p=609"},"modified":"2017-12-22T22:03:09","modified_gmt":"2017-12-22T21:03:09","slug":"chip-battery-sh","status":"publish","type":"post","link":"https:\/\/snakenet.eu\/blog\/chip-battery-sh\/609\/","title":{"rendered":"Chip Battery.sh"},"content":{"rendered":"<pre>#!\/bin\/sh\r\n# This program gets the battery info from PMU\r\n# Voltage and current charging\/discharging\r\n#\r\n# Nota : temperature can be more than real because of self heating\r\n#######################################################################\r\n# Copyright (c) 2014 by RzBo, Bellesserre, France\r\n#\r\n# Permission is granted to use the source code within this\r\n# file in whole or in part for any use, personal or commercial,\r\n# without restriction or limitation.\r\n#\r\n# No warranties, either explicit or implied, are made as to the\r\n# suitability of this code for any purpose. Use at your own risk.\r\n#######################################################################\r\n\r\n# force ADC enable for battery voltage and current\r\n#i2cset -y -f 0 0x34 0x82 0xC3\r\ni2cset -y -f 0 0x34 0x82 0xff\r\n\r\n################################\r\n#read Power status register @00h\r\nPOWER_STATUS=$(i2cget -y -f 0 0x34 0x00)\r\n#echo $POWER_STATUS\r\n\r\nBAT_STATUS=$(($(($POWER_STATUS&amp;0x02))\/2)) # divide by 2 is like shifting rigth 1 times\r\n#echo $(($POWER_STATUS&amp;0x02))\r\necho \"BAT_STATUS=\"$BAT_STATUS\r\n# echo $BAT_STATUS\r\n\r\n################################\r\n#read Power OPERATING MODE register @01h\r\nPOWER_OP_MODE=$(i2cget -y -f 0 0x34 0x01)\r\n#echo $POWER_OP_MODE\r\n\r\nCHARG_IND=$(($(($POWER_OP_MODE&amp;0x40))\/64)) # divide by 64 is like shifting rigth 6 times\r\n#echo $(($POWER_OP_MODE&amp;0x40))\r\necho \"CHARG_IND=\"$CHARG_IND\r\n# echo $CHARG_IND\r\n\r\n[ $(($POWER_STATUS &amp; 0x80)) -ne 0 ] &amp;&amp; {\r\n echo \"ACIN Present\"\r\n ACIN_VOLT_MSB=$(i2cget -y -f 0 0x34 0x56)\r\n ACIN_VOLT_LSB=$(i2cget -y -f 0 0x34 0x57)\r\n ACIN_BIN=$(( $(($ACIN_VOLT_MSB &lt;&lt; 4)) | $(($(($ACIN_VOLT_LSB &amp; 0x0F)) )) ))\r\n ACIN_VOLT=$(echo \"($ACIN_BIN*1.7)\"|bc)\r\n echo \" ACIN voltage = \"$ACIN_VOLT\"mV\"\r\n\r\nACIN_I_MSB=$(i2cget -y -f 0 0x34 0x58)\r\n ACIN_I_LSB=$(i2cget -y -f 0 0x34 0x59)\r\n ACIN_I_BIN=$(( $(($ACIN_I_MSB &lt;&lt; 4)) | $(($(($ACIN_I_LSB &amp; 0x0F)) )) ))\r\n ACIN_I=$(echo \"($ACIN_I_BIN*0.625)\"|bc)\r\n echo \" ACIN current = \"$ACIN_I\"mA\"\r\n}\r\n[ $(($POWER_STATUS &amp; 0x20)) -ne 0 ] &amp;&amp; {\r\n echo \"VBUS Present\"\r\n VBIN_VOLT_MSB=$(i2cget -y -f 0 0x34 0x5a)\r\n VBIN_VOLT_LSB=$(i2cget -y -f 0 0x34 0x5b)\r\n VBIN_BIN=$(( $(($VBIN_VOLT_MSB &lt;&lt; 4)) | $(($(($VBIN_VOLT_LSB &amp; 0x0F)) )) ))\r\n VBIN_VOLT=$(echo \"($VBIN_BIN*1.7)\"|bc)\r\n echo \" VBUS voltage = \"$VBIN_VOLT\"mV\"\r\n\r\nVBIN_I_MSB=$(i2cget -y -f 0 0x34 0x5c)\r\n VBIN_I_LSB=$(i2cget -y -f 0 0x34 0x5d)\r\n VBIN_I_BIN=$(( $(($VBIN_I_MSB &lt;&lt; 4)) | $(($(($VBIN_I_LSB &amp; 0x0F)) )) ))\r\n VBIN_I=$(echo \"($VBIN_I_BIN*0.375)\"|bc)\r\n echo \" VBUS current = \"$VBIN_I\"mA\"\r\n}\r\n\r\n################################\r\n#read Charge control register @33h\r\nCHARGE_CTL=$(i2cget -y -f 0 0x34 0x33)\r\necho \"CHARGE_CTL=\"$CHARGE_CTL\r\n# echo $CHARGE_CTL\r\n\r\n\r\n################################\r\n#read Charge control register @34h\r\nCHARGE_CTL2=$(i2cget -y -f 0 0x34 0x34)\r\necho \"CHARGE_CTL2=\"$CHARGE_CTL2\r\n# echo $CHARGE_CTL2\r\n\r\n###################\r\n#read internal temperature 5eh, 5fh -144.7c -&gt; 000h, 0.1c\/bitFFFh -&gt; 264.8c\r\nTEMP_MSB=$(i2cget -y -f 0 0x34 0x5e)\r\nTEMP_LSB=$(i2cget -y -f 0 0x34 0x5f)\r\n\r\n# bash math -- converts hex to decimal so `bc` won't complain later...\r\n# MSB is 8 bits, LSB is lower 4 bits\r\nTEMP_BIN=$(( $(($TEMP_MSB &lt;&lt; 4)) | $(($(($TEMP_LSB &amp; 0x0F)) )) ))\r\n\r\nTEMP_C=$(echo \"($TEMP_BIN*0.1-144.7)\"|bc)\r\necho \"Internal temperature = \"$TEMP_C\"c\"\r\n\r\nBAT_EXIST=$(($(($POWER_OP_MODE&amp;0x20))\/32)) # divide by 32 is like shifting rigth 5 times\r\n#echo $(($POWER_OP_MODE&amp;0x20))\r\necho \"BAT_EXIST=\"$BAT_EXIST\r\n# echo $BAT_EXIST\r\n\r\n[ $BAT_EXIST -ne 0 ] &amp;&amp; {\r\n################################\r\n#read battery voltage 79h, 78h 0 mV -&gt; 000h, 1.1 mV\/bit FFFh -&gt; 4.5045 V\r\nBAT_VOLT_MSB=$(i2cget -y -f 0 0x34 0x78)\r\nBAT_VOLT_LSB=$(i2cget -y -f 0 0x34 0x79)\r\n\r\n#echo $BAT_VOLT_MSB $BAT_VOLT_LSB\r\n# bash math -- converts hex to decimal so `bc` won't complain later...\r\n# MSB is 8 bits, LSB is lower 4 bits\r\nBAT_BIN=$(( $(($BAT_VOLT_MSB &lt;&lt; 4)) | $(($(($BAT_VOLT_LSB &amp; 0x0F)) )) ))\r\n\r\nBAT_VOLT=$(echo \"($BAT_BIN*1.1)\"|bc)\r\necho \" Battery voltage = \"$BAT_VOLT\"mV\"\r\n\r\n\r\n###################\r\n#read Battery Discharge Current 7Ch, 7Dh 0 mV -&gt; 000h, 0.5 mA\/bit 1FFFh -&gt; 1800 mA\r\n#AXP209 datasheet is wrong, discharge current is in registers 7Ch 7Dh\r\n#13 bits\r\nBAT_IDISCHG_MSB=$(i2cget -y -f 0 0x34 0x7C)\r\nBAT_IDISCHG_LSB=$(i2cget -y -f 0 0x34 0x7D)\r\n\r\n#echo $BAT_IDISCHG_MSB $BAT_IDISCHG_LSB\r\n\r\nBAT_IDISCHG_BIN=$(( $(($BAT_IDISCHG_MSB &lt;&lt; 5)) | $(($(($BAT_IDISCHG_LSB &amp; 0x1F)) )) ))\r\n\r\nBAT_IDISCHG=$(echo \"($BAT_IDISCHG_BIN*0.5)\"|bc)\r\necho \" Battery discharge current = \"$BAT_IDISCHG\"mA\"\r\n\r\n###################\r\n#read Battery Charge Current 7Ah, 7Bh 0 mV -&gt; 000h, 0.5 mA\/bit FFFh -&gt; 1800 mA\r\n#AXP209 datasheet is wrong, charge current is in registers 7Ah 7Bh\r\n#(12 bits)\r\nBAT_ICHG_MSB=$(i2cget -y -f 0 0x34 0x7A)\r\nBAT_ICHG_LSB=$(i2cget -y -f 0 0x34 0x7B)\r\n\r\n#echo $BAT_ICHG_MSB $BAT_ICHG_LSB\r\n\r\nBAT_ICHG_BIN=$(( $(($BAT_ICHG_MSB &lt;&lt; 4)) | $(($(($BAT_ICHG_LSB &amp; 0x0F)) )) ))\r\n\r\nBAT_ICHG=$(echo \"($BAT_ICHG_BIN*0.5)\"|bc)\r\necho \" Battery charge current = \"$BAT_ICHG\"mA\"\r\n\r\nFUEL_GAUGE=$(i2cget -y -f 0 0x34 0x0b9)\r\n FUEL_GAUGE=$(($FUEL_GAUGE&amp;0x7f))\r\n echo \" Fuel Gauge=$FUEL_GAUGE%\"\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#!\/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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-609","post","type-post","status-publish","format-standard","hentry","category-non-classe"],"_links":{"self":[{"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/posts\/609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/comments?post=609"}],"version-history":[{"count":1,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/posts\/609\/revisions"}],"predecessor-version":[{"id":610,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/posts\/609\/revisions\/610"}],"wp:attachment":[{"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/media?parent=609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/categories?post=609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/tags?post=609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}