Mega324P and AVR-USB

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

Mega324P and AVR-USB

Post by Guest » Fri Jul 20, 2007 11:18 am

Ok, I've been hitting my head against the wall for the last few days now. I'm trying to get AVR-USB to work on the 324P, with no luck at all. I on getting the USB Device not recognized message.

At first the driver wouldn't compile, as the timers are different from the Mega8 to the Mega324P. I've managed to get it to compile without errors, but it still doesn't work properly. Here's the code with the timers changed & commented - am I missing something? Have I got the timers wrong? Any help is muchly appreciated!

I did get it to work flawlessly on a Mega8L that I had lying around, but need it to work on the 324P (need the extra pins).


Code: Select all

  

static void beep(uchar duration)
{
    beepCnt = duration * 200;
    TIMSK2 |= 1 << OCIE2A;

//TIMSK is original, have choice between TIMSK 0 - 2
//OCIE2 is original, can be OCIE2A or B

}

UTIL_INTERRUPT(SIG_OUTPUT_COMPARE2) /* run with global interrupt enable */
{
    if(--beepCnt <= 0){
        TIMSK2 &= ~(1 << OCIE2A);

/TIMSK is original, have choice between TIMSK 0 - 2
//OCIE2 is original, can be OCIE2A or B

        PORTD |= 2;     /* make sure LED is off */
    }else{
        if(beepCnt & 1){
            PORTD |= 2;
        }else{
            PORTD &= ~2;
        }
    }
}

