If anyone is interested, this is the contents of my “main_gtest.cpp” which is executed by the Google Test (one can use gdb to debug the executable in Linux):
#include "FreeRTOSConfig.h"
#include "FreeRTOS.h"
extern "C"
{
#include "task.h"
}
#include "gtest/gtest.h"
/**
* Handle to the FreeRTOS scheduler thread id.
*/
static pthread_t m_freertos_thread_id;
static int m_result = 0;
static bool m_test_is_done = false;
/**
* FreeRTOS task which runs all my unit tests.
*/
static void gtest_task(void *param)
{
(void)param;
// Run Google Test from here!
m_result = RUN_ALL_TESTS();
m_test_is_done = true;
vTaskSuspend(nullptr);
}
/**
* Creates the unit test FreeRTOS task.
*/
static void start_free_rtos()
{
BaseType_t rtos_res = xTaskCreate(gtest_task,
"gtest_task",
TASK_STACK_SIZE_DEFAULT * 10U,
nullptr,
TASK_PRIO_DEFAULT,
nullptr);
if (rtos_res != pdPASS)
{
abort();
}
}
/**
* FreeRTOS scheduler thread.
*/
static void* free_rtos_thread(void* data)
{
(void)data;
// Make this thread cancellable
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
// Start tests
start_free_rtos();
// Start FreeRTOS scheduler (it never returns)
vTaskStartScheduler();
return nullptr;
}
static void end_free_rtos()
{
pthread_cancel(m_freertos_thread_id);
pthread_join(m_freertos_thread_id, nullptr);
// Note: A call to vTaskEndScheduler() never returns.
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
// Create the FreeRTOS scheduler thread and wait until tests are done
pthread_create(&m_freertos_thread_id, nullptr, &free_rtos_thread, nullptr);
while (!m_test_is_done)
{
sleep(1);
}
end_free_rtos();
return m_result;
}