Jun 9, 2016

How to make a HW without a FW engineer

When I was in biotech, I had interesting experiences working with scientists--biologists in particular.  Just as I will never properly understand the issues and techniques of biology, biologists do not understand the low level SW stack--what makes the instruments they use work.  It's not a matter of intellect or willingness to study; working with the Python scripts they are willing to write and debug dispelled me of any such notion.  But the low level operation of electronic HW requires years of investments that makes it cost prohibitive for all but the most determined scientists to create a reliably working instrument.  Because the scientists are comfortable working with high level scripting language, what they would really like is to control electronics from scripts--and to some degree, they can--using HW with USB interfaces and Python interfaces (as I described how to do in a previous blog entry).  Motion controllers and cameras especially have made lots of progress in this regard.

But if you have a HW without a USB interface feature, you often had to get a FW engineer to control it from a micro-controller.  But if you are a scientist with some ability to work in the .NET environment, you can still regain control with a USB-to-SPI or USB-to-I2C bridge.  For example, MCP2210 shows up as an HID USB device, and you can control it by issuing USB HID "Reports" in the format described in the MCP2210 datasheet.  For example, let's say you need to issue 0x7B7FFF80 to an IC (e.g. humidity sensor).  On the wire, the SPI pins should show this kind of waveform:

With freely downloadable SPI Terminal example application from Microchip (below), you can control the majority of aspects of SPI communication (MCP2210 start to data, data to stop, and byte to byte delays seem rather large and I could not bring it down to lower than shown above).
Microchip even give you a command-line application (CLI), through which you can drive an SPI enabled IC from a Python script with subprocess.call() API.

You can use the MCP2210DLL-M.dll assembly from any .NET language.  The above example app, for example, is written in Visual Basic.  Observe (part of) the code that run when you click the "Transfer SPI Data" button:

iResult = UsbSpi.Functions.TxferSpiData(TxData, RxData)

This API exposes the fact that a master and slave exchanges data in an SPI transaction.  But if you are willing to read the HW datasheet and map it to what your program needs to send and receive, you can control your HW directly from a high level script.

IronPython project to control the MCP2210

On the .NET platform, IronPython is a proven method to access .NET assemblies.  After installing the latest stable IronPython (2.7.4), I also installed the VSTP (Visual Studio Tools for Python) version 2.2.2.  It let me create an "IronPython Application" project (I called it Test1), which is then pre-populated with Test1.py.  VSTP eases development pain; I could set breakpoints right away and examine all variables.  When I used python in a hosted IronPython environment (a WPF app hosts the Python shell), debugging was the biggest pain.  Debugging a python shell script was easy; debugging other IronPython project types (WPF GUI for example) is probably also pleasant.

I copied the MCP2210 DLL into the project folder, and Solution Explorer window --> right-clicked on Test1 project --> Add reference --> Browse, to have Visual Studio auto-discover the DLL just copied.
Then you can see the top level namespace of the assembly.

This script just checks the connected status of the device

import sys, clr
clr.AddReference("MCP2210DLL-M")
import MCP2210 # from MCP2210 import DevIO as UsbSpi
   
def main():
    print("MCP2210 test ######################")
    #print(dir(UsbSpi))

    VID = 0x4D8; PID = 0xDE  # MCP2210 USB ID
    UsbSpi = MCP2210.DevIO(VID, PID)
    connected = UsbSpi.Settings.GetConnectionStatus()
    print("Connected = %d" % (connected,))
    return 0

if __name__ == "__main__":
    sys.exit(main())

No comments:

Post a Comment