static void initHardware(void)
{
uchar   i, j;

    DDRC = 0;
    PORTC = 0x20;
    DDRD = 0x07;    /* 0000 0111 bin includes USB reset */
    PORTD = 0x3a;   /* 0011 1010 bin */
    setOutput(outputStatus);
    j = 0;
    while(--j){         /* USB Reset by device only required on Watchdog Reset */
        i = 0;
        while(--i);     /* delay >10ms for USB reset */
    }
    DDRD = 0x02;    /* 0000 0010 bin remove USB reset condition */
    /* ADC used for clock speed input: */
    ADMUX = 0x44;   /* ref=AVcc, select PC4 */
    ADCSRA = 0xc7;  /* enable ADC, start conversion, rate = 1/128 */


    /* Timer0 used to sample input (~50Hz) */
    TCCR0A = 5;  /* prescaler = 1024 */
//TCCR0 is original, TCCR0A or B

    /* Timer1 used for event clock */
    TCCR1A = 0; /* normal mode, no output actions */
    TCCR1B = 0x08 | 5;  /* CTC mode, clear on OCR1A, prescaler = 1024 */
    OCR1A = 0xffff;
    /* Timer2 used for acoustic signaling, we want 2kHz interrupt rate */
    TCCR2A = 0x08 | 3;   /* CTC mode, prescaler = 32 */
    OCR2A = 188;

//TCCR2 & OCR2 are original, A or B choice on both


}


  DBG1(0x00, (void *)&codeEepromSize, sizeof(codeEepromSize));
    for(;;){    /* main event loop */
        wdt_reset();
        usbPoll();
        if(TIFR1 & (1 << OCF1A)){
            TIFR1 |= 1 << OCF1A; /* clear pending flag */

//TIFR is original, can be TIFR0-2

            timerInterrupt();
        }
        pollAction();
        if(TIFR0 & (1 << TOV0)){ /* debounce by sampling with reduced rate */
            TIFR0 |= 1 << TOV0;

 //TIFR is original, can be TIFR0-2         

          pollInput();
            pollOutput();
            pollCommands();


THANKS!

Joe

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Post by christian » Fri Jul 20, 2007 6:50 pm

The USB driver itself does not use any timers. All it needs is a hardware interrupt (int0) and two I/O pins located on the same port.

Have you checked that the AVR really runs on 12 MHz? Did you initialize the I/O pins correctly and are you sure you send the USB reset/disconnect on the correct pins?

Since AVR-USB requires so little hardware resources, it's very likely to work on all AVRs with enough RAM and Flash.

If the 324P has a UART, you may want to enable debugging on the serial line. That could give you a hint what's going on.

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

Post by Grendel » Sat Jul 21, 2007 9:36 am

Does it enumerate if you comment out your timer handling and just have the USB stack running ? 2kHz is quite an ISR load and may interfer w/ the USB ISR.

Guest

Post by Guest » Sat Jul 21, 2007 10:47 am

Grendel wrote:Does it enumerate if you comment out your timer handling and just have the USB stack running ? 2kHz is quite an ISR load and may interfer w/ the USB ISR.


Ok, I've just checked again (deleted everything except the USB), and still get the same error. I've got the USB on ports D0 & D2 (D- & D+ respectively, as D2 is INT0 on the 324P, and the header is set correctly 0 & 2). The only thing I can think of is that it's not running at the right speed - I know the crystal works & the fuse bits are set to the same as what I set the mega 8 to. It's the same crystal that I used on the mega 8, and it worked fine there. I'm still at a complete loss as to where to go next, though I'm going to look into UART debugging & see if that would help.

iphi
Rank 2
Rank 2
Posts: 68
Joined: Mon Jun 25, 2007 11:37 am

Post by iphi » Sat Jul 21, 2007 2:18 pm

Anonymous wrote:I've got the USB on ports D0 & D2 (D- & D+ respectively, as D2 is INT0 on the 324P, and the header is set correctly 0 & 2).

So you have the USB D+ connected to one MCU pin only, using it as port and interrupt input at the same time.
I've seen that kind of setup as well on the PowerSwitch website. But I couldn't get it to work on my ATTINY2313. After I changed to D0=> D-, D1=>D+ and D2=INT0=>D+ things worked fine.

You might want to try this.

Guest

Post by Guest » Sat Jul 21, 2007 7:07 pm

iphi wrote: D0=> D-, D1=>D+ and D2=INT0=>D+

You might want to try this.


Ok, gave that a try, and still no go - thanks though!

Joe

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Post by christian » Mon Jul 23, 2007 1:44 pm

You can safely use bits D0 and D2, I do this in all my projects.

Please verify your basic configuration:
  1. Clock frequency. Write a simple test program which uses _delay_ms() from util/delay.h to generate a known frequency and check whether it's correct. The processor has a clock prescaler which might be active.
  2. Fuse values: Did you use the same hexadecimal fuse values as for the Mega8, or did you use the same options? Bit locations are often not consistent between AVR versions.
  3. Please check your I/O initialization. Instead of starting the driver, check that both USB lines are inputs and that D+ triggers an interrupt.

Guest

Post by Guest » Fri Jul 27, 2007 5:55 am

Hi Christian,

Ok, I've double verified the clock frequency with the _delay_ms() function - it is 12Mhz, and generates perfect timing. I "think" the values for the fuses are what they should be - I'm using AVRStudio, and picking what looks to be the equivalent setting on the Mega324p (no division of clock frequency, etc). I've also placed a few LED toggles at key points in the usbdrv.c file as a debug check (waiting for some parts in order to use USART) - they do change upon USB cable insertion, so it appears everything is working well in the sense of interrupts. Should hopefully have parts tomorrow, and can get more in depth look at what's happening.

Joe

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Post by christian » Mon Jul 30, 2007 4:22 pm

And you are sure you compile for 12 MHz and not with an assembler module for another frequency. In this case you should really try to get a debug output with -DDEBUG_LEVEL=2...

murdok

Post by murdok » Wed Aug 01, 2007 4:22 pm

Hi,

i have the same problem. Also when i enable the debug interface by DEBUG_LEVEL=2, all i get is FF: FF FF FF FF FF ... and so on. Whats wrong there? ... all i changed from original powerswitch SW is:
- PortD instead of PortB
- PinD.3 instead of B.0 for D-
- PinD.2 instead of B.1 for D+ (INT0)
- use ATMega168-20 instead of ATMega8-16 (had to change register names for ee-write/read functions)

thanks in advance,

Robert.

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Post by christian » Wed Aug 01, 2007 4:36 pm

The debug log

ff:
ff:
ff:
...

is printed while the USB is in RESET state. This means that the driver reads both, D+ and D- as zero. See usbdrv.c and search for DBG1(0xff, 0, 0); to see how reset is detected.

Guest

Post by Guest » Thu Aug 02, 2007 8:18 am

Ok, I managed to hookup USART, and when I plug in a USB cable, I get 00: 00 (debug set to 2)

Joe

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Post by christian » Thu Aug 02, 2007 2:23 pm

If you started with HIDKeys, you probably get the log from

Code: Select all

int   main(void)
{
uchar   key, lastKey = 0, keyDidChange = 0;
uchar   idleCounter = 0;

    wdt_enable(WDTO_2S);
    hardwareInit();
    odDebugInit();
    usbInit();
    sei();
    DBG1(0x00, 0, 0);


This would indicate that you get a controller reset.

david.l.harrison@rogers.c

USB on ATmega324P

Post by david.l.harrison@rogers.c » Sat Sep 22, 2007 10:25 pm

I just got AVR-USB working fine on my ATmega324P.

I am using PORTD, PD2(INT0) for DATA+ and PD3 for DATA-.
- no problems.

Just be careful with your usbconfig.h settings.

butrus.butrus
Posts: 10
Joined: Wed Sep 12, 2007 7:57 pm
Contact:

Post by butrus.butrus » Thu Sep 27, 2007 11:02 am

Hi! Is you MCU running at 5V or 3.3V? If 5V are you using proper zener diodes? Did you try with another computer?

Post Reply