SysTick_Handler crashes when incrementing static variable using ++
Hi all,
I'm working on an STM32F411 project using STM32CubeIDE (bare-metal, no HAL). I'm setting up a 1ms tick using SysTick_Config() and defining my own SysTick_Handler() to keep a millisecond counter. Here's the issue:
When I increment a static volatile global counter inside the handler using '++', the system crashes and ends up in Default_Handler. But strangely, if I assign a constant (e.g. millis = 123;), it works perfectly fine. Moreover, if I increment a local temp variable and increment millis, it'll take it just fine. Here's my code snippet. I'm not using the HAL library for this. Would you know why this would be? I'm using an STM32F411CE6 black pill.
Thanks!
static volatile uint32_t millis = 0;
void SysTick_Handler(void)
{
// This works:
// millis = 123;
// This works as well:
uint32_t tmp = millis;
tmp++;
millis = tmp;
// This causes a crash into a forever loop in default handler:
millis++;
(void)(SysTick->CTRL); // Clear COUNTFLAG
}
// SysTick is initialized like this in main, got all the clocks, etc, working. Running at 75MHz PLL, 25MHZ external crystal.
SysTick_Config(SystemCoreClock / 1000); // 1ms tick

