Host-side library reference

class fx2.FX2Config(vendor_id=1204, product_id=34323, device_id=0, disconnect=False, i2c_400khz=False)[source]

Cypress FX2 EEPROM configuration data.

vendor_idint

USB vendor ID, 16 bits.

product_idint

USB product ID, 16 bits.

device_idint

USB device ID, 16 bits in binary-coded decimal.

disconnectbool

If True, do not enumerate on startup. If False, enumerate as a default FX2 device with specified VID/PID/DID.

i2c_400khzbool

If True, use 400 kHz I2C clock to read firmware from EEPROM. If False, use 100 kHz clock.

firmwarelist

Firmware to be loaded into on-chip program/data RAM. If empty, “C0 load” is used; if not, “C2 load” is used, and the command to bring the CPU out of reset is inserted automatically.

append(addr, chunk)[source]

Append a command to load chunk at addr into on-chip program/data RAM on device startup.

encode(max_size=None)[source]

Convert configuration to an image that can be loaded into an EEPROM.

Returns the EEPROM image if the resulting configuration is smaller than max_size, or raises ValueError if it is not.

classmethod decode(data, partial=False)[source]

Parse configuration from an image loaded from an EEPROM.

Returns None if the EEPROM image is empty (the EEPROM was erased), FX2Config if it contains a valid configuration, or raises ValueError if it does not.

If partial is True, only requires any data records present to be complete; if it is False, it is an error unless the image contains the final data record.

class fx2.FX2Device(vendor_id=1204, product_id=34323)[source]

A Cypress FX2 series device.

The initializer of this class locates the device by provided VID:PID pair, or raises a FX2DeviceError.

usbusb1.USBDeviceHandle

Raw USB device handle.

control_read(request_type, request, value, index, length, timeout=None)[source]

Issue an USB control read request with timeout defaulting to self.timeout.

control_write(request_type, request, value, index, data, timeout=None)[source]

Issue an USB control write request with timeout defaulting to self.timeout.

bulk_read(endpoint, length, timeout=None)[source]

Issue an USB bulk read request with timeout defaulting to self.timeout.

bulk_write(endpoint, data, timeout=None)[source]

Issue an USB bulk write request with timeout defaulting to self.timeout.

read_ram(addr, length)[source]

Read length bytes at addr from internal RAM. Note that not all memory can be addressed this way; consult the TRM.

write_ram(addr, data)[source]

Write data to addr to internal RAM. Note that not all memory can be addressed this way; consult the TRM.

load_ram(chunks)[source]

Write chunks, a list of (address, data) pairs, to internal RAM, and start the CPU core. See also write_ram.

cpu_reset(is_reset)[source]

Bring CPU in or out of reset.

read_boot_eeprom(addr, length, addr_width, chunk_size=256)[source]

Read length bytes at addr from boot EEPROM in chunk_size chunks.

Requires the second stage bootloader.

write_boot_eeprom(addr, data, addr_width, chunk_size=16, page_size=0)[source]

Write data to addr in boot EEPROM that has 2 ** page_size byte pages in chunk_size chunks.

Writing EEPROM is much slower than reading; for best performance, specify page_size per EEPROM datasheet, and set chunk_size to a small multiple of 2 ** page_size. Otherwise, timeouts may occur.

Requires the second stage bootloader or a compatible firmware.

reenumerate()[source]

Trigger re-enumeration.

Requires the second stage bootloader or a compatible firmware.

exception fx2.FX2DeviceError[source]

An exception raised on a communication error.

fx2.format.autodetect(file)[source]

Autodetect file format based on properties of a given file object.

Returns “ihex” for .hex, .ihex and .ihx file extensions, “bin” for .bin file extension, “hex” if file is a TTY, and raises ValueError otherwise.

fx2.format.input_data(file_or_data, fmt='auto', offset=0)[source]

Read Intel HEX, hexadecimal, or binary data from file_or_data. If file_or_data is a string, it is treated as hexadecimal. Otherwise, the format is determined by the fmt argument.

Raises ValueError if the input data has invalid format.

Returns a list of (address, data) chunks.

Parameters
  • fmt"ihex" for Intel HEX, "hex" for hexadecimal, "bin" for binary, or "auto" for autodetection via autodetect().

  • offset – Offset the data by specified amount of bytes.

fx2.format.output_data(file, data, fmt='auto', offset=0)[source]

Write Intel HEX, hexadecimal, or binary data to file.

Parameters
  • data – A byte array (bytes, bytearray and list of (addr, chunk) pairs are all valid).

  • fmt"ihex" for Intel HEX, "hex" for hexadecimal, "bin" for binary, or "auto" for autodetection via autodetect().

  • offset – Offset the data by specified amount of bytes.

fx2.format.flatten_data(data, *, fill=0)[source]

Flatten a list of (addr, chunk) pairs, such as that returned by input_data(), to a flat byte array, such as that accepted by output_data().

fx2.format.diff_data(old, new)[source]

Compute the difference between old and new byte arrays, and return a list of (addr, chunk) pairs containing only the bytes that are changed between new and old.