Page 1 of 1

usbPoll() in Timer0 interrupt routine.

Posted: Thu Feb 24, 2011 10:54 pm
by maciek_nh
Hello,

I wrote following program:

Code: Select all

#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>  /* for sei() */
#include <util/delay.h>     /* for _delay_ms() */
#include <avr/pgmspace.h>   /* required by usbdrv.h */
#include "usbdrv/usbdrv.h"
#include "usbdrv/requests.h"       /* The custom request numbers we use */
#include "usb_interface.h"
#include "lcd.h"

int main(void)
{
   DDRB = 0xFF;
   DDRD = 0xF0;         //D+ and D- both as input;
   
   // usb init      
    usbInit();
    usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
    usbDeviceConnect();
   // interrupt enable
    sei();     

   //Timer0 init
   TCCR0 = (1<<CS00);
   TIMSK =  (1<<TOIE0);
   TCNT0 = 223;   

   // init other devices
   lcd_init();   

   
    for(;;){}
    return 0;
}

/* ------------------------------------------------------------------------- */

ISR(TIMER0_OVF_vect)
{   
   usbPoll();
   TCNT0 = 230;
}


When I connect my device to my computer, the device is not recognized. There is something wrong with Timer0 interrupt routine. I do not even know if it is possible to place usbPoll() in interrupt routine. If I put usbPoll() to 'for' loop everything works fine. Could someone tell me if it is possible to call usbPoll() in any interrupt routine. If yes, please write how.

Re: usbPoll() in Timer0 interrupt routine.

Posted: Fri Feb 25, 2011 3:57 am
by maxi
You should be calling usbPoll() from the main loop. It is a bad idea in any case to call a subroutine from an ISR
6.- Interrupts and Timing - Make sure that you're not using interrupts higher than the interrupt you have set for VUSB. Also make sure you're not disabling interrupts for longer than a few milliseconds, or that you have more than 10ms between calls to usbPoll(). A common mistake is to have sections of floating-point calculations in a main loop which does call usbPoll(), however, keep in mind that floating-point math is EXTREMELY slow, it may be worthwhile to place usbPoll() before and after these calculations.

Re: usbPoll() in Timer0 interrupt routine.

Posted: Sat Feb 26, 2011 9:41 am
by Augend
If you put big codes in main loop your device will not be seen in next system enumeration. calling usbpoll() in short intervals is mandatory for device recognition. if you are using a watchdog dont forget to reset it during your calculations.