Jump to main content

##

# 2.2.4.1 USB_DEVICE_Initialize Function

## C

    SYS_MODULE_OBJ USB_DEVICE_Initialize(
        const SYS_MODULE_INDEX instanceIndex,
        const SYS_MODULE_INIT * const init
    );

## Summary

This function initializes an instance of USB device layer, making it ready for clients to open and use it. The number of instances is limited by the value of macro USB_DEVICE_MAX_INSTANCES defined in system_config.h file.

## Precondition

None.

## Parameters

Parameters | Description
instanceIndex | In case of microcontrollers with multiple USB peripherals, user can create multiple instances of USB device layer. Parameter instanceIndex identifies this instance.
init | Pointer to a data structure containing any data necessary to initialize the USB device layer.

## Returns

If successful, returns a valid handle to a device layer object. Otherwise, it returns SYS_MODULE_OBJ_INVALID.

## Example

    // This code example shows the initialization of the
    // the USB Device Layer. Note how an endpoint table is
    // created and assigned.

    USB_DEVICE_INIT deviceLayerInit;
    SYS_MODULE_OBJ usbDeviceObj;
    uint8_t __attribute__((aligned(512))) endpointTable[USB_DEVICE_ENDPOINT_TABLE_SIZE];

    // System module initialization
    deviceLayerInit.moduleInit.value = SYS_MODULE_POWER_RUN_FULL;

    // Identifies peripheral (PLIB-level) ID
    deviceLayerInit.usbID = USB_ID_1;

    // Boolean flag: true -> Stop USB module in Idle Mode
    deviceLayerInit.stopInIdle = false;

    // Boolean flag: true -> Suspend USB in Sleep Mode
    deviceLayerInit.suspendInSleep = false;

    // Interrupt Source for USB module
    deviceLayerInit.interruptSource = INT_SOURCE_USB_1;

    // Number of function drivers registered to this instance of the
    // USB device layer
    deviceLayerInit.registeredFuncCount = 1;

    // Function driver table registered to this instance of the USB device layer
    deviceLayerInit.registeredFunctions = funcRegistrationTable;

    // Pointer to USB Descriptor structure
    deviceLayerInit.usbMasterDescriptor   = &usbMasterDescriptor;

    // Pointer to an endpoint table.
    deviceLayerInit.endpointTable = endpointTable;

    // USB device initialization
    usbDeviceObj = USB_DEVICE_Initialize(USB_DEVICE_INDEX_0, &deviceLayerInit);

    if (SYS_MODULE_OBJ_INVALID == usbDeviceObj)
    {
        // Handle error
    }

## Remarks

This routine must be called before any other USB Device Layer routine is called and after the initialization of USB Device Driver. This routine should only be called once during system initialization.

.