Skip to main content
May 29, 2025
Solved

Debug a basic C++ program on STMCube IDE

  • May 29, 2025
  • 4 replies
  • 1137 views

Here's my infinite loop in a cpp file 

 
#include "main.h"
#include "lib.hpp"

void run() {
 auto cnt = 0u;
 while (1) {
 if(!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13))
 cnt++;
 }
}

I run the Debugger and execute the statements until I get to the "if" statement above then when pressing F6 another time the execution stops waiting to read the value of pin 13 (the blue button on the board). But while I have also added "cnt" onto the "live expressions" its value doesn't go beyond 0 even though I press the button couple of times.
My purpose is to see the value of "cnt" after pressing the button no matter how much it is.

 

Best answer by

Apparently the problem is with `cnt` being a local variable. When I make it global, i.e., declared before `void run() { }` it works properly and Live Expressions shows a big number for it after pressing the button even only once.

 

4 replies

TDK
Super User
May 29, 2025

Look at the disassembly. The compiler is probably optimizing it away since it has no effect.

Make cnt volatile if you want to avoid this.

volatile uint32_t cnt = 0;

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
May 29, 2025

That's right, I forgot that. 
Used `volatile auto cnt = ou;` and `cnt = cnt + 1;` then: debugger -> F6 until I reach the `if` statement then another F6 and pressing the button, yet nothing changes. The control also gets out of the debugger there.

 

EDIT: The value in the "live expressions" changes for `cnt` if I press and hold the button for a few moments! 

TDK
Super User
May 29, 2025

Perhaps don't F6 to single step, but let the program run normally and observe. Hit pause if you need it to stop.

"If you feel a post has answered your question, please click ""Accept as Solution""."
May 29, 2025

I used the label STM32F4 series in my first post above. My board is STM32F411. 

`run()` is called once in `main.c`: 

//while (1)
 {
	 run();
 /* USER CODE END WHILE */
 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */

 

Andrew Neil
Super User
May 30, 2025

@unknown wrote:

I used the label STM32F4 series


That's an entire series of chips - it doesn't identify a particular chip, or a particular board.

How to write your question to maximize your chances to find a solution

 


@unknown wrote:

My board is STM32F411. 


That's doesn't identify a board - that's just a partial chip number.

 

As you mentioned a "blue button", perhaps you mean a NUCLEO-F411RE board?

https://www.st.com/en/evaluation-tools/nucleo-f411re.html

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
May 30, 2025

@Andrew Neil 

>> NUCLEO-F411RE board?

Yes, sorry for the inconsistency. 

@TDK 

I exactly did the following step-by-step:

1- Run debugger 

2- Hit F8 (Resume)

3- Press the blue button a few times

4- Pause the execution 

Still no change in the value of `cnt` in the "live expressions". (It's blank)

Have you tested this on your board? 

Best answer
May 30, 2025

Apparently the problem is with `cnt` being a local variable. When I make it global, i.e., declared before `void run() { }` it works properly and Live Expressions shows a big number for it after pressing the button even only once.

 

Andrew Neil
Super User
June 2, 2025

That's another issue with using "Live Expressions" - the things you're watching need to be permanently visible.

Making it static (but still local) might work;

Making it static at file scope should work ...

 

PS:

This is not specific to C++

The same would apply to plain C.

 

#LiveExpression #GlobalData

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
Super User
June 2, 2025

@STTwo-32 perhaps this is something which needs to be made clearer in the UM?

AndrewNeil_1-1748855167589.png

 

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.