Hi,
It's my first post here so please forgive me if I forgot to mention something important.
I'm running pretty complex IoT device (fully in MicroPython) based on the Raspberry Pi Pico W (RP2040) with connected WIZnet W5500 Ethernet module. All is connected on the custom PCB so the connection between W5500 and Pico reflects those on the board https://micropython.org/download/W5500_EVB_PICO/ (at least as far as SPI between them goes). Board works in general and my issue is not with the hardware.
My application has 2 threads: (display+sensors) and (MQTT). Threads are created using _thread.start_new_thread(). On the complex queries over MQTT I'm getting an exception: "maximum recursion depth exceeded". I have found that on the Pico there is no set limit on the recursion calls (confirmed experimentally), rather it is thrown when free stack memory runs out. I've tested several firmwares and ended up with building uPython from sources (version 3.4.0). My build mostly works, only one thing that still doesn't is problem with stack size (basically build with BOARD=W5500_EVB_PICO -- I do not need WiFi or BLE that comes with Pico W).
Creating thread is done in main.py as follows:Experimentally tested, program crushes when stack exceeds ~2.5kiB (tested with micropython.stack_use()), which is strange giving 8kiB of basic stack size (bank 5, 6 of RAM in RP2040)
I've tried many solutions, but non of them seems to solve my issue.
1. I have tried to edit linker script but with no effect. I could not attach txt file, hence the screenshot (sorry if there is better way to do this). Apart from what's on the screenshot I've made no changes to the original one. I've modified end of the GC and the stack limit so the stack (at least in theory) should have 8kiB more safe memory before it runs into the GC. This modification had absolutely no effect on the code, it still crashed with same exception on long MQTT queries. I've tracked down that in /micropython/ports/rp2/main.c there are:I'm also printing those values at software reset and they change but with no effect on my problem. 2. I've tried to use _thread.stack_size() with different values but with no effects. I think I don't understand how this method should work. Based on the official documentation https://docs.python.org/3/library/_thread.html my reasoning is as follows: if I call _thread.stack_size(6000), every new thread should have assigned 6kiB of stack. Yet it does not. Program does not crush and still does not fully work. Return value from stack_size() method changes over calls with different arguments, so it definitely does something.
3. I've tried to modify stack size in CMakeLists.txt. There is such line: PICO_STACK_SIZE=0x2000. Changing it's value to even 0x4000 does not affect my issue. I found out that setting MICROPY_STACK_CHECK to 0 allows to swipe problem under the rug with high risk of seg faults.
I've searched in the Internet, but so far no luck for me.
It's my first post here so please forgive me if I forgot to mention something important.
I'm running pretty complex IoT device (fully in MicroPython) based on the Raspberry Pi Pico W (RP2040) with connected WIZnet W5500 Ethernet module. All is connected on the custom PCB so the connection between W5500 and Pico reflects those on the board https://micropython.org/download/W5500_EVB_PICO/ (at least as far as SPI between them goes). Board works in general and my issue is not with the hardware.
My application has 2 threads: (display+sensors) and (MQTT). Threads are created using _thread.start_new_thread(). On the complex queries over MQTT I'm getting an exception: "maximum recursion depth exceeded". I have found that on the Pico there is no set limit on the recursion calls (confirmed experimentally), rather it is thrown when free stack memory runs out. I've tested several firmwares and ended up with building uPython from sources (version 3.4.0). My build mostly works, only one thing that still doesn't is problem with stack size (basically build with BOARD=W5500_EVB_PICO -- I do not need WiFi or BLE that comes with Pico W).
Creating thread is done in main.py as follows:
Code:
_thread.start_new_thread(mqtt_thread)display_loop()
I've tried many solutions, but non of them seems to solve my issue.
1. I have tried to edit linker script but with no effect. I could not attach txt file, hence the screenshot (sorry if there is better way to do this). Apart from what's on the screenshot I've made no changes to the original one. I've modified end of the GC and the stack limit so the stack (at least in theory) should have 8kiB more safe memory before it runs into the GC. This modification had absolutely no effect on the code, it still crashed with same exception on long MQTT queries. I've tracked down that in /micropython/ports/rp2/main.c there are:
Code:
// Initialise stack extents and GC heap.mp_stack_set_top(&__StackTop);mp_stack_set_limit(&__StackTop - &__StackBottom - 256);gc_init(&__GcHeapStart, &__GcHeapEnd);
3. I've tried to modify stack size in CMakeLists.txt. There is such line: PICO_STACK_SIZE=0x2000. Changing it's value to even 0x4000 does not affect my issue. I found out that setting MICROPY_STACK_CHECK to 0 allows to swipe problem under the rug with high risk of seg faults.
I've searched in the Internet, but so far no luck for me.
Statistics: Posted by Brodacz — Tue Jul 09, 2024 9:44 am — Replies 0 — Views 4