Add power supply current measuring functions
authorDan White <dan@whiteaudio.com>
Tue, 17 Jul 2012 17:28:36 +0000 (12:28 -0500)
committerDan White <dan@whiteaudio.com>
Tue, 17 Jul 2012 17:28:41 +0000 (12:28 -0500)
python-lib/mpsse-test.py

index 7f697582f5efc04aa204a48c7cd10050d1c62db1..5582585cd2fbcf205f56d45353a101bf4123409e 100755 (executable)
@@ -81,6 +81,105 @@ def bisect(fset, limits=[0.0, 1.0], dgmin=None, up='uk', down='dj'):
 
 
 
+
+
+#
+# devboard conversion constants
+#
+VREF = 2.5
+SETTLE_TIME = 250e-6
+
+# Supply current sense circuitry
+RS = {  'Vdd_ns430':    1.0,
+        'DVdd_ns430': 100.0,
+        'Vdd_digi':    50.0,
+        'AVdd_atoi':  100.0 }
+
+IPS_CH = {  'Vdd_ns430':    0,
+            'DVdd_ns430':   1,
+            'Vdd_digi':     0,
+            'AVdd_atoi':    1 }
+
+PGA = { 'Vdd_ns430':    1.0,
+        'DVdd_ns430':   1.0,
+        'Vdd_digi':     1.0,
+        'AVdd_atoi':    1.0 }
+
+GM =  { 'Vdd_ns430':    5e-6/1e-3,
+        'DVdd_ns430':   5e-6/1e-3,
+        'Vdd_digi':     5e-6/1e-3,
+        'AVdd_atoi':    5e-6/1e-3 }
+
+RG =  { 'Vdd_ns430':    10e3,
+        'DVdd_ns430':   10e3,
+        'Vdd_digi':     10e3,
+        'AVdd_atoi':    10e3 }
+
+def count2volt(c, warn=True):
+    """Properly handle 14-b data from 12b ADC value.  Average bits
+    originate from 12b samples, so max value is 0xfff0 NOT 0xfffc.
+    """
+    if warn and c >= 0xfff0:
+        print 'WARNING: clipped ADC value'
+    return VREF * (((c >> 0) / 16.0) / 2**12)
+
+def ips(name, count, warn=True):
+    return ((count2volt(count, warn) / PGA[name]) /
+            (GM[name] * RG[name] * RS[name]))
+
+# Report the resolution for reference
+print
+print 'PS current sense max / resolution:'
+for name in RS.keys():
+    imax = ips(name, 0x10000, False)
+    ires = imax/2**12
+    print 'Max current: %-10s %8.2f uA / %5.2f uA' % (
+            name, 1e6*imax, 1e6*ires)
+print
+
+def get_i_supply():
+    """Return a dict of measured supply currents."""
+    result = {}
+
+    # give 430 outputs time to settle
+    vatoi.gpo1 = 0
+    v430.gpo1 = 1
+
+    # ensure IRQ pin is an input
+    PIN_IRQ = 2
+    i2c.set_pindir(i2c.io & ~(1 << PIN_IRQ))
+
+    # ADC setup
+    adc.reset()
+    adc.triggerMode(adc.MODE_IDLE)
+    adc.channelMode(0, adc.SE)
+    adc.average(16)
+    adc.convst_spi(1)
+    for name in RS.keys():
+        if 'ns430' in name:
+            adc.channelGain(IPS_CH[name], PGA[name])
+    #now do the two conversions
+    adc.triggerMode(adc.MODE_MANUAL_MANUAL)
+
+    #get the two results
+    for name in ('Vdd_ns430', 'DVdd_ns430'):
+        adc.mux(IPS_CH[name])
+        adc.read()
+        result[name] = ips(name, adc.read())
+
+    # give atoi outputs time to settle
+    vatoi.gpo1 = 1
+    v430.gpo1 = 0
+
+    #get the two results
+    for name in ('Vdd_digi', 'AVdd_atoi'):
+        adc.mux(IPS_CH[name])
+        adc.read()
+        result[name] = ips(name, adc.read())
+
+    return result
+
+
 ##############################################################################
 # Setup FTDI serial ports
 #
@@ -140,7 +239,7 @@ order = [s0, s1, s2, s3]
 orderiter = permutations(order)
 for p in orderiter:
     break
-    print p
+    #print p
     #setup POR values each time
     vatoi.posA = 128
     vatoi.posB = 128
@@ -157,6 +256,7 @@ for p in orderiter:
 
 psdefaults()
 
+
 ##############################################################################
 # DAC
 # default mode is dac.INPUT_UPDATE_SINGLE
@@ -230,10 +330,10 @@ for h in chain.h:
         ota.gain = 15
         ota.offset = 0
 out = chain.write()
-print str2hex(out)
+#print str2hex(out)
 chain.write()
-print str2hex(out)
-print str2hex(bytes2str(chain.bytes))
+#print str2hex(out)
+#print str2hex(bytes2str(chain.bytes))
 
 mux.selA = 0
 mux.selB = 0
@@ -403,6 +503,9 @@ if 0:
             dac.vinb(vb)
             sleep(0.01)
 
+isupply = get_i_supply()
+for name, i in isupply.iteritems():
+    print '%-10s %8.2f uA' % (name, 1e6*i)
 
 ##############################################################################
 # drop into an IPython shell