Page 1 of 1

Question: usbpoll()

Posted: Tue Jul 22, 2008 11:56 pm
by Psychomax
Hello together.

First i must say my english is not that good. So please be nice! ;)

So here is my question. I really don't know how to use the function usbpoll(). Or the other way arround, i don't know how to use it with my problem. :)

First here is a example code. It just make the LED on and off.

Code: Select all

#define WAIT_TIME 100

....

int main()
{
    // LED-Port auf OUT
    DDR_LED  |= (1 << PAD_LED);

    // Timer1 initialisieren
    timer1_init();

    // Interrupts aktivieren
    sei();

    // Endlosschleife
    // Die LED ist jeweils 1 Sekunde an und 1 Sekunde aus,
    // blinkt also mit einer Frequenz von 0.5 Hz
    while (1)
    {
        PORT_LED |= (1 << PAD_LED);
        wait_10ms (WAIT_TIME);


        PORT_LED &= ~(1 << PAD_LED);
        wait_10ms (WAIT_TIME);
    }
}


So know i wanna change the "WAIT_TIME" via USB. I modifiy this code to that.

Code: Select all

#define WAIT_TIME 100

....

int main()
{
   
    ... USB INIT etc....

      while (1)
    {
        usbpoll();//CHANGE WAIT_TIME and GOES ON

        PORT_LED |= (1 << PAD_LED);
        wait_10ms (WAIT_TIME);


        PORT_LED &= ~(1 << PAD_LED);
        wait_10ms (WAIT_TIME);
    }
}
}


Will that work?! So the problem is for me does "usbpoll()" only poll, when a interrupt comes or polling everytime, so the it has a effect on the timer of the LED?!

Hope you know what i mean... :)

So shortly say. Is "usbpoll()" everytime aktiv or only when the host wants an action. Or the otherway arround will any code after "usbpoll()" be called?!

LG Max

Posted: Tue Jul 29, 2008 5:37 pm
by christian
usbPoll() returns within a few microseconds, unless a message was received. If a message was received, it calls one of your functions (e.g. usbFunctionSetup()) and returns then. usbPoll() NEVER blocks by itself.

The code example you gave won't work because you don't call usbPoll() often enough. You want something like this:

Code: Select all

    int cnt = 0;
    for(;;){
        usbPoll();
        if(timerInterruptOccurred){
            timerInterruptOccurred = 0;
            if(cnt++ >= 500){
                cnt = 0;
                PORT_LED ^= (1 << PAD_LED);
            }
        }
    }


You need a timer interrupt which sets a global volatile variable "timerInterruptOccurred" every millisecond.

In principle, you can also do it like this:

Code: Select all

    char cnt = 0;
    for(;;){
        usbPoll();
        wait_10ms(1);
        if(cnt++ >= 5)
            cnt = 0;
            PORT_LED ^= (1 << PAD_LED);
        }
    }


But the timing will be unreliable because wait_10ms() does not account for time spent in interrupt routines (such as the USB interrupt).

Posted: Thu Jul 31, 2008 7:47 am
by paddy
how often must usbPoll(); be called and how long does it take to complete it?

Posted: Thu Jul 31, 2008 12:21 pm
by christian
If no data must be processed (99% of the calls), it does two global variable loads, two comparisons and returns. You must call it at least every 50 ms, see usbdrv.h for more info about the API.

Re: Question: usbpoll()

Posted: Mon Feb 04, 2013 7:47 pm
by simtr
christian wrote:You need a timer interrupt which sets a global volatile variable "timerInterruptOccurred" every millisecond.


Can you tell me how?