Skip to main content
picatostas
Associate II
October 2, 2020
Question

Function is not called if not declared as inline/static

  • October 2, 2020
  • 4 replies
  • 1713 views

Hi I am starting to do some basics with register operations and came across with this situation.

The board I am using is a STM32L031k6 nucleo.

Code is the following

#include "stm32l031xx.h"
 
// Green led is at PB3
 
static void delay_ms(uint32_t delay);
 
int main(void)
{
	// Enable clock for GPIOB
	RCC->IOPENR |= RCC_IOPENR_GPIOBEN;
	// Set Gpio B3 as output, default mode is analog mode
	GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE3)) | (GPIO_MODER_MODE3_0);
	while (1)
	{
		GPIOB->BSRR |= 0x8;
		delay_ms(20);
		GPIOB->BRR |= 0x8;
		delay_ms(20);
	}
}
 
void delay_ms(uint32_t delay)
{
	for (; delay > 0; delay--)
	{
		for (uint16_t i = 0; i < 2000; i++)
		{
			/* code */
		}
	}
 
}

if function is not declared as static, it never works.

If declared as static, it works most of the times that I do clean-build-transfer ( I don' t understand when it doesn' t)

if declared as __INLINE code takes like extra 60 B of space and works.

As IDE I am using VSCode with stm32-for-vscode extension, which sets the makefile based on a CubeMx proy.

Is the compiler doing some optimization and removing the function ? Is the MCU hanging for some reason ?

This topic has been closed for replies.

4 replies

TDK
Super User
October 2, 2020

That's a bad delay function. The compiler can optimize it out entirely. It is probably doing so in certain cases.

A better (but still bad) delay would use volatile variables to prevent them from being optimized out. A good delay would be based on actual time, such as systick or timer counts.

"If you feel a post has answered your question, please click ""Accept as Solution""."
picatostas
Associate II
October 3, 2020

Thanks for the answer, what will change in this case calling it static or inline? I am aware that the way to go would've been as you said, timmer count or systick. I wanted something fast for the sake of blinking the led I will change it.

Piranha
Principal III
October 3, 2020

For your quick test to work, add volatile qualifier for both - delay and i variables.

picatostas
Associate II
October 3, 2020

Adding volatile to i and neither of static nor inline for the function declaration made it work. Thanks a lot.

Nikita91
Lead II
October 3, 2020

For an active delay function based on the cycle counter have a look at:

https://community.st.com/s/question/0D53W00000BJ7KdSAL/is-haldelay1-quaranteed-to-be-close-to-1ms

Tesla DeLorean
Guru
October 3, 2020

You shouldn't need to RMW against BRR/BSRR, the whole point is you can do a single write at a bit level.

Review the code output by the compiler, and run in the debugger.

For the cases that dont work, inspect the listing file, and show that code.

U​se the counter in a freerunning TIM to mark time.

Use a scope to see signal​s faster than the human eye.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..