From cb1a9a62fbcd75072ee21302984f96f9d47923e5 Mon Sep 17 00:00:00 2001 From: SG Date: Sat, 12 Nov 2022 00:19:06 +1000 Subject: [PATCH 01/13] invalidate memmgt thread dict --- furi/core/memmgr_heap.c | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/furi/core/memmgr_heap.c b/furi/core/memmgr_heap.c index ac51b4a20..c72684419 100644 --- a/furi/core/memmgr_heap.c +++ b/furi/core/memmgr_heap.c @@ -191,8 +191,41 @@ size_t memmgr_heap_get_thread_memory(FuriThreadId thread_id) { #undef traceMALLOC static inline void traceMALLOC(void* pointer, size_t size) { FuriThreadId thread_id = furi_thread_get_current_id(); + volatile uint32_t ptr = 0; + volatile uint32_t sz = 0; + const uint32_t ram_start = 0x20000000; + const uint32_t ram_end = 0x20030000; + if(thread_id && memmgr_heap_thread_trace_depth == 0) { memmgr_heap_thread_trace_depth++; + + { + MemmgrHeapThreadDict_it_t thread_dict_it; + for(MemmgrHeapThreadDict_it(thread_dict_it, memmgr_heap_thread_dict); + !MemmgrHeapThreadDict_end_p(thread_dict_it); + MemmgrHeapThreadDict_next(thread_dict_it)) { + MemmgrHeapThreadDict_itref_t* data = MemmgrHeapThreadDict_ref(thread_dict_it); + if(data->key != 0) { + MemmgrHeapAllocDict_t* alloc_dict = &data->value; + MemmgrHeapAllocDict_it_t alloc_dict_it; + for(MemmgrHeapAllocDict_it(alloc_dict_it, *alloc_dict); + !MemmgrHeapAllocDict_end_p(alloc_dict_it); + MemmgrHeapAllocDict_next(alloc_dict_it)) { + MemmgrHeapAllocDict_itref_t* data = MemmgrHeapAllocDict_ref(alloc_dict_it); + + ptr = data->key; + sz = data->value; + + if(ptr < ram_start || ptr > ram_end) { + furi_crash("Invalid pointer"); + } else if(sz == 0 || sz > 0x10000) { + furi_crash("Invalid size"); + } + } + } + } + } + MemmgrHeapAllocDict_t* alloc_dict = MemmgrHeapThreadDict_get(memmgr_heap_thread_dict, (uint32_t)thread_id); if(alloc_dict) { @@ -200,6 +233,15 @@ static inline void traceMALLOC(void* pointer, size_t size) { } memmgr_heap_thread_trace_depth--; } + + ptr = (uint32_t)pointer; + sz = (uint32_t)size; + + if(ptr < ram_start || ptr > ram_end) { + furi_crash("^ Invalid pointer"); + } else if(sz == 0 || sz > 0x10000) { + furi_crash("^ Invalid size"); + } } #undef traceFREE From 861f47dd8dd35a1f5e5f11df4ffd3ae185764072 Mon Sep 17 00:00:00 2001 From: SG Date: Sat, 12 Nov 2022 08:13:39 +1000 Subject: [PATCH 02/13] Core: rollback memmgt thread dict invalidation --- furi/core/memmgr_heap.c | 42 ----------------------------------------- 1 file changed, 42 deletions(-) diff --git a/furi/core/memmgr_heap.c b/furi/core/memmgr_heap.c index c72684419..ac51b4a20 100644 --- a/furi/core/memmgr_heap.c +++ b/furi/core/memmgr_heap.c @@ -191,41 +191,8 @@ size_t memmgr_heap_get_thread_memory(FuriThreadId thread_id) { #undef traceMALLOC static inline void traceMALLOC(void* pointer, size_t size) { FuriThreadId thread_id = furi_thread_get_current_id(); - volatile uint32_t ptr = 0; - volatile uint32_t sz = 0; - const uint32_t ram_start = 0x20000000; - const uint32_t ram_end = 0x20030000; - if(thread_id && memmgr_heap_thread_trace_depth == 0) { memmgr_heap_thread_trace_depth++; - - { - MemmgrHeapThreadDict_it_t thread_dict_it; - for(MemmgrHeapThreadDict_it(thread_dict_it, memmgr_heap_thread_dict); - !MemmgrHeapThreadDict_end_p(thread_dict_it); - MemmgrHeapThreadDict_next(thread_dict_it)) { - MemmgrHeapThreadDict_itref_t* data = MemmgrHeapThreadDict_ref(thread_dict_it); - if(data->key != 0) { - MemmgrHeapAllocDict_t* alloc_dict = &data->value; - MemmgrHeapAllocDict_it_t alloc_dict_it; - for(MemmgrHeapAllocDict_it(alloc_dict_it, *alloc_dict); - !MemmgrHeapAllocDict_end_p(alloc_dict_it); - MemmgrHeapAllocDict_next(alloc_dict_it)) { - MemmgrHeapAllocDict_itref_t* data = MemmgrHeapAllocDict_ref(alloc_dict_it); - - ptr = data->key; - sz = data->value; - - if(ptr < ram_start || ptr > ram_end) { - furi_crash("Invalid pointer"); - } else if(sz == 0 || sz > 0x10000) { - furi_crash("Invalid size"); - } - } - } - } - } - MemmgrHeapAllocDict_t* alloc_dict = MemmgrHeapThreadDict_get(memmgr_heap_thread_dict, (uint32_t)thread_id); if(alloc_dict) { @@ -233,15 +200,6 @@ static inline void traceMALLOC(void* pointer, size_t size) { } memmgr_heap_thread_trace_depth--; } - - ptr = (uint32_t)pointer; - sz = (uint32_t)size; - - if(ptr < ram_start || ptr > ram_end) { - furi_crash("^ Invalid pointer"); - } else if(sz == 0 || sz > 0x10000) { - furi_crash("^ Invalid size"); - } } #undef traceFREE From 61adff99b71825c90ea7e83cb39473c315691910 Mon Sep 17 00:00:00 2001 From: SG Date: Sat, 12 Nov 2022 08:14:11 +1000 Subject: [PATCH 03/13] Dialogs: move api lock to toolbox --- applications/services/dialogs/dialogs.c | 4 ++-- applications/services/dialogs/dialogs_api.c | 10 +++++----- .../services/dialogs/dialogs_api_lock.h | 18 ------------------ .../services/dialogs/dialogs_message.h | 2 +- .../dialogs/dialogs_module_file_browser.c | 12 ++++++------ .../services/dialogs/dialogs_module_message.c | 12 ++++++------ lib/toolbox/api_lock.h | 19 +++++++++++++++++++ 7 files changed, 39 insertions(+), 38 deletions(-) delete mode 100644 applications/services/dialogs/dialogs_api_lock.h create mode 100644 lib/toolbox/api_lock.h diff --git a/applications/services/dialogs/dialogs.c b/applications/services/dialogs/dialogs.c index e2db4a0f8..3c6588889 100644 --- a/applications/services/dialogs/dialogs.c +++ b/applications/services/dialogs/dialogs.c @@ -1,6 +1,6 @@ #include "dialogs/dialogs_message.h" #include "dialogs_i.h" -#include "dialogs_api_lock.h" +#include #include "dialogs_module_file_browser.h" #include "dialogs_module_message.h" @@ -35,7 +35,7 @@ static void dialogs_app_process_message(DialogsApp* app, DialogsAppMessage* mess dialogs_app_process_module_message(&message->data->dialog); break; } - API_LOCK_UNLOCK(message->lock); + api_lock_unlock(message->lock); } int32_t dialogs_srv(void* p) { diff --git a/applications/services/dialogs/dialogs_api.c b/applications/services/dialogs/dialogs_api.c index ee959a33c..c1de59993 100644 --- a/applications/services/dialogs/dialogs_api.c +++ b/applications/services/dialogs/dialogs_api.c @@ -1,6 +1,6 @@ #include "dialogs/dialogs_message.h" #include "dialogs_i.h" -#include "dialogs_api_lock.h" +#include #include /****************** File browser ******************/ @@ -10,7 +10,7 @@ bool dialog_file_browser_show( FuriString* result_path, FuriString* path, const DialogsFileBrowserOptions* options) { - FuriApiLock lock = API_LOCK_INIT_LOCKED(); + FuriApiLock lock = api_lock_alloc_locked(); furi_check(lock != NULL); DialogsAppData data = { @@ -35,7 +35,7 @@ bool dialog_file_browser_show( furi_check( furi_message_queue_put(context->message_queue, &message, FuriWaitForever) == FuriStatusOk); - API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(lock); + api_lock_wait_unlock_and_free(lock); return return_data.bool_value; } @@ -43,7 +43,7 @@ bool dialog_file_browser_show( /****************** Message ******************/ DialogMessageButton dialog_message_show(DialogsApp* context, const DialogMessage* dialog_message) { - FuriApiLock lock = API_LOCK_INIT_LOCKED(); + FuriApiLock lock = api_lock_alloc_locked(); furi_check(lock != NULL); DialogsAppData data = { @@ -61,7 +61,7 @@ DialogMessageButton dialog_message_show(DialogsApp* context, const DialogMessage furi_check( furi_message_queue_put(context->message_queue, &message, FuriWaitForever) == FuriStatusOk); - API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(lock); + api_lock_wait_unlock_and_free(lock); return return_data.dialog_value; } diff --git a/applications/services/dialogs/dialogs_api_lock.h b/applications/services/dialogs/dialogs_api_lock.h deleted file mode 100644 index a165803f4..000000000 --- a/applications/services/dialogs/dialogs_api_lock.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -typedef FuriEventFlag* FuriApiLock; - -#define API_LOCK_EVENT (1U << 0) - -#define API_LOCK_INIT_LOCKED() furi_event_flag_alloc(); - -#define API_LOCK_WAIT_UNTIL_UNLOCK(_lock) \ - furi_event_flag_wait(_lock, API_LOCK_EVENT, FuriFlagWaitAny, FuriWaitForever); - -#define API_LOCK_FREE(_lock) furi_event_flag_free(_lock); - -#define API_LOCK_UNLOCK(_lock) furi_event_flag_set(_lock, API_LOCK_EVENT); - -#define API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(_lock) \ - API_LOCK_WAIT_UNTIL_UNLOCK(_lock); \ - API_LOCK_FREE(_lock); diff --git a/applications/services/dialogs/dialogs_message.h b/applications/services/dialogs/dialogs_message.h index 91e040ce4..d01c5aa9c 100644 --- a/applications/services/dialogs/dialogs_message.h +++ b/applications/services/dialogs/dialogs_message.h @@ -1,7 +1,7 @@ #pragma once #include #include "dialogs_i.h" -#include "dialogs_api_lock.h" +#include #ifdef __cplusplus extern "C" { diff --git a/applications/services/dialogs/dialogs_module_file_browser.c b/applications/services/dialogs/dialogs_module_file_browser.c index 166fd1113..7e67d6c60 100644 --- a/applications/services/dialogs/dialogs_module_file_browser.c +++ b/applications/services/dialogs/dialogs_module_file_browser.c @@ -1,5 +1,5 @@ #include "dialogs_i.h" -#include "dialogs_api_lock.h" +#include #include "gui/modules/file_browser.h" typedef struct { @@ -11,14 +11,14 @@ static void dialogs_app_file_browser_back_callback(void* context) { furi_assert(context); DialogsAppFileBrowserContext* file_browser_context = context; file_browser_context->result = false; - API_LOCK_UNLOCK(file_browser_context->lock); + api_lock_unlock(file_browser_context->lock); } static void dialogs_app_file_browser_callback(void* context) { furi_assert(context); DialogsAppFileBrowserContext* file_browser_context = context; file_browser_context->result = true; - API_LOCK_UNLOCK(file_browser_context->lock); + api_lock_unlock(file_browser_context->lock); } bool dialogs_app_process_module_file_browser(const DialogsAppMessageDataFileBrowser* data) { @@ -27,7 +27,7 @@ bool dialogs_app_process_module_file_browser(const DialogsAppMessageDataFileBrow DialogsAppFileBrowserContext* file_browser_context = malloc(sizeof(DialogsAppFileBrowserContext)); - file_browser_context->lock = API_LOCK_INIT_LOCKED(); + file_browser_context->lock = api_lock_alloc_locked(); ViewHolder* view_holder = view_holder_alloc(); view_holder_attach_to_gui(view_holder, gui); @@ -44,7 +44,7 @@ bool dialogs_app_process_module_file_browser(const DialogsAppMessageDataFileBrow view_holder_set_view(view_holder, file_browser_get_view(file_browser)); view_holder_start(view_holder); - API_LOCK_WAIT_UNTIL_UNLOCK(file_browser_context->lock); + api_lock_wait_unlock(file_browser_context->lock); ret = file_browser_context->result; @@ -52,7 +52,7 @@ bool dialogs_app_process_module_file_browser(const DialogsAppMessageDataFileBrow view_holder_free(view_holder); file_browser_stop(file_browser); file_browser_free(file_browser); - API_LOCK_FREE(file_browser_context->lock); + api_lock_free(file_browser_context->lock); free(file_browser_context); furi_record_close(RECORD_GUI); diff --git a/applications/services/dialogs/dialogs_module_message.c b/applications/services/dialogs/dialogs_module_message.c index 8d1c3ba75..879ba9baa 100644 --- a/applications/services/dialogs/dialogs_module_message.c +++ b/applications/services/dialogs/dialogs_module_message.c @@ -1,5 +1,5 @@ #include "dialogs_i.h" -#include "dialogs_api_lock.h" +#include #include typedef struct { @@ -30,7 +30,7 @@ static void dialogs_app_message_back_callback(void* context) { furi_assert(context); DialogsAppMessageContext* message_context = context; message_context->result = DialogMessageButtonBack; - API_LOCK_UNLOCK(message_context->lock); + api_lock_unlock(message_context->lock); } static void dialogs_app_message_callback(DialogExResult result, void* context) { @@ -49,7 +49,7 @@ static void dialogs_app_message_callback(DialogExResult result, void* context) { default: break; } - API_LOCK_UNLOCK(message_context->lock); + api_lock_unlock(message_context->lock); } DialogMessageButton dialogs_app_process_module_message(const DialogsAppMessageDataDialog* data) { @@ -57,7 +57,7 @@ DialogMessageButton dialogs_app_process_module_message(const DialogsAppMessageDa Gui* gui = furi_record_open(RECORD_GUI); const DialogMessage* message = data->message; DialogsAppMessageContext* message_context = malloc(sizeof(DialogsAppMessageContext)); - message_context->lock = API_LOCK_INIT_LOCKED(); + message_context->lock = api_lock_alloc_locked(); ViewHolder* view_holder = view_holder_alloc(); view_holder_attach_to_gui(view_holder, gui); @@ -87,14 +87,14 @@ DialogMessageButton dialogs_app_process_module_message(const DialogsAppMessageDa view_holder_set_view(view_holder, dialog_ex_get_view(dialog_ex)); view_holder_start(view_holder); - API_LOCK_WAIT_UNTIL_UNLOCK(message_context->lock); + api_lock_wait_unlock(message_context->lock); ret = message_context->result; view_holder_stop(view_holder); view_holder_free(view_holder); dialog_ex_free(dialog_ex); - API_LOCK_FREE(message_context->lock); + api_lock_free(message_context->lock); free(message_context); furi_record_close(RECORD_GUI); diff --git a/lib/toolbox/api_lock.h b/lib/toolbox/api_lock.h new file mode 100644 index 000000000..ae9ba3ab2 --- /dev/null +++ b/lib/toolbox/api_lock.h @@ -0,0 +1,19 @@ +#pragma once +#include + +typedef FuriEventFlag* FuriApiLock; + +#define API_LOCK_EVENT (1U << 0) + +#define api_lock_alloc_locked() furi_event_flag_alloc() + +#define api_lock_wait_unlock(_lock) \ + furi_event_flag_wait(_lock, API_LOCK_EVENT, FuriFlagWaitAny, FuriWaitForever) + +#define api_lock_free(_lock) furi_event_flag_free(_lock) + +#define api_lock_unlock(_lock) furi_event_flag_set(_lock, API_LOCK_EVENT) + +#define api_lock_wait_unlock_and_free(_lock) \ + api_lock_wait_unlock(_lock); \ + api_lock_free(_lock); From e6e073f1dc0fbdd37f6eba3f259c5ebf030c6e04 Mon Sep 17 00:00:00 2001 From: SG Date: Sat, 12 Nov 2022 08:14:36 +1000 Subject: [PATCH 04/13] HAL: blocking usb API --- .../plugins/dap_link/usb/dap_v2_usb.c | 11 - firmware/targets/f7/furi_hal/furi_hal.c | 3 +- firmware/targets/f7/furi_hal/furi_hal_usb.c | 395 +++++++++++++----- 3 files changed, 285 insertions(+), 124 deletions(-) diff --git a/applications/plugins/dap_link/usb/dap_v2_usb.c b/applications/plugins/dap_link/usb/dap_v2_usb.c index 0c303a3ba..2bf347345 100644 --- a/applications/plugins/dap_link/usb/dap_v2_usb.c +++ b/applications/plugins/dap_link/usb/dap_v2_usb.c @@ -627,14 +627,6 @@ static void hid_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) { 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) { 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_prod_descr); - - FURI_SW_MEMBARRIER(); - deinit_flag = true; } static void hid_on_wakeup(usbd_device* dev) { diff --git a/firmware/targets/f7/furi_hal/furi_hal.c b/firmware/targets/f7/furi_hal/furi_hal.c index 141efdb60..e42fdc6a4 100644 --- a/firmware/targets/f7/furi_hal/furi_hal.c +++ b/firmware/targets/f7/furi_hal/furi_hal.c @@ -63,6 +63,7 @@ void furi_hal_init() { // USB #ifndef FURI_RAM_EXEC + // TODO: call furi_hal_memory_init before furi_hal_usb_init, to get more memory furi_hal_usb_init(); FURI_LOG_I(TAG, "USB OK"); #endif @@ -79,7 +80,7 @@ void furi_hal_init() { furi_hal_rfid_init(); #endif 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(); // FatFS driver initialization diff --git a/firmware/targets/f7/furi_hal/furi_hal_usb.c b/firmware/targets/f7/furi_hal/furi_hal_usb.c index 86b10c79c..aafa4a2a2 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_usb.c +++ b/firmware/targets/f7/furi_hal/furi_hal_usb.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "usb.h" @@ -11,8 +12,48 @@ #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; @@ -21,25 +62,19 @@ typedef struct { void* if_ctx; FuriHalUsbStateCallback callback; void* cb_ctx; + + bool request_pending; } 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); @@ -80,62 +115,143 @@ void furi_hal_usb_init(void) { NVIC_EnableIRQ(USB_LP_IRQn); NVIC_EnableIRQ(USB_HP_IRQn); + usb.queue = furi_message_queue_alloc(1, sizeof(UsbApiEventMessage)); + usb.thread = furi_thread_alloc(); furi_thread_set_name(usb.thread, "UsbDriver"); furi_thread_set_stack_size(usb.thread, 1024); furi_thread_set_callback(usb.thread, furi_hal_usb_thread); + furi_thread_mark_as_service(usb.thread); furi_thread_start(usb.thread); FURI_LOG_I(TAG, "Init OK"); } bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) { - if(usb.mode_lock) { - return false; - } + UsbApiEventMessage msg = { + .lock = api_lock_alloc_locked(), + .type = UsbApiEventTypeSetConfig, + .data.interface = + { + .interface = new_if, + .context = ctx, + }, + .return_data = + { + .bool_value = false, + }, + }; - 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; + 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; } 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() { - FURI_LOG_I(TAG, "Mode lock"); - usb.mode_lock = true; + UsbApiEventMessage msg = { + .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() { - FURI_LOG_I(TAG, "Mode unlock"); - usb.mode_lock = false; + UsbApiEventMessage msg = { + .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() { - 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() { - 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_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() { - 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_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() { - 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_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 */ @@ -148,7 +264,7 @@ static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_ 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); } @@ -183,16 +299,11 @@ 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); } @@ -228,90 +339,150 @@ static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) { } } +static void usb_process_mode_start(FuriHalUsbInterface* interface, void* context) { + if(usb.if_cur != NULL) { + usb.if_cur->deinit(&udev); + } + + __disable_irq(); + usb.if_cur = interface; + usb.if_ctx = 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.if_cur) { + 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.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; + } +} + +void usb_process_enable(bool enable) { + if(enable) { + if((!usb.enabled) && (usb.if_cur != 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"); + } + } +} + +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); - 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); + + { + UsbApiEventMessage message; + if(furi_message_queue_get(usb.queue, &message, 0) == FuriStatusOk) { + usb_process_message(&message); + } + } + 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(flags & UsbEventReset) { 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"); - } - } - if(flags & EventReset) { - 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; } } } From 77d12f4a8e803ba235cc10c425418f9313e3af1b Mon Sep 17 00:00:00 2001 From: SG Date: Sat, 12 Nov 2022 09:09:22 +1000 Subject: [PATCH 05/13] HAL usb: fix api return data --- firmware/targets/f7/furi_hal/furi_hal_usb.c | 103 ++++++++++---------- 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/firmware/targets/f7/furi_hal/furi_hal_usb.c b/firmware/targets/f7/furi_hal/furi_hal_usb.c index aafa4a2a2..8f244e11d 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_usb.c +++ b/firmware/targets/f7/furi_hal/furi_hal_usb.c @@ -48,7 +48,7 @@ typedef struct { FuriApiLock lock; UsbApiEventType type; UsbApiEventData data; - UsbApiEventReturnData return_data; + UsbApiEventReturnData* return_data; } UsbApiEventMessage; typedef struct { @@ -57,12 +57,10 @@ typedef struct { bool enabled; bool connected; bool mode_lock; - FuriHalUsbInterface* if_cur; - FuriHalUsbInterface* if_next; - void* if_ctx; + FuriHalUsbInterface* interface; + void* interface_context; FuriHalUsbStateCallback callback; - void* cb_ctx; - + void* callback_context; bool request_pending; } UsbSrv; @@ -109,7 +107,7 @@ 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); @@ -128,6 +126,10 @@ void furi_hal_usb_init(void) { } 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, @@ -136,34 +138,32 @@ bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) { .interface = new_if, .context = ctx, }, - .return_data = - { - .bool_value = false, - }, + .return_data = &return_data, }; 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; + return return_data.bool_value; } FuriHalUsbInterface* furi_hal_usb_get_config() { + UsbApiEventReturnData return_data = { + .void_value = NULL, + }; + UsbApiEventMessage msg = { .lock = api_lock_alloc_locked(), .type = UsbApiEventTypeGetConfig, - .return_data = - { - .void_value = NULL, - }, + .return_data = &return_data, }; 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; + return return_data.void_value; } void furi_hal_usb_lock() { @@ -189,20 +189,21 @@ void furi_hal_usb_unlock() { } bool furi_hal_usb_is_locked() { + UsbApiEventReturnData return_data = { + .bool_value = false, + }; + UsbApiEventMessage msg = { .lock = api_lock_alloc_locked(), .type = UsbApiEventTypeIsLocked, - .return_data = - { - .bool_value = false, - }, + .return_data = &return_data, }; 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; + return return_data.bool_value; } void furi_hal_usb_disable() { @@ -260,29 +261,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), 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; @@ -305,7 +306,7 @@ static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) { UNUSED(ep); 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); } } @@ -313,14 +314,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); } } @@ -328,25 +329,25 @@ 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.if_cur != NULL) { - usb.if_cur->deinit(&udev); + if(usb.interface != NULL) { + usb.interface->deinit(&udev); } __disable_irq(); - usb.if_cur = interface; - usb.if_ctx = context; + usb.interface = interface; + usb.interface_context = context; __enable_irq(); if(interface != NULL) { @@ -358,7 +359,7 @@ static void usb_process_mode_start(FuriHalUsbInterface* interface, void* context } static void usb_process_mode_change(FuriHalUsbInterface* interface, void* context) { - if(interface != usb.if_cur) { + if(interface != usb.interface) { if(usb.enabled) { // Disable current interface susp_evt(&udev, 0, 0); @@ -382,7 +383,7 @@ static void usb_process_mode_reinit() { usbd_enable(&udev, true); furi_delay_ms(USB_RECONNECT_DELAY); - usb_process_mode_start(usb.if_cur, usb.if_ctx); + usb_process_mode_start(usb.interface, usb.interface_context); } bool usb_process_set_config(FuriHalUsbInterface* interface, void* context) { @@ -396,7 +397,7 @@ bool usb_process_set_config(FuriHalUsbInterface* interface, void* context) { void usb_process_enable(bool enable) { if(enable) { - if((!usb.enabled) && (usb.if_cur != NULL)) { + if((!usb.enabled) && (usb.interface != NULL)) { usbd_connect(&udev, true); usb.enabled = true; FURI_LOG_I(TAG, "USB Enable"); @@ -415,11 +416,11 @@ void usb_process_enable(bool enable) { void usb_process_message(UsbApiEventMessage* message) { switch(message->type) { case UsbApiEventTypeSetConfig: - message->return_data.bool_value = usb_process_set_config( + 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; + message->return_data->void_value = usb.interface; break; case UsbApiEventTypeLock: FURI_LOG_I(TAG, "Mode lock"); @@ -430,7 +431,7 @@ void usb_process_message(UsbApiEventMessage* message) { usb.mode_lock = false; break; case UsbApiEventTypeIsLocked: - message->return_data.bool_value = usb.mode_lock; + message->return_data->bool_value = usb.mode_lock; break; case UsbApiEventTypeDisable: usb_process_enable(false); @@ -443,7 +444,7 @@ void usb_process_message(UsbApiEventMessage* message) { break; case UsbApiEventTypeSetStateCallback: usb.callback = message->data.state_callback.callback; - usb.cb_ctx = message->data.state_callback.context; + usb.callback_context = message->data.state_callback.context; break; } From 370d2cbc905093679efe39bfca0aae9e8eaf8740 Mon Sep 17 00:00:00 2001 From: SG Date: Sat, 12 Nov 2022 09:16:05 +1000 Subject: [PATCH 06/13] HAL usb: api optimization --- firmware/targets/f7/furi_hal/furi_hal_usb.c | 53 ++++++++------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/firmware/targets/f7/furi_hal/furi_hal_usb.c b/firmware/targets/f7/furi_hal/furi_hal_usb.c index 8f244e11d..715eb9ffe 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_usb.c +++ b/firmware/targets/f7/furi_hal/furi_hal_usb.c @@ -57,11 +57,11 @@ typedef struct { bool enabled; bool connected; bool mode_lock; + bool request_pending; FuriHalUsbInterface* interface; void* interface_context; FuriHalUsbStateCallback callback; void* callback_context; - bool request_pending; } UsbSrv; typedef enum { @@ -125,6 +125,12 @@ void furi_hal_usb_init(void) { FURI_LOG_I(TAG, "Init OK"); } +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); +} + bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) { UsbApiEventReturnData return_data = { .bool_value = false, @@ -141,10 +147,7 @@ bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) { .return_data = &return_data, }; - 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); - + furi_hal_usb_send_message(&msg); return return_data.bool_value; } @@ -159,10 +162,7 @@ FuriHalUsbInterface* furi_hal_usb_get_config() { .return_data = &return_data, }; - 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); - + furi_hal_usb_send_message(&msg); return return_data.void_value; } @@ -172,9 +172,7 @@ void furi_hal_usb_lock() { .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); + furi_hal_usb_send_message(&msg); } void furi_hal_usb_unlock() { @@ -183,9 +181,7 @@ void furi_hal_usb_unlock() { .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); + furi_hal_usb_send_message(&msg); } bool furi_hal_usb_is_locked() { @@ -199,10 +195,7 @@ bool furi_hal_usb_is_locked() { .return_data = &return_data, }; - 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); - + furi_hal_usb_send_message(&msg); return return_data.bool_value; } @@ -212,9 +205,7 @@ void furi_hal_usb_disable() { .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); + furi_hal_usb_send_message(&msg); } void furi_hal_usb_enable() { @@ -223,9 +214,7 @@ void furi_hal_usb_enable() { .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); + furi_hal_usb_send_message(&msg); } void furi_hal_usb_reinit() { @@ -234,9 +223,7 @@ void furi_hal_usb_reinit() { .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); + furi_hal_usb_send_message(&msg); } void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* ctx) { @@ -250,9 +237,7 @@ void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* 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); + furi_hal_usb_send_message(&msg); } /* Get device / configuration descriptors */ @@ -386,7 +371,7 @@ static void usb_process_mode_reinit() { usb_process_mode_start(usb.interface, usb.interface_context); } -bool usb_process_set_config(FuriHalUsbInterface* interface, void* context) { +static bool usb_process_set_config(FuriHalUsbInterface* interface, void* context) { if(usb.mode_lock) { return false; } else { @@ -395,7 +380,7 @@ bool usb_process_set_config(FuriHalUsbInterface* interface, void* context) { } } -void usb_process_enable(bool enable) { +static void usb_process_enable(bool enable) { if(enable) { if((!usb.enabled) && (usb.interface != NULL)) { usbd_connect(&udev, true); @@ -413,7 +398,7 @@ void usb_process_enable(bool enable) { } } -void usb_process_message(UsbApiEventMessage* message) { +static void usb_process_message(UsbApiEventMessage* message) { switch(message->type) { case UsbApiEventTypeSetConfig: message->return_data->bool_value = usb_process_set_config( From 3eed788c36829fc58a09709418c10a89a64a1b17 Mon Sep 17 00:00:00 2001 From: SG Date: Sat, 12 Nov 2022 10:10:11 +1000 Subject: [PATCH 07/13] api lock: test results --- lib/toolbox/api_lock.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/toolbox/api_lock.h b/lib/toolbox/api_lock.h index ae9ba3ab2..900fafd09 100644 --- a/lib/toolbox/api_lock.h +++ b/lib/toolbox/api_lock.h @@ -1,6 +1,30 @@ #pragma once #include +/* +Testing 10000 api calls + +No lock + Time diff: 445269.218750 us + Time per call: 44.526920 us + +furi_thread_flags + Time diff: 430279.875000 us // lol wtf? smaller than no lock? + Time per call: 43.027988 us // I tested it many times, it's always smaller + +FuriEventFlag + Time diff: 831523.625000 us + Time per call: 83.152359 us + +FuriSemaphore + Time diff: 999807.125000 us + Time per call: 99.980713 us + +FuriMutex + Time diff: 1071417.500000 us + Time per call: 107.141747 us +*/ + typedef FuriEventFlag* FuriApiLock; #define API_LOCK_EVENT (1U << 0) @@ -16,4 +40,4 @@ typedef FuriEventFlag* FuriApiLock; #define api_lock_wait_unlock_and_free(_lock) \ api_lock_wait_unlock(_lock); \ - api_lock_free(_lock); + api_lock_free(_lock); \ No newline at end of file From 2f82351b3eb5f8313471ac488532a053b8a5f9de Mon Sep 17 00:00:00 2001 From: SG Date: Sun, 13 Nov 2022 01:42:46 +1000 Subject: [PATCH 08/13] Fix build errors --- lib/toolbox/api_lock.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/toolbox/api_lock.h b/lib/toolbox/api_lock.h index 900fafd09..5902a4922 100644 --- a/lib/toolbox/api_lock.h +++ b/lib/toolbox/api_lock.h @@ -40,4 +40,4 @@ typedef FuriEventFlag* FuriApiLock; #define api_lock_wait_unlock_and_free(_lock) \ api_lock_wait_unlock(_lock); \ - api_lock_free(_lock); \ No newline at end of file + api_lock_free(_lock); From e3a878477c124265a2d2fe9b4443a29091e6c862 Mon Sep 17 00:00:00 2001 From: SG Date: Mon, 14 Nov 2022 11:41:25 +1000 Subject: [PATCH 09/13] DAP Link: fix imports --- applications/plugins/dap_link/dap_link.c | 1 - applications/plugins/dap_link/usb/dap_v2_usb.h | 2 -- 2 files changed, 3 deletions(-) diff --git a/applications/plugins/dap_link/dap_link.c b/applications/plugins/dap_link/dap_link.c index 443d77c5e..7aefe042a 100644 --- a/applications/plugins/dap_link/dap_link.c +++ b/applications/plugins/dap_link/dap_link.c @@ -247,7 +247,6 @@ static int32_t dap_process(void* p) { // deinit usb furi_hal_usb_set_config(usb_config_prev, NULL); - dap_common_wait_for_deinit(); dap_common_usb_free_name(); dap_deinit_gpio(swd_pins_prev); return 0; diff --git a/applications/plugins/dap_link/usb/dap_v2_usb.h b/applications/plugins/dap_link/usb/dap_v2_usb.h index 2a0e86056..3f1534ffd 100644 --- a/applications/plugins/dap_link/usb/dap_v2_usb.h +++ b/applications/plugins/dap_link/usb/dap_v2_usb.h @@ -51,5 +51,3 @@ void dap_common_usb_set_state_callback(DapStateCallback callback); void dap_common_usb_alloc_name(const char* name); void dap_common_usb_free_name(); - -void dap_common_wait_for_deinit(); \ No newline at end of file From 853d840ec92883df414bfb444ca678487a830cf9 Mon Sep 17 00:00:00 2001 From: SG Date: Tue, 15 Nov 2022 23:27:30 +1000 Subject: [PATCH 10/13] Crash when malloc in ISR --- furi/core/memmgr_heap.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/furi/core/memmgr_heap.c b/furi/core/memmgr_heap.c index ac51b4a20..95afd3087 100644 --- a/furi/core/memmgr_heap.c +++ b/furi/core/memmgr_heap.c @@ -340,6 +340,10 @@ void* pvPortMalloc(size_t xWantedSize) { void* pvReturn = NULL; size_t to_wipe = xWantedSize; + if(FURI_IS_ISR()) { + furi_crash("malloc in ISR"); + } + #ifdef HEAP_PRINT_DEBUG BlockLink_t* print_heap_block = NULL; #endif From 2198a38a2202d6cc10088e77d7819f20187344ec Mon Sep 17 00:00:00 2001 From: SG Date: Tue, 15 Nov 2022 23:28:19 +1000 Subject: [PATCH 11/13] Fix dap-link copypaste error --- applications/plugins/dap_link/usb/dap_v2_usb.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/applications/plugins/dap_link/usb/dap_v2_usb.c b/applications/plugins/dap_link/usb/dap_v2_usb.c index 2bf347345..b42df2836 100644 --- a/applications/plugins/dap_link/usb/dap_v2_usb.c +++ b/applications/plugins/dap_link/usb/dap_v2_usb.c @@ -618,9 +618,6 @@ static void hid_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) { if(dap_state.semaphore_v2 == NULL) dap_state.semaphore_v2 = furi_semaphore_alloc(1, 1); if(dap_state.semaphore_cdc == NULL) dap_state.semaphore_cdc = furi_semaphore_alloc(1, 1); - usb_hid.dev_descr->idVendor = DAP_HID_VID; - usb_hid.dev_descr->idProduct = DAP_HID_PID; - usbd_reg_config(dev, hid_ep_config); usbd_reg_control(dev, hid_control); @@ -639,9 +636,6 @@ static void hid_deinit(usbd_device* dev) { usbd_reg_config(dev, NULL); usbd_reg_control(dev, NULL); - - free(usb_hid.str_manuf_descr); - free(usb_hid.str_prod_descr); } static void hid_on_wakeup(usbd_device* dev) { From c832ad6d1f2460d2f526b36132f3b621b80eb2ee Mon Sep 17 00:00:00 2001 From: SG Date: Tue, 15 Nov 2022 23:58:36 +1000 Subject: [PATCH 12/13] Moar memory management crashes. --- firmware/targets/f7/furi_hal/furi_hal_memory.c | 4 ++++ furi/core/memmgr_heap.c | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/firmware/targets/f7/furi_hal/furi_hal_memory.c b/firmware/targets/f7/furi_hal/furi_hal_memory.c index 9cf2c3120..016108a00 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_memory.c +++ b/firmware/targets/f7/furi_hal/furi_hal_memory.c @@ -81,6 +81,10 @@ void furi_hal_memory_init() { } void* furi_hal_memory_alloc(size_t size) { + if(FURI_IS_ISR()) { + furi_crash("memmgt in ISR"); + } + if(furi_hal_memory == NULL) { return NULL; } diff --git a/furi/core/memmgr_heap.c b/furi/core/memmgr_heap.c index 95afd3087..5ccabaefb 100644 --- a/furi/core/memmgr_heap.c +++ b/furi/core/memmgr_heap.c @@ -341,7 +341,7 @@ void* pvPortMalloc(size_t xWantedSize) { size_t to_wipe = xWantedSize; if(FURI_IS_ISR()) { - furi_crash("malloc in ISR"); + furi_crash("memmgt in ISR"); } #ifdef HEAP_PRINT_DEBUG @@ -490,6 +490,10 @@ void vPortFree(void* pv) { uint8_t* puc = (uint8_t*)pv; BlockLink_t* pxLink; + if(FURI_IS_ISR()) { + furi_crash("memmgt in ISR"); + } + if(pv != NULL) { /* The memory being freed will have an BlockLink_t structure immediately before it. */ From 0d60b456af446dd597169b5fe1abb953a9becefc Mon Sep 17 00:00:00 2001 From: SG Date: Wed, 16 Nov 2022 10:47:43 +1000 Subject: [PATCH 13/13] Crash when malloc in IRQ, not ISR --- firmware/targets/f7/furi_hal/furi_hal_memory.c | 2 +- furi/core/memmgr_heap.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/firmware/targets/f7/furi_hal/furi_hal_memory.c b/firmware/targets/f7/furi_hal/furi_hal_memory.c index 016108a00..ec71e6660 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_memory.c +++ b/firmware/targets/f7/furi_hal/furi_hal_memory.c @@ -81,7 +81,7 @@ void furi_hal_memory_init() { } void* furi_hal_memory_alloc(size_t size) { - if(FURI_IS_ISR()) { + if(FURI_IS_IRQ_MODE()) { furi_crash("memmgt in ISR"); } diff --git a/furi/core/memmgr_heap.c b/furi/core/memmgr_heap.c index 5ccabaefb..01153fe57 100644 --- a/furi/core/memmgr_heap.c +++ b/furi/core/memmgr_heap.c @@ -340,7 +340,7 @@ void* pvPortMalloc(size_t xWantedSize) { void* pvReturn = NULL; size_t to_wipe = xWantedSize; - if(FURI_IS_ISR()) { + if(FURI_IS_IRQ_MODE()) { furi_crash("memmgt in ISR"); } @@ -490,7 +490,7 @@ void vPortFree(void* pv) { uint8_t* puc = (uint8_t*)pv; BlockLink_t* pxLink; - if(FURI_IS_ISR()) { + if(FURI_IS_IRQ_MODE()) { furi_crash("memmgt in ISR"); }