Page 1 of 1

V-USB HID joystick doesn't work ... sometimes

Posted: Sat Jan 16, 2010 8:56 pm
by Guest
Hi,

I'm tyring to create a HID joystick device based on V-USB and I'm nearly going mad. I took the HID-mouse example as basis for my project: I modified the descriptor, threw out the advanceCircleByFixedAngle() function, added the ADC routines and got it finally running. The device got recognized by windows 7 x64 and the captured analog data (1 axis, 10 bit) was shown in the gamepad controller panel.

Then I wanted to extend the firmware so that now 5 analog inputs are sampled by the ADC and sent to the host. First, I just added the ADC routines and didn't modify the USB descriptor (in usbSetInterrupt() the same data is sent as before) - the ADC routines are working fine (I checked this with AVR dragon and AVRStudio), but now the device doesn't get recognized by windows any more! :shock: The data sent via USB hasn't been modified and the structure to send the data hasn't also been modified - I just added some code to the ADC routine.

My assumption is that because of the ADC interrupt service routine gets triggered more often now this breaks the usb communication part somehow. I tried to get some information about time-criticial parts of V-USB, but either I'm blind or their not documented.

The problem occurs on an ATmega32 running at 12MHz. The endless loop in main.c looks like this:

Code: Select all

for(;;){                /* main event loop */
        wdt_reset();

        AdcGetCurrentStatus (&AdcCurrentState_en);

        /* Get data from Adc if AD conversion is finished */
        if (ADC_FINISHED == AdcCurrentState_en) {

            /* Read single channel data from ADC */
            AdcGetCurrentData(&AnalogAxisData_u16);
        }

        usbPoll();

        if(usbInterruptIsReady()){
            usbSetInterrupt((void *)&AnalogAxisData_u16, sizeof(AnalogAxisData_u16));
        }
    }


Any ideas, anyone?

Re: V-USB HID joystick doesn't work ... sometimes

Posted: Sat Jan 16, 2010 9:59 pm
by z0ttel
I forgot something to mention: sometimes (1 out of 10 tries or so), the device gets recognized by windows and is shown in the game controller panel. But then it seems, that the communication broke down, because no matter what I do, the shown values of the analog axis don't change any more. If I change the analog input value on the device (by twisting a poti), then that value seems to be displayed after disconnecting and connecting the device to the usb bus.

Re: V-USB HID joystick doesn't work ... sometimes

Posted: Sun Jan 17, 2010 9:01 am
by Grendel
Check usbdrv.h:

Code: Select all

Interrupt latency:
The application must ensure that the USB interrupt is not disabled for more
than 25 cycles (this is for 12 MHz, faster clocks allow longer latency).
This implies that all interrupt routines must either be declared as "INTERRUPT"
instead of "SIGNAL" (see "avr/signal.h") or that they are written in assembler
with "sei" as the first instruction.

Re: V-USB HID joystick doesn't work ... sometimes

Posted: Sun Jan 17, 2010 12:31 pm
by z0ttel
@Grendel: thanks for the hint - I must have overlooked this ... :|

That would mean that I must ensure that there are no ISR blocking situations which last longer than ~2µs (25/12MHz) - is that correct? In the code there's a section where I copy several 100 bytes of data with interrupts disabled - so I will have to check the runtime for that, too.

Damn, time to get an oscilloscope ... :D

Re: V-USB HID joystick doesn't work ... sometimes

Posted: Sun Jan 17, 2010 7:44 pm
by z0ttel
Update: Grendel, your hint was right. After inserting a sei() at the beginning of the ADC ISR the device ran as expected. Thank you very much - you saved my day :D