From ac1eb06b4c2f090b8ab9c3a0dab262e0fe1173a4 Mon Sep 17 00:00:00 2001
From: Dan White <dan@whiteaudio.com>
Date: Sat, 27 Oct 2012 20:37:26 -0500
Subject: [PATCH] Enable usage of ft4232 suspend pin

Need to add an external circuit which switches off power to devboard when
SUSPEND pin is low.
---
 python-lib/calibrate.py          |  3 ++-
 python-lib/devboard.py           | 15 +++++++++++++++
 python-lib/enable_usb_suspend.sh | 16 ++++++++++++++++
 python-lib/test-nco.py           | 19 +++++++++++++++++++
 4 files changed, 52 insertions(+), 1 deletion(-)
 create mode 100755 python-lib/enable_usb_suspend.sh
 create mode 100644 python-lib/test-nco.py

diff --git a/python-lib/calibrate.py b/python-lib/calibrate.py
index 98e43ac..c10f955 100644
--- a/python-lib/calibrate.py
+++ b/python-lib/calibrate.py
@@ -457,6 +457,8 @@ def calibrate(name, elements=None, verbose=True):
     an element or list of the specific elements to be calibrated, leaving
     others alone.
 
+    returns: (offsets, stats)
+
     NB: The calibration routines only modify the relevant ota.offset values,
     and sets the .cal bit for the chains.  All other settings (.fast, cint,
     etc) are left un-touched.
@@ -479,7 +481,6 @@ def calibrate(name, elements=None, verbose=True):
                 x0[j,i] = chain.h[i].ota[j].offset
         offsets, stats = secant_opt(func, x0, limits,
                 elements=elements, verbose=verbose)
-
     elif name == 'mux':
         #save mux state
         old_mode = [mux.ota[i].mode for i in range(2)]
diff --git a/python-lib/devboard.py b/python-lib/devboard.py
index 709793b..0d99217 100644
--- a/python-lib/devboard.py
+++ b/python-lib/devboard.py
@@ -186,6 +186,20 @@ mux = None
 arb = None
 amux = None
 
+
+def close_usb():
+    """
+    Close the three MPSSE ports from the usb side.  If the serial port is also
+    closed, the kernel will suspend the device.  This makes the SUSPEND pin go
+    low on the usbio board and allows a circuit to cut power to the devboard.
+    """
+    print 'closing usb ports'
+    for c in (spi0.context, spi1.context, i2c.context):
+        usbio.ftdi.usb_close(c)
+    print 'sleeping...'
+    sleep(5)
+
+
 def init_devboard(name='devboard-defaults.yaml'):
     """Initialize devboard <-> usbio with a known configuration.  `name` may be
     a string filename, defaulting to 'devboard-defaults.yaml' or an integer
@@ -202,6 +216,7 @@ def init_devboard(name='devboard-defaults.yaml'):
 
     #blank the slate
     if is_initialized:
+        close_usb()
         _config = {}
         spi0 = None
         spi1 = None
diff --git a/python-lib/enable_usb_suspend.sh b/python-lib/enable_usb_suspend.sh
new file mode 100755
index 0000000..b3120e2
--- /dev/null
+++ b/python-lib/enable_usb_suspend.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+#
+# turn on autosuspend for usbio
+#
+
+for f in /sys/bus/usb/devices/* ; do
+    if [ -e $f/manufacturer ]; then
+        if [ "$(cat $f/manufacturer)" == "White Audio" ]; then
+            echo $f
+            echo "0" | sudo tee $f/power/autosuspend_delay_ms
+            echo "auto" | sudo tee $f/power/control
+        fi
+    fi
+done
+
diff --git a/python-lib/test-nco.py b/python-lib/test-nco.py
new file mode 100644
index 0000000..d1f3f28
--- /dev/null
+++ b/python-lib/test-nco.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+
+from time import sleep
+
+import devboard as dev
+dev.init_devboard(1)
+
+from calibrate import calibrate
+
+
+
+
+
+sleep(5)
+dev.init_devboard(1)
+
+
+
+
-- 
2.25.1