Files
gps/soc/software/gps_system/Makefile
T
onur f59d6ebb43 fix(gps): correct week-number handling across almanac, ephemeris and flash
Persisting navigation data changed its lifetime from one power cycle to
weeks, and every piece of arithmetic that had only ever run over the
shorter lifetime turned out to be wrong. Each fix below reproduces in a
host test that fails against the previous behaviour.

Almanac (gps_almanac.c, gps_lnav_parser.c)

WNa was lifted against gps_ephems.toe.week, a field nothing ever writes,
so any WNa above 128 underflowed to ~4.29e9. The propagation then reduced
tk with a while-loop, turning one visibility refresh into 4.29e9
iterations - hours on the target, inside a 10 s periodic call. WNa is the
week modulo 256, so this armed at GPS week 2433 and then for 127 weeks in
every 256. Now lifted against tow.week in signed arithmetic, refused if
impossible, and the propagation is closed form.

The IS-GPS-200 20.3.3.5.2.3 half-week wrap was also applied to tk
unconditionally. It is a week-crossover correction, not a general
reduction: a GPS week is 14.0384 orbital periods, so aliasing one week
moves a satellite 5161 km, enough to report one 5.1 degrees below the
horizon as 5.7 above. The crossover guess now lives in
gps_almanac_assumed_week(), used only before page 25 arrives, and an
almanac too old to propagate yields no prediction rather than a wrong one.

Ephemeris age (gps_ephemeris_validity.c, gps_lnav_parser.c)

The curve-fit check compared two second-of-week values and never read a
week at all, so a record exactly N weeks old read as current. Harmless
while ephemeris lived in RAM; directly reachable once persisted across a
power-off. gps_resolve_toe_week() now reconstructs Toe's week at commit
time from the subframe 1 week number and the handover word; an
unresolvable week yields no age, which reads as a rejection.

A restored record could also outrank the live broadcast, because the Toe
ordering inverts past half a week. Flash records are now marked restored
and are candidates, never incumbents.

Flash wear (gps_nav_context_store.c, gps_interrupt_handlers.c)

The CRC wear policy never engaged: the payload carried the live WLS
position and a receiver timestamp nothing on the restore path read, so
every 60 s save was an erase - about 139 days to wear-out. The seed is
now held and refreshed only on 5 km of movement, at most hourly: one
erase per day instead of 1440.

Measurement model (NavEquations.c)

The elevation weighting was floored at 10 degrees, capping its term at
17.3 m where the model's own answer is 34.4 m at 5 degrees and 86 m at 2,
so a low satellite was trusted several times more than the model allowed.
Floor lowered to 3 degrees, which is where 1/sin must still be bounded
because a zero weight fails the whole epoch. Measured on a six-satellite
geometry, 30 m of uncorrected delay now moves the fix 4.18 m through a
4-degree satellite against 48.50 m through a 72-degree one.

Both atmospheric corrections switched off below the horizon, so a
satellite crossing it lost 78.8 m of tropospheric delay in one step.
Elevation is now clamped at the horizon for the models, and the
extrapolation is paid for in variance.

Also: gps_pps_select_reference() returned early on the preferred PRN,
leaving later non-finite capture times unscreened.

Checked and found correct, with regressions added: the epoch assembly
under satellite connect/disconnect and reassignment, coalesced captures,
the 32-bit RF sample counter wrap (pseudoranges agree to 3.3 um across
it), the refusal to publish a solvable subset of an incoherent epoch, and
the Klobuchar model against IS-GPS-200 20.3.3.5.2.5.

Tests: 22 host suites, 5 RTL testbenches, sanitizer clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-10 01:45:35 +03:00

135 lines
5.0 KiB
Makefile

