|
|
@@ -4,6 +4,7 @@
|
|
|
#include <furi_hal_power.h>
|
|
|
#include <stm32wbxx_ll_pwr.h>
|
|
|
#include <furi.h>
|
|
|
+#include <toolbox/api_lock.h>
|
|
|
|
|
|
#include "usb.h"
|
|
|
|
|
|
@@ -11,35 +12,67 @@
|
|
|
|
|
|
#define USB_RECONNECT_DELAY 500
|
|
|
|
|
|
+typedef enum {
|
|
|
+ UsbApiEventTypeSetConfig,
|
|
|
+ UsbApiEventTypeGetConfig,
|
|
|
+ UsbApiEventTypeLock,
|
|
|
+ UsbApiEventTypeUnlock,
|
|
|
+ UsbApiEventTypeIsLocked,
|
|
|
+ UsbApiEventTypeEnable,
|
|
|
+ UsbApiEventTypeDisable,
|
|
|
+ UsbApiEventTypeReinit,
|
|
|
+ UsbApiEventTypeSetStateCallback,
|
|
|
+} UsbApiEventType;
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ FuriHalUsbStateCallback callback;
|
|
|
+ void* context;
|
|
|
+} UsbApiEventDataStateCallback;
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ FuriHalUsbInterface* interface;
|
|
|
+ void* context;
|
|
|
+} UsbApiEventDataInterface;
|
|
|
+
|
|
|
+typedef union {
|
|
|
+ UsbApiEventDataStateCallback state_callback;
|
|
|
+ UsbApiEventDataInterface interface;
|
|
|
+} UsbApiEventData;
|
|
|
+
|
|
|
+typedef union {
|
|
|
+ bool bool_value;
|
|
|
+ void* void_value;
|
|
|
+} UsbApiEventReturnData;
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ FuriApiLock lock;
|
|
|
+ UsbApiEventType type;
|
|
|
+ UsbApiEventData data;
|
|
|
+ UsbApiEventReturnData* return_data;
|
|
|
+} UsbApiEventMessage;
|
|
|
+
|
|
|
typedef struct {
|
|
|
FuriThread* thread;
|
|
|
+ FuriMessageQueue* queue;
|
|
|
bool enabled;
|
|
|
bool connected;
|
|
|
bool mode_lock;
|
|
|
- FuriHalUsbInterface* if_cur;
|
|
|
- FuriHalUsbInterface* if_next;
|
|
|
- void* if_ctx;
|
|
|
+ bool request_pending;
|
|
|
+ FuriHalUsbInterface* interface;
|
|
|
+ void* interface_context;
|
|
|
FuriHalUsbStateCallback callback;
|
|
|
- void* cb_ctx;
|
|
|
+ void* callback_context;
|
|
|
} UsbSrv;
|
|
|
|
|
|
typedef enum {
|
|
|
- EventModeChange = (1 << 0),
|
|
|
- EventEnable = (1 << 1),
|
|
|
- EventDisable = (1 << 2),
|
|
|
- EventReinit = (1 << 3),
|
|
|
-
|
|
|
- EventReset = (1 << 4),
|
|
|
- EventRequest = (1 << 5),
|
|
|
-
|
|
|
- EventModeChangeStart = (1 << 6),
|
|
|
+ UsbEventReset = (1 << 0),
|
|
|
+ UsbEventRequest = (1 << 1),
|
|
|
+ UsbEventMessage = (1 << 2),
|
|
|
} UsbEvent;
|
|
|
|
|
|
-#define USB_SRV_ALL_EVENTS \
|
|
|
- (EventModeChange | EventEnable | EventDisable | EventReinit | EventReset | EventRequest | \
|
|
|
- EventModeChangeStart)
|
|
|
+#define USB_SRV_ALL_EVENTS (UsbEventReset | UsbEventRequest | UsbEventMessage)
|
|
|
|
|
|
-static UsbSrv usb;
|
|
|
+PLACE_IN_SECTION("MB_MEM2") static UsbSrv usb = {0};
|
|
|
|
|
|
static const struct usb_string_descriptor dev_lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
|
|
|
|
|
|
@@ -74,12 +107,13 @@ void furi_hal_usb_init(void) {
|
|
|
// Reset callback will be enabled after first mode change to avoid getting false reset events
|
|
|
|
|
|
usb.enabled = false;
|
|
|
- usb.if_cur = NULL;
|
|
|
+ usb.interface = NULL;
|
|
|
NVIC_SetPriority(USB_LP_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
|
|
|
NVIC_SetPriority(USB_HP_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 15, 0));
|
|
|
NVIC_EnableIRQ(USB_LP_IRQn);
|
|
|
NVIC_EnableIRQ(USB_HP_IRQn);
|
|
|
|
|
|
+ usb.queue = furi_message_queue_alloc(1, sizeof(UsbApiEventMessage));
|
|
|
usb.thread = furi_thread_alloc_ex("UsbDriver", 1024, furi_hal_usb_thread, NULL);
|
|
|
furi_thread_mark_as_service(usb.thread);
|
|
|
furi_thread_start(usb.thread);
|
|
|
@@ -87,53 +121,119 @@ void furi_hal_usb_init(void) {
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
|
|
}
|
|
|
|
|
|
-bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
|
|
|
- if(usb.mode_lock) {
|
|
|
- return false;
|
|
|
- }
|
|
|
+static void furi_hal_usb_send_message(UsbApiEventMessage* message) {
|
|
|
+ furi_message_queue_put(usb.queue, message, FuriWaitForever);
|
|
|
+ furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
|
|
+ api_lock_wait_unlock_and_free(message->lock);
|
|
|
+}
|
|
|
|
|
|
- usb.if_next = new_if;
|
|
|
- usb.if_ctx = ctx;
|
|
|
- if(usb.thread == NULL) {
|
|
|
- // Service thread hasn't started yet, so just save interface mode
|
|
|
- return true;
|
|
|
- }
|
|
|
- furi_assert(usb.thread);
|
|
|
- furi_thread_flags_set(furi_thread_get_id(usb.thread), EventModeChange);
|
|
|
- return true;
|
|
|
+bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
|
|
|
+ UsbApiEventReturnData return_data = {
|
|
|
+ .bool_value = false,
|
|
|
+ };
|
|
|
+
|
|
|
+ UsbApiEventMessage msg = {
|
|
|
+ .lock = api_lock_alloc_locked(),
|
|
|
+ .type = UsbApiEventTypeSetConfig,
|
|
|
+ .data.interface =
|
|
|
+ {
|
|
|
+ .interface = new_if,
|
|
|
+ .context = ctx,
|
|
|
+ },
|
|
|
+ .return_data = &return_data,
|
|
|
+ };
|
|
|
+
|
|
|
+ furi_hal_usb_send_message(&msg);
|
|
|
+ return return_data.bool_value;
|
|
|
}
|
|
|
|
|
|
FuriHalUsbInterface* furi_hal_usb_get_config() {
|
|
|
- return usb.if_cur;
|
|
|
+ UsbApiEventReturnData return_data = {
|
|
|
+ .void_value = NULL,
|
|
|
+ };
|
|
|
+
|
|
|
+ UsbApiEventMessage msg = {
|
|
|
+ .lock = api_lock_alloc_locked(),
|
|
|
+ .type = UsbApiEventTypeGetConfig,
|
|
|
+ .return_data = &return_data,
|
|
|
+ };
|
|
|
+
|
|
|
+ furi_hal_usb_send_message(&msg);
|
|
|
+ return return_data.void_value;
|
|
|
}
|
|
|
|
|
|
void furi_hal_usb_lock() {
|
|
|
- FURI_LOG_I(TAG, "Mode lock");
|
|
|
- usb.mode_lock = true;
|
|
|
+ UsbApiEventMessage msg = {
|
|
|
+ .lock = api_lock_alloc_locked(),
|
|
|
+ .type = UsbApiEventTypeLock,
|
|
|
+ };
|
|
|
+
|
|
|
+ furi_hal_usb_send_message(&msg);
|
|
|
}
|
|
|
|
|
|
void furi_hal_usb_unlock() {
|
|
|
- FURI_LOG_I(TAG, "Mode unlock");
|
|
|
- usb.mode_lock = false;
|
|
|
+ UsbApiEventMessage msg = {
|
|
|
+ .lock = api_lock_alloc_locked(),
|
|
|
+ .type = UsbApiEventTypeUnlock,
|
|
|
+ };
|
|
|
+
|
|
|
+ furi_hal_usb_send_message(&msg);
|
|
|
}
|
|
|
|
|
|
bool furi_hal_usb_is_locked() {
|
|
|
- return usb.mode_lock;
|
|
|
+ UsbApiEventReturnData return_data = {
|
|
|
+ .bool_value = false,
|
|
|
+ };
|
|
|
+
|
|
|
+ UsbApiEventMessage msg = {
|
|
|
+ .lock = api_lock_alloc_locked(),
|
|
|
+ .type = UsbApiEventTypeIsLocked,
|
|
|
+ .return_data = &return_data,
|
|
|
+ };
|
|
|
+
|
|
|
+ furi_hal_usb_send_message(&msg);
|
|
|
+ return return_data.bool_value;
|
|
|
}
|
|
|
|
|
|
void furi_hal_usb_disable() {
|
|
|
- furi_assert(usb.thread);
|
|
|
- furi_thread_flags_set(furi_thread_get_id(usb.thread), EventDisable);
|
|
|
+ UsbApiEventMessage msg = {
|
|
|
+ .lock = api_lock_alloc_locked(),
|
|
|
+ .type = UsbApiEventTypeDisable,
|
|
|
+ };
|
|
|
+
|
|
|
+ furi_hal_usb_send_message(&msg);
|
|
|
}
|
|
|
|
|
|
void furi_hal_usb_enable() {
|
|
|
- furi_assert(usb.thread);
|
|
|
- furi_thread_flags_set(furi_thread_get_id(usb.thread), EventEnable);
|
|
|
+ UsbApiEventMessage msg = {
|
|
|
+ .lock = api_lock_alloc_locked(),
|
|
|
+ .type = UsbApiEventTypeEnable,
|
|
|
+ };
|
|
|
+
|
|
|
+ furi_hal_usb_send_message(&msg);
|
|
|
}
|
|
|
|
|
|
void furi_hal_usb_reinit() {
|
|
|
- furi_assert(usb.thread);
|
|
|
- furi_thread_flags_set(furi_thread_get_id(usb.thread), EventReinit);
|
|
|
+ UsbApiEventMessage msg = {
|
|
|
+ .lock = api_lock_alloc_locked(),
|
|
|
+ .type = UsbApiEventTypeReinit,
|
|
|
+ };
|
|
|
+
|
|
|
+ furi_hal_usb_send_message(&msg);
|
|
|
+}
|
|
|
+
|
|
|
+void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* ctx) {
|
|
|
+ UsbApiEventMessage msg = {
|
|
|
+ .lock = api_lock_alloc_locked(),
|
|
|
+ .type = UsbApiEventTypeSetStateCallback,
|
|
|
+ .data.state_callback =
|
|
|
+ {
|
|
|
+ .callback = cb,
|
|
|
+ .context = ctx,
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
+ furi_hal_usb_send_message(&msg);
|
|
|
}
|
|
|
|
|
|
/* Get device / configuration descriptors */
|
|
|
@@ -142,29 +242,29 @@ static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_
|
|
|
const uint8_t dnumber = req->wValue & 0xFF;
|
|
|
const void* desc;
|
|
|
uint16_t len = 0;
|
|
|
- if(usb.if_cur == NULL) return usbd_fail;
|
|
|
+ if(usb.interface == NULL) return usbd_fail;
|
|
|
|
|
|
switch(dtype) {
|
|
|
case USB_DTYPE_DEVICE:
|
|
|
- furi_thread_flags_set(furi_thread_get_id(usb.thread), EventRequest);
|
|
|
+ furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventRequest);
|
|
|
if(usb.callback != NULL) {
|
|
|
- usb.callback(FuriHalUsbStateEventDescriptorRequest, usb.cb_ctx);
|
|
|
+ usb.callback(FuriHalUsbStateEventDescriptorRequest, usb.callback_context);
|
|
|
}
|
|
|
- desc = usb.if_cur->dev_descr;
|
|
|
+ desc = usb.interface->dev_descr;
|
|
|
break;
|
|
|
case USB_DTYPE_CONFIGURATION:
|
|
|
- desc = usb.if_cur->cfg_descr;
|
|
|
- len = ((struct usb_string_descriptor*)(usb.if_cur->cfg_descr))->wString[0];
|
|
|
+ desc = usb.interface->cfg_descr;
|
|
|
+ len = ((struct usb_string_descriptor*)(usb.interface->cfg_descr))->wString[0];
|
|
|
break;
|
|
|
case USB_DTYPE_STRING:
|
|
|
if(dnumber == UsbDevLang) {
|
|
|
desc = &dev_lang_desc;
|
|
|
- } else if((dnumber == UsbDevManuf) && (usb.if_cur->str_manuf_descr != NULL)) {
|
|
|
- desc = usb.if_cur->str_manuf_descr;
|
|
|
- } else if((dnumber == UsbDevProduct) && (usb.if_cur->str_prod_descr != NULL)) {
|
|
|
- desc = usb.if_cur->str_prod_descr;
|
|
|
- } else if((dnumber == UsbDevSerial) && (usb.if_cur->str_serial_descr != NULL)) {
|
|
|
- desc = usb.if_cur->str_serial_descr;
|
|
|
+ } else if((dnumber == UsbDevManuf) && (usb.interface->str_manuf_descr != NULL)) {
|
|
|
+ desc = usb.interface->str_manuf_descr;
|
|
|
+ } else if((dnumber == UsbDevProduct) && (usb.interface->str_prod_descr != NULL)) {
|
|
|
+ desc = usb.interface->str_prod_descr;
|
|
|
+ } else if((dnumber == UsbDevSerial) && (usb.interface->str_serial_descr != NULL)) {
|
|
|
+ desc = usb.interface->str_serial_descr;
|
|
|
} else
|
|
|
return usbd_fail;
|
|
|
break;
|
|
|
@@ -181,18 +281,13 @@ static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_
|
|
|
return usbd_ack;
|
|
|
}
|
|
|
|
|
|
-void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* ctx) {
|
|
|
- usb.callback = cb;
|
|
|
- usb.cb_ctx = ctx;
|
|
|
-}
|
|
|
-
|
|
|
static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
|
|
UNUSED(dev);
|
|
|
UNUSED(event);
|
|
|
UNUSED(ep);
|
|
|
- furi_thread_flags_set(furi_thread_get_id(usb.thread), EventReset);
|
|
|
+ furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventReset);
|
|
|
if(usb.callback != NULL) {
|
|
|
- usb.callback(FuriHalUsbStateEventReset, usb.cb_ctx);
|
|
|
+ usb.callback(FuriHalUsbStateEventReset, usb.callback_context);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -200,14 +295,14 @@ static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
|
|
UNUSED(dev);
|
|
|
UNUSED(event);
|
|
|
UNUSED(ep);
|
|
|
- if((usb.if_cur != NULL) && (usb.connected == true)) {
|
|
|
+ if((usb.interface != NULL) && (usb.connected == true)) {
|
|
|
usb.connected = false;
|
|
|
- usb.if_cur->suspend(&udev);
|
|
|
+ usb.interface->suspend(&udev);
|
|
|
|
|
|
furi_hal_power_insomnia_exit();
|
|
|
}
|
|
|
if(usb.callback != NULL) {
|
|
|
- usb.callback(FuriHalUsbStateEventSuspend, usb.cb_ctx);
|
|
|
+ usb.callback(FuriHalUsbStateEventSuspend, usb.callback_context);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -215,101 +310,161 @@ static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
|
|
UNUSED(dev);
|
|
|
UNUSED(event);
|
|
|
UNUSED(ep);
|
|
|
- if((usb.if_cur != NULL) && (usb.connected == false)) {
|
|
|
+ if((usb.interface != NULL) && (usb.connected == false)) {
|
|
|
usb.connected = true;
|
|
|
- usb.if_cur->wakeup(&udev);
|
|
|
+ usb.interface->wakeup(&udev);
|
|
|
|
|
|
furi_hal_power_insomnia_enter();
|
|
|
}
|
|
|
if(usb.callback != NULL) {
|
|
|
- usb.callback(FuriHalUsbStateEventWakeup, usb.cb_ctx);
|
|
|
+ usb.callback(FuriHalUsbStateEventWakeup, usb.callback_context);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void usb_process_mode_start(FuriHalUsbInterface* interface, void* context) {
|
|
|
+ if(usb.interface != NULL) {
|
|
|
+ usb.interface->deinit(&udev);
|
|
|
+ }
|
|
|
+
|
|
|
+ __disable_irq();
|
|
|
+ usb.interface = interface;
|
|
|
+ usb.interface_context = context;
|
|
|
+ __enable_irq();
|
|
|
+
|
|
|
+ if(interface != NULL) {
|
|
|
+ interface->init(&udev, interface, context);
|
|
|
+ usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
|
|
|
+ FURI_LOG_I(TAG, "USB Mode change done");
|
|
|
+ usb.enabled = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+static void usb_process_mode_change(FuriHalUsbInterface* interface, void* context) {
|
|
|
+ if(interface != usb.interface) {
|
|
|
+ if(usb.enabled) {
|
|
|
+ // Disable current interface
|
|
|
+ susp_evt(&udev, 0, 0);
|
|
|
+ usbd_connect(&udev, false);
|
|
|
+ usb.enabled = false;
|
|
|
+ furi_delay_ms(USB_RECONNECT_DELAY);
|
|
|
+ }
|
|
|
+ usb_process_mode_start(interface, context);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void usb_process_mode_reinit() {
|
|
|
+ // Temporary disable callback to avoid getting false reset events
|
|
|
+ usbd_reg_event(&udev, usbd_evt_reset, NULL);
|
|
|
+ FURI_LOG_I(TAG, "USB Reinit");
|
|
|
+ susp_evt(&udev, 0, 0);
|
|
|
+ usbd_connect(&udev, false);
|
|
|
+ usb.enabled = false;
|
|
|
+
|
|
|
+ usbd_enable(&udev, false);
|
|
|
+ usbd_enable(&udev, true);
|
|
|
+
|
|
|
+ furi_delay_ms(USB_RECONNECT_DELAY);
|
|
|
+ usb_process_mode_start(usb.interface, usb.interface_context);
|
|
|
+}
|
|
|
+
|
|
|
+static bool usb_process_set_config(FuriHalUsbInterface* interface, void* context) {
|
|
|
+ if(usb.mode_lock) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ usb_process_mode_change(interface, context);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void usb_process_enable(bool enable) {
|
|
|
+ if(enable) {
|
|
|
+ if((!usb.enabled) && (usb.interface != NULL)) {
|
|
|
+ usbd_connect(&udev, true);
|
|
|
+ usb.enabled = true;
|
|
|
+ FURI_LOG_I(TAG, "USB Enable");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if(usb.enabled) {
|
|
|
+ susp_evt(&udev, 0, 0);
|
|
|
+ usbd_connect(&udev, false);
|
|
|
+ usb.enabled = false;
|
|
|
+ usb.request_pending = false;
|
|
|
+ FURI_LOG_I(TAG, "USB Disable");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void usb_process_message(UsbApiEventMessage* message) {
|
|
|
+ switch(message->type) {
|
|
|
+ case UsbApiEventTypeSetConfig:
|
|
|
+ message->return_data->bool_value = usb_process_set_config(
|
|
|
+ message->data.interface.interface, message->data.interface.context);
|
|
|
+ break;
|
|
|
+ case UsbApiEventTypeGetConfig:
|
|
|
+ message->return_data->void_value = usb.interface;
|
|
|
+ break;
|
|
|
+ case UsbApiEventTypeLock:
|
|
|
+ FURI_LOG_I(TAG, "Mode lock");
|
|
|
+ usb.mode_lock = true;
|
|
|
+ break;
|
|
|
+ case UsbApiEventTypeUnlock:
|
|
|
+ FURI_LOG_I(TAG, "Mode unlock");
|
|
|
+ usb.mode_lock = false;
|
|
|
+ break;
|
|
|
+ case UsbApiEventTypeIsLocked:
|
|
|
+ message->return_data->bool_value = usb.mode_lock;
|
|
|
+ break;
|
|
|
+ case UsbApiEventTypeDisable:
|
|
|
+ usb_process_enable(false);
|
|
|
+ break;
|
|
|
+ case UsbApiEventTypeEnable:
|
|
|
+ usb_process_enable(true);
|
|
|
+ break;
|
|
|
+ case UsbApiEventTypeReinit:
|
|
|
+ usb_process_mode_reinit();
|
|
|
+ break;
|
|
|
+ case UsbApiEventTypeSetStateCallback:
|
|
|
+ usb.callback = message->data.state_callback.callback;
|
|
|
+ usb.callback_context = message->data.state_callback.context;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ api_lock_unlock(message->lock);
|
|
|
+}
|
|
|
+
|
|
|
static int32_t furi_hal_usb_thread(void* context) {
|
|
|
UNUSED(context);
|
|
|
- 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) {
|
|
|
- furi_thread_flags_set(furi_thread_get_id(usb.thread), EventModeChange);
|
|
|
+ if(furi_message_queue_get_count(usb.queue) > 0) {
|
|
|
+ furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
|
|
}
|
|
|
|
|
|
while(true) {
|
|
|
uint32_t flags = furi_thread_flags_wait(USB_SRV_ALL_EVENTS, FuriFlagWaitAny, 500);
|
|
|
- if((flags & FuriFlagError) == 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;
|
|
|
- furi_delay_ms(USB_RECONNECT_DELAY);
|
|
|
- }
|
|
|
- flags |= EventModeChangeStart;
|
|
|
- }
|
|
|
- }
|
|
|
- if(flags & EventReinit) {
|
|
|
- // Temporary disable callback to avoid getting false reset events
|
|
|
- usbd_reg_event(&udev, usbd_evt_reset, NULL);
|
|
|
- FURI_LOG_I(TAG, "USB Reinit");
|
|
|
- susp_evt(&udev, 0, 0);
|
|
|
- usbd_connect(&udev, false);
|
|
|
- usb.enabled = false;
|
|
|
-
|
|
|
- usbd_enable(&udev, false);
|
|
|
- usbd_enable(&udev, true);
|
|
|
-
|
|
|
- if_new = usb.if_cur;
|
|
|
- furi_delay_ms(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(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 = if_new;
|
|
|
- }
|
|
|
- if(flags & EventEnable) {
|
|
|
- if((!usb.enabled) && (usb.if_cur != NULL)) {
|
|
|
- usbd_connect(&udev, true);
|
|
|
- usb.enabled = true;
|
|
|
- FURI_LOG_I(TAG, "USB Enable");
|
|
|
- }
|
|
|
- }
|
|
|
- if(flags & EventDisable) {
|
|
|
- if(usb.enabled) {
|
|
|
- susp_evt(&udev, 0, 0);
|
|
|
- usbd_connect(&udev, false);
|
|
|
- usb.enabled = false;
|
|
|
- usb_request_pending = false;
|
|
|
- FURI_LOG_I(TAG, "USB Disable");
|
|
|
- }
|
|
|
+
|
|
|
+ {
|
|
|
+ UsbApiEventMessage message;
|
|
|
+ if(furi_message_queue_get(usb.queue, &message, 0) == FuriStatusOk) {
|
|
|
+ usb_process_message(&message);
|
|
|
}
|
|
|
- if(flags & EventReset) {
|
|
|
+ }
|
|
|
+
|
|
|
+ if((flags & FuriFlagError) == 0) {
|
|
|
+ if(flags & UsbEventReset) {
|
|
|
if(usb.enabled) {
|
|
|
- usb_request_pending = true;
|
|
|
+ usb.request_pending = true;
|
|
|
usb_wait_time = 0;
|
|
|
}
|
|
|
}
|
|
|
- if(flags & EventRequest) {
|
|
|
- usb_request_pending = false;
|
|
|
+ if(flags & UsbEventRequest) {
|
|
|
+ usb.request_pending = false;
|
|
|
}
|
|
|
- } else if(usb_request_pending) {
|
|
|
+ } else if(usb.request_pending) {
|
|
|
usb_wait_time++;
|
|
|
if(usb_wait_time > 4) {
|
|
|
- furi_hal_usb_reinit();
|
|
|
- usb_request_pending = false;
|
|
|
+ usb_process_mode_reinit();
|
|
|
+ usb.request_pending = false;
|
|
|
}
|
|
|
}
|
|
|
}
|