Skip to content

Commit

Permalink
Fix RTC ticks per second calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jun 30, 2024
1 parent cabeb5d commit fb255e5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions frontend/desktop/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func (r *RTCTracker) update(updateInterval time.Duration) {

ticks := uint32(C.rtc_get_counter(r.rtc))

interval := time.Duration(C.rtc_get_tick_interval_us(r.rtc)) * time.Microsecond
if interval == 0 {
usInterval := float64(C.rtc_get_tick_interval_us(r.rtc))
if usInterval == 0 {
return
}

r.TargetTicksPerSecond = 1e6 / uint32(interval.Microseconds())
r.TargetTicksPerSecond = uint32(1e6 / usInterval)
r.TicksPerSecond = (1e6 * (ticks - r.lastTicks)) / uint32(updateInterval.Microseconds())

r.lastTicks = ticks
Expand Down Expand Up @@ -294,7 +294,9 @@ func NewEmulator(program *Program, spiFlash []byte, big bool) *Emulator {
}

ptProgram := C.program_new(flashSize)
C.program_load_binary(ptProgram, 0, (*C.uchar)(&flash[0]), C.size_t(len(flash)))
if !C.program_load_elf(ptProgram, 0, (*C.uchar)(&flash[0]), C.size_t(len(flash))) {
C.program_load_binary(ptProgram, 0, (*C.uchar)(&flash[0]), C.size_t(len(flash)))
}

pt := C.pinetime_new(ptProgram)
C.pinetime_reset(pt)
Expand Down
2 changes: 1 addition & 1 deletion include/peripherals/nrf52832/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ NRF52_PERIPHERAL(RTC, rtc, size_t cc_num)

uint32_t rtc_is_running(RTC_t *);
uint32_t rtc_get_counter(RTC_t *);
uint32_t rtc_get_tick_interval_us(RTC_t *);
double rtc_get_tick_interval_us(RTC_t *);
6 changes: 3 additions & 3 deletions src/peripherals/nrf52832/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct RTC_inst_t

bool running;

size_t tick_interval_us;
double tick_interval_us;
size_t last_check_us;

inten_t inten, evten;
Expand Down Expand Up @@ -129,7 +129,7 @@ OPERATION(rtc)
else
{
rtc->prescaler = *value;
rtc->tick_interval_us = ((size_t)(rtc->prescaler + 1) * 1e6) / 32768;
rtc->tick_interval_us = ((double)(rtc->prescaler + 1) * 1e6) / 32768.0;
}

return MEMREG_RESULT_OK;
Expand Down Expand Up @@ -203,7 +203,7 @@ uint32_t rtc_get_counter(RTC_t *rtc)
return rtc->counter;
}

uint32_t rtc_get_tick_interval_us(RTC_t *rtc)
double rtc_get_tick_interval_us(RTC_t *rtc)
{
return rtc->tick_interval_us;
}

0 comments on commit fb255e5

Please sign in to comment.