Skip to main content
Control the power state of USB devices and ports on your testbed using USB hubs with per-port power control.

Import

There are two ways to control USB ports:
# Option 1: Module-level functions
from lager.usb_hub import enable, disable, toggle

# Option 2: Net-based API
from lager import Net, NetType

Module Functions

FunctionDescription
enable(net_name)Enable (power on) USB port
disable(net_name)Disable (power off) USB port
toggle(net_name)Toggle USB port power state

Exception Classes

ExceptionDescription
USBBackendErrorBase class for USB hub errors
LibraryMissingErrorRequired vendor SDK not installed
DeviceNotFoundErrorUSB hub not found
PortStateErrorError changing port state

Function Reference

enable(net_name)

Enable (power on) a USB port.
from lager.usb_hub import enable

enable('CAMERA_USB')
print("USB port powered on")
Parameters:
ParameterTypeDescription
net_namestrName of the USB net

disable(net_name)

Disable (power off) a USB port.
from lager.usb_hub import disable

disable('CAMERA_USB')
print("USB port powered off")
Parameters:
ParameterTypeDescription
net_namestrName of the USB net

toggle(net_name)

Toggle the power state of a USB port.
from lager.usb_hub import toggle

toggle('SENSOR_USB')  # On -> Off or Off -> On
Parameters:
ParameterTypeDescription
net_namestrName of the USB net

Net-Based API

You can also use the Net abstraction for USB control:
from lager import Net, NetType

usb = Net.get('DUT_USB', type=NetType.Usb)
usb.enable()   # Power on
usb.disable()  # Power off

Examples

Basic Power Control

from lager.usb_hub import enable, disable

# Power on USB device
enable('CAMERA')
print("Camera powered on")

# Power off USB device
disable('CAMERA')
print("Camera powered off")

Power Cycle Device

from lager.usb_hub import disable, enable
import time

def power_cycle(net_name, delay=2):
    """Power cycle a USB device."""
    print(f"Power cycling {net_name}...")
    disable(net_name)
    time.sleep(delay)
    enable(net_name)
    print(f"{net_name} restarted")

power_cycle('DUT_USB')

Error Handling

from lager.usb_hub import enable, disable
from lager.usb_hub.usb_net import (
    USBBackendError,
    LibraryMissingError,
    DeviceNotFoundError,
    PortStateError
)

try:
    enable('SENSOR_USB')
    print("Sensor powered on")

except LibraryMissingError:
    print("USB hub SDK not installed")

except DeviceNotFoundError:
    print("USB hub not found - check connection")

except PortStateError as e:
    print(f"Port error: {e}")

except USBBackendError as e:
    print(f"USB error: {e}")

Automated Test Setup

from lager.usb_hub import enable, disable
from lager import Net, NetType
import time

def setup_test():
    """Power on all USB peripherals for testing."""
    usb_devices = ['PROGRAMMER', 'SENSOR', 'DEBUGGER']

    for name in usb_devices:
        try:
            enable(name)
            print(f"{name} powered on")
        except Exception as e:
            print(f"Warning: {name} - {e}")

    time.sleep(1)  # Wait for USB enumeration

    # Enable main power
    psu = Net.get('VDD', type=NetType.PowerSupply)
    psu.set_voltage(3.3)
    psu.enable()

    return True

def teardown_test():
    """Power off all USB peripherals."""
    # Disable main power first
    psu = Net.get('VDD', type=NetType.PowerSupply)
    psu.disable()

    # Power off USB peripherals
    for name in ['PROGRAMMER', 'SENSOR', 'DEBUGGER']:
        try:
            disable(name)
        except Exception:
            pass

USB Device Reset

from lager.usb_hub import disable, enable
import time

def reset_usb_device(net_name, reset_time=2, settle_time=3):
    """
    Reset a USB device by power cycling.

    Args:
        net_name: USB net name
        reset_time: Time to keep power off (seconds)
        settle_time: Time to wait after power on (seconds)
    """
    print(f"Resetting {net_name}...")

    # Power off
    disable(net_name)
    print(f"  Power off for {reset_time}s")
    time.sleep(reset_time)

    # Power on
    enable(net_name)
    print(f"  Power on, waiting {settle_time}s for enumeration")
    time.sleep(settle_time)

    print(f"  {net_name} reset complete")

# Usage
reset_usb_device('DUT_USB', reset_time=2, settle_time=5)

Toggle for Quick State Change

from lager.usb_hub import toggle
import time

# Quick on/off cycle using toggle
for i in range(5):
    toggle('LED_USB')
    time.sleep(0.5)

Supported Hardware

HardwareFeatures
Acroname BrainStem USB HubIndividual port power control, current monitoring
YKUSH USB HubPer-port power switching

Notes

  • USB nets must be configured on the Lager Box with hub serial number and port mapping
  • Power state changes take effect immediately
  • Allow time for USB enumeration after powering on (~1-3 seconds)
  • Power cycling can be useful for device reset/recovery
  • The toggle() function is useful for quick state changes
  • Use exception handling for robust error recovery