STM32 gotchas
252. Memory mapped QUADSPI reads may "fail", when timeout counter is enabled

This article has been contributed by Andrew Berry.

This problem is now mentioned in the 'F767/'F777 errata sheet (2.4.3 Memory-mapped read operations may fail when timeout counter is enabled), although it wasn't when I first ran into it:

In memory-mapped mode with the timeout counter enabled (by setting the TCEN bit of the QUADSPI_CR register), the QUADSPI peripheral may hang and memory-mapped read operation fail. This occurs if the timeout flag TOF is set at the same clock edge as a new memory-mapped read request.

What the errata sheet still leaves out is that "fail" in this case doesn't mean 'cause a hardfault' -- it means the entire chip comes to a dead stop, including the debug interface. The watchdog timer still works, but all execution stops, including the early warning interrupt, so good luck isolating the issue if you don't already know where to look.

Even worse, the failure depends on two uncorrelated events happening on the same clock cycle -- and on a dual-issue, superscalar part with out-of-order accesses, no less -- so this is prime Heisenbug material, as any changes made to diagnose the behavior are certain to change it, as are interrupts/user input timing. This issue cost about four weeks of my life, as it only appeared many months into a complex project, so I had no reason to suspect the QUADSPI was causing these random lockups that didn't correlate to any observable behavior.

The problem can be easily replicated, even with no external flash IC connected -- just configure the QUADSPI for memory mapped mode, and start reading from it in a loop while sweeping the timeout value. Fortunately as the errata sheet indicates the solution is simple: just don't use the timeout counter.

While this issue has been obeserved on 'F767/'F777, presumably it affects all other 'F7/'H7 parts that share the same QUADSPI IP.