Ejemplo n.º 1
0
    def init(self):
        """ Init ftdi com.
        """

        # Create ftdi context
        self._log.info("Try to Create ftdi context")
        self.__ftdic = ftdi.ftdi_context()
        if self.__ftdic is None:
            self._log.error("Can't create ftdi context")
            raise FtdiError("Can't create ftdi context")

        # Init ftdi context
        err = ftdi.ftdi_init(self.__ftdic)
        if err < 0:
            self._log.error("Can't init ftdi context (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
            raise FtdiError("Can't init ftdi context (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))

        # Open port
        self._log.info("Try to open ftdi port")
        err = ftdi.ftdi_usb_open(self.__ftdic, USB_VENDOR, USB_PRODUCT)
        if err < 0:
            self._log.error("Can't open usb (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
            raise FtdiError("Can't open usb (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))

        err = ftdi.ftdi_set_baudrate(self.__ftdic, BAUD_RATE)
        if err < 0:
            self._log.error("Can't set baudrate (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
            raise FtdiError("Can't set baudrate (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))

        # Because of the usb interface, must use 8 bits transmission data, instead of 7 bits
        err = ftdi.ftdi_set_line_property(self.__ftdic, ftdi.BITS_8, ftdi.EVEN, ftdi.STOP_BIT_1)
        if err < 0:
            self._log.error("Can't set line property (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
            raise FtdiError("Can't set line property (%d, %s)" % (err, ftdi.ftdi_get_error_string(self.__ftdic)))
Ejemplo n.º 2
0
def ouvrirPort(port):
	# Création du context
	ftdiContext = ftdi.ftdi_context()

	# Initialisation du context
	if ftdi.ftdi_init(ftdiContext) < 0:
		raise IOError("Echec de l'initialisation")

	# Ouverture du port
	ret = ftdi.ftdi_usb_open(ftdiContext, 0x0403, 0x6001)
	if (ret) < 0:
		raise IOError("Impossible d'ouvrir le port " + hex(port) + " : " + repr(ret) + "(" + ftdi.ftdi_get_error_string(ftdiContext) + ")")


	# Fixer le debit à 1200 bit/s => 150 car/s (8 bits/car)
	ret = ftdi.ftdi_set_baudrate(ftdiContext, 1200)
	if (ret) < 0:
		raise IOError("Impossible de fixer le baudrate pour le port " + hex(port) + " : " + repr(ret) + "(" + ftdi.ftdi_get_error_string(ftdiContext) + ")")

	# Pour une obscure raison pour le moment il faut mettre ftdi.BITS_8
	# et non ftdi.BITS_7 comme indiqué dans la spec pour que cela fonctionne ???
	ret = ftdi.ftdi_set_line_property(ftdiContext, ftdi.BITS_8, ftdi.EVEN, ftdi.STOP_BIT_1)

	# Activation du compteur 1 et lecture de qqes trames
	ret = ftdi.ftdi_set_bitmode(ftdiContext, port, ftdi.BITMODE_CBUS);

	# Fixer le timeout de lecture
	ftdiContext.usb_read_timeout = 50000; 

	return ftdiContext
Ejemplo n.º 3
0
	def reset(self, bootloader=False):
		handle = ftdi.ftdi_context()
		if ftdi.ftdi_init(handle) < 0:
			print 'Initialization error.',
			print
			sys.exit(-1)
		serialnum = self.getFTDIIdFromDevPort(self.port)
		if subprocess.call(['rmmod', 'ftdi_sio']) != 0:
			print 'Close all processes that may access serial ports. You may also need to run the program as root.',
			print
			sys.exit(-1)
		error = ftdi.ftdi_usb_open_desc(handle, FT232R_VENDOR, FT232R_PRODUCT, None, serialnum)
		if error < 0:
			print ('Unable to open port. Error:' + str(error)),
			print
			subprocess.call(['modprobe', 'ftdi_sio'])
			sys.exit(-1)
		item0, item1 = self.guessResetDirection(handle)
		resetValue = item0
		nBootValue = item1
		if (resetValue, nBootValue) == (None, None):
			ftdi.ftdi_usb_close(handle)
			subprocess.call(['modprobe', 'ftdi_sio'])
			return 1
		else:
			ftdi.ftdi_set_bitmode(handle, resetValue, ftdi.BITMODE_CBUS)
			time.sleep(0.5)
			if bootloader:
				ftdi.ftdi_set_bitmode(handle, nBootValue, ftdi.BITMODE_CBUS)
				time.sleep(0.10000000000000001)
			ftdi.ftdi_set_bitmode(handle, 0, ftdi.BITMODE_CBUS)
			ftdi.ftdi_usb_close(handle)
			subprocess.call(['modprobe', 'ftdi_sio'])
			time.sleep(1)
			return None
Ejemplo n.º 4
0
    def __init__(self, serial=None, index=None):
        self.ftdic = ftdi.ftdi_context()
        self.ftdi = ftdi
        self.sensors = dict()
        self.hw_sw = dict()
        self.hw_sw["DC"] = nvouten
        self.hw_sw["SW0"] = sws[0]
        self.hw_sw["SW1"] = sws[1]
        self.hw_sw["ALERT"] = alert
        self.hw_sw["GPIO0"] = gpios[0]
        self.hw_sw["GPIO1"] = gpios[1]
        self.hw_sw["GPIO2"] = gpios[2]
        self.hw_sw["GPIO3"] = gpios[3]
        self.switches = dict()

        try:
            ret = ftdi.ftdi_init(self.ftdic)
            if ret < 0:
                raise Exception
            ret = ftdi.ftdi_usb_open_desc_index(self.ftdic, 0x0403, 0x06010, "PMC-17 v1.0", serial, index if index else 0)
            if ret < 0:
                raise Exception("Could not open device", ftdi.ftdi_get_error_string(self.ftdic))
            ret = ftdi.ftdi_set_interface(self.ftdic, ftdi.INTERFACE_A)
            if ret < 0:
                raise Exception
            self.hw = i2c_ftdi.i2c_ftdi(self.ftdic, scl, sda_out, sda_in, 400000, initial_output)
            self.i2c = i2c.i2c(self.hw)
        except Exception as e:
            ftdi.ftdi_deinit(self.ftdic)
            raise
Ejemplo n.º 5
0
def open_ftdi():
	if (ftdi.ftdi_init(ftdic) < 0) :
		print "ftdi_init fehlgeschlagen"
		exit(-1)
	
	else:
		print "ftdi_init geklappt"


	#Öffne FTDI-Chip FT232R - evt. andere Nummern eintragen! 
	#Tut auch mit FT245BL
	ret = ftdi.ftdi_usb_open(ftdic, 0x0403, 0x6001)
	if (ret) < 0:
		print "Konnte Gerät nicht öffnen:" + ret + "(" + \
			ftdi.ftdi_get_error_string(ftdic) + ")"
		exit(-1)
	else:
		print "Gerät erfolgreich geöffnet"

	#Bitbag mit DTR, RX und TX beginnen
	#2. Argument macht die Pins D0-D7 ansprechbar


	#ftdi.ftdi_enable_bitbang(ftdic, D[0]|D[1]|D[2]|D[3]|D[4]|D[5]|D[6]|D[7] )
	ftdi.ftdi_enable_bitbang(ftdic,0x1 | 0x2| 0x4| 0x8| 0x10| 0x20| 0x40| 0x80)

	#Am Anfang ausschalten
	ftdi.ftdi_write_data(ftdic, chr(255), 1)
Ejemplo n.º 6
0
	def reset(self, bootloader=False):
		handle = ftdi.ftdi_context()
		if ftdi.ftdi_init(handle) < 0:
			print 'Initialization error.',
			print
			sys.exit(-1)
		serialnum = self.getFTDIIdFromDevPort(self.port)
		if subprocess.call(['rmmod', 'ftdi_sio']) != 0:
			print 'Close all processes that may access serial ports. You may also need to run the program as root.',
			print
			sys.exit(-1)
		error = ftdi.ftdi_usb_open_desc(handle, FT232R_VENDOR, FT232R_PRODUCT, None, serialnum)
		if error < 0:
			print ('Unable to open port. Error:' + str(error)),
			print
			subprocess.call(['modprobe', 'ftdi_sio'])
			sys.exit(-1)
		item0, item1 = self.guessResetDirection(handle)
		resetValue = item0
		nBootValue = item1
		if (resetValue, nBootValue) == (None, None):
			ftdi.ftdi_usb_close(handle)
			subprocess.call(['modprobe', 'ftdi_sio'])
			return 1
		else:
			ftdi.ftdi_set_bitmode(handle, resetValue, ftdi.BITMODE_CBUS)
			time.sleep(0.5)
			if bootloader:
				ftdi.ftdi_set_bitmode(handle, nBootValue, ftdi.BITMODE_CBUS)
				time.sleep(0.10000000000000001)
			ftdi.ftdi_set_bitmode(handle, 0, ftdi.BITMODE_CBUS)
			ftdi.ftdi_usb_close(handle)
			subprocess.call(['modprobe', 'ftdi_sio'])
			time.sleep(1)
			return None
Ejemplo n.º 7
0
    def __init__(self, serial=None, index=None):
        self.ftdic = ftdi.ftdi_context()
        self.ftdi = ftdi
        self.sensors = dict()
        self.hw_sw = dict()
        self.hw_sw["DC"] = nvouten
        self.hw_sw["SW0"] = sws[0]
        self.hw_sw["SW1"] = sws[1]
        self.hw_sw["ALERT"] = alert
        self.hw_sw["GPIO0"] = gpios[0]
        self.hw_sw["GPIO1"] = gpios[1]
        self.hw_sw["GPIO2"] = gpios[2]
        self.hw_sw["GPIO3"] = gpios[3]
        self.switches = dict()

        try:
            ret = ftdi.ftdi_init(self.ftdic)
            if ret < 0:
                raise Exception
            ret = ftdi.ftdi_usb_open_desc_index(self.ftdic, 0x0403, 0x06010,
                                                "PMC-17 v1.0", serial,
                                                index if index else 0)
            if ret < 0:
                raise Exception("Could not open device",
                                ftdi.ftdi_get_error_string(self.ftdic))
            ret = ftdi.ftdi_set_interface(self.ftdic, ftdi.INTERFACE_A)
            if ret < 0:
                raise Exception
            self.hw = i2c_ftdi.i2c_ftdi(self.ftdic, scl, sda_out, sda_in,
                                        400000, initial_output)
            self.i2c = i2c.i2c(self.hw)
        except Exception as e:
            ftdi.ftdi_deinit(self.ftdic)
            raise
Ejemplo n.º 8
0
    def __init__(self):
        # open channel A
        self.ft_ctx = ftdi.ftdi_context()
        if ftdi.ftdi_init(self.ft_ctx) < 0:
            raise RuntimeError("ftdi_init failed")
        ftdi.ftdi_set_interface(self.ft_ctx, ftdi.INTERFACE_A)
        if ftdi.ftdi_usb_open(self.ft_ctx, 0x0403, 0x6010) != 0:
            raise RuntimeError(ftdi.ftdi_get_error_string(self.ft_ctx))

        try:
            # open and purge channel B
            ft_ctx_b = ftdi.ftdi_context()
            if ftdi.ftdi_init(ft_ctx_b) < 0:
                raise RuntimeError("ftdi_init failed")
            ftdi.ftdi_set_interface(ft_ctx_b, ftdi.INTERFACE_B)
            if ftdi.ftdi_usb_open(ft_ctx_b, 0x0403, 0x6010) != 0:
                raise RuntimeError(ftdi.ftdi_get_error_string(ft_ctx_b))
            # purge buffers
            if ftdi.ftdi_usb_purge_buffers(ft_ctx_b) != 0:
                raise RuntimeError(ftdi.ftdi_get_error_string(ft_ctx_b))
            # close channel B
            ftdi.ftdi_usb_close(ft_ctx_b)
            ftdi.ftdi_deinit(ft_ctx_b)
        except RuntimeError:
            self.close()
            raise

        # setup FIFO mode
        try:
            self.ft_ctx.usb_read_timeout = 5000000
            self.ft_ctx.usb_write_timeout = 5000000
            ftdi.ftdi_read_data_set_chunksize(self.ft_ctx, self.READ_CHUNKSIZE)
            ftdi.ftdi_write_data_set_chunksize(self.ft_ctx,
                                               self.WRITE_CHUNKSIZE)

            if ftdi.ftdi_usb_reset(self.ft_ctx) != 0:
                raise RuntimeError("Reset failed")
            time.sleep(100e-3)

            if ftdi.ftdi_set_bitmode(self.ft_ctx, 0, ftdi.BITMODE_SYNCFF) != 0:
                raise RuntimeError("Setting FIFO mode failed")

            if ftdi.ftdi_set_latency_timer(self.ft_ctx, 1) != 0:
                raise RuntimeError("Setting latency failed")
        except RuntimeError:
            self.close()
            raise
Ejemplo n.º 9
0
    def __init__(self):
        # open channel A
        self.ft_ctx = ftdi.ftdi_context()
        if ftdi.ftdi_init(self.ft_ctx) < 0:
            raise RuntimeError("ftdi_init failed")
        ftdi.ftdi_set_interface(self.ft_ctx, ftdi.INTERFACE_A)
        if ftdi.ftdi_usb_open(self.ft_ctx, 0x0403, 0x6010) != 0:
            raise RuntimeError(ftdi.ftdi_get_error_string(self.ft_ctx))

        try:
            # open and purge channel B
            ft_ctx_b = ftdi.ftdi_context()
            if ftdi.ftdi_init(ft_ctx_b) < 0:
                raise RuntimeError("ftdi_init failed")
            ftdi.ftdi_set_interface(ft_ctx_b, ftdi.INTERFACE_B)
            if ftdi.ftdi_usb_open(ft_ctx_b, 0x0403, 0x6010) != 0:
                raise RuntimeError(ftdi.ftdi_get_error_string(ft_ctx_b))
            # purge buffers
            if ftdi.ftdi_usb_purge_buffers(ft_ctx_b) != 0:
                raise RuntimeError(ftdi.ftdi_get_error_string(ft_ctx_b))
            # close channel B
            ftdi.ftdi_usb_close(ft_ctx_b)
            ftdi.ftdi_deinit(ft_ctx_b)
        except RuntimeError:
            self.close()
            raise

        # setup FIFO mode
        try:            
            self.ft_ctx.usb_read_timeout = 5000000;
            self.ft_ctx.usb_write_timeout = 5000000;
            ftdi.ftdi_read_data_set_chunksize(self.ft_ctx, self.READ_CHUNKSIZE)
            ftdi.ftdi_write_data_set_chunksize(self.ft_ctx, self.WRITE_CHUNKSIZE)
            
            if ftdi.ftdi_usb_reset(self.ft_ctx) != 0:
                raise RuntimeError("Reset failed")
            time.sleep(100e-3)
            
            if ftdi.ftdi_set_bitmode(self.ft_ctx, 0, ftdi.BITMODE_SYNCFF) != 0:
                raise RuntimeError("Setting FIFO mode failed")
                
            if ftdi.ftdi_set_latency_timer(self.ft_ctx, 1) != 0:
                raise RuntimeError("Setting latency failed")
        except RuntimeError:
            self.close()
            raise
Ejemplo n.º 10
0
#!/usr/bin/python
import ftdi
import time

F = ftdi.ftdi_context()

ftdi.ftdi_init(F)

ftdi.ftdi_usb_open(F, 0x0403, 0x6011)

ftdi.ftdi_set_interface(F, ftdi.INTERFACE_B)

ftdi.ftdi_set_bitmode(F, 0x08, ftdi.BITMODE_BITBANG)

ftdi.ftdi_write_data(F, '\x00', 1)

ftdi.ftdi_usb_close(F)
Ejemplo n.º 11
0
@app.route('/refresh/')
def refresh():
    return redirect(url_for('index'))


def run_http():
    app.run(host='0.0.0.0')


# run the HTTP server in a background thread
thread.start_new_thread(run_http, ())

# initialize to the ftdi device
ftdic = ftdi.ftdi_context()
ftdi.ftdi_init(ftdic)
ftdi.ftdi_usb_open(ftdic, 0x0403, 0x6001)
ftdi.ftdi_enable_bitbang(ftdic, RXT | CTS)

while True:
    result = " "
    ftdi.ftdi_read_pins(ftdic, result)

    was_closed = is_closed
    is_closed = 0 != ord(result) & RXT

    if was_closed != is_closed:
        with open('status', 'w') as f:             \
               f.write ('closed' if is_closed else 'open')

    LOG.info("Full: %s\t Masked: %s\t Is Closed: %s\t To Write: %s", \
Ejemplo n.º 12
0
def init_ftdi(log):
    logger = log
    ftdi.ftdi_init(F)
    result = ftdi.ftdi_usb_open(F, 0x0403, 0x6011)
    if result <> 0:
        print "Unable to open ftdi device"
Ejemplo n.º 13
0
    def hw_open(self, interface, port, spiport, bitrate, spibitrate, i2caddr):
        myException = Exception()

        if (self.handle != None) :
            print "Hardware channel is already open. Ignoring redundant open request\n"
            return

        if (self.openCloseActive == 1) :
            print "Hardware channel open or close in process by another thread. Ignoring redundant request\n"
            return


        # lock the thread incase multiple threads attempt to open or close simultaneously
        with self.activityLock :
            # recheck within protected space. Exterior check is to keep requests from queueing
            if (self.handle != None) :
                print "Hardware channel is already open. Ignoring redundant open request\n"
                return

            if (self.openCloseActive == 1) :
                print "Hardware channel open or close in process by another thread. Ignoring redundant request\n"
                return

            self.openCloseActive = 1
            
            
            # Initialize the I2C hardware interface based on settings in config.py
            print '\nTPS65982 Debug Tool Version %s\n' %TI_DEBUG_TOOL_VERSION

            self.HW_INTERFACE = interface
            
            if interface == config.AARDVARK:
                if (self.aardvarkInterfaceOpen == 0) :
                    try :
                        import aardvark_py
                        global aardvark_py
                        import aardvark_rw
                        global aardvark_rw
                        import aardvark_spi
                        global aardvark_spi
#                        globals()[aardvark_py] = __import__('aardvark_py') 
#                        globals()[aardvark_rw] = __import__('aardvark_rw') 
#                        globals()[aardvark_spi] = __import__('aardvark_spi')
                        self.aardvarkInterfaceOpen = 1
                    except Exception as e:
                        self.aardvarkInterfaceOpen = 0
                        self.aardvarkInterfaceFailure = 1
                        self.aardvarkInterfaceFailureMsg = " Aardvark Interface loading Error: %s" % e.message

                if (self.aardvarkInterfaceFailure == 0) :
                    try :        
                        self.handle = aardvark_rw.init_aardvark(port, bitrate, spibitrate)
                        self.spihandle = self.handle
                    except Exception as e:
                        self.openCloseActive = 0
                        self.handle = None
                        self.spihandle = None
                        raise e
                else :
                    self.openCloseActive = 0
                    myException.message = self.aardvarkInterfaceFailureMsg
                    raise myException

                
            elif interface == config.FTDI:
                if (self.ftdiInterfaceOpen == 0) :
                    try :
                        import ftdi
                        global ftdi
                        import ftdi_spi
                        global ftdi_spi
#                        globals()[ftdi] = __import__(ftdi)
#                        globals()[ftdi_spi] = __import__(ftdi_spi)
                        self.ftdiInterfaceOpen = 1
                    except Exception as e:
                        self.ftdiInterfaceOpen = 0
                        self.ftdiInterfaceFailure = 1
                        self.ftdiInterfaceFailureMsg = " FTDI Interface loading Error: %s" % e.message


                if (self.ftdiInterfaceFailure == 0) :
                    try:        
                        (self.handle, self.spihandle) = ftdi.ftdi_init(port, spiport, bitrate, spibitrate)
                    except Exception as e:
                        self.openCloseActive = 0
                        self.handle = None
                        self.spihandle = None
                        raise e
                else :
                    self.openCloseActive = 0
                    myException.message = self.ftdiInterfaceFailureMsg
                    raise myException
                    

            elif interface == config.USB_TO_ANY:
                self.openCloseActive = 0
                myException.message =  'USB_TO_ANY hardware interface hw_interface_init not implemented!' 
                raise myException
                
            elif interface == config.USB_EP:
                if (self.usbepInterfaceOpen == 0) :
                    try :
                        import usbep
                        global usbep
                        self.usbepInterfaceOpen = 1
                    except Exception as e:
                        self.usbepInterfaceOpen = 0
                        self.usbepInterfaceFailure = 1
                        self.usbepInterfaceFailureMsg = " USB_EP Interface loading Error: %s" % e.message

                if (self.usbepInterfaceFailure == 0) :
                    try :        
                        (self.handle) = usbep.usbep_init()
                    except Exception as e:
                        self.handle = None
                        self.spihandle = None
                        self.openCloseActive = 0
                        raise e
                else:
                    self.openCloseActive = 0
                    myException.message = self.usbepInterfaceFailureMsg
                    raise myException

            self.openCloseActive = 0
Ejemplo n.º 14
0
		toggle_door = (not toggle_door)
		return redirect(url_for('index'))

@app.route('/refresh/')
def refresh():
	return redirect(url_for('index'))

def run_http():
	app.run(host='0.0.0.0')

# run the HTTP server in a background thread
thread.start_new_thread(run_http, ())

# initialize to the ftdi device
ftdic = ftdi.ftdi_context()
ftdi.ftdi_init(ftdic)
ftdi.ftdi_usb_open(ftdic, 0x0403, 0x6001)
ftdi.ftdi_enable_bitbang(ftdic, RXT | CTS)

while True:
	result = " "
	ftdi.ftdi_read_pins(ftdic, result)

	was_closed = is_closed
	is_closed = 0 != ord(result) & RXT

	if was_closed != is_closed:
		with open ('status', 'w') as f: \
			f.write ('closed' if is_closed else 'open')

	LOG.info("Full: %s\t Masked: %s\t Is Closed: %s\t To Write: %s", \
Ejemplo n.º 15
0
def init_ftdi(log):
   logger = log
   ftdi.ftdi_init(F)
   result = ftdi.ftdi_usb_open(F, 0x0403, 0x6011)
   if result <> 0:
      print "Unable to open ftdi device"