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

freeRTOS task stack diagnostics using freeRTOS function #2976

Merged
merged 3 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions radio/src/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ extern uint32_t SystemCoreClock;

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_uxTaskGetStackHighWaterMark 1

#if defined(THREADSAFE_MALLOC)
#define INCLUDE_xTaskGetSchedulerState 1
Expand Down
4 changes: 2 additions & 2 deletions radio/src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#define CLI_PRINT_BUFFER_SIZE 128

RTOS_TASK_HANDLE cliTaskId;
RTOS_DEFINE_STACK(cliStack, CLI_STACK_SIZE);
RTOS_DEFINE_STACK(cliTaskId, cliStack, CLI_STACK_SIZE);

static uint8_t cliRxBufferStorage[CLI_RX_BUFFER_SIZE];
static StaticStreamBuffer_t cliRxBufferStatic;
Expand Down Expand Up @@ -843,7 +843,7 @@ int cliTrace(const char ** argv)

int cliStackInfo(const char ** argv)
{
cliSerialPrint("[MAIN] %d available / %d bytes", stackAvailable()*4, stackSize()*4);
cliSerialPrint("[MAIN] %d available / %d bytes", mainStackAvailable()*4, stackSize()*4);
cliSerialPrint("[MENUS] %d available / %d bytes", menusStack.available()*4, menusStack.size());
cliSerialPrint("[MIXER] %d available / %d bytes", mixerStack.available()*4, mixerStack.size());
cliSerialPrint("[AUDIO] %d available / %d bytes", audioStack.available()*4, audioStack.size());
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/212x64/view_statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void menuStatisticsDebug(event_t event)
lcdDrawText(lcdLastRightPos+2, y+1, "[A]", SMLSIZE);
lcdDrawNumber(lcdLastRightPos, y, audioStack.available(), LEFT);
lcdDrawText(lcdLastRightPos+2, y+1, "[I]", SMLSIZE);
lcdDrawNumber(lcdLastRightPos, y, stackAvailable(), LEFT);
lcdDrawNumber(lcdLastRightPos, y, mainStackAvailable(), LEFT);
y += FH;

#if defined(DEBUG_LATENCY)
Expand Down
4 changes: 0 additions & 4 deletions radio/src/opentx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,10 +1867,6 @@ int main()

boardInit();

#if !defined(SIMU)
stackPaint();
#endif

#if defined(PCBHORUS)
if (!IS_FIRMWARE_COMPATIBLE_WITH_BOARD()) {
runFatalErrorScreen(STR_WRONG_PCBREV);
Expand Down
30 changes: 12 additions & 18 deletions radio/src/rtos.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ extern "C++" {
}

template<int SIZE>
class FakeTaskStack
class TaskStack
{
public:
FakeTaskStack()
TaskStack()
{
}

Expand All @@ -97,7 +97,7 @@ extern "C++" {
return SIZE / 2;
}
};
#define RTOS_DEFINE_STACK(name, size) FakeTaskStack<size> name
#define RTOS_DEFINE_STACK(taskHandle, name, size) TaskStack<size> name

#define TASK_FUNCTION(task) void* task(void *)

Expand All @@ -110,7 +110,7 @@ extern "C++" {
}

template<int SIZE>
inline void RTOS_CREATE_TASK(pthread_t &taskId, void * (*task)(void *), const char * name, FakeTaskStack<SIZE> &, unsigned size = 0, unsigned priority = 0)
inline void RTOS_CREATE_TASK(pthread_t &taskId, void * (*task)(void *), const char * name, TaskStack<SIZE> &, unsigned size = 0, unsigned priority = 0)
{
UNUSED(size);
UNUSED(priority);
Expand All @@ -119,7 +119,7 @@ inline void RTOS_CREATE_TASK(pthread_t &taskId, void * (*task)(void *), const ch

#define TASK_RETURN() return nullptr

constexpr uint32_t stackAvailable()
constexpr uint32_t mainStackAvailable()
{
return 500;
}
Expand Down Expand Up @@ -218,7 +218,6 @@ inline void RTOS_CREATE_TASK(pthread_t &taskId, void * (*task)(void *), const ch

#define RTOS_UNLOCK_MUTEX(handle) _RTOS_UNLOCK_MUTEX(&handle)

//TODO: replace with FreeRTOS functions
static inline uint32_t getStackAvailable(void * address, uint32_t size)
{
uint32_t * array = (uint32_t *)address;
Expand All @@ -236,7 +235,7 @@ inline void RTOS_CREATE_TASK(pthread_t &taskId, void * (*task)(void *), const ch
return ((unsigned char *)&_estack - (unsigned char *)&_main_stack_start) / 4;
}

static inline uint32_t stackAvailable()
static inline uint32_t mainStackAvailable()
{
return getStackAvailable(&_main_stack_start, stackSize());
}
Expand Down Expand Up @@ -272,15 +271,8 @@ inline void RTOS_CREATE_TASK(pthread_t &taskId, void * (*task)(void *), const ch
class TaskStack
{
public:
TaskStack()
{
}

void paint()
{
for (uint32_t i=0; i<SIZE; i++) {
stack[i] = 0x55555555;
}
TaskStack(RTOS_TASK_HANDLE *h) {
this->h = h;
}

uint32_t size()
Expand All @@ -290,10 +282,12 @@ inline void RTOS_CREATE_TASK(pthread_t &taskId, void * (*task)(void *), const ch

uint32_t available()
{
return getStackAvailable(stack, SIZE);
return uxTaskGetStackHighWaterMark(h->rtos_handle);
}

StackType_t stack[SIZE];
protected:
RTOS_TASK_HANDLE *h;
};
#endif // __cplusplus

Expand All @@ -308,7 +302,7 @@ inline void RTOS_CREATE_TASK(pthread_t &taskId, void * (*task)(void *), const ch
}

// stack must be aligned to 8 bytes otherwise printf for %f does not work!
#define RTOS_DEFINE_STACK(name, size) TaskStack<size> __ALIGNED(8) name __CCMRAM
#define RTOS_DEFINE_STACK(taskHandle, name, size) TaskStack<size> __ALIGNED(8) name __CCMRAM (&taskHandle)

#define TASK_FUNCTION(task) void task(void *)
#define TASK_RETURN() vTaskDelete(nullptr)
Expand Down
16 changes: 3 additions & 13 deletions radio/src/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,17 @@
#include "timers_driver.h"

RTOS_TASK_HANDLE menusTaskId;
RTOS_DEFINE_STACK(menusStack, MENUS_STACK_SIZE);
RTOS_DEFINE_STACK(menusTaskId, menusStack, MENUS_STACK_SIZE);

RTOS_TASK_HANDLE mixerTaskId;
RTOS_DEFINE_STACK(mixerStack, MIXER_STACK_SIZE);
RTOS_DEFINE_STACK(mixerTaskId, mixerStack, MIXER_STACK_SIZE);

RTOS_TASK_HANDLE audioTaskId;
RTOS_DEFINE_STACK(audioStack, AUDIO_STACK_SIZE);
RTOS_DEFINE_STACK(audioTaskId, audioStack, AUDIO_STACK_SIZE);

RTOS_MUTEX_HANDLE audioMutex;
RTOS_MUTEX_HANDLE mixerMutex;

void stackPaint()
{
menusStack.paint();
mixerStack.paint();
audioStack.paint();
#if defined(CLI) && !defined(SIMU)
cliStack.paint();
#endif
}

volatile uint16_t timeForcePowerOffPressed = 0;

bool isForcePowerOffRequested()
Expand Down
13 changes: 7 additions & 6 deletions radio/src/tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,23 @@
#define CLI_TASK_PRIO (1)
#endif

extern RTOS_TASK_HANDLE menusTaskId;
extern RTOS_DEFINE_STACK(menusStack, MENUS_STACK_SIZE);

extern RTOS_MUTEX_HANDLE mixerMutex;

extern RTOS_TASK_HANDLE menusTaskId;
extern TaskStack<MENUS_STACK_SIZE> menusStack;

extern RTOS_TASK_HANDLE mixerTaskId;
extern RTOS_DEFINE_STACK(mixerStack, MIXER_STACK_SIZE);
extern TaskStack<MIXER_STACK_SIZE> mixerStack;

extern RTOS_TASK_HANDLE audioTaskId;
extern RTOS_DEFINE_STACK(audioStack, AUDIO_STACK_SIZE);
extern TaskStack<AUDIO_STACK_SIZE> audioStack;

#if defined(CLI)
extern RTOS_TASK_HANDLE cliTaskId;
extern RTOS_DEFINE_STACK(cliStack, CLI_STACK_SIZE);
extern TaskStack<CLI_STACK_SIZE> cliStack;
#endif

void stackPaint();
void tasksStart();

extern volatile uint16_t timeForcePowerOffPressed;
Expand Down