CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyftdi

Pure Python FTDI device driver for USB-to-serial/GPIO/SPI/I2C/JTAG bridge devices

Pending
Overview
Eval results
Files

core-ftdi.mddocs/

Core FTDI Device Management

Low-level FTDI device access and configuration for direct hardware communication. This module provides the foundation for all protocol-specific controllers and enables custom protocol implementation.

Capabilities

Device Connection Management

Open, configure, and close connections to FTDI devices with support for multiple interfaces and communication modes.

class Ftdi:
    def open(self, device, interface=1, direction=None, **kwargs):
        """
        Open connection to FTDI device.
        
        Parameters:
        - device: Device URL, UsbDevice, or device descriptor
        - interface: FTDI interface number (1-4 depending on device)
        - direction: Data direction (None for bidirectional)
        - frequency: Clock frequency for MPSSE mode
        - latency: USB latency timer (1-255ms)
        
        Raises:
        - FtdiError: Device connection failed
        - FtdiFeatureError: Requested feature not supported
        """
    
    def close(self):
        """Close device connection and release resources."""
    
    def is_connected(self) -> bool:
        """Check if device is connected."""

Data Transfer

Direct data read/write operations for custom protocol implementation.

def read_data(self, size: int) -> bytes:
    """
    Read data from FTDI device.
    
    Parameters:
    - size: Number of bytes to read
    
    Returns:
    bytes: Data received from device
    
    Raises:
    - FtdiError: Read operation failed
    """

def write_data(self, data: bytes) -> int:
    """
    Write data to FTDI device.
    
    Parameters:
    - data: Data bytes to write
    
    Returns:
    int: Number of bytes written
    
    Raises:
    - FtdiError: Write operation failed
    """

def read_data_bytes(self, size: int, attempt: int = 1) -> bytes:
    """Read exact number of bytes with retry logic."""

def write_data_set_chunksize(self, chunksize: int):
    """Set write chunk size for large transfers."""

def read_data_set_chunksize(self, chunksize: int):
    """Set read chunk size for large transfers."""

Device Configuration

Configure communication parameters, timing, and device behavior.

def set_baudrate(self, baudrate: int):
    """
    Set device baudrate for UART mode.
    
    Parameters:
    - baudrate: Baudrate in bits per second (300-12000000)
    
    Raises:
    - FtdiError: Invalid baudrate or configuration failed
    """

def set_line_property(self, bits: int, stopbits: int, parity: str, break_: bool = False):
    """
    Set UART line properties.
    
    Parameters:
    - bits: Data bits (7 or 8)
    - stopbits: Stop bits (1 or 2)
    - parity: Parity ('N', 'E', 'O', 'M', 'S')
    - break_: Send break condition
    """

def set_flowctrl(self, flowctrl: str):
    """
    Set flow control mode.
    
    Parameters:
    - flowctrl: Flow control ('', 'hw', 'sw')
    """

Control Lines

Manage DTR, RTS, CTS, DSR control lines for UART communication.

def set_dtr_rts(self, dtr: bool, rts: bool):
    """Set DTR and RTS line states simultaneously."""

def set_dtr(self, state: bool):
    """Set DTR (Data Terminal Ready) line state."""

def set_rts(self, state: bool):
    """Set RTS (Request To Send) line state."""

def get_cts(self) -> bool:
    """Get CTS (Clear To Send) line state."""

def get_dsr(self) -> bool:
    """Get DSR (Data Set Ready) line state."""

def enable_dtr_rts(self):
    """Enable DTR/RTS lines."""

def disable_dtr_rts(self):
    """Disable DTR/RTS lines."""

Buffer Management

Control device buffers and data flow for optimal performance.

def flush(self):
    """Flush both TX and RX buffers."""

def purge_rx_buffer(self):
    """Purge receive buffer."""

def purge_tx_buffer(self):
    """Purge transmit buffer."""

def reset_tx_buffer(self):
    """Reset transmit buffer."""

