Jump to main content

##

# 2.2.4.16 USB_DEVICE_Attach Function

## C

    void USB_DEVICE_Attach(
        USB_DEVICE_HANDLE usbDeviceHandle
    );

## Summary

This function will attach the device to the USB. It does this by enabling the pull up resistors on the D+ or D- lines. This function should be called after the USB device layer has generated the USB_DEVICE_EVENT_POWER_DETECTED event.

## Precondition

Client handle should be valid. The device layer should have been initialized and an device layer event handler function should have been assigned.

## Parameters

Parameters | Description
usbDeviceHandle | Client's USB device layer handle (returned from USB_DEVICE_Open)

## Returns

None.

## Example

    // This code example shows the set
    // of steps to follow before attaching the
    // device on the bus. It is assumed that the
    // device layer is already initialized.

    USB_DEVICE_HANDLE usbDeviceHandle;

    // Get an handle to the USB device layer.
    usbDeviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0,
                                                      DRV_IO_INTENT_READWRITE );

    if(USB_DEVICE_HANDLE_INVALID == usbDeviceHandle)
    {
        // Failed to open handle.
        // Handle error.
    }

    // Register an event handler call back function with device layer
    // so that we are ready to receive events when the device is
    // attached to the bus.

    USB_DEVICE_EventHandlerSet(usbDeviceHandle, APP_USBDeviceEventHandler, NULL);

    // Now, connect device to USB
    USB_DEVICE_Attach(usbDeviceHandle);

## Remarks

None.