TARGET := gps_system
# Keep the standalone app build observable on the board. The board and parent
# software Makefiles already pass these values, but a direct `make` in this
# directory previously compiled every GPS_LOG call out and produced a silent
# UART image.
GPS_DEBUG ?= 1
GPS_DEBUG_LEVEL ?= 1
GPS_BUILD_ID ?= $(shell git describe --always --dirty --abbrev=12 2>/dev/null || printf unknown)
# The current Genesys2 Rocket/fabric domain that clocks the UART is 50 MHz.
# The 50 Hz fused-location stream plus GPS diagnostics does not fit safely in
# the polling UART budget at 115200 baud.
BSP_UART_CLOCK_HZ ?= 50000000
BSP_UART_BAUDRATE ?= 460800
BSP_CLINT_TIMEBASE_HZ ?= 500000
# Select one sensor backend, or keep the default read-only WHO_AM_I probe.
# Accepted values: auto, tdk, st (stm is an alias for st).
IMU_SENSOR ?= auto
ifeq ($(IMU_SENSOR),auto)
IMU_DRIVER_SRCS := src/drivers/imu/tdk_icm42688.c src/drivers/imu/st_lsm6dsox.c
IMU_BACKEND_CFLAG := -DIMU_SENSOR_BUILD_AUTO=1
else ifeq ($(IMU_SENSOR),tdk)
IMU_DRIVER_SRCS := src/drivers/imu/tdk_icm42688.c
IMU_BACKEND_CFLAG := -DIMU_SENSOR_BUILD_TDK=1
else ifeq ($(IMU_SENSOR),st)
IMU_DRIVER_SRCS := src/drivers/imu/st_lsm6dsox.c
IMU_BACKEND_CFLAG := -DIMU_SENSOR_BUILD_ST=1
else ifeq ($(IMU_SENSOR),stm)
IMU_DRIVER_SRCS := src/drivers/imu/st_lsm6dsox.c
IMU_BACKEND_CFLAG := -DIMU_SENSOR_BUILD_ST=1
else
$(error IMU_SENSOR must be auto, tdk, st or stm)
endif
# A non-zero value is a sensor-independent target. The common layer accepts
# only a hardware ODR within 5%; thus 200 selects 200 Hz on TDK and the real
# 208 Hz setting on LSM6DSOX. Zero keeps the backend default.
ifdef TDK_IMU_ODR_HZ
IMU_ODR_HZ ?= $(TDK_IMU_ODR_HZ)
else
IMU_ODR_HZ ?= 50
endif
IMU_ACCEL_FS_G ?= 0
IMU_GYRO_FS_DPS ?= 0
GPS_ESKF_ENABLE ?= 1
# UART telemetry cadence only; ESKF prediction still runs for every IMU sample.
# At the default 50 Hz ODR, 5 samples produce one corrected record every 100 ms.
IMU_CORRECTED_PUBLISH_EVERY_N_SAMPLES ?= 5
APP_SRCS := \
gps_system.c \
src/gps/gps_receiver.c \
src/imu/imu_calibration.c \
src/imu/imu_sensor.c \
src/adapters/imu_fusion_profile.c \
src/adapters/gps_system_uart.c \
$(IMU_DRIVER_SRCS) \
src/fusion/eskf.c \
src/fusion/fusion_motion.c \
src/fusion/fusion_rest_detector.c \
src/fusion/fusion_engine.c \
src/fusion/fusion_gps_adapter.c \
src/fusion/navigation_fusion.c \
src/adapters/gps_fusion_publisher.c \
src/gps/gps_ip.c \
src/gps/gps_interrupt_handlers.c \
src/gps/gps_acq_manager.c \
src/gps/gps_ephemeris_cache.c \
src/gps/gps_ephemeris_validity.c \
src/gps/gps_ephemeris_store.c \
src/gps/gps_lnav_parser.c \
src/gps/gps_almanac.c \
src/gps/gps_nav_context_store.c \
src/gps/gps_time_anchor.c \
src/gps/gps_nav_epoch.c \
src/gps/gps_pps.c \
src/gps/gps_pps_quality.c \
src/gps/gps_pps_time_filter.c \
src/gps/gps_tracking_measurement.c \
src/gps/linked_list.c \
src/gps/NavEquations.c \
src/gps/matrixLib.c
BSP_SRCS := init.c uart.c plic.c irq.c spi_flash.c pmod_spi.c
GPS_NAV_SOLVE_TIMER_TICKS ?= 10
# Warm/hot reacquisition epoch-phase seeding. This needs the tracking IP's
# NAV_SOLVE_PHASE_SEED register, added alongside this firmware. Building the
# feature in and running against an older bitstream would make the write decode
# to PSLVERR, which the APB-to-TileLink bridge reports as `denied` and Rocket
# can take as a store access fault. Set to 0 to build firmware for a bitstream
# that predates the register.
GPS_WARM_SEED_ENABLE ?= 1
# Persistent ephemeris cache in the top two 64 KiB flash sectors. Set to 0 to
# build firmware that never writes to flash.
GPS_EPHEMERIS_FLASH_ENABLE ?= 1
# Test hooks. MAX_SLOTS caps slots per sector so flash compaction is reachable
# on hardware in seconds instead of after a full sector; FORCE_FORMAT erases
# both sectors at boot. Both must be 0 in a production image.
GPS_EPHEMERIS_STORE_MAX_SLOTS ?= 0
GPS_EPHEMERIS_FLASH_FORCE_FORMAT ?= 0
GPS_PPS_STATUS_LOG_INTERVAL ?= 10
GPS_PPS_DOPPLER_PREDICTOR_ENABLE ?= 1
GPS_PPS_DOPPLER_PREDICTOR_AB_UPDATES ?= 0
include ../common/app.mk
CFLAGS += -Iheader
CFLAGS += $(IMU_BACKEND_CFLAG) \
-DGPS_BUILD_ID=\"$(GPS_BUILD_ID)\" \
-DIMU_ODR_HZ=$(IMU_ODR_HZ) \
-DIMU_ACCEL_FS_G=$(IMU_ACCEL_FS_G) \
-DIMU_GYRO_FS_DPS=$(IMU_GYRO_FS_DPS) \
-DGPS_ESKF_ENABLE=$(GPS_ESKF_ENABLE) \
-DIMU_CORRECTED_PUBLISH_EVERY_N_SAMPLES=$(IMU_CORRECTED_PUBLISH_EVERY_N_SAMPLES) \
-DGPS_NAV_SOLVE_TIMER_TICKS=$(GPS_NAV_SOLVE_TIMER_TICKS) \
-DGPS_WARM_SEED_ENABLE=$(GPS_WARM_SEED_ENABLE) \
-DGPS_EPHEMERIS_FLASH_ENABLE=$(GPS_EPHEMERIS_FLASH_ENABLE) \
-DGPS_EPHEMERIS_STORE_MAX_SLOTS=$(GPS_EPHEMERIS_STORE_MAX_SLOTS)u \
-DGPS_EPHEMERIS_FLASH_FORCE_FORMAT=$(GPS_EPHEMERIS_FLASH_FORCE_FORMAT) \
-DGPS_PPS_STATUS_LOG_INTERVAL=$(GPS_PPS_STATUS_LOG_INTERVAL) \
-DGPS_PPS_DOPPLER_PREDICTOR_ENABLE=$(GPS_PPS_DOPPLER_PREDICTOR_ENABLE) \
-DGPS_PPS_DOPPLER_PREDICTOR_AB_UPDATES=$(GPS_PPS_DOPPLER_PREDICTOR_AB_UPDATES)
LDLIBS += -lm
ifeq ($(GPS_DEBUG),1)
CFLAGS += -DDEBUG -DGPS_DEBUG_LEVEL=$(GPS_DEBUG_LEVEL)
endif
ifeq ($(GPS_NAV_DIAGNOSTICS),1)
CFLAGS += -DGPS_NAV_JUMP_DIAGNOSTICS=1
endif