Make timer interface work (kind of). We can cyclically call functions now.

This commit is contained in:
Marco 2023-02-26 15:44:26 +01:00
parent 5cf13d04c0
commit 10c21c1058
12 changed files with 185 additions and 77 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
/build/
# ---> C
# Prerequisites
*.d

View File

@ -1,23 +1,23 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/src/include/**",
"${default}",
"/usr/include/libftdi1/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "linux-gcc-x64",
"browse": {
"path": [
"/usr/include/libftdi1/**"
]
}
}
],
"version": 4
"configurations": [
{
"name": "Linux",
"includePath": [
"/usr/include/**",
"${workspaceFolder}/src/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "linux-clang-x64",
"browse": {
"path": [
"/usr/include/**",
"${workspaceFolder}/src/include/**"
]
}
}
],
"version": 4
}

28
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/inverter",
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

10
.vscode/settings.json vendored
View File

@ -1,10 +0,0 @@
{
"C_Cpp.errorSquiggles": "Enabled",
"files.associations": {
"pwm.h": "c",
"ftdi.h": "c",
"bridge.h": "c",
"pins.h": "c",
"stdio.h": "c"
}
}

38
.vscode/tasks.json vendored
View File

@ -1,30 +1,12 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I/usr/include/libftdi1",
"-lftdi1"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "build",
"command": "make debug",
"group": "build",
"detail": "compiler: /usr/bin/gcc"
}
]
}

View File

@ -3,7 +3,7 @@ BUILDFOLDER = ./build/
TARGET = $(BUILDFOLDER)$(TARGETNAME)
INC = -Isrc/include -I/usr/include -I/usr/include/libftdi1
CFLAGS = -Wall -Wpedantic -Wextra -Os
CFLAGS = -Wall -Wpedantic -Wextra -Os
CPPFLAGS =
SRC = $(wildcard src/*.c)
@ -12,7 +12,7 @@ all: $(TARGET)
$(TARGET): $(SRC)
mkdir -p build
gcc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1
gcc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1 -g
clean:
rm -fr ./src/*.o ./build/

Binary file not shown.

View File

@ -0,0 +1,8 @@
//microseconds in nanoseconds
#define USEC (1000)
//milliseconds in nanoseconds
#define MSEC (1000 * USEC)
//seconds in nanoseconds
#define SEC (1000 * MSEC)

21
src/include/timer_utils.h Normal file
View File

@ -0,0 +1,21 @@
#include <time.h>
#include <signal.h>
typedef struct
{
timer_t timerid;
long interval;
struct sigevent sevp;
struct itimerspec times;
void (*funcToBeCalled)(void);
} tmr_ctx;
void tmr_timerOverflow();
void tmr_initTimer(tmr_ctx *ctx);
void *tmr_handleTimerOverflow(sigval_t *timer_context);
int tmr_callEveryMs(void (*func)(void));
void tmr_add_ns_to_current_time(struct itimerspec *current, long usecs);

View File

@ -1,16 +0,0 @@
#include <ftdi.h>
#include "pins.h"
#include "pwm.h"
#include "ftdi_utils.h"
int main()
{
init_ftdi();
ftdi_free(ftdi);
return 0;
}

27
src/main.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include <ftdi.h>
#include <unistd.h>
#include "ftdi_utils.h"
#include "timer_utils.h"
static uint64_t counter =0;
void test(void)
{
printf("%lu\n",counter);
counter++;
}
int main()
{
printf("Program started\n");
tmr_callEveryMs(test);
while (1)
{
sleep(10000);
}
return 0;
}

66
src/timer_utils.c Normal file
View File

@ -0,0 +1,66 @@
#include <time.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#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){
.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);
}
void *tmr_handleTimerOverflow(sigval_t *ctx)
{
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);
return (void *)0;
}
int tmr_callEveryMs(void (*func)(void))
{
timer_context.interval = 1000000;
timer_context.funcToBeCalled = func;
if (!func)
return -1;
tmr_initTimer(&timer_context);
return 0;
}
void tmr_add_ns_to_current_time(struct itimerspec *time, long nsec)
{
if ((time->it_value.tv_nsec + nsec) >= 1 * SEC)
{
time->it_value.tv_sec++;
time->it_value.tv_nsec -= (1 * SEC - nsec);
}
else
{
time->it_value.tv_nsec += nsec;
}
}