Teensy 4.1: 1.79 s CPU stalls / bus fault from the 32 MB FlexSPI2 MPU region when less is fitted

SamiH

Member
Hi all,

AI content warning: this was found by Claude while debugging my firmware. I'm posting the findings because it looks like a general Teensy 4.1 issue, with a simple fix.

Setup: Teensy 4.1, Teensyduino 1.62, 600 MHz, 16 MB PSRAM (two 8 MB chips).

SYMPTOM
Our firmware (8 kHz IntervalTimer, USB host talking to three other Teensys, logs in EXTMEM) froze for 1.79 s, usually twice back to back (3.58 s), once every 7.16 s. 7.16 s is exactly 2^32 CPU cycles at 600 MHz.

CAUSE
configure_cache() in startup.c maps the FlexSPI2 window as 32 MB of normal, cacheable memory:

Code:
SCB_MPU_RBAR = 0x70000000 | REGION(i++); // FlexSPI2
SCB_MPU_RASR = MEM_CACHE_WBWA | READWRITE | NOEXEC | SIZE_32M;

With 16 MB fitted, 0x71000000-0x71FFFFFF is "normal memory" with no chip behind it. With 8 MB or no PSRAM, even more of the window is empty. The Cortex-M7 may read speculatively from normal memory, which is the problem the ARM article linked in configure_cache() describes. The 4 GB no-access background region protects everything else, but not this part of the window.

Our code keeps ARM_DWT_CYCCNT values in registers. For about 28 ms of every 2^32-cycle wrap, the counter's value lies in 0x71xxxxxx, and the core reads ahead there. FlexSPI2 then waits for data from a chip that isn't there until its AHB timeout expires: MCR1 AHBBUSWAIT is 0xFFFF, so 0xFFFF x 1024 cycles = 111.85 ms. That happens 16 times: 16 x 111.85 ms = 1.79 s. Any real PSRAM access made in that time waits behind it. A speculative read never faults, so all you see is the stall.

MINIMAL REPRODUCTION
A plain read of the first address past the fitted PSRAM:

Code:
extern "C" uint8_t external_psram_size;

void setup() {
  while (!Serial) ;
  if (CrashReport) Serial.print(CrashReport);
  volatile uint32_t *hole = (volatile uint32_t *)(0x70000000 + external_psram_size * 0x100000);
  Serial.println(*hole);   // never prints
}

void loop() {}

On our 16 MB board the CPU is held for over 10 s. An IntervalTimer at priority 32 printing every 250 ms only gets in about once every 1.79 s. Then it takes a precise bus fault:

Code:
CFSR: 8200
  (PRECISERR) Data bus error (address in BFAR)
  (BFARVALID) Accessed Address: 0x71000000

HOW IT WAS CONFIRMED
- AHBBUSWAIT 0xFFFF -> 0x0FFF made the stalls 16x shorter (1.79 s -> 111.8 ms).
- PSRAM clock 105.6 MHz -> 88 MHz: no change.
- The stalls start when ARM_DWT_CYCCNT is between 0x7157xxxx and 0x71E9xxxx. Writing a different start value into ARM_DWT_CYCCNT moves the stalls with it.
- A probe sampling the interrupted PC from SysTick found the CPU parked on ordinary EXTMEM loads and stores during each stall.
- Heavy PSRAM + flash traffic on its own, with forced cache misses, never stalled.
- With the fix below: no stalls in a 10-minute run (about 84 passes of the counter through that range, 1.8 M USB packets, worst timer gap 125 us). The direct read above now faults immediately (DACCVIOL, MMFAR 0x71000000) instead of hanging.

FIX
Make the unfitted part of the window strongly ordered and no-access once the PSRAM size is known. We do it from startup_late_hook(), using MPU regions 15..13; higher-numbered regions override the one configure_cache() sets:

Code:
extern "C" uint8_t external_psram_size;

