cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing functions from within interrupts

Inverse Inductor
Associate
Posted on April 13, 2018 at 02:37

I would like to access a global object in my main.cpp from stm32l0xx_it.c.

In particular, I've created a LED object with a member function called update(). I'd like to be able to run the update function from within a timer based interrupt to change the state of the LED pins based on the internal state of the LED object. At the moment I'm just using the systick interrupt for prototyping purposes.

I've attached a small project demonstrating what I'd like to do.

#stm32-interrupts
3 REPLIES 3
AvaTar
Lead
Posted on April 13, 2018 at 08:40

Perhaps others might browse through 5Mb of zipped sources.

I would like to access a global object in my main.cpp from stm32l0xx_it.c.

Basically just the standard way - if the variable is visible within the stm32l0xx_it.c module (global), you can directly access it. Be aware of race conditions when accessing variables from asynchronous handlers.

From a design point of view, it makes no sense to make volatile process input/output globally visible.

Pavel A.
Evangelist III
Posted on April 13, 2018 at 14:57

Like the previous person I haven't looked into the 5+ MB zip...

But basically,  if the LED is connected to a GPIO pin, it can be toggled with just one memory write instruction. This should be OK in a interrupt handler, with either 'HAL' or 'LL' library for the GPIO.

Any other C++ stuff with objects and so on should be carefully inspected.

Good luck,

-- pa

Posted on April 13, 2018 at 15:31

The IRQ Handlers don't need to be in stm32l0xx_it.c you can make callable functions in main.cpp using the form

extern 'C' void USART1_IRQHandler(void)

{

...

}

Or create a wrapper function in main.cpp with similar C calling (avoids name mangling) that you can call from stm32l0xx_it.c

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