pimoroni / pimoroni-pico

Libraries and examples to support Pimoroni Pico add-ons in C++ and MicroPython.
https://shop.pimoroni.com/collections/pico
MIT License
1.31k stars 495 forks source link

Tufty2040 main.py menu causes memory fragmentation #631

Closed LionsPhil closed 1 year ago

LionsPhil commented 1 year ago

The cool little Tufty 2040 menu/loader (https://github.com/pimoroni/pimoroni-pico/blob/1337d2abdb57d2995b2252186d591d8efcc2413f/micropython/examples/tufty2040/main.py) attempts to free up memory before chaining over to the selected script. Unfortunately this isn't perfect, and is sometimes causing PicoGraphics to fail to allocate a large enough contiguous block (I think) for the display. I can aggrivate this pretty reliably (~80% of the time...on a microcontroller without all the churn of a full OS I'm not actually sure why it's so random) with my heavily-modified retro_badge.py that uses 8bpp graphics mode, but haven't really managed to get a reliable repro with something cut-down that's still pulling in the same imports and doing the same init...I'm guessing simple script size is a factor too.

See below for memory maps where I've been trying to identify if there's a better fix for this, or narrow down a culprit. It sorta seems like PicoGraphics is only able to allocate from a higher region that's getting a stray byte at 27c00 -ish subdividing it in the bad case, but there's a larger free block at 04c00 -ish in either case. I'm guessing the script itself, modules, and statics are eating it up and the allocator isn't defensive about keeping large regions intact.

Possible workaround

Reboot into the new script. I've reworked meny.py to reset inbetween the selection and loading, so it can avoid the large allocations of the display --- https://gist.github.com/LionsPhil/26c640356b074513d6c612a199db9d3f --- and put that functionality on the B button. The big downside is that Thonny hates the device resetting itself and gets in a bit of a mess, so it's not joyous to debug. It also causes a small amount of writing to flash where previously there was zero.

(That gist also has the memory debugging enabled. Which of course causes more memory allocations.)

Alternatively I don't know if MicroPython has something more akin to an exec(), that could reset the interpreter state, python heap, etc. without fully resetting the device.

Memory maps

In these:

Exhaustion

MEMORY MAP BEFORE MENU INIT:
stack: 484 out of 7936
GC: total: 192064, used: 12240, free: 179824
 No. of 1-blocks: 122, 2-blocks: 32, max blk sz: 72, max free sz: 10784
GC memory layout; from 2000afa0:
00000: h=MhhhBDThBTTBDhTBDBBBh===DBDh====B=BBBBBBTB=BTB=BBBTB=TBTB=Bh==
00400: =BB=h===========TB=h=h===================h=====MDBh=============
00800: ====h===========================================================
00c00: ====h===========================================================
01000: ====h=hhhBThhThDSFhh=====h=T=hDT=BBB..hDhBDTBh=B=BBh=======DBT=B
01400: =BDSh=======h========Bh==h===Fh=BT=B=TBBBDBB=Bhhh==h===h===T=B=B
01800: hh==h===BDBTB=h=DTFh===h===FFBh=B=h===FFh=======================
01c00: ================================================BDhBBBhhhh===Bh=
02000: ==..hh===========h===......h========.........Sh=................
02400: ..h=======T=...............Sh=..................Sh=.............
02800: .......................F.................h================......
02c00: ...................F.......................................F....
03000: .............................................................FSh
03400: =................................h=====h==......................
03800: ...............................................................h
03c00: =======.........................................................
04000: .............................h==================================
04400: =============================h==================================
04800: ============================h============h======h==h============
04c00: ====............................................................
       (167 lines all free)
2ec00: ....................................

MEMORY MAP BEFORE CLEANUP:
stack: 732 out of 7936
GC: total: 192064, used: 179600, free: 12464
 No. of 1-blocks: 5331, 2-blocks: 303, max blk sz: 4800, max free sz: 454
GC memory layout; from 2000afa0:
00000: h=MhhFFDFhFFFFDhFFDBBBh===DBDh====B=BBBBBBFB=BFB=BBBFB=FBFB=Bh==
00400: =BB=h===========FB=h=h===================h=====MDFh=============
00800: ====h===========================================================
00c00: ====h===========================================================
01000: ====h=hhhBFhFThDSFhh=====h=T=hDFFBBBFFhDhFDFBh=B=FFh=======DBT=B
01400: =FDSh=======h========Bh==h===Fh=BFFB=FBBFDBB=Bhhh==h===h===FFB=B
01800: hh==h===FDBFB=h=DFFh===h===FFBh=B=h===FFFFFFFFT=FFFFFFFFFFFFFFFF
01c00: FT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFDhBBBhhhFFFFBh=
02000: ==FFhh===========h===hhhhBSh===Fh===FFT=FFFShSh=FFShFFShFFShFFFF
02400: Fhh=======T=FFFT=FFFShFFShSSh=FFFFh=FFFFFFFFFT=SSh=FFh=FFFFFFShF
02800: FFFFFFFFT=FFFFFFFFFFFFFFFFFFT=hSDhFFFFFFFh================FFFFFF
02c00: FFFFFhSDhT=FFFFFFFFFFFFFFFFFT=FFFFhSDhFFFFFFFFFFFFFT=FFFhSDFhFFF
03000: FFFFFFFFFFFT=FFFFFFFFFFFFFFhSDhFFFT=FFFFFFFFFFFFFFFFFFT=FFFFhFSh
03400: =SDhFFFFFFFFFFFFFT=FFFFFFFFFFFFFhh=====h==SDhFFFFT=FFFFFFFFFFFFF
03800: FFFFT=FFFFFFFFFFFFFFh=SDhFFFT=FFFFFFFFFFFFFFFFFT=FFFFhSDhFFFFFFh
03c00: =======LhShFFFFFFFT=FFFFFFFFFFFhSDhFLFFh===FFFT=FFFFFFFh===h=h==
04000: =Fh===h===FFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFF
04400: FFFFFFFFFFFFFT=FFFFFFFFFFFFFFh==================================
04800: ============================h============h======h==h============
04c00: ====h===========================================================
05000: ================================================================
05400: ================================================================
05800: ================================================================
05c00: ================================================================
06000: ================================================================
06400: ================================================================
06800: ================================================================
06c00: ================================================================
07000: ================================================================
07400: ================================================================
07800: ================================================================
07c00: ================================================================
08000: ================================================================
08400: ================================================================
08800: ================================================================
08c00: ================================================================
09000: ================================================================
09400: ================================================================
09800: ================================================================
09c00: ================================================================
0a000: ================================================================
0a400: ================================================================
0a800: ================================================================
0ac00: ================================================================
0b000: ================================================================
0b400: ================================================================
0b800: ================================================================
0bc00: ================================================================
0c000: ================================================================
0c400: ================================================================
0c800: ================================================================
0cc00: ================================================================
0d000: ================================================================
0d400: ================================================================
0d800: ================================================================
0dc00: ================================================================
0e000: ================================================================
0e400: ================================================================
0e800: ================================================================
0ec00: ================================================================
0f000: ================================================================
0f400: ================================================================
0f800: ================================================================
0fc00: ================================================================
10000: ================================================================
10400: ================================================================
10800: ================================================================
10c00: ================================================================
11000: ================================================================
11400: ================================================================
11800: ================================================================
11c00: ================================================================
12000: ================================================================
12400: ================================================================
12800: ================================================================
12c00: ================================================================
13000: ================================================================
13400: ================================================================
13800: ================================================================
13c00: ================================================================
14000: ================================================================
14400: ================================================================
14800: ================================================================
14c00: ================================================================
15000: ================================================================
15400: ================================================================
15800: ================================================================
15c00: ================================================================
16000: ================================================================
16400: ================================================================
16800: ================================================================
16c00: ================================================================
17000: ================================================================
17400: ================================================================
17800: ====FFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=hhTFFFFFFFFFFFFFF
17c00: T=FFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFT
18000: FFFFFFFTFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=
18400: FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFF
18800: FFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFF
18c00: FFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=F
19000: FFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFF
19400: FFFFFFFFFT=FFFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFF
19800: FFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FF
19c00: FFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFFT=FFFFFFFF
1a000: FFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFF
1a400: FFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFF
1a800: FFFFFFFFFFFFFFFT=FFFFFFFFF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FT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFF
29c00: FFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFF
2a000: FT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFF
2a400: FFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFF
2a800: FFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFF
2ac00: T=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFFT=FFFF
2b000: FFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFF
2b400: FFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=hhTFFFFFFFFFFFFFFT
2b800: =FFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTF
2bc00: FFFFFFTFFFFFFFF.................................................
       (6 lines all free)
2d800: .....................F......F..FF...............................
       (4 lines all free)
2ec00: ......F.F......................F.FFF

MEMORY MAP AFTER CLEANUP:
stack: 732 out of 7936
GC: total: 192064, used: 9808, free: 182256
 No. of 1-blocks: 99, 2-blocks: 27, max blk sz: 64, max free sz: 5717
GC memory layout; from 2000afa0:
00000: h=Mhh..D.h....Dh..DBBBh===DBDh====B=BBBBBB.B=B.B=BBB.B=.B.B=Bh==
00400: =BB=h===========.B=h=h===================h=====MD.h=============
00800: ====h===========================================================
00c00: ====h===========================================================
01000: ====h=hhhB.h.ThDSFhh=====h=..hD....B..hDh.D.Bh=B=..h=======DB..B
01400: =.DSh=======h========Bh==h===Fh=B..B=.BB.DBB=Bhhh==h===h===..B=B
01800: hh==h===.DB.B=h=D..h===h===..Bh=B=h===..........................
01c00: .................................................DhBBBhhh....Bh=
02000: ==..hh===========h===.....S..................Sh=................
02400: .hh=======T=...............Sh=..................Sh=.............
02800: .......................F.................h================......
02c00: ...................F.......................................F....
03000: ...........................hSDh..............................FSh
03400: =................................h=====h==......................
03800: ...............................................................h
03c00: =======LhSh....................hS...............................
04000: ................................................................
04400: .............................h==================================
04800: ============================h============h======h==h============
04c00: ====............................................................
       (88 lines all free)
1b000: .........................h=============================.........
       (50 lines all free)
27c00: .....................................F..........................
       (27 lines all free)
2ec00: ....................................

Traceback (most recent call last):
  File "<stdin>", line 142, in <module>
  File "<stdin>", line 94, in launch_application
  File "lp202_badge.py", line 18, in <module>
MemoryError: memory allocation failed, allocating 76800 bytes

Success

MEMORY MAP BEFORE MENU INIT:
stack: 484 out of 7936
GC: total: 192064, used: 12384, free: 179680
 No. of 1-blocks: 123, 2-blocks: 33, max blk sz: 72, max free sz: 10758
GC memory layout; from 2000afa0:
00000: h=MhhhBDThBTTBDhTBDBBBh===DBDh====B=BBBBBBTB=BTB=BBBTB=TBTB=Bh==
00400: =BB=h===========TB=h=h===================h=====MDBh=============
00800: ====h===========================================================
00c00: ====h===========================================================
01000: ====h=hhhBThhThDSFhh=====h=T=hDT=BBBB.hDhBDTBh=B=BBDBT=B=BDBh==h
01400: ===Sh=======h========BT=B=TBBFh=BDBB=h===h===Bhhh==T=B=Bhh==h===
01800: BDBTB=h=h===h===DTBh=BFFB=h===DhBFFBBBh===.SFh==================
01c00: =====================================================h==========
02000: =h===...hh=...hh......h.........h===========.............h=.....
02400: ..........T=...............Sh=...................h=======.......
02800: ...........Sh=...........Sh=....................................
02c00: ...F..........Fh================..............................h=
03000: ======..........................................................
03400: ....................................Fh===.....................F.
03800: ........................................................h=======
03c00: ................................................................
04000: ...........................................h====================
04400: ===========================================h====================
04800: ========================================h============h=====h====
04c00: ==h====h==h==h================..................................
       (167 lines all free)
2ec00: ....................................

MEMORY MAP BEFORE CLEANUP:
stack: 732 out of 7936
GC: total: 192064, used: 114064, free: 78000
 No. of 1-blocks: 1618, 2-blocks: 108, max blk sz: 4800, max free sz: 4466
GC memory layout; from 2000afa0:
00000: h=MhhFFDFhT=FFDhFFDBBBh===DBDh====B=BBBBBBFB=BFB=BBBFB=FBFB=Bh==
00400: =BB=h===========FB=h=h===================h=====MDFh=============
00800: ====h===========================================================
00c00: ====h===========================================================
01000: ====h=hhhBFhFThDSFhh=====h=FFhDFFBBBBFhDhFDFBh=B=T=DBFFB=FDBh==h
01400: ===Sh=======h========BFFB=FBBFh=FDBB=h===h===Bhhh==FFB=Bhh==h===
01800: FDBFB=h=h===h===DFBh=FT=B=h===DhBFFBBBh===FSFFFFFFFFFFFFFFFFFFFT
01c00: =FFFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFh==========
02000: =h===FFShh=FFShhhhhhBhhT=FFFFFShh=h===h===FFFFShFFShFFFFFh=FT=Fh
02400: FFFFShFFShT=FFFFSh=FFFFFFT=Sh=FFFFFSh=FFFFFFShFFFh=======FFFT=FF
02800: FFFFFFFFFFFSh=FFFFT=FFFhSSh=DhFFFFFFFFFFFFFFT=FFhSDhFFFFFFFFFFFF
02c00: FFFFFT=FFFFFFhFh================SDhFFFFFFFFFFFT=FFFFFhSDhFFFFFh=
03000: ======FFFFFFFT=FFFFFFFFFFFFFFFFhSDhFT=FFFFFFFFFFFFFFFFFT=FFFFFFF
03400: hSDhFFFFFFFFFFT=FFFFFFFFFFFFFFFFhSDhFFT=FFFFFFFFFFFFFFFFFT=FFFFF
03800: FFFFFFFFFFFFFFT=Fh=SDhFFFFFFFFFFFFFFFFT=FFFFFFFFFFhSDhFFh=======
03c00: h===FFFFFT=FFFFFFFFFFFFFFFFFhSDhFLT=h===FFFFFh===h===h===FFFh===
04000: FFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=hhTFFFFFFFFFFFFF
04400: FT=FFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFh====================
04800: ========================================h============h=====h====
04c00: ==h====h==h==h================h=================================
05000: ================================================================
05400: ================================================================
05800: ================================================================
05c00: ================================================================
06000: ================================================================
06400: ================================================================
06800: ================================================================
06c00: ================================================================
07000: ================================================================
07400: ================================================================
07800: ================================================================
07c00: ================================================================
08000: ================================================================
08400: ================================================================
08800: ================================================================
08c00: ================================================================
09000: ================================================================
09400: ================================================================
09800: ================================================================
09c00: ================================================================
0a000: ================================================================
0a400: ================================================================
0a800: ================================================================
0ac00: ================================================================
0b000: ================================================================
0b400: ================================================================
0b800: ================================================================
0bc00: ================================================================
0c000: ================================================================
0c400: ================================================================
0c800: ================================================================
0cc00: ================================================================
0d000: ================================================================
0d400: ================================================================
0d800: ================================================================
0dc00: ================================================================
0e000: ================================================================
0e400: ================================================================
0e800: ================================================================
0ec00: ================================================================
0f000: ================================================================
0f400: ================================================================
0f800: ================================================================
0fc00: ================================================================
10000: ================================================================
10400: ================================================================
10800: ================================================================
10c00: ================================================================
11000: ================================================================
11400: ================================================================
11800: ================================================================
11c00: ================================================================
12000: ================================================================
12400: ================================================================
12800: ================================================================
12c00: ================================================================
13000: ================================================================
13400: ================================================================
13800: ================================================================
13c00: ================================================================
14000: ================================================================
14400: ================================================================
14800: ================================================================
14c00: ================================================================
15000: ================================================================
15400: ================================================================
15800: ================================================================
15c00: ================================================================
16000: ================================================================
16400: ================================================================
16800: ================================================================
16c00: ================================================================
17000: ================================================================
17400: ================================================================
17800: ==============================FFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFF
17c00: FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFF
18000: FFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=
18400: FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFFT=FFFFFF
18800: FFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFF
18c00: FFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=F
19000: FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFF
19400: FFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFF
19800: FFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=F
19c00: FFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFF
1a000: FFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFF
1a400: FFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FF
1a800: FFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFF
1ac00: FFFFFFFFT=FFFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFF
1b000: FFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFh=============================FFF
1b400: FFFFFFT=FFFFFFFFFFFFFFFFFT=FFFFFFFFFFFFFFFFFT=hhTFFFFFFFFFFFFFFT
1b800: =FFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTFFFFFFFTF
1bc00: FFFFFFTFFFFFFFF.................................................
       (69 lines all free)
2d400: .F......F..FF...................................................
       (3 lines all free)
2e400: .................................................F..............
2e800: ................................................................
2ec00: ..F..F....................F...FF....

MEMORY MAP AFTER CLEANUP:
stack: 732 out of 7936
GC: total: 192064, used: 10768, free: 181296
 No. of 1-blocks: 144, 2-blocks: 31, max blk sz: 64, max free sz: 5031
GC memory layout; from 2000afa0:
00000: h=Mhh..D.h....Dh..DBBBh===DBDh====B=BBBBBB.B=B.B=BBB.B=.B.B=Bh==
00400: =BB=h===========.B=h=h===================h=====MD.h=============
00800: ====h===========================================================
00c00: ====h===========================================================
01000: ====h=hhhB.h.ThDSFhh=====h=..hD....B..hDh.D.Bh=B=..DB..B=.DBh==h
01400: ===Sh=======h========B..B=.BBFh=.DBB=h===h===Bhhh==..B=Bhh==h===
01800: .DB.B=h=h===h===D.Bh=...B=h===DhB..BBBh===.S....................
01c00: .....................................................h==========
02000: =h===..Shh=..Shh.....hh.......Sh..............Sh..Sh.....h=....h
02400: ....Sh..ShT=....Sh=........Sh=.....Sh=......Sh...h=======.......
02800: ...........Sh=.........hSSh=Dh..................hSDh............
02c00: ...F.........hFh================SDh..................hSDh.....h=
03000: ======.........................hSDh.............................
03400: hSDh............................hSDhF.........................F.
03800: .................h=SDh............................hSDh..h=======
03c00: h===........................hSDh................................
04000: ................................................................
04400: ...........................................h====================
04800: ========================================h============h=====h====
04c00: ==h====h==h==h================..................................
       (75 lines all free)
17c00: .......F........................................................
       (12 lines all free)
1b000: ...............................h=============================...
       (78 lines all free)
2ec00: ....................................

MEMORY MAP AFTER SUCCESSFUL BADGE INIT:
stack: 1396 out of 7936
GC: total: 192064, used: 108976, free: 83088
 No. of 1-blocks: 228, 2-blocks: 42, max blk sz: 4800, max free sz: 2562
GC memory layout; from 2000afa0:
00000: MDMhhh=DShhhShDhShDBBBh===DBDh====B=BBBBBBFB=BTB=BBBSB=SBSB=Bh==
00400: =BB=h===========SB=h=h===================h=====MDSh=============
00800: ====h===========================================================
00c00: ====h===========================================================
01000: ====h=hhhBhhSThDSFhh=====h=hhhDBT=hBh=hDhhDFBh=B=h=DBh=B=FDBh==h
01400: ===Sh=======h========Bh=B=FBBFh=FDBB=h===h===Bhhh==FFB=Bhh==h===
01800: FDBFB=h=h===h===DFBh=h==B=h===DhBFFBBBh===FSh===================
01c00: ====================================================Fh==========
02000: =h===FFShh=FSShhh=FFFhhShFFFFFShh=T=h=h===h=FFShFFShFFFFFh=hhhhh
02400: hhhhShhhShT=hh.hSh=h=======Sh=h=...Sh=h===.hSh..hh=======h===...
02800: h===.......Sh=h===h===hhSSh=Dh..................hSDhh===========
02c00: ===F........hhFh================SDh..................hSDh.....h=
03000: ======...............h=========hSDh.............................
03400: hSDh............................hSDhF.........................F.
03800: ................hh=SDh........hh=======...........hSDh..h=======
03c00: h===........................hSDh................................
04000: h===============================...................h=======h....
04400: ...........................................h====================
04800: ========================================h============h=====h====
04c00: ==h====h==h==h================..................................
05000: ................................................................
05400: ........................h=======................................
       (3 lines all free)
06400: ............................................................h===
06800: ====..................................................h=======..
06c00: ................................................h=======........
07000: ................................................................
07400: .............hh.................................................
       (5 lines all free)
08c00: ..................h=======......................................
09000: ........................h=======================================
09400: ======================..........................................
09800: ..............................................h.....h.....h.....
09c00: ..................h==...........................................
0a000: ..................h====h========================================
0a400: =========h=========h=====h===h===h=======================h======
0a800: ==h=======h===h=================================================
0ac00: ==================================h=====h=======================
0b000: ======================================h========h=======h========
0b400: ================h===============================================
0b800: ================================================================
0bc00: ================================================================
0c000: ================================================================
0c400: ================================================================
0c800: ================================================================
0cc00: ================================================================
0d000: ================================================================
0d400: ================================================================
0d800: ================================================================
0dc00: =====...........................................................
       (39 lines all free)
17c00: .......F........................................................
       (12 lines all free)
1b000: ...............................h=============================h==
1b400: ================================================================
1b800: ================================================================
1bc00: ================================================================
1c000: ================================================================
1c400: ================================================================
1c800: ================================================================
1cc00: ================================================================
1d000: ================================================================
1d400: ================================================================
1d800: ================================================================
1dc00: ================================================================
1e000: ================================================================
1e400: ================================================================
1e800: ================================================================
1ec00: ================================================================
1f000: ================================================================
1f400: ================================================================
1f800: ================================================================
1fc00: ================================================================
20000: ================================================================
20400: ================================================================
20800: ================================================================
20c00: ================================================================
21000: ================================================================
21400: ================================================================
21800: ================================================================
21c00: ================================================================
22000: ================================================================
22400: ================================================================
22800: ================================================================
22c00: ================================================================
23000: ================================================================
23400: ================================================================
23800: ================================================================
23c00: ================================================================
24000: ================================================================
24400: ================================================================
24800: ================================================================
24c00: ================================================================
25000: ================================================================
25400: ================================================================
25800: ================================================================
25c00: ================================================================
26000: ================================================================
26400: ================================================================
26800: ================================================================
26c00: ================================================================
27000: ================================================================
27400: ================================================================
27800: ================================================================
27c00: ================================================================
28000: ================================================================
28400: ================================================================
28800: ================================================================
28c00: ================================================================
29000: ================================================================
29400: ================================================================
29800: ================================================================
29c00: ================================================================
2a000: ================================================================
2a400: ================================================================
2a800: ================================================================
2ac00: ================================================================
2b000: ================================================================
2b400: ================================================================
2b800: ================================================================
2bc00: ================================================================
2c000: ================================================================
2c400: ================================================================
2c800: ================================================================
2cc00: ================================================================
2d000: ================================================================
2d400: ================================================================
2d800: ================================================================
2dc00: =============================================================...
       (3 lines all free)
2ec00: ....................................
insanire-monachus commented 1 year ago

Hello LionsPhil,

I am having issues with this program. When I run it attached w/ USB (under Thonny) I am able to launch my application. No issues. For Thonny; I just need to run main.py, select my program and reconnect to the device and the program launches. However if I disconnect and run on battery power it fails to load the application. I just get a black screen, and the need to reboot.

Cheers

Gadgetoid commented 1 year ago

@LionsPhil Memory fragmentation (and, in fact, memory usage in general) has been a huge thorn in our side for working with Pico/RP2040. The W taking a decent slice of memory from MicroPython's GC Heap hasn't helped either.

I suspect the canonical answer would be to simply not do what we're doing- since it's admittedly quite weird for a micro-controller and we're begging for this kind of trouble. PicoSystem handily sidesteps this issue by allocating the fixed-size graphics buffer at compile-time and reducing the size of MicroPython's "gc_heap" accordingly.

Any solution I can contrive involves moving some distance towards PicoSystem's (very inflexible) approach and allocating a chunk of RAM to use across scripts.

That said I don't understand MicroPython's garbage collection and heap allocation well enough to suggest anything else, but there's probably something I'm missing there.

LionsPhil commented 1 year ago

(Found the legend for those heap maps---it's a random float that's sometimes subdiving the large region, apparently. Guessing Tufty's Micropython port is one that heap-allocates floats...might be worth seeing what can be moved to fixed-point integers in main.py.)

What is the native boot process on the Pico like? I haven't been able to find that---is there some C[++] stub that the system jumps to that is then responsible for loading micropython and making it exec (python sense, not libc) main.py? If that exec returns, could it pluck some return value out of it (in normal python, apparently you get a dict of globals it modified), tear down and re-init micropython to completely reset its heap, and run the script it indicated instead? That would allow for a better workaround withouth the full device restart of having to write to the filesystem, but I don't know if these assumptions hold.

Even if not, if micropython gets restarted if it exits (I noticed this was documented for a different micropython device), maybe using sys.exit() instead of machine.reset() in the workaround gist would at least upset Thonny less.

Gadgetoid commented 1 year ago

MicroPython's main loop is, in fact, here:

https://github.com/micropython/micropython/blob/3b5cafc7163724a50b3d077baf83c4d8c7b6de4a/ports/rp2/main.c#L154-L222

It's owned by the MicroPython project, though, which makes it rather difficult for us to make application specific changes to. Not unheard of, by any means, but the fewer hacks we rely upon the better :laughing:

Would fixing Thonny to gracefully handle soft resets into new scripts be feasible, I wonder? I don't really know much about the specifics of how it interacts with MicroPython.

It's possible to use a C/C++ module and C's own heap (unknown to MicroPython) to persist data across soft resets without a flash write. Not sure if it's sensible, but it's definitely possible.

LionsPhil commented 1 year ago

Yeah, rats, sys.exit() just drops it into the REPL, even if I sys.exit(0x100) (which should be PYEXEC_FORCED_EXIT, but it looks like it's not actually propagated). Guess that other board may indeed apply a hack.

What I have done is rearchitected main.py a bunch so as little as possible is global, in the hopes it helps the GC collect it all before trying to load the application script, and I think that's helped. I seem to be fairly consistency getting 167 contiguous free lines now, and my own badge script is loading without issues (although it's changed in the meantime, so that's not a perfect test). It's also got type annotations because I was going to experiment with integer math and @micropython.viper to keep things to stack-friendly ints, but that might be turning out to be pointlessly overcomplicated. Updated the gist with a new revision: https://gist.github.com/LionsPhil/26c640356b074513d6c612a199db9d3f

If that seems to be promising I can rip the whole reboot-on-B part back out (and probably the memory debugging) and turn it into a PR.