Pyhon script to dump all accessible data from i2c

Various Python Tools that helps handling of the UPS PIco

Pyhon script to dump all accessible data from i2c

Postby znibb » Fri Oct 16, 2015 1:03 am

Hey all

I put together this very brute force script to dump all available data from the i2c-accessible registers into the terminal for easy troubleshooting. It only requires that you set up your i2c-bus properly, no other dependencies (other than Python itself ofc). It will only work for a 512MB Raspberry Pi Model B and later boards.

I hope it can be of use for someone:

Code: Select all
#! /usr/bin/python

# pico_register_readout.py
# Written by Pontus Petersson (pontus.pson@gmail.com)
# Version 1: 2015-10-16

import commands

# RTC data
year =   commands.getstatusoutput("sudo i2cget -y 1 0x6A 6")
month =   commands.getstatusoutput("sudo i2cget -y 1 0x6A 5")
day =    commands.getstatusoutput("sudo i2cget -y 1 0x6A 4")
dow =   commands.getstatusoutput("sudo i2cget -y 1 0x6A 3")
hour =    commands.getstatusoutput("sudo i2cget -y 1 0x6A 2")
min =    commands.getstatusoutput("sudo i2cget -y 1 0x6A 1")
sec =    commands.getstatusoutput("sudo i2cget -y 1 0x6A 0")
ccf =    commands.getstatusoutput("sudo i2cget -y 1 0x6A 7")

days_of_the_week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

YEAR =   "20" + year[1][2:]
MONTH = month[1][2:]
DAY =    day[1][2:]
DOW =    days_of_the_week[int(dow[1][3:])-1]
HOUR =    hour[1][2:]
MIN =    min[1][2:]
SEC =   sec[1][2:]
CCF =   ccf[1][2:]

print("--- RTC data ---")
print(YEAR+"-"+MONTH+"-"+DAY+" "+DOW+" "+HOUR+":"+MIN+":"+SEC+" /"+CCF+"\n")

# Status registers
mode =      commands.getstatusoutput("sudo i2cget -y 1 0x69 0")
batlevel_1 =    commands.getstatusoutput("sudo i2cget -y 1 0x69 2")
batlevel_2 =    commands.getstatusoutput("sudo i2cget -y 1 0x69 1")
rpilevel_1 =    commands.getstatusoutput("sudo i2cget -y 1 0x69 4")
rpilevel_2 =    commands.getstatusoutput("sudo i2cget -y 1 0x69 3")
tmpcels =    commands.getstatusoutput("sudo i2cget -y 1 0x69 12")

powering_modes = ['RPi', 'Bat']

MODE =      powering_modes[int(mode[1][2:])-1]
BATLEVEL =   batlevel_1[1][3:]+"."+batlevel_2[1][2:]+" V"
RPILEVEL =   rpilevel_1[1][3:]+"."+rpilevel_2[1][2:]+" V"
TMPCELS =   tmpcels[1][2:]+" "+unichr(176)+"C"

print("--- Status registers ---   ")
print("Mode\t\tBat_lvl\t\tRPi_lvl\t\tTemp")
print(MODE+"\t\t"+BATLEVEL+"\t\t"+RPILEVEL+"\t\t"+TMPCELS+"\n")

# Module commands
version =    commands.getstatusoutput("sudo i2cget -y 1 0x6B 0")
error_code =    commands.getstatusoutput("sudo i2cget -y 1 0x6B 1")
rpi_serror_1 =    commands.getstatusoutput("sudo i2cget -y 1 0x6B 3")
rpi_serror_2 =    commands.getstatusoutput("sudo i2cget -y 1 0x6B 2")
bat_serror_1 =    commands.getstatusoutput("sudo i2cget -y 1 0x6B 5")
bat_serror_2 =    commands.getstatusoutput("sudo i2cget -y 1 0x6B 4")
tmp_serror_1 =    commands.getstatusoutput("sudo i2cget -y 1 0x6B 7")
tmp_serror_2 =    commands.getstatusoutput("sudo i2cget -y 1 0x6B 6")
sta_counter =    commands.getstatusoutput("sudo i2cget -y 1 0x6B 8")
fssd_batime =   commands.getstatusoutput("sudo i2cget -y 1 0x6B 9")
lprsta =    commands.getstatusoutput("sudo i2cget -y 1 0x6B 10")
btto =      commands.getstatusoutput("sudo i2cget -y 1 0x6B 11")
led_blue =   commands.getstatusoutput("sudo i2cget -y 1 0x6B 12")
led_red =   commands.getstatusoutput("sudo i2cget -y 1 0x6B 13")
buzmode =   commands.getstatusoutput("sudo i2cget -y 1 0x6B 14")
fanmode =   commands.getstatusoutput("sudo i2cget -y 1 0x6B 15")
fanspeed =   commands.getstatusoutput("sudo i2cget -y 1 0x6B 16")
xbmc =      commands.getstatusoutput("sudo i2cget -y 1 0x6B 23")
fssd_tout =   commands.getstatusoutput("sudo i2cget -y 1 0x6B 24")

status =   ['OFF', 'ON']
buz_fan_modes =   ['Disabled', 'Enabled', 'Automatic']
fan_speeds =   ['0', '100', '25', '50', '75']

