How to use the pyftdi.bits.BitSequence function in pyftdi

To help you get started, we’ve selected a few pyftdi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eblot / pyftdi / pyftdi / jtag.py View on Github external
def _read_bits(self, length: int):
        """Read out bits from TDO"""
        if length > 8:
            raise JtagError("Cannot fit into FTDI fifo")
        cmd = bytearray((Ftdi.READ_BITS_NVE_LSB, length-1))
        self._stack_cmd(cmd)
        self.sync()
        data = self._ftdi.read_data_bytes(1, 4)
        # need to shift bits as they are shifted in from the MSB in FTDI
        byte = data[0] >> 8-length
        bs = BitSequence(byte, length=length)
        # print("READ BITS %s" % bs)
        return bs
github eblot / pyftdi / examples / jtag / arm.py View on Github external
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL NEOTION BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from pyftdi.jtag import JtagEngine
from pyftdi.bits import BitSequence
from six import print_

# ARM 926
JTAG_INSTR = {'EXTEST'  : BitSequence('0000', msb=True, length=4),
              'SAMPLE'  : BitSequence('0011', msb=True, length=4),
              'PRELOAD' : BitSequence('0011', msb=True, length=4),
              'SCAN_N'  : BitSequence('0010', msb=True, length=4),
              'INTEST'  : BitSequence('1100', msb=True, length=4),
              'IDCODE'  : BitSequence('1110', msb=True, length=4),
              'BYPASS'  : BitSequence('1111', msb=True, length=4),
              'RESTART' : BitSequence('0100', msb=True, length=4)}


class ArmJtag(object):
    """JTAG helper for ARM core"""

    def __init__(self, vendor, product, interface):
        self.jtag = JtagEngine()
        self.jtag.configure(vendor, product, interface)
        self.jtag.reset()
github eblot / pyftdi / pyftdi / jtag.py View on Github external
# do both for now
        if not self._ftdi.is_connected:
            raise JtagError("FTDI controller terminated")
        if self._trst:
            # nTRST
            value = 0
            cmd = bytearray((Ftdi.SET_BITS_LOW, value, self.direction))
            self._ftdi.write_data(cmd)
            sleep(0.1)
            # nTRST should be left to the high state
            value = JtagController.TRST_BIT
            cmd = bytearray((Ftdi.SET_BITS_LOW, value, self.direction))
            self._ftdi.write_data(cmd)
            sleep(0.1)
        # TAP reset (even with HW reset, could be removed though)
        self.write_tms(BitSequence('11111'))
        if sync:
            self.sync()
github eblot / pyftdi / pyftdi / jtag.py View on Github external
def write(self, out: Union[BitSequence, str], use_last: bool = True):
        """Write a sequence of bits to TDI"""
        if isinstance(out, str):
            if len(out) > 1:
                self._write_bytes_raw(out[:-1])
                out = out[-1]
            out = BitSequence(bytes_=out)
        elif not isinstance(out, BitSequence):
            out = BitSequence(out)
        if use_last:
            (out, self._last) = (out[:-1], bool(out[-1]))
        byte_count = len(out)//8
        pos = 8*byte_count
        bit_count = len(out)-pos
        if byte_count:
            self._write_bytes(out[:pos])
        if bit_count:
            self._write_bits(out[pos:])
github eblot / pyftdi / pyftdi / jtag.py View on Github external
# Freely inpired from UrJTAG
        stm = self._engine.state_machine
        if not stm.state_of('shift'):
            raise JtagError("Invalid state: %s" % stm.state())
        if stm.state_of('capture'):
            bs = BitSequence(False)
            self._engine.controller.write_tms(bs)
            stm.handle_events(bs)
        MAX_REG_LEN = 1024
        PATTERN_LEN = 8
        stuck = None
        for length in range(1, MAX_REG_LEN):
            print("Testing for length %d" % length)
            if length > 5:
                return
            zero = BitSequence(length=length)
            inj = BitSequence(length=length+PATTERN_LEN)
            inj.inc()
            ok = False
            for p in range(1, 1 << PATTERN_LEN):
                ok = False
                self._engine.write(zero, False)
                rcv = self._engine.shift_register(inj)
                try:
                    tdo = rcv.invariant()
                except ValueError:
                    tdo = None
                if stuck is None:
                    stuck = tdo
                if stuck != tdo:
                    stuck = None
                rcv >>= length
