Skip to content

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:

  1. The nvm_write callback updates the relevant fields inside the SystemSettings_t struct in RAM.
  2. The ee_write() function is called, which serializes and writes the entire struct to a dedicated Flash memory page.

On boot:

  1. recalibration_storage_init() is invoked before the sensors are initialized.
  2. ee_init() and ee_read() fetch the raw data from Flash into the SystemSettings_t RAM structure.
  3. During apps_recalibration_init() and steering_recalibration_init(), the generic driver explicitly tries to load these values using Recal_LoadFromNvm().
  4. 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.
  5. If the CRC passes, the driver re-initializes the respective sensor with the saved raw_min and raw_max boundaries.

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.

Released under the MIT License.