Make timer interface work (kind of). We can cyclically call functions now.
This commit is contained in:
parent
5cf13d04c0
commit
10c21c1058
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
|||||||
|
/build/
|
||||||
|
|
||||||
# ---> C
|
# ---> C
|
||||||
# Prerequisites
|
# Prerequisites
|
||||||
*.d
|
*.d
|
||||||
|
12
.vscode/c_cpp_properties.json
vendored
12
.vscode/c_cpp_properties.json
vendored
@ -3,18 +3,18 @@
|
|||||||
{
|
{
|
||||||
"name": "Linux",
|
"name": "Linux",
|
||||||
"includePath": [
|
"includePath": [
|
||||||
"${workspaceFolder}/src/include/**",
|
"/usr/include/**",
|
||||||
"${default}",
|
"${workspaceFolder}/src/include/**"
|
||||||
"/usr/include/libftdi1/**"
|
|
||||||
],
|
],
|
||||||
"defines": [],
|
"defines": [],
|
||||||
"compilerPath": "/usr/bin/gcc",
|
"compilerPath": "/usr/bin/clang",
|
||||||
"cStandard": "c17",
|
"cStandard": "c17",
|
||||||
"cppStandard": "c++14",
|
"cppStandard": "c++14",
|
||||||
"intelliSenseMode": "linux-gcc-x64",
|
"intelliSenseMode": "linux-clang-x64",
|
||||||
"browse": {
|
"browse": {
|
||||||
"path": [
|
"path": [
|
||||||
"/usr/include/libftdi1/**"
|
"/usr/include/**",
|
||||||
|
"${workspaceFolder}/src/include/**"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
28
.vscode/launch.json
vendored
Normal file
28
.vscode/launch.json
vendored
Normal 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
10
.vscode/settings.json
vendored
@ -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"
|
|
||||||
}
|
|
||||||
}
|
|
32
.vscode/tasks.json
vendored
32
.vscode/tasks.json
vendored
@ -1,30 +1,12 @@
|
|||||||
{
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
"tasks": [
|
"tasks": [
|
||||||
{
|
{
|
||||||
"type": "cppbuild",
|
"type": "shell",
|
||||||
"label": "C/C++: gcc build active file",
|
"label": "build",
|
||||||
"command": "/usr/bin/gcc",
|
"command": "make debug",
|
||||||
"args": [
|
"group": "build",
|
||||||
"-fdiagnostics-color=always",
|
"detail": "compiler: /usr/bin/gcc"
|
||||||
"-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"
|
|
||||||
}
|
}
|
2
Makefile
2
Makefile
@ -12,7 +12,7 @@ all: $(TARGET)
|
|||||||
|
|
||||||
$(TARGET): $(SRC)
|
$(TARGET): $(SRC)
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
gcc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1
|
gcc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1 -g
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -fr ./src/*.o ./build/
|
rm -fr ./src/*.o ./build/
|
||||||
|
BIN
build/inverter
BIN
build/inverter
Binary file not shown.
8
src/include/time_scales.h
Normal file
8
src/include/time_scales.h
Normal 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
21
src/include/timer_utils.h
Normal 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);
|
@ -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
27
src/main.c
Normal 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
66
src/timer_utils.c
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user