Many changes. Timer works now (using calloc instead of malloc). Adding test framework.

This commit is contained in:
Marco 2023-03-03 18:17:00 +01:00
parent 1f6c94746c
commit f8e09d656d
7 changed files with 43 additions and 39 deletions

View File

@ -12,7 +12,7 @@
"intelliSenseMode": "linux-gcc-x64", "intelliSenseMode": "linux-gcc-x64",
"browse": { "browse": {
"path": [ "path": [
"/usr/include", "/usr/include/**",
"${workspaceFolder}/**" "${workspaceFolder}/**"
] ]
} }

View File

@ -2,17 +2,30 @@ TARGETNAME = inverter
BUILDFOLDER = ./build/ BUILDFOLDER = ./build/
TARGET = $(BUILDFOLDER)$(TARGETNAME) TARGET = $(BUILDFOLDER)$(TARGETNAME)
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 -I/usr/include/libftdi1
CFLAGS = -Wall -Wpedantic -Wextra -Os CFLAGS = -Wall -Wpedantic -Wextra -g
CPPFLAGS = CPPFLAGS =
SRC = $(wildcard src/*.c) SRC = $(wildcard src/*.c)
TEST_SRC = $(filter-out src/main.c, $(SRC) $(wildcard test/*.c))
all: $(TARGET) $(TARGETNAME): $(TARGET)
$(TARGET): $(SRC) $(TARGET): $(SRC)
mkdir -p build mkdir -p build
gcc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1 -g cc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1
$(TEST_TARGETNAME): $(TEST_TARGET)
$(TEST_TARGET): $(TEST_SRC)
echo $(TEST_SRC)
mkdir -p $(TEST_BUILDFOLDER)
cc -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(INC) -lm -lftdi1 -lcunit
clean: clean:
rm -fr ./src/*.o ./build/ rm -fr ./src/*.o ./build/*
rm -fr $(TEST_BUILDFOLDER)*

View File

@ -39,45 +39,45 @@ sm_t bridge_statemachine = {
void brdg_BothGndOn(void) void brdg_BothGndOn(void)
{ {
/* Turn both GND transistors on */ /* Turn both GND transistors on */
setPin(PB6); setPin(D0);
setPin(PB4); setPin(D2);
/* Turn both PWR transistors off */ /* Turn both PWR transistors off */
unsetPin(PB7); unsetPin(D3);
unsetPin(PB5); unsetPin(D1);
} }
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(PB6); setPin(D0);
/* And then we turn GND of half-bridge 2 off */ /* And then we turn GND of half-bridge 2 off */
unsetPin(PB4); unsetPin(D2);
} }
void brdg_Hb1PowerOn(void) void brdg_Hb1PowerOn(void)
{ {
setPin(PB7); setPin(D3);
} }
void brdg_Hb1PowerOff(void) void brdg_Hb1PowerOff(void)
{ {
unsetPin(PB7); unsetPin(D3);
} }
void brdg_Hb2GndOn(void) void brdg_Hb2GndOn(void)
{ {
setPin(PB4); setPin(D0);
unsetPin(PB6); unsetPin(D2);
} }
void brdg_Hb2PowerOn(void) void brdg_Hb2PowerOn(void)
{ {
setPin(PB5); setPin(D1);
} }
void brdg_Hb2PowerOff(void) void brdg_Hb2PowerOff(void)
{ {
unsetPin(PB5); unsetPin(D1);
} }

View File

@ -1,9 +1,9 @@
#include <inttypes.h> #include <inttypes.h>
#define PB6 1 #define D0 0
#define PB4 2 #define D1 1
#define PB5 3 #define D2 2
#define PB7 4 #define D3 3
void togglePin(unsigned int pin); void togglePin(unsigned int pin);
void unsetPin(unsigned int pin); void unsetPin(unsigned int pin);

View File

@ -14,4 +14,4 @@ typedef struct
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), long nsec);
void tmr_add_ns_to_current_time(struct itimerspec *current, long usecs); void tmr_add_ns_to_current_time(struct itimerspec *current, long nsecs);

View File

@ -12,43 +12,32 @@ static uint64_t counter = 0;
void test1(void) void test1(void)
{ {
printf("%lu\n", counter); printf("%lu\n", counter);
fflush(stdout);
counter++; counter++;
} }
void test2(void) void test2(void)
{ {
printf("This is test2\n"); printf("This is test2\n");
fflush(stdout);
} }
void togglePin1(void) void togglePin1(void)
{ {
printf("toggling pin\n"); togglePin(D0);
setPin(0);
} }
int main() int main()
{ {
printf("Program started\n"); printf("Program started\n");
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()) if (init_ftdi())
{ {
printf("Error initializing ftdi\n"); printf("Error initializing ftdi\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (tmr_callEvery(togglePin1, 1 * SEC)) 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;

View File

@ -10,7 +10,8 @@
int tmr_registry_index = 0; int tmr_registry_index = 0;
tmr_ctx *tmr_registry[10]; tmr_ctx *tmr_registry[10];
void tmr_add_to_registry(tmr_ctx* ctx) { void tmr_add_to_registry(tmr_ctx *ctx)
{
tmr_registry[tmr_registry_index] = ctx; tmr_registry[tmr_registry_index] = ctx;
tmr_registry_index++; tmr_registry_index++;
} }
@ -44,7 +45,8 @@ void tmr_handleTimerOverflow(sigval_t ctx_par)
int tmr_callEvery(void (*func)(void), long nsec) int tmr_callEvery(void (*func)(void), long nsec)
{ {
tmr_ctx *tmr = malloc(sizeof(tmr_ctx)); tmr_ctx *tmr = calloc(1,sizeof(tmr_ctx));
// tmr_ctx *tmr = malloc(sizeof(tmr_ctx));
tmr_add_to_registry(tmr); tmr_add_to_registry(tmr);