cancel
Showing results for 
Search instead for 
Did you mean: 

API: STLink detected but cannot be connected to.

AMabi.1
Associate III

I want to use the STM32CubeProgrammer C++ API to do high-speed real-time automated debugging (i.e. use it as an oscilloscope).

I used as a basis the Example_2 given in the API's documentation. The STLink is detected since I can get info from it which I confirmed with the CubeProgrammer. However, when I try to connect to it the connectStLink function returns the value -545 (see the screenshot). I have tried to change various parameters for the past two days with no effect, I am at a complete loss. Here is my very simple code:

Thank you for your help.

#include <DisplayManager.h>
#include <stdio.h>
#include <iostream>
#include <CubeProgrammer_API.h>
 
void initProgBar() {
	std::cout << "initProgBar";
}
 
void MylogMessage(int msgType, const wchar_t* str) {}
 
void loadBar(int x, int n) {}
 
int main() {
 
	debugConnectParameters *stLinkList;
	debugConnectParameters debugParameters;
	generalInf* genInfo;
 
	displayCallBacks dc;
	dc.initProgressBar = initProgBar;
	dc.logMessage = MylogMessage;
	dc.loadBar = loadBar;
 
	setDisplayCallbacks(dc);
 
	int getStlinkListNb = getStLinkList(&stLinkList, 0);
 
	if (getStlinkListNb == 0)
	{
		logMessage(Error, "No STLINK available\n");
		return 0;
	}
	else {
		logMessage(Title, "\n-------- Connected ST-LINK Probes List --------");
		for (int i = 0; i < getStlinkListNb; i++)
		{
			logMessage(Normal, "\nST-LINK Probe %d :\n", i);
			logMessage(Info, "   ST-LINK SN   : %s \n", stLinkList[i].serialNumber);
			logMessage(Info, "   ST-LINK FW   : %s \n", stLinkList[i].firmwareVersion);
		}
		logMessage(Title, "-----------------------------------------------\n\n");
	}
 
	for (int index = 0; index < getStlinkListNb; index++) {
 
		logMessage(Title, "\n--------------------- ");
		logMessage(Title, "\n ST-LINK Probe %d ", index);
		logMessage(Title, "\n--------------------- \n\n");
 
		debugParameters = stLinkList[index];
		debugParameters.connectionMode = HOTPLUG_MODE;
		debugParameters.resetMode = SOFTWARE_RESET;
 
 
		/* Target connect */
		int connectStlinkFlag = connectStLink(debugParameters);
 
		if (connectStlinkFlag != 0) {
			logMessage(Error, "Establishing connection with the device failed. Return value: %d\n", connectStlinkFlag);
 
			disconnect();
			continue;
		}
		else {
			logMessage(GreenInfo, "\n--- Device %d Connected --- \n", index);
		}
 
		/* Display device informations */
		genInfo = getDeviceGeneralInf();
		logMessage(Normal, "\nDevice name : %s ", genInfo->name);
		logMessage(Normal, "\nDevice type : %s ", genInfo->type);
		logMessage(Normal, "\nDevice CPU  : %s \n", genInfo->cpu);
	}
 
	deleteInterfaceList();
	return 1;
}


_legacyfs_online_stmicro_images_0693W00000bjDGPQA2.png

7 REPLIES 7
Kamil Duljas
Senior III

Perhaps function connectStLink(debugParameters) uses as parameter pointer instead value. Try pass address like this:

​connectStLink(&debugParameters)

Dudo

I already tried but it gives me a compilation error. No surprise there the documentation clearly indicates what type the arguments must be.

OK. I saw it in other code. Have you link to documentation? What's mean 545?

Dudo

The doc is provided in CubeProgrammer/api folder.

It says that the function should return 0 if it’s successful and something else if not.

I asked because now I work from phone:)​ Did you try use direct stLinkList[index] instead of local variable?

Dudo

I tried, but no. Both variables are of the same type (debugConnectParameters) and I checked that the data are copied.

In my case, I was able to solve that problem by setting the path to the parent folder of the FlashLoader and ExternalLoader folders with the setLoadersPath() function before calling other API functions. Those folders are located at /api/lib by default. If that folder also is the working directory, which is the case in the Visual Studio examples, one can simply set the path to "./.".

 

const char* loaderPath = "./.";
/* Set device loaders path that contains FlashLoader and ExternalLoader folders */
setLoadersPath(loaderPath);