OpenCores
URL https://opencores.org/ocsvn/usb_dongle_fpga/usb_dongle_fpga/trunk

Subversion Repositories usb_dongle_fpga

[/] [usb_dongle_fpga/] [trunk/] [sw/] [dongle.py] - Diff between revs 44 and 46

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 44 Rev 46
Line 40... Line 40...
#            14 Nov. 2007  Improved help. 
#            14 Nov. 2007  Improved help. 
#            10 Mar. 2008  Forced code to hw flow control settings made linux 1 byte read to 2 bytes
#            10 Mar. 2008  Forced code to hw flow control settings made linux 1 byte read to 2 bytes
#                          as dongle never reads 1 byte at the time
#                          as dongle never reads 1 byte at the time
#            18 Apr. 2008  Added file size boundary check on write to see if remaining size from
#            18 Apr. 2008  Added file size boundary check on write to see if remaining size from
#                          given offset fits the file size
#                          given offset fits the file size
 
 
 
#            24 Apr. 2008  Mac OS X support by Stefan Reinauer <stepan@coresystems.de>
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
 
 
import os
import os
import sys
import sys
import string
import string
Line 130... Line 132...
    from termios import *
    from termios import *
    import fcntl
    import fcntl
    import exceptions
    import exceptions
    import array
    import array
    print "Linux platform detected:"
    print "Linux platform detected:"
 
elif sys.platform=='darwin':
 
    from termios import *
 
    import fcntl
 
    import exceptions
 
    import array
 
    print "Mac OS X platform detected:"
else:
else:
    sys.exit('Sorry, no implementation for this platform yet')
    sys.exit('Sorry, no implementation for this platform yet')
 
 
 
 
 
 
Line 429... Line 437...
    def flush(self):
    def flush(self):
        """Discards all bytes from the output or input buffer"""
        """Discards all bytes from the output or input buffer"""
        tcflush(self.__handle, TCIOFLUSH)
        tcflush(self.__handle, TCIOFLUSH)
 
 
 
 
 
