예제 #1
0
def _eeprom_spi_write_byte(
    spi: busio.SPI,
    csel: digitalio.DigitalInOut,
    address: int,
    data: int,
    timeout: float = 1.0,
) -> bool:

    # Make sure address is only one byte:
    if address > 255:
        return False

    # Make sure data is only one byte:
    if data > 255:
        return False

    # Wait for WIP to be low
    if not _eeprom_spi_wait(spi, csel, timeout):
        return False

    # Enable writing
    csel.value = False
    spi.write(bytearray([EEPROM_SPI_WREN]))
    csel.value = True

    # Write to address
    csel.value = False
    spi.write(bytearray([EEPROM_SPI_WRITE, address, data]))
    csel.value = True

    return True
예제 #2
0
def _eeprom_spi_wait(
    spi: busio.SPI, csel: digitalio.DigitalInOut, timeout: float = 1.0
) -> bool:

    # Continually read from STATUS register
    timestamp = time.monotonic()
    while time.monotonic() < timestamp + timeout:

        # Perfrom RDSR operation
        csel.value = False
        result = bytearray(1)
        spi.write(bytearray([EEPROM_SPI_RDSR]))
        spi.readinto(result)
        csel.value = True

        # Mask out and compare WIP bit
        if (result[0] & (1 << EEPROM_SPI_WIP_BIT)) == 0:
            return True

    return False
예제 #3
0
def _eeprom_spi_read_byte(
    spi: busio.SPI, csel: digitalio.DigitalInOut, address: int, timeout: float = 1.0
) -> Tuple[bool, bytearray]:

    # Make sure address is only one byte:
    if address > 255:
        return False, bytearray()

    # Wait for WIP to be low
    if not _eeprom_spi_wait(spi, csel, timeout):
        return False, bytearray()

    # Read byte from address
    csel.value = False
    result = bytearray(1)
    spi.write(bytearray([EEPROM_SPI_READ, address]))
    spi.readinto(result)
    csel.value = True

    return True, result