Skip to content

Commit

Permalink
demo: improve tranmission timing
Browse files Browse the repository at this point in the history
GetTickCount64 has a documented resolution of 10-16 ms.
Switch code to QueryPerformanceCounter which like has
better resolution.
  • Loading branch information
jgressmann committed Jan 25, 2025
1 parent 5b47040 commit fc28c25
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Boards
Submodule Boards updated 1013 files
22 changes: 21 additions & 1 deletion Windows/app/app.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2021-2022 Jean Gressmann <[email protected]>
* Copyright (c) 2021-2025 Jean Gressmann <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -123,3 +123,23 @@ void log_candump(
fprintf(f, "\n");
}
}

static uint64_t s_perf_counter_freq = 1;

void app_init()
{
QueryPerformanceFrequency((LARGE_INTEGER*)&s_perf_counter_freq);

}

uint64_t mono_ticks()
{
uint64_t now;
QueryPerformanceCounter((LARGE_INTEGER*)&now);
return now;
}

uint64_t mono_millis()
{
return (mono_ticks() * 1000U) / s_perf_counter_freq;
}
7 changes: 6 additions & 1 deletion Windows/app/app.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2022 Jean Gressmann <[email protected]>
* Copyright (c) 2020-2025 Jean Gressmann <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -157,6 +157,11 @@ void log_candump(
uint8_t dlc,
uint8_t const* data);

uint64_t mono_ticks();
uint64_t mono_millis();

void app_init();

#ifdef __cplusplus
} // extern "C"
#endif
2 changes: 2 additions & 0 deletions Windows/app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ int main(int argc, char** argv)

cia_fd_cbt_init_default_real(&ac.nominal_user_constraints, &ac.data_user_constraints);

app_init();

for (int i = 1; i < argc; ) {
if (0 == strcmp("-h", argv[i]) ||
0 == strcmp("--help", argv[i]) ||
Expand Down
16 changes: 12 additions & 4 deletions Windows/app/shared.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2023 Jean Gressmann <[email protected]>
* Copyright (c) 2020-2025 Jean Gressmann <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -495,8 +495,11 @@ int run(app_ctx* ac)
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);

while (1) {

uint64_t const wait_start_time = mono_millis();
auto r = WaitForMultipleObjects(static_cast<DWORD>(_countof(handles)), handles, FALSE, timeout_ms);
uint64_t const now = mono_millis();
DWORD const elapsed_ms = (DWORD)(now - wait_start_time);

if (r >= WAIT_OBJECT_0 && r < WAIT_OBJECT_0 + _countof(handles)) {
auto index = r - WAIT_OBJECT_0;
auto handle = handles[index];
Expand All @@ -522,8 +525,13 @@ int run(app_ctx* ac)
process_rx(ac);

if (ac->tx_job_count) {
timeout_ms = 0xffffffff;
ULONGLONG now = GetTickCount64();
if (elapsed_ms >= timeout_ms) {
timeout_ms = 0xffffffff;
}
else {
timeout_ms -= elapsed_ms;
}

bool queued = false;

for (size_t i = 0; i < ac->tx_job_count; ++i) {
Expand Down
14 changes: 11 additions & 3 deletions Windows/app/single.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2023 Jean Gressmann <[email protected]>
* Copyright (c) 2020-2025 Jean Gressmann <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -695,7 +695,11 @@ int run_single(struct app_ctx* ac)
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);

while (1) {
uint64_t const wait_start_time = mono_millis();
error = sc_can_stream_rx(can_state.stream, timeout_ms);
uint64_t const now = mono_millis();
DWORD const elapsed_ms = (DWORD)(now - wait_start_time);

if (error) {
if (SC_DLL_ERROR_USER_HANDLE_SIGNALED == error) {
break;
Expand All @@ -710,8 +714,12 @@ int run_single(struct app_ctx* ac)
}

if (ac->tx_job_count) {
timeout_ms = 0xffffffff;
ULONGLONG now = GetTickCount64();
if (elapsed_ms >= timeout_ms) {
timeout_ms = 0xffffffff;
}
else {
timeout_ms -= elapsed_ms;
}

error = sc_can_stream_tx_batch_begin(can_state.stream);
if (error) {
Expand Down

0 comments on commit fc28c25

Please sign in to comment.