extern "C" FLASHMEM void startup_late_hook(void)
{
    uint32_t addr = 0x70000000 + external_psram_size * 0x100000;
    const uint32_t end = 0x72000000;              // configure_cache(): SIZE_32M
    uint32_t region = 15;
    while (addr < end && region >= 13) {
        uint32_t size = addr & (~addr + 1);       // largest aligned power of two...
        while (addr + size > end) size >>= 1;     // ...that fits
        SCB_MPU_RBAR = addr | SCB_MPU_RBAR_REGION(region) | SCB_MPU_RBAR_VALID;
        SCB_MPU_RASR = SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_AP(0) | SCB_MPU_RASR_XN
                     | SCB_MPU_RASR_SIZE(30 - __builtin_clz(size)) | SCB_MPU_RASR_ENABLE;
        addr += size;
        region--;
    }
    asm volatile("dsb");
    asm volatile("isb");
}

In the core itself, the natural place would be configure_external_ram(), after the PSRAM size is detected: either size the FlexSPI2 region to what's fitted, or add the no-access region there. If any board maps something else in that window, that range would need to stay accessible.

Thanks for all the work on Teensy!

I apologize if this was a waste of time
 
Hi all,

Second report from the same project, again found by Claude while debugging my firmware. Teensy 4.1, Teensyduino 1.62, USB type Serial.

SYMPTOM
Short writes to Serial can stay on the Teensy indefinitely if the program keeps calling Serial.availableForWrite(). For us, the tail end of a long log dump only arrived once the PC sent a byte.

REPRODUCTION
Code:
void setup() {
  while (!Serial) ;
}

void loop() {
  static uint32_t n = 0;
  Serial.printf("line %lu at %lu ms\n", n++, millis());
  uint32_t t0 = millis();
  while (millis() - t0 < 200) {
    (void)Serial.availableForWrite();   // remove this line and it works
  }
}

Expected: one line every 200 ms. With the stock usb_serial.c, only 99 of about 150 lines arrived within 30 s, and 38 of those were more than 100 ms late (worst 1.4 s). Without the availableForWrite() call, all 150 arrived on time. With TX_NUM raised to 16, as we use, it's worse: 35 of 150, worst 2.2 s.

CAUSE
A write that leaves a partly filled buffer starts the 75 us one-shot flush timer. usb_serial_flush_callback() returns without sending if tx_noautoflush is set, and nothing re-arms the timer. usb_serial_write_buffer_free(), which is behind availableForWrite(), sets tx_noautoflush for its whole scan. So a loop that polls availableForWrite() holds the flag almost all the time, and the flush is nearly always lost. The data then waits for the next write or for the buffer to fill.

FIX
Hold off the USB device interrupt during the scan instead of setting the flag. tx_head still can't change under the scan, and the flush runs right after it instead of being dropped:

Code:
int usb_serial_write_buffer_free(void)
{
    uint32_t sum = 0;
    const uint32_t usb_irq_was_enabled = NVIC_IS_ENABLED(IRQ_USB1);
    NVIC_DISABLE_IRQ(IRQ_USB1);
    asm("dsb" ::: "memory");
    asm("isb");
    for (uint32_t i=0; i < TX_NUM; i++) {
        if (i == tx_head) continue;
        if (!(usb_transfer_status(tx_transfer + i) & 0x80)) sum += TX_SIZE;
    }
    if (usb_irq_was_enabled) NVIC_ENABLE_IRQ(IRQ_USB1);
    return sum;
}

With this change the test above delivers 150 of 150 lines on time (worst 61 ms, which is PC-side). Streaming throughput is unchanged (about 9.8 MB/s), and our long dumps no longer hang. Making the callback re-arm the timer when it finds the flag set wouldn't be enough on its own: a polling loop holds the flag so much of the time that the retries keep losing too.

usb_serial2.c, usb_serial3.c and usb_seremu.c have the same tx_noautoflush pattern in their write_buffer_free functions. We haven't tested those.

Thanks!
 
Nice to know Claude is scraping the forum, I guess...
Hi jmarsh,
I am not Claude, but I was using claude to write some code, I am not an expert in microcontroller coding in anyway, nor do i want to take up time of expert people, on ai hallucinations, I apologize if the post is a waste of people's time, I will take it down, if asked, really don't know what to do here, if I am violating forum etiquette or policy or even like untold norms, please let me know, I do not want to pollute this forum, I enjoy reading people's problem from time to time and the offered solutions, I would like not to pollute the space either with weird ai question, I only posted cause i came across sth and didn't know what to do
 
You're fine, I was just making the point that we figured out that exact problem (including modifying AHBBUSWAIT to confirm it) just last month, and Claude has already latched on to it whereas before it had no idea.
 
Back
Top