Skip to content

Commit

Permalink
Merge branch 'aku/usb_n_thread_fix' of https://github.com/flipperdevi…
Browse files Browse the repository at this point in the history
…ces/flipperzero-firmware into aku/usb_n_thread_fix
  • Loading branch information
DrZlo13 committed Jun 12, 2024
2 parents ecd83ac + 016be3f commit fba8371
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
38 changes: 19 additions & 19 deletions furi/core/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ struct FuriThreadStdout {

struct FuriThread {
StaticTask_t container;
TaskHandle_t task_handle;
StackType_t* stack_buffer;

FuriThreadState state;
Expand All @@ -54,6 +53,7 @@ struct FuriThread {
// this ensures that the size of this structure is minimal
bool is_service;
bool heap_trace_enabled;
volatile bool is_active;
};

// IMPORTANT: container MUST be the FIRST struct member
Expand Down Expand Up @@ -90,9 +90,8 @@ static void furi_thread_body(void* context) {
furi_check(thread->state == FuriThreadStateStarting);
furi_thread_set_state(thread, FuriThreadStateRunning);

TaskHandle_t task_handle = xTaskGetCurrentTaskHandle();
if(thread->heap_trace_enabled == true) {
memmgr_heap_enable_thread_trace((FuriThreadId)task_handle);
memmgr_heap_enable_thread_trace(thread);
}

thread->ret = thread->callback(thread->context);
Expand All @@ -101,14 +100,14 @@ static void furi_thread_body(void* context) {

if(thread->heap_trace_enabled == true) {
furi_delay_ms(33);
thread->heap_size = memmgr_heap_get_thread_memory((FuriThreadId)task_handle);
thread->heap_size = memmgr_heap_get_thread_memory(thread);
furi_log_print_format(
thread->heap_size ? FuriLogLevelError : FuriLogLevelInfo,
TAG,
"%s allocation balance: %zu",
thread->name ? thread->name : "Thread",
thread->heap_size);
memmgr_heap_disable_thread_trace((FuriThreadId)task_handle);
memmgr_heap_disable_thread_trace(thread);
}

furi_check(thread->state == FuriThreadStateRunning);
Expand Down Expand Up @@ -197,7 +196,7 @@ void furi_thread_free(FuriThread* thread) {
furi_check(thread->is_service == false);
// Cannot free a non-joined thread
furi_check(thread->state == FuriThreadStateStopped);
furi_check(thread->task_handle == NULL);
furi_check(!thread->is_active);

furi_thread_set_name(thread, NULL);
furi_thread_set_appid(thread, NULL);
Expand Down Expand Up @@ -313,25 +312,26 @@ void furi_thread_start(FuriThread* thread) {
uint32_t stack_depth = thread->stack_size / sizeof(StackType_t);
UBaseType_t priority = thread->priority ? thread->priority : FuriThreadPriorityNormal;

thread->task_handle = xTaskCreateStatic(
furi_thread_body,
thread->name,
stack_depth,
thread,
priority,
thread->stack_buffer,
&thread->container);
thread->is_active = true;

furi_check(thread->task_handle == (TaskHandle_t)&thread->container);
furi_check(
xTaskCreateStatic(
furi_thread_body,
thread->name,
stack_depth,
thread,
priority,
thread->stack_buffer,
&thread->container) == (TaskHandle_t)thread);
}

void furi_thread_cleanup_tcb_event(TaskHandle_t task) {
FuriThread* thread = pvTaskGetThreadLocalStoragePointer(task, 0);
if(thread) {
// clear thread local storage
vTaskSetThreadLocalStoragePointer(task, 0, NULL);
furi_check(thread->task_handle == task);
thread->task_handle = NULL;
furi_check(thread == (FuriThread*)task);
thread->is_active = false;
}
}

Expand All @@ -346,7 +346,7 @@ bool furi_thread_join(FuriThread* thread) {
//
// If your thread exited, but your app stuck here: some other thread uses
// all cpu time, which delays kernel from releasing task handle
while(thread->task_handle) {
while(thread->is_active) {
furi_delay_ms(10);
}

Expand All @@ -355,7 +355,7 @@ bool furi_thread_join(FuriThread* thread) {

FuriThreadId furi_thread_get_id(FuriThread* thread) {
furi_check(thread);
return thread->task_handle;
return thread;
}

void furi_thread_enable_heap_trace(FuriThread* thread) {
Expand Down
2 changes: 1 addition & 1 deletion targets/f7/furi_hal/furi_hal_interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ void USB_LP_IRQHandler(void) {
#endif
}

void USB_HP_IRQHandler(void) {
void USB_HP_IRQHandler(void) { //-V524
#ifndef FURI_RAM_EXEC
usbd_poll(&udev);
#endif
Expand Down
2 changes: 1 addition & 1 deletion targets/f7/furi_hal/furi_hal_usb_ccid.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static const struct CcidConfigDescriptor ccid_cfg_desc = {
.bConfigurationValue = 1,
.iConfiguration = NO_DESCRIPTOR,
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
.bMaxPower = USB_CFG_POWER_MA(100),
.bMaxPower = USB_CFG_POWER_MA(500),
},
.intf_0 =
{
Expand Down
4 changes: 2 additions & 2 deletions targets/f7/furi_hal/furi_hal_usb_cdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static const struct CdcConfigDescriptorSingle cdc_cfg_desc_single = {
.bConfigurationValue = 1,
.iConfiguration = NO_DESCRIPTOR,
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
.bMaxPower = USB_CFG_POWER_MA(100),
.bMaxPower = USB_CFG_POWER_MA(500),
},
.iad_0 =
{
Expand Down Expand Up @@ -188,7 +188,7 @@ static const struct CdcConfigDescriptorDual
.bConfigurationValue = 1,
.iConfiguration = NO_DESCRIPTOR,
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
.bMaxPower = USB_CFG_POWER_MA(100),
.bMaxPower = USB_CFG_POWER_MA(500),
},
.iad_0 =
{
Expand Down
2 changes: 1 addition & 1 deletion targets/f7/furi_hal/furi_hal_usb_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ static const struct HidConfigDescriptor hid_cfg_desc = {
.bConfigurationValue = 1,
.iConfiguration = NO_DESCRIPTOR,
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
.bMaxPower = USB_CFG_POWER_MA(100),
.bMaxPower = USB_CFG_POWER_MA(500),
},
.intf_0 =
{
Expand Down
2 changes: 1 addition & 1 deletion targets/f7/furi_hal/furi_hal_usb_u2f.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static const struct HidConfigDescriptor hid_u2f_cfg_desc = {
.bConfigurationValue = 1,
.iConfiguration = NO_DESCRIPTOR,
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
.bMaxPower = USB_CFG_POWER_MA(100),
.bMaxPower = USB_CFG_POWER_MA(500),
},
.iad_0 =
{
Expand Down

0 comments on commit fba8371

Please sign in to comment.