Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VA Fixes: USB IRQ Handling and EP configuration, Thread handler shenanigans. #3705

Merged
merged 9 commits into from
Jun 12, 2024
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
6 changes: 6 additions & 0 deletions targets/f7/furi_hal/furi_hal_interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ void USB_LP_IRQHandler(void) {
#endif
}

void USB_HP_IRQHandler(void) { //-V524
#ifndef FURI_RAM_EXEC
usbd_poll(&udev);
#endif
}

void IPCC_C1_TX_IRQHandler(void) {
HW_IPCC_Tx_Handler();
}
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
12 changes: 6 additions & 6 deletions targets/f7/furi_hal/furi_hal_usb_cdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#include "usb.h"
#include "usb_cdc.h"

#define CDC0_RXD_EP 0x02
#define CDC0_RXD_EP 0x01
#define CDC0_TXD_EP 0x82
#define CDC0_NTF_EP 0x81
#define CDC0_NTF_EP 0x83

#define CDC1_RXD_EP 0x04
#define CDC1_TXD_EP 0x84
#define CDC1_NTF_EP 0x83
#define CDC1_TXD_EP 0x85
#define CDC1_NTF_EP 0x86

#define CDC_NTF_SZ 0x08

Expand Down 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
Loading