Teensy 4.1: exact 2^30-cycle CPU stall (1491 ms @720 MHz), depends on the build

Under what situation would the MPU be disabled? I think that would break all sorts of things.
I agree, I don’t see a normal case where the MPU is disabled. The case I had in mind is code that reprograms MPU region 9 (a library or a sketch that widens it again or reuses the slot), where the FLSHA2CR0 coverage would be the only protection left. I’m testing fix #2 on its own only to get that measurement, by widening the region to 32 MB in a sketch, not because I expect the MPU to be off.
 
Opps, sorry, I mis-spoke. The MPU shouldn't get disabled. I only meant disabling the 6 lines which replace the default 32MB config with the actual PSRAM size. I should know better to be precise... wrote this after a session testing lots of stuff with 5 boards.

My hope is to get the FLSH*CR0 registers configured so we don't stall, even if the MPU remains set for 32MB. I'm also a bit curious if the stall problem gets worse if the MPU is configured for a larger size like 64M or 128MB? I guess you could say I'm still really curious to learn what we can about this speculative access issue...

Secondary goal is to use smaller FLSH*CR0 numbers, so as much of the FlexSPI2 address space remains available for LittleFS to use a flash chip.
 
Here are the measurements you asked for, all on the same board: Teensy 4.1, 2 x APS6404L (8 MB + 8 MB = 16 MB), core 1.62.0 with your startup.c v2 installed (at boot FLSHA1CR0=0x2000, FLSHA2CR0=0x8000, MPU region 0x70000000 = 32 MB). F_CPU 720 MHz.

I used jmarsh's PLD test, extended so that it alternates every 4 s between a valid address (0x70100000) and an address that is NOT backed by memory, and measures the worst PLD time plus FLEXSPI2 INTR. For each row the sketch rewrites the SIZE field of the MPU region at 0x70000000 (SRD cleared) and, where noted, FLEXSPI2_FLSHA2CR0 at runtime (MCR0[MDIS] toggled around the write).

Code:
row  MPU     A1+A2 (CR0)       unbacked address   result
1    32 MB   8 + 32 = 40 MB    0x71C00000 (28 MB) clean, INTR 0x40, worst PLD < 1000 cycles
2    64 MB   8 + 32 = 40 MB    0x72800000 (40 MB) STALL: 1.491 s, then 2.983 s per cache line, INTR 0x440
3    128 MB  8 + 32 = 40 MB    0x73000000 (48 MB) STALL: same 1.491 s / 2.983 s pattern, INTR 0x440
4    32 MB   8 + 24 = 32 MB    0x71C00000 (28 MB) clean, FLSHA2CR0=0x6000, INTR 0x40
5    32 MB   8 + 23 = 31 MB    0x71F00000 (31 MB) STALL: 1.491 s, then 2.983 s per cache line, INTR 0x440

What I read from this:

  • Your "fix #2 without fix #1" (MPU left at 32 MB, only the FLSH*CR0 values changed) removes the stall for everything that lies inside the CR0 coverage (rows 1 and 4).
  • Enlarging the MPU region to 64 MB or 128 MB without also enlarging the CR0 coverage brings back exactly the stock behaviour for addresses beyond A1+A2 (rows 2 and 3). It is not worse than stock: same 2^30 cycles first, then 2^31 per line, INTR 0x440. So a bigger MPU region does not make the stall longer, it just exposes the uncovered range again.
  • Minimal CR0: with 2 x 8 MB, A2 = 24 MB (A1 + A2 = 32 MB, exactly the MPU region size) is enough (row 4). One MB less (A2 = 23 MB, row 5) and the last MB of the 32 MB MPU region stalls again. So the rule that works here is simply "CR0 coverage must reach the end of the MPU region", nothing more is needed.

Caveats: single board, one PSRAM configuration (2 x 8 MB), and I only probed one unbacked address per row. I have not tried the 8 MB-only or no-PSRAM cases.

Also, the overnight logger result with your startup.c v1 (MPU region 9 = detected size, 16 MB): 10 h 04 min continuous logging on my pocket logger, 8 of the windows where stock core stalls (first at ~1886 s, then every 4295 s) passed with 0 bad records and a max GAP of 62 ms. Next I will run the logger overnight with v2.
 
The extra 16MB PSRAM chips arrived today. Will soon test the 24MB cases. Just now soldered them...

psram_24mb.jpg
 
