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,13 +1,11 @@
#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,
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,
@ -32,12 +30,10 @@ sm_trans_t bridge_sm_transitions[] =
.to_state = HB2_GND_ON,
.trans_fct = brdg_Hb2GndOn}};
sm_t bridge_statemachine = {
.current_state = BRIDGE_SM_START,
sm_t bridge_statemachine = {.current_state = BRIDGE_SM_START,
.transitions = bridge_sm_transitions};
void brdg_BothGndOn(void)
{
void brdg_BothGndOn(void) {
/* Turn both GND transistors on */
setPin(D0);
setPin(D2);
@ -47,8 +43,7 @@ void brdg_BothGndOn(void)
unsetPin(D1);
}
void brdg_Hb1GndOn(void)
{
void brdg_Hb1GndOn(void) {
/* We turn GND of half-bridge 1 on */
setPin(D0);
@ -56,28 +51,15 @@ void brdg_Hb1GndOn(void)
unsetPin(D2);
}
void brdg_Hb1PowerOn(void)
{
setPin(D3);
}
void brdg_Hb1PowerOn(void) { setPin(D3); }
void brdg_Hb1PowerOff(void)
{
unsetPin(D3);
}
void brdg_Hb1PowerOff(void) { unsetPin(D3); }
void brdg_Hb2GndOn(void)
{
void brdg_Hb2GndOn(void) {
setPin(D0);
unsetPin(D2);
}
void brdg_Hb2PowerOn(void)
{
setPin(D1);
}
void brdg_Hb2PowerOn(void) { setPin(D1); }
void brdg_Hb2PowerOff(void)
{
unsetPin(D1);
}
void brdg_Hb2PowerOff(void) { unsetPin(D1); }

View File

@ -6,26 +6,25 @@
struct ftdi_context *ftdi;
int init_ftdi()
{
int init_ftdi() {
struct ftdi_version_info version;
uint8_t pins;
unsigned int chipid;
if ((ftdi = ftdi_new()) == 0)
{
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",
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));
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;
}

View File

@ -1,5 +1,4 @@
typedef enum
{
typedef enum {
BRIDGE_SM_START,
BRIDGE_SM_INIT_IO,
HB1_GND_ON,
@ -10,7 +9,6 @@ typedef enum
HB2_PWR_OFF
} bdg_sm_state_t;
void brdg_BothGndOn(void);
void brdg_Hb1GndOn(void);

View File

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

View File

@ -1,8 +1,7 @@
#include <time.h>
#include <signal.h>
#include <time.h>
typedef struct
{
typedef struct {
timer_t timerid;
long interval;
struct sigevent sevp;

View File

@ -1,50 +1,41 @@
#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)
{
void test1(void) {
printf("%lu\n", counter);
fflush(stdout);
counter++;
}
void test2(void)
{
void test2(void) {
printf("This is test2\n");
fflush(stdout);
}
void togglePin1(void)
{
togglePin(D0);
}
void togglePin1(void) { togglePin(D0); }
int main()
{
int main() {
printf("Program started\n");
if (init_ftdi())
{
if (init_ftdi()) {
printf("Error initializing ftdi\n");
return EXIT_FAILURE;
}
if (tmr_callEvery(togglePin1, 10 * USEC))
{
if (tmr_callEvery(togglePin1, 10 * USEC)) {
printf("Error starting timer for togglePin\n");
return EXIT_FAILURE;
}
while (1)
{
while (1) {
sleep(10000);
}

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)
{
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
{
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
{
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)
{
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
{
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)
{
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,14 +10,12 @@
int tmr_registry_index = 0;
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_index++;
}
void tmr_initTimer(tmr_ctx *ctx)
{
void tmr_initTimer(tmr_ctx *ctx) {
ctx->sevp = (struct sigevent){
.sigev_notify = SIGEV_THREAD,
.sigev_notify_function = tmr_handleTimerOverflow,
@ -34,8 +32,7 @@ void tmr_initTimer(tmr_ctx *ctx)
timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL);
}
void tmr_handleTimerOverflow(sigval_t ctx_par)
{
void tmr_handleTimerOverflow(sigval_t ctx_par) {
tmr_ctx *ctx = (tmr_ctx *)ctx_par.sival_ptr;
ctx->funcToBeCalled();
@ -43,8 +40,7 @@ void tmr_handleTimerOverflow(sigval_t ctx_par)
timer_settime(ctx->timerid, TIMER_ABSTIME, &ctx->times, NULL);
}
int tmr_callEvery(void (*func)(void), long nsec)
{
int tmr_callEvery(void (*func)(void), long nsec) {
tmr_ctx *tmr = calloc(1, sizeof(tmr_ctx));
// tmr_ctx *tmr = malloc(sizeof(tmr_ctx));
@ -61,15 +57,11 @@ int tmr_callEvery(void (*func)(void), long nsec)
return 0;
}
void tmr_add_ns_to_current_time(struct itimerspec *time, long nsec)
{
if ((time->it_value.tv_nsec + nsec) >= 1 * SEC)
{
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
{
} else {
time->it_value.tv_nsec += nsec;
}
}

View File

@ -1,82 +1,61 @@
#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;
}
int clean_suite1(void) { return 0; }
void test_add_time_1(void)
{
struct itimerspec test_time = {
.it_value = {
.tv_nsec = 0,
.tv_sec = 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_2(void)
{
void test_add_time_2(void) {
struct itimerspec test_time = {
.it_value = {
.tv_nsec = 999999999,
.tv_sec = 0}};
.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_3(void)
{
void test_add_time_3(void) {
struct itimerspec test_time = {
.it_value = {
.tv_nsec = 999999111,
.tv_sec = 0}};
.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_4(void)
{
void test_add_time_4(void) {
const uint32_t start = 999999990;
struct itimerspec time = {
.it_value = {
.tv_nsec = start,
.tv_sec = 0}};
struct itimerspec time = {.it_value = {.tv_nsec = start, .tv_sec = 0}};
for (uint32_t i = start; i < 999999999; i++)
{
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);
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("%u,%u\n", time.it_value.tv_sec, time.it_value.tv_nsec);
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_big_number(void)
{
void test_big_number(void) {
struct itimerspec time = {
.it_value = {
.it_value =
{
.tv_nsec = 999999999,
.tv_sec = 0,
},
@ -87,24 +66,30 @@ void test_big_number(void)
CU_ASSERT(time.it_value.tv_sec == 1);
}
int main()
{
int main(void) {
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)
{
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 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);