KSociety.SharpCubeProgrammer

Logo

build status latest version download count

KSociety.SharpCubeProgrammer Home

KSociety.SharpCubeProgrammer

KSociety.SharpCubeProgrammer is a .NET library providing interop with the CubeProgrammer_API API from managed code using .NET APIs.

Emulation of the STM32CubePrgAPI program is available in the examples: SharpCubePrgAPI.

Sample app

First unofficial and open source C# wrapper.

It makes use of several 3rd party tools:

Introduction

This library exposes types that wrap the native CubeProgrammer_API API to perform operations on ST’s microcontrollers & microprocessors.

This is a C# wrapper for STM32 CubeProgrammer_API v2.21.0 (not fully tested). This package does not contain any C/C++ runtimes (MSVC), and is meant to run on Windows operating systems only (for now).

[!IMPORTANT] Please make sure you have updated the firmware of your ST-LINK V2 / V3, you can do this using STM32CubeProgrammer.

STM32 CubeProgrammer_API is a C library created by ST to manage microcontrollers, it allows to access memory for reading and writing and to manage option bytes. The ST-Link drivers is required, and can be downloaded from st.com and installed (STSW-LINK009). This has been tested on Windows 10 and Windows 11.

[!NOTE] You don’t need to install STM32CubeProgrammer, everything you need is already included in the NuGet package. Only the ST-Link drivers need to be installed.

KSociety.SharpCubeProgrammer

STM32CubeProgrammer_API C# wrapper, the first wrapper for C#. Any suggestions are welcome.

Get Packages

You can get KSociety.SharpCubeProgrammer by grabbing the latest NuGet package.

[!IMPORTANT] You need to use PackageReference, otherwise some contents will not be copied to the output folder and consequently it will not work.

Install

Package Manager Console

PM> Install-Package KSociety.SharpCubeProgrammer

.NET CLI Console

> dotnet add package KSociety.SharpCubeProgrammer

Currently supported features

All functions also exist in an asynchronous version.

Bootloader functions

General purposes functions

Option Bytes functions

Loaders functions

STM32WB specific functions

STM32MP specific functions

STM32 HSM specific functions

Prerequisites

Workloads
.NET desktop development

Examples

Examples include:

Get Started

var cubeProgrammerApi = new SharpCubeProgrammer.CubeProgrammerApi();
var displayCallBacks = new DisplayCallBacks
{
    InitProgressBar = InitProgressBar,
    LogMessage = ReceiveMessage,
    LoadBar = ProgressBarUpdate
};

cubeProgrammerApi.SetDisplayCallbacks(displayCallBacks);

cubeProgrammerApi.SetVerbosityLevel(CubeProgrammerVerbosityLevel.CubeprogrammerVerLevelDebug);
var stLinkList = cubeProgrammerApi.GetStLinkEnumerationList();

or

var stLinkList = await cubeProgrammerApi.GetStLinkEnumerationListAsync();
if (stLinkList.Any())
{
    var stLink = stLinkList.First();
    stLink.ConnectionMode = DebugConnectionMode.UnderResetMode;
    stLink.Shared = 0;

    var connectionResult = cubeProgrammerApi.ConnectStLink(stLink);

    //...

}
else
{
    Console.WriteLine("No ST-Link found!");
}
if (connectionResult.Equals(CubeProgrammerError.CubeprogrammerNoError))
{
    var generalInfo = cubeProgrammerApi.GetDeviceGeneralInf();
    if (generalInfo != null)
    {
        Console.WriteLine("INFO: \n" +
                            "Board: {0} \n" +
                            "Bootloader Version: {1} \n" +
                            "Cpu: {2} \n" +
                            "Description: {3} \n" +
                            "DeviceId: {4} \n" +
                            "FlashSize: {5} \n" +
                            "RevisionId: {6} \n" +
                            "Name: {7} \n" +
                            "Series: {8} \n" +
                            "Type: {9}",
            generalInfo.Value.Board,
            generalInfo.Value.BootloaderVersion,
            generalInfo.Value.Cpu,
            generalInfo.Value.Description,
            generalInfo.Value.DeviceId,
            generalInfo.Value.FlashSize,
            generalInfo.Value.RevisionId,
            generalInfo.Value.Name,
            generalInfo.Value.Series,
            generalInfo.Value.Type);
    }
}
var sendOptionBytesCmd = cubeProgrammerApi.SendOptionBytesCmd("-ob RDP=170");
var massErase = cubeProgrammerApi.MassErase("");
var downloadFile = cubeProgrammerApi.DownloadFile(firmwarePath, "0x08000000", 1U, 1U);
var execute = cubeProgrammerApi.Execute("0x08000000");
cubeProgrammerApi.Disconnect();
cubeProgrammerApi.DeleteInterfaceList();

License

The project is under:

Dependencies

List of technologies, frameworks and libraries used for implementation:

This library is copyright (c) K-Society 2022-2026.

ST is a trademark and copyright of the STMicroelectronics NV.