if sys.platform=='darwin':
 
  class SerialPortOSX:
 
    """Encapsulate methods for accesing to a serial port."""
 
 
 
    BaudRatesDic={
 
        110: B110,
 
        300: B300,
 
        600: B600,
 
        1200: B1200,
 
        2400: B2400,
 
        4800: B4800,
 
        9600: B9600,
 
        19200: B19200,
 
        38400: B38400,
 
        57600: B57600,
 
        115200: B115200,
 
        230400: B230400
 
        }
 
    buf = array.array('h', '\000'*4)
 
 
 
    def __init__(self, dev, timeout=None, speed=115200, mode='232', params=None):
 
        self.__devName, self.__timeout, self.__speed=dev, timeout, speed
 
        self.__mode=mode
 
        self.__params=params
 
        self.__speed = 0
 
        self.__reopen = 0
 
        while 1:
 
            try:
 
                self.__handle=os.open(dev, os.O_RDWR)
 
                break
 
 
 
            except:
 
                n=0
 
                while (n < 2000000):
 
                    n += 1;
 
                self.__reopen = self.__reopen + 1
 
            if self.__reopen > 32:
 
                print "Port does not exist..."
 
                raise SerialPortException('Port does not exist...')
 
                break
 
 
 
        self.__configure()
 
 
 
    def __del__(self):
 
        if self.__speed:
 
            #tcsetattr(self.__handle, TCSANOW, self.__oldmode)
 
            pass
 
            try:
 
                pass
 
                #os.close(self.__handle)
 
            except IOError:
 
                raise SerialPortException('Unable to close port')
 
 
 
 
 
    def __configure(self):
 
        if not self.__speed:
 
            self.__speed=115200
 
 
 
        # Save the initial port configuration
 
        self.__oldmode=tcgetattr(self.__handle)
 
        if not self.__params:
 
            # print "Create MacOSX params for serialport..."
 
            # self.__params is a list of attributes of the file descriptor
 
            # self.__handle as follows:
 
            # [c_iflag, c_oflag, c_cflag, c_lflag, c_ispeed, c_ospeed, cc]
 
            # where cc is a list of the tty special characters.
 
            self.__params=[]
 
            # c_iflag
 
            self.__params.append(IGNPAR)
 
            # c_oflag
 
            self.__params.append(0)
 
            # c_cflag
 
            self.__params.append(CS8|CREAD|CRTSCTS)
 
            # c_lflag
 
            self.__params.append(0)
 
            # c_ispeed
 
            self.__params.append(SerialPortOSX.BaudRatesDic[self.__speed])
 
            # c_ospeed
 
            self.__params.append(SerialPortOSX.BaudRatesDic[self.__speed])
 
            cc=[0]*NCCS
 
        if self.__timeout==None:
 
            # A reading is only complete when VMIN characters have
 
            # been received (blocking reading)
 
            cc[VMIN]=1
 
            cc[VTIME]=0
 
        elif self.__timeout==0:
 
            cc[VMIN]=0
 
            cc[VTIME]=0
 
        else:
 
            cc[VMIN]=0
 
            cc[VTIME]=self.__timeout #/100
 
        self.__params.append(cc)               # c_cc
 
 
 
        tcsetattr(self.__handle, TCSANOW, self.__params)
 
 
 
 
 
    def fileno(self):
 
        return self.__handle
 
 
 
 
 
    def __read1(self):
 
        tryCnt = 0
 
        byte = ""
 
        while(len(byte)==0 and tryCnt<10):
 
            tryCnt+=1
 
            byte = os.read(self.__handle, 2)
 
        if len(byte)==0 and self.__timeout!=0: # Time-out
 
            print 'Time out cnt was %i'%(tryCnt)
 
            print 'Expected 1 byte but got %i before timeout'%(len(byte))
 
            sys.stdout.flush()
 
            raise SerialPortException('Timeout')
 
        else:
 
            return byte
 
 
 
 
 
    def read(self, num=1):
 
        s=''
 
        for i in range(num/2):
 
            s=s+SerialPortOSX.__read1(self)
 
        return s
 
 
 
 
 
    def readline(self):
 
 
 
        s = ''
 
        while not '\n' in s:
 
            s = s+SerialPortOSX.__read1(self)
 
 
 
        return s
 
 
 
 
 
    def write(self, s):
 
        """Write the string s to the serial port"""
 
        return os.write(self.__handle, s)
 
 
 
    def inWaiting(self):
 
        """Returns the number of bytes waiting to be read"""
 
        data = struct.pack("L", 0)
 
        data=fcntl.ioctl(self.__handle, FIONREAD, data)
 
        return struct.unpack("L", data)[0]
 
 
 
    def outWaiting(self):
 
        """Returns the number of bytes waiting to be write
 
        mod. by J.Grauheding
 
        result needs some finetunning
 
        """
 
        rbuf=fcntl.ioctl(self.__handle, FIONWRITE, self.buf)
 
        return rbuf
 
 
 
 
 
    def flush(self):
 
        """Discards all bytes from the output or input buffer"""
 
        tcflush(self.__handle, TCIOFLUSH)
 
 
 
 
 
 
#### end inline of artec FTDI spesific Uspp code ###############################################
#### end inline of artec FTDI spesific Uspp code ###############################################
 
 
 
 
#### Dongle code starts here  ##################################################################
#### Dongle code starts here  ##################################################################
 
 
 
 
#### global funcs ####
#### global funcs ####
def usage(s):
def usage(s):
    print "Artec USB Dongle programming utility ver. 2.52"
    print "Artec USB Dongle programming utility ver. 2.6"
    print "Usage:"
    print "Usage:"
    print "Write file      : ",s," [-vq] -c <name> <file> <offset>"
    print "Write file      : ",s," [-vq] -c <name> <file> <offset>"
    print "Readback file   : ",s," [-vq] -c <name> [-vq] -r <offset> <length> <file>"
    print "Readback file   : ",s," [-vq] -c <name> [-vq] -r <offset> <length> <file>"
    print "Options:"
    print "Options:"
    print " <file> <offset> When file and offset are given file will be written to dongle"
    print " <file> <offset> When file and offset are given file will be written to dongle"
