Format and fix linter warnings
This commit is contained in:
parent
fd28d9815a
commit
99684ac531
@ -1,83 +1,65 @@
|
|||||||
|
|
||||||
#include "bridge_utils.h"
|
#include "bridge_utils.h"
|
||||||
#include "statemachine.h"
|
|
||||||
#include "pins.h"
|
#include "pins.h"
|
||||||
|
#include "statemachine.h"
|
||||||
|
|
||||||
bdg_sm_state_t bridge_state;
|
bdg_sm_state_t bridge_state;
|
||||||
|
|
||||||
sm_trans_t bridge_sm_transitions[] =
|
sm_trans_t bridge_sm_transitions[] = {{.from_state = BRIDGE_SM_START,
|
||||||
{
|
.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 = {
|
sm_t bridge_statemachine = {.current_state = BRIDGE_SM_START,
|
||||||
.current_state = BRIDGE_SM_START,
|
.transitions = bridge_sm_transitions};
|
||||||
.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(D0);
|
setPin(D2);
|
||||||
setPin(D2);
|
|
||||||
|
|
||||||
/* Turn both PWR transistors off */
|
/* Turn both PWR transistors off */
|
||||||
unsetPin(D3);
|
unsetPin(D3);
|
||||||
unsetPin(D1);
|
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(D0);
|
||||||
setPin(D0);
|
|
||||||
|
|
||||||
/* And then we turn GND of half-bridge 2 off */
|
/* And then we turn GND of half-bridge 2 off */
|
||||||
unsetPin(D2);
|
unsetPin(D2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void brdg_Hb1PowerOn(void)
|
void brdg_Hb1PowerOn(void) { setPin(D3); }
|
||||||
{
|
|
||||||
setPin(D3);
|
void brdg_Hb1PowerOff(void) { unsetPin(D3); }
|
||||||
|
|
||||||
|
void brdg_Hb2GndOn(void) {
|
||||||
|
setPin(D0);
|
||||||
|
unsetPin(D2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void brdg_Hb1PowerOff(void)
|
void brdg_Hb2PowerOn(void) { setPin(D1); }
|
||||||
{
|
|
||||||
unsetPin(D3);
|
|
||||||
}
|
|
||||||
|
|
||||||
void brdg_Hb2GndOn(void)
|
void brdg_Hb2PowerOff(void) { unsetPin(D1); }
|
||||||
{
|
|
||||||
setPin(D0);
|
|
||||||
unsetPin(D2);
|
|
||||||
}
|
|
||||||
|
|
||||||
void brdg_Hb2PowerOn(void)
|
|
||||||
{
|
|
||||||
setPin(D1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void brdg_Hb2PowerOff(void)
|
|
||||||
{
|
|
||||||
unsetPin(D1);
|
|
||||||
}
|
|
||||||
|
@ -6,38 +6,37 @@
|
|||||||
|
|
||||||
struct ftdi_context *ftdi;
|
struct ftdi_context *ftdi;
|
||||||
|
|
||||||
int init_ftdi()
|
int init_ftdi() {
|
||||||
{
|
struct ftdi_version_info version;
|
||||||
struct ftdi_version_info version;
|
uint8_t pins;
|
||||||
uint8_t pins;
|
unsigned int chipid;
|
||||||
unsigned int chipid;
|
|
||||||
|
|
||||||
if ((ftdi = ftdi_new()) == 0)
|
if ((ftdi = ftdi_new()) == 0) {
|
||||||
{
|
fprintf(stderr, "ftdi_new failed\n");
|
||||||
fprintf(stderr, "ftdi_new failed\n");
|
return EXIT_FAILURE;
|
||||||
return EXIT_FAILURE;
|
}
|
||||||
}
|
version = ftdi_get_library_version();
|
||||||
version = ftdi_get_library_version();
|
printf("Initialized libftdi %s (major: %d, minor: %d, micro: %d, snapshot "
|
||||||
printf("Initialized libftdi %s (major: %d, minor: %d, micro: %d, snapshot ver: %s)\n",
|
"ver: %s)\n",
|
||||||
version.version_str, version.major, version.minor, version.micro,
|
version.version_str, version.major, version.minor, version.micro,
|
||||||
version.snapshot_str);
|
version.snapshot_str);
|
||||||
|
|
||||||
int ret = ftdi_usb_open(ftdi, 0x0403, 0x6014);
|
int ret = ftdi_usb_open(ftdi, 0x0403, 0x6014);
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
{
|
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret,
|
||||||
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
|
ftdi_get_error_string(ftdi));
|
||||||
ftdi_free(ftdi);
|
ftdi_free(ftdi);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable bitbang mode
|
// Enable bitbang mode
|
||||||
ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG);
|
ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG);
|
||||||
|
|
||||||
printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(ftdi, &chipid));
|
printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(ftdi, &chipid));
|
||||||
printf("FTDI chipid: %X\n", chipid);
|
printf("FTDI chipid: %X\n", chipid);
|
||||||
ftdi_read_pins(ftdi, &pins);
|
ftdi_read_pins(ftdi, &pins);
|
||||||
|
|
||||||
printf("Pins state: %X\n", pins);
|
printf("Pins state: %X\n", pins);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
@ -1,16 +1,14 @@
|
|||||||
typedef enum
|
typedef enum {
|
||||||
{
|
BRIDGE_SM_START,
|
||||||
BRIDGE_SM_START,
|
BRIDGE_SM_INIT_IO,
|
||||||
BRIDGE_SM_INIT_IO,
|
HB1_GND_ON,
|
||||||
HB1_GND_ON,
|
HB1_PWR_ON,
|
||||||
HB1_PWR_ON,
|
HB1_PWR_OFF,
|
||||||
HB1_PWR_OFF,
|
HB2_GND_ON,
|
||||||
HB2_GND_ON,
|
HB2_PWR_ON,
|
||||||
HB2_PWR_ON,
|
HB2_PWR_OFF
|
||||||
HB2_PWR_OFF
|
|
||||||
} bdg_sm_state_t;
|
} bdg_sm_state_t;
|
||||||
|
|
||||||
|
|
||||||
void brdg_BothGndOn(void);
|
void brdg_BothGndOn(void);
|
||||||
|
|
||||||
void brdg_Hb1GndOn(void);
|
void brdg_Hb1GndOn(void);
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
typedef int sm_state_t;
|
typedef int sm_state_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
sm_state_t from_state;
|
||||||
sm_state_t from_state;
|
sm_state_t to_state;
|
||||||
sm_state_t to_state;
|
void (*trans_fct)(void);
|
||||||
void (*trans_fct)(void);
|
|
||||||
} sm_trans_t;
|
} sm_trans_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
sm_state_t current_state;
|
||||||
sm_state_t current_state;
|
sm_trans_t *transitions;
|
||||||
sm_trans_t *transitions;
|
|
||||||
} sm_t;
|
} sm_t;
|
||||||
|
|
||||||
void sm_handleTransition(sm_t *statemachine, sm_state_t to);
|
void sm_handleTransition(sm_t *statemachine, sm_state_t to);
|
@ -1,8 +1,8 @@
|
|||||||
//microseconds in nanoseconds
|
// microseconds in nanoseconds
|
||||||
#define USEC (1000)
|
#define USEC (1000)
|
||||||
|
|
||||||
//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)
|
@ -1,14 +1,13 @@
|
|||||||
#include <time.h>
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
timer_t timerid;
|
||||||
timer_t timerid;
|
long interval;
|
||||||
long interval;
|
struct sigevent sevp;
|
||||||
struct sigevent sevp;
|
struct itimerspec times;
|
||||||
struct itimerspec times;
|
struct timespec runsSince;
|
||||||
struct timespec runsSince;
|
void (*funcToBeCalled)(void);
|
||||||
void (*funcToBeCalled)(void);
|
|
||||||
} tmr_ctx;
|
} tmr_ctx;
|
||||||
|
|
||||||
void tmr_initTimer(tmr_ctx *ctx);
|
void tmr_initTimer(tmr_ctx *ctx);
|
||||||
|
69
src/main.c
69
src/main.c
@ -1,52 +1,43 @@
|
|||||||
|
#include <ftdi.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <ftdi.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "ftdi_utils.h"
|
#include "ftdi_utils.h"
|
||||||
#include "timer_utils.h"
|
|
||||||
#include "time_scales.h"
|
|
||||||
#include "pins.h"
|
#include "pins.h"
|
||||||
|
#include "time_scales.h"
|
||||||
|
#include "timer_utils.h"
|
||||||
|
|
||||||
static uint64_t counter = 0;
|
static uint64_t counter = 0;
|
||||||
void test1(void)
|
void test1(void) {
|
||||||
{
|
printf("%lu\n", counter);
|
||||||
printf("%lu\n", counter);
|
fflush(stdout);
|
||||||
fflush(stdout);
|
counter++;
|
||||||
counter++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test2(void)
|
void test2(void) {
|
||||||
{
|
printf("This is test2\n");
|
||||||
printf("This is test2\n");
|
fflush(stdout);
|
||||||
fflush(stdout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void togglePin1(void)
|
void togglePin1(void) { togglePin(D0); }
|
||||||
{
|
|
||||||
togglePin(D0);
|
int main() {
|
||||||
}
|
printf("Program started\n");
|
||||||
|
|
||||||
int main()
|
if (init_ftdi()) {
|
||||||
{
|
printf("Error initializing ftdi\n");
|
||||||
printf("Program started\n");
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
if (init_ftdi())
|
|
||||||
{
|
if (tmr_callEvery(togglePin1, 10 * USEC)) {
|
||||||
printf("Error initializing ftdi\n");
|
printf("Error starting timer for togglePin\n");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tmr_callEvery(togglePin1, 10 * USEC))
|
while (1) {
|
||||||
{
|
sleep(10000);
|
||||||
printf("Error starting timer for togglePin\n");
|
}
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
return 0;
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
sleep(10000);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
67
src/pins.c
67
src/pins.c
@ -1,53 +1,44 @@
|
|||||||
|
#include <ftdi.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <ftdi.h>
|
|
||||||
|
|
||||||
#include "pins.h"
|
|
||||||
#include "ftdi_utils.h"
|
#include "ftdi_utils.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);
|
||||||
uint8_t pin_state = pin_state_cache | (1 << pin);
|
int ret = ftdi_write_data(ftdi, &pin_state, 1);
|
||||||
int ret = ftdi_write_data(ftdi, &pin_state, 1);
|
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
{
|
fprintf(stderr, "setting pin failed for data 0x%x, error %d (%s)\n",
|
||||||
fprintf(stderr, "setting pin failed for data 0x%x, error %d (%s)\n", 1 << pin, ret, ftdi_get_error_string(ftdi));
|
1 << pin, ret, ftdi_get_error_string(ftdi));
|
||||||
}
|
} else {
|
||||||
else
|
pin_state_cache = pin_state;
|
||||||
{
|
}
|
||||||
pin_state_cache = pin_state;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void unsetPin(unsigned int pin)
|
void unsetPin(unsigned int pin) {
|
||||||
{
|
uint8_t pin_state = pin_state_cache & ~(1 << pin);
|
||||||
uint8_t pin_state = pin_state_cache & ~(1 << pin);
|
int ret = ftdi_write_data(ftdi, &pin_state, 1);
|
||||||
int ret = ftdi_write_data(ftdi, &pin_state, 1);
|
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
{
|
fprintf(stderr, "unsetting pin failed for data 0x%x, error %d (%s)\n",
|
||||||
fprintf(stderr, "unsetting pin failed for data 0x%x, error %d (%s)\n", 1 << pin, ret, ftdi_get_error_string(ftdi));
|
1 << pin, ret, ftdi_get_error_string(ftdi));
|
||||||
}
|
} else {
|
||||||
else
|
pin_state_cache = pin_state;
|
||||||
{
|
}
|
||||||
pin_state_cache = pin_state;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void togglePin(unsigned int pin)
|
void togglePin(unsigned int pin) {
|
||||||
{
|
uint8_t pin_state = pin_state_cache ^ (1 << pin);
|
||||||
uint8_t pin_state = pin_state_cache ^ (1 << pin);
|
int ret = ftdi_write_data(ftdi, &pin_state, 1);
|
||||||
int ret = ftdi_write_data(ftdi, &pin_state, 1);
|
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
{
|
fprintf(stderr, "toggling pin failed for data 0x%x, error %d (%s)\n",
|
||||||
fprintf(stderr, "toggling pin failed for data 0x%x, error %d (%s)\n", 1 << pin, ret, ftdi_get_error_string(ftdi));
|
1 << pin, ret, ftdi_get_error_string(ftdi));
|
||||||
}
|
} else {
|
||||||
else
|
pin_state_cache = pin_state;
|
||||||
{
|
}
|
||||||
pin_state_cache = pin_state;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#include "statemachine.h"
|
#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;
|
||||||
statemachine = statemachine;
|
to = to;
|
||||||
to = to;
|
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
#include <time.h>
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "time_scales.h"
|
#include "time_scales.h"
|
||||||
#include "timer_utils.h"
|
#include "timer_utils.h"
|
||||||
@ -10,66 +10,58 @@
|
|||||||
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++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tmr_initTimer(tmr_ctx *ctx)
|
void tmr_initTimer(tmr_ctx *ctx) {
|
||||||
{
|
ctx->sevp = (struct sigevent){
|
||||||
ctx->sevp = (struct sigevent){
|
.sigev_notify = SIGEV_THREAD,
|
||||||
.sigev_notify = SIGEV_THREAD,
|
.sigev_notify_function = tmr_handleTimerOverflow,
|
||||||
.sigev_notify_function = tmr_handleTimerOverflow,
|
.sigev_value.sival_ptr = ctx,
|
||||||
.sigev_value.sival_ptr = ctx,
|
};
|
||||||
};
|
|
||||||
|
|
||||||
timer_create(CLOCK_MONOTONIC, &ctx->sevp, &ctx->timerid);
|
timer_create(CLOCK_MONOTONIC, &ctx->sevp, &ctx->timerid);
|
||||||
clock_gettime(CLOCK_MONOTONIC, &ctx->times.it_value);
|
clock_gettime(CLOCK_MONOTONIC, &ctx->times.it_value);
|
||||||
|
|
||||||
ctx->runsSince.tv_sec = ctx->times.it_value.tv_sec;
|
ctx->runsSince.tv_sec = ctx->times.it_value.tv_sec;
|
||||||
ctx->runsSince.tv_nsec = ctx->times.it_value.tv_nsec;
|
ctx->runsSince.tv_nsec = ctx->times.it_value.tv_nsec;
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
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), long nsec) {
|
||||||
{
|
tmr_ctx *tmr = calloc(1, sizeof(tmr_ctx));
|
||||||
tmr_ctx *tmr = calloc(1,sizeof(tmr_ctx));
|
// tmr_ctx *tmr = malloc(sizeof(tmr_ctx));
|
||||||
// tmr_ctx *tmr = malloc(sizeof(tmr_ctx));
|
|
||||||
|
|
||||||
tmr_add_to_registry(tmr);
|
tmr_add_to_registry(tmr);
|
||||||
|
|
||||||
tmr->interval = nsec;
|
tmr->interval = nsec;
|
||||||
tmr->funcToBeCalled = func;
|
tmr->funcToBeCalled = func;
|
||||||
|
|
||||||
if (!func)
|
if (!func)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
tmr_initTimer(tmr);
|
tmr_initTimer(tmr);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tmr_add_ns_to_current_time(struct itimerspec *time, long nsec)
|
void tmr_add_ns_to_current_time(struct itimerspec *time, long nsec) {
|
||||||
{
|
if ((time->it_value.tv_nsec + nsec) >= 1 * SEC) {
|
||||||
if ((time->it_value.tv_nsec + nsec) >= 1 * SEC)
|
time->it_value.tv_sec++;
|
||||||
{
|
time->it_value.tv_nsec -= (1 * SEC - nsec);
|
||||||
time->it_value.tv_sec++;
|
} else {
|
||||||
time->it_value.tv_nsec -= (1 * SEC - nsec);
|
time->it_value.tv_nsec += nsec;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
time->it_value.tv_nsec += nsec;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,115 +1,100 @@
|
|||||||
|
#include <inttypes.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <inttypes.h>
|
|
||||||
|
|
||||||
#include "CUnit/Basic.h"
|
#include "CUnit/Basic.h"
|
||||||
#include "timer_utils.h"
|
|
||||||
#include "time_scales.h"
|
#include "time_scales.h"
|
||||||
|
#include "timer_utils.h"
|
||||||
|
|
||||||
int init_suite1(void)
|
int init_suite1(void) { return 0; }
|
||||||
{
|
|
||||||
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)
|
void test_add_time_2(void) {
|
||||||
{
|
struct itimerspec test_time = {
|
||||||
return 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_1(void)
|
void test_add_time_3(void) {
|
||||||
{
|
struct itimerspec test_time = {
|
||||||
struct itimerspec test_time = {
|
.it_value = {.tv_nsec = 999999111, .tv_sec = 0}};
|
||||||
.it_value = {
|
tmr_add_ns_to_current_time(&test_time, 1000);
|
||||||
.tv_nsec = 0,
|
CU_ASSERT(test_time.it_value.tv_sec == 1);
|
||||||
.tv_sec = 0}};
|
CU_ASSERT(test_time.it_value.tv_nsec == 111);
|
||||||
|
|
||||||
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_4(void) {
|
||||||
{
|
const uint32_t start = 999999990;
|
||||||
struct itimerspec test_time = {
|
struct itimerspec time = {.it_value = {.tv_nsec = start, .tv_sec = 0}};
|
||||||
.it_value = {
|
|
||||||
.tv_nsec = 999999999,
|
for (uint32_t i = start; i < 999999999; i++) {
|
||||||
.tv_sec = 0}};
|
tmr_add_ns_to_current_time(&time, 1);
|
||||||
tmr_add_ns_to_current_time(&test_time, 1);
|
printf("%u:\t", i);
|
||||||
CU_ASSERT(test_time.it_value.tv_sec == 1);
|
printf("%ld,%ld\n", time.it_value.tv_sec, time.it_value.tv_nsec);
|
||||||
CU_ASSERT(test_time.it_value.tv_nsec == 0);
|
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)
|
void test_big_number(void) {
|
||||||
{
|
struct itimerspec time = {
|
||||||
struct itimerspec test_time = {
|
.it_value =
|
||||||
.it_value = {
|
{
|
||||||
.tv_nsec = 999999111,
|
.tv_nsec = 999999999,
|
||||||
.tv_sec = 0}};
|
.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);
|
|
||||||
|
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)
|
int main(void) {
|
||||||
{
|
CU_pSuite pSuite = NULL;
|
||||||
const uint32_t start = 999999990;
|
|
||||||
struct itimerspec time = {
|
|
||||||
.it_value = {
|
|
||||||
.tv_nsec = start,
|
|
||||||
.tv_sec = 0}};
|
|
||||||
|
|
||||||
for (uint32_t i = start; i < 999999999; i++)
|
if (CUE_SUCCESS != CU_initialize_registry())
|
||||||
{
|
return CU_get_error();
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
tmr_add_ns_to_current_time(&time, 1);
|
pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
|
||||||
printf("1000000000:\t");
|
if (NULL == pSuite) {
|
||||||
printf("%u,%u\n", time.it_value.tv_sec, time.it_value.tv_nsec);
|
CU_cleanup_registry();
|
||||||
CU_ASSERT(time.it_value.tv_nsec == 0);
|
return CU_get_error();
|
||||||
CU_ASSERT(time.it_value.tv_sec == 1);
|
}
|
||||||
|
|
||||||
|
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();
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user