Skip to main content
Visitor II
February 8, 2021
Solved

Counter after interrupt request doesn't enable if condition in main

  • February 8, 2021
  • 1 reply
  • 1143 views

Hello STM-Community,

I'm fairly new to STM programming and I use an STM32 microcontroller with STM32CubeMX encountered a weird issue with my functions.

I've got an IRQ, which collects data every interrupt. Similair like this:

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
 
{
 
	if (GPIO_Pin == INT_EXT_Pin)
 
	{
 
 ACCMETER_IRQ(accdata);
 
 data_counter++;
 
	}
 
}

In the main function I want to see if enough data have been collected for an FFT.

Similair like this in my while loop:

while(1)
 
{
 
	if (data_counter > 4096)
 
	{
 
 FFT_ACCMETER(accdata);
 
 data_counter = 0;
 
	}
 
}
 
 

while performing the FFT, the IRQ still collects data for a buffer function, therefore no data should be lost in between each FFT.

Here's the issue, after gathering enough data, data_couner is set to 4096 (or higher), but never actually goes into the if condition. I've tried out many different things such as accessing the variable to different one within my main loop and it didn't help. I'd be really glad for some advices.

Kind Regards

Norman

    This topic has been closed for replies.
    Best answer by KnarfB

    data_counter must be defined volatile if used for synchronization, otherwise the compiler may optimize too much.

    1 reply

    KnarfBAnswer
    Super User
    February 8, 2021

    data_counter must be defined volatile if used for synchronization, otherwise the compiler may optimize too much.

    crackl1ngAuthor
    Visitor II
    February 8, 2021

    Oh okay thank you! I just wanted to write that it might have been fixed since it worked when I just checked it after restarting the pc. Though I will consider this the next time it happens to me.

    Thanks!