mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-26 05:54:46 -07:00
HAL: blocking usb API
This commit is contained in:
@@ -627,14 +627,6 @@ static void hid_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
|
|||||||
usbd_connect(dev, true);
|
usbd_connect(dev, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool deinit_flag = false;
|
|
||||||
|
|
||||||
void dap_common_wait_for_deinit() {
|
|
||||||
while(!deinit_flag) {
|
|
||||||
furi_delay_ms(50);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void hid_deinit(usbd_device* dev) {
|
static void hid_deinit(usbd_device* dev) {
|
||||||
dap_state.usb_dev = NULL;
|
dap_state.usb_dev = NULL;
|
||||||
|
|
||||||
@@ -650,9 +642,6 @@ static void hid_deinit(usbd_device* dev) {
|
|||||||
|
|
||||||
free(usb_hid.str_manuf_descr);
|
free(usb_hid.str_manuf_descr);
|
||||||
free(usb_hid.str_prod_descr);
|
free(usb_hid.str_prod_descr);
|
||||||
|
|
||||||
FURI_SW_MEMBARRIER();
|
|
||||||
deinit_flag = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hid_on_wakeup(usbd_device* dev) {
|
static void hid_on_wakeup(usbd_device* dev) {
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ void furi_hal_init() {
|
|||||||
|
|
||||||
// USB
|
// USB
|
||||||
#ifndef FURI_RAM_EXEC
|
#ifndef FURI_RAM_EXEC
|
||||||
|
// TODO: call furi_hal_memory_init before furi_hal_usb_init, to get more memory
|
||||||
furi_hal_usb_init();
|
furi_hal_usb_init();
|
||||||
FURI_LOG_I(TAG, "USB OK");
|
FURI_LOG_I(TAG, "USB OK");
|
||||||
#endif
|
#endif
|
||||||
@@ -79,7 +80,7 @@ void furi_hal_init() {
|
|||||||
furi_hal_rfid_init();
|
furi_hal_rfid_init();
|
||||||
#endif
|
#endif
|
||||||
furi_hal_bt_init();
|
furi_hal_bt_init();
|
||||||
furi_hal_memory_init();
|
furi_hal_memory_init(); // must be called after furi_hal_bt_init
|
||||||
furi_hal_compress_icon_init();
|
furi_hal_compress_icon_init();
|
||||||
|
|
||||||
// FatFS driver initialization
|
// FatFS driver initialization
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <furi_hal_power.h>
|
#include <furi_hal_power.h>
|
||||||
#include <stm32wbxx_ll_pwr.h>
|
#include <stm32wbxx_ll_pwr.h>
|
||||||
#include <furi.h>
|
#include <furi.h>
|
||||||
|
#include <toolbox/api_lock.h>
|
||||||
|
|
||||||
#include "usb.h"
|
#include "usb.h"
|
||||||
|
|
||||||
@@ -11,8 +12,48 @@
|
|||||||
|
|
||||||
#define USB_RECONNECT_DELAY 500
|
#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 {
|
typedef struct {
|
||||||
FuriThread* thread;
|
FuriThread* thread;
|
||||||
|
FuriMessageQueue* queue;
|
||||||
bool enabled;
|
bool enabled;
|
||||||
bool connected;
|
bool connected;
|
||||||
bool mode_lock;
|
bool mode_lock;
|
||||||
@@ -21,25 +62,19 @@ typedef struct {
|
|||||||
void* if_ctx;
|
void* if_ctx;
|
||||||
FuriHalUsbStateCallback callback;
|
FuriHalUsbStateCallback callback;
|
||||||
void* cb_ctx;
|
void* cb_ctx;
|
||||||
|
|
||||||
|
bool request_pending;
|
||||||
} UsbSrv;
|
} UsbSrv;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
EventModeChange = (1 << 0),
|
UsbEventReset = (1 << 0),
|
||||||
EventEnable = (1 << 1),
|
UsbEventRequest = (1 << 1),
|
||||||
EventDisable = (1 << 2),
|
UsbEventMessage = (1 << 2),
|
||||||
EventReinit = (1 << 3),
|
|
||||||
|
|
||||||
EventReset = (1 << 4),
|
|
||||||
EventRequest = (1 << 5),
|
|
||||||
|
|
||||||
EventModeChangeStart = (1 << 6),
|
|
||||||
} UsbEvent;
|
} UsbEvent;
|
||||||
|
|
||||||
#define USB_SRV_ALL_EVENTS \
|
#define USB_SRV_ALL_EVENTS (UsbEventReset | UsbEventRequest | UsbEventMessage)
|
||||||
(EventModeChange | EventEnable | EventDisable | EventReinit | EventReset | EventRequest | \
|
|
||||||
EventModeChangeStart)
|
|
||||||
|
|
||||||
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);
|
static const struct usb_string_descriptor dev_lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
|
||||||
|
|
||||||
@@ -80,62 +115,143 @@ void furi_hal_usb_init(void) {
|
|||||||
NVIC_EnableIRQ(USB_LP_IRQn);
|
NVIC_EnableIRQ(USB_LP_IRQn);
|
||||||
NVIC_EnableIRQ(USB_HP_IRQn);
|
NVIC_EnableIRQ(USB_HP_IRQn);
|
||||||
|
|
||||||
|
usb.queue = furi_message_queue_alloc(1, sizeof(UsbApiEventMessage));
|
||||||
|
|
||||||
usb.thread = furi_thread_alloc();
|
usb.thread = furi_thread_alloc();
|
||||||
furi_thread_set_name(usb.thread, "UsbDriver");
|
furi_thread_set_name(usb.thread, "UsbDriver");
|
||||||
furi_thread_set_stack_size(usb.thread, 1024);
|
furi_thread_set_stack_size(usb.thread, 1024);
|
||||||
furi_thread_set_callback(usb.thread, furi_hal_usb_thread);
|
furi_thread_set_callback(usb.thread, furi_hal_usb_thread);
|
||||||
|
furi_thread_mark_as_service(usb.thread);
|
||||||
furi_thread_start(usb.thread);
|
furi_thread_start(usb.thread);
|
||||||
|
|
||||||
FURI_LOG_I(TAG, "Init OK");
|
FURI_LOG_I(TAG, "Init OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
|
bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
|
||||||
if(usb.mode_lock) {
|
UsbApiEventMessage msg = {
|
||||||
return false;
|
.lock = api_lock_alloc_locked(),
|
||||||
}
|
.type = UsbApiEventTypeSetConfig,
|
||||||
|
.data.interface =
|
||||||
|
{
|
||||||
|
.interface = new_if,
|
||||||
|
.context = ctx,
|
||||||
|
},
|
||||||
|
.return_data =
|
||||||
|
{
|
||||||
|
.bool_value = false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
usb.if_next = new_if;
|
furi_message_queue_put(usb.queue, &msg, FuriWaitForever);
|
||||||
usb.if_ctx = ctx;
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
||||||
if(usb.thread == NULL) {
|
api_lock_wait_unlock_and_free(msg.lock);
|
||||||
// Service thread hasn't started yet, so just save interface mode
|
|
||||||
return true;
|
return msg.return_data.bool_value;
|
||||||
}
|
|
||||||
furi_assert(usb.thread);
|
|
||||||
furi_thread_flags_set(furi_thread_get_id(usb.thread), EventModeChange);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FuriHalUsbInterface* furi_hal_usb_get_config() {
|
FuriHalUsbInterface* furi_hal_usb_get_config() {
|
||||||
return usb.if_cur;
|
UsbApiEventMessage msg = {
|
||||||
|
.lock = api_lock_alloc_locked(),
|
||||||
|
.type = UsbApiEventTypeGetConfig,
|
||||||
|
.return_data =
|
||||||
|
{
|
||||||
|
.void_value = NULL,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
furi_message_queue_put(usb.queue, &msg, FuriWaitForever);
|
||||||
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
||||||
|
api_lock_wait_unlock_and_free(msg.lock);
|
||||||
|
|
||||||
|
return msg.return_data.void_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void furi_hal_usb_lock() {
|
void furi_hal_usb_lock() {
|
||||||
FURI_LOG_I(TAG, "Mode lock");
|
UsbApiEventMessage msg = {
|
||||||
usb.mode_lock = true;
|
.lock = api_lock_alloc_locked(),
|
||||||
|
.type = UsbApiEventTypeLock,
|
||||||
|
};
|
||||||
|
|
||||||
|
furi_message_queue_put(usb.queue, &msg, FuriWaitForever);
|
||||||
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
||||||
|
api_lock_wait_unlock_and_free(msg.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void furi_hal_usb_unlock() {
|
void furi_hal_usb_unlock() {
|
||||||
FURI_LOG_I(TAG, "Mode unlock");
|
UsbApiEventMessage msg = {
|
||||||
usb.mode_lock = false;
|
.lock = api_lock_alloc_locked(),
|
||||||
|
.type = UsbApiEventTypeUnlock,
|
||||||
|
};
|
||||||
|
|
||||||
|
furi_message_queue_put(usb.queue, &msg, FuriWaitForever);
|
||||||
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
||||||
|
api_lock_wait_unlock_and_free(msg.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool furi_hal_usb_is_locked() {
|
bool furi_hal_usb_is_locked() {
|
||||||
return usb.mode_lock;
|
UsbApiEventMessage msg = {
|
||||||
|
.lock = api_lock_alloc_locked(),
|
||||||
|
.type = UsbApiEventTypeIsLocked,
|
||||||
|
.return_data =
|
||||||
|
{
|
||||||
|
.bool_value = false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
furi_message_queue_put(usb.queue, &msg, FuriWaitForever);
|
||||||
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
||||||
|
api_lock_wait_unlock_and_free(msg.lock);
|
||||||
|
|
||||||
|
return msg.return_data.bool_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void furi_hal_usb_disable() {
|
void furi_hal_usb_disable() {
|
||||||
furi_assert(usb.thread);
|
UsbApiEventMessage msg = {
|
||||||
furi_thread_flags_set(furi_thread_get_id(usb.thread), EventDisable);
|
.lock = api_lock_alloc_locked(),
|
||||||
|
.type = UsbApiEventTypeDisable,
|
||||||
|
};
|
||||||
|
|
||||||
|
furi_message_queue_put(usb.queue, &msg, FuriWaitForever);
|
||||||
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
||||||
|
api_lock_wait_unlock_and_free(msg.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void furi_hal_usb_enable() {
|
void furi_hal_usb_enable() {
|
||||||
furi_assert(usb.thread);
|
UsbApiEventMessage msg = {
|
||||||
furi_thread_flags_set(furi_thread_get_id(usb.thread), EventEnable);
|
.lock = api_lock_alloc_locked(),
|
||||||
|
.type = UsbApiEventTypeEnable,
|
||||||
|
};
|
||||||
|
|
||||||
|
furi_message_queue_put(usb.queue, &msg, FuriWaitForever);
|
||||||
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
||||||
|
api_lock_wait_unlock_and_free(msg.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void furi_hal_usb_reinit() {
|
void furi_hal_usb_reinit() {
|
||||||
furi_assert(usb.thread);
|
UsbApiEventMessage msg = {
|
||||||
furi_thread_flags_set(furi_thread_get_id(usb.thread), EventReinit);
|
.lock = api_lock_alloc_locked(),
|
||||||
|
.type = UsbApiEventTypeReinit,
|
||||||
|
};
|
||||||
|
|
||||||
|
furi_message_queue_put(usb.queue, &msg, FuriWaitForever);
|
||||||
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
||||||
|
api_lock_wait_unlock_and_free(msg.lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
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_message_queue_put(usb.queue, &msg, FuriWaitForever);
|
||||||
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
||||||
|
api_lock_wait_unlock_and_free(msg.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get device / configuration descriptors */
|
/* Get device / configuration descriptors */
|
||||||
@@ -148,7 +264,7 @@ static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_
|
|||||||
|
|
||||||
switch(dtype) {
|
switch(dtype) {
|
||||||
case USB_DTYPE_DEVICE:
|
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) {
|
if(usb.callback != NULL) {
|
||||||
usb.callback(FuriHalUsbStateEventDescriptorRequest, usb.cb_ctx);
|
usb.callback(FuriHalUsbStateEventDescriptorRequest, usb.cb_ctx);
|
||||||
}
|
}
|
||||||
@@ -183,16 +299,11 @@ static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_
|
|||||||
return usbd_ack;
|
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) {
|
static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
||||||
UNUSED(dev);
|
UNUSED(dev);
|
||||||
UNUSED(event);
|
UNUSED(event);
|
||||||
UNUSED(ep);
|
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) {
|
if(usb.callback != NULL) {
|
||||||
usb.callback(FuriHalUsbStateEventReset, usb.cb_ctx);
|
usb.callback(FuriHalUsbStateEventReset, usb.cb_ctx);
|
||||||
}
|
}
|
||||||
@@ -228,34 +339,38 @@ static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t furi_hal_usb_thread(void* context) {
|
static void usb_process_mode_start(FuriHalUsbInterface* interface, void* context) {
|
||||||
UNUSED(context);
|
if(usb.if_cur != NULL) {
|
||||||
bool usb_request_pending = false;
|
usb.if_cur->deinit(&udev);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while(true) {
|
__disable_irq();
|
||||||
uint32_t flags = furi_thread_flags_wait(USB_SRV_ALL_EVENTS, FuriFlagWaitAny, 500);
|
usb.if_cur = interface;
|
||||||
if((flags & FuriFlagError) == 0) {
|
usb.if_ctx = context;
|
||||||
if(flags & EventModeChange) {
|
__enable_irq();
|
||||||
if(usb.if_next != usb.if_cur) {
|
|
||||||
if_new = usb.if_next;
|
if(interface != NULL) {
|
||||||
if_ctx_new = usb.if_ctx;
|
interface->init(&udev, interface, context);
|
||||||
if(usb.enabled) { // Disable current interface
|
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.if_cur) {
|
||||||
|
if(usb.enabled) {
|
||||||
|
// Disable current interface
|
||||||
susp_evt(&udev, 0, 0);
|
susp_evt(&udev, 0, 0);
|
||||||
usbd_connect(&udev, false);
|
usbd_connect(&udev, false);
|
||||||
usb.enabled = false;
|
usb.enabled = false;
|
||||||
furi_delay_ms(USB_RECONNECT_DELAY);
|
furi_delay_ms(USB_RECONNECT_DELAY);
|
||||||
}
|
}
|
||||||
flags |= EventModeChangeStart;
|
usb_process_mode_start(interface, context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(flags & EventReinit) {
|
|
||||||
|
static void usb_process_mode_reinit() {
|
||||||
// Temporary disable callback to avoid getting false reset events
|
// Temporary disable callback to avoid getting false reset events
|
||||||
usbd_reg_event(&udev, usbd_evt_reset, NULL);
|
usbd_reg_event(&udev, usbd_evt_reset, NULL);
|
||||||
FURI_LOG_I(TAG, "USB Reinit");
|
FURI_LOG_I(TAG, "USB Reinit");
|
||||||
@@ -266,52 +381,108 @@ static int32_t furi_hal_usb_thread(void* context) {
|
|||||||
usbd_enable(&udev, false);
|
usbd_enable(&udev, false);
|
||||||
usbd_enable(&udev, true);
|
usbd_enable(&udev, true);
|
||||||
|
|
||||||
if_new = usb.if_cur;
|
|
||||||
furi_delay_ms(USB_RECONNECT_DELAY);
|
furi_delay_ms(USB_RECONNECT_DELAY);
|
||||||
flags |= EventModeChangeStart;
|
usb_process_mode_start(usb.if_cur, usb.if_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool usb_process_set_config(FuriHalUsbInterface* interface, void* context) {
|
||||||
|
if(usb.mode_lock) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
usb_process_mode_change(interface, context);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
if(flags & EventModeChangeStart) { // Second stage of mode change process
|
}
|
||||||
if(usb.if_cur != NULL) {
|
|
||||||
usb.if_cur->deinit(&udev);
|
void usb_process_enable(bool enable) {
|
||||||
}
|
if(enable) {
|
||||||
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)) {
|
if((!usb.enabled) && (usb.if_cur != NULL)) {
|
||||||
usbd_connect(&udev, true);
|
usbd_connect(&udev, true);
|
||||||
usb.enabled = true;
|
usb.enabled = true;
|
||||||
FURI_LOG_I(TAG, "USB Enable");
|
FURI_LOG_I(TAG, "USB Enable");
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
if(flags & EventDisable) {
|
|
||||||
if(usb.enabled) {
|
if(usb.enabled) {
|
||||||
susp_evt(&udev, 0, 0);
|
susp_evt(&udev, 0, 0);
|
||||||
usbd_connect(&udev, false);
|
usbd_connect(&udev, false);
|
||||||
usb.enabled = false;
|
usb.enabled = false;
|
||||||
usb_request_pending = false;
|
usb.request_pending = false;
|
||||||
FURI_LOG_I(TAG, "USB Disable");
|
FURI_LOG_I(TAG, "USB Disable");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(flags & EventReset) {
|
}
|
||||||
|
|
||||||
|
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.if_cur;
|
||||||
|
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.cb_ctx = message->data.state_callback.context;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
api_lock_unlock(message->lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int32_t furi_hal_usb_thread(void* context) {
|
||||||
|
UNUSED(context);
|
||||||
|
uint8_t usb_wait_time = 0;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
{
|
||||||
|
UsbApiEventMessage message;
|
||||||
|
if(furi_message_queue_get(usb.queue, &message, 0) == FuriStatusOk) {
|
||||||
|
usb_process_message(&message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if((flags & FuriFlagError) == 0) {
|
||||||
|
if(flags & UsbEventReset) {
|
||||||
if(usb.enabled) {
|
if(usb.enabled) {
|
||||||
usb_request_pending = true;
|
usb.request_pending = true;
|
||||||
usb_wait_time = 0;
|
usb_wait_time = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(flags & EventRequest) {
|
if(flags & UsbEventRequest) {
|
||||||
usb_request_pending = false;
|
usb.request_pending = false;
|
||||||
}
|
}
|
||||||
} else if(usb_request_pending) {
|
} else if(usb.request_pending) {
|
||||||
usb_wait_time++;
|
usb_wait_time++;
|
||||||
if(usb_wait_time > 4) {
|
if(usb_wait_time > 4) {
|
||||||
furi_hal_usb_reinit();
|
usb_process_mode_reinit();
|
||||||
usb_request_pending = false;
|
usb.request_pending = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user