From 1f6c94746cc35586cbe794930d96b97d8b340c97 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 26 Feb 2023 18:31:02 +0100 Subject: [PATCH] Research issue where togglePin1() is not being called. --- .vscode/c_cpp_properties.json | 11 +++++----- .vscode/launch.json | 2 +- src/include/timer_utils.h | 3 ++- src/main.c | 41 +++++++++++++++++++++++++++++++++-- src/timer_utils.c | 19 ++++++++++++++-- 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index b55a19a..6449072 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -4,17 +4,16 @@ "name": "Linux", "includePath": [ "/usr/include/**", - "${workspaceFolder}/src/include/**" + "${workspaceFolder}/**" ], "defines": [], - "compilerPath": "/usr/bin/clang", + "compilerPath": "/usr/bin/gcc", "cStandard": "c17", - "cppStandard": "c++14", - "intelliSenseMode": "linux-clang-x64", + "intelliSenseMode": "linux-gcc-x64", "browse": { "path": [ - "/usr/include/**", - "${workspaceFolder}/src/include/**" + "/usr/include", + "${workspaceFolder}/**" ] } } diff --git a/.vscode/launch.json b/.vscode/launch.json index 361ecc4..156b5a3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/inverter", - "stopAtEntry": true, + "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, diff --git a/src/include/timer_utils.h b/src/include/timer_utils.h index 5efcc44..3f5b189 100644 --- a/src/include/timer_utils.h +++ b/src/include/timer_utils.h @@ -7,10 +7,11 @@ typedef struct long interval; struct sigevent sevp; struct itimerspec times; + struct timespec runsSince; void (*funcToBeCalled)(void); } tmr_ctx; void tmr_initTimer(tmr_ctx *ctx); void tmr_handleTimerOverflow(union sigval); -int tmr_callEveryMs(void (*func)(void)); +int tmr_callEvery(void (*func)(void), long nsec); void tmr_add_ns_to_current_time(struct itimerspec *current, long usecs); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 69599e7..6096d3f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,21 +1,58 @@ #include +#include #include #include #include "ftdi_utils.h" #include "timer_utils.h" +#include "time_scales.h" +#include "pins.h" static uint64_t counter = 0; -void test(void) +void test1(void) { printf("%lu\n", counter); counter++; } +void test2(void) +{ + printf("This is test2\n"); +} + +void togglePin1(void) +{ + printf("toggling pin\n"); + setPin(0); +} + int main() { printf("Program started\n"); - tmr_callEveryMs(test); + + if (tmr_callEvery(test1, 1 * SEC)) + { + printf("Error starting timer for test\n"); + return EXIT_FAILURE; + } + + if (tmr_callEvery(test2, 1 * SEC)) + { + printf("Error starting timer for test\n"); + return EXIT_FAILURE; + } + + if (init_ftdi()) + { + printf("Error initializing ftdi\n"); + return EXIT_FAILURE; + } + + if (tmr_callEvery(togglePin1, 1 * SEC)) + { + printf("Error starting timer for togglePin\n"); + return EXIT_FAILURE; + } while (1) { diff --git a/src/timer_utils.c b/src/timer_utils.c index d31bbf5..bfa021e 100644 --- a/src/timer_utils.c +++ b/src/timer_utils.c @@ -7,6 +7,14 @@ #include "time_scales.h" #include "timer_utils.h" +int tmr_registry_index = 0; +tmr_ctx *tmr_registry[10]; + +void tmr_add_to_registry(tmr_ctx* ctx) { + tmr_registry[tmr_registry_index] = ctx; + tmr_registry_index++; +} + void tmr_initTimer(tmr_ctx *ctx) { ctx->sevp = (struct sigevent){ @@ -17,6 +25,10 @@ void tmr_initTimer(tmr_ctx *ctx) timer_create(CLOCK_MONOTONIC, &ctx->sevp, &ctx->timerid); clock_gettime(CLOCK_MONOTONIC, &ctx->times.it_value); + + ctx->runsSince.tv_sec = ctx->times.it_value.tv_sec; + ctx->runsSince.tv_nsec = ctx->times.it_value.tv_nsec; + tmr_add_ns_to_current_time(&ctx->times, ctx->interval); timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL); } @@ -30,10 +42,13 @@ void tmr_handleTimerOverflow(sigval_t ctx_par) timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL); } -int tmr_callEveryMs(void (*func)(void)) +int tmr_callEvery(void (*func)(void), long nsec) { tmr_ctx *tmr = malloc(sizeof(tmr_ctx)); - tmr->interval = 1000000; + + tmr_add_to_registry(tmr); + + tmr->interval = nsec; tmr->funcToBeCalled = func; if (!func)