VERSION =    version[1][2:]
ERROR_CODE =   '{0:08b}'.format(int(error_code[1][2:]))
RPI_SERROR =   rpi_serror_1[1][3:]+"."+rpi_serror_2[1][2:]+" V"
BAT_SERROR =   bat_serror_1[1][3:]+"."+bat_serror_2[1][2:]+" V"
TMP_SERROR =   tmp_serror_1[1][2:]+"."+tmp_serror_2[1][2:]+" "+unichr(176)+"C"
STA_COUNTER =   int(sta_counter[1], 16)
FSSD_BATIME =   int(fssd_batime[1], 16)
LPRSTA =   int(lprsta[1], 16)
BTTO =      int(btto[1], 16)
LED_BLUE =   status[int(led_blue[1][3:])]
LED_RED =   status[int(led_red[1][3:])]
BUZMODE =   buz_fan_modes[int(buzmode[1][3:])]
FANMODE =   buz_fan_modes[int(fanmode[1][3:])]
FANSPEED =   fan_speeds[int(fanspeed[1][3:])]
XBMC =      status[int(xbmc[1][3:])]
FSSD_TOUT =   int(fssd_tout[1], 16)

print("--- Status commands ---")
print("Firmware: %s" % VERSION)
print("Error code: %s" % ERROR_CODE)
print("rpi_serror: %s" % RPI_SERROR)
print("bat_serror: %s" % BAT_SERROR)
print("tmp_serror: %s" % TMP_SERROR)
print("Still Alive Timeout Counter: %d s (255=disabled)" % STA_COUNTER)
print("Battery Running Time: %d s (255=disabled)" % FSSD_BATIME)
print("Low Power Restart Time: %d s" % LPRSTA)
print("Battery Powering Testing Timeout: %d s" % BTTO)
print("led_blue: %s" % LED_BLUE)
print("led_red: %s" % LED_RED)
print("Integrated Buzzer Mode: %s" % BUZMODE)
print("Integrated Fan Mode: %s" % FANMODE)
print("Integrated Fan Speed: %s %%" % FANSPEED)
print("XBMC Mode: %s" % XBMC)
print("FSSD Timeout: %d s" % FSSD_TOUT)


Example output:
Code: Select all
--- RTC data ---
2000-01-01 Mon 04:04:31 /00

--- Status registers ---   
Mode      Bat_lvl      RPi_lvl      Temp
RPi      3.74 V      4.88 V      35 °C

--- Status commands ---
Firmware: 38
Error code: 00000000
rpi_serror: 0.00 V
bat_serror: 0.00 V
tmp_serror: 00.00 °C
Still Alive Timeout Counter: 255 s (255=disabled)
Battery Running Time: 255 s (255=disabled)
Low Power Restart Time: 5 s
Battery Powering Testing Timeout: 3 s
led_blue: OFF
led_red: OFF
Integrated Buzzer Mode: Automatic
Integrated Fan Mode: Disabled
Integrated Fan Speed: 0 %
XBMC Mode: OFF
FSSD Timeout: 32 s


If you have improvement suggestions feel free to email me.
Ciao!
znibb
 
Posts: 1
Joined: Fri Oct 16, 2015 12:35 am

Re: Pyhon script to dump all accessible data from i2c

Postby muckleyj » Mon Nov 09, 2015 11:02 pm

Thanks, I've been thinking of making a script for this but now I don't he to. :D
muckleyj
 
Posts: 5
Joined: Tue Nov 03, 2015 12:15 pm

Re: Pyhon script to dump all accessible data from i2c

Postby alazarid » Mon Apr 11, 2016 5:56 pm

thanks for the script I tried to run it with python ./pico.py (the name that I gave to the script) but it gives me an error
import commands
^
IndentationError: unexpected indent

Am I missing something !!!!

Thanks in advance

Aris
alazarid
 
Posts: 1
Joined: Mon Apr 11, 2016 4:08 pm

Re: Pyhon script to dump all accessible data from i2c

Postby SlySven » Wed Jul 06, 2016 9:07 pm

Looking at the script I see two possible things that might be an issue for you alazarid:
  • according to a quick "DuckDuckGo" (like a "Google" but it doesn't track your searching habits! ;) ) the Python documentation suggests things have changed with the use of "import" in Python 3.3 - I dunno what you are using but perhaps it is too old (or new)?
  • python is a spacing sensitive language and it is important to indent each line to the correct amount so that it lines up as given in a source listing - or so I've found (and I am a python novice!) - as the "import" command as quoted is the very first line with actual code in it - does it start in the first character position on that line? It might be worth checking what version of python you are using (try python --version) and that there IS something called "python" (or a symbolic link with that name that links something that looks like it might understand python scripts) in the "/usr/bin" directory! You may find it linked to an executable file with a name beginning with python and a trailing number 2.something or 3.something which will likely be version 2.x or 3.x (where x is the "minor" version number) python interpreters respectively.
SlySven
 
Posts: 19
Joined: Tue Feb 23, 2016 4:55 am


Return to PIco Python and other Tools

Who is online

Users browsing this forum: No registered users and 1 guest