Answer the question
In order to leave comments, you need to log in
How to make a function safe from interruption?
Good afternoon.
The program uses interrupts, an interrupt can interrupt another interrupt =)
The main problem is malloc() , sometimes it happens that it hits exactly at the moment of memory allocation, but in another interrupt there is also a memory allocation. And when there are coincidences, then there is a collapse.
So, in order not to put down a bunch of __disable_irq(); __enable_irq();
how can some functions be completely protected from interruption (but that it would be called after the execution of the function itself)? By the whole thing, I mean not to write commands inside the function, but somehow outside or something ...
Taking into account the fact that wrapping the function call itself is not an option (a crutch).
Using __disable_irq(); and __enable_irq(); the same can be problematic, because if interrupts are already disabled in the caller of this function, we can accidentally turn them on, which is very undesirable.
PS Memory allocation in interrupts is evil, etc. yeah.
PPS can be configured so that interrupts do not interrupt one another, but this is not suitable, because. you only need some functions that are called in the interrupt.
Answer the question
In order to leave comments, you need to log in
For such purposes, additional wrappers are introduced over disable_irq and enable_irq, which calculate the "depth" of interrupt prohibition. In fact, there is simply a global counter that increases with each call to ext_disable_irq and decreases with ext_enable_irq. The real enabling of interrupts occurs at the very end of ext_enable_irq and only if the counter is zero at that moment
Smoke to dock on the compiler which you use. Many compilers have built-in special mechanisms that can be used to disable and enable interrupts. For example, there are macros that disable interrupts when entering a block, and then either allow them no matter what, or restore the value that was before entering this block, because. if interrupts were disabled, then they remain disabled.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question