spacewrench wrote on Wednesday, December 10, 2008:
Delays on the order of microseconds almost certainly have to be done with either a hardware timer (and you just monitor the count value) or with a timed sequence of instructions (like NOPs). But for USB work (and I’m just getting to the “oh, I see” stage with that) you really have to use interrupts. As you’ve discovered, it’s very timing-sensitive, but if you’re on the device side, the timing is pretty much not up to you. The reason to use an RTOS is so you can do something else while you’re waiting for the host to demand that you do the next thing, within the next X microseconds. You can poll, but you’re wasting a lot of time.
For very initial development, I had fair results by calling the ISR in a tight loop from an ordinary task. However, this risks having things work (or not) because of random lucky (or unlucky) timing coincidences. For example, I was able to get enumeration to work this way, but only if I had certain debugging messages turned on. The problem was, with the messages, I was pausing long enough for a STATUS phase to complete, but without, I was doing the next thing too soon. The correct solution is to key off the STATUS-PHASE-COMPLETE interrupt.