🛠 Comprendre la mémoire : 2 Mo de flash, 1 Mo de RAM TCM/DMA, EEPROM émulée et LittleFS

Teensy 4.0 · intermediaire · Schéma : IMG-teensy-40-intermediaire-6-teensy40-memoire-flash-ram-eeprom.svg — carte mémoire du Teensy 4.0 : puce flash externe W25Q16JV 2 Mo (1984 Ko utilisables, 64 Ko réservés EEPROM+restore), RAM 1024 Ko coupée en RAM1 (TCM, accès 1 cycle) et RAM2 (optimisée DMA), 16 octets de RTC RAM sur pile VBAT, 1080 octets d'EEPROM émulée.

Objectives

The memory architecture of the Teensy 4.0 is its greatest strength — and its main porting pitfall. The NXP chip has no embedded flash: the program lives in an external Winbond W25Q16JV SPI flash of 2 MB (1984 KB usable — the top 64 KB store the emulated EEPROM and the LED-blink fallback program), executed via a 65536-byte cache at 66 MB/s. On the RAM side: 1024 KB total, split between RAM1 (TCM, single-cycle access via 64-bit ITCM/DTCM bus) and RAM2 (optimized for DMA). You will learn the placement keywords and the emulated EEPROM.

Placement Keywords

Complete Code

// Démonstration des zones mémoire + EEPROM émulée.
#include <EEPROM.h>

const char message[] PROGMEM = "constante en flash (F pour les littéraux)";
DMAMEM uint16_t tamponDMA[1024];        // RAM2 : idéal pour le DMA
FASTRUN void cheminCritique {         // code dans RAM1 (TCM)
  for (uint32_t i = 0; i < 1024; i++) tamponDMA[i] = i * 3;
}

void setup {
  Serial.begin(9600);
  Serial.println((const char*)message);

  // EEPROM : 1080 OCTETS émulés dans la flash (pas de vraie EEPROM !)
  uint8_t compteur = EEPROM.read(0);
  compteur++;
  uint32_t t0 = micros;
  EEPROM.update(0, compteur);   // écriture : le code S'ARRÊTE un instant
  uint32_t duree = micros - t0;

  Serial.printf("session %u, écriture EEPROM = %u us\n", compteur, duree);
}

void loop { cheminCritique; delay(1000); }

For files (logs, audio samples), the remaining flash is used via LittleFS: SD.begin(BUILTIN_SDCARD) targets the SDIO connector on the bottom pads, while LittleFS forms the free flash zone — same File API.

Expected Verification

The session counter survives power loss (1080 persistent bytes). Measuring micros around EEPROM.update shows the micro-execution stall documented by PJRC: writing from flash interrupts the code — to be banned from a real-time audio loop. Compare the duration of a cheminCritique loop with and without FASTRUN to feel the difference between TCM and cached flash.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials