V-USB on ATmega2560 -- bad descriptor

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
gruvin
Posts: 6
Joined: Sun Aug 19, 2012 6:22 am

V-USB on ATmega2560 -- bad descriptor

Post by gruvin » Sun Aug 19, 2012 6:28 am

Hello

I'm trying to get USBaspLoader.2010-07-27 working on an ATmega2560, in the bootloader area. There seems to be some problem with the device descriptor location in Flash (progmem), despite having ...

Code: Select all

#define USB_CFG_DRIVER_FLASH_PAGE       3

(My ATmega2560 bootloader is of course installed at 0x3E000.)

In my OS X system logs, I'm seeing ...

The USB Family found a device at port 2 of hub @ 0x26400000 with a bad USB device descriptor (0xffffffffffffffff, 0x0 )


... which does seem to suggest that empty flash data from the wrong address page is being used.

I see in usbdrv/usbportability.h the following ...

Code: Select all

#ifdef __HAS_ELPM__
#   define PROGMEM __farflash
#else
#   define PROGMEM __flash
#endif


... and ...

Code: Select all

#if USB_CFG_DRIVER_FLASH_PAGE
#   define USB_READ_FLASH(addr)    pgm_read_byte_far(((long)USB_CFG_DRIVER_FLASH_PAGE << 16) | (long)(addr))
#else
#   define USB_READ_FLASH(addr)    pgm_read_byte(addr)
#endif


... but I am not confident that these are being used (detected) as intended. (And I'm not sure how to test for that. I tried, but failed. :-/ )

I'm running on an iMac, under OS X 10.8, with CrossPack-AVR-20120217 (avr-gcc version 4.5.1.)

Thanks in advance for any help.

Gruvin.

gruvin
Posts: 6
Joined: Sun Aug 19, 2012 6:22 am

Re: V-USB on ATmega2560 -- bad descriptor

Post by gruvin » Sun Aug 19, 2012 6:34 am

UPDATE: I just updated the usbdrv/ folder from USBaspLoader.2010-07-27 to the latest vusb-20120109 version. No change.

gruvin
Posts: 6
Joined: Sun Aug 19, 2012 6:22 am

Re: V-USB on ATmega2560 -- bad descriptor

Post by gruvin » Sun Aug 19, 2012 7:04 am

UPDATE:

I have confirmed that the wrong USB_READ_FLASH macro is being defined, in usbdrv/usbportatbility.h ...

Code: Select all

#ifdef USB_CFG_DRIVER_FLASH_PAGE
#   define USB_READ_FLASH(addr)    pgm_read_byte_far(((long)USB_CFG_DRIVER_FLASH_PAGE << 16) | (long)(addr))
#else
#   define USB_READ_FLASH(addr)    pgm_read_byte(addr)
#endif


... that #ifdef should resolve to TRUE, but in fact only the second macro is defined, thus preventing the correct Flash page from being read.

Replacing that whole block with ...

Code: Select all

#define USB_READ_FLASH(addr)    pgm_read_byte_far(0x30000UL | (unsigned long)(addr))


... fixes the problem. I now has a USBasp USB device enumerated! Yay \o/

But I don't understand why the #ifdef failed. I definitely have the following in usbconfig.h ..

Code: Select all

#define USB_CFG_DRIVER_FLASH_PAGE       3


... and a grep of all files tells me there's no other instances of same, to do any overriding.

Anyone?

gruvin
Posts: 6
Joined: Sun Aug 19, 2012 6:22 am

Re: V-USB on ATmega2560 -- bad descriptor

Post by gruvin » Sun Aug 19, 2012 9:34 am

UPDATE

Code reformed to avoid the compiler warning about type casting ...

Code: Select all

#define USB_READ_FLASH(addr)    pgm_read_byte_far(0x30000UL + (unsigned)(addr))

gruvin
Posts: 6
Joined: Sun Aug 19, 2012 6:22 am

Re: V-USB on ATmega2560 -- bad descriptor

Post by gruvin » Sun Aug 19, 2012 11:41 am

UPDATE

Another wee bug for device with a lot of Flash. I had to change the following line in main.c ...

Code: Select all

*data = pgm_read_byte((void *)CURRENT_ADDRESS);


... to ...

Code: Select all

*data = pgm_read_byte_far(CURRENT_ADDRESS);


Hmmm, for compatibility with smaller ATmega's still, I probably should have made it ...

Code: Select all

#if (FLASHEND) > 0xffff /* we need long addressing */
*data = pgm_read_byte_far(CURRENT_ADDRESS);
#else
*data = pgm_read_byte((void *)(unsigned)CURRENT_ADDRESS);
#endif


(Would anyone more knowledgeable than I, regards PROGMEM etc like to comment? *shrug*)

In any case, without this change, USBasp Flash reads resulted in the same 64Kbyte address space being read over and over, instead of progressing to the next bank as they should. First indication of this fault was "Verify failed. First mismatch at 0x10000".

Seems I now have a fully functioning

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

Re: V-USB on ATmega2560 -- bad descriptor

Post by christian » Mon Aug 20, 2012 4:41 pm

gruvin wrote:UPDATE:
... that #ifdef should resolve to TRUE, but in fact only the second macro is defined, thus preventing the correct Flash page from being read.


Yes, that's a bug, the order of includes is wrong: usbportability.h is included before usbconfig.h. To fix it, simply delete the include of usbportability.h in usbdrv.c. This fix will be in the next release.

gruvin
Posts: 6
Joined: Sun Aug 19, 2012 6:22 am

Re: V-USB on ATmega2560 -- bad descriptor

Post by gruvin » Tue Aug 21, 2012 8:08 am

Ah, Cool. Thanks!

Yes, that fixed it. Yay \o/ :-D

Post Reply