Help implementing full USB Audio Class 2.0 (UAC2) async on Teensy 4.1 (32-bit / 786 kHz)

Two questions from your testing: (1) under a continuous real-time load, audio DMA plus USB interrupts firing through the wait, did you measure the worst-case latency an interrupt picks up from a suspend/resume cycle? (2) How often does the AHB sequence fail to auto-resume in practice, i.e. how load-bearing is your manual-resume fallback? My flash is the stock Teensy 4.1 part, if the suspend timing is part-specific
In my real-world testing the flash programming never gets suspended; for that to happen a triggered interrupt has to access flash and I don't have anything like that in my project.
 
(2) How often does the AHB sequence fail to auto-resume in practice,

Maybe there is some misunderstanding? Or maybe I'm reading too much from only a few words like "AHB sequence" and "auto-resume"?

Just in case of misunderstanding, please let me explain the pretty limited hardware capability and how the EEPROM emulation software in the core library is intended to work with the hardware we actually have.

Most important point is the FlexSPI hardware in RT1062 doesn't have any specific hardware support to "know" when the flash memory is busy. FlexSPI is configurable with up to 16 custom sequences. But those sequences can only perform individual access operations, like reading data, or reading the status register, or enabling write, or stating a write, or starting a sector erase.

Flash memory chips become "busy" when you start any write or erase. During the busy time, only a few operations actually work. The main one is reading the flash chip's status register, where bit 0 tells if the chip is still busy.

The AHB bus interface to FlexSPI is also pretty simple. Or perhaps simple isn't the right word, it's limited in scope. When any bus request for the flash memory range (starting at 0x60000000) is made, which normally comes from the CPU if it tries to execute a FLASHMEM function where the code isn't already in the Cortex-M7 32K instruction cache, or in FlexSPI's own small buffer, then FlexSPI sequence 0 is executed. Sequence 0 is set up at boot time to read from the flash chip. That setup happens initially based on the boot config fuses to bring the hardware up enough so NXP's ROM can read the first 512 bytes of flash (using normal 1 bit SPI at only 30 MHz), which configures FlexSPI sequence 0 for 4 bit SPI at 103 MHz.

Some NXP documentation mentions flash functionality meant for the other 15 sequences, but only sequence 0 has any automatic triggering by hardware bus activity. The other 15 sequences are only used by software. Conceptually it's pretty simple. When the AHB bus wants to read from flash memory area at 0x60000000, and the request can't come from the buffer, sequence 0 is triggered to perform the read operation.

Because FlexSPI hardware doesn't have any way to "know" when the flash is busy with write or erase, if the CPU or any DMA causes a AHB bus request to read the flash, FlexSPI attempts to read even though the flash is busy and can't reply with valid data.

We could wish for the hardware to be more capable than it really is. It would have been awesome had NXP designed FlexSPI to "know" the flash is busy or ready, perhaps with something in the sequence config. Or another awesome capabily that sadly doesn't exist a way to communicate busy/ready status at the beginning of each flash read. Especially awesome would have been that busy status could be communicated to the AHB bus. But that simply is not the reality of the RT1062 and flash hardware. Even though the AHB bus protocol is capable of handling busy status or "wait states", to do so the "memory" the bus accesses needs to communicate the busy status to the bus. FlexSPI simply does not have any hardware support for flash busy vs ready. When an AHB bus request is made, the FlexSPI sequence to read is automatically used.

The long-winded point is you don't get any sort of flash busy handling from hardware. From the FlexSPI and AHB bus hardware, there is nothing like "auto-resume". If any AHB bus access requests data from the flash while it's busy, FlexSPI blindly does the read sequence, the flash chip ignore the request so FlexSPI receives garbage of whatever voltages happed to be on the data pins, and that garbage data is passed to the AHB bus request.

If the Cortex M7 processor tries to execute garbage instructions, while theoretically anything could happen, the very likely result will be a fault caught by the ARM core or by the MPU which enforces memory access policy. Teensy 4.x default fault handler logs CrashReport data, waits 8 seconds, and attempts a soft reboot.

Now to finally get to how the EEPROM emulation software handles this sitation. It takes a simple and very conservative approach. Interrupts are disabled just before sending the command to begin a write or erase operation. A loop just repeatedly reads the flash status register until it's no longer busy, and then interrupts are re-enabled.

This is a very cautious way. The common use case has all functions defaulting to ITCM RAM. Even FLASHMEM functions often still in the 32K instruction cache, so AHB access requesting from the flash is unlikely. But because we simply do not know if any interrupt will attempt run any functions which run from flash and won't be fulfilled from the Cortex M7 instruction cache, and the consequence for this unlikely case is usually a fault which will (hopefully) result in a warm reboot, the core library code takes the very cautious approach.

If you edit the core library, or just make a modified copy into your program, odds of trouble if you don't disable interrupts are low. If we ever do add an option for functions to default to flash rather than RAM (it's on my radar for future software), choosing that would of course make leaving interrupts enabled much more risky.

If you're building hardware for high-end audio or any other application where disabling interrupts during EEPROM emulation writes could cause trouble, perhaps the best choice would be to add a dedicated EEPROM chip. They're inexpensive, though it is another piece of hardware to connect by I2C or SPI. Or if you don't want to wait for a chip's busy time, even with interrupts enabled, maybe MRAM is a better choice. They're not so cheap, but if you're building a high-end audio system or any other project where timing concerns are so stringent, maybe MRAM would be worth the cost.

One final point I'd like to make, along the lines of wishing for hardware we don't have, is flash memory with RWW (Read While Write). Those chips implent flash in large banks, where each bank has the normal busy behavior, but you can read from the other banks while 1 of them is busy. Today this feature is still pretty rare. As far as I know, only a few pretty high-end (large capacity and/or expensive) flash chips have it. But I'm hopeful it'll eventually be possible to make a future Teensy model with RWW flash. It still won't be fast like expensive MRAM, but it RWW would let us keep interrupts enabled for the default EEPROM emulation behavior.
 
The long-winded point is you don't get any sort of flash busy handling from hardware. From the FlexSPI and AHB bus hardware, there is nothing like "auto-resume". If any AHB bus access requests data from the flash while it's busy, FlexSPI blindly does the read sequence, the flash chip ignore the request so FlexSPI receives garbage of whatever voltages happed to be on the data pins, and that garbage data is passed to the AHB bus request.
The "auto-resume" mentioned refers to the code modifications, specifically LUT14.
The way I workaround flash programming blocking interrupts is to do the following:
- set LUT12 to perform a programming suspend command
- set LUT13 to perform a read
- set LUT14 to perform a programming resume command
- change the default "read" sequence from LUT0:length 1 to LUT12:length 3.

This allows flash read access via FlexSPI to safely happen while waiting for an erase/programming operation to complete - FlexSPI automatically suspends/resumes the programming operation on either side of the read. However there's a catch - FlexSPI inconsistently skips sending the resume command (LUT14). For this reason the flash busy-wait loop manually sends the resume command if the status check indicates there is a suspended operation.
 
Last edited:
Oh that is a really interesting idea to use the flash chip erase/write suspend. But I don't see how it can work in practice with Winbond's timing requirements, unless there's some way I don't know for FlexSPI to integrate with timers. The "tSUS" spec on page 90 is 20us!

1792850423176.png
 
Oh that is a really interesting idea to use the flash chip erase/write suspend. But I don't see how it can work in practice with Winbond's timing requirements, unless there's some way I don't know for FlexSPI to integrate with timers. The "tSUS" spec on page 90 is 20us!
Yep, if you follow the modified code that's why I reprogram CSINTERVAL to 3000 ticks (@133 MHz = 22.5us) during the flash busy-wait. It does increase the total time required for reprogramming significantly but that doesn't matter so much when interrupts can stay active.

I posted about this at the time I implemented it: https://forum.pjrc.com/index.php?threads/onboard-flash-can-be-faster.77078/#post-364135
 
Last edited:
Back
Top