def get_version(handle):
    """This function reads the firmware version from the I2C-to-SWI Bridge."""
    print "Get firmware version of Bridge"
    res = write(handle, array('B', [0x04, 0x00, 0x03]))
    if res <= 0:
        return res
    return read(handle, 5)
def set_wakeup_delay(handle, delay):
    """This function sets the SWI Wakeup delay (us)."""
    delay %= 25500
    print "Set the SWI Wakeup delay to %d us" % (delay)
    res = write(handle, array('B', [0x01, 0x01, 0x00, delay / 100]))
    if res <= 0:
        return res
    return read(handle, 2)
def set_swi_timeout(handle, timeout):
    """This function sets the SWI timeout (ms)."""
    timeout %= 2550
    print "Set the SWI timeout to %d ms" % (timeout)
    res = write(handle, array('B', [0x03, 0x01, 0x00, timeout / 10]))
    if res <= 0:
        return res
    return read(handle, 2)
def read_config(handle):
    """This functions reads data from the SHA204 configuration zone."""
    print "Send Read command to SHA204 and receive its response"
    res = write(
        handle,
        array('B',
              [0x00, 0x07, 0x23, 0x07, 0x02, 0x80, 0x08, 0x00, 0x0A, 0x4D]))
    if res <= 0:
        return res
    return read(handle, 37)
def dev_rev(handle):
    """This function sends a DevRev command to the SHA204 device and receives its response."""
    print "Send DevRev command to SHA204 and receive its response"
    res = write(
        handle,
        array('B',
              [0x00, 0x07, 0x07, 0x07, 0x30, 0x00, 0x00, 0x00, 0x03, 0x5D]))
    if res <= 0:
        return res
    return read(handle, 9)
def nonce(handle):
    """This function sends a Nonce command to the SHA204 device and receives its response."""
    print "Send Nonce command to SHA204 and receive its response"
    res = write(handle, array('B', [0x00, 0x1B, 0x23, \
                              0x1B, 0x16, 0x00, 0x00, 0x00, \
                              0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, \
                              0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, \
                              0x98, 0xC6]))
    if res <= 0:
        return res
    return read(handle, 37)
def mac(handle):
    """This function sends a Mac command to the SHA204 device and receives its response."""
    print "Send Mac command to SHA204 and receive its response"
    res = write(handle, array('B', [0x00, 0x27, 0x23, \
                              0x27, 0x08, 0x00, 0x00, 0x00, \
                              0x3C, 0x37, 0x0E, 0x25, 0x98, 0x43, 0x8E, 0x6C, \
                              0xE9, 0x8A, 0x9B, 0x1E, 0xBD, 0xB3, 0x1E, 0x7D, \
                              0x8C, 0xBD, 0x38, 0xE3, 0x25, 0x06, 0xBB, 0x40, \
                              0x19, 0x68, 0x9C, 0x34, 0x49, 0x8B, 0x1A, 0x97, \
                              0x68, 0x1B]))
    if res <= 0:
        return res
    return read(handle, 37)
def write_config(handle):
    """This functions writes data to the SHA204 configuration zone."""
    print "Send Write command to SHA204 and receive its response"
    res = write(handle, array('B', [0x00, 0x27, 0x04, \
                              0x27, 0x12, 0x80, 0x08, 0x00, \
                              0x86, 0x40, 0x87, 0x07, 0x0F, 0x00, 0x89, 0xF2, \
                              0x8A, 0x7A, 0x0B, 0x8B, 0x0C, 0x4C, 0xDD, 0x4D, \
                              0xC2, 0x42, 0xAF, 0x8F, 0xFF, 0x00, 0xFF, 0x00, \
                              0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, \
                              0x05, 0x2A]))
    if res <= 0:
        return res
    return read(handle, 6)