Format and fix linter warnings

This commit is contained in:
Marco 2023-12-27 22:54:29 +01:00
parent fd28d9815a
commit 99684ac531
11 changed files with 285 additions and 351 deletions

View File

@ -1,83 +1,65 @@
#include "bridge_utils.h"
#include "statemachine.h"
#include "pins.h"
#include "statemachine.h"
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);
void brdg_BothGndOn(void) {
/* Turn both GND transistors on */
setPin(D0);
setPin(D2);
/* Turn both PWR transistors off */
unsetPin(D3);
unsetPin(D1);
/* Turn both PWR transistors off */
unsetPin(D3);
unsetPin(D1);
}
void brdg_Hb1GndOn(void)
{
/* We turn GND of half-bridge 1 on */
setPin(D0);
void brdg_Hb1GndOn(void) {
/* We turn GND of half-bridge 1 on */
setPin(D0);
/* And then we turn GND of half-bridge 2 off */
unsetPin(D2);
/* And then we turn GND of half-bridge 2 off */
unsetPin(D2);
}
void brdg_Hb1PowerOn(void)
{
setPin(D3);
void brdg_Hb1PowerOn(void) { setPin(D3); }
void brdg_Hb1PowerOff(void) { unsetPin(D3); }
void brdg_Hb2GndOn(void) {
setPin(D0);
unsetPin(D2);
}
void brdg_Hb1PowerOff(void)
{
unsetPin(D3);
}
void brdg_Hb2PowerOn(void) { setPin(D1); }
void brdg_Hb2GndOn(void)
{
setPin(D0);
unsetPin(D2);
}
void brdg_Hb2PowerOn(void)
{
setPin(D1);
}
void brdg_Hb2PowerOff(void)
{
unsetPin(D1);
}
void brdg_Hb2PowerOff(void) { unsetPin(D1); }

View File

@ -6,38 +6,37 @@
struct ftdi_context *ftdi;
int init_ftdi()
{
struct ftdi_version_info version;
uint8_t pins;
unsigned int chipid;
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);
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;
}
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);
// 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("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);
printf("Pins state: %X\n", pins);
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,16 +1,14 @@
typedef enum
{
BRIDGE_SM_START,
BRIDGE_SM_INIT_IO,
HB1_GND_ON,
HB1_PWR_ON,
HB1_PWR_OFF,
HB2_GND_ON,
HB2_PWR_ON,
HB2_PWR_OFF
typedef enum {
BRIDGE_SM_START,
BRIDGE_SM_INIT_IO,
HB1_GND_ON,
HB1_PWR_ON,
HB1_PWR_OFF,
HB2_GND_ON,
HB2_PWR_ON,
HB2_PWR_OFF
} bdg_sm_state_t;
void brdg_BothGndOn(void);
void brdg_Hb1GndOn(void);

View File

@ -1,16 +1,14 @@
typedef int sm_state_t;
typedef struct
{
sm_state_t from_state;
sm_state_t to_state;
void (*trans_fct)(void);
typedef struct {
sm_state_t from_state;
sm_state_t to_state;
void (*trans_fct)(void);
} sm_trans_t;
typedef struct
{
sm_state_t current_state;
sm_trans_t *transitions;
typedef struct {
sm_state_t current_state;
sm_trans_t *transitions;
} sm_t;
void sm_handleTransition(sm_t *statemachine, sm_state_t to);

View File

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

View File

@ -1,14 +1,13 @@
#include <time.h>
#include <signal.h>
#include <time.h>
typedef struct
{
timer_t timerid;
long interval;
struct sigevent sevp;
struct itimerspec times;
struct timespec runsSince;
void (*funcToBeCalled)(void);
typedef struct {
timer_t timerid;
long interval;
struct sigevent sevp;
struct itimerspec times;
struct timespec runsSince;
void (*funcToBeCalled)(void);
} tmr_ctx;
void tmr_initTimer(tmr_ctx *ctx);

View File

@ -1,52 +1,43 @@
#include <ftdi.h>
#include <stdio.h>
#include <stdlib.h>
#include <ftdi.h>
#include <unistd.h>
#include "ftdi_utils.h"
#include "timer_utils.h"
#include "time_scales.h"
#include "pins.h"
#include "time_scales.h"
#include "timer_utils.h"
static uint64_t counter = 0;
void test1(void)
{
printf("%lu\n", counter);
fflush(stdout);
counter++;
void test1(void) {
printf("%lu\n", counter);
fflush(stdout);
counter++;
}
void test2(void)
{
printf("This is test2\n");
fflush(stdout);
void test2(void) {
printf("This is test2\n");
fflush(stdout);
}
void togglePin1(void)
{
togglePin(D0);
}
int main()
{
printf("Program started\n");
if (init_ftdi())
{
printf("Error initializing ftdi\n");
return EXIT_FAILURE;
}
if (tmr_callEvery(togglePin1, 10 * USEC))
{
printf("Error starting timer for togglePin\n");
return EXIT_FAILURE;
}
while (1)
{
sleep(10000);
}
return 0;
void togglePin1(void) { togglePin(D0); }
int main() {
printf("Program started\n");
if (init_ftdi()) {
printf("Error initializing ftdi\n");
return EXIT_FAILURE;
}
if (tmr_callEvery(togglePin1, 10 * USEC)) {
printf("Error starting timer for togglePin\n");
return EXIT_FAILURE;
}
while (1) {
sleep(10000);
}
return 0;
}

View File

@ -1,53 +1,44 @@
#include <ftdi.h>
#include <inttypes.h>
#include <stdio.h>
#include <ftdi.h>
#include "pins.h"
#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) {
uint8_t pin_state = pin_state_cache | (1 << pin);
int ret = ftdi_write_data(ftdi, &pin_state, 1);
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;
}
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)
{
uint8_t pin_state = pin_state_cache & ~(1 << pin);
int ret = ftdi_write_data(ftdi, &pin_state, 1);
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;
}
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);
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;
}
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

@ -1,7 +1,6 @@
#include "statemachine.h"
void sm_handleTransition(sm_t *statemachine, sm_state_t to)
{
statemachine = statemachine;
to = to;
void sm_handleTransition(sm_t *statemachine, sm_state_t to) {
statemachine = statemachine;
to = to;
}

View File

@ -1,8 +1,8 @@
#include <time.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "time_scales.h"
#include "timer_utils.h"
@ -10,66 +10,58 @@
int tmr_registry_index = 0;
tmr_ctx *tmr_registry[10];
void tmr_add_to_registry(tmr_ctx *ctx)
{
tmr_registry[tmr_registry_index] = ctx;
tmr_registry_index++;
void tmr_add_to_registry(tmr_ctx *ctx) {
tmr_registry[tmr_registry_index] = ctx;
tmr_registry_index++;
}
void tmr_initTimer(tmr_ctx *ctx)
{
ctx->sevp = (struct sigevent){
.sigev_notify = SIGEV_THREAD,
.sigev_notify_function = tmr_handleTimerOverflow,
.sigev_value.sival_ptr = ctx,
};
void tmr_initTimer(tmr_ctx *ctx) {
ctx->sevp = (struct sigevent){
.sigev_notify = SIGEV_THREAD,
.sigev_notify_function = tmr_handleTimerOverflow,
.sigev_value.sival_ptr = ctx,
};
timer_create(CLOCK_MONOTONIC, &ctx->sevp, &ctx->timerid);
clock_gettime(CLOCK_MONOTONIC, &ctx->times.it_value);
timer_create(CLOCK_MONOTONIC, &ctx->sevp, &ctx->timerid);
clock_gettime(CLOCK_MONOTONIC, &ctx->times.it_value);
ctx->runsSince.tv_sec = ctx->times.it_value.tv_sec;
ctx->runsSince.tv_nsec = ctx->times.it_value.tv_nsec;
ctx->runsSince.tv_sec = ctx->times.it_value.tv_sec;
ctx->runsSince.tv_nsec = ctx->times.it_value.tv_nsec;
tmr_add_ns_to_current_time(&ctx->times, ctx->interval);
timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL);
tmr_add_ns_to_current_time(&ctx->times, ctx->interval);
timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL);
}
void tmr_handleTimerOverflow(sigval_t ctx_par)
{
tmr_ctx *ctx = (tmr_ctx *)ctx_par.sival_ptr;
ctx->funcToBeCalled();
void tmr_handleTimerOverflow(sigval_t ctx_par) {
tmr_ctx *ctx = (tmr_ctx *)ctx_par.sival_ptr;
ctx->funcToBeCalled();
tmr_add_ns_to_current_time(&ctx->times, ctx->interval);
timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL);
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));
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_add_to_registry(tmr);
tmr->interval = nsec;
tmr->funcToBeCalled = func;
tmr->interval = nsec;
tmr->funcToBeCalled = func;
if (!func)
return -1;
if (!func)
return -1;
tmr_initTimer(tmr);
tmr_initTimer(tmr);
return 0;
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;
}
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;
}
}

