Sensor Recalibration & NVM Storage
The CAN-Gateway supports on-the-fly recalibration for critical sensors (currently APPS and Steering). This system captures new minimum and maximum raw ADC values and recalculates the appropriate scaling.
Recalibration Driver
The recalibration logic is handled by a generic state machine (generic_sensor_recal.c). For the CAN-Gateway, the board-specific integration is located in apps_recalibration.c.
Non-Volatile Memory (NVM) Storage
Since the STM32G4 does not have dedicated hardware EEPROM, the CAN-Gateway uses the nimaltd/ee EEPROM Emulation Library to store calibration values in the internal Flash memory.
Architecture
All recalibration data is aggregated into a single global configuration struct (SystemSettings_t inside apps_recalibration.c).
When recalibration completes successfully:
- The
nvm_writecallback updates the relevant fields inside theSystemSettings_tstruct in RAM. - The
ee_write()function is called, which serializes and writes the entire struct to a dedicated Flash memory page.
On boot:
recalibration_storage_init()is invoked before the sensors are initialized.ee_init()andee_read()fetch the raw data from Flash into theSystemSettings_tRAM structure.- During
apps_recalibration_init()andsteering_recalibration_init(), the generic driver explicitly tries to load these values usingRecal_LoadFromNvm(). - The generic driver performs a CRC check. If the Flash is blank (first boot) or corrupted, the CRC fails, and the driver falls back to hardcoded defaults.
- If the CRC passes, the driver re-initializes the respective sensor with the saved
raw_minandraw_maxboundaries.
Flash Configuration
The exact flash page used is defined in Drivers/Flash_EE/ee_config.h. By default, if EE_MANUAL_CONFIG is set to 0, the library automatically uses the last available page of the microcontroller's Flash memory.
