USB Libraries Help > USB Device Libraries > USB Device Layer Library > Library Interface > c) Device Power State Management Functions > USB_DEVICE_PowerStateSet Function
MPLAB Harmony USB Stack
USB_DEVICE_PowerStateSet Function

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.

C
void USB_DEVICE_PowerStateSet(
    USB_DEVICE_HANDLE usbDeviceHandle, 
    USB_DEVICE_POWER_STATE powerState
);
Preconditions

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.

Remarks

By default, the device is bus powered.

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);
}