Page 1 of 1

Bug in USB_PROP_IS_RAM found.

Posted: Mon Sep 07, 2009 3:05 pm
by pe0fko
I was trying to make a dynamic usb serial number in the firmware and want to use the USB_PROP_IS_RAM (not the USB_PROP_IS_DYNAMIC that seems to work).
The USB_PROP_IS_RAM did not work!
I did define in main.c:

Code: Select all

int   usbDescriptorStringSerialNumber[] = {
    USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN),
    USB_CFG_SERIAL_NUMBER
};

And in the usbconfig.h file:

Code: Select all

#define   USB_CFG_SERIAL_NUMBER      'P','E','0','F','K','O','-','2','.','0'
#define   USB_CFG_SERIAL_NUMBER_LEN   10
#define   USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER   (USB_PROP_IS_RAM | USB_PROP_LENGTH(USB_CFG_SERIAL_NUMBER_LEN))

This source change alone was not sufficient to make the firmware running!

I did found it had something to do with the length and changed the macro GET_DESCRIPTOR(..) in the source file usbdrv.c according the next code.

Code: Select all

//+PE0FKO
#if 1

#define GET_DESCRIPTOR(cfgProp, staticName)         \
    if(cfgProp){                                    \
        if((cfgProp) & USB_PROP_IS_DYNAMIC){        \
            len = usbFunctionDescriptor(rq);        \
        }else{                                      \
            len = USB_PROP_LENGTH(cfgProp);         \
            usbMsgPtr = (uchar *)(staticName);      \
        }                                           \
        if((cfgProp) & USB_PROP_IS_RAM)             \
        {    flags = 0; len = len*2+2;   }          \
    }

#else

#define GET_DESCRIPTOR(cfgProp, staticName)         \
    if(cfgProp){                                    \
        if((cfgProp) & USB_PROP_IS_RAM)             \
            flags = 0;                              \
        if((cfgProp) & USB_PROP_IS_DYNAMIC){        \
            len = usbFunctionDescriptor(rq);        \
        }else{                                      \
            len = USB_PROP_LENGTH(cfgProp);         \
            usbMsgPtr = (uchar *)(staticName);      \
        }                                           \
    }

#endif
//-PE0FKO


The length was not correct because the code #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber) in the same usbdrv.c source file was not compiled in the next code part.

Code: Select all

#if USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER == 0 && USB_CFG_SERIAL_NUMBER_LEN
#undef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER    sizeof(usbDescriptorStringSerialNumber)
PROGMEM int usbDescriptorStringSerialNumber[] = {
    USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN),
    USB_CFG_SERIAL_NUMBER
};
#endif


By changing the macro GET_DESCRIPTOR(..) I did get the code running :D

Fred Krom
PE0FKO

Re: Bug in USB_PROP_IS_RAM found.

Posted: Tue Sep 22, 2009 10:46 am
by christian
The macro USB_PROP_LENGTH() is expected to return the descriptor length, not the string length.

Your fix with len = 2 * (len + 1) works for string descriptors, but not for HID, report, device, endpoint and other descriptors. You should rather define

Code: Select all

#define   USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER   (USB_PROP_IS_RAM |(2 * USB_CFG_SERIAL_NUMBER_LEN + 2))

Re: Bug in USB_PROP_IS_RAM found.

Posted: Sat Oct 24, 2009 2:06 pm
by pe0fko
Hi Cristian,

Thanks for your reply. I misunderstood the documentation I expect, did not understood that a string descriptor was different as a other descriptor.
Did make the change as you suggested and it is working now.

Thanks for the useful library and sharing the code!
Fred