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

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
Guest

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

Post by Guest » Sat Jan 16, 2010 8:56 pm

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?

z0ttel
Posts: 11
Joined: Thu Dec 27, 2007 3:21 pm

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

Post by z0ttel » Sat Jan 16, 2010 9:59 pm

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.

Grendel
Rank 4
Rank 4
Posts: 167
Joined: Sat Dec 16, 2006 9:53 pm
Location: Oregon, USA
Contact:

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

Post by Grendel » Sun Jan 17, 2010 9:01 am

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.

z0ttel
Posts: 11
Joined: Thu Dec 27, 2007 3:21 pm

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

Post by z0ttel » Sun Jan 17, 2010 12:31 pm

@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

z0ttel
Posts: 11
Joined: Thu Dec 27, 2007 3:21 pm

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

Post by z0ttel » Sun Jan 17, 2010 7:44 pm

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

Post Reply