diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 2fb2e9c..5428d5c 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ { "type": "shell", "label": "build", - "command": "make debug", + "command": "make", "group": "build", "detail": "compiler: /usr/bin/gcc" } diff --git a/src/include/timer_utils.h b/src/include/timer_utils.h index 791d904..69d309f 100644 --- a/src/include/timer_utils.h +++ b/src/include/timer_utils.h @@ -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)); diff --git a/src/main.c b/src/main.c index cd9ad20..69599e7 100644 --- a/src/main.c +++ b/src/main.c @@ -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++; } diff --git a/src/timer_utils.c b/src/timer_utils.c index c8fd543..128c19a 100644 --- a/src/timer_utils.c +++ b/src/timer_utils.c @@ -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; }