Line 480... Line 643...
    print "Examples:"
    print "Examples:"
    print ""
    print ""
    print " ",s," -c COM3 loader.bin 0                       "
    print " ",s," -c COM3 loader.bin 0                       "
    print " ",s," -c /dev/ttyS3 boot.bin 3840K"
    print " ",s," -c /dev/ttyS3 boot.bin 3840K"
    print " ",s," -c COM3 -r 0x3C0000 256K flashcontent.bin"
    print " ",s," -c COM3 -r 0x3C0000 256K flashcontent.bin"
 
    print " ",s," -c /dev/cu.usbserial-003011FD -v (Mac OS X)"
######################
######################
 
 
 
 
class DongleMode:
class DongleMode:
    def __init__(self):
    def __init__(self):
Line 530... Line 694...
    def __init__(self,name, baud, timeout):  #time out in millis 1000 = 1s baud like 9600, 57600
    def __init__(self,name, baud, timeout):  #time out in millis 1000 = 1s baud like 9600, 57600
        self.mode = 0
        self.mode = 0
        try:
        try:
            if sys.platform=='win32':
            if sys.platform=='win32':
                self.tty = SerialPortWin(name,timeout, baud)
                self.tty = SerialPortWin(name,timeout, baud)
            else:
            elif sys.platform=='linux2':
                self.tty = SerialPortLin(name,timeout, baud)
                self.tty = SerialPortLin(name,timeout, baud)
 
            elif sys.platform=='darwin':
 
                self.tty = SerialPortOSX(name,timeout, baud)
 
 
        except SerialPortException , e:
        except SerialPortException , e:
            print "Unable to open port " + name
            print "Unable to open port " + name
            sys.exit();
            sys.exit();
 
 
Line 720... Line 886...
        if self.mode.version <5:
        if self.mode.version <5:
            n = 0
            n = 0
            if sys.platform=='win32':
            if sys.platform=='win32':
                while (n < 1024):
                while (n < 1024):
                    n += 1;
                    n += 1;
            elif sys.platform=='linux2':
            elif sys.platform=='linux2' or sys.platform=='darwin':
                #Linux FTDI VCP driver is way faster and needs longer grace time than windows driver
                #Linux FTDI VCP driver is way faster and needs longer grace time than windows driver
                while (n < 1024*8):
                while (n < 1024*8):
                    n += 1;
                    n += 1;
 
 
    def write_buf_cmd(self, buffer):
    def write_buf_cmd(self, buffer):
Line 853... Line 1019...
    if sys.platform=='win32':
    if sys.platform=='win32':
        don  = Dongle(mode.portname,256000,6000)
        don  = Dongle(mode.portname,256000,6000)
    elif sys.platform=='linux2':
    elif sys.platform=='linux2':
        don  = Dongle(mode.portname,230400,6000)
        don  = Dongle(mode.portname,230400,6000)
        #don.tty.cts()
        #don.tty.cts()
 
    elif sys.platform=='darwin':
 
        don  = Dongle(mode.portname,230400,6000)
 
        #don.tty.cts()
    else:
    else:
        sys.exit('Sorry, no implementation for this platform yet')
        sys.exit('Sorry, no implementation for this platform yet')
 
 
 
 
    don.tty.wait = wait
    don.tty.wait = wait
Line 874... Line 1043...
        if sys.platform=='win32':
        if sys.platform=='win32':
            don  = Dongle(mode.portname,256000,6000)
            don  = Dongle(mode.portname,256000,6000)
        elif sys.platform=='linux2':
        elif sys.platform=='linux2':
            don  = Dongle(mode.portname,230400,6000)
            don  = Dongle(mode.portname,230400,6000)
            #self.tty.cts()
            #self.tty.cts()
 
        elif sys.platform=='darwin':
 
            don  = Dongle(mode.portname,230400,6000)
 
            #self.tty.cts()
        else:
        else:
            sys.exit('Sorry, no implementation for this platform yet')
            sys.exit('Sorry, no implementation for this platform yet')
        don.tty.wait = wait
        don.tty.wait = wait
 
 
    buf=don.getReturn(2)  # two bytes expected to this command
    buf=don.getReturn(2)  # two bytes expected to this command

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.