2021-09-13 23:51:26 -07:00
/*
Speeduino - Simple engine management for the Arduino Mega 2560 platform
Copyright ( C ) Josh Stewart
A full copy of the license may be found in the projects root directory
*/
/** @file
* Lower level ConfigPage * , Table2D , Table3D and EEPROM storage operations .
*/
# include "globals.h"
# include EEPROM_LIB_H //This is defined in the board .h files
# include "storage.h"
# include "pages.h"
//The maximum number of write operations that will be performed in one go. If we try to write to the EEPROM too fast (Each write takes ~3ms) then the rest of the system can hang)
# if defined(CORE_STM32) || defined(CORE_TEENSY) & !defined(USE_SPI_EEPROM)
# define EEPROM_MAX_WRITE_BLOCK 64
# else
2022-01-17 21:57:55 -08:00
# define EEPROM_MAX_WRITE_BLOCK 12
//#define EEPROM_MAX_WRITE_BLOCK 8
2021-09-13 23:51:26 -07:00
# endif
# define EEPROM_DATA_VERSION 0
// Calibration data is stored at the end of the EEPROM (This is in case any further calibration tables are needed as they are large blocks)
# define STORAGE_END 0xFFF // Should be E2END?
# define EEPROM_CALIBRATION_CLT_VALUES (STORAGE_END-sizeof(cltCalibration_values))
# define EEPROM_CALIBRATION_CLT_BINS (EEPROM_CALIBRATION_CLT_VALUES-sizeof(cltCalibration_bins))
# define EEPROM_CALIBRATION_IAT_VALUES (EEPROM_CALIBRATION_CLT_BINS-sizeof(iatCalibration_values))
# define EEPROM_CALIBRATION_IAT_BINS (EEPROM_CALIBRATION_IAT_VALUES-sizeof(iatCalibration_bins))
# define EEPROM_CALIBRATION_O2_VALUES (EEPROM_CALIBRATION_IAT_BINS-sizeof(o2Calibration_values))
# define EEPROM_CALIBRATION_O2_BINS (EEPROM_CALIBRATION_O2_VALUES-sizeof(o2Calibration_bins))
# define EEPROM_LAST_BARO (EEPROM_CALIBRATION_O2_BINS-1)
static bool eepromWritesPending = false ;
2022-01-06 21:49:04 -08:00
static bool forceBurn = false ;
2022-01-17 21:57:55 -08:00
bool deferEEPROMWrites = false ;
2021-09-13 23:51:26 -07:00
bool isEepromWritePending ( )
{
return eepromWritesPending ;
}
/** Write all config pages to EEPROM.
*/
void writeAllConfig ( )
{
uint8_t pageCount = getPageCount ( ) ;
uint8_t page = 1U ;
writeConfig ( page + + ) ;
2022-01-06 21:49:04 -08:00
while ( page < pageCount & & ( ! eepromWritesPending | | forceBurn ) )
2021-09-13 23:51:26 -07:00
{
writeConfig ( page + + ) ;
}
}
2022-01-06 21:49:04 -08:00
void enableForceBurn ( ) { forceBurn = true ; }
void disableForceBurn ( ) { forceBurn = false ; }
2021-09-13 23:51:26 -07:00
2022-01-06 21:49:04 -08:00
// ================================= Internal write support ===============================
2021-11-17 16:30:29 -08:00
struct write_location {
2021-09-13 23:51:26 -07:00
eeprom_address_t address ;
2022-01-06 21:49:04 -08:00
uint16_t counter ;
2021-09-13 23:51:26 -07:00
2021-11-17 16:30:29 -08:00
/** Update byte to EEPROM by first comparing content and the need to write it.
We only ever write to the EEPROM where the new value is different from the currently stored byte
This is due to the limited write life of the EEPROM ( Approximately 100 , 000 writes )
*/
void update ( uint8_t value )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
if ( EEPROM . read ( address ) ! = value )
{
EEPROM . write ( address , value ) ;
+ + counter ;
}
2021-09-13 23:51:26 -07:00
}
2021-11-17 16:30:29 -08:00
write_location & operator + + ( )
{
+ + address ;
return * this ;
}
bool can_write ( ) const
{
2022-01-06 21:49:04 -08:00
return ( counter < = EEPROM_MAX_WRITE_BLOCK ) ;
2021-11-17 16:30:29 -08:00
}
} ;
2021-09-13 23:51:26 -07:00
static inline write_location write_range ( const byte * pStart , const byte * pEnd , write_location location )
{
2022-01-06 21:49:04 -08:00
while ( ( location . can_write ( ) | | forceBurn ) & & pStart ! = pEnd )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
location . update ( * pStart ) ;
+ + pStart ;
+ + location ;
2021-09-13 23:51:26 -07:00
}
return location ;
}
2021-11-17 16:30:29 -08:00
static inline write_location write ( const table_row_iterator & row , write_location location )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
return write_range ( & * row , row . end ( ) , location ) ;
2021-09-13 23:51:26 -07:00
}
2021-11-17 16:30:29 -08:00
static inline write_location write ( table_value_iterator it , write_location location )
2021-09-13 23:51:26 -07:00
{
2022-01-06 21:49:04 -08:00
while ( ( location . can_write ( ) | | forceBurn ) & & ! it . at_end ( ) )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
location = write ( * it , location ) ;
+ + it ;
2021-09-13 23:51:26 -07:00
}
return location ;
}
2021-11-17 16:30:29 -08:00
static inline write_location write ( table_axis_iterator it , write_location location )
2021-09-13 23:51:26 -07:00
{
2022-01-06 21:49:04 -08:00
while ( ( location . can_write ( ) | | forceBurn ) & & ! it . at_end ( ) )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
location . update ( ( byte ) * it ) ;
+ + location ;
+ + it ;
2021-09-13 23:51:26 -07:00
}
return location ;
}
2021-11-17 16:30:29 -08:00
static inline write_location writeTable ( const void * pTable , table_type_t key , write_location location )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
return write ( y_begin ( pTable , key ) . reverse ( ) ,
write ( x_begin ( pTable , key ) ,
write ( rows_begin ( pTable , key ) , location ) ) ) ;
2021-09-13 23:51:26 -07:00
}
2022-01-17 21:57:55 -08:00
//Simply an alias for EEPROM.update()
void EEPROMWriteRaw ( uint16_t address , uint8_t data ) { EEPROM . update ( address , data ) ; }
uint8_t EEPROMReadRaw ( uint16_t address ) { return EEPROM . read ( address ) ; }
2021-09-13 23:51:26 -07:00
// ================================= End write support ===============================
/** Write a table or map to EEPROM storage.
Takes the current configuration ( config pages and maps )
and writes them to EEPROM as per the layout defined in storage . h .
*/
void writeConfig ( uint8_t pageNum )
{
write_location result = { 0 , 0 } ;
2022-01-17 21:57:55 -08:00
if ( deferEEPROMWrites = = true ) { result . counter = ( EEPROM_MAX_WRITE_BLOCK + 1 ) ; } //If we are deferring writes then we don't want to write anything. This will force can_write() to return false and the write will be skipped.
2021-09-13 23:51:26 -07:00
switch ( pageNum )
{
case veMapPage :
/*---------------------------------------------------
| Fuel table ( See storage . h for data layout ) - Page 1
| 16 x16 table itself + the 16 values along each of the axis
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2021-11-17 16:30:29 -08:00
result = writeTable ( & fuelTable , fuelTable . type_key , { EEPROM_CONFIG1_MAP , 0 } ) ;
2021-09-13 23:51:26 -07:00
break ;
case veSetPage :
/*---------------------------------------------------
| Config page 2 ( See storage . h for data layout )
| 64 byte long config table
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
result = write_range ( ( byte * ) & configPage2 , ( byte * ) & configPage2 + sizeof ( configPage2 ) , { EEPROM_CONFIG2_START , 0 } ) ;
break ;
case ignMapPage :
/*---------------------------------------------------
| Ignition table ( See storage . h for data layout ) - Page 1
| 16 x16 table itself + the 16 values along each of the axis
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2021-11-17 16:30:29 -08:00
result = writeTable ( & ignitionTable , ignitionTable . type_key , { EEPROM_CONFIG3_MAP , 0 } ) ;
2021-09-13 23:51:26 -07:00
break ;
case ignSetPage :
/*---------------------------------------------------
| Config page 2 ( See storage . h for data layout )
| 64 byte long config table
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
result = write_range ( ( byte * ) & configPage4 , ( byte * ) & configPage4 + sizeof ( configPage4 ) , { EEPROM_CONFIG4_START , 0 } ) ;
break ;
case afrMapPage :
/*---------------------------------------------------
| AFR table ( See storage . h for data layout ) - Page 5
| 16 x16 table itself + the 16 values along each of the axis
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2021-11-17 16:30:29 -08:00
result = writeTable ( & afrTable , afrTable . type_key , { EEPROM_CONFIG5_MAP , 0 } ) ;
2021-09-13 23:51:26 -07:00
break ;
case afrSetPage :
/*---------------------------------------------------
| Config page 3 ( See storage . h for data layout )
| 64 byte long config table
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
result = write_range ( ( byte * ) & configPage6 , ( byte * ) & configPage6 + sizeof ( configPage6 ) , { EEPROM_CONFIG6_START , 0 } ) ;
break ;
case boostvvtPage :
/*---------------------------------------------------
| Boost and vvt tables ( See storage . h for data layout ) - Page 8
| 8 x8 table itself + the 8 values along each of the axis
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2021-11-17 16:30:29 -08:00
result = writeTable ( & boostTable , boostTable . type_key , { EEPROM_CONFIG7_MAP1 , 0 } ) ;
result = writeTable ( & vvtTable , vvtTable . type_key , { EEPROM_CONFIG7_MAP2 , result . counter } ) ;
result = writeTable ( & stagingTable , stagingTable . type_key , { EEPROM_CONFIG7_MAP3 , result . counter } ) ;
2021-09-13 23:51:26 -07:00
break ;
case seqFuelPage :
/*---------------------------------------------------
| Fuel trim tables ( See storage . h for data layout ) - Page 9
| 6 x6 tables itself + the 6 values along each of the axis
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2021-11-17 16:30:29 -08:00
result = writeTable ( & trim1Table , trim1Table . type_key , { EEPROM_CONFIG8_MAP1 , 0 } ) ;
result = writeTable ( & trim2Table , trim2Table . type_key , { EEPROM_CONFIG8_MAP2 , result . counter } ) ;
result = writeTable ( & trim3Table , trim3Table . type_key , { EEPROM_CONFIG8_MAP3 , result . counter } ) ;
result = writeTable ( & trim4Table , trim4Table . type_key , { EEPROM_CONFIG8_MAP4 , result . counter } ) ;
result = writeTable ( & trim5Table , trim5Table . type_key , { EEPROM_CONFIG8_MAP5 , result . counter } ) ;
result = writeTable ( & trim6Table , trim6Table . type_key , { EEPROM_CONFIG8_MAP6 , result . counter } ) ;
result = writeTable ( & trim7Table , trim7Table . type_key , { EEPROM_CONFIG8_MAP7 , result . counter } ) ;
result = writeTable ( & trim8Table , trim8Table . type_key , { EEPROM_CONFIG8_MAP8 , result . counter } ) ;
2021-09-13 23:51:26 -07:00
break ;
case canbusPage :
/*---------------------------------------------------
| Config page 10 ( See storage . h for data layout )
| 192 byte long config table
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
result = write_range ( ( byte * ) & configPage9 , ( byte * ) & configPage9 + sizeof ( configPage9 ) , { EEPROM_CONFIG9_START , 0 } ) ;
break ;
case warmupPage :
/*---------------------------------------------------
| Config page 11 ( See storage . h for data layout )
| 192 byte long config table
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
result = write_range ( ( byte * ) & configPage10 , ( byte * ) & configPage10 + sizeof ( configPage10 ) , { EEPROM_CONFIG10_START , 0 } ) ;
break ;
case fuelMap2Page :
/*---------------------------------------------------
| Fuel table 2 ( See storage . h for data layout )
| 16 x16 table itself + the 16 values along each of the axis
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2021-11-17 16:30:29 -08:00
result = writeTable ( & fuelTable2 , fuelTable2 . type_key , { EEPROM_CONFIG11_MAP , 0 } ) ;
2021-09-13 23:51:26 -07:00
break ;
case wmiMapPage :
/*---------------------------------------------------
| WMI and Dwell tables ( See storage . h for data layout ) - Page 12
| 8 x8 WMI table itself + the 8 values along each of the axis
| 8 x8 VVT2 table + the 8 values along each of the axis
| 4 x4 Dwell table itself + the 4 values along each of the axis
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2021-11-17 16:30:29 -08:00
result = writeTable ( & wmiTable , wmiTable . type_key , { EEPROM_CONFIG12_MAP , 0 } ) ;
result = writeTable ( & vvt2Table , vvt2Table . type_key , { EEPROM_CONFIG12_MAP2 , result . counter } ) ;
result = writeTable ( & dwellTable , dwellTable . type_key , { EEPROM_CONFIG12_MAP3 , result . counter } ) ;
2021-09-13 23:51:26 -07:00
break ;
case progOutsPage :
/*---------------------------------------------------
| Config page 13 ( See storage . h for data layout )
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
result = write_range ( ( byte * ) & configPage13 , ( byte * ) & configPage13 + sizeof ( configPage13 ) , { EEPROM_CONFIG13_START , 0 } ) ;
break ;
case ignMap2Page :
/*---------------------------------------------------
| Ignition table ( See storage . h for data layout ) - Page 1
| 16 x16 table itself + the 16 values along each of the axis
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2021-11-17 16:30:29 -08:00
result = writeTable ( & ignitionTable2 , ignitionTable2 . type_key , { EEPROM_CONFIG14_MAP , 0 } ) ;
2021-09-13 23:51:26 -07:00
break ;
default :
break ;
}
2021-11-17 16:30:29 -08:00
eepromWritesPending = ! result . can_write ( ) ;
2021-09-13 23:51:26 -07:00
}
/** Reset all configPage* structs (2,4,6,9,10,13) and write them full of null-bytes.
*/
void resetConfigPages ( )
{
for ( uint8_t page = 1 ; page < getPageCount ( ) ; + + page )
{
page_iterator_t entity = page_begin ( page ) ;
while ( entity . type ! = End )
{
if ( entity . type = = Raw )
{
memset ( entity . pData , 0 , entity . size ) ;
}
entity = advance ( entity ) ;
}
}
}
// ================================= Internal read support ===============================
/** Load range of bytes form EEPROM offset to memory.
* @ param address - start offset in EEPROM
* @ param pFirst - Start memory address
* @ param pLast - End memory address
*/
static inline eeprom_address_t load_range ( eeprom_address_t address , byte * pFirst , const byte * pLast )
{
2021-11-18 19:22:21 -08:00
# if defined(CORE_AVR)
// The generic code in the #else branch works but this provides a 45% speed up on AVR
size_t size = pLast - pFirst ;
eeprom_read_block ( pFirst , ( void * ) address , size ) ;
return address + size ;
# else
2021-09-13 23:51:26 -07:00
for ( ; pFirst ! = pLast ; + + address , ( void ) + + pFirst )
{
* pFirst = EEPROM . read ( address ) ;
}
return address ;
2021-11-18 19:22:21 -08:00
# endif
2021-09-13 23:51:26 -07:00
}
2021-11-17 16:30:29 -08:00
static inline eeprom_address_t load ( table_row_iterator row , eeprom_address_t address )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
return load_range ( address , & * row , row . end ( ) ) ;
2021-09-13 23:51:26 -07:00
}
2021-11-17 16:30:29 -08:00
static inline eeprom_address_t load ( table_value_iterator it , eeprom_address_t address )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
while ( ! it . at_end ( ) )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
address = load ( * it , address ) ;
+ + it ;
2021-09-13 23:51:26 -07:00
}
return address ;
}
2021-11-17 16:30:29 -08:00
static inline eeprom_address_t load ( table_axis_iterator it , eeprom_address_t address )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
while ( ! it . at_end ( ) )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
* it = EEPROM . read ( address ) ;
2021-09-13 23:51:26 -07:00
+ + address ;
2021-11-17 16:30:29 -08:00
+ + it ;
2021-09-13 23:51:26 -07:00
}
return address ;
}
2021-11-17 16:30:29 -08:00
static inline eeprom_address_t loadTable ( void * pTable , table_type_t key , eeprom_address_t address )
2021-09-13 23:51:26 -07:00
{
2021-11-17 16:30:29 -08:00
return load ( y_begin ( pTable , key ) . reverse ( ) ,
load ( x_begin ( pTable , key ) ,
load ( rows_begin ( pTable , key ) , address ) ) ) ;
2021-09-13 23:51:26 -07:00
}
// ================================= End internal read support ===============================
/** Load all config tables from storage.
*/
void loadConfig ( )
{
2021-11-17 16:30:29 -08:00
loadTable ( & fuelTable , fuelTable . type_key , EEPROM_CONFIG1_MAP ) ;
2021-09-13 23:51:26 -07:00
load_range ( EEPROM_CONFIG2_START , ( byte * ) & configPage2 , ( byte * ) & configPage2 + sizeof ( configPage2 ) ) ;
//*********************************************************************************************************************************************************************************
//IGNITION CONFIG PAGE (2)
2021-11-17 16:30:29 -08:00
loadTable ( & ignitionTable , ignitionTable . type_key , EEPROM_CONFIG3_MAP ) ;
2021-09-13 23:51:26 -07:00
load_range ( EEPROM_CONFIG4_START , ( byte * ) & configPage4 , ( byte * ) & configPage4 + sizeof ( configPage4 ) ) ;
//*********************************************************************************************************************************************************************************
//AFR TARGET CONFIG PAGE (3)
2021-11-17 16:30:29 -08:00
loadTable ( & afrTable , afrTable . type_key , EEPROM_CONFIG5_MAP ) ;
2021-09-13 23:51:26 -07:00
load_range ( EEPROM_CONFIG6_START , ( byte * ) & configPage6 , ( byte * ) & configPage6 + sizeof ( configPage6 ) ) ;
//*********************************************************************************************************************************************************************************
// Boost and vvt tables load
2021-11-17 16:30:29 -08:00
loadTable ( & boostTable , boostTable . type_key , EEPROM_CONFIG7_MAP1 ) ;
loadTable ( & vvtTable , vvtTable . type_key , EEPROM_CONFIG7_MAP2 ) ;
loadTable ( & stagingTable , stagingTable . type_key , EEPROM_CONFIG7_MAP3 ) ;
2021-09-13 23:51:26 -07:00
//*********************************************************************************************************************************************************************************
// Fuel trim tables load
2021-11-17 16:30:29 -08:00
loadTable ( & trim1Table , trim1Table . type_key , EEPROM_CONFIG8_MAP1 ) ;
loadTable ( & trim2Table , trim2Table . type_key , EEPROM_CONFIG8_MAP2 ) ;
loadTable ( & trim3Table , trim3Table . type_key , EEPROM_CONFIG8_MAP3 ) ;
loadTable ( & trim4Table , trim4Table . type_key , EEPROM_CONFIG8_MAP4 ) ;
loadTable ( & trim5Table , trim5Table . type_key , EEPROM_CONFIG8_MAP5 ) ;
loadTable ( & trim6Table , trim6Table . type_key , EEPROM_CONFIG8_MAP6 ) ;
loadTable ( & trim7Table , trim7Table . type_key , EEPROM_CONFIG8_MAP7 ) ;
loadTable ( & trim8Table , trim8Table . type_key , EEPROM_CONFIG8_MAP8 ) ;
2021-09-13 23:51:26 -07:00
//*********************************************************************************************************************************************************************************
//canbus control page load
load_range ( EEPROM_CONFIG9_START , ( byte * ) & configPage9 , ( byte * ) & configPage9 + sizeof ( configPage9 ) ) ;
//*********************************************************************************************************************************************************************************
//CONFIG PAGE (10)
load_range ( EEPROM_CONFIG10_START , ( byte * ) & configPage10 , ( byte * ) & configPage10 + sizeof ( configPage10 ) ) ;
//*********************************************************************************************************************************************************************************
//Fuel table 2 (See storage.h for data layout)
2021-11-17 16:30:29 -08:00
loadTable ( & fuelTable2 , fuelTable2 . type_key , EEPROM_CONFIG11_MAP ) ;
2021-09-13 23:51:26 -07:00
//*********************************************************************************************************************************************************************************
// WMI, VVT2 and Dwell table load
2021-11-17 16:30:29 -08:00
loadTable ( & wmiTable , wmiTable . type_key , EEPROM_CONFIG12_MAP ) ;
loadTable ( & vvt2Table , vvt2Table . type_key , EEPROM_CONFIG12_MAP2 ) ;
loadTable ( & dwellTable , dwellTable . type_key , EEPROM_CONFIG12_MAP3 ) ;
2021-09-13 23:51:26 -07:00
//*********************************************************************************************************************************************************************************
//CONFIG PAGE (13)
load_range ( EEPROM_CONFIG13_START , ( byte * ) & configPage13 , ( byte * ) & configPage13 + sizeof ( configPage13 ) ) ;
//*********************************************************************************************************************************************************************************
//SECOND IGNITION CONFIG PAGE (14)
2021-11-17 16:30:29 -08:00
loadTable ( & ignitionTable2 , ignitionTable2 . type_key , EEPROM_CONFIG14_MAP ) ;
2021-09-13 23:51:26 -07:00
//*********************************************************************************************************************************************************************************
}
/** Read the calibration information from EEPROM.
This is separate from the config load as the calibrations do not exist as pages within the ini file for Tuner Studio .
*/
void loadCalibration ( )
{
// If you modify this function be sure to also modify writeCalibration();
// it should be a mirror image of this function.
EEPROM . get ( EEPROM_CALIBRATION_O2_BINS , o2Calibration_bins ) ;
EEPROM . get ( EEPROM_CALIBRATION_O2_VALUES , o2Calibration_values ) ;
EEPROM . get ( EEPROM_CALIBRATION_IAT_BINS , iatCalibration_bins ) ;
EEPROM . get ( EEPROM_CALIBRATION_IAT_VALUES , iatCalibration_values ) ;
EEPROM . get ( EEPROM_CALIBRATION_CLT_BINS , cltCalibration_bins ) ;
EEPROM . get ( EEPROM_CALIBRATION_CLT_VALUES , cltCalibration_values ) ;
}
/** Write calibration tables to EEPROM.
This takes the values in the 3 calibration tables ( Coolant , Inlet temp and O2 )
and saves them to the EEPROM .
*/
void writeCalibration ( )
{
// If you modify this function be sure to also modify loadCalibration();
// it should be a mirror image of this function.
EEPROM . put ( EEPROM_CALIBRATION_O2_BINS , o2Calibration_bins ) ;
EEPROM . put ( EEPROM_CALIBRATION_O2_VALUES , o2Calibration_values ) ;
EEPROM . put ( EEPROM_CALIBRATION_IAT_BINS , iatCalibration_bins ) ;
EEPROM . put ( EEPROM_CALIBRATION_IAT_VALUES , iatCalibration_values ) ;
EEPROM . put ( EEPROM_CALIBRATION_CLT_BINS , cltCalibration_bins ) ;
EEPROM . put ( EEPROM_CALIBRATION_CLT_VALUES , cltCalibration_values ) ;
}
2022-01-08 22:05:05 -08:00
void writeCalibrationPage ( uint8_t pageNum )
{
if ( pageNum = = O2_CALIBRATION_PAGE )
{
EEPROM . put ( EEPROM_CALIBRATION_O2_BINS , o2Calibration_bins ) ;
EEPROM . put ( EEPROM_CALIBRATION_O2_VALUES , o2Calibration_values ) ;
}
else if ( pageNum = = IAT_CALIBRATION_PAGE )
{
EEPROM . put ( EEPROM_CALIBRATION_IAT_BINS , iatCalibration_bins ) ;
EEPROM . put ( EEPROM_CALIBRATION_IAT_VALUES , iatCalibration_values ) ;
}
else if ( pageNum = = CLT_CALIBRATION_PAGE )
{
EEPROM . put ( EEPROM_CALIBRATION_CLT_BINS , cltCalibration_bins ) ;
EEPROM . put ( EEPROM_CALIBRATION_CLT_VALUES , cltCalibration_values ) ;
}
}
2021-09-13 23:51:26 -07:00
static eeprom_address_t compute_crc_address ( uint8_t pageNum )
{
return EEPROM_LAST_BARO - ( ( getPageCount ( ) - pageNum ) * sizeof ( uint32_t ) ) ;
}
/** Write CRC32 checksum to EEPROM.
Takes a page number and CRC32 value then stores it in the relevant place in EEPROM
2021-10-20 14:03:29 -07:00
@ param pageNum - Config page number
@ param crcValue - CRC32 checksum
2021-09-13 23:51:26 -07:00
*/
void storePageCRC32 ( uint8_t pageNum , uint32_t crcValue )
{
EEPROM . put ( compute_crc_address ( pageNum ) , crcValue ) ;
}
/** Retrieves and returns the 4 byte CRC32 checksum for a given page from EEPROM.
2021-10-20 14:03:29 -07:00
@ param pageNum - Config page number
2021-09-13 23:51:26 -07:00
*/
uint32_t readPageCRC32 ( uint8_t pageNum )
{
uint32_t crc32_val ;
return EEPROM . get ( compute_crc_address ( pageNum ) , crc32_val ) ;
}
2022-02-10 16:59:36 -08:00
/** Same as above, but writes the CRC32 for the calibration page rather than tune data
@ param pageNum - Calibration page number
@ param crcValue - CRC32 checksum
*/
void storeCalibrationCRC32 ( uint8_t calibrationPageNum , uint32_t calibrationCRC )
{
uint16_t targetAddress ;
switch ( calibrationPageNum )
{
case O2_CALIBRATION_PAGE :
targetAddress = EEPROM_CALIBRATION_O2_CRC ;
break ;
case IAT_CALIBRATION_PAGE :
targetAddress = EEPROM_CALIBRATION_IAT_CRC ;
break ;
case CLT_CALIBRATION_PAGE :
targetAddress = EEPROM_CALIBRATION_CLT_CRC ;
break ;
default :
targetAddress = EEPROM_CALIBRATION_CLT_CRC ; //Obviously should never happen
break ;
}
EEPROM . put ( targetAddress , calibrationCRC ) ;
}
/** Retrieves and returns the 4 byte CRC32 checksum for a given calibration page from EEPROM.
@ param pageNum - Config page number
*/
uint32_t readCalibrationCRC32 ( uint8_t calibrationPageNum )
{
uint32_t crc32_val ;
uint16_t targetAddress ;
switch ( calibrationPageNum )
{
case O2_CALIBRATION_PAGE :
targetAddress = EEPROM_CALIBRATION_O2_CRC ;
break ;
case IAT_CALIBRATION_PAGE :
targetAddress = EEPROM_CALIBRATION_IAT_CRC ;
break ;
case CLT_CALIBRATION_PAGE :
targetAddress = EEPROM_CALIBRATION_CLT_CRC ;
break ;
default :
targetAddress = EEPROM_CALIBRATION_CLT_CRC ; //Obviously should never happen
break ;
}
EEPROM . get ( targetAddress , crc32_val ) ;
return crc32_val ;
}
2022-01-17 21:57:55 -08:00
uint16_t getEEPROMSize ( )
{
return EEPROM . length ( ) ;
}
2021-09-13 23:51:26 -07:00
// Utility functions.
// By having these in this file, it prevents other files from calling EEPROM functions directly. This is useful due to differences in the EEPROM libraries on different devces
/// Read last stored barometer reading from EEPROM.
byte readLastBaro ( ) { return EEPROM . read ( EEPROM_LAST_BARO ) ; }
/// Write last acquired arometer reading to EEPROM.
void storeLastBaro ( byte newValue ) { EEPROM . update ( EEPROM_LAST_BARO , newValue ) ; }
/// Read EEPROM current data format version (from offset EEPROM_DATA_VERSION).
byte readEEPROMVersion ( ) { return EEPROM . read ( EEPROM_DATA_VERSION ) ; }
/// Store EEPROM current data format version (to offset EEPROM_DATA_VERSION).
void storeEEPROMVersion ( byte newVersion ) { EEPROM . update ( EEPROM_DATA_VERSION , newVersion ) ; }