View File

@ -1,115 +1,100 @@
#include <inttypes.h>
#include <stdio.h>
#include <time.h>
#include <inttypes.h>
#include "CUnit/Basic.h"
#include "timer_utils.h"
#include "time_scales.h"
#include "timer_utils.h"
int init_suite1(void)
{
return 0;
int init_suite1(void) { return 0; }
int clean_suite1(void) { return 0; }
void test_add_time_1(void) {
struct itimerspec test_time = {.it_value = {.tv_nsec = 0, .tv_sec = 0}};
tmr_add_ns_to_current_time(&test_time, 1);
CU_ASSERT(test_time.it_value.tv_nsec == 1);
}
int clean_suite1(void)
{
return 0;
void test_add_time_2(void) {
struct itimerspec test_time = {
.it_value = {.tv_nsec = 999999999, .tv_sec = 0}};
tmr_add_ns_to_current_time(&test_time, 1);
CU_ASSERT(test_time.it_value.tv_sec == 1);
CU_ASSERT(test_time.it_value.tv_nsec == 0);
}
void test_add_time_1(void)
{
struct itimerspec test_time = {
.it_value = {
.tv_nsec = 0,
.tv_sec = 0}};
tmr_add_ns_to_current_time(&test_time, 1);
CU_ASSERT(test_time.it_value.tv_nsec == 1);
void test_add_time_3(void) {
struct itimerspec test_time = {
.it_value = {.tv_nsec = 999999111, .tv_sec = 0}};
tmr_add_ns_to_current_time(&test_time, 1000);
CU_ASSERT(test_time.it_value.tv_sec == 1);
CU_ASSERT(test_time.it_value.tv_nsec == 111);
}
void test_add_time_2(void)
{
struct itimerspec test_time = {
.it_value = {
.tv_nsec = 999999999,
.tv_sec = 0}};
tmr_add_ns_to_current_time(&test_time, 1);
CU_ASSERT(test_time.it_value.tv_sec == 1);
CU_ASSERT(test_time.it_value.tv_nsec == 0);
void test_add_time_4(void) {
const uint32_t start = 999999990;
struct itimerspec time = {.it_value = {.tv_nsec = start, .tv_sec = 0}};
for (uint32_t i = start; i < 999999999; i++) {
tmr_add_ns_to_current_time(&time, 1);
printf("%u:\t", i);
printf("%ld,%ld\n", time.it_value.tv_sec, time.it_value.tv_nsec);
CU_ASSERT(time.it_value.tv_nsec == i + 1);
}
tmr_add_ns_to_current_time(&time, 1);
printf("1000000000:\t");
printf("%ld,%ld\n", time.it_value.tv_sec, time.it_value.tv_nsec);
CU_ASSERT(time.it_value.tv_nsec == 0);
CU_ASSERT(time.it_value.tv_sec == 1);
}
void test_add_time_3(void)
{
struct itimerspec test_time = {
.it_value = {
.tv_nsec = 999999111,
.tv_sec = 0}};
tmr_add_ns_to_current_time(&test_time, 1000);
CU_ASSERT(test_time.it_value.tv_sec == 1);
CU_ASSERT(test_time.it_value.tv_nsec == 111);
void test_big_number(void) {
struct itimerspec time = {
.it_value =
{
.tv_nsec = 999999999,
.tv_sec = 0,
},
};
tmr_add_ns_to_current_time(&time, 1 * SEC);
CU_ASSERT(time.it_value.tv_nsec == 999999999);
CU_ASSERT(time.it_value.tv_sec == 1);
}
void test_add_time_4(void)
{
const uint32_t start = 999999990;
struct itimerspec time = {
.it_value = {
.tv_nsec = start,
.tv_sec = 0}};
int main(void) {
CU_pSuite pSuite = NULL;
for (uint32_t i = start; i < 999999999; i++)
{
tmr_add_ns_to_current_time(&time, 1);
printf("%u:\t", i);
printf("%u,%u\n", time.it_value.tv_sec, time.it_value.tv_nsec);
CU_ASSERT(time.it_value.tv_nsec == i + 1);
}
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
tmr_add_ns_to_current_time(&time, 1);
printf("1000000000:\t");
printf("%u,%u\n", time.it_value.tv_sec, time.it_value.tv_nsec);
CU_ASSERT(time.it_value.tv_nsec == 0);
CU_ASSERT(time.it_value.tv_sec == 1);
pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
CU_add_test(
pSuite,
"test if time is added correctly with tmr_add_ns_to_current_time()",
test_add_time_1);
CU_add_test(
pSuite,
"test if time is added correctly with tmr_add_ns_to_current_time()",
test_add_time_2);
CU_add_test(
pSuite,
"test if time is added correctly with tmr_add_ns_to_current_time()",
test_add_time_3);
CU_add_test(pSuite, "test adding time in loop", test_add_time_4);
CU_add_test(pSuite, "test adding big numbers", test_big_number);
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}
void test_big_number(void)
{
struct itimerspec time = {
.it_value = {
.tv_nsec = 999999999,
.tv_sec = 0,
},
};
tmr_add_ns_to_current_time(&time, 1*SEC);
CU_ASSERT(time.it_value.tv_nsec == 999999999);
CU_ASSERT(time.it_value.tv_sec == 1);
}
int main()
{
CU_pSuite pSuite = NULL;
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
if (NULL == pSuite)
{
CU_cleanup_registry();
return CU_get_error();
}
CU_add_test(pSuite, "test if time is added correctly with tmr_add_ns_to_current_time()", test_add_time_1);
CU_add_test(pSuite, "test if time is added correctly with tmr_add_ns_to_current_time()", test_add_time_2);
CU_add_test(pSuite, "test if time is added correctly with tmr_add_ns_to_current_time()", test_add_time_3);
CU_add_test(pSuite, "test adding time in loop", test_add_time_4);
CU_add_test(pSuite, "test adding big numbers", test_big_number);
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}