def reset_rx_buffer(self):
    """Reset receive buffer."""

Timing Control

Configure latency and timing parameters for optimal performance.

def set_latency_timer(self, latency: int):
    """
    Set USB latency timer.
    
    Parameters:
    - latency: Latency in milliseconds (1-255)
    """

def get_latency_timer(self) -> int:
    """Get current USB latency timer value."""

def set_write_timeout(self, timeout: int):
    """Set write timeout in milliseconds."""

def set_read_timeout(self, timeout: int):
    """Set read timeout in milliseconds."""

Device Reset and Recovery

Reset device state and recover from error conditions.

def reset(self):
    """Reset FTDI device to initial state."""

def reset_device(self):
    """Perform USB device reset."""

def get_error_string(self) -> str:
    """Get description of last error."""

Device Information

Query device capabilities, status, and identification information.

def device_name(self) -> str:
    """Get FTDI device name (e.g., 'ft232h', 'ft2232h')."""

def device_version(self) -> int:
    """Get device version identifier."""

def has_wide_port(self) -> bool:
    """Check if device supports 16-bit MPSSE mode."""

def has_drivezero_mode(self) -> bool:
    """Check if device supports drive-zero mode."""

def mpsse_bit_delay(self, frequency: float) -> int:
    """Calculate bit delay for given MPSSE frequency."""

def validate_mpsse(self):
    """Validate MPSSE mode availability and configuration."""

Device Constants

class Ftdi:
    # URL scheme
    SCHEME = 'ftdi'
    
    # Vendor/Product IDs  
    FTDI_VENDOR = 0x403
    VENDOR_IDS = {'ftdi': 0x403}
    DEFAULT_VENDOR = 0x403
    
    # Supported devices
    PRODUCT_IDS = {
        0x403: {
            'ft232h': 0x6014,
            'ft2232h': 0x6010, 
            'ft4232h': 0x6011,
            'ft232r': 0x6001,
            'ft230x': 0x6015,
            # ... additional device mappings
        }
    }
    
    # Device capabilities
    DEVICE_NAMES = {
        0x0500: 'ft2232c',
        0x0700: 'ft232r', 
        0x0800: 'ft2232h',
        0x0900: 'ft4232h',
        0x1000: 'ft232h',
        # ... additional device mappings
    }

Usage Examples

Basic Device Connection

from pyftdi.ftdi import Ftdi

# Open device
ftdi = Ftdi()
ftdi.open('ftdi:///1')  # First available device, interface 1

# Configure for custom protocol
ftdi.set_baudrate(115200)
ftdi.reset()

# Direct data exchange
ftdi.write_data(b'\x01\x02\x03')
response = ftdi.read_data(10)

# Clean up
ftdi.close()

Custom Protocol Implementation

from pyftdi.ftdi import Ftdi

class CustomProtocol:
    def __init__(self, url):
        self.ftdi = Ftdi()
        self.ftdi.open(url)
        self.ftdi.reset()
        
    def send_command(self, cmd, data=b''):
        # Custom protocol frame
        frame = bytes([cmd, len(data)]) + data
        self.ftdi.write_data(frame)
        
        # Read response
        header = self.ftdi.read_data(2)
        if header:
            data_len = header[1]
            return self.ftdi.read_data(data_len)
        return b''
    
    def close(self):
        self.ftdi.close()

Exception Handling

from pyftdi.ftdi import Ftdi, FtdiError, FtdiFeatureError

try:
    ftdi = Ftdi()
    ftdi.open('ftdi://0x403:0x6014/1')
    # Device operations...
except FtdiFeatureError as e:
    print(f"Feature not supported: {e}")
except FtdiError as e:
    print(f"FTDI error: {e}")
finally:
    if ftdi.is_connected():
        ftdi.close()

Install with Tessl CLI

npx tessl i tessl/pypi-pyftdi

docs

core-ftdi.md

eeprom.md

gpio.md

i2c.md

index.md

jtag.md

serial.md

spi.md

usb-tools.md

tile.json