Skip to content

fix(gps): clamp negative uBlox nano field before millis conversion#11642

Open
daijoubu wants to merge 1 commit into
iNavFlight:release/9.1from
daijoubu:fix/ublox-nano-rtc-offset
Open

fix(gps): clamp negative uBlox nano field before millis conversion#11642
daijoubu wants to merge 1 commit into
iNavFlight:release/9.1from
daijoubu:fix/ublox-nano-rtc-offset

Conversation

@daijoubu

Copy link
Copy Markdown
Contributor

Summary

The nano field in UBX-NAV-TIMEUTC and UBX-NAV-PVT messages is int32_t and legitimately goes negative near second boundaries (e.g. −50,000,000 ns). The previous code divided without clamping:

gpsSolDRV.time.millis = _buffer.pvt.nano / (1000*1000);

A negative value such as −50,000,000 ns → −50 ms → wraps to 65,486 when assigned to uint16_t, setting the RTC approximately 64 seconds fast on first GPS lock. Once rtcHasTime() returns true the RTC cannot be corrected until reboot.

Changes

  • src/main/io/gps_ublox.c: clamp nano to zero before the millis division in both MSG_TIMEUTC and MSG_PVT handlers
gpsSolDRV.time.millis = (uint16_t)(MAX(0, _buffer.pvt.nano) / (1000*1000));

Testing

Build matrix — all targets clean, no warnings:

Family Target
SITL SITL
F4 MATEKF405
F7 MATEKF765SE
H7 KAKUTEH7WING
AT32 IFLIGHT_BLITZ_ATF435

Hardware test with negative nano values at second boundaries not yet performed (requires a uBlox receiver and GPS lock capture near a second boundary).

The nano field in UBX-NAV-TIMEUTC and UBX-NAV-PVT messages is int32_t
and legitimately goes negative near second boundaries. Dividing without
clamping produced a large positive uint16_t (e.g. -50ms → 65486 ms),
setting the RTC ~64 seconds fast on first GPS lock. Once set, rtcHasTime()
prevents correction until reboot.

Clamp to zero before the division on both message handlers.
@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@github-actions

Copy link
Copy Markdown

Test firmware build ready — commit 6c6df28

Download firmware for PR #11642

238 targets built. Find your board's .hex file by name on that page (e.g. MATEKF405SE.hex). Files are individually downloadable — no GitHub login required.

Development build for testing only. Use Full Chip Erase when flashing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant