This commit is contained in:
Marco 2024-01-16 16:28:21 +01:00
parent 99684ac531
commit b6e88b3751
14 changed files with 137 additions and 207 deletions

View File

@ -6,7 +6,7 @@ TEST_TARGETNAME = test
TEST_BUILDFOLDER = ./test/build/ TEST_BUILDFOLDER = ./test/build/
TEST_TARGET = $(TEST_BUILDFOLDER)$(TEST_TARGETNAME) TEST_TARGET = $(TEST_BUILDFOLDER)$(TEST_TARGETNAME)
INC = -Isrc/include -I/usr/include -I/usr/include/libftdi1 INC = -Isrc/include -I/usr/include
CFLAGS = -Wall -Wpedantic -Wextra -g CFLAGS = -Wall -Wpedantic -Wextra -g
CPPFLAGS = CPPFLAGS =
@ -17,13 +17,13 @@ $(TARGETNAME): $(TARGET)
$(TARGET): $(SRC) $(TARGET): $(SRC)
mkdir -p $(BUILDFOLDER) mkdir -p $(BUILDFOLDER)
cc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1 cc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm
$(TEST_TARGETNAME): $(TEST_TARGET) $(TEST_TARGETNAME): $(TEST_TARGET)
$(TEST_TARGET): $(TEST_SRC) $(TEST_TARGET): $(TEST_SRC)
mkdir -p $(TEST_BUILDFOLDER) mkdir -p $(TEST_BUILDFOLDER)
cc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1 -lcunit cc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lcunit
clean: clean:
rm -fr ./src/*.o $(BUILDFOLDER)* rm -fr ./src/*.o $(BUILDFOLDER)*

View File

@ -9,14 +9,13 @@
"-g", "-g",
"-Isrc/include", "-Isrc/include",
"-I/usr/include", "-I/usr/include",
"-I/usr/include/libftdi1",
"-o", "-o",
"build/inverter", "build/inverter",
"src/bridge_utils.c" "src/bridge_utils.c"
], ],
"directory": "/home/m/dev/electronics/inverter/sw_ftdi", "directory": "/home/m/dev/electronics/inverter/sw_linux",
"file": "/home/m/dev/electronics/inverter/sw_ftdi/src/bridge_utils.c", "file": "/home/m/dev/electronics/inverter/sw_linux/src/bridge_utils.c",
"output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter" "output": "/home/m/dev/electronics/inverter/sw_linux/build/inverter"
}, },
{ {
"arguments": [ "arguments": [
@ -28,33 +27,13 @@
"-g", "-g",
"-Isrc/include", "-Isrc/include",
"-I/usr/include", "-I/usr/include",
"-I/usr/include/libftdi1",
"-o",
"build/inverter",
"src/ftdi_utils.c"
],
"directory": "/home/m/dev/electronics/inverter/sw_ftdi",
"file": "/home/m/dev/electronics/inverter/sw_ftdi/src/ftdi_utils.c",
"output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter"
},
{
"arguments": [
"/usr/bin/cc",
"-c",
"-Wall",
"-Wpedantic",
"-Wextra",
"-g",
"-Isrc/include",
"-I/usr/include",
"-I/usr/include/libftdi1",
"-o", "-o",
"build/inverter", "build/inverter",
"src/main.c" "src/main.c"
], ],
"directory": "/home/m/dev/electronics/inverter/sw_ftdi", "directory": "/home/m/dev/electronics/inverter/sw_linux",
"file": "/home/m/dev/electronics/inverter/sw_ftdi/src/main.c", "file": "/home/m/dev/electronics/inverter/sw_linux/src/main.c",
"output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter" "output": "/home/m/dev/electronics/inverter/sw_linux/build/inverter"
}, },
{ {
"arguments": [ "arguments": [
@ -66,14 +45,13 @@
"-g", "-g",
"-Isrc/include", "-Isrc/include",
"-I/usr/include", "-I/usr/include",
"-I/usr/include/libftdi1",
"-o", "-o",
"build/inverter", "build/inverter",
"src/pins.c" "src/pins.c"
], ],
"directory": "/home/m/dev/electronics/inverter/sw_ftdi", "directory": "/home/m/dev/electronics/inverter/sw_linux",
"file": "/home/m/dev/electronics/inverter/sw_ftdi/src/pins.c", "file": "/home/m/dev/electronics/inverter/sw_linux/src/pins.c",
"output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter" "output": "/home/m/dev/electronics/inverter/sw_linux/build/inverter"
}, },
{ {
"arguments": [ "arguments": [
@ -85,14 +63,13 @@
"-g", "-g",
"-Isrc/include", "-Isrc/include",
"-I/usr/include", "-I/usr/include",
"-I/usr/include/libftdi1",
"-o", "-o",
"build/inverter", "build/inverter",
"src/statemachine.c" "src/statemachine.c"
], ],
"directory": "/home/m/dev/electronics/inverter/sw_ftdi", "directory": "/home/m/dev/electronics/inverter/sw_linux",
"file": "/home/m/dev/electronics/inverter/sw_ftdi/src/statemachine.c", "file": "/home/m/dev/electronics/inverter/sw_linux/src/statemachine.c",
"output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter" "output": "/home/m/dev/electronics/inverter/sw_linux/build/inverter"
}, },
{ {
"arguments": [ "arguments": [
@ -104,13 +81,12 @@
"-g", "-g",
"-Isrc/include", "-Isrc/include",
"-I/usr/include", "-I/usr/include",
"-I/usr/include/libftdi1",
"-o", "-o",
"build/inverter", "build/inverter",
"src/timer_utils.c" "src/timer_utils.c"
], ],
"directory": "/home/m/dev/electronics/inverter/sw_ftdi", "directory": "/home/m/dev/electronics/inverter/sw_linux",
"file": "/home/m/dev/electronics/inverter/sw_ftdi/src/timer_utils.c", "file": "/home/m/dev/electronics/inverter/sw_linux/src/timer_utils.c",
"output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter" "output": "/home/m/dev/electronics/inverter/sw_linux/build/inverter"
} }
] ]

View File

@ -5,61 +5,65 @@
bdg_sm_state_t bridge_state; bdg_sm_state_t bridge_state;
sm_trans_t bridge_sm_transitions[] = {{.from_state = BRIDGE_SM_START, sm_trans_t bridge_sm_transitions[] = {
.to_state = BRIDGE_SM_INIT_IO, {.from_state = BRIDGE_SM_START,
.trans_fct = brdg_BothGndOn}, .to_state = BRIDGE_SM_INIT_IO,
{.from_state = BRIDGE_SM_INIT_IO, .trans_fct = brdg_BothGndOn},
.to_state = HB1_PWR_ON, {.from_state = BRIDGE_SM_INIT_IO,
.trans_fct = brdg_Hb1PowerOn}, .to_state = HB1_PWR_ON,
{.from_state = HB1_GND_ON, .trans_fct = brdg_Hb1PowerOn},
.to_state = HB1_PWR_ON, {.from_state = HB1_GND_ON,
.trans_fct = brdg_Hb1PowerOn}, .to_state = HB1_PWR_ON,
{.from_state = HB1_PWR_ON, .trans_fct = brdg_Hb1PowerOn},
.to_state = HB1_PWR_OFF, {.from_state = HB1_PWR_ON,
.trans_fct = brdg_Hb1PowerOff}, .to_state = HB1_PWR_OFF,
{.from_state = HB1_PWR_OFF, .trans_fct = brdg_Hb1PowerOff},
.to_state = HB2_GND_ON, {.from_state = HB1_PWR_OFF,
.trans_fct = brdg_Hb2GndOn}, .to_state = HB2_GND_ON,
{.from_state = HB2_GND_ON, .trans_fct = brdg_Hb2GndOn},
.to_state = HB2_PWR_ON, {.from_state = HB2_GND_ON,
.trans_fct = brdg_Hb2PowerOn}, .to_state = HB2_PWR_ON,
{.from_state = HB2_PWR_ON, .trans_fct = brdg_Hb2PowerOn},
.to_state = HB2_PWR_OFF, {.from_state = HB2_PWR_ON,
.trans_fct = brdg_Hb2PowerOff}, .to_state = HB2_PWR_OFF,
{.from_state = HB2_PWR_OFF, .trans_fct = brdg_Hb2PowerOff},
.to_state = HB2_GND_ON, {.from_state = HB2_PWR_OFF,
.trans_fct = brdg_Hb2GndOn}}; .to_state = HB2_GND_ON,
.trans_fct = brdg_Hb2GndOn},
};
sm_t bridge_statemachine = {.current_state = BRIDGE_SM_START, sm_t bridge_statemachine = {
.transitions = bridge_sm_transitions}; .current_state = BRIDGE_SM_START,
.transitions = bridge_sm_transitions,
};
void brdg_BothGndOn(void) { void brdg_BothGndOn(void) {
/* Turn both GND transistors on */ /* Turn both GND transistors on */
setPin(D0); setPin(GND_HALFBRIDGE_1);
setPin(D2); setPin(GND_HALFBRIDGE_2);
/* Turn both PWR transistors off */ /* Turn both PWR transistors off */
unsetPin(D3); unsetPin(PWR_HALFBRIDGE_1);
unsetPin(D1); unsetPin(PWR_HALFBRIDGE_2);
} }
void brdg_Hb1GndOn(void) { void brdg_Hb1GndOn(void) {
/* We turn GND of half-bridge 1 on */ /* We turn GND of half-bridge 1 on */
setPin(D0); setPin(GND_HALFBRIDGE_1);
/* And then we turn GND of half-bridge 2 off */ /* And then we turn GND of half-bridge 2 off */
unsetPin(D2); unsetPin(GND_HALFBRIDGE_2);
} }
void brdg_Hb1PowerOn(void) { setPin(D3); } void brdg_Hb1PowerOn(void) { setPin(PWR_HALFBRIDGE_1); }
void brdg_Hb1PowerOff(void) { unsetPin(D3); } void brdg_Hb1PowerOff(void) { unsetPin(PWR_HALFBRIDGE_1); }
void brdg_Hb2GndOn(void) { void brdg_Hb2GndOn(void) {
setPin(D0); setPin(GND_HALFBRIDGE_2);
unsetPin(D2); unsetPin(GND_HALFBRIDGE_1);
} }
void brdg_Hb2PowerOn(void) { setPin(D1); } void brdg_Hb2PowerOn(void) { setPin(PWR_HALFBRIDGE_2); }
void brdg_Hb2PowerOff(void) { unsetPin(D1); } void brdg_Hb2PowerOff(void) { unsetPin(PWR_HALFBRIDGE_2); }

