Change handling of timer context.

This commit is contained in:
Marco 2023-02-26 16:21:21 +01:00
parent 10c21c1058
commit 62418ea06c
4 changed files with 18 additions and 21 deletions

2
.vscode/tasks.json vendored
View File

@ -4,7 +4,7 @@
{
"type": "shell",
"label": "build",
"command": "make debug",
"command": "make",
"group": "build",
"detail": "compiler: /usr/bin/gcc"
}

View File

@ -14,7 +14,7 @@ void tmr_timerOverflow();
void tmr_initTimer(tmr_ctx *ctx);
void *tmr_handleTimerOverflow(sigval_t *timer_context);
void *tmr_handleTimerOverflow(void *timer_context);
int tmr_callEveryMs(void (*func)(void));

View File

@ -5,11 +5,10 @@
#include "ftdi_utils.h"
#include "timer_utils.h"
static uint64_t counter =0;
static uint64_t counter = 0;
void test(void)
{
printf("%lu\n",counter);
printf("%lu\n", counter);
counter++;
}

View File

@ -7,47 +7,45 @@
#include "time_scales.h"
#include "timer_utils.h"
static tmr_ctx timer_context;
void tmr_timerOverflow()
{
}
void tmr_initTimer(tmr_ctx *ctx)
{
timer_context.sevp = (struct sigevent){
ctx->sevp = (struct sigevent){
.sigev_notify = SIGEV_THREAD,
.sigev_notify_function = tmr_handleTimerOverflow,
.sigev_value.sival_ptr = ctx,
};
timer_create(CLOCK_MONOTONIC, &timer_context.sevp, &ctx->timerid);
clock_gettime(CLOCK_MONOTONIC, &timer_context.times.it_value);
tmr_add_ns_to_current_time(&timer_context.times, timer_context.interval);
timer_settime(timer_context.timerid, TIMER_ABSTIME, &timer_context.times, NULL);
timer_create(CLOCK_MONOTONIC, &ctx->sevp, &ctx->timerid);
clock_gettime(CLOCK_MONOTONIC, &ctx->times.it_value);
tmr_add_ns_to_current_time(&ctx->times, ctx->interval);
timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL);
}
void *tmr_handleTimerOverflow(sigval_t *ctx)
void *tmr_handleTimerOverflow(void *ctx_par)
{
tmr_ctx *ctx = (tmr_ctx *)ctx_par;
ctx->funcToBeCalled();
timer_context.funcToBeCalled();
tmr_add_ns_to_current_time(&timer_context.times, timer_context.interval);
timer_settime(timer_context.timerid, TIMER_ABSTIME, &timer_context.times, NULL);
tmr_add_ns_to_current_time(&ctx->times, ctx->interval);
timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL);
return (void *)0;
}
int tmr_callEveryMs(void (*func)(void))
{
timer_context.interval = 1000000;
timer_context.funcToBeCalled = func;
tmr_ctx *tmr = malloc(sizeof(tmr_ctx));
tmr->interval = 1000000;
tmr->funcToBeCalled = func;
if (!func)
return -1;
tmr_initTimer(&timer_context);
tmr_initTimer(tmr);
return 0;
}