USB Device Not Recognised with AVR Studio and D1/2 for D-/+

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

USB Device Not Recognised with AVR Studio and D1/2 for D-/+

Post by mojo » Sat Mar 29, 2008 11:50 pm

I am trying to get AVR-USB to work with a project based on the HIDKeys code. So far the only thing I have changed is the hardware and a little bit of the code.

On my hardware, D- is PD1 and D+ is PD2, so D+ is directly connected to INT0. I am using 3.6V zener diodes with a 2k2 pull-up (works fine with joystick controller based on Igor Cesko's code, see http://joystick.world3.net).

I made the following changes in the code:

usbconfig.h: (to reflect different USB port connections)

Code: Select all

#define USB_CFG_IOPORTNAME      D
/* This is the port where the USB bus is connected. When you configure it to
 * "B", the registers PORTB, PINB and DDRB will be used.
 */
#define USB_CFG_DMINUS_BIT      1
/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
 * This may be any bit in the port.
 */
#define USB_CFG_DPLUS_BIT       2


main.c (renamed to Replay_Gain_HW.c, shouldn't make any difference, and altered to init port D for USB)

Code: Select all

static void hardwareInit(void)
{
   uint8_t   i, j;

    PORTB = 0xff;   /* activate all pull-ups */
    DDRB = 0;       /* all pins input */
    PORTC = 0xff;   /* activate all pull-ups */
    DDRC = 0;       /* all pins input */
    PORTD = 0b00111011;   /* activate pull-up on PD4/int1 */
    DDRD  = 0b11110101;   /* all pins output except PD4/int1 */
   j = 0;
   while(--j)           /* USB Reset by device only required on Watchdog Reset */
   {
      i = 0;
      while(--i);    /* delay >10ms for USB reset */
   }
    PORTD = 0b00111111;   /* activate pull-up on PD4/int1 */
   DDRD  = 0b11110001;   /* remove USB reset condition and make int0 input */
   //PORTD = 0b00111010;   /* activate pull-up on PD4/int1 */
                   /* configure timer 0 for a rate of 12M/(1024 * 256) = 45.78 Hz (~22ms) */
    TCCR0 = 5;         /* timer 0 prescaler: 1024 */
}


I am using AVR Studio with avr-gcc 4.2.2 (WinAVR 20071221). I have tried with the supplied makefile and one of my own creation.

I can verify that the AVR gets as far the the infinite loop in main() and through at least one cycle of usbPoll(). After what seems like a few cycles the loop stops executing (my in-loop PWM LED turns off, and it doesn't work with the LED code removed either, just in case that was an issue).

I always get a "USB Device Not Recognised" error. Any ideas?

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

Post by christian » Mon Mar 31, 2008 3:16 pm

You're activating pull-ups on D+ and D-. These bits of PORTD must be initialized to 0.

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Post by mojo » Mon Mar 31, 2008 5:56 pm

Okay, well I was just copying the settings from the HIDKeys project, but anyway I fixed it now.

I removed the oddebug.h include and calls too, just in case they were messing things up.

Still getting the same message though.

Could compiler optimisations have anything to do with it? Currently I have them disabled, but I did try with -O2.

Is it okay to have the D+ pin directly connected to the INT0 pin? And the limitation of having D- on bit 0 of a port has been removed?

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

Post by christian » Tue Apr 01, 2008 3:09 pm

Your code suggests that you have wired D+ to INT1 instead of INT0 since you have only configured INT1 as an input. If you use INT1, you must also change the interrupt configuration in usbconfig.h accordingly. Otherwise make INT0 an input without pull-up.

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Post by mojo » Tue Apr 01, 2008 6:06 pm

Here is my current code:

Code: Select all

    PORTD = 0b00111001;
    DDRD  = 0b11110101;
   j = 0;
   while(--j)
   {
      i = 0;
      while(--i);
   }
    PORTD = 0b00111001;   
   DDRD  = 0b11110001;


As you can see, I have both PD1 and PD2/INT0 as inputs. D- is on PD1 and D+ on PD2/INT0.

Are the any examples of working code on an ATmega8 with similar configuration to this? I thought it would simply be a question of changing a couple of values and hitting "compile", since the HIDKeys example is mega8 based, but it does not work.

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

Post by christian » Tue Apr 01, 2008 9:22 pm

See the USBaspLoader project. It uses Port D bits 2 and 4 in the default configuration.

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Post by mojo » Tue Apr 01, 2008 10:02 pm

Thanks, but I think I need a project based on AVR Studio and WinAVR. I am starting to think the problem could be a problem with the makefile for AVR Studio or something.

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Post by mojo » Tue Apr 01, 2008 11:53 pm

Okay, I tried compiling the 3Dvert project with simple changes to reflect my new pin configuration, and it didn't work. The only thing I can think to do now is make a PowerSwitch on a bit of breadboard and try it with the original firmware and one I compile myself.

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Post by mojo » Thu Apr 03, 2008 4:44 pm

I finally cracked it.

The problem seems to stem from the makefile. The makefile supplied with the HIDKeys project does not work with AVR Studio, because AVR Studio expects the output to be named project_name.hex, and also requires ELF binaries to be built for debugging.

As such, I started by making my own makefile. I use the MFile program from WinAVR. This makefile compiled okay, but the resulting code did not work. I thought maybe it was a compiler issue - I have had problems in the past with compiler optimisations and GCC, for example optimsing _delay_ms() to oblivion. I tried various different options but could never get it to work.

It's strange because the 3DVpert project uses what appears to be an MFile based makefile.

Anyway, I went back to the HIDKeys makefile and fixed it to work with AVR Studio. Unfortunately the resulting ELF binary does not contain debug data, but it satisfies AVR Studio and produced a .hex file that is identical to the example one supplied. My guess is that maybe the MFile generated makefile was not assembling the .hex file correctly.

I'll put some info on my blog about it. Why must C compilers be so awkward? One of the reasons I prefer assembler.

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

Post by Grendel » Thu Apr 03, 2008 11:16 pm

You don't need a makefile if you let AVR Studio use its built-in support for WinAVR. The current version of the 3DP-Vert project archive contains AVRS *.aps files that allow you build the hex images from w/in AVRS -- pretty straight forward.

Edit: the makefiles included are intended to be used only via command line, not ARVS.

Post Reply