+++ /dev/null
-
-// The following ifdef block is the standard way of creating macros which make exporting
-// from a DLL simpler. All files within this DLL are compiled with the CP210xRUNTIMEDLL_EXPORTS
-// symbol defined on the command line. this symbol should not be defined on any project
-// that uses this DLL. This way any other project whose source files include this file see
-// CP210xRUNTIMEDLL_API functions as being imported from a DLL, wheras this DLL sees symbols
-// defined with this macro as being exported.
-#ifdef CP210xRUNTIMEDLL_EXPORTS
-#define CP210xRUNTIMEDLL_API __declspec(dllexport)
-#else
-#define CP210xRUNTIMEDLL_API __declspec(dllimport)
-#endif
-
-#define CP210x_MAX_SETUP_LENGTH 65536
-
-#ifndef _CP210x_STANDARD_DEF_
-#define _CP210x_STANDARD_DEF_
-// GetDeviceVersion() return codes
-#define CP210x_CP2101_VERSION 0x01
-#define CP210x_CP2102_VERSION 0x02
-#define CP210x_CP2103_VERSION 0x03
-
-// Return codes
-#define CP210x_SUCCESS 0x00
-#define CP210x_DEVICE_NOT_FOUND 0xFF
-#define CP210x_INVALID_HANDLE 0x01
-#define CP210x_INVALID_PARAMETER 0x02
-#define CP210x_DEVICE_IO_FAILED 0x03
-#define CP210x_FUNCTION_NOT_SUPPORTED 0x04
-#define CP210x_GLOBAL_DATA_ERROR 0x05
-#define CP210x_COMMAND_FAILED 0x08
-#define CP210x_INVALID_ACCESS_TYPE 0x09
-
-// Type definitions
-typedef int CP210x_STATUS;
-#endif /*_CP210x_STANDARD_DEF_*/
-
-// Mask and Latch value bit definitions
-#define CP210x_GPIO_0 0x01
-#define CP210x_GPIO_1 0x02
-#define CP210x_GPIO_2 0x04
-#define CP210x_GPIO_3 0x08
-
-CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI
-CP210xRT_ReadLatch( HANDLE cyHandle,
- LPBYTE lpbLatch);
-
-CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI
-CP210xRT_WriteLatch( HANDLE cyHandle,
- BYTE bMask,
- BYTE bLatch);
-
-
-CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI
-CP210xRT_GetPartNumber( HANDLE cyHandle,
- LPBYTE lpbPartNum);
+++ /dev/null
-/*
- * Copyright (c) 2010, Titanium Mirror, Inc.
- * All rights reserved.
- * Based upon work Copyright (c) 2006-2007 by Sporian Microsystems, Inc.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * - Neither the name of the Technische Universität Berlin nor the names
- * of its contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * @author R. Steve McKown <rsmckown@gmail.com>
- */
-
-#include <Python.h>
-#include <windows.h>
-#include <io.h>
-#include <dlfcn.h>
-#include "CP210xRuntimeDLL.h"
-
-typedef CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI (*fn_t)(HANDLE, BYTE, BYTE);
-
-void* dll;
-fn_t writeLatch;
-PyObject* exception;
-
-
-static PyObject* cp210xrt_writeLatch(PyObject* self, PyObject* args)
-{
- int fd;
- BYTE bMask;
- BYTE bData;
- HANDLE hDevice;
- CP210x_STATUS ret;
-
- if (!PyArg_ParseTuple(args, "iBB", &fd, &bMask, &bData))
- return NULL;
- if (!writeLatch) {
- PyErr_SetString(exception, "No access to CP210xRuntime.dll");
- return NULL;
- }
-
- hDevice = (HANDLE)get_osfhandle(fd);
- if (hDevice == INVALID_HANDLE_VALUE)
- {
- PyErr_SetString(exception, "No access to device");
- return NULL;
- }
-
- /* If we don't call writeLatch, then we don't core dump when we return to
- * the python caller. If we do call writeLatch, the latches are set, but
- * when we return return into python, perhaps the stack has been corrupted
- * somehow. We get a stackdump from cygwin.
- *
- * Note: when we opened the handle here, via CreateFile(), then closed it
- * before returning, we had no problems.
- */
- ret = writeLatch(hDevice, bMask, bData);
- if (ret != CP210x_SUCCESS) {
- PyErr_SetString(exception, "IO Error with device");
- return NULL;
- }
-
-#if 1
- Py_INCREF(Py_None);
- return Py_None;
-#else
- return Py_BuildValue("i", ret);
-#endif
-}
-
-
-static PyMethodDef cp210xrtMethods[] = {
- { "writeLatch", cp210xrt_writeLatch, METH_VARARGS, "Set GPIO bits" },
- { NULL, NULL, 0, NULL }
-};
-
-
-PyMODINIT_FUNC initcp210xrt(void)
-{
- PyObject* m = Py_InitModule("cp210xrt", cp210xrtMethods);
- exception = PyErr_NewException("cp210xrt.error", NULL, NULL);
- Py_INCREF(exception);
- PyModule_AddObject(m, "error", exception);
-
- if (!dll) {
- dll = dlopen("CP210xRuntime.dll", RTLD_NOW);
- if (dll) {
- dlerror();
- writeLatch = (fn_t)dlsym(dll, "CP210xRT_WriteLatch");
- if (dlerror())
- writeLatch = NULL;
- }
- }
-}
-
+++ /dev/null
-#!/usr/bin/python
-
-from distutils.core import setup, Extension
-
-module1 = Extension('cp210xrt', sources = ['cp210xrtmodule.c'])
-
-setup(name = 'cp210xrt', version = '0.1',
- description = 'Interface to cp210x runtime dll',
- ext_modules = [module1])
-
+++ /dev/null
-#!/usr/bin/python
-
-import sys, os, time, serial, cp210xrt;
-
-sys.stderr.write("os.name is %s\n" % os.name);
-sys.stderr.write("sys.platform is %s\n" % sys.platform);
-
-serialport = serial.Serial(9, 9600, parity = serial.PARITY_EVEN,
- timeout = 1000)
-sys.stderr.write("using serial port %r\n" % serialport.portstr)
-sys.stderr.write("serial port fd is %d\n" % serialport.fd)
-#sys.stderr.write("serial port HANDLE is %d\n" % _get_osfhandle(serialport.fd))
-
-
-sys.stderr.write("clear bit...\n")
-cp210xrt.writeLatch(serialport.fd, 0x04, 0x0)
-sys.stderr.write("clear bit done\n")
-time.sleep(0.250)
-sys.stderr.write("set bit...\n")
-cp210xrt.writeLatch(serialport.fd, 0x04, 0x04)
-
--- /dev/null
+
+// The following ifdef block is the standard way of creating macros which make exporting
+// from a DLL simpler. All files within this DLL are compiled with the CP210xRUNTIMEDLL_EXPORTS
+// symbol defined on the command line. this symbol should not be defined on any project
+// that uses this DLL. This way any other project whose source files include this file see
+// CP210xRUNTIMEDLL_API functions as being imported from a DLL, wheras this DLL sees symbols
+// defined with this macro as being exported.
+#ifdef CP210xRUNTIMEDLL_EXPORTS
+#define CP210xRUNTIMEDLL_API __declspec(dllexport)
+#else
+#define CP210xRUNTIMEDLL_API __declspec(dllimport)
+#endif
+
+#ifndef _CP210x_STANDARD_DEF_
+#define _CP210x_STANDARD_DEF_
+
+// GetDeviceVersion() return codes
+#define CP210x_CP2101_VERSION 0x01
+#define CP210x_CP2102_VERSION 0x02
+#define CP210x_CP2103_VERSION 0x03
+#define CP210x_CP2104_VERSION 0x04
+#define CP210x_CP2105_VERSION 0x05
+#define CP210x_CP2108_VERSION 0x08
+
+// Return codes
+#define CP210x_SUCCESS 0x00
+#define CP210x_DEVICE_NOT_FOUND 0xFF
+#define CP210x_INVALID_HANDLE 0x01
+#define CP210x_INVALID_PARAMETER 0x02
+#define CP210x_DEVICE_IO_FAILED 0x03
+#define CP210x_FUNCTION_NOT_SUPPORTED 0x04
+#define CP210x_GLOBAL_DATA_ERROR 0x05
+#define CP210x_FILE_ERROR 0x06
+#define CP210x_COMMAND_FAILED 0x08
+#define CP210x_INVALID_ACCESS_TYPE 0x09
+
+// Type definitions
+typedef int CP210x_STATUS;
+
+// Buffer size limits
+#define CP210x_MAX_PRODUCT_STRLEN 126
+#define CP210x_MAX_SERIAL_STRLEN 63
+
+// Type definitions
+typedef char CP210x_PRODUCT_STRING[CP210x_MAX_PRODUCT_STRLEN];
+typedef char CP210x_SERIAL_STRING[CP210x_MAX_SERIAL_STRLEN];
+
+#endif //_CP210x_STANDARD_DEF
+
+// Mask and Latch value bit definitions
+#define CP210x_GPIO_0 0x0001
+#define CP210x_GPIO_1 0x0002
+#define CP210x_GPIO_2 0x0004
+#define CP210x_GPIO_3 0x0008
+#define CP210x_GPIO_4 0x0010
+#define CP210x_GPIO_5 0x0020
+#define CP210x_GPIO_6 0x0040
+#define CP210x_GPIO_7 0x0080
+#define CP210x_GPIO_8 0x0100
+#define CP210x_GPIO_9 0x0200
+#define CP210x_GPIO_10 0x0400
+#define CP210x_GPIO_11 0x0800
+#define CP210x_GPIO_12 0x1000
+#define CP210x_GPIO_13 0x2000
+#define CP210x_GPIO_14 0x4000
+#define CP210x_GPIO_15 0x8000
+
+CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI
+CP210xRT_ReadLatch( HANDLE cyHandle,
+ LPWORD lpLatch);
+
+CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI
+CP210xRT_WriteLatch(HANDLE cyHandle,
+ WORD mask,
+ WORD latch);
+
+
+CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI
+CP210xRT_GetPartNumber(HANDLE cyHandle,
+ LPBYTE lpbPartNum);
+
+CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI
+CP210xRT_GetDeviceProductString(HANDLE cyHandle,
+ LPVOID lpProduct,
+ LPBYTE lpbLength,
+ BOOL bConvertToASCII = TRUE
+ );
+
+CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI
+CP210xRT_GetDeviceSerialNumber(HANDLE cyHandle,
+ LPVOID lpSerialNumber,
+ LPBYTE lpbLength,
+ BOOL bConvertToASCII = TRUE
+ );
+
+CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI
+CP210xRT_GetDeviceInterfaceString(HANDLE cyHandle,
+ LPVOID lpInterfaceString,
+ LPBYTE lpbLength,
+ BOOL bConvertToASCII);
--- /dev/null
+/*
+ * Copyright (c) 2010-2013, Titanium Mirror, Inc.
+ * All rights reserved.
+ * Based upon work Copyright (c) 2006-2007 by Sporian Microsystems, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - Neither the name of the Technische Universität Berlin nor the names
+ * of its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @author R. Steve McKown <rsmckown@gmail.com>
+ */
+
+#include "Python.h"
+#include "windows.h"
+#include <io.h>
+#include "CP210xRuntimeDLL.h"
+
+
+static PyObject* exception;
+
+
+static PyObject* ex_writeLatch(PyObject* self, PyObject* args)
+{
+ BYTE bMask;
+ BYTE bData;
+ HANDLE hDevice;
+ CP210x_STATUS ret;
+
+ if (!PyArg_ParseTuple(args, "iBB", &hDevice, &bMask, &bData))
+ return NULL;
+
+ if (hDevice == INVALID_HANDLE_VALUE) {
+ PyErr_SetString(exception, "No access to device");
+ return NULL;
+ }
+
+ ret = CP210xRT_WriteLatch(hDevice, bMask, bData);
+ if (ret != CP210x_SUCCESS) {
+ PyErr_SetString(exception, "IO Error with device");
+ return NULL;
+ }
+
+ Py_INCREF(Py_None);
+ return Py_None;
+ //return Py_BuildValue("i", ret);
+}
+
+
+static PyMethodDef cp210xrt_methods[] = {
+ { "writeLatch", ex_writeLatch, METH_VARARGS, "Set GPIO bits" },
+ { NULL, NULL }
+};
+
+
+PyMODINIT_FUNC initcp210xrt(void)
+{
+ PyObject* m = Py_InitModule("cp210xrt", cp210xrt_methods);
+ exception = PyErr_NewException("cp210xrt.error", NULL, NULL);
+ Py_INCREF(exception);
+ PyModule_AddObject(m, "error", exception);
+}
--- /dev/null
+# This is an example of a distutils 'setup' script for the example_nt
+# sample. This provides a simpler way of building your extension
+# and means you can avoid keeping MSVC solution files etc in source-control.
+# It also means it should magically build with all compilers supported by
+# python.
+
+# USAGE: you probably want 'setup.py install' - but execute 'setup.py --help'
+# for all the details.
+
+# NOTE: This is *not* a sample for distutils - it is just the smallest
+# script that can build this. See distutils docs for more info.
+
+from distutils.core import setup, Extension
+
+cp210xrt_mod = Extension('cp210xrt', sources = ['cp210xrt.cpp'], \
+ include_dirs = ['.'], library_dirs = ['.'], libraries = ['CP210xRuntime'])
+
+setup(name = "cp210xrt",
+ version = "0.1",
+ description = "CP210x runtime access",
+ ext_modules = [cp210xrt_mod],
+)
--- /dev/null
+#!/usr/bin/python
+
+import sys, time, serial, cp210xrt;
+
+if sys.platform != 'win32':
+ print 'Sorry, the cp210xrt module only works for Win32 platforms'
+ sys.exit(1)
+elif sys.argc != 2:
+ print 'usage: %s <COMport>' % sys.argv[0]
+ sys.exit(1)
+else:
+ comport = sys.argvp[1]
+ print 'Opening %r\n' % comport
+ serialport = serial.Serial(comport, 9600)
+ print 'Clear bit...\n'
+ cp210xrt.writeLatch(serialport.fd, 0x04, 0x0)
+ time.sleep(0.250)
+ print 'Set bit...\n'
+ cp210xrt.writeLatch(serialport.fd, 0x04, 0x04)
+ print 'Device should now be reset'
+
# from bsl_standard or one of its specializations.
#
-import sys, fcntl, time, string, cStringIO, struct
+import sys, time, string, cStringIO, struct
sys.path.append("@tinyoslibdir@")
import serial
bsl_standard.bslInit(self)
+if sys.platform == 'win32':
+ sys.path.append('.')
+ import cp210xrt
+else:
+ import fcntl
+
GPIOBIC = 0x89f2
GPIOBIS = 0x89f3
GPIO_2 = 0x04
def setGpio2(self, assertme):
""" set not assertme to the gpio pin """
- if not self.fd: raise "connection not open"
+ if not self.fd: raise BSLException("connection not open")
if assertme:
fcntl.ioctl(self.fd, GPIOBIC, GPIO_2)
else:
def setGpio3(self, assertme):
""" set not assertme to the gpio pin """
- if not self.fd: raise "connection not open"
+ if not self.fd: raise BSLException("connection not open")
if assertme:
fcntl.ioctl(self.fd, GPIOBIC, GPIO_3)
else:
time.sleep(0.010) # no sleep = too fast
-class cp2103_cygwin:
+class cp2103_win32:
"""
- Implements cp2103 gpio access for windows cygwin systems via the
+ Implements cp2103 gpio access for windows win32 systems via the
cp210x VCP and the CP210xRuntime.dll.
"""
def __init__(self, serialport):
- import cp210xrt
- sys.stderr.write("cp2103_cygwin\n")
- self.fd = serialport.fd
+ self.hComPort = serialport.hComPort
def setGpio2(self, assertme):
""" set not assertme to the gpio pin """
- import cp210xrt
if assertme:
- cp210xrt.writeLatch(self.fd, GPIO_2, 0)
+ cp210xrt.writeLatch(self.hComPort, GPIO_2, 0)
else:
- cp210xrt.writeLatch(self.fd, GPIO_2, GPIO_2)
+ cp210xrt.writeLatch(self.hComPort, GPIO_2, GPIO_2)
def setGpio3(self, assertme):
""" set not assertme to the gpio pin """
- import cp210xrt
if assertme:
- cp210xrt.writeLatch(self.fd, GPIO_3, 0)
+ cp210xrt.writeLatch(self.hComPort, GPIO_3, 0)
else:
- cp210xrt.writeLatch(self.fd, GPIO_3, GPIO_3)
+ cp210xrt.writeLatch(self.hComPort, GPIO_3, GPIO_3)
def cp2103_factory(serialport):
- import os;
- if os.name == 'posix':
- if sys.platform == 'cygwin':
- return cp2103_cygwin(serialport)
- else:
- return cp2103_posix(serialport)
+ if sys.platform == 'win32':
+ return cp2103_win32(serialport)
+ elif sys.platform != 'cygwin':
+ return cp2103_posix(serialport)
else:
- raise "No cp2103 support is available for your platform."
+ raise BSLException("No cp2103 support is available for your platform.")
def getClass(classname, modulename="__main__"):