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:
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:
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:
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:
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
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