示例#1
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
    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 guessResetDirection(self, h):
     if (self.resetValue is None or self.nBootValue is None):
         CBUS_2 = 4
         CBUS_0 = 1
         RESET_LINE = CBUS_2
         GPIO5_LINE = CBUS_0
         resetLow = ((RESET_LINE << 4) | 0)
         resetHigh = ((RESET_LINE << 4) | RESET_LINE)
         GPIO5Low = ((GPIO5_LINE << 4) | 0)
         GPIO5High = ((GPIO5_LINE << 4) | GPIO5_LINE)
         h.usb_write_timeout = 1000
         h.usb_read_timeout = 1000
         ret = ftdi.ftdi_set_baudrate(h, 115200)
         ret = (ret + ftdi.ftdi_setflowctrl(h, ftdi.SIO_DISABLE_FLOW_CTRL))
         ret = (ret + ftdi.ftdi_set_line_property(
             h, ftdi.BITS_8, ftdi.STOP_BIT_1, ftdi.NONE))
         for i in range(2):
             if (i % 2) == 0:
                 resetValue = resetHigh
                 nbootValue = GPIO5High
             else:
                 resetValue = resetLow
                 nbootValue = GPIO5Low
             ftdi.ftdi_set_bitmode(h, resetValue, ftdi.BITMODE_CBUS)
             time.sleep(0.10000000000000001)
             ftdi.ftdi_set_bitmode(h, nbootValue, ftdi.BITMODE_CBUS)
             time.sleep(0.10000000000000001)
             ftdi.ftdi_set_bitmode(h, 0, ftdi.BITMODE_CBUS)
             ftdi.ftdi_usb_purge_rx_buffer(h)
             ftdi.ftdi_usb_purge_tx_buffer(h)
             ftdi.ftdi_write_data(h, struct.pack('B', 127), 1)
             startTime = time.time()
             inbuff = '\x00'
             nbyte_rcvd = 0
             while (nbyte_rcvd == 0 and (time.time() - startTime) < 1):
                 nbyte_rcvd = ftdi.ftdi_read_data(h, inbuff, 1)
                 continue
             if nbyte_rcvd > 0:
                 reply = struct.unpack('B', inbuff)
                 if (reply[0]) == 121:
                     self.resetValue = resetValue
                     self.nBootValue = nbootValue
                     break
                 else:
                     continue
             continue
     return (self.resetValue, self.nBootValue)
示例#4
0
	def guessResetDirection(self, h):
		if (self.resetValue is None or self.nBootValue is None):
			CBUS_2 = 4
			CBUS_0 = 1
			RESET_LINE = CBUS_2
			GPIO5_LINE = CBUS_0
			resetLow = ((RESET_LINE << 4) | 0)
			resetHigh = ((RESET_LINE << 4) | RESET_LINE)
			GPIO5Low = ((GPIO5_LINE << 4) | 0)
			GPIO5High = ((GPIO5_LINE << 4) | GPIO5_LINE)
			h.usb_write_timeout = 1000
			h.usb_read_timeout = 1000
			ret = ftdi.ftdi_set_baudrate(h, 115200)
			ret = (ret + ftdi.ftdi_setflowctrl(h, ftdi.SIO_DISABLE_FLOW_CTRL))
			ret = (ret + ftdi.ftdi_set_line_property(h, ftdi.BITS_8, ftdi.STOP_BIT_1, ftdi.NONE))
			for i in range(2):
				if (i % 2) == 0:
					resetValue = resetHigh
					nbootValue = GPIO5High
				else:
					resetValue = resetLow
					nbootValue = GPIO5Low
				ftdi.ftdi_set_bitmode(h, resetValue, ftdi.BITMODE_CBUS)
				time.sleep(0.10000000000000001)
				ftdi.ftdi_set_bitmode(h, nbootValue, ftdi.BITMODE_CBUS)
				time.sleep(0.10000000000000001)
				ftdi.ftdi_set_bitmode(h, 0, ftdi.BITMODE_CBUS)
				ftdi.ftdi_usb_purge_rx_buffer(h)
				ftdi.ftdi_usb_purge_tx_buffer(h)
				ftdi.ftdi_write_data(h, struct.pack('B', 127), 1)
				startTime = time.time()
				inbuff = '\x00'
				nbyte_rcvd = 0
				while (nbyte_rcvd == 0 and (time.time() - startTime) < 1):
					nbyte_rcvd = ftdi.ftdi_read_data(h, inbuff, 1)
					continue
				if nbyte_rcvd > 0:
					reply = struct.unpack('B', inbuff)
					if (reply[0]) == 121:
						self.resetValue = resetValue
						self.nBootValue = nbootValue
						break
					else:
						continue
				continue
		return (self.resetValue, self.nBootValue)