Page 1 of 1

safely re-enabling interrupts after chSysDisable()

Posted: Mon Sep 25, 2017 8:47 am
by tridge
After using chSysDisable() to disable interrupts the obvious thing to re-enable interrupts is chSysEnable(). That doesn't seem to cope with the case where the caller of the function had also disabled interrupts, so I'm wondering if there is a recommended approach to restore the previous interrupt mask state.
What I was expecting was something like:

isr_state_t state = chSysDisable();

// do stuff with interrupts disabled

chSysRestore(state);

is there an equivalent to this in ChibiOS? Or is there some other way to safely restore the previous interrupt mask state?
I'm on a stm32f4xx in case it matters.
Cheers, Tridge

Re: safely re-enabling interrupts after chSysDisable()

Posted: Mon Sep 25, 2017 8:56 am
by Giovanni
Hi,

It is possible to do this in a portable way:

Code: Select all

syssts_t sts = chSysGetStatusAndLockX(void);
...
chSysRestoreStatusX(sts);


Note that depending on the architecture this can disable interrupts globally or raise the interrupt mask to a certain level. On the F4 it masks all IRQs up to priority 2 (0 and 1 are not masked), it operates on the BASEPRI register in order to do so. This is how critical zones are implemented.

chSysDisable() masks interrupts globally, it should not be used for critical zones.

Giovanni

Re: safely re-enabling interrupts after chSysDisable()

Posted: Mon Sep 25, 2017 9:20 am
by tridge
Giovanni wrote:Hi,
It is possible to do this in a portable way:

thanks, much appreciated!