This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Is Keil USB sample code weird?

Hi,

I'm just implementing the Keil HID sample code, but I found some strange things even the compiler complains about:

In usbcore.c there are some variables used without being set before. The variables are "alt" in USB_SetConfiguration() and "msk", "old", "alt" and "ifn" in USB_SetInterface().

__inline BOOL USB_SetConfiguration(void)
{
  USB_COMMON_DESCRIPTOR *pD;
  DWORD alt, n, m;

         :

  case USB_INTERFACE_DESCRIPTOR_TYPE:
    alt = ((USB_INTERFACE_DESCRIPTOR *) pD)->bAlternateSetting;
  break;
  case USB_ENDPOINT_DESCRIPTOR_TYPE:
    if (alt == 0)
    {
         :
    }
         :
}


The code at USB_ENDPOINT_DESCRIPTOR_TYPE is executed when "alt" is 0 by chance only and setting "alt" in USB_INTERFACE_DESCRIPTOR_TYPE has no effect as "alt" is an auto-variable loosing it's contents after USB_SetConfiguration() is finished.

USB_SetInterface() shows similar things.

The source code does work in my environment, but I'm anxious that this is by chance only.

Could anybody explain me how to come to a more reliable solution?

BTW: Code parts like

  (BYTE *) pD += pD->bLength;


in a sample code scare me even after more than 20 years C programming (Realview complains as well).

Regards
Herbert

Parents
  • that is hardly surprising considering that they managed to cram that much of USB functionality into a little over 700 lines of code."

    That is an absolutely stupid thing to do in example code - unless the purpose of the example is specifically to demonstrate "optimum" coding for minimum size.

    The whole point of example code is to provide a clear, detailed, and easy-to-follow illustration of the subject at hand - the use of "clever tricks" is totally out of place!

    For the same reason, example code should be extremely well-commented.

    Unfortunately, a very large amount of so-called "example code" posted on the internet is extremely poor in these respects!

    :-)

    (quite a lot of it is also poor in that it doesn't actually work, either!)

Reply
  • that is hardly surprising considering that they managed to cram that much of USB functionality into a little over 700 lines of code."

    That is an absolutely stupid thing to do in example code - unless the purpose of the example is specifically to demonstrate "optimum" coding for minimum size.

    The whole point of example code is to provide a clear, detailed, and easy-to-follow illustration of the subject at hand - the use of "clever tricks" is totally out of place!

    For the same reason, example code should be extremely well-commented.

    Unfortunately, a very large amount of so-called "example code" posted on the internet is extremely poor in these respects!

    :-)

    (quite a lot of it is also poor in that it doesn't actually work, either!)

Children
No data