I have a project using a Due, which is very heavily interrupt-driven. There are a few low-level classes that interact with external hardware (a custom FPGA that requires some pretty complex bit-banged I/O to communicate with it). The methods in these classes that "talk" to the hardware cannot be interrupted, lest the bit-banged I/O sequence get broken, so they must run with interrupts disabled. BUT, some of them can be called either from a foreground task or an interrupt handler, so they need to be able to save the current interrupt enable state, disable all interrupts, do their thing, then restore the interrupt enable state to what it was on entry.
It appears to me the way to do this is by saving the ARM PRIMASK register, writing it to disable all maskable interrupts (writing 0x10 to PRIMASK if I'm understanding correctly), then restoring the saved value when done. It also appears this code needs to be semaphored, since the register accesses are not atomic. I have been unable to find existing code to do this, and my attempts at writing my own methods have been less than successful.
So, does anyone have code to do this safely and robustly?
Regards,
Ray L.