github eblot / pyftdi / pyftdi / bits.py View on Github external
def __init__(self, value=None, msb=False, length=0):
        BitSequence.__init__(self, value=value, msb=msb, length=length)
github eblot / pyftdi / examples / jtag / arm.py View on Github external
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from pyftdi.jtag import JtagEngine
from pyftdi.bits import BitSequence
from six import print_

# ARM 926
JTAG_INSTR = {'EXTEST'  : BitSequence('0000', msb=True, length=4),
              'SAMPLE'  : BitSequence('0011', msb=True, length=4),
              'PRELOAD' : BitSequence('0011', msb=True, length=4),
              'SCAN_N'  : BitSequence('0010', msb=True, length=4),
              'INTEST'  : BitSequence('1100', msb=True, length=4),
              'IDCODE'  : BitSequence('1110', msb=True, length=4),
              'BYPASS'  : BitSequence('1111', msb=True, length=4),
              'RESTART' : BitSequence('0100', msb=True, length=4)}


class ArmJtag(object):
    """JTAG helper for ARM core"""

    def __init__(self, vendor, product, interface):
        self.jtag = JtagEngine()
        self.jtag.configure(vendor, product, interface)
        self.jtag.reset()

    def get_idcode_from_reset(self):
        """Read the IDCODE right after a JTAG reset"""
        idcode = self.jtag.read_dr(32)
        self.jtag.go_idle()
        print_("IDCODE: 0x%x" % int(idcode))
        return int(idcode)
github eblot / pyftdi / examples / jtag / arm.py View on Github external
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from pyftdi.jtag import JtagEngine
from pyftdi.bits import BitSequence
from six import print_

# ARM 926
JTAG_INSTR = {'EXTEST'  : BitSequence('0000', msb=True, length=4),
              'SAMPLE'  : BitSequence('0011', msb=True, length=4),
              'PRELOAD' : BitSequence('0011', msb=True, length=4),
              'SCAN_N'  : BitSequence('0010', msb=True, length=4),
              'INTEST'  : BitSequence('1100', msb=True, length=4),
              'IDCODE'  : BitSequence('1110', msb=True, length=4),
              'BYPASS'  : BitSequence('1111', msb=True, length=4),
              'RESTART' : BitSequence('0100', msb=True, length=4)}


class ArmJtag(object):
    """JTAG helper for ARM core"""

    def __init__(self, vendor, product, interface):
        self.jtag = JtagEngine()
        self.jtag.configure(vendor, product, interface)
        self.jtag.reset()

    def get_idcode_from_reset(self):
        """Read the IDCODE right after a JTAG reset"""
        idcode = self.jtag.read_dr(32)
        self.jtag.go_idle()
        print_("IDCODE: 0x%x" % int(idcode))
github eblot / pyftdi / pyftdi / jtag.py View on Github external
def write_tms(self, tms: BitSequence) -> None:
        """Change the TAP controller state"""
        if not isinstance(tms, BitSequence):
            raise JtagError('Expect a BitSequence')
        length = len(tms)
        if not 0 < length < 8:
            raise JtagError('Invalid TMS length')
        out = BitSequence(tms, length=8)
        # apply the last TDO bit
        if self._last is not None:
            out[7] = self._last
        # print("TMS", tms, (self._last is not None) and 'w/ Last' or '')
        # reset last bit
        self._last = None
        cmd = bytearray((Ftdi.WRITE_BITS_TMS_NVE, length-1, out.tobyte()))
        self._stack_cmd(cmd)
        self.sync()
github eblot / pyftdi / pyftdi / jtag.py View on Github external
def get_events(self, path):
        """Build up an event sequence from a state sequence, so that the
           resulting event sequence allows the JTAG state machine to advance
           from the first state to the last one of the input sequence"""
        events = []
        for s, d in zip(path[:-1], path[1:]):
            for e, x in enumerate(s.exits):
                if x == d:
                    events.append(e)
        if len(events) != len(path) - 1:
            raise JtagError("Invalid path")
        return BitSequence(events)