High Ram1 usage

FogozIV

New member
Hello everyone,


I'm working on a Teensy 4.1 project and running into RAM usage issues due to the default behavior of placing most code in ITCM (RAM). I understand that, by default, functions are placed in RAM unless explicitly marked with FLASHMEM or PROGMEM.


However, instead of manually annotating every non-critical function, I'd like to reverse the default behavior:


  • All code should go to QSPI Flash by default
  • Only functions marked with FASTRUN should go into ITCM

To achieve this, I created a custom linker script that moves the default .text section to FLASH. My goal is to reduce ITCM usage significantly while preserving the normal behavior of FASTRUN, DMAMEM, and other sections like .bss and .data.




❗ Issues I'm Facing​


I’m encountering a few issues, and I’d appreciate any guidance:


  1. Crashes or freezes when using DMAMEM arrays
    • These sometimes cause reboots, or worse, a hard freeze that never recovers.
    • I’m wondering if this could be caused by the combination of my linker script and TeensyThreads, which I’m using in my project.
  2. Possible instability from my linker script
    • I don’t have much experience with linker scripts, so I’m not sure whether I’ve done something subtly wrong.
    • I'd really appreciate if anyone has a known-good linker script that does what I'm aiming for — moving .text to Flash by default and keeping everything else (like FASTRUN, DMAMEM, etc.) working correctly.



🙏 What I'm Looking For​


  • Has anyone written a linker script for Teensy 4.1 that matches this layout and is known to work well?
  • Is there a known issue when combining custom memory layouts with TeensyThreads and DMAMEM?
  • Could the crash be caused by something subtle like misalignment or improper MPU region mapping?

Sorry if this has been asked before — I searched the forum but couldn’t find a thread discussing a similar custom linker setup.


Thanks in advance!
 

Attachments

  • custom_linker.zip
    1.3 KB · Views: 46
Hi again,


Sorry, I forgot to mention in my original post — the full (work-in-progress/bad) code and custom linker script can be found publicly on GitHub here:


🔗 https://github.com/FogozIV/PAMITeensy


Feel free to take a look if you're curious about the setup or want to point out anything obviously wrong. I'm aware it's a bit messy right now, but any feedback or suggestions would be super helpful!


Thanks again for your time and help!
 
Erratum: Possible Cause of the Issue - Core Functions Needing RAM

Upon further testing, it seems that the issue isn’t fully resolved. It appears that some core functions, which are essential for normal operation, might need to remain in RAM but are currently being placed in QSPI Flash due to my custom linker script. This includes functions related to:

EEPROM access

Possibly other core system functions that expect to execute from RAM


These functions may not behave correctly when placed in Flash, and this could be causing the crashes and freezes I've been experiencing. However, I am not yet certain that this is the root cause, and further investigation is needed to confirm.

Conclusion

At this point, it seems that the issue is not fully resolved, and it could be related to certain core functions needing to reside in RAM for proper operation. I’ll continue to investigate this and adjust the linker script accordingly, but I wanted to share this potential cause in case others are encountering similar issues.

Request for Feedback

If anyone has encountered similar issues or has any insights into how to handle this, I’d greatly appreciate any input or feedback. I’m still stuck and would love some help resolving this.
 
Hello,

I cannot say something about the errors/problems you're experiencing directly, but I have tinkered also with the linker script some time ago. I did not change the default behaviour, but I had solution for placing whole files in flash instead in RAM. This solution seemed to work back then. (But is not tested well.) Maybe this could help too. 🤔

I inserted in the (default) linker script the section ".angelscriptFlash" below the section ".text.progmem".
(Name of the inserted section should not matter. I began to port the script language AngelScript to the Teensy 4.1 and the AngelScript code does not fit in ITCM.)

This is the inserted section:
Code:
.angelscriptFlash : {
    *as_*.o (.text .text* .rodata .rodata*)
    . = ALIGN(4);
} > FLASH

The angelscript source files start all with "as_" in the file name, therefore the corresponding object files start all with "as_" in the file name too.
The line "*as_*.o (.text .text* .rodata .rodata*)" in combination with "> FLASH" at the end of the section means, that all code ("text") and all constant data ("rodata") of object files containing "as_" ("*as_*.o") in the file name are placed in flash.

I have attached the full linker script for reference.

P.S.: Feedback or corrections are welcome. 😊
 

Attachments

  • imxrt1062_t41_angelscript.ld.txt
    2.7 KB · Views: 40
The EEPROM writing functions shouldn't be a problem unless you actually make use of them.
Since you mention TeensyThreads that opens up a whole can of worms since threads may end up using cached memory for their stack, leading to all sorts of issues with other libraries that assume pointer arguments reference uncached memory.
 
Back
Top