View File

@ -1,42 +0,0 @@
#include <ftdi.h>
#include <stdio.h>
#include <stdlib.h>
#include "ftdi_utils.h"
struct ftdi_context *ftdi;
int init_ftdi() {
struct ftdi_version_info version;
uint8_t pins;
unsigned int chipid;
if ((ftdi = ftdi_new()) == 0) {
fprintf(stderr, "ftdi_new failed\n");
return EXIT_FAILURE;
}
version = ftdi_get_library_version();
printf("Initialized libftdi %s (major: %d, minor: %d, micro: %d, snapshot "
"ver: %s)\n",
version.version_str, version.major, version.minor, version.micro,
version.snapshot_str);
int ret = ftdi_usb_open(ftdi, 0x0403, 0x6014);
if (ret < 0) {
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret,
ftdi_get_error_string(ftdi));
ftdi_free(ftdi);
return EXIT_FAILURE;
}
// Enable bitbang mode
ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG);
printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(ftdi, &chipid));
printf("FTDI chipid: %X\n", chipid);
ftdi_read_pins(ftdi, &pins);
printf("Pins state: %X\n", pins);
return EXIT_SUCCESS;
}

View File

@ -1,3 +0,0 @@
extern struct ftdi_context *ftdi;
int init_ftdi();

