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
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
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)))
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
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
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
#!/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)