Skip to content

Commit

Permalink
Add support for FreeRTOS (#298)
Browse files Browse the repository at this point in the history
* Add support for FreeRTOS

Compiles for FreeRTOS when __FREERTOS__ is defined.

Note that _WIN32 and __FREERTOS__ can coexist!

* Add FreeRTOS sample

---------

Co-authored-by: Sergey Podobry <[email protected]>
  • Loading branch information
Yveaux and SergiusTheBest authored Jan 15, 2025
1 parent 96637a6 commit 94899e0
Show file tree
Hide file tree
Showing 6 changed files with 766 additions and 6 deletions.
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

0 comments on commit 94899e0

Please sign in to comment.