View File

@ -0,0 +1 @@
#define UNUSED __attribute__((unused))

1
src/include/misc_utils.h Normal file
View File

@ -0,0 +1 @@
unsigned long abs_long(long);

View File

@ -1,10 +1,11 @@
#include <inttypes.h> #include <inttypes.h>
#define D0 0 #define GND_HALFBRIDGE_1 0
#define D1 1 #define GND_HALFBRIDGE_2 1
#define D2 2 #define PWR_HALFBRIDGE_1 2
#define D3 3 #define PWR_HALFBRIDGE_2 3
void togglePin(unsigned int pin); void togglePin(unsigned int pin);
void unsetPin(unsigned int pin); void unsetPin(unsigned int pin);
void setPin(unsigned int pin); void setPin(unsigned int pin);

View File

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

View File

@ -7,10 +7,11 @@ typedef struct {
struct sigevent sevp; struct sigevent sevp;
struct itimerspec times; struct itimerspec times;
struct timespec runsSince; struct timespec runsSince;
void (*funcToBeCalled)(void); void (*funcToBeCalled)(void *);
void *args;
} tmr_ctx; } tmr_ctx;
void tmr_initTimer(tmr_ctx *ctx); void tmr_initTimer(tmr_ctx *ctx);
void tmr_handleTimerOverflow(union sigval); void tmr_handleTimerOverflow(union sigval);
int tmr_callEvery(void (*func)(void), long nsec); int tmr_callEvery(void (*func)(void *), void *args, long nsec);
void tmr_add_ns_to_current_time(struct itimerspec *current, long nsecs); void tmr_add_ns_to_current_time(struct itimerspec *current, long nsecs);

View File

@ -1,42 +1,33 @@
#include <ftdi.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "ftdi_utils.h" #include "global_utils.h"
#include "pins.h" #include "pins.h"
#include "time_scales.h" #include "time_scales.h"
#include "timer_utils.h" #include "timer_utils.h"
static uint64_t counter = 0; struct itimerspec lastTime;
void test1(void) {
printf("%lu\n", counter); void printTime(UNUSED void *arg) {
struct itimerspec time;
clock_gettime(CLOCK_REALTIME, &time.it_value);
fflush(stdout); fflush(stdout);
counter++;
lastTime = time;
} }
void test2(void) { int main(void) {
printf("This is test2\n");
fflush(stdout);
}
void togglePin1(void) { togglePin(D0); }
int main() {
printf("Program started\n"); printf("Program started\n");
if (init_ftdi()) { clock_gettime(CLOCK_REALTIME, &lastTime.it_value);
printf("Error initializing ftdi\n"); if (tmr_callEvery(printTime, NULL, 1 * MSEC)) {
return EXIT_FAILURE;
}
if (tmr_callEvery(togglePin1, 10 * USEC)) {
printf("Error starting timer for togglePin\n"); printf("Error starting timer for togglePin\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
while (1) { while (1) {
sleep(10000); sleep(100);
} }
return 0; return 0;

8
src/misc_utils.c Normal file
View File

@ -0,0 +1,8 @@
#include "misc_utils.h"
unsigned long abs_long(long a) {
if (a > 0)
return a;
else
return -a;
}

View File

@ -1,44 +1,12 @@
#include <ftdi.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include "ftdi_utils.h"
#include "pins.h" #include "pins.h"
uint8_t pin_state_cache = 0x0; uint8_t pin_state_cache = 0x0;
void setPin(unsigned int pin) { void setPin(unsigned int pin) {}
uint8_t pin_state = pin_state_cache | (1 << pin);
int ret = ftdi_write_data(ftdi, &pin_state, 1);
if (ret < 0) { void unsetPin(unsigned int pin) {}
fprintf(stderr, "setting pin failed for data 0x%x, error %d (%s)\n",
1 << pin, ret, ftdi_get_error_string(ftdi));
} else {
pin_state_cache = pin_state;
}
}
void unsetPin(unsigned int pin) { void togglePin(unsigned int pin) {}
uint8_t pin_state = pin_state_cache & ~(1 << pin);
int ret = ftdi_write_data(ftdi, &pin_state, 1);
if (ret < 0) {
fprintf(stderr, "unsetting pin failed for data 0x%x, error %d (%s)\n",
1 << pin, ret, ftdi_get_error_string(ftdi));
} else {
pin_state_cache = pin_state;
}
}
void togglePin(unsigned int pin) {
uint8_t pin_state = pin_state_cache ^ (1 << pin);
int ret = ftdi_write_data(ftdi, &pin_state, 1);
if (ret < 0) {
fprintf(stderr, "toggling pin failed for data 0x%x, error %d (%s)\n",
1 << pin, ret, ftdi_get_error_string(ftdi));
} else {
pin_state_cache = pin_state;
}
}

View File

@ -4,15 +4,23 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "misc_utils.h"
#include "time_scales.h" #include "time_scales.h"
#include "timer_utils.h" #include "timer_utils.h"
int tmr_registry_index = 0; typedef struct {
tmr_ctx *tmr_registry[10]; int index;
tmr_ctx *req[10];
} timer_registry_t;
static timer_registry_t timer_registry = {
.index = 0,
.req = {0},
};
void tmr_add_to_registry(tmr_ctx *ctx) { void tmr_add_to_registry(tmr_ctx *ctx) {
tmr_registry[tmr_registry_index] = ctx; timer_registry.req[timer_registry.index] = ctx;
tmr_registry_index++; timer_registry.index++;
} }
void tmr_initTimer(tmr_ctx *ctx) { void tmr_initTimer(tmr_ctx *ctx) {
@ -34,24 +42,23 @@ void tmr_initTimer(tmr_ctx *ctx) {
void tmr_handleTimerOverflow(sigval_t ctx_par) { void tmr_handleTimerOverflow(sigval_t ctx_par) {
tmr_ctx *ctx = (tmr_ctx *)ctx_par.sival_ptr; tmr_ctx *ctx = (tmr_ctx *)ctx_par.sival_ptr;
ctx->funcToBeCalled(); ctx->funcToBeCalled(ctx->args);
tmr_add_ns_to_current_time(&ctx->times, ctx->interval); tmr_add_ns_to_current_time(&ctx->times, ctx->interval);
timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL); timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL);
} }
int tmr_callEvery(void (*func)(void), long nsec) { int tmr_callEvery(void (*func)(void *), void *args, long nsec) {
tmr_ctx *tmr = calloc(1, sizeof(tmr_ctx));
// tmr_ctx *tmr = malloc(sizeof(tmr_ctx));
tmr_add_to_registry(tmr);
tmr->interval = nsec;
tmr->funcToBeCalled = func;
if (!func) if (!func)
return -1; return -1;
tmr_ctx *tmr = calloc(1, sizeof(tmr_ctx));
tmr->interval = nsec;
tmr->funcToBeCalled = func;
tmr->args = args;
tmr_add_to_registry(tmr);
tmr_initTimer(tmr); tmr_initTimer(tmr);
return 0; return 0;
@ -64,4 +71,16 @@ void tmr_add_ns_to_current_time(struct itimerspec *time, long nsec) {
} else { } else {
time->it_value.tv_nsec += nsec; time->it_value.tv_nsec += nsec;
} }
} }
struct timespec tmr_getAbsoluteDiffBetween(struct timespec a,
struct timespec b) {
struct timespec diff;
diff.tv_sec = abs_long(a.tv_sec - b.tv_sec);
if (diff.tv_sec != 0) {
}
return diff;
}