diff --git a/applications/bad_usb/scenes/bad_usb_scene_file_select.c b/applications/bad_usb/scenes/bad_usb_scene_file_select.c index 678a003c..b1ab3194 100644 --- a/applications/bad_usb/scenes/bad_usb_scene_file_select.c +++ b/applications/bad_usb/scenes/bad_usb_scene_file_select.c @@ -1,5 +1,6 @@ #include "../bad_usb_app_i.h" #include "furi_hal_power.h" +#include "furi_hal_usb.h" static bool bad_usb_file_select(BadUsbApp* bad_usb) { furi_assert(bad_usb); @@ -18,9 +19,12 @@ static bool bad_usb_file_select(BadUsbApp* bad_usb) { void bad_usb_scene_file_select_on_enter(void* context) { BadUsbApp* bad_usb = context; + furi_hal_usb_disable(); + if(bad_usb_file_select(bad_usb)) { scene_manager_next_scene(bad_usb->scene_manager, BadUsbSceneWork); } else { + furi_hal_usb_enable(); //scene_manager_previous_scene(bad_usb->scene_manager); view_dispatcher_stop(bad_usb->view_dispatcher); } diff --git a/firmware/targets/f7/furi_hal/furi_hal_usb.c b/firmware/targets/f7/furi_hal/furi_hal_usb.c index db769e2a..00425c75 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_usb.c +++ b/firmware/targets/f7/furi_hal/furi_hal_usb.c @@ -13,7 +13,6 @@ typedef struct { FuriThread* thread; - osTimerId_t tmr; bool enabled; bool connected; bool mode_lock; @@ -53,8 +52,6 @@ static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep); static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep); static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep); -static void furi_hal_usb_tmr_cb(void* context); - /* Low-level init */ void furi_hal_usb_init(void) { LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; @@ -139,11 +136,6 @@ void furi_hal_usb_reinit() { osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventReinit); } -static void furi_hal_usb_tmr_cb(void* context) { - furi_assert(usb.thread); - osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChangeStart); -} - /* Get device / configuration descriptors */ static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) { const uint8_t dtype = req->wValue >> 8; @@ -226,10 +218,10 @@ static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) { } static int32_t furi_hal_usb_thread(void* context) { - usb.tmr = osTimerNew(furi_hal_usb_tmr_cb, osTimerOnce, NULL, NULL); - bool usb_request_pending = false; uint8_t usb_wait_time = 0; + FuriHalUsbInterface* if_new = NULL; + FuriHalUsbInterface* if_ctx_new = NULL; if(usb.if_next != NULL) { osThreadFlagsSet(furi_thread_get_thread_id(usb.thread), EventModeChange); @@ -240,14 +232,15 @@ static int32_t furi_hal_usb_thread(void* context) { if((flags & osFlagsError) == 0) { if(flags & EventModeChange) { if(usb.if_next != usb.if_cur) { + if_new = usb.if_next; + if_ctx_new = usb.if_ctx; if(usb.enabled) { // Disable current interface susp_evt(&udev, 0, 0); usbd_connect(&udev, false); usb.enabled = false; - osTimerStart(usb.tmr, USB_RECONNECT_DELAY); - } else { - flags |= EventModeChangeStart; + osDelay(USB_RECONNECT_DELAY); } + flags |= EventModeChangeStart; } } if(flags & EventReinit) { @@ -261,19 +254,20 @@ static int32_t furi_hal_usb_thread(void* context) { usbd_enable(&udev, false); usbd_enable(&udev, true); - usb.if_next = usb.if_cur; - osTimerStart(usb.tmr, USB_RECONNECT_DELAY); + if_new = usb.if_cur; + osDelay(USB_RECONNECT_DELAY); + flags |= EventModeChangeStart; } if(flags & EventModeChangeStart) { // Second stage of mode change process if(usb.if_cur != NULL) { usb.if_cur->deinit(&udev); } - if(usb.if_next != NULL) { - usb.if_next->init(&udev, usb.if_next, usb.if_ctx); + if(if_new != NULL) { + if_new->init(&udev, if_new, if_ctx_new); usbd_reg_event(&udev, usbd_evt_reset, reset_evt); FURI_LOG_I(TAG, "USB Mode change done"); usb.enabled = true; - usb.if_cur = usb.if_next; + usb.if_cur = if_new; } } if(flags & EventEnable) { @@ -293,8 +287,10 @@ static int32_t furi_hal_usb_thread(void* context) { } } if(flags & EventReset) { - usb_request_pending = true; - usb_wait_time = 0; + if(usb.enabled) { + usb_request_pending = true; + usb_wait_time = 0; + } } if(flags & EventRequest) { usb_request_pending = false;