improved EEPROM-Initialize and recovery, renamed typo in varname and comments by ChatGPT

This commit is contained in:
2025-08-24 13:14:06 +02:00
parent 12ee18adee
commit c998cce1a8
6 changed files with 304 additions and 163 deletions

View File

@@ -1,27 +1,26 @@
/**
* @file config.h
* @brief Configuration structures and EEPROM API for ChainLube firmware.
*
* @brief Header file for configuration settings and EEPROM operations in the ChainLube application.
* Defines EEPROM layout versions, configuration and persistence data structures,
* and the public functions for storing, loading, formatting and validating
* configuration/persistence records.
*
* This file defines configuration settings for the ChainLube project, including default values,
* EEPROM structures, and functions for EEPROM operations. It also defines enums for different sources
* of speed input, GPS baud rates, and CAN bus sources. Additionally, it includes functions for EEPROM handling
* such as storing, retrieving, and formatting configuration data.
*
* @author Marcel Peterkau
* @date 09.01.2024
* Notes:
* - The system always boots with defaults in RAM; EEPROM is optional.
* - DTC handling for EEPROM availability and integrity is centralized in EEPROM_Process().
*/
#ifndef _CONFIG_H_
#define _CONFIG_H_
#include <Arduino.h>
#include <Wire.h>
#include <stdint.h>
#include <I2C_eeprom.h>
#include "dtc.h"
#include "common.h"
#define EEPROM_STRUCTURE_REVISION 4 // Increment this version when changing EEPROM structures
// Increment when EEPROM structure changes
#define EEPROM_STRUCTURE_REVISION 4
#if PCB_REV == 1 || PCB_REV == 2 || PCB_REV == 3
#define EEPROM_SIZE_BYTES I2C_DEVICESIZE_24LC64
@@ -29,9 +28,14 @@
#define EEPROM_SIZE_BYTES I2C_DEVICESIZE_24LC256
#endif
/**
* @brief EEPROM request state machine codes.
*
* Used by globals.requestEEAction to schedule EEPROM operations.
*/
typedef enum EERequest_e
{
EE_IDLE,
EE_IDLE = 0,
EE_CFG_SAVE,
EE_CFG_LOAD,
EE_CFG_FORMAT,
@@ -39,11 +43,13 @@ typedef enum EERequest_e
EE_PDS_LOAD,
EE_PDS_FORMAT,
EE_FORMAT_ALL,
EE_ALL_SAVE
EE_ALL_SAVE,
EE_REINITIALIZE
} EERequest_t;
// Structure for persistence data stored in EEPROM
/**
* @brief Wear-levelled persistence data block.
*/
typedef struct
{
uint16_t writeCycleCounter;
@@ -54,7 +60,9 @@ typedef struct
uint32_t checksum;
} persistenceData_t;
// Structure for configuration settings stored in EEPROM
/**
* @brief User configuration stored in EEPROM.
*/
typedef struct
{
uint8_t EEPROM_Version;
@@ -85,7 +93,9 @@ typedef struct
uint32_t checksum;
} LubeConfig_t;
// Default configuration settings
/**
* @brief Factory defaults for configuration (in RAM).
*/
const LubeConfig_t LubeConfig_defaults = {
0, 8000, 4000, 320, DEFAULT_PUMP_DOSE, 30, 1, 150, 70, 18, 2000, 25, 500, 10, SOURCE_IMPULSE,
BAUD_115200,
@@ -100,21 +110,31 @@ const LubeConfig_t LubeConfig_defaults = {
true,
0};
/* ==== Public API ==== */
// Initialization & main process
void InitEEPROM();
void EEPROM_Process();
// Config & persistence access
void StoreConfig_EEPROM();
void GetConfig_EEPROM();
void StorePersistence_EEPROM();
void GetPersistence_EEPROM();
void FormatConfig_EEPROM();
void FormatPersistence_EEPROM();
void MovePersistencePage_EEPROM(boolean reset);
// Utilities
uint32_t Checksum_EEPROM(uint8_t const *data, size_t len);
void dumpEEPROM(uint16_t memoryAddress, uint16_t length);
void MovePersistencePage_EEPROM(boolean reset);
uint32_t ConfigSanityCheck(bool autocorrect = false);
bool validateWiFiString(char *string, size_t size);
/* ==== Externals ==== */
extern LubeConfig_t LubeConfig;
extern persistenceData_t PersistenceData;
extern uint16_t eePersistenceMarker;
extern uint16_t eePersistenceAddress;
#endif // _CONFIG_H_

View File

@@ -27,7 +27,7 @@ typedef struct Globals_s
EERequest_t requestEEAction = EE_IDLE; /**< EEPROM-related request */
char DeviceName[33]; /**< Device name */
char FlashVersion[10]; /**< Flash version */
uint16_t eePersistanceAdress; /**< EEPROM persistence address */
uint16_t eePersistenceAddress; /**< EEPROM persistence address */
uint8_t TankPercentage; /**< Tank percentage */
bool hasDTC; /**< Flag indicating the presence of Diagnostic Trouble Codes (DTC) */
bool measurementActive; /**< Flag indicating active measurement */