Timer fixes & tests added

This commit is contained in:
Ken Van Hoeylandt 2024-02-02 22:19:36 +01:00 committed by GitHub
parent 47377439dd
commit 4f89918e91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 168 additions and 43 deletions

View File

@ -16,13 +16,7 @@ typedef struct {
} TimerCallback_t; } TimerCallback_t;
static void timer_callback(TimerHandle_t hTimer) { static void timer_callback(TimerHandle_t hTimer) {
TimerCallback_t* callb; TimerCallback_t* callb = (TimerCallback_t*)pvTimerGetTimerID(hTimer);
/* Retrieve pointer to callback function and context */
callb = (TimerCallback_t*)pvTimerGetTimerID(hTimer);
/* Remove dynamic allocation flag */
callb = (TimerCallback_t*)((uint32_t)callb & ~1U);
if (callb != NULL) { if (callb != NULL) {
callb->func(callb->context); callb->func(callb->context);
@ -31,36 +25,26 @@ static void timer_callback(TimerHandle_t hTimer) {
Timer* tt_timer_alloc(TimerCallback func, TimerType type, void* context) { Timer* tt_timer_alloc(TimerCallback func, TimerType type, void* context) {
tt_assert((tt_kernel_is_irq() == 0U) && (func != NULL)); tt_assert((tt_kernel_is_irq() == 0U) && (func != NULL));
TimerCallback_t* callb = (TimerCallback_t*)malloc(sizeof(TimerCallback_t));
TimerHandle_t hTimer;
TimerCallback_t* callb;
UBaseType_t reload;
hTimer = NULL;
/* Dynamic memory allocation is available: if memory for callback and */
/* its context is not provided, allocate it from dynamic memory pool */
callb = (TimerCallback_t*)malloc(sizeof(TimerCallback_t));
callb->func = func; callb->func = func;
callb->context = context; callb->context = context;
UBaseType_t reload;
if (type == TimerTypeOnce) { if (type == TimerTypeOnce) {
reload = pdFALSE; reload = pdFALSE;
} else { } else {
reload = pdTRUE; reload = pdTRUE;
} }
/* Store callback memory dynamic allocation flag */
callb = (TimerCallback_t*)((uint32_t)callb | 1U);
// TimerCallback function is always provided as a callback and is used to call application // TimerCallback function is always provided as a callback and is used to call application
// specified function with its context both stored in structure callb. // specified function with its context both stored in structure callb.
// TODO: should we use pointer to function or function directly as-is? // TODO: should we use pointer to function or function directly as-is?
hTimer = xTimerCreate(NULL, portMAX_DELAY, reload, callb, timer_callback); TimerHandle_t hTimer = xTimerCreate(NULL, portMAX_DELAY, reload, callb, timer_callback);
tt_check(hTimer); tt_check(hTimer);
/* Return timer ID */ /* Return timer ID */
return ((Timer*)hTimer); return (Timer*)hTimer;
} }
void tt_timer_free(Timer* instance) { void tt_timer_free(Timer* instance) {
@ -68,21 +52,14 @@ void tt_timer_free(Timer* instance) {
tt_assert(instance); tt_assert(instance);
TimerHandle_t hTimer = (TimerHandle_t)instance; TimerHandle_t hTimer = (TimerHandle_t)instance;
TimerCallback_t* callb; TimerCallback_t* callb = (TimerCallback_t*)pvTimerGetTimerID(hTimer);
callb = (TimerCallback_t*)pvTimerGetTimerID(hTimer);
tt_check(xTimerDelete(hTimer, portMAX_DELAY) == pdPASS); tt_check(xTimerDelete(hTimer, portMAX_DELAY) == pdPASS);
while (tt_timer_is_running(instance)) tt_delay_tick(2); while (tt_timer_is_running(instance)) tt_delay_tick(2);
if ((uint32_t)callb & 1U) { /* Return allocated memory to dynamic pool */
/* Callback memory was allocated from dynamic pool, clear flag */ free(callb);
callb = (TimerCallback_t*)((uint32_t)callb & ~1U);
/* Return allocated memory to dynamic pool */
free(callb);
}
} }
TtStatus tt_timer_start(Timer* instance, uint32_t ticks) { TtStatus tt_timer_start(Timer* instance, uint32_t ticks) {

View File

@ -0,0 +1,52 @@
#include "doctest.h"
#include "bundle.h"
#include <cstring>
TEST_CASE("boolean can be stored and retrieved") {
Bundle bundle = tt_bundle_alloc();
tt_bundle_put_bool(bundle, "key", true);
CHECK(tt_bundle_get_bool(bundle, "key"));
bool opt_result = false;
CHECK(tt_bundle_opt_bool(bundle, "key", &opt_result));
CHECK(tt_bundle_has_bool(bundle, "key"));
CHECK_EQ(opt_result, true);
tt_bundle_free(bundle);
}
TEST_CASE("int32 can be stored and retrieved") {
Bundle bundle = tt_bundle_alloc();
tt_bundle_put_int32(bundle, "key", 42);
CHECK(tt_bundle_get_int32(bundle, "key"));
int32_t opt_result = 0;
CHECK(tt_bundle_opt_int32(bundle, "key", &opt_result));
CHECK(tt_bundle_has_int32(bundle, "key"));
CHECK_EQ(opt_result, 42);
tt_bundle_free(bundle);
}
TEST_CASE("string can be stored and retrieved") {
Bundle bundle = tt_bundle_alloc();
tt_bundle_put_string(bundle, "key", "value");
const char* value_from_bundle = tt_bundle_get_string(bundle, "key");
CHECK_EQ(strcmp(value_from_bundle, "value"), 0);
char* opt_result = NULL;
CHECK(tt_bundle_opt_string(bundle, "key", &opt_result));
CHECK(tt_bundle_has_string(bundle, "key"));
CHECK(opt_result != NULL);
tt_bundle_free(bundle);
}
TEST_CASE("bundle copy holds all copied values when original is freed") {
Bundle original = tt_bundle_alloc();
tt_bundle_put_bool(original, "bool", true);
tt_bundle_put_int32(original, "int32", 123);
tt_bundle_put_string(original, "string", "text");
Bundle copy = tt_bundle_alloc_copy(original);
tt_bundle_free(original);
CHECK(tt_bundle_get_bool(copy, "bool") == true);
CHECK_EQ(tt_bundle_get_int32(copy, "int32"), 123);
CHECK_EQ(strcmp(tt_bundle_get_string(copy, "string"), "text"), 0);
tt_bundle_free(copy);
}

View File

@ -0,0 +1,34 @@
#include "doctest.h"
#include "tactility_core.h"
#include "mutex.h"
static int thread_with_mutex_parameter(void* parameter) {
Mutex mutex = (Mutex)parameter;
tt_mutex_acquire(mutex, TtWaitForever);
return 0;
}
TEST_CASE("a mutex can block a thread") {
Mutex mutex = tt_mutex_alloc(MutexTypeNormal);
tt_mutex_acquire(mutex, TtWaitForever);
Thread* thread = tt_thread_alloc_ex(
"thread",
1024,
&thread_with_mutex_parameter,
mutex
);
tt_thread_start(thread);
tt_delay_ms(5);
CHECK_EQ(tt_thread_get_state(thread), ThreadStateRunning);
tt_mutex_release(mutex);
tt_delay_ms(5);
CHECK_EQ(tt_thread_get_state(thread), ThreadStateStopped);
tt_thread_join(thread);
tt_thread_free(thread);
tt_mutex_free(mutex);
}

View File

@ -1,40 +1,39 @@
#include "doctest.h" #include "doctest.h"
#include "tactility_core.h"
#include "thread.h" #include "thread.h"
#include "FreeRTOS.h" static int interruptable_thread(void* parameter) {
#include "task.h"
static int interruptable_thread(TT_UNUSED void* parameter) {
bool* interrupted = (bool*)parameter; bool* interrupted = (bool*)parameter;
while (!*interrupted) { while (!*interrupted) {
vTaskDelay(5); tt_delay_ms(5);
} }
return 0; return 0;
} }
static bool immedate_return_thread_called = false; static int immediate_return_thread(void* parameter) {
static int immediate_return_thread(TT_UNUSED void* parameter) { bool* has_called = (bool*)parameter;
immedate_return_thread_called = true; *has_called = true;
return 0; return 0;
} }
static int thread_with_return_code(TT_UNUSED void* parameter) { static int thread_with_return_code(void* parameter) {
int* code = (int*)parameter; int* code = (int*)parameter;
return *code; return *code;
} }
TEST_CASE("when a thread is started then its callback should be called") { TEST_CASE("when a thread is started then its callback should be called") {
bool has_called = false;
Thread* thread = tt_thread_alloc_ex( Thread* thread = tt_thread_alloc_ex(
"immediate return task", "immediate return task",
4096, 4096,
&immediate_return_thread, &immediate_return_thread,
NULL &has_called
); );
CHECK(!immedate_return_thread_called); CHECK(!has_called);
tt_thread_start(thread); tt_thread_start(thread);
tt_thread_join(thread); tt_thread_join(thread);
tt_thread_free(thread); tt_thread_free(thread);
CHECK(immedate_return_thread_called); CHECK(has_called);
} }
TEST_CASE("a thread can be started and stopped") { TEST_CASE("a thread can be started and stopped") {

View File

@ -0,0 +1,63 @@
#include "doctest.h"
#include "tactility_core.h"
#include "timer.h"
void* timer_callback_context = NULL;
static void timer_callback_with_context(void* context) {
timer_callback_context = context;
}
static void timer_callback_with_counter(void* context) {
int* int_ptr = (int*)context;
(*int_ptr)++;
}
TEST_CASE("a timer passes the context correctly") {
int foo = 1;
Timer* timer = tt_timer_alloc(&timer_callback_with_context, TimerTypeOnce, &foo);
tt_timer_start(timer, 1);
tt_delay_tick(10);
tt_timer_stop(timer);
tt_timer_free(timer);
CHECK_EQ(timer_callback_context, &foo);
}
TEST_CASE("TimerTypePeriodic timers can be stopped and restarted") {
int counter = 0;
Timer* timer = tt_timer_alloc(&timer_callback_with_counter, TimerTypePeriodic, &counter);
tt_timer_start(timer, 1);
tt_delay_tick(10);
tt_timer_stop(timer);
tt_delay_tick(10);
tt_timer_stop(timer);
tt_timer_free(timer);
CHECK_GE(counter, 2);
}
TEST_CASE("TimerTypePeriodic calls the callback periodically") {
int counter = 0;
int ticks_to_run = 10;
Timer* timer = tt_timer_alloc(&timer_callback_with_counter, TimerTypePeriodic, &counter);
tt_timer_start(timer, 1);
tt_delay_tick(ticks_to_run);
tt_timer_stop(timer);
tt_timer_free(timer);
CHECK_EQ(counter, ticks_to_run);
}
TEST_CASE("restarting TimerTypeOnce timers has no effect") {
int counter = 0;
Timer* timer = tt_timer_alloc(&timer_callback_with_counter, TimerTypeOnce, &counter);
tt_timer_start(timer, 1);
tt_delay_tick(10);
tt_timer_stop(timer);
tt_delay_tick(10);
tt_timer_stop(timer);
tt_timer_free(timer);
CHECK_EQ(counter, 1);
}