blob: 3e5cb17d9b161abda3ad243ecc9eac668df15448 [file] [log] [blame]
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Helper script to generate system control files for INA219 adcs."""
import imp
import os
import sys
import time
def dump_adcs(adcs):
"""Dump xml formatted INA219 adcs for servod.
module_name: name of python module file to load that contains ina array
specification. Each array element is a tuple consisting of:
slv: int representing the i2c slave address
name: string name of the power rail
sense: float of sense resitor size in ohms
nom: float of nominal voltage of power rail.
mux: string name of i2c mux leg these ADC's live on
is_calib: boolean to determine if calibration is possible for this rail
string (large) of xml for the system config of these ADCs to eventually be
parsed by servod daemon ( servo/ )
regs = ['cfg', 'shv', 'busv', 'pwr', 'cur', 'cal']
rsp = ""
for (slv, name, nom, sense, mux, is_calib) in adcs:
rsp += (
'<doc>Voltage of %(name)s rail in millivolts on i2c_mux:%(mux)s</doc>\n'
'<params interface="2" drv="ina219" slv="%(slv)s"'
' mux="%(mux)s" rsense="%(sense)s" type="get" subtype="millivolts"'
' nom="%(nom)s">\n</params></control>\n'
) % {'name':name, 'slv':slv, 'mux':mux, 'sense':sense, 'nom':nom}
# in some instances we may not know sense resistor size ( re-work ) or other
# custom factors may not allow for calibration and those reliable readings
# on the current and power registers. This boolean determines which
# controls should be enumerated based on rails input specification
if is_calib:
rsp += (
'<doc>Current of %(name)s rail in milliamps on i2c_mux:%(mux)s</doc>\n'
'<params interface="2" drv="ina219" slv="%(slv)s"'
'rsense="%(sense)s" type="get" subtype="milliamps">\n'
'<doc>Power of %(name)s rail in milliwatts on i2c_mux:%(mux)s</doc>\n'
'<params interface="2" drv="ina219" slv="%(slv)s"'
' mux="%(mux)s" rsense="%(sense)s" type="get" subtype="milliwatts">\n'
'</params></control>\n') % {'name':name, 'slv':slv, 'mux':mux,
'sense':sense, 'nom':nom}
for i, reg in enumerate(regs):
rsp += (
'<doc>Raw register value of %(reg)s on i2c_mux:%(mux)s</doc>'
'<params cmd="get" interface="2" drv="ina219" slv="%(slv)s"'
' subtype="readreg" reg="%(i)s" mux="%(mux)s"'
' fmt="hex">\n</params>') % {'name':name, 'slv':slv,
'mux':mux, 'sense':sense,
'reg':reg, 'i':i}
if reg == "cfg":
rsp += (
'<params cmd="set" interface="2" drv="ina219" slv="%(slv)s"'
' subtype="writereg" reg="%(i)s" mux="%(mux)s"'
' fmt="hex">\n</params></control>') % {'slv':slv, 'mux':mux,
'sense':sense, 'reg':reg,
rsp += ('</control>')
return rsp
def main():
if len(sys.argv) != 2:
raise Exception("Missing arg with name of py file w/ inas list declared")
module_name = sys.argv[1]
ina_pkg = imp.load_module(module_name, *imp.find_module(module_name))
f = open('%s.xml' % module_name, 'w')
f.write("<?xml version=\"1.0\"?>\n<root>\n")
f.write("<!-- Autogenerated on "+time.asctime()+"-->\n")
except AttributeError:
except Exception:
rv = os.system("tidy -quiet -mi -xml %s.xml" % module_name)
if rv:
print "Error tidying xml output"
# be sure to run 'tidy -mi -xml <file>
if __name__ == "__main__":