🛠 Ingénierie mémoire du Teensy 4.1 : TCM, DMAMEM, EXTMEM, flash QSPI et EEPROM
Objectives
The Teensy 4.1 is a memory machine: 1,024 KB of RAM (512 KB RAM1 in single-cycle access TCM + 512 KB RAM2 optimized for DMA), 8 MB of QSPI flash (7,936 KB usable, with the upper 256 KB reserved for emulated EEPROM and the backup program), two 32 KB caches, and two empty QSPI footprints under the board for soldering PSRAM. This tutorial consciously places each piece of data in the right location.
Keywords
FASTRUN: code placed in the instruction TCM (ITCM) — the default behavior of the Teensy compiler.DMAMEM: buffer in RAM2, the only memory optimized for DMA; the heap (malloc, new, Arduino String) also lives there.EXTMEM+extmem_malloc: data in the soldered PSRAM. Beware of the documented pitfall: with a single chip, it must go on the smaller of the two footprints; the larger one accommodates certain LittleFS flash chips.PROGMEM/F: constants in QSPI flash (flash bandwidth listed at 1,984 MB/s in the techspecs).- EEPROM: 4,284 emulated bytes — and each write pauses execution from flash. To be banned from real-time loops.
- 16 bytes of RTC RAM saved by battery (LPGPR0-LPGPR3) survive power-off.
Complete Code
#include <InternalTemperature.h>
DMAMEM uint8_t audioBuf[8192]; // RAM2, compatible DMA
EXTMEM uint8_t bigBuf[1024 * 1024]; // PSRAM : ne lie que si la puce est soudée
const char msg[] PROGMEM = "constante dans le flash QSPI";
void setup {
Serial.begin(9600);
while (!Serial && millis < 3000);
}
void loop {
Serial.print("Temp. die : ");
Serial.print(InternalTemperature.readTemperature); // capteur embarqué
Serial.print(" C | tas libre (RAM2) : ");
Serial.print(malloc_free / 1024);
Serial.println(" Ko");
delay(1000);
}
Expected Verification
With a PSRAM chip soldered on the small footprint, the sketch compiles with EXTMEM and the monitor displays die temperature and free heap. Without PSRAM, the EXTMEM keyword causes a compilation error — this is the signal to check the footprint.
Common Errors
- Soldering the single PSRAM chip on the large footprint: non-functional, documented by PJRC.
- Placing a DMA buffer in RAM1: DMA targets RAM2; use
DMAMEM. - Writing to EEPROM on every loop to "save" data: writes block execution from flash — guaranteed micro-stutters.
- Counting 8,192 KB of free flash: subtract the 256 KB reserved → 7,936 KB.
- Ignoring overclocking: 912 MHz is officially supported with automatic core voltage increase (1.25 V at 600 MHz); for sustained operation, plan for thermal management — PJRC notes that returning to ≤ 528 MHz reduces power consumption (~100 mA typical at 600 MHz).
Other tutorials for this board
- Premiers pas sur le Teensy 4.1 : Teensyduino, Teensy Loader et premier Blink
- GPIO et PWM sur le Teensy 4.1 : 55 broches en 3,3 V, ni 5 V tolérées ni traîne
- Entrées analogiques du Teensy 4.1 : 18 broches, 2 ADC, 0-3,3 V fixes et zéro DAC
- Logger sur carte SD via le port SDIO natif du Teensy 4.1
- Serveur web embarqué : l'Ethernet natif 10/100 du Teensy 4.1 (DP83825)
- Boîte à rythmes USB MIDI : classes MIDI, MIDIx16 et port hôte USBHost_t36
Code examples for this board
clignotante.inoArduinotouche-capacitive.inoArduinoanalogique-12-bits.inoArduinomidi-piano.inoArduinopwm-respiration.inoArduinoservo-angle.inoArduino