def h2(handle):
    global x, y, ch_ind

    def set_channel_and_coordinates(coord, ch):
        global x, y, ch_ind
        ch_ind = ch
        coord = (coord >> 24) & 0xff
        x = coord >> 4
        y = coord & 0x0f

    for i in range(0, 64):
        if (not ch_ind == None) and (not ch_ind == i):
            continue
        msg_i = rf.read_bytes_and_decrypt(handle, 2 * i, 3)
        rf.encrypt_and_write_bytes(handle, 2 * i + 1, 4, msg_i)
        received_ack = rf.read_bytes_and_decrypt(handle, 2 * i, 3)
        if received_ack == ack1:
            # Genuine controller at this index
            print("Genuine controller at channel " + hex(2 * i))
            set_channel_and_coordinates(msg_i, i)
            break
        else:
            print("Didn't get ack on channel " + hex(2 * i))
            print("Waiting for 5 seconds and retrying ...")
            fl.flSleep(5000)
            received_ack = rf.read_bytes_and_decrypt(handle, 2 * i, 5)
            if received_ack == ack1:
                print("Genuine controller at channel " + hex(2 * i))
                set_channel_and_coordinates(msg_i, i)
                break
            else:
                print("No controller here. Continuing ...")
                continue
Exemple #2
0
def read_bytes_and_decrypt(handle, channel, to):

    byte_buffer = [None] * 4
    byte_count = 0
    current_time = time.time()
    time_out = False
    not_zero_det = False

    while not not_zero_det:
        if time.time() - current_time > to:
            time_out = True
            break
        else:
            num = fl.flReadChannel(handle, channel, 1)
            fl.flSleep(50)
            if not num == 0:
                byte_buffer[byte_count] = num
                byte_count += 1
                while byte_count < 4:
                    num = fl.flReadChannel(handle, channel, 1)
                    byte_buffer[byte_count] = num
                    fl.flSleep(50)
                    byte_count += 1
                not_zero_det = True

    if not time_out:
        byte_buffer.reverse()
        c = 0
        for read_byte in byte_buffer:
            c = c << 8 | read_byte
        p = decrypt(c)
        return p
    else:
        return False
def read_bytes_and_decrypt(handle, channel, to):

    # Like a circular queue. Will keep
    # reading and storing data into the
    # till 4 bytes of data is recieved.
    # Then decryption starts.
    byte_buffer = [None] * 4
    byte_count = 0
    current_time = time.time()
    time_out = True

    while byte_count < 4:

        fl.flReadChannelAsyncSubmit(handle, channel, 1)
        fl.flSleep(50)
        num = ord(fl.flReadChannelAsyncAwait(handle))
        # Based on the assumption that the return
        # value of flReadChannelAsyncAwait is a
        # binary string (b'12')
        byte_buffer[byte_count] = num
        print num
        byte_count += 1
        fl.flSleep(50)

    if time_out:
        byte_buffer.reverse()
        c = 0
        for read_byte in byte_buffer:
            c = c << 8 | read_byte
        p = decrypt(c)
        print p
        return p
    else:
        return False
Exemple #4
0
def encrypt_and_write_bytes(handle, channel, numBytes, data):
    send = []
    p = encrypt(data)
    for i in range(numBytes):
        eight_bits = p & 0xff
        send.append(eight_bits)
        p = p >> 8

    for num in send:
        fl.flWriteChannel(handle, channel, num)
        fl.flSleep(50)
Exemple #5
0
# Read the JEDEC ID from the flash chip on the Lattice IceBlink40 board. This
# example demonstrates the usage of the bit-level I/O and SPI functions. The
# board has MISO=PB3, MOSI=PB2, SS=PB0, SCK=PB1, CRESET=PB6 & POWER=C2.
#
import fl

VID_PID = "1D50:602B:0001"
PROG_CONFIG = "B3B2B0B1"
handle = fl.FLHandle()
try:
    fl.flInitialise(0)

    # Connect, reset the board, open SPI interface & get SS port:
    handle = fl.flOpen(VID_PID)
    fl.flMultiBitPortAccess(handle, "B6-,C2-")  # RESET low & cut the power
    fl.flSleep(10)
    fl.flSingleBitPortAccess(handle, 2, 2, fl.PIN_HIGH)  # power on in RESET
    fl.progOpen(handle, PROG_CONFIG)
    (port, bit) = fl.progGetPort(handle, fl.LP_SS)
    fl.flSingleBitPortAccess(handle, port, bit, fl.PIN_HIGH)
    
    # Send JEDEC device-id command, retrieve three bytes back
    fl.flSingleBitPortAccess(handle, port, bit, fl.PIN_LOW)
    fl.spiSend(handle, b"\x9F", fl.SPI_MSBFIRST)
    bs = fl.spiRecv(handle, 3, fl.SPI_MSBFIRST)
    print("JEDEC ID: {}".format(
        " ".join(["{:02X}".format(b) for b in bs])))
    fl.flSingleBitPortAccess(handle, port, bit, fl.PIN_HIGH)
    
    # Close SPI interface, release reset and close connection
    fl.progClose(handle)
try:
    fl.flInitialise(0)

    if ( not fl.flIsDeviceAvailable(VID_PID) ):
        print("Loading firmware...")
        fl.flLoadStandardFirmware("04b4:8613", VID_PID)

        print("Awaiting...")
        fl.flAwaitDevice(VID_PID, 600)

    conn = fl.flOpen(VID_PID)

    fl.flSingleBitPortAccess(conn, 3, 7, fl.PIN_LOW)
    print("flMultiBitPortAccess() returned {:032b}".format(
        fl.flMultiBitPortAccess(conn, "D7+")))
    fl.flSleep(100)

    print("flGetFirmwareID(): {:04X}".format(fl.flGetFirmwareID(conn)))
    print("flGetFirmwareVersion(): {:08X}".format(fl.flGetFirmwareVersion(conn)))
    print("flIsNeroCapable(): {}".format(fl.flIsNeroCapable(conn)))
    print("flIsCommCapable(): {}".format(fl.flIsCommCapable(conn, CONDUIT)))

    fl.progOpen(conn, PROG_CONFIG)
    print("progGetPort(): {")
    (port, bit) = fl.progGetPort(conn, fl.LP_MISO)
    print("  MISO: {:c}{}".format(ord('A')+port, bit))
    (port, bit) = fl.progGetPort(conn, fl.LP_MOSI)
    print("  MOSI: {:c}{}".format(ord('A')+port, bit))
    (port, bit) = fl.progGetPort(conn, fl.LP_SS)
    print("  SS:   {:c}{}".format(ord('A')+port, bit))
    (port, bit) = fl.progGetPort(conn, fl.LP_SCK)