FreeRTOS SMP on RP2040 Caused Flash Erase and Write Fails

Hi, I am working on a project required to erase/write flash memory on Raspberry Pi Pico inside a FreeRTOS task. I know in order to erase/write flash I need to disable interrupt, I tried wrap around flash function with taskENTER_CRITICAL() and taskExit_CRITICAL() but no luck, still freeze when flash erase is call. I removed FreeRTOS and just run the flash code by itself, it works fine. Below is the snippet of the flash function I am using.

    uart_puts(UART_ID, "Erase\r");
    //uint32_t ints = save_and_disable_interrupts();
   //restore_interrupts (ints);
    uart_puts(UART_ID, "Write\r");
    //uint32_t ints1 = save_and_disable_interrupts();
    flash_range_program(FLASH_TARGET_OFFSET, (uint8_t *)err, FLASH_PAGE_SIZE);
    //restore_interrupts (ints1);

I am wondering if I didn’t disable the interrupt correctly or if there are something to do with FreeRTOS SMP version.


With SMP “taskENTER_CRITICAL” only disables interrupt for the current processor, but not the other.

Thanks for the reply, is there a way to disable interrupt for both core? I don’t have anything running on second core yet. I thought about could be the issue with second core but I didn’t find any info on it.

I don’t know the SMP code well enough to say, but it is unlikely. If you don’t need the second core, maybe you should just be running in single processor mode on just one core.

1 Like

Can you break the code in debugger and see what it is doing when it freezes?

Hello, I have the same problem.
Do you have any update about that ?

Can you break in the debugger and share what you find?

I didn’t try the single core version of FreeRTOS yet, not sure if thats the issue. I don’t have a debugger set up yet, I need to figure out how to add debugging flag in my CMake.

You might be able to fake a single core version by pinning all of your tasks in your SMP version to a single core.

As for debugging, this page seems to have good information on one way to set this up.

I have same problem.
The debug breakpoint information was as follows.
Please let me know what additional GDB information you need and I will try to find more.

Please check my answer below.

The call stack is of the idle task. We need to find what happened to the task that called flash erase. Can you see if that task is on the ready list? Also, have you defined configASSERT and enabled stack overflow check?

Thanks for your answer.
I am studying the ready list and assert you mentioned.
However, I have a question because there are some things that occur in common.
When a function located in RAM is called from Rtos, the operation stops as shown below.
The save_to_flash() function was located in RAM and when executed, the OS stopped operating. The flash_range_erase() function we asked about earlier is also located in RAM.
I wonder if there is a way to reliably execute functions located in RAM(0x20000000 over). (RP2040, pico w)

Are you sure you didn’t overwrite the OS code in flash??

When using RTOS in pico_sdk, there is a flash_safe_execute(…) function in flash.c for using flash. I solved the current problem using this function. Thanks a lot for your help.

Thank you for reporting back.