cancel
Showing results for 
Search instead for 
Did you mean: 

Debug a basic C++ program on STMCube IDE

TechyAry
Associate III

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
TechyAry
Associate III

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.

 

View solution in original post

13 REPLIES 13
TDK
Super User

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".

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! 

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".

Sorry I didn't get that (especially the part "run normally").

You mean to still run the debugger, yeah?
Then it stops at line `HAL_Init();` in `main.c` and I need to press F6 to let it go forward. 
 

Ok, so you don't specify a board

Or how/where run() gets called, or where you initialize the pin, etc.  So "run normally" would suggest doing whatever you're currently doing to make it execute, absent any context. Press the button several times, stop in the debugger, and see how many thousands of counts you got as in loops whilst you have the button pressed.

Perhaps inspect the machine code output by the compiler, in cnt in a memory variable or a register.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TechyAry
Associate III

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 */

 

F6 is step over.

F8 is what I am calling "run normally".

TDK_0-1748563635302.png

 

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

@TechyAry 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

 


@TechyAry 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.

@TDK wrote:

Hit pause if you need it to stop.


@TechyAry that's this button:

AndrewNeil_0-1748610508644.png

 

What that does is to halt the execution of your code wherever it's currently at, and allows you to use the debugger to inspect variables, etc.

To set the code running again, you use the 'Resume' button (or F8).

 

There are many reports on here of unreliability with "live expressions" - this approach is reliable.

 

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.