Running the Sample Application

The first step to integrate the demo application into your application code is to call App_USBD_Init(). This function is responsible for the following steps:

  1. Initializing the USB device stack.
  2. Creating and adding a device instance.
  3. Creating and adding configurations.
  4. Calling USB class-specific application code.
  5. Starting the USB device stack.

The App_USBD_Init() function is described in Listing - App_USBD_Init() Function.

CPU_BOOLEAN  App_USBD_Init (void)
{
    CPU_INT08U   dev_nbr;
    CPU_INT08U   cfg_hs_nbr;
    CPU_INT08U   cfg_fs_nbr;
    CPU_BOOLEAN  ok;
    USBD_ERR     err;


    USBD_Init(&err);                                                          (1)
    if (err != USBD_ERR_NONE) {
        /* $$$$ Handle error. */
        return (DEF_FAIL);
    }
 
    dev_nbr = USBD_DevAdd(&USBD_DevCfg_<controller>,                          (2)
                          &App_USBD_BusFncts,
                          &USBD_DrvAPI_<controller>,
                          &USBD_DrvCfg_<controller>,
                          &USBD_DrvBSP_<board name>,
                          &err);
    if (err != USBD_ERR_NONE) {
        /* $$$$ Handle error. */
        return (DEF_FAIL);
    }
    cfg_hs_nbr = USBD_CFG_NBR_NONE;
    cfg_fs_nbr = USBD_CFG_NBR_NONE;
 
    if (USBD_DrvCfg_<controller>.Spd == USBD_DEV_SPD_HIGH) {

        cfg_hs_nbr = USBD_CfgAdd(dev_nbr,                                     (3)
                                 USBD_DEV_ATTRIB_SELF_POWERED,
                                 100u,
                                 USBD_DEV_SPD_HIGH,
                                "HS configuration",
                                &err);
        if (err != USBD_ERR_NONE) {
            /* $$$$ Handle error. */
            return (DEF_FAIL);
        }
    }
 
    cfg_fs_nbr = USBD_CfgAdd(dev_nbr,                                         (4)
                             USBD_DEV_ATTRIB_SELF_POWERED,
                             100u,
                             USBD_DEV_SPD_FULL,
                            "FS configuration",
                            &err);
    if (err != USBD_ERR_NONE) {
        /* $$$$ Handle error. */
        return (DEF_FAIL);
    }
 
    if ((cfg_fs_nbr != USBD_CFG_NBR_NONE) &&
        (cfg_hs_nbr != USBD_CFG_NBR_NONE)) {
        USBD_CfgOtherSpeed(dev_nbr,                                           (5)
                           cfg_hs_nbr,
                           cfg_fs_nbr,
                          &err);
        if (err != USBD_ERR_NONE) {
            /* $$$$ Handle error. */
            return (DEF_FAIL);
        }
    }
 
#if (APP_CFG_USBD_XXXX_EN == DEF_ENABLED)                                     (6)
    ok = App_USBD_XXXX_Init(dev_nbr,            
                            cfg_hs_nbr,
                            cfg_fs_nbr);
    if (ok != DEF_OK) {
       /* $$$$ Handle error. */
       return (DEF_FAIL);
    }
#endif
if (APP_CFG_USBD_XXXX_EN == DEF_ENABLED)                                      (6)
    .
    .
    .
#endif
    
    USBD_DevStart(dev_nbr, &err);                                             (7)
 
    (void)ok;
    return (DEF_OK);
}

(1) USBD_Init() initializes the USB device stack. This must be the first USB function called by your application’s initialization code. If µC/USB-Device is used with µC/OS-II or -III, OSInit() must be called prior to USBD_Init() in order to initialize the kernel services.

(2) USBD_DevAdd() creates and adds a USB device instance. A given USB device instance is associated with a single USB device controller. µC/USB-Device can support multiple USB device controllers concurrently. If your target supports multiple controllers, you can create multiple USB device instances for them. The function USBD_DevAdd() returns a device instance number; this number is used as a parameter for all subsequent operations.

(3) Create and add a high-speed configuration to your device. USBD_CfgAdd() creates and adds a configuration to the USB device stack. At a minimum, your USB device application only needs one full-speed and one high-speed configuration if your device is a high-speed capable device. For a full-speed device, only a full-speed configuration will be required. You can create as many configurations as needed by your application, and you can associate multiple instances of USB classes to these configurations. For example, you can create a configuration to contain a mass storage device, and another configuration for a human interface device such as a keyboard, and a vendor specific device.

(4) Create and add a full-speed configuration to your device.

(5) Associate the high-speed configuration to it’s full-speed counterpart. This inform the stack that both configurations offer comparable functionality regardless of speed. This is useful to generate the “Other Speed Configuration” descriptor.

(6) Initialize the class-specific application demos by calling the function App_USBD_XXXX_Init() where XXXX can be CDC, HID, MSC, PHDC or VENDOR. Class-specific demos are enabled and disabled using the APP_CFG_USB_XXXX_EN #define.

(7) After all the class instances are created and added to the device configurations, the application should call USBD_DevStart(). This function connects the device with the host by enabling the pull-up resistor on the D+ line.


Table - USB Class Demos Init Functions lists the sections you should refer to for more details about each App_USBD_XXXX_Init() function.

Table - USB Class Demos Init Functions
ClassFunctionRefer to...
AudioApp_USBD_Audio_Init()Audio Class Configuration
CDC ACMApp_USBD_CDC_Init()CDC Configuration
HIDApp_USBD_HID_Init()HID Class Configuration
MSCApp_USBD_MSC_Init()MSC Configuration
PHDCApp_USBD_PHDC_Init()PHDC Configuration
VendorApp_USBD_Vendor_Init()Vendor Class Configuration


After building and downloading the application into your target, you should be able to successfully connect your target to a host PC through USB. Once the USB sample application is running, the host detects the connection of a new device and starts the enumeration process. If you are using a Windows PC, it will load a driver which will manage your device. If no driver is found for your device, Windows will display the “found new hardware” wizard so that you can specify which driver to load. Once the driver is loaded, your device is ready for communication. Table - USB Class Demos References lists the different section(s) you should refer to for more details on each USB class demo.


Related pages