Today I'm running 4 tests on each of the 7 boards. Just to quickly document how I'm actually testing, these are the 4 programs:
  1. MPU size test, code below
  2. PSRAM memtest
  3. Fault timing, msg #16
  4. Preload instruction, msg #37 - slightly modified, code below
The 7 test boards have these memory chips:
  1. No PSRAM and Winbond 25N01G flash
  2. 8MB PSRAM and Gigadevice 25Q256D flash
  3. 8MB + 8MB PSRAM
  4. 16MB PSRAM and Winbond 25N01G flash
  5. 8MB + 16MB PSRAM
  6. 16MB + 8MB PSRAM
  7. 16MB + 16MB PSRAM
All tests are using the second version startup.c from msg #50.

This MPU size test just writes to the PSRAM at 1MB increments until the MPU detects a fault.

Code:
extern "C" uint8_t external_psram_size;

void setup() {
  Serial.begin(9600);
  Serial.println("PSRAM settings");
  Serial.printf("external_psram_size = %u\n", external_psram_size);
  uint32_t a1 = FLEXSPI2_FLSHA1CR0;
  uint32_t a2 = FLEXSPI2_FLSHA2CR0;

  Serial.printf("A1 size = %04X (%uMB)\n",  a1, a1 >> 10);
  Serial.printf("A2 size = %04X (%uMB)\n",  a2, a2 >> 10);
  SCB_MPU_RNR = 9;
  uint32_t rbar = SCB_MPU_RBAR;
  uint32_t rasr = SCB_MPU_RASR;
  Serial.printf("MPU: addr:%x, config:%x", rbar & 0xFFFFFFE0, rasr);
  //uint32_t bytes = 2 << ((rasr >> 1) & 0x1E);
  //Serial.printf(" (%u bytes)\n", bytes);
  Serial.printf(" (%s)\n", mpu_size(rasr));

  for (uint32_t addr = 0; addr <= 32; addr += 1) {
    volatile uint32_t *ptr = (uint32_t *)(0x70000000 + addr * 1024 * 1024);
    Serial.printf("  write at %uMB...", addr);
    delay(250);
    *ptr = 0;
    delay(50);
    Serial.println("OK");
  }
}

const char *mpu_size(uint32_t r) {
  if ((r & 1) == 0) return "disabled";
  r &= 0xFF3E;
  if (r == SCB_MPU_RASR_SIZE(22)) return "8MB";
  if (r == SCB_MPU_RASR_SIZE(23)) return "16MB";
  if (r == (SCB_MPU_RASR_SIZE(23) & SCB_MPU_RASR_SRD(0xC0))) return "24MB";
  if (r == SCB_MPU_RASR_SIZE(24)) return "32MB";
  return "unknown";
}

void loop() {
}

This is the preload instruction test from msg #37, slightly modified to use the detected PSRAM size, rather than fixed at 16MB

Code:
// Should blink at 1 Hz

// blinks every 3.58 second if preload instruction causes FlexIO stall

// from thread 78171 msg #37
// https://forum.pjrc.com/index.php?threads/78171/post-370932

elapsedMillis ticker;
uint32_t addr;
extern "C" uint8_t external_psram_size;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWriteFast(LED_BUILTIN, LOW);
  addr = 0x70000000 + external_psram_size * 1024 * 1024;
}

void loop() {
  static uint16_t offset;
  if (ticker > 500) {
    digitalToggle(LED_BUILTIN);
    ticker = 0;
  }
  asm volatile("pld [%0, %1]" :: "r"(addr), "r"(offset));
  ++offset;
}
 
Last edited:
Ok, I've run all 28 tests. Everything looks good.

Code:
Board           MPU     Memtest Fault Timing    Preload
-----           ---     ------- ------------    -------
None + flash    0MB     none    112 cycles      1 Hz
8MB + flash     8MB     pass    112 cycles      1 Hz
8MB + 8MB       16MB    pass    112 cycles      1 Hz
16MB + flash    16MB    pass    112 cycles      1 Hz
8MB + 16MB      24MB    pass    112 cycles      1 Hz
16MB + 8MB      24MB    pass    112 cycles      1 Hz
16MB + 16MB     32MB    pass    112 cycles      1 Hz

Unless anyone has further comments or concerns, I'm going to commit the msg #50 (version 2) startup code later today.

Will do LittleFS testing at a later time...
 
Back
Top