CVS: python/msp430 jtag.py,1.21,1.22

Chris Liechti <[email protected]>
Newsgroups gmane.comp.hardware.texas-instruments.msp430.gcc.cvs
Message-ID <[email protected]>
Update of /cvsroot/mspgcc/python/msp430
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31475/python/msp430

Modified Files:
	jtag.py 
Log Message:
- library backend can be enforced
- change search strategy (use bin/lib for 3'rd party MSP430.dll)
- msp430-dco fixes (no messages in case of failre), enfore use of MSP430mspgcc lib
- fix --upload size

Index: jtag.py
===================================================================
RCS file: /cvsroot/mspgcc/python/msp430/jtag.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -w -d -r1.21 -r1.22
--- jtag.py	13 Mar 2006 23:21:29 -0000	1.21
+++ jtag.py	4 Apr 2006 21:58:10 -0000	1.22
@@ -37,16 +37,24 @@
     DEBUG = 1
 
 #ctypes backend variations:
-CTYPES_MSPGCC = "ctypes/mspgcc lib"
-CTYPES_TI = "ctypes/TI or 3rd party lib"
+CTYPES_PARJTAG = "parjtag/mspgcc"
+CTYPES_MSPGCC = "ctypes/mspgcc"
+CTYPES_TI = "ctypes/TI or 3rd party"
 
 #exceptions
 class JTAGException(Exception): pass
 
-#1st try the ctypes implementation, if thats not available try to use the C extension
+backend = None
+def init_backend(force=None):
+    global backend
+    global _parjtag
+    
+    #1st try the ctypes implementation, if that's not available try to use the C extension
 try:
     import ctypes
 except ImportError:
+        if force is not None and force != CTYPES_MSPGCC:
+            raise JTAGException("Desired backend is not available: %s" % force)
     try:
         import _parjtag
     except ImportError:
@@ -60,11 +68,35 @@
             "can be built from sources from http://mspgcc.sf.net\n"
         )
     else:
-        backend = "_parjtag so/dll"
+            backend = CTYPES_PARJTAG
 else:
+        import os
     #create a wrapper class with ctypes, that has the same API as _parjtag
-    backend = "ctypes"
+        def locate_library(libname, paths=sys.path, loader=ctypes.windll):
+            for path in paths:
+                library = os.path.join(path, libname)
+                if DEBUG: sys.stderr.write('trying %r...\n' % library)
+                if os.path.exists(library):
+                    if DEBUG: sys.stderr.write('using %r\n' % library)
+                    return loader.LoadLibrary(library)
+            else:
+                raise IOError('%s not found' % libname)
     
+        #an absolute path to the library can be given.
+        #LIBMSPGCC_PATH is used to pass its location
+        search_path = sys.path[:]   #copy sys.path list
+        #if environment variable is set, insert this path first
+        try:
+            search_path.insert(0, os.environ['LIBMSPGCC_PATH'])
+        except KeyError:
+            if DEBUG: sys.stderr.write('LIBMSPGCC_PATH is not set\n')
+        #as fallback, append PATH
+        try:
+            search_path.extend(os.environ['PATH'].split(os.pathsep))
+        except KeyError:
+            pass
+        
+        #~ print search_path
     STATUS_OK    = 0
     STATUS_ERROR = -1
     TRUE         = 1
@@ -74,22 +106,29 @@
     if sys.platform == 'win32':
         #the library is found on the PATH, respectively in the executables directory
         
+            if force == CTYPES_MSPGCC:
+                MSP430mspgcc = locate_library('MSP430mspgcc.dll', search_path)
+                backend = CTYPES_MSPGCC
+            elif force == CTYPES_TI:
+                #~ MSP430mspgcc = ctypes.windll.MSP430
+                MSP430mspgcc = locate_library('MSP430.dll', search_path)
+                backend = CTYPES_TI
+            else:
+                #autodetect
         try:
-            #try to use the TI library
-            MSP430mspgcc = ctypes.windll.MSP430
+                    #try to use the TI or third party library
+                    MSP430mspgcc = locate_library('MSP430.dll', search_path)
             backend = CTYPES_TI
-        except WindowsError:
+                except IOError:
             #when that fails, use the mspgcc implementation
-            MSP430mspgcc = ctypes.windll.MSP430mspgcc
+                    MSP430mspgcc = locate_library('MSP430mspgcc.dll', search_path)
             backend = CTYPES_MSPGCC
     else:
-        #an absolute path to the library has to be given.
-        #LIBMSPGCC_PATH is used to pass its location
-        import os
+            #now try to locate library
         try:
-            MSP430mspgcc = ctypes.cdll.LoadLibrary(os.path.join(os.environ['LIBMSPGCC_PATH'], 'libMSP430mspgcc.so'))
-        except KeyError:
-            raise KeyError('The environment variable "LIBMSPGCC_PATH" must point to the folder that contains "libMSP430mspgcc.so"')
+                MSP430mspgcc = locate_library('libMSP430mspgcc.so', search_path, ctypes.cdll)
+            except IOError, e:
+                raise IOError('The environment variable "LIBMSPGCC_PATH" must point to the folder that contains "libMSP430mspgcc.so": %s' % e)
         backend = CTYPES_MSPGCC
     
     MSP430_Initialize               = MSP430mspgcc.MSP430_Initialize
@@ -237,6 +276,7 @@
             """Read 'size' bytes starting at the specified address.
             The return value is a string with the (binary) data.
             It is possible to read peripherals, RAM as well as Flash."""
+                if size < 0: raise ValueError("Size must not be negative")
             buffer = (ctypes.c_char*size)();
             
             status = MSP430_Memory(address, buffer, size, READ)
@@ -355,6 +395,7 @@
     """
 
     def __init__(self):
+        if backend is None: init_backend()
         self.showprogess = 0
         self.data = None
         self.verbose = 1
@@ -524,10 +565,12 @@
         _parjtag.secure()
 
 
-# simple, stupid module test
+# simple, stupid module test, debug is set above
 if __name__ == '__main__':
-    print "Backend: %s" % (backend, )
+    #~ init_backend(CTYPES_MSPGCC)
+    #~ init_backend(CTYPES_TI)
     jtagobj = JTAG()
+    print "Backend: %s" % (backend, )
     jtagobj.open()
     try:
         jtagobj.connect()



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.