Skip to content

Commit

Permalink
Rework on the task worker component (#31)
Browse files Browse the repository at this point in the history
* Rework on task worker module

* rework on dependeny between FreeRTOS and task worker
  • Loading branch information
QuangHaiNguyen authored Aug 4, 2024
1 parent 22e6929 commit 1e2e945
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 78 deletions.
4 changes: 1 addition & 3 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@
"rhs": "Linux"
},
"cacheVariables": {
"TARGET_PATH": "targets/linux_freertos",
"FREERTOS_HEAP": "4",
"FREERTOS_PORT": "GCC_POSIX"
"TARGET_PATH": "targets/linux_freertos"
}
},
{
Expand Down
1 change: 0 additions & 1 deletion easy_embedded/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ target_include_directories(easy_embedded_lib
# Please add interface folders here
)


# Link libraries -------------------------------------------------------------
target_link_libraries(easy_embedded_lib
PUBLIC
Expand Down
6 changes: 2 additions & 4 deletions easy_embedded/service/task_worker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ target_include_directories(ez_task_worker_lib
# Link libraries -------------------------------------------------------------
target_link_libraries(ez_task_worker_lib
PUBLIC
# Please add public libraries
$<$<BOOL:${ENABLE_FREERTOS}>:freertos_kernel>
$<$<BOOL:${ENABLE_THREADX}>:threadx>
PRIVATE
ez_utilities_lib
$<$<BOOL:${ENABLE_THREADX}>:threadx>
$<$<BOOL:${ENABLE_FREERTOS}>:freertos_kernel>
INTERFACE
# Please add interface libraries
)

# End of file
119 changes: 67 additions & 52 deletions easy_embedded/service/task_worker/ez_task_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,66 +51,79 @@ extern "C" {
#define EZ_THREAD_WAIT_FOREVER 0xFFFFFFFF /* Thread waits for event, semaphore forever */
#define EZ_EVENT_TASK_AVAIL 0x01 /* Task avaialble event */


#if (EZ_THREADX_PORT_ENABLE == 1)
#define INIT_THREAD_FUNCTIONS(worker_name) \
static void worker_name##_thread(ULONG thread_input);\
static void worker_name##_thread_body(void);\

#define GET_THREAD_FUNC(worker_name) \
worker_name##_thread\
#ifndef EZ_RTOS_USE_STATIC_ALLOC
#define EZ_RTOS_USE_STATIC_ALLOC 1
#endif

#define INIT_THREAD_FUNCTIONS(worker_name) \
static void worker_name##_thread(ULONG thread_input);\
static void worker_name##_thread_body(void);\

#define THREAD_FUNC(worker_name) \
static void worker_name##_thread(ULONG thread_input)\
{\
while(1)\
#define GET_THREAD_FUNC(worker_name) \
worker_name##_thread\

#define THREAD_FUNC(worker_name) \
static void worker_name##_thread(ULONG thread_input)\
{\
worker_name##_thread_body();\
if(worker_name.sleep_ticks > 0)\
{ tx_thread_sleep(worker_name.sleep_ticks); }\
while(1)\
{\
worker_name##_thread_body();\
if(worker_name.sleep_ticks > 0)\
{ tx_thread_sleep(worker_name.sleep_ticks); }\
}\
}\
}\
static void worker_name##_thread_body(void)

#define INIT_WORKER(name, worker_sleep_ticks, worker_priority, worker_stack_size) \
struct ezTaskWorker name =\
{\
.worker_name = #name,\
.sleep_ticks = worker_sleep_ticks,\
.priority = worker_priority,\
.stack_size = worker_stack_size,\
}
static void worker_name##_thread_body(void)

#define INIT_WORKER(name, worker_sleep_ticks, worker_priority, worker_stack_size) \
struct ezTaskWorker name =\
{\
.worker_name = #name,\
.sleep_ticks = worker_sleep_ticks,\
.priority = worker_priority,\
.stack_size = worker_stack_size,\
}
#elif (EZ_FREERTOS_PORT_ENABLE == 1)

#ifndef EZ_RTOS_USE_STATIC_ALLOC
#define EZ_RTOS_USE_STATIC_ALLOC configSUPPORT_STATIC_ALLOCATION
#endif

#define INIT_THREAD_FUNCTIONS(worker_name) \
static void worker_name##_thread(void * parameters);\
static void worker_name##_thread_body(void);\
static void worker_name##_thread(void * parameters);\
static void worker_name##_thread_body(void);\

#define GET_THREAD_FUNC(worker_name) \
worker_name##_thread\
#define GET_THREAD_FUNC(worker_name) \
worker_name##_thread\

#define THREAD_FUNC(worker_name) \
static void worker_name##_thread(void * parameters)\
{\
while(1)\
#define THREAD_FUNC(worker_name) \
static void worker_name##_thread(void * parameters)\
{\
worker_name##_thread_body();\
if(worker_name.sleep_ticks > 0)\
{ vTaskDelay(worker_name.sleep_ticks); }\
while(1)\
{\
worker_name##_thread_body();\
if(worker_name.sleep_ticks > 0)\
{ vTaskDelay(worker_name.sleep_ticks); }\
}\
}\
}\
static void worker_name##_thread_body(void)

#define INIT_WORKER(name, worker_sleep_ticks, worker_priority, worker_stack_size) \
struct ezTaskWorker name =\
{\
.worker_name = #name,\
.sleep_ticks = worker_sleep_ticks,\
.priority = worker_priority,\
.stack_size = worker_stack_size,\
.semaphore_h = NULL,\
.events_h = NULL,\
}
static void worker_name##_thread_body(void)

#define INIT_WORKER(name, worker_sleep_ticks, worker_priority, worker_stack_size) \
struct ezTaskWorker name =\
{\
.worker_name = #name,\
.sleep_ticks = worker_sleep_ticks,\
.priority = worker_priority,\
.stack_size = worker_stack_size,\
.semaphore_h = NULL,\
.events_h = NULL,\
}
#else
#ifndef EZ_RTOS_USE_STATIC_ALLOC
#define EZ_RTOS_USE_STATIC_ALLOC 1
#endif
#define INIT_THREAD_FUNCTIONS(worker_name)
#define GET_THREAD_FUNC(worker_name)
#define THREAD_FUNC(worker_name)
Expand Down Expand Up @@ -141,11 +154,13 @@ struct ezTaskWorker
uint8_t priority; /**< Priority of the worker thread, the value must match the number of the activated RTOS */
uint32_t stack_size; /**< Stask size of the worker thread, in bytes */
uint32_t sleep_ticks; /**< Number of tick the thread must sleep before being activated again */
StaticTask_t thread;
StaticSemaphore_t sem;
SemaphoreHandle_t semaphore_h;
StaticEventGroup_t events;
EventGroupHandle_t events_h;
SemaphoreHandle_t semaphore_h; /**< FreeRTOS Semaphore handle */
EventGroupHandle_t events_h; /**< FreeRTOS Events handle*/
#if (EZ_RTOS_USE_STATIC_ALLOC == 1)
StaticTask_t thread; /**< Pointer to the memory hold the static thread block */
StaticSemaphore_t sem; /**< Pointer to the memory hold the static semaphore block */
StaticEventGroup_t events; /**< Pointer to the memory hold the static events block */
#endif /* (EZ_RTOS_USE_STATIC_ALLOC == 1) */
#else
struct Node node; /**< Linked list node */
#endif /* EZ_THREADX_PORT_ENABLE == 1 */
Expand Down
27 changes: 25 additions & 2 deletions easy_embedded/service/task_worker/freertos_port/ez_freertos_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ static EZ_RTOS_STATUS ezFreeRTOSPort_CreateThread(struct ezTaskWorker *worker,
void *thread_func)
{
EZ_RTOS_STATUS ret_status = RTOS_STATUS_ERR_ARG;
#if (EZ_RTOS_USE_STATIC_ALLOC == 1)
TaskHandle_t xHandle = NULL;
#else
BaseType_t xReturned = pdFAIL;
#endif

EZTRACE("ezFreeRTOSPort_CreateThread()");
if((worker != NULL) && (thread_func != NULL))
Expand All @@ -145,6 +149,7 @@ static EZ_RTOS_STATUS ezFreeRTOSPort_CreateThread(struct ezTaskWorker *worker,
worker->priority = configMAX_PRIORITIES - 1U;
}

#if (EZ_RTOS_USE_STATIC_ALLOC == 1)
xHandle = xTaskCreateStatic(thread_func,
worker->worker_name,
worker->stack_size,
Expand All @@ -153,9 +158,20 @@ static EZ_RTOS_STATUS ezFreeRTOSPort_CreateThread(struct ezTaskWorker *worker,
&TaskStack[used_stack_size],
&worker->thread);
used_stack_size += worker->stack_size;
#else
xReturned = xTaskCreate(thread_func,
worker->worker_name,
worker->stack_size,
NULL,
worker->priority,
NULL);
#endif /* (EZ_RTOS_USE_STATIC_ALLOC == 1) */
}

#if (EZ_RTOS_USE_STATIC_ALLOC == 1)
if(xHandle == NULL)
#else
if(xReturned == pdFAIL)
#endif /* (EZ_RTOS_USE_STATIC_ALLOC == 1) */
{
ret_status = RTOS_STATUS_ERR;
EZERROR("Create thread failed");
Expand Down Expand Up @@ -200,8 +216,11 @@ static EZ_RTOS_STATUS ezFreeRTOSPort_CreateSemaphore(struct ezTaskWorker *worker
EZTRACE("ezFreeRTOSPort_CreateSemaphore()");
if(worker != NULL)
{
#if (EZ_RTOS_USE_STATIC_ALLOC == 1)
worker->semaphore_h = xSemaphoreCreateMutexStatic(&worker->sem);

#else
worker->semaphore_h = xSemaphoreCreateMutex();
#endif /* (EZ_RTOS_USE_STATIC_ALLOC == 1) */
if(worker->semaphore_h == NULL)
{
EZERROR("Create semaphore failed");
Expand Down Expand Up @@ -359,7 +378,11 @@ static EZ_RTOS_STATUS ezFreeRTOSPort_CreateEvent(struct ezTaskWorker *worker)
EZTRACE("ezFreeRTOSPort_CreateEvent()");
if(worker != NULL)
{
#if (EZ_RTOS_USE_STATIC_ALLOC == 1)
worker->events_h = xEventGroupCreateStatic(&worker->events);
#else
worker->events_h = xEventGroupCreate();
#endif /* (EZ_RTOS_USE_STATIC_ALLOC == 1) */

if(worker->events_h == NULL)
{
Expand Down
14 changes: 1 addition & 13 deletions extern/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,6 @@ if(ENABLE_FREERTOS)
message(STATUS "freertos completed")
endif()

add_library(freertos_config INTERFACE)

target_include_directories(freertos_config SYSTEM
INTERFACE
${CMAKE_SOURCE_DIR}/targets/linux_freertos/
)

target_compile_definitions(freertos_config
INTERFACE
projCOVERAGE_TEST=0
)

add_subdirectory(freertos)
add_subdirectory(${CMAKE_SOURCE_DIR}/extern/freertos)
endif()

14 changes: 13 additions & 1 deletion targets/linux_freertos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,26 @@ target_include_directories(ez_target
# Please add interface folders here
)

# Config RTOS -----------------------------------------------------------------
set(FREERTOS_HEAP "4" CACHE INTERNAL "")
set(FREERTOS_PORT "GCC_POSIX" CACHE INTERNAL "")

add_library(freertos_config INTERFACE)
target_include_directories(freertos_config SYSTEM
INTERFACE
${CMAKE_SOURCE_DIR}/targets/linux_freertos/
)
target_compile_definitions(freertos_config
INTERFACE
projCOVERAGE_TEST=0
)

# Link libraries -------------------------------------------------------------
target_link_libraries(ez_target
PUBLIC
# Please add public libraries
PRIVATE
easy_embedded_lib
freertos_kernel
INTERFACE
# Please add interface libraries
)
Expand Down
3 changes: 1 addition & 2 deletions targets/linux_freertos/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@
#include "ez_logging.h"
#include "ez_easy_embedded.h"
#include "ez_task_worker.h"
#include <FreeRTOS.h>
#include <task.h>
#include "ez_freertos_port.h"


/******************************************************************************
* Module Preprocessor Macros
*******************************************************************************/
Expand Down

0 comments on commit 1e2e945

Please sign in to comment.