diff --git a/Makefile b/Makefile index b6d58a7..7e29e54 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ TEST_TARGETNAME = test TEST_BUILDFOLDER = ./test/build/ 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 CPPFLAGS = @@ -17,13 +17,13 @@ $(TARGETNAME): $(TARGET) $(TARGET): $(SRC) mkdir -p $(BUILDFOLDER) - cc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1 + cc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm $(TEST_TARGETNAME): $(TEST_TARGET) $(TEST_TARGET): $(TEST_SRC) mkdir -p $(TEST_BUILDFOLDER) - cc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1 -lcunit + cc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lcunit clean: rm -fr ./src/*.o $(BUILDFOLDER)* diff --git a/compile_commands.json b/compile_commands.json index 695a66f..f38487f 100644 --- a/compile_commands.json +++ b/compile_commands.json @@ -9,14 +9,13 @@ "-g", "-Isrc/include", "-I/usr/include", - "-I/usr/include/libftdi1", "-o", "build/inverter", "src/bridge_utils.c" ], - "directory": "/home/m/dev/electronics/inverter/sw_ftdi", - "file": "/home/m/dev/electronics/inverter/sw_ftdi/src/bridge_utils.c", - "output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter" + "directory": "/home/m/dev/electronics/inverter/sw_linux", + "file": "/home/m/dev/electronics/inverter/sw_linux/src/bridge_utils.c", + "output": "/home/m/dev/electronics/inverter/sw_linux/build/inverter" }, { "arguments": [ @@ -28,33 +27,13 @@ "-g", "-Isrc/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", "build/inverter", "src/main.c" ], - "directory": "/home/m/dev/electronics/inverter/sw_ftdi", - "file": "/home/m/dev/electronics/inverter/sw_ftdi/src/main.c", - "output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter" + "directory": "/home/m/dev/electronics/inverter/sw_linux", + "file": "/home/m/dev/electronics/inverter/sw_linux/src/main.c", + "output": "/home/m/dev/electronics/inverter/sw_linux/build/inverter" }, { "arguments": [ @@ -66,14 +45,13 @@ "-g", "-Isrc/include", "-I/usr/include", - "-I/usr/include/libftdi1", "-o", "build/inverter", "src/pins.c" ], - "directory": "/home/m/dev/electronics/inverter/sw_ftdi", - "file": "/home/m/dev/electronics/inverter/sw_ftdi/src/pins.c", - "output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter" + "directory": "/home/m/dev/electronics/inverter/sw_linux", + "file": "/home/m/dev/electronics/inverter/sw_linux/src/pins.c", + "output": "/home/m/dev/electronics/inverter/sw_linux/build/inverter" }, { "arguments": [ @@ -85,14 +63,13 @@ "-g", "-Isrc/include", "-I/usr/include", - "-I/usr/include/libftdi1", "-o", "build/inverter", "src/statemachine.c" ], - "directory": "/home/m/dev/electronics/inverter/sw_ftdi", - "file": "/home/m/dev/electronics/inverter/sw_ftdi/src/statemachine.c", - "output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter" + "directory": "/home/m/dev/electronics/inverter/sw_linux", + "file": "/home/m/dev/electronics/inverter/sw_linux/src/statemachine.c", + "output": "/home/m/dev/electronics/inverter/sw_linux/build/inverter" }, { "arguments": [ @@ -104,13 +81,12 @@ "-g", "-Isrc/include", "-I/usr/include", - "-I/usr/include/libftdi1", "-o", "build/inverter", "src/timer_utils.c" ], - "directory": "/home/m/dev/electronics/inverter/sw_ftdi", - "file": "/home/m/dev/electronics/inverter/sw_ftdi/src/timer_utils.c", - "output": "/home/m/dev/electronics/inverter/sw_ftdi/build/inverter" + "directory": "/home/m/dev/electronics/inverter/sw_linux", + "file": "/home/m/dev/electronics/inverter/sw_linux/src/timer_utils.c", + "output": "/home/m/dev/electronics/inverter/sw_linux/build/inverter" } ] diff --git a/src/bridge_utils.c b/src/bridge_utils.c index 05e46a2..e4ad31d 100644 --- a/src/bridge_utils.c +++ b/src/bridge_utils.c @@ -5,61 +5,65 @@ bdg_sm_state_t bridge_state; -sm_trans_t bridge_sm_transitions[] = {{.from_state = BRIDGE_SM_START, - .to_state = BRIDGE_SM_INIT_IO, - .trans_fct = brdg_BothGndOn}, - {.from_state = BRIDGE_SM_INIT_IO, - .to_state = HB1_PWR_ON, - .trans_fct = brdg_Hb1PowerOn}, - {.from_state = HB1_GND_ON, - .to_state = HB1_PWR_ON, - .trans_fct = brdg_Hb1PowerOn}, - {.from_state = HB1_PWR_ON, - .to_state = HB1_PWR_OFF, - .trans_fct = brdg_Hb1PowerOff}, - {.from_state = HB1_PWR_OFF, - .to_state = HB2_GND_ON, - .trans_fct = brdg_Hb2GndOn}, - {.from_state = HB2_GND_ON, - .to_state = HB2_PWR_ON, - .trans_fct = brdg_Hb2PowerOn}, - {.from_state = HB2_PWR_ON, - .to_state = HB2_PWR_OFF, - .trans_fct = brdg_Hb2PowerOff}, - {.from_state = HB2_PWR_OFF, - .to_state = HB2_GND_ON, - .trans_fct = brdg_Hb2GndOn}}; +sm_trans_t bridge_sm_transitions[] = { + {.from_state = BRIDGE_SM_START, + .to_state = BRIDGE_SM_INIT_IO, + .trans_fct = brdg_BothGndOn}, + {.from_state = BRIDGE_SM_INIT_IO, + .to_state = HB1_PWR_ON, + .trans_fct = brdg_Hb1PowerOn}, + {.from_state = HB1_GND_ON, + .to_state = HB1_PWR_ON, + .trans_fct = brdg_Hb1PowerOn}, + {.from_state = HB1_PWR_ON, + .to_state = HB1_PWR_OFF, + .trans_fct = brdg_Hb1PowerOff}, + {.from_state = HB1_PWR_OFF, + .to_state = HB2_GND_ON, + .trans_fct = brdg_Hb2GndOn}, + {.from_state = HB2_GND_ON, + .to_state = HB2_PWR_ON, + .trans_fct = brdg_Hb2PowerOn}, + {.from_state = HB2_PWR_ON, + .to_state = HB2_PWR_OFF, + .trans_fct = brdg_Hb2PowerOff}, + {.from_state = HB2_PWR_OFF, + .to_state = HB2_GND_ON, + .trans_fct = brdg_Hb2GndOn}, +}; -sm_t bridge_statemachine = {.current_state = BRIDGE_SM_START, - .transitions = bridge_sm_transitions}; +sm_t bridge_statemachine = { + .current_state = BRIDGE_SM_START, + .transitions = bridge_sm_transitions, +}; void brdg_BothGndOn(void) { /* Turn both GND transistors on */ - setPin(D0); - setPin(D2); + setPin(GND_HALFBRIDGE_1); + setPin(GND_HALFBRIDGE_2); /* Turn both PWR transistors off */ - unsetPin(D3); - unsetPin(D1); + unsetPin(PWR_HALFBRIDGE_1); + unsetPin(PWR_HALFBRIDGE_2); } void brdg_Hb1GndOn(void) { /* We turn GND of half-bridge 1 on */ - setPin(D0); + setPin(GND_HALFBRIDGE_1); /* 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) { - setPin(D0); - unsetPin(D2); + setPin(GND_HALFBRIDGE_2); + 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); } diff --git a/src/ftdi_utils.c b/src/ftdi_utils.c deleted file mode 100644 index 5d9774d..0000000 --- a/src/ftdi_utils.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include - -#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; -} \ No newline at end of file diff --git a/src/include/ftdi_utils.h b/src/include/ftdi_utils.h deleted file mode 100644 index b32f409..0000000 --- a/src/include/ftdi_utils.h +++ /dev/null @@ -1,3 +0,0 @@ -extern struct ftdi_context *ftdi; - -int init_ftdi(); \ No newline at end of file diff --git a/src/include/global_utils.h b/src/include/global_utils.h new file mode 100644 index 0000000..c783242 --- /dev/null +++ b/src/include/global_utils.h @@ -0,0 +1 @@ +#define UNUSED __attribute__((unused)) diff --git a/src/include/misc_utils.h b/src/include/misc_utils.h new file mode 100644 index 0000000..a4e228b --- /dev/null +++ b/src/include/misc_utils.h @@ -0,0 +1 @@ +unsigned long abs_long(long); diff --git a/src/include/pins.h b/src/include/pins.h index 4766f00..bd2d003 100644 --- a/src/include/pins.h +++ b/src/include/pins.h @@ -1,10 +1,11 @@ #include -#define D0 0 -#define D1 1 -#define D2 2 -#define D3 3 +#define GND_HALFBRIDGE_1 0 +#define GND_HALFBRIDGE_2 1 +#define PWR_HALFBRIDGE_1 2 +#define PWR_HALFBRIDGE_2 3 void togglePin(unsigned int pin); void unsetPin(unsigned int pin); -void setPin(unsigned int pin); \ No newline at end of file +void setPin(unsigned int pin); + diff --git a/src/include/time_scales.h b/src/include/time_scales.h index eac9cd5..05ddbee 100644 --- a/src/include/time_scales.h +++ b/src/include/time_scales.h @@ -1,8 +1,13 @@ +#define NSEC (1) + // microseconds in nanoseconds -#define USEC (1000) +#define USEC (1000 * NSEC) // milliseconds in nanoseconds #define MSEC (1000 * USEC) // seconds in nanoseconds -#define SEC (1000 * MSEC) \ No newline at end of file +#define SEC (1000 * MSEC) + + +#define NSEC_IN_SEC (1000000000) diff --git a/src/include/timer_utils.h b/src/include/timer_utils.h index 6ae597d..ae45794 100644 --- a/src/include/timer_utils.h +++ b/src/include/timer_utils.h @@ -7,10 +7,11 @@ typedef struct { struct sigevent sevp; struct itimerspec times; struct timespec runsSince; - void (*funcToBeCalled)(void); + void (*funcToBeCalled)(void *); + void *args; } tmr_ctx; void tmr_initTimer(tmr_ctx *ctx); void tmr_handleTimerOverflow(union sigval); -int tmr_callEvery(void (*func)(void), long nsec); -void tmr_add_ns_to_current_time(struct itimerspec *current, long nsecs); \ No newline at end of file +int tmr_callEvery(void (*func)(void *), void *args, long nsec); +void tmr_add_ns_to_current_time(struct itimerspec *current, long nsecs); diff --git a/src/main.c b/src/main.c index bb093b0..3565aff 100644 --- a/src/main.c +++ b/src/main.c @@ -1,42 +1,33 @@ -#include #include #include #include -#include "ftdi_utils.h" +#include "global_utils.h" #include "pins.h" #include "time_scales.h" #include "timer_utils.h" -static uint64_t counter = 0; -void test1(void) { - printf("%lu\n", counter); +struct itimerspec lastTime; + +void printTime(UNUSED void *arg) { + struct itimerspec time; + clock_gettime(CLOCK_REALTIME, &time.it_value); + fflush(stdout); - counter++; + + lastTime = time; } -void test2(void) { - printf("This is test2\n"); - fflush(stdout); -} - -void togglePin1(void) { togglePin(D0); } - -int main() { +int main(void) { printf("Program started\n"); - if (init_ftdi()) { - printf("Error initializing ftdi\n"); - return EXIT_FAILURE; - } - - if (tmr_callEvery(togglePin1, 10 * USEC)) { + clock_gettime(CLOCK_REALTIME, &lastTime.it_value); + if (tmr_callEvery(printTime, NULL, 1 * MSEC)) { printf("Error starting timer for togglePin\n"); return EXIT_FAILURE; } - while (1) { - sleep(10000); + sleep(100); } return 0; diff --git a/src/misc_utils.c b/src/misc_utils.c new file mode 100644 index 0000000..1dadf81 --- /dev/null +++ b/src/misc_utils.c @@ -0,0 +1,8 @@ +#include "misc_utils.h" + +unsigned long abs_long(long a) { + if (a > 0) + return a; + else + return -a; +} diff --git a/src/pins.c b/src/pins.c index 6981ae4..e1f6d18 100644 --- a/src/pins.c +++ b/src/pins.c @@ -1,44 +1,12 @@ -#include #include #include -#include "ftdi_utils.h" #include "pins.h" uint8_t pin_state_cache = 0x0; -void setPin(unsigned int pin) { - uint8_t pin_state = pin_state_cache | (1 << pin); - int ret = ftdi_write_data(ftdi, &pin_state, 1); +void setPin(unsigned int pin) {} - if (ret < 0) { - 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 unsetPin(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; - } -} +void togglePin(unsigned int pin) {} diff --git a/src/timer_utils.c b/src/timer_utils.c index 0807381..3e50b96 100644 --- a/src/timer_utils.c +++ b/src/timer_utils.c @@ -4,15 +4,23 @@ #include #include +#include "misc_utils.h" #include "time_scales.h" #include "timer_utils.h" -int tmr_registry_index = 0; -tmr_ctx *tmr_registry[10]; +typedef struct { + 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) { - tmr_registry[tmr_registry_index] = ctx; - tmr_registry_index++; + timer_registry.req[timer_registry.index] = ctx; + timer_registry.index++; } void tmr_initTimer(tmr_ctx *ctx) { @@ -34,24 +42,23 @@ void tmr_initTimer(tmr_ctx *ctx) { void tmr_handleTimerOverflow(sigval_t ctx_par) { 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); timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL); } -int tmr_callEvery(void (*func)(void), 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; - +int tmr_callEvery(void (*func)(void *), void *args, long nsec) { if (!func) 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); return 0; @@ -64,4 +71,16 @@ void tmr_add_ns_to_current_time(struct itimerspec *time, long nsec) { } else { time->it_value.tv_nsec += nsec; } -} \ No newline at end of file +} + +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; +}