diff --git a/src/usb_device_module.c b/src/usb_device_module.c index 2605e32..872301c 100644 --- a/src/usb_device_module.c +++ b/src/usb_device_module.c @@ -11,6 +11,8 @@ #include #include +#include + #include "usb_device_module.h" #include "usb_device_state_event.h" #include "usb_function_ready_event.h" @@ -71,6 +73,13 @@ static void update_usb_device_state(enum usb_device_state state) submit_usb_device_state_event(state); } +static void update_power_manager_restriction(bool vbus_present) +{ + power_manager_restrict(MODULE_IDX(MODULE), + vbus_present ? POWER_MANAGER_LEVEL_ALIVE : + POWER_MANAGER_LEVEL_SUSPENDED); +} + static int usb_descriptors_init(void) { int err; @@ -112,6 +121,7 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx, switch (msg->type) { case USBD_MSG_VBUS_READY: + update_power_manager_restriction(true); if (!usb_enabled) { int err = usbd_enable(&blinky_usbd); @@ -125,6 +135,7 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx, break; case USBD_MSG_VBUS_REMOVED: + update_power_manager_restriction(false); if (usb_enabled) { int err = usbd_disable(&blinky_usbd); @@ -207,6 +218,7 @@ static int module_init(void) ready_function_mask = 0U; prepare_broadcasted = false; usbd_initialized = false; + update_power_manager_restriction(false); return 0; }