safely re-enabling interrupts after chSysDisable() Topic is solved

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
tridge
Posts: 141
Joined: Mon Sep 25, 2017 8:27 am
Has thanked: 10 times
Been thanked: 20 times

safely re-enabling interrupts after chSysDisable()

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: safely re-enabling interrupts after chSysDisable()

Post 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
tridge
Posts: 141
Joined: Mon Sep 25, 2017 8:27 am
Has thanked: 10 times
Been thanked: 20 times

Re: safely re-enabling interrupts after chSysDisable()

Post by tridge »

Giovanni wrote:Hi,
It is possible to do this in a portable way:

thanks, much appreciated!
Post Reply