-
-
Notifications
You must be signed in to change notification settings - Fork 110
fix: use malloc() for LVGL pool on CONFIG_SPIRAM_USE_MALLOC builds #354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MuddyPaws2
wants to merge
1
commit into
meshtastic:master
Choose a base branch
from
MuddyPaws2:fix/lv-conf-psram-malloc-cap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+6
−2
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: meshtastic/device-ui
Length of output: 6289
🏁 Script executed:
Repository: meshtastic/device-ui
Length of output: 4548
🌐 Web query:
ESP-IDF CONFIG_SPIRAM_USE_MALLOC heap_caps_aligned_alloc MALLOC_CAP_SPIRAM returns NULL💡 Result:
In ESP-IDF, when
CONFIG_SPIRAM_USE_MALLOCis enabled, the heap allocator adds PSRAM to the pool of memory capable of satisfyingmalloc()andheap_caps_malloc()requests [1][2]. Ifheap_caps_aligned_alloc(alignment, size, MALLOC_CAP_SPIRAM)returnsNULL, it indicates that the allocator was unable to find a contiguous block in the external PSRAM that meets both the size and alignment requirements [3][4][5]. Common reasons and troubleshooting steps include: 1. PSRAM Initialization Failure: Ensure that the PSRAM is successfully initialized during boot. Check your serial logs for messages like "PSRAM initialized" or "Adding pool of... of external SPI memory to heap allocator" [6][7]. If the PSRAM is not initialized,MALLOC_CAP_SPIRAMallocations will fail [7]. 2. Alignment and Size Constraints: Theheap_caps_aligned_allocfunction requires a contiguous block of memory [4][5]. Even if the total free PSRAM appears sufficient, heap fragmentation may prevent the allocation of a single block of the requested size and alignment [8]. You can check the largest free block in PSRAM usingheap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM)to verify if the requested size can actually be satisfied [6]. 3. MisunderstandingMALLOC_CAP_SPIRAMvs.MALLOC_CAP_DEFAULT: WhileCONFIG_SPIRAM_USE_MALLOCallowsmalloc()to use PSRAM, explicitly requestingMALLOC_CAP_SPIRAMtells the allocator to only look in PSRAM [1][2]. If PSRAM is full or fragmented, this call will returnNULLeven if internal memory is available [3][5]. 4. Configuration Conflicts: Ensure yoursdkconfigsettings are correct for your specific module. CheckCONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL, which defines a threshold below which allocations prefer internal memory [1][9]. Also, verify that other components (like WiFi/LWIP) are not exhausting the PSRAM pool if you have enabled options likeCONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP[10]. To diagnose, try the following: - Useheap_caps_get_free_size(MALLOC_CAP_SPIRAM)andheap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM)to see the current state of the PSRAM heap [9][6]. - If you suspect fragmentation, consider if you can useEXT_RAM_BSS_ATTRfor static buffers instead of dynamic allocation [8][2]. - Verify that your alignment value is a power of two, as required by the API [4].Citations:
Gate the
malloc()allocator onCONFIG_SPIRAM_USE_MALLOC.BOARD_HAS_PSRAMalso covers builds whereCONFIG_SPIRAM_USE_MALLOCis disabled, in which ESP-IDF does not automatically integrate PSRAM into the default heap. In those builds,LV_MEM_POOL_ALLOCcurrently routes PSRAM-backed LVGL draw buffers throughmalloc()instead of the existingheap_caps_aligned_alloc(..., MALLOC_CAP_SPIRAM)path, so allocations can consume internal RAM or fail.🤖 Prompt for AI Agents