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

Add support for FreeRTOS #298

Merged
merged 2 commits into from
Jan 15, 2025
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
31 changes: 25 additions & 6 deletions include/plog/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
# endif
#endif

#ifdef __FREERTOS__ // There is no standard way to know if the code is compiled for FreeRTOS. We expect __FREERTOS__ macro to be defined in such case.
# include <FreeRTOS.h>
# include <semphr.h>
# include <task.h>
#endif

#if PLOG_CHAR_IS_UTF8
# define PLOG_NSTR(x) x
#else
Expand Down Expand Up @@ -170,7 +176,9 @@ namespace plog

inline unsigned int gettid()
{
#ifdef _WIN32
#if defined(__FREERTOS__) && defined(INCLUDE_xTaskGetCurrentTaskHandle)
return static_cast<unsigned int>(reinterpret_cast<uintptr_t>(xTaskGetCurrentTaskHandle()));
#elif defined(_WIN32)
return GetCurrentThreadId();
#elif defined(__linux__)
return static_cast<unsigned int>(::syscall(__NR_gettid));
Expand Down Expand Up @@ -501,7 +509,10 @@ namespace plog
public:
Mutex()
{
#ifdef _WIN32
#ifdef __FREERTOS__
m_sync = xSemaphoreCreateBinary();
xSemaphoreGive(m_sync);
#elif defined(_WIN32)
InitializeCriticalSection(&m_sync);
#elif defined(__rtems__)
rtems_semaphore_create(0, 1,
Expand All @@ -515,7 +526,9 @@ namespace plog

~Mutex()
{
#ifdef _WIN32
#ifdef __FREERTOS__
vSemaphoreDelete(m_sync);
#elif defined(_WIN32)
DeleteCriticalSection(&m_sync);
#elif defined(__rtems__)
rtems_semaphore_delete(m_sync);
Expand All @@ -529,7 +542,9 @@ namespace plog
private:
void lock()
{
#ifdef _WIN32
#ifdef __FREERTOS__
xSemaphoreTake(m_sync, portMAX_DELAY);
#elif defined(_WIN32)
EnterCriticalSection(&m_sync);
#elif defined(__rtems__)
rtems_semaphore_obtain(m_sync, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
Expand All @@ -540,7 +555,9 @@ namespace plog

void unlock()
{
#ifdef _WIN32
#ifdef __FREERTOS__
xSemaphoreGive(m_sync);
#elif defined(_WIN32)
LeaveCriticalSection(&m_sync);
#elif defined(__rtems__)
rtems_semaphore_release(m_sync);
Expand All @@ -550,7 +567,9 @@ namespace plog
}

private:
#ifdef _WIN32
#ifdef __FREERTOS__
SemaphoreHandle_t m_sync;
#elif defined(_WIN32)
CRITICAL_SECTION m_sync;
#elif defined(__rtems__)
rtems_id m_sync;
Expand Down
1 change: 1 addition & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ add_subdirectory(DisableLogging)
add_subdirectory(DynamicAppender)
add_subdirectory(EventLog)
add_subdirectory(Facilities)
add_subdirectory(FreeRTOS)
add_subdirectory(Hello)
add_subdirectory(HexDump)
add_subdirectory(Library)
Expand Down
29 changes: 29 additions & 0 deletions samples/FreeRTOS/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
include(FetchContent)

# Select FreeRTOS port
set(FREERTOS_PORT "GCC_POSIX")

# Select FreeRTOS heap
set(FREERTOS_HEAP "4")

# Add the freertos_config for FreeRTOS-Kernel
add_subdirectory(freertos_config)

# Download FreeRTOS-Kernel sources
FetchContent_Declare(
freertos_kernel
URL https://github.com/FreeRTOS/FreeRTOS-Kernel/archive/refs/tags/V11.1.0.tar.gz
URL_HASH SHA256=0e21928b3bcc4f9bcaf7333fb1c8c0299d97e2ec9e13e3faa2c5a7ac8a3bc573
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)

FetchContent_MakeAvailable(freertos_kernel)

# Describe our executable
add_executable(FreeRTOS main.cpp)
target_link_libraries(FreeRTOS freertos_kernel freertos_config plog::plog)

# Important!!! There is no standard way to know if the code is compiled for FreeRTOS. So we define __FREERTOS__ macro.
target_compile_definitions(FreeRTOS PUBLIC __FREERTOS__)
endif()
2 changes: 2 additions & 0 deletions samples/FreeRTOS/freertos_config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_library(freertos_config INTERFACE)
target_include_directories(freertos_config INTERFACE .)
Loading