microbit-foundation / micropython-microbit-v2

Temporary home for MicroPython for micro:bit v2 as we stablise it before pushing upstream
MIT License
41 stars 22 forks source link

Program Freezes When Using [MICROPY_BEGIN_ATOMIC_SECTION(); ]Function #165

Open scarletwiz opened 7 months ago

scarletwiz commented 7 months ago

I am currently working on integrating the network functionality into an existing MicroPython module, specifically for WIZNET5K.

I've successfully implemented most features by porating SPI and the micropython-microbit-v2\lib\micropython\extmod\network_wiznet5k.c file. However, I encounter a problem where the process freezes at the point in the code where MICROPY_BEGIN_ATOMIC_SECTION(); is called.

Here's the relevant code snippet (micropython-microbit-v2\lib\micropython\extmod\network_wiznet5k.c/ Line 130):

STATIC void wiz_cris_enter(void) {
    wiznet5k_obj.cris_state = MICROPY_BEGIN_ATOMIC_SECTION();
}

If I omit this section, the process runs, but the mutex functionality, which is crucial for this program, is lost.

This feature operates flawlessly on other MCUs, such as the rp2040 and stm32. What might be causing the difference with the microbit? Or is there another aspect of the modification that I might have overlooked?

dpgeorge commented 6 months ago

With the wiznet driver, SPI transactions are done within critical sections. On the microbit, the CODAL handles SPI transfers and probably uses interrupts (and DMA) to perform this transfer. With MICROPY_BEGIN_ATOMIC_SECTION the interrupts are disabled and that probably breaks SPI.

You'll need to implement the atomic section in a different way. I'm not sure exactly why the wiznet driver needs atomicity. It may be enough to just implement them as a no-op.