imxrt-rs/imxrt-hal

CAS loop or disable interrupts on unsafe modify of shared registers

teburd opened this issue · 1 comments

A few places I'm finding we are doing unsafe modify_reg(); These need to be reviewed in the hal to ensure that when doing an unsafe modify that the register isn't possibly modified elsewhere in the hal.

This is so that after a read but before a write for a modify operation a pre-empted ISR which also modifies the same register may cause a data race once returned to the previously interrupted code.

In such cases (GPIO !) we need to likely either temporarily disable interrupts before and re-enable after these operations or implement a compare and swap loop such that when interrupts to occur an atomic cas will fail and a loop may retry the whole task.

This seems like its more likely an issue in the GPIO code where the GPIO port registers are shared among multiple individual GPIO pins, unless we change the way the GPIO is used perhaps to be on a port by port basis with a gpio port singleton.

Fixed for now using critical sections (cortex_m::interrupt::free) but we want to use ownership and mutability rules in the future, so closing this and opening a new issue related to that