When trying to use the live expression view to look at a variable, I noticed it wasn’t appearing in the live expression view. I remembered that global variables and variables in main are stored at different places in memory and I thought that might be why. Turns out I was right.

Apparently the live expression viewer doesn’t work for variables stored on the stack. This should be added to the list of traps for young players. Luckily I have watched the course on Modern Embedded Programming so I know that the uninitialized global variables are stored in the .bss section of ROM and copied to RAM at runtime.

Update 2024-04-11 Similarly the variable window will only show the current function’s stack frame, so this means that global variables won’t show up here (since they are not even on the main() stack frame).

See video demo below.

Example Code

This code does NOT work for live expressions in STM32CubeIDE.

 
int main(void)
{
	int counter; // will be stored in the stack 
 
    /* Loop forever */
	while(1) {
		counter ++;
	}
}

Instead, declare the variable outside of main:

int counter; // will be stored in the .bss section of ROM (uninitialized), copied to RAM at runtime
// note in some DSPs the .bss section will NOT be zeroed out
 
int main(void)
{
    /* Loop forever */
	while(1) {
		counter ++;
	}
}
 

Video