eblot / pyftdi

FTDI device driver written in pure Python
Other
509 stars 213 forks source link

Confusing error reporting on hardware disconnect #120

Open mcondarelli opened 5 years ago

mcondarelli commented 5 years ago

I found I can get two different errors (UsbToolsError or USBError) in essentially the same place, when a USB disconnect occurs.

Underlying (hardware) problem is MiniUSB connector on FT4232 module is not vibration resistant; manually wiggling the cable or the mechanical vibrations induced by clamp fastening board to nail-bed is enough to cause USB disconnection. I can ascertain this as lsusb reports a different (increasing) Device number.

I currently have a "complex" initialization structure which opens ftdi and then tries to read a few parts of the flash in order to understand if and how it was initialized.

Apparently (I cannot test now because I have no access to hardware) if disconnect happens before re-opening spi: i.e.:

            self._spi = pyftdi_spi.SpiController()
            self._spi.configure(self.url, cs_count=1)
            self._chn = self.spi.get_port(0)

I get a UsbToolsError, on the contrary I get the USBError if it happens while reading:

    def read_block(self, addr, count):
        cmd = pack('BBBB', self.READ, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff)
        return self.chn.exchange(cmd, count)

    def read(self, addr, count):
        ret = bytearray()
        while count > 0:
            u = count if count < self.channel.spi.PAYLOAD_MAX_LENGTH else self.channel.spi.PAYLOAD_MAX_LENGTH
            t = self.read_block(addr, u)
            ret += t
            addr += u
            count -= u
        return ret

    self.param = bytes(self.read(0x40000, 512))

Note: I have no problem in sending the whole program, if deemed useful.

Right now I somewhat trapped the errors by closing the SPI interface at end of programming (self._spi.terminate()), resetting USB (UsbTools.flush_cache()) when all boards (4) are programmed and then re-opening everything on next cycle; If you need some debugging on this I can revert to previous behavior and send error traces (probably not before next weekend, though).

Originally posted by @mcondarelli in https://github.com/eblot/pyftdi/issues/119#issuecomment-458079943

mcuee commented 3 years ago

Take note pyusb does not support hotplug yet. https://github.com/pyusb/pyusb/pull/160