This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Header file app_usbd_cdc_acm.h

Hi everyone,

In the file header "app_usbd_cdc_acm.h" I notice this function:

/**
* @brief Helper function to get class instance from CDC ACM class.
*
* @param[in] p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF).
*
* @return Base class instance.
*/
static inline app_usbd_class_inst_t const *
app_usbd_cdc_acm_class_inst_get(app_usbd_cdc_acm_t const * p_cdc_acm)
{
return &p_cdc_acm->base;
}

What does it means " Base class instance"? What's a base class instance? I don't understand what the function is defined for.

Can anyone help me?

BR

  • Hi Alex

    Similar to other SDK libraries the USB libraries are based on an object oriented programming principle, but since C is not really an object oriented language instance structs are used instead of classes/objects. 

    The instance struct will contain information related to that particular object. In the case of the USBD classes this is mainly the different data buffers and function pointers used by that particular USB class. 

    This allows you to more easily create USB applications that combine multiple USB classes, and as an example it is possible to instantiate two CDC classes if you need two virtual comports, or combine different USB classes like CDC and MSC in the same example. 

    More concretely you typically need to pass a pointer to the class instance every time you interact with the USBD library, to tell the library which particular instance you are performing an action on. 

    Whenever you get a callback from the USB library you will also be provided a pointer to the class instance, so you know which one that particular event was related to. 

    Best regards
    Torbjørn

  • So if I have not misunderstood the app_usbd_cdc_acm_class_inst_get function is used to pass from a structure containing all the information on the CDC ACM class to an instance pointer which instead contains the pointers to the functions used and information relating to the addresses of the endpoints whose class has access. Is that so?

    Doing this is possible to use for example the app_usbd_class_append regardless of class. Is it right?

    Thanks 

    BR

    Alex

  • Hi Alex

    Exactly. It allows the USB driver to treat all the instances the same, even though they might have widely different implementations based on which USB class they are implementing. 

    This is similar to how object oriented languages might have a base class or interface that defines the API of an object, and then you can have various implementations with different functionality that can all be accessed the same way since they share a common API. 

    Best regards
    Torbjørn

Related