Skip to content

ST-LINK Example

This example showcases the interactions between a HIL gateway and a STM32 microcontroller using the ST-LINK interface.

It consists of a single file:

test_st_link_interface.py - Contains a test that programs a STM32L476RG with embedops-dev-demo.bin firmware, which can be built by following the EmbedOps CLI Quickstart, starts execution of the firmware, and erases the device.

import os
from hil_sdk.interfaces.st_link_interface import STLinkInterface

def test_st_link_interface(hil_extras_get_path):
    """
    Simple example demonstrating ST-LINK interface usage:
    1. Program the STM32L476RG with embedops-dev-demo.bin firmware
    2. Start execution of the firmware
    3. Erase the device
    """

    # Note: This is a binary file can be built by following the EmbedOps CLI Quickstart (https://docs.embedops.io/quickstarts/embedops_cli_quickstart/).
    # If you have a different binary you want to test with, change the binary file path accordingly.
    # The binary file can be passed to the HIL gateway via the .embedops/hil/config.yml file (https://docs.embedops.io/hil/reference/yaml_config/).
    bin_file = "embedops-dev-demo.bin"
    # Assuming the binary is in the "build" directory
    bin_file_path = hil_extras_get_path(f"build/{bin_file}")
    assert os.path.isfile(bin_file_path), f"Binary file not found: {bin_file_path}"

    # Create ST-LINK interface instance
    st_link = STLinkInterface()

    # Step 1: Program the device
    print(f"\n1. Programming device with {bin_file} firmware...")
    success = st_link.download(bin_file_path, start_address=0x08000000, logfile="program_log.txt")

    if success:
        print("✓ Programming successful")
    else:
        print("✗ Programming failed - check program_log.txt for details")
        return 1

    # Step 2: Start execution
    print("\n2. Starting execution...")
    success = st_link.start(start_address=0x08000000, logfile="reset_log.txt")

    if success:
        print(f"✓ Start execution successful - {bin_file} should be running")
    else:
        print("✗ Start execution failed - check reset_log.txt for details")
        return 1

    # Step 3: Erase the device
    print("\n3. Erasing device...")
    success = st_link.erase_all(logfile="erase_log.txt")

    if success:
        print("✓ Erase successful")
    else:
        print("✗ Erase failed - check erase_log.txt for details")
        return 1

    print("\n✓ All operations completed successfully!")
    print("Log files created: program_log.txt, reset_log.txt, erase_log.txt")

    return 0