Skip to content

Instantly share code, notes, and snippets.

@marcobergamin
Last active November 30, 2023 05:29
Show Gist options
  • Save marcobergamin/2e77003e7efa2a891a6726b14e8c61fe to your computer and use it in GitHub Desktop.
Save marcobergamin/2e77003e7efa2a891a6726b14e8c61fe to your computer and use it in GitHub Desktop.
GTest main for FreeRTOS
#include "FreeRTOS.h"
extern "C" {
#include "task.h"
}
#include <atomic>
#include <cstdint>
#include <gtest/gtest.h>
constexpr uint16_t MAX_STACK_DEPTH = UINT16_MAX;
static pthread_t m_freertos_thread_id;
static int m_result = 0;
static std::atomic_bool m_test_is_done(false);
static void gtest_task(void *param) {
(void) param;
m_result = RUN_ALL_TESTS();
m_test_is_done = true;
vTaskSuspend(nullptr);
}
static void start_free_rtos() {
BaseType_t rtos_res = xTaskCreate(gtest_task, "gtest_task", MAX_STACK_DEPTH, nullptr, 0, nullptr);
if (rtos_res != pdPASS) {
abort();
}
}
static void *free_rtos_thread(void *data) {
(void) data;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
start_free_rtos();
vTaskStartScheduler();
return nullptr;
}
static void end_free_rtos() {
pthread_cancel(m_freertos_thread_id);
pthread_join(m_freertos_thread_id, nullptr);
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
sigset_t set;
sigfillset(&set);
pthread_sigmask(SIG_SETMASK, &set, NULL);
pthread_create(&m_freertos_thread_id, nullptr, &free_rtos_thread, nullptr);
while(!m_test_is_done) {
usleep(10 * 1000);
}
end_free_rtos();
return m_result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment