141 lines
3.4 KiB
C
141 lines
3.4 KiB
C
/**
|
|
* @file config.h
|
|
* @brief Configuration structures and EEPROM API for ChainLube firmware.
|
|
*
|
|
* Defines EEPROM layout versions, configuration and persistence data structures,
|
|
* and the public functions for storing, loading, formatting and validating
|
|
* configuration/persistence records.
|
|
*
|
|
* 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 <stdint.h>
|
|
#include <I2C_eeprom.h>
|
|
#include "dtc.h"
|
|
#include "common.h"
|
|
|
|
// 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
|
|
#elif PCB_REV == 4
|
|
#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 = 0,
|
|
EE_CFG_SAVE,
|
|
EE_CFG_LOAD,
|
|
EE_CFG_FORMAT,
|
|
EE_PDS_SAVE,
|
|
EE_PDS_LOAD,
|
|
EE_PDS_FORMAT,
|
|
EE_FORMAT_ALL,
|
|
EE_ALL_SAVE,
|
|
EE_REINITIALIZE
|
|
} EERequest_t;
|
|
|
|
/**
|
|
* @brief Wear-levelled persistence data block.
|
|
*/
|
|
typedef struct
|
|
{
|
|
uint16_t writeCycleCounter;
|
|
uint32_t tankRemain_microL;
|
|
uint32_t TravelDistance_highRes_mm;
|
|
uint32_t odometer_mm;
|
|
uint32_t odometer;
|
|
uint32_t checksum;
|
|
} persistenceData_t;
|
|
|
|
/**
|
|
* @brief User configuration stored in EEPROM.
|
|
*/
|
|
typedef struct
|
|
{
|
|
uint8_t EEPROM_Version;
|
|
uint32_t DistancePerLube_Default;
|
|
uint32_t DistancePerLube_Rain;
|
|
uint32_t tankCapacity_ml;
|
|
uint32_t amountPerDose_microL;
|
|
uint8_t TankRemindAtPercentage;
|
|
uint8_t PulsePerRevolution;
|
|
uint32_t TireWidth_mm;
|
|
uint32_t TireWidthHeight_Ratio;
|
|
uint32_t RimDiameter_Inch;
|
|
uint32_t DistancePerRevolution_mm;
|
|
uint16_t BleedingPulses;
|
|
uint16_t WashMode_Distance;
|
|
uint16_t WashMode_Interval;
|
|
SpeedSource_t SpeedSource;
|
|
GPSBaudRate_t GPSBaudRate;
|
|
CANSource_t CANSource;
|
|
bool LED_Mode_Flash;
|
|
uint8_t LED_Max_Brightness;
|
|
uint8_t LED_Min_Brightness;
|
|
char wifi_ap_ssid[33];
|
|
char wifi_ap_password[64];
|
|
char wifi_client_ssid[33];
|
|
char wifi_client_password[64];
|
|
bool wifi_autoconnect;
|
|
uint32_t checksum;
|
|
} LubeConfig_t;
|
|
|
|
/**
|
|
* @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,
|
|
KTM_890_ADV_R_2021,
|
|
false,
|
|
255,
|
|
5,
|
|
"ChainLube",
|
|
QUOTE(WIFI_AP_PASSWORD),
|
|
QUOTE(WIFI_SSID_CLIENT),
|
|
QUOTE(WIFI_PASSWORD_CLIENT),
|
|
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);
|
|
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 eePersistenceAddress;
|
|
|
|
#endif // _CONFIG_H_
|