SysTick_Handler crashes when incrementing static variable using ++
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-21 9:57 PM - last edited on ‎2025-05-22 12:43 AM by Andrew Neil
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
Solved! Go to Solution.
- Labels:
-
Bug-report
-
STM32F4 Series
-
SysTick
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-22 10:28 AM
Yup, hard fault, thanks for all your help. Seems like I had an issue with my clock setup - when I ran it at 75MHz, I think I was supposed to set the power control scaling for it to be able to go that high. The system defaults to scale 2, which supports only lower clock frequencies; anything greater would need this code to be put before the PLL setup.
The reference manual doesn't really state that you have to do this for 75MHz, it says only if you run less than 100MHz, but I guess I needed it.
Enable interface clock and set the internal voltage regulator scale to scale 1
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
PWR->CR |= PWR_CR_VOS;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-21 10:27 PM
Problem is in
static volatile
declaration.
If you want more help, post the assembler code generated, the memory address of millis variable, and (if you do not see yourself ) we will show the problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-21 10:48 PM
> ...post the assembler code generated, the memory address of millis variable, ...
Switching debugging to assembler level (AFAIK "instruction stepping" in Eclipse lingo) and making a screenshot including the line where it crashes should do the trick.
I think there is something else, outside the code you have shown.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-22 12:45 AM
@drdrone wrote:a static volatile global counter
If it's static, then it's not global - at most, it's file-scope.
@drdrone wrote:the system crashes and ends up in Default_Handler.
On debugging Hard Faults:
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-22 1:22 AM
Hello @drdrone
Could you try the config below please:
void SysTick_Handler(void) {
__disable_irq(); // Disable interrupts
millis++;
__enable_irq(); // Enable interrupts
}
Saket_Om
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-22 1:52 AM
This is not a solution nor an explanation !
Solution is in generated assembler ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-22 2:28 AM
> If it's static, then it's not global - at most, it's file-scope.
That should be ok, I often use similiar definitions without problems. Although not with theCube IDE / toolchain.
A common example is a wait / delay routine based upon such a counter, implemented in the same module.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-22 2:41 AM
@Ozone wrote:> If it's static, then it's not global - at most, it's file-scope.
That should be ok,
But the point is it's not a global variable - as the OP said - so checking if that was just a typo...
@drdrone - does it make any difference if you make it truly global (ie, remove static)?
Also if you remove volatile?
And what if you use:
millis += 1;
What optimisation level are you using?
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-22 2:43 AM
@Ozone wrote:I think there is something else, outside the code you have shown.
Indeed.
@drdrone please post a minimum but complete example which demonstrates the issue.
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-22 2:52 AM
I think as a quasi-"global" variable (non-auto), it should show up in the map file.
I would at least check.
