keskiviikko 29. huhtikuuta 2015

STM32F4 & USB HID - headbashing time

I briefly mentioned working with USB with little-to-none success on STM32F4 discovery board when I last wrote about this board. I wanted to use USB HS pins/core in device mode only (MicroC connector on Discovery board) but aside the hellishly ugly library there seemed to be nothing available.

As I don't have enough knowledge of USB on protocol level I finally relented and adapted the relevant parts of provided libraries (totalling 25 different files and ~250k of source) as a part of my own library. Except that it didn't work - I only got session request, reset, few SOF frames and eventual failure - Windows just couldn't identify my board at all.

I've spent more hours on figuring this out than I'd like to admit, and finally I found the problem. To minimize power usage I have most of the heavy lifting initiated by interrupts (external I/O, timers etc), then actual work is done in main and then processor is put in idle mode ("wfi" instruction - not even proper sleep mode, just "no-op until interrupt is received"). During typical use this cut the processor power usage to about third of normal, which is great indeed. Now side effect of this is that it apparently interferes with USB core somehow, preventing it from working properly. Removing this single instruction made the USB work immediately. D'oh! ...And I've been trying to figure out what is wrong with USB code. (insert bash-head-to-table moment here)

Since I'm using USB mainly to configure the board I then just changed system so that sleep instruction is only allowed when USB is not connected. Momentary added power consumption (of whopping few tens of milliamps!) is no problem there. However I somewhat suspect that USB is not the only place that will cause problems with power saving but we'll see...

Sources will be following after some cleanup.

Edit, the next day: Less than hour of writing above, after some trivial modifications that should affect nothing (mostly debug messages to figure out HID report contents) it stopped working again. I spent some time trying to figure out what I did, but day was already nearing end so I moved on, suspecting I knew the reason. (and as unrelated thing, the entire night my head was filled with orbital trajectories. Three words: Kerbal Space Program)

This morning I come in, turn computer on and plug discovery board in and it works. Again.

This isn't the first time this stupid pre-build Acer desktop fails with USB (first and last time I buy such, from now on it's selected components only and possibly assembly in shop). It has had habit of occasionally refusing to work with my (older, with different processor) boards until it's rebooted, and looks like that behavior continues with discovery boards too.

I have said this before it is worth repeating: Never, ever use USB for anything that needs to be reliable. USB certainly isn't. Even with better quality computers.


Ei kommentteja:

Lähetä kommentti