How to integrate slow-write (SPI) FLASH into STM32 USB device library, to return "OK" CSW when FLASH write finishes?
Currently I have it working, but while the FLASH READ is about 200us and can be done wholly inside STORAGE_Read_FS (which is within an ISR), the FLASH WRITE takes ~17ms and while it works fine done wholly inside STORAGE_Write_FS, it needs the disabling of all USB interrupts when the FLASH is accessed elsewhere.
This works as far as the USB host (a PC) is concerned, and apparently USB FLASH sticks do more or less just this (return the "OK" CSW only when the FLASH write has finished), the long USB ISR time of ~17ms creates problems inside my system. For example a) it screws up USB VCP (CDC) operation during a tight series of USB disk writes and b) it disables all lower priority interrupts for the ~17ms FLASH write.
As I say, it works, but only if host-triggered file writes are sporadic. For example a ~30ms gap after each internally initiated FLASH sector write makes it hang together OK, without the host timing out. FLASH reading is OK because I have put in a lot of time to optimise it with DMA.
So I am looking at ways to do this more neatly, which means not returning the "OK" CSW initially (currently it is returned immediately STORAGE_Write_FS returns (if len=0 i.e. the last 64-byte packet has filled the 512 byte sector buffer and the buffer is ready to be flashed) in SCSI_ProcessWrite) and instead subcontracting the block write to an RTOS task and returning the "OK" CSW from that task when the FLASH write has finished.
I am sure lots of people have gone up this path...
The current ST code is OK only if writing to a RAM disk or some such.
Some background to this can be found here:
https://www.eevblog.com/forum/microcontrollers/cleanest-way-to-block-usb-interrupts/
https://www.eevblog.com/forum/microcontrollers/32f417-usb-fs-(not-hs)-and-dma/
I am happy to pay somebody to do this : - )
