Jump to main content

##

# 2.2.4.9 USB_DEVICE_PowerStateSet Function

## C

    void USB_DEVICE_PowerStateSet(
        USB_DEVICE_HANDLE usbDeviceHandle,
        USB_DEVICE_POWER_STATE powerState
    );

## Summary

Application clients can use this function to set the power state of the device. A USB device can be bus powered or self powered. Based on hardware configuration, this power state may change while the device is on operation. The application can call this function to update the Device Layer on the present power status of the device.

## Precondition

The device layer should have been initialized and opened.

## Parameters

Parameters | Description
usbDeviceHandle | USB device handle returned by USB_DEVICE_Open()
powerState | USB_DEVICE_POWER_STATE_BUS_POWERED/ USB_DEVICE_POWER_STATE_SELF_POWERED

## Returns

None.

## Example

    / The following code example shows how the application can
    // change the power state of the device. In this case the application checks
    // if a battery is charged and if so, the application set the device power
    // state to self-powered.

    if(APP_BATTERY_IS_CHARGED == APP_BatteryChargeStatusGet())
    {
        // The application switches if power source.

        APP_PowerSourceSwitch(APP_POWER_SOURCE_BATTERY);
        USB_DEVICE_PowerStateSet(usbDeviceHandle, USB_DEVICE_POWER_STATE_SELF_POWERED);
    }
    else
    {
        // The battery is still not charged. The application uses the USB power.

        USB_DEVICE_PowerStateSet(usbDeviceHandle, USB_DEVICE_POWER_STATE_BUS_POWERED);
    }

## Remarks

By default, the device is bus powered.