USB and Flash Writing

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
avrhack
Posts: 140
Joined: Tue Dec 24, 2013 1:42 pm
Has thanked: 17 times
Been thanked: 3 times

USB and Flash Writing

Post by avrhack »

I'm working on an app that needs to write to flash in response to USB commands and I'm struggling! If I single step the app on the host things are fine, but if I run it at full tilt I get an error after the second transaction.

I'm using a bulk endpoint (one in, one out), send a command to erase a block of flash using an out and then sit in a loop in the application reading in until I get something. On the STM32 obviously when the flash erase kicks off, as it's in normal flash (trying NOT to use CCM!) all access will be blocked so no interrupts will run in the USB HAL driver. My understanding was that the hardware was just NAK the IN transactions until the flash is erased and the interrupt servicing happens at which point the IN transaction would get the Data token.

Sticking a logic analyser on the USB bus that is indeed what is happening, yet after the second such transaction the host goes into a loop getting USB_TIMEOUT from libusb.

I suspect this may not be an STM32 / firmware issue but wanted to know what others do for this circumstance i.e. where interrupts and code execution is suspended (in this case for about 24mS)?

Thanks.
mobyfab
Posts: 484
Joined: Sat Nov 19, 2011 6:47 pm
Has thanked: 21 times
Been thanked: 31 times

Re: USB and Flash Writing

Post by mobyfab »

Maybe you should try to increase the libusb timeout, or put one in your code?

I have no issues erasing/writing to flash from USB, but I don't send any data while the flash is busy.
You should use some return value to see if the write was sucessful before you send more data.

Also, moving the flash and USB functions/interrupts to CCM will help but is not mandatory.
avrhack
Posts: 140
Joined: Tue Dec 24, 2013 1:42 pm
Has thanked: 17 times
Been thanked: 3 times

Re: USB and Flash Writing

Post by avrhack »

mobyfab wrote:Maybe you should try to increase the libusb timeout, or put one in your code?

Have already done that thanks - the issue isn't with timeout it's that after 2 transactions the host permanently shows timeout no matter how long the timeout is in the USB call.

mobyfab wrote:I have no issues erasing/writing to flash from USB, but I don't send any data while the flash is busy.
You should use some return value to see if the write was sucessful before you send more data.

That's exactly what I'm doing - send one transaction on USB to start the erase, then wait for a reply to say it's done. Obviously in USB terms that means the host sends an interrupt IN poll every mS in this case as the endpoint is set to 1mS timing.

mobyfab wrote:Also, moving the flash and USB functions/interrupts to CCM will help but is not mandatory.

Yes I definitely want to avoid that as it seems I'd have to hack ChibiOS source code to reallocate e.g. LLD code into CCM RAM too which I'm not going to do - I want to keep ChibiOS exactly as it comes.
mobyfab
Posts: 484
Joined: Sat Nov 19, 2011 6:47 pm
Has thanked: 21 times
Been thanked: 31 times

Re: USB and Flash Writing

Post by mobyfab »

You should try to debug your program to see where it hangs.

I suspect your flash code might have a bug. Maybe you can try with stub flash functions to verify.

I don't use any interrupt endpoints, just bulk with a read timeout on the host side.

You can move the code to CCM with just a linker file, which can be located outside of chibios.
avrhack
Posts: 140
Joined: Tue Dec 24, 2013 1:42 pm
Has thanked: 17 times
Been thanked: 3 times

Re: USB and Flash Writing

Post by avrhack »

mobyfab wrote:You should try to debug your program to see where it hangs. I suspect your flash code might have a bug. Maybe you can try with stub flash functions to verify


Yep done that. Flash code works perfectly and I get the same problem when I replace it with a wait loop with interrupts disabled which is effectively what happens when flash is being written - the bus locks up read access so the processor stalls.

mobyfab wrote:I don't use any interrupt endpoints, just bulk with a read timeout on the host side.


Interesting; I wonder whether it's my use of interrupt rather than bulk. What do you use on the host side? Mine is libusb-1.0 on OSX....

mobyfab wrote:You can move the code to CCM with just a linker file, which can be located outside of chibios.


Presumably though that will move ALL code rather than just the interrupt routines and I don't have enough space in CCM RAM for that......
mobyfab
Posts: 484
Joined: Sat Nov 19, 2011 6:47 pm
Has thanked: 21 times
Been thanked: 31 times

Re: USB and Flash Writing

Post by mobyfab »

Putting some wait loop is not exactly the same as it still allows interrupts to run.
That suggest the problem comes from the USB code. Maybe you get a stack overflow in your thread, try to increase the working area. traceback?

I use libusb-1.0 and WinUsb. This is the library I wrote: https://github.com/fpoussin/QtUsb

You can move only the specific code you want to CCM. (per signature or object file)
In my case it works both with and without CCM so that's not the issue.

By the way, it's on an F303 device.
Post Reply