Skip to content

Commit

Permalink
firmware: improve rounding during Hz -> mHz conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
theacodes committed Feb 10, 2024
1 parent 3426d2d commit 5881a90
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion firmware/src/lib/gem_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ inline static __attribute__((always_inline)) fix16_t gem_voct_to_frequency(fix16
inline static uint64_t __attribute__((always_inline)) gem_frequency_to_millihertz_f16_u64(fix16_t freq_hz) {
uint32_t freq_whole = freq_hz >> 16;
uint32_t freq_frac = freq_hz & 0xFFFF;
return (freq_whole * 100) + (freq_frac * 100 / 0xFFFF);
return (freq_whole * 100) + ((freq_frac * 100 + 0x8000) >> 16);
}
3 changes: 2 additions & 1 deletion firmware/tests/test_oscillator.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "gem_test.h"
#include "gem_pulseout.h"
#include "wntr_uint12.h"
#include <inttypes.h>
#include <math.h>

const struct WntrErrorCorrection error_correction = {.offset = F16(0), .gain = F16(1)};
Expand Down Expand Up @@ -485,7 +486,7 @@ TEST_CASE_BEGIN(cv_pitch_period_conversion)
print_f16(cv);
printf(" V freq = ");
print_f16(freq);
printf(" Hz millihertz = %llu mHz period = %u, roundtrip = %f Hz, diff = %f ppm", mhertz, period, roundtrip, diff);
printf(" Hz millihertz = %" PRIu64 " mHz period = %" PRIu32 ", roundtrip = %f Hz, diff = %f ppm", mhertz, period, roundtrip, diff);
}
printf("\n");
TEST_CASE_END
Expand Down

0 comments on commit 5881a90

Please sign in to comment.