2022-07-20 10:56:33 +00:00
|
|
|
#include "timer.h"
|
|
|
|
#include "check.h"
|
2022-09-08 06:47:23 +00:00
|
|
|
#include "kernel.h"
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <timers.h>
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
struct FuriTimer {
|
|
|
|
StaticTimer_t container;
|
|
|
|
FuriTimerCallback cb_func;
|
|
|
|
void* cb_context;
|
|
|
|
volatile bool can_be_removed;
|
|
|
|
};
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
// IMPORTANT: container MUST be the FIRST struct member
|
|
|
|
static_assert(offsetof(FuriTimer, container) == 0);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
static void TimerCallback(TimerHandle_t hTimer) {
|
|
|
|
FuriTimer* instance = pvTimerGetTimerID(hTimer);
|
|
|
|
furi_check(instance);
|
|
|
|
instance->cb_func(instance->cb_context);
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FuriTimer* furi_timer_alloc(FuriTimerCallback func, FuriTimerType type, void* context) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check((furi_kernel_is_irq_or_masked() == 0U) && (func != NULL));
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
FuriTimer* instance = malloc(sizeof(FuriTimer));
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
instance->cb_func = func;
|
|
|
|
instance->cb_context = context;
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
const UBaseType_t reload = (type == FuriTimerTypeOnce ? pdFALSE : pdTRUE);
|
|
|
|
const TimerHandle_t hTimer = xTimerCreateStatic(
|
|
|
|
NULL, portMAX_DELAY, reload, instance, TimerCallback, &instance->container);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
furi_check(hTimer == (TimerHandle_t)instance);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void furi_timer_epilogue(void* context, uint32_t arg) {
|
|
|
|
furi_assert(context);
|
|
|
|
UNUSED(arg);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
FuriTimer* instance = context;
|
2022-12-26 12:13:30 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
instance->can_be_removed = true;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_timer_free(FuriTimer* instance) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
|
|
|
furi_check(instance);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
TimerHandle_t hTimer = (TimerHandle_t)instance;
|
2024-06-05 17:04:03 +00:00
|
|
|
furi_check(xTimerDelete(hTimer, portMAX_DELAY) == pdPASS);
|
|
|
|
furi_check(xTimerPendFunctionCall(furi_timer_epilogue, instance, 0, portMAX_DELAY) == pdPASS);
|
2022-09-08 06:47:23 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
while(!instance->can_be_removed) {
|
|
|
|
furi_delay_tick(2);
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
2024-02-08 09:22:03 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
free(instance);
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FuriStatus furi_timer_start(FuriTimer* instance, uint32_t ticks) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
|
|
|
furi_check(instance);
|
|
|
|
furi_check(ticks < portMAX_DELAY);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
TimerHandle_t hTimer = (TimerHandle_t)instance;
|
|
|
|
FuriStatus stat;
|
|
|
|
|
|
|
|
if(xTimerChangePeriod(hTimer, ticks, portMAX_DELAY) == pdPASS) {
|
|
|
|
stat = FuriStatusOk;
|
|
|
|
} else {
|
|
|
|
stat = FuriStatusErrorResource;
|
|
|
|
}
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
return stat;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 15:23:02 +00:00
|
|
|
FuriStatus furi_timer_restart(FuriTimer* instance, uint32_t ticks) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
|
|
|
furi_check(instance);
|
|
|
|
furi_check(ticks < portMAX_DELAY);
|
2023-11-01 07:24:11 +00:00
|
|
|
|
|
|
|
TimerHandle_t hTimer = (TimerHandle_t)instance;
|
|
|
|
FuriStatus stat;
|
|
|
|
|
2023-11-01 15:23:02 +00:00
|
|
|
if(xTimerChangePeriod(hTimer, ticks, portMAX_DELAY) == pdPASS &&
|
|
|
|
xTimerReset(hTimer, portMAX_DELAY) == pdPASS) {
|
2023-11-01 07:24:11 +00:00
|
|
|
stat = FuriStatusOk;
|
|
|
|
} else {
|
|
|
|
stat = FuriStatusErrorResource;
|
|
|
|
}
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
return stat;
|
2023-11-01 07:24:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriStatus furi_timer_stop(FuriTimer* instance) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
|
|
|
furi_check(instance);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
TimerHandle_t hTimer = (TimerHandle_t)instance;
|
|
|
|
|
2023-11-15 16:11:05 +00:00
|
|
|
furi_check(xTimerStop(hTimer, portMAX_DELAY) == pdPASS);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2023-11-15 16:11:05 +00:00
|
|
|
return FuriStatusOk;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t furi_timer_is_running(FuriTimer* instance) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
|
|
|
furi_check(instance);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
TimerHandle_t hTimer = (TimerHandle_t)instance;
|
|
|
|
|
|
|
|
/* Return 0: not running, 1: running */
|
|
|
|
return (uint32_t)xTimerIsTimerActive(hTimer);
|
|
|
|
}
|
2023-05-05 12:40:55 +00:00
|
|
|
|
2023-11-01 07:24:11 +00:00
|
|
|
uint32_t furi_timer_get_expire_time(FuriTimer* instance) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
|
|
|
furi_check(instance);
|
2023-11-01 07:24:11 +00:00
|
|
|
|
|
|
|
TimerHandle_t hTimer = (TimerHandle_t)instance;
|
|
|
|
|
|
|
|
return (uint32_t)xTimerGetExpiryTime(hTimer);
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:40:55 +00:00
|
|
|
void furi_timer_pending_callback(FuriTimerPendigCallback callback, void* context, uint32_t arg) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(callback);
|
|
|
|
|
2023-05-05 12:40:55 +00:00
|
|
|
BaseType_t ret = pdFAIL;
|
|
|
|
if(furi_kernel_is_irq_or_masked()) {
|
|
|
|
ret = xTimerPendFunctionCallFromISR(callback, context, arg, NULL);
|
|
|
|
} else {
|
|
|
|
ret = xTimerPendFunctionCall(callback, context, arg, FuriWaitForever);
|
|
|
|
}
|
2024-03-19 14:43:52 +00:00
|
|
|
|
2023-05-05 12:40:55 +00:00
|
|
|
furi_check(ret == pdPASS);
|
2023-11-01 07:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_timer_set_thread_priority(FuriTimerThreadPriority priority) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
2023-11-01 15:23:02 +00:00
|
|
|
|
|
|
|
TaskHandle_t task_handle = xTimerGetTimerDaemonTaskHandle();
|
|
|
|
furi_check(task_handle); // Don't call this method before timer task start
|
2023-11-01 07:24:11 +00:00
|
|
|
|
|
|
|
if(priority == FuriTimerThreadPriorityNormal) {
|
|
|
|
vTaskPrioritySet(task_handle, configTIMER_TASK_PRIORITY);
|
|
|
|
} else if(priority == FuriTimerThreadPriorityElevated) {
|
|
|
|
vTaskPrioritySet(task_handle, configMAX_PRIORITIES - 1);
|
|
|
|
} else {
|
|
|
|
furi_crash();
|
|
|
|
}
|
2024-02-08 09:22:03 +00:00
|
|
|
}
|