feat(radio): OS compatibility layer (#5840)

This commit is contained in:
Raphael Coeffic
2025-05-03 19:07:49 +10:00
committed by GitHub
parent c996612b0a
commit ab2a19e153
92 changed files with 1740 additions and 896 deletions
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
typedef void (*async_func_t)(void* param1, uint32_t param2);
// schedule a function call for later
// return true if the async call could be stacked, false otherwise
bool async_call(async_func_t func, volatile bool* excl_flag, void* param1,
uint32_t param2);
// schedule a function call for later (interrupt handler)
bool async_call_isr(async_func_t func, volatile bool* excl_flag, void* param1,
uint32_t param2);
+68
View File
@@ -0,0 +1,68 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "async.h"
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/timers.h>
bool async_call(async_func_t func, volatile bool* excl_flag, void* param1,
uint32_t param2)
{
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
// check exclusive flag first
if (excl_flag && *excl_flag) return false;
BaseType_t xReturn = xTimerPendFunctionCall(func, param1, param2, 0);
// update exclusive flag if provided
if (excl_flag && (xReturn == pdPASS)) *excl_flag = true;
return xReturn == pdPASS;
}
func(param1, param2);
return true;
}
bool async_call_isr(async_func_t func, volatile bool* excl_flag, void* param1,
uint32_t param2)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
// check exclusive flag first
if (excl_flag && *excl_flag) return false;
BaseType_t xReturn = xTimerPendFunctionCallFromISR(
func, param1, param2, &xHigherPriorityTaskWoken);
// update exclusive flag if provided
if (excl_flag && (xReturn == pdPASS)) *excl_flag = true;
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
return xReturn == pdPASS;
}
func(param1, param2);
return true;
}
+38
View File
@@ -0,0 +1,38 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "async.h"
#include "timer_pthread_impl.h"
bool async_call(async_func_t func, volatile bool* excl_flag, void* param1,
uint32_t param2)
{
if (excl_flag && *excl_flag) return false;
timer_queue::instance().pend_function(func, param1, param2);
if (excl_flag) *excl_flag = true;
return true;
}
bool async_call_isr(async_func_t func, volatile bool* excl_flag, void* param1,
uint32_t param2)
{
return async_call(func, excl_flag, param1, param2);
}
+27
View File
@@ -0,0 +1,27 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "time.h"
void sleep_ms(uint32_t ms);
void sleep_until(time_point_t* tp, uint32_t ts_ms);
+41
View File
@@ -0,0 +1,41 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "sleep.h"
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/task.h>
void sleep_ms(uint32_t ms)
{
if (!ms) return;
ms /= portTICK_PERIOD_MS;
if (!ms) ms = 1;
vTaskDelay(ms);
}
void sleep_until(time_point_t* tp, uint32_t inc)
{
inc /= portTICK_PERIOD_MS;
vTaskDelayUntil(tp, inc);
}
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "sleep.h"
#include <thread>
extern uint8_t simuSleep(uint32_t ms);
void sleep_ms(uint32_t ms)
{
simuSleep(ms);
}
void sleep_until(time_point_t* tp, uint32_t inc)
{
*tp += std::chrono::duration<uint32_t, std::milli>{inc};
std::this_thread::sleep_until(*tp);
}
+41
View File
@@ -0,0 +1,41 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "sleep.h"
#include "timers_driver.h"
void sleep_ms(uint32_t ms)
{
if (!ms) return;
uint32_t timeout = timersGetMsTick();
while (timersGetMsTick() - timeout < ms) {}
}
void sleep_until(time_point_t* tp, uint32_t inc)
{
uint32_t start = *tp;
*tp = start + inc;
while (timersGetMsTick() - start < inc) {}
}
+46
View File
@@ -0,0 +1,46 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
// OS specific implementation
#if defined(POSIX_THREADS)
#include "task_pthread.h"
#elif defined(FREE_RTOS)
#include "task_freertos.h"
#endif
typedef void (*task_func_t)();
void task_create(task_handle_t* h, task_func_t func, const char* name,
void* stack, unsigned stack_size, unsigned priority);
unsigned task_get_stack_usage(task_handle_t* h);
unsigned task_get_stack_size(task_handle_t* h);
void mutex_create(mutex_handle_t* h);
bool mutex_lock(mutex_handle_t* h);
void mutex_unlock(mutex_handle_t* h);
bool mutex_trylock(mutex_handle_t* h);
+62
View File
@@ -0,0 +1,62 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "task.h"
static void _task_stub(void* p)
{
task_func_t func = (task_func_t)p;
func();
}
void task_create(task_handle_t* h, task_func_t func, const char* name,
void* stack, unsigned stack_size, unsigned priority)
{
h->_stack_size = stack_size;
h->_rtos_handle =
xTaskCreateStatic(_task_stub, name, stack_size, (void*)func, priority,
(StackType_t*)stack, &h->_task_struct);
}
unsigned task_get_stack_usage(task_handle_t* h)
{
return uxTaskGetStackHighWaterMark(h->_rtos_handle);
}
unsigned task_get_stack_size(task_handle_t* h)
{
return h->_stack_size * sizeof(StackType_t);
}
void mutex_create(mutex_handle_t* h)
{
h->_rtos_handle = xSemaphoreCreateMutexStatic(&h->_mutex_struct);
mutex_unlock(h);
}
static inline bool _lock_mutex(mutex_handle_t* h, TickType_t xTickToWait)
{
return xSemaphoreTake(h->_rtos_handle, xTickToWait) == pdTRUE;
}
bool mutex_lock(mutex_handle_t* h) { return _lock_mutex(h, portMAX_DELAY); }
bool mutex_trylock(mutex_handle_t* h) { return _lock_mutex(h, (TickType_t)0); }
void mutex_unlock(mutex_handle_t* h) { xSemaphoreGive(h->_rtos_handle); }
+41
View File
@@ -0,0 +1,41 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/task.h>
#include <FreeRTOS/include/semphr.h>
#include "definitions.h"
#define TASK_DEFINE_STACK(name, size) StackType_t __ALIGNED(8) name[size] __CCMRAM
struct task_handle_t {
TaskHandle_t _rtos_handle;
StaticTask_t _task_struct;
uint32_t _stack_size;
};
struct mutex_handle_t {
SemaphoreHandle_t _rtos_handle;
StaticSemaphore_t _mutex_struct;
};
+69
View File
@@ -0,0 +1,69 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "task.h"
static void* _task_stub(void* p)
{
task_func_t func = (task_func_t)p;
func();
return nullptr;
}
void task_create(task_handle_t* h, task_func_t func, const char* name,
void* stack, unsigned stack_size, unsigned priority)
{
h->_stack_size = stack_size;
pthread_create(&h->_thread_handle, nullptr, _task_stub, (void*)func);
}
unsigned task_get_stack_usage(task_handle_t* h)
{
// fake...
return 0;
}
unsigned task_get_stack_size(task_handle_t* h)
{
return h->_stack_size * 4;
}
void mutex_create(mutex_handle_t* h)
{
*h = PTHREAD_MUTEX_INITIALIZER;
}
bool mutex_lock(mutex_handle_t* h)
{
return pthread_mutex_lock(h) == 0;
}
void mutex_unlock(mutex_handle_t* h)
{
pthread_mutex_unlock(h);
}
bool mutex_trylock(mutex_handle_t* h)
{
return pthread_mutex_trylock(h) == 0;
}
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <pthread.h>
#include <semaphore.h>
#include "definitions.h"
#define TASK_DEFINE_STACK(name, size) void* name
struct task_handle_t {
pthread_t _thread_handle;
uint32_t _stack_size;
};
typedef pthread_mutex_t mutex_handle_t;
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <stdint.h>
// OS specific implementation
#if defined(POSIX_THREADS)
#include "time_native.h"
#elif defined(FREE_RTOS)
#include "time_freertos.h"
#else
#include "time_nortos.h"
#endif
uint32_t time_get_ms();
time_point_t time_point_now();
+35
View File
@@ -0,0 +1,35 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "time.h"
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/task.h>
uint32_t time_get_ms()
{
return xTaskGetTickCount() / portTICK_PERIOD_MS;
}
time_point_t time_point_now()
{
return xTaskGetTickCount();
}
+26
View File
@@ -0,0 +1,26 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <FreeRTOS/include/FreeRTOS.h>
typedef TickType_t time_point_t;
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "time.h"
#include <chrono>
extern uint64_t simuTimerMicros(void);
uint32_t time_get_ms()
{
return simuTimerMicros() / 1000;
}
time_point_t time_point_now()
{
return std::chrono::steady_clock::now();
}
+27
View File
@@ -0,0 +1,27 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <chrono>
typedef std::chrono::time_point<std::chrono::steady_clock> time_point_t;
+35
View File
@@ -0,0 +1,35 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "time.h"
#include "timers_driver.h"
uint32_t time_get_ms()
{
return timersGetMsTick();
}
time_point_t time_point_now()
{
return time_get_ms();
}
+27
View File
@@ -0,0 +1,27 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <stdint.h>
typedef uint32_t time_point_t;
+45
View File
@@ -0,0 +1,45 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
// OS specific implementation
#if defined(POSIX_THREADS)
#include "timer_pthread.h"
#elif defined(FREE_RTOS)
#include "timer_freertos.h"
#endif
typedef void (*timer_func_t)(timer_handle_t*);
int timer_create(timer_handle_t* h, timer_func_t func, const char* name,
unsigned period, bool repeat);
bool timer_is_created(timer_handle_t* h);
bool timer_is_active(timer_handle_t* h);
int timer_start(timer_handle_t* h);
int timer_stop(timer_handle_t* h);
int timer_set_period(timer_handle_t* h, unsigned period);
+70
View File
@@ -0,0 +1,70 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "timer.h"
static void _timer_cb(TimerHandle_t xTimer)
{
timer_handle_t* h = (timer_handle_t*)pvTimerGetTimerID(xTimer);
if (h->_func) h->_func(h);
}
int timer_create(timer_handle_t* h, timer_func_t func, const char* name,
unsigned period, bool repeat)
{
if (h->_rtos_handle) return -1;
h->_rtos_handle = xTimerCreateStatic(name, period / portTICK_PERIOD_MS,
repeat ? pdTRUE : pdFALSE, (void*)h,
_timer_cb, &h->_timer_struct);
h->_func = func;
return 0;
}
bool timer_is_created(timer_handle_t* h)
{
return h->_rtos_handle != nullptr;
}
bool timer_is_active(timer_handle_t* h)
{
return (h->_rtos_handle != nullptr) && (xTimerIsTimerActive(h->_rtos_handle));
}
int timer_start(timer_handle_t* h)
{
if (!h->_rtos_handle) return -1;
return xTimerStart(h->_rtos_handle, 0) == pdPASS ? 0 : -1;
}
int timer_stop(timer_handle_t* h)
{
if (!h->_rtos_handle) return -1;
return xTimerStop(h->_rtos_handle, 0) == pdPASS ? 0 : -1;
}
int timer_set_period(timer_handle_t* h, unsigned period)
{
if (!h->_rtos_handle) return -1;
return xTimerChangePeriod(h->_rtos_handle, period / portTICK_PERIOD_MS, 0) ==
pdPASS
? 0
: -1;
}
+35
View File
@@ -0,0 +1,35 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/timers.h>
struct timer_handle_t;
struct timer_handle_t {
TimerHandle_t _rtos_handle;
StaticTimer_t _timer_struct;
void (*_func)(timer_handle_t*);
};
#define TIMER_INITIALIZER { ._rtos_handle = nullptr }
+215
View File
@@ -0,0 +1,215 @@
#include "timer.h"
#include "timer_pthread_impl.h"
#include <algorithm>
bool _timer_cmp(timer_handle_t *lh, timer_handle_t *rh) {
return lh->next_trigger < rh->next_trigger;
}
timer_queue::timer_queue() {}
void timer_queue::update_current_time() {
_current_time = std::chrono::steady_clock::now();
}
void timer_queue::sort_timers() {
std::sort(_timers.begin(), _timers.end(), _timer_cmp);
}
timer_queue& timer_queue::instance()
{
static timer_queue _instance;
return _instance;
}
void timer_queue::start()
{
std::lock_guard<std::mutex> lock(_cmds_mutex);
if (!_running) {
_running = true;
_thread = std::make_unique<std::thread>([&]() { main_loop(); });
}
}
void timer_queue::stop() {
bool stopping = false;
std::unique_lock<std::mutex> lock(_cmds_mutex);
std::unique_lock<std::mutex> slock(_stop_mutex);
if (_running) {
stopping = true;
_running = false;
lock.unlock();
_cmds_condition.notify_one();
_stop_condition.wait(slock);
slock.unlock();
}
_thread->join();
}
void timer_queue::create_timer(timer_handle_t *timer, timer_func_t func, const char *name,
unsigned period, bool repeat) {
timer->func = func;
timer->name = name;
timer->period = period;
timer->repeat = repeat;
timer->next_trigger = time_point_t{};
}
void timer_queue::send_cmd(timer_req_t&& req)
{
{
std::lock_guard lock(_cmds_mutex);
_cmds.emplace_back(req);
}
_cmds_condition.notify_one();
}
void timer_queue::start_timer(timer_handle_t *timer) {
send_cmd(timer_req_t{.cmd = timer_req_t::cmd_start, .timer = timer});
}
void timer_queue::stop_timer(timer_handle_t *timer) {
send_cmd(timer_req_t{.cmd = timer_req_t::cmd_stop, .timer = timer});
}
void timer_queue::pend_function(timer_async_func_t func, void *param1,
uint32_t param2) {
send_cmd(timer_req_t{
.cmd = timer_req_t::cmd_pend_func,
.func_call = {.func = func, .param1 = param1, .param2 = param2}});
}
void timer_queue::main_loop() {
while (true) {
{
std::unique_lock lock(_cmds_mutex);
time_point_t until;
update_current_time();
process_cmds();
if (_timers.size() > 0) {
timer_handle_t* t = _timers[0];
until = t->next_trigger;
} else {
until = _current_time + std::chrono::milliseconds(1000);
}
std::cv_status result = _cmds_condition.wait_until(lock, until);
if (!_running) break;
}
async_calls();
trigger_timers();
}
_stop_condition.notify_one();
}
void timer_queue::process_cmds()
{
while (!_cmds.empty()) {
timer_req_t req = _cmds.back();
_cmds.pop_back();
if (req.cmd == timer_req_t::cmd_pend_func) {
if (req.func_call.func) {
_funcs.emplace_back(std::move(req.func_call));
}
} else {
timer_handle_t *t = req.timer;
auto pos = std::find(_timers.begin(), _timers.end(), t);
switch (req.cmd) {
case timer_req_t::cmd_start:
t->next_trigger = _current_time + std::chrono::milliseconds(t->period);
t->active = true;
if (pos == _timers.end()) {
_timers.emplace_back(t);
}
break;
case timer_req_t::cmd_stop:
if (pos != _timers.end()) {
_timers.erase(pos);
}
break;
default:
break;
}
}
}
}
void timer_queue::trigger_timers() {
int triggered = 0;
for (auto t : _timers) {
if (t->next_trigger <= _current_time) {
if (t->repeat) {
t->next_trigger += std::chrono::milliseconds(t->period);
} else {
t->active = false;
stop_timer(t);
}
t->func(t);
triggered++;
} else {
break;
}
}
if (triggered) {
sort_timers();
}
}
void timer_queue::async_calls() {
for (auto f : _funcs) {
f.func(f.param1, f.param2);
}
_funcs.clear();
}
int timer_create(timer_handle_t* h, timer_func_t func, const char* name,
unsigned period, bool repeat)
{
if (!h || !func) return -1;
timer_queue::create_timer(h, func, name, period, repeat);
return 0;
}
bool timer_is_created(timer_handle_t* h)
{
return h && h->func;
}
bool timer_is_active(timer_handle_t* h)
{
return h && h->active;
}
int timer_start(timer_handle_t* h)
{
if (!timer_is_created(h)) return -1;
timer_queue::instance().start_timer(h);
return 0;
}
int timer_stop(timer_handle_t* h)
{
if (!timer_is_created(h)) return -1;
timer_queue::instance().stop_timer(h);
return 0;
}
int timer_set_period(timer_handle_t *h, unsigned period)
{
if (!timer_is_created(h)) return -1;
h->period = period;
timer_queue::instance().start_timer(h);
return 0;
}
+45
View File
@@ -0,0 +1,45 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "time.h"
#include <atomic>
struct timer_handle_t;
typedef void (*timer_func_t)(timer_handle_t*);
typedef void (*timer_async_func_t)(void*, uint32_t);
using time_point = std::chrono::time_point<std::chrono::steady_clock>;
struct timer_handle_t {
timer_func_t func;
const char* name;
unsigned period;
bool repeat;
time_point_t next_trigger;
std::atomic_bool active;
};
#define TIMER_INITIALIZER {}
+74
View File
@@ -0,0 +1,74 @@
#include "timer_pthread.h"
#include <condition_variable>
#include <deque>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
struct timer_async_call_t {
timer_async_func_t func;
void *param1;
uint32_t param2;
};
struct timer_req_t {
enum type {
cmd_start,
cmd_stop,
cmd_pend_func,
};
type cmd;
union {
timer_handle_t *timer;
timer_async_call_t func_call;
};
};
class timer_queue {
std::unique_ptr<std::thread> _thread;
bool _running;
std::deque<timer_req_t> _cmds;
std::mutex _cmds_mutex;
std::condition_variable _cmds_condition;
std::mutex _stop_mutex;
std::condition_variable _stop_condition;
std::vector<timer_handle_t*> _timers;
std::vector<timer_async_call_t> _funcs;
time_point_t _current_time;
timer_queue();
void update_current_time();
void sort_timers();
void main_loop();
void trigger_timers();
void async_calls();
void process_cmds();
void send_cmd(timer_req_t&& req);
public:
timer_queue(timer_queue const &) = delete;
void operator=(timer_queue const &) = delete;
static timer_queue &instance();
static void create_timer(timer_handle_t *timer, timer_func_t func, const char *name,
unsigned period, bool repeat);
void start();
void stop();
void start_timer(timer_handle_t *timer);
void stop_timer(timer_handle_t *timer);
void pend_function(timer_async_func_t func, void* param1, uint32_t param2);
};