2013-07-18 04:53:40 -07:00
# include <EEPROM.h>
2015-02-14 09:04:00 -08:00
# include "storage.h"
# include "globals.h"
2014-05-12 04:28:24 -07:00
//#include "table.h"
2013-07-16 05:29:17 -07:00
2015-02-14 09:04:00 -08:00
2013-08-25 21:11:47 -07:00
/*
Takes the current configuration ( config pages and maps )
2014-01-09 23:54:33 -08:00
and writes them to EEPROM as per the layout defined in storage . h
2013-08-25 21:11:47 -07:00
*/
2013-07-18 04:53:40 -07:00
void writeConfig ( )
{
/*
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 )
*/
2013-08-25 21:11:47 -07:00
int offset ;
//Create a pointer to the config page
byte * pnt_configPage ;
pnt_configPage = ( byte * ) & configPage1 ; //Create a pointer to Page 1 in memory
2013-07-18 04:53:40 -07:00
if ( EEPROM . read ( 0 ) ! = data_structure_version ) { EEPROM . write ( 0 , data_structure_version ) ; } //Write the data structure version
2013-08-25 21:11:47 -07:00
//Fuel table (See storage.h for data layout)
2013-07-18 04:53:40 -07:00
if ( EEPROM . read ( EEPROM_CONFIG1_XSIZE ) ! = fuelTable . xSize ) { EEPROM . write ( EEPROM_CONFIG1_XSIZE , fuelTable . xSize ) ; } //Write the VE Tables RPM dimension size
if ( EEPROM . read ( EEPROM_CONFIG1_YSIZE ) ! = fuelTable . ySize ) { EEPROM . write ( EEPROM_CONFIG1_YSIZE , fuelTable . ySize ) ; } //Write the VE Tables MAP/TPS dimension size
2013-08-25 21:11:47 -07:00
for ( int x = EEPROM_CONFIG1_MAP ; x < EEPROM_CONFIG1_XBINS ; x + + )
2013-07-18 04:53:40 -07:00
{
2013-08-25 21:11:47 -07:00
offset = x - EEPROM_CONFIG1_MAP ;
if ( EEPROM . read ( x ) ! = fuelTable . values [ 7 - offset / 8 ] [ offset % 8 ] ) { EEPROM . write ( x , fuelTable . values [ 7 - offset / 8 ] [ offset % 8 ] ) ; } //Write the 8x8 map
}
2013-09-15 05:39:55 -07:00
2013-08-25 21:11:47 -07:00
//RPM bins
for ( int x = EEPROM_CONFIG1_XBINS ; x < EEPROM_CONFIG1_YBINS ; x + + )
{
offset = x - EEPROM_CONFIG1_XBINS ;
2013-09-14 04:29:21 -07:00
if ( EEPROM . read ( x ) ! = byte ( fuelTable . axisX [ offset ] / 100 ) ) { EEPROM . write ( x , byte ( fuelTable . axisX [ offset ] / 100 ) ) ; } //RPM bins are divided by 100 and converted to a byte
2013-08-25 21:11:47 -07:00
}
//TPS/MAP bins
for ( int x = EEPROM_CONFIG1_YBINS ; x < EEPROM_CONFIG1_SETTINGS ; x + + )
{
offset = x - EEPROM_CONFIG1_YBINS ;
if ( EEPROM . read ( x ) ! = fuelTable . axisY [ offset ] ) { EEPROM . write ( x , fuelTable . axisY [ offset ] ) ; }
}
2014-01-09 23:54:33 -08:00
//The next 45 bytes can simply be pulled straight from the configTable
2014-02-06 01:48:19 -08:00
for ( int x = EEPROM_CONFIG1_SETTINGS ; x < EEPROM_CONFIG1_END ; x + + )
2013-08-25 21:11:47 -07:00
{
2013-09-15 17:18:33 -07:00
if ( EEPROM . read ( x ) ! = * ( pnt_configPage + byte ( x - EEPROM_CONFIG1_SETTINGS ) ) ) { EEPROM . write ( x , * ( pnt_configPage + byte ( x - EEPROM_CONFIG1_SETTINGS ) ) ) ; }
2013-07-18 04:53:40 -07:00
}
//That concludes the writing of the VE table
2013-09-11 05:06:13 -07:00
//*********************************************************************************************************************************************************************************
//IGNITION CONFIG PAGE (2)
pnt_configPage = ( byte * ) & configPage2 ; //Create a pointer to Page 2 in memory
2013-07-18 04:53:40 -07:00
//Begin writing the Ignition table, basically the same thing as above
if ( EEPROM . read ( EEPROM_CONFIG2_XSIZE ) ! = ignitionTable . xSize ) { EEPROM . write ( EEPROM_CONFIG2_XSIZE , ignitionTable . xSize ) ; } //Write the ignition Table RPM dimension size
if ( EEPROM . read ( EEPROM_CONFIG2_YSIZE ) ! = ignitionTable . ySize ) { EEPROM . write ( EEPROM_CONFIG2_YSIZE , ignitionTable . ySize ) ; } //Write the ignition Table MAP/TPS dimension size
2013-09-11 05:06:13 -07:00
for ( int x = EEPROM_CONFIG2_MAP ; x < EEPROM_CONFIG2_XBINS ; x + + )
{
offset = x - EEPROM_CONFIG2_MAP ;
if ( EEPROM . read ( x ) ! = ignitionTable . values [ 7 - offset / 8 ] [ offset % 8 ] ) { EEPROM . write ( x , ignitionTable . values [ 7 - offset / 8 ] [ offset % 8 ] ) ; } //Write the 8x8 map
}
//RPM bins
for ( int x = EEPROM_CONFIG2_XBINS ; x < EEPROM_CONFIG2_YBINS ; x + + )
{
offset = x - EEPROM_CONFIG2_XBINS ;
2013-09-14 04:29:21 -07:00
if ( EEPROM . read ( x ) ! = byte ( ignitionTable . axisX [ offset ] / 100 ) ) { EEPROM . write ( x , byte ( ignitionTable . axisX [ offset ] / 100 ) ) ; } //RPM bins are divided by 100 and converted to a byte
2013-09-11 05:06:13 -07:00
}
//TPS/MAP bins
for ( int x = EEPROM_CONFIG2_YBINS ; x < EEPROM_CONFIG2_SETTINGS ; x + + )
{
offset = x - EEPROM_CONFIG2_YBINS ;
if ( EEPROM . read ( x ) ! = ignitionTable . axisY [ offset ] ) { EEPROM . write ( x , ignitionTable . axisY [ offset ] ) ; }
}
2014-01-09 23:54:33 -08:00
//The next 45 bytes can simply be pulled straight from the configTable
2015-02-03 02:33:22 -08:00
for ( int x = EEPROM_CONFIG2_SETTINGS ; x < EEPROM_CONFIG2_END ; x + + )
2013-07-18 04:53:40 -07:00
{
2013-09-15 17:18:33 -07:00
if ( EEPROM . read ( x ) ! = * ( pnt_configPage + byte ( x - EEPROM_CONFIG2_SETTINGS ) ) ) { EEPROM . write ( x , * ( pnt_configPage + byte ( x - EEPROM_CONFIG2_SETTINGS ) ) ) ; }
2013-07-18 04:53:40 -07:00
}
2015-02-03 02:33:22 -08:00
//That concludes the writing of the IGN table
//*********************************************************************************************************************************************************************************
//AFR CONFIG PAGE (3)
pnt_configPage = ( byte * ) & configPage3 ; //Create a pointer to Page 3 in memory
//Begin writing the Ignition table, basically the same thing as above
if ( EEPROM . read ( EEPROM_CONFIG3_XSIZE ) ! = afrTable . xSize ) { EEPROM . write ( EEPROM_CONFIG3_XSIZE , afrTable . xSize ) ; } //Write the ignition Table RPM dimension size
if ( EEPROM . read ( EEPROM_CONFIG3_YSIZE ) ! = afrTable . ySize ) { EEPROM . write ( EEPROM_CONFIG3_YSIZE , afrTable . ySize ) ; } //Write the ignition Table MAP/TPS dimension size
for ( int x = EEPROM_CONFIG3_MAP ; x < EEPROM_CONFIG3_XBINS ; x + + )
{
offset = x - EEPROM_CONFIG3_MAP ;
if ( EEPROM . read ( x ) ! = afrTable . values [ 7 - offset / 8 ] [ offset % 8 ] ) { EEPROM . write ( x , afrTable . values [ 7 - offset / 8 ] [ offset % 8 ] ) ; } //Write the 8x8 map
}
//RPM bins
for ( int x = EEPROM_CONFIG3_XBINS ; x < EEPROM_CONFIG3_YBINS ; x + + )
{
offset = x - EEPROM_CONFIG3_XBINS ;
if ( EEPROM . read ( x ) ! = byte ( afrTable . axisX [ offset ] / 100 ) ) { EEPROM . write ( x , byte ( afrTable . axisX [ offset ] / 100 ) ) ; } //RPM bins are divided by 100 and converted to a byte
}
//TPS/MAP bins
for ( int x = EEPROM_CONFIG3_YBINS ; x < EEPROM_CONFIG3_SETTINGS ; x + + )
{
offset = x - EEPROM_CONFIG3_YBINS ;
if ( EEPROM . read ( x ) ! = afrTable . axisY [ offset ] ) { EEPROM . write ( x , afrTable . axisY [ offset ] ) ; }
}
//The next 45 bytes can simply be pulled straight from the configTable
for ( int x = EEPROM_CONFIG3_SETTINGS ; x < EEPROM_CONFIG3_END ; x + + )
{
if ( EEPROM . read ( x ) ! = * ( pnt_configPage + byte ( x - EEPROM_CONFIG3_SETTINGS ) ) ) { EEPROM . write ( x , * ( pnt_configPage + byte ( x - EEPROM_CONFIG3_SETTINGS ) ) ) ; }
}
2013-07-18 04:53:40 -07:00
}
2013-07-16 05:31:01 -07:00
void loadConfig ( )
{
2013-09-11 05:34:40 -07:00
int offset ;
//Create a pointer to the config page
2013-07-19 04:02:19 -07:00
byte * pnt_configPage ;
pnt_configPage = ( byte * ) & configPage1 ; //Create a pointer to Page 1 in memory
2013-09-11 05:34:40 -07:00
//Fuel table (See storage.h for data layout)
//fuelTable.xSize = EEPROM.read(EEPROM_CONFIG1_XSIZE); //Read the VE Tables RPM dimension size
//fuelTable.ySize = EEPROM.read(EEPROM_CONFIG1_YSIZE); //Read the VE Tables MAP/TPS dimension size
for ( int x = EEPROM_CONFIG1_MAP ; x < EEPROM_CONFIG1_XBINS ; x + + )
{
offset = x - EEPROM_CONFIG1_MAP ;
fuelTable . values [ 7 - offset / 8 ] [ offset % 8 ] = EEPROM . read ( x ) ; //Read the 8x8 map
}
//RPM bins
for ( int x = EEPROM_CONFIG1_XBINS ; x < EEPROM_CONFIG1_YBINS ; x + + )
{
offset = x - EEPROM_CONFIG1_XBINS ;
fuelTable . axisX [ offset ] = ( EEPROM . read ( x ) * 100 ) ; //RPM bins are divided by 100 when stored. Multiply them back now
}
//TPS/MAP bins
for ( int x = EEPROM_CONFIG1_YBINS ; x < EEPROM_CONFIG1_SETTINGS ; x + + )
{
offset = x - EEPROM_CONFIG1_YBINS ;
fuelTable . axisY [ offset ] = EEPROM . read ( x ) ;
}
2014-01-09 23:54:33 -08:00
//The next 45 bytes can simply be pulled straight from the configTable
2014-02-06 01:48:19 -08:00
for ( int x = EEPROM_CONFIG1_SETTINGS ; x < EEPROM_CONFIG1_END ; x + + )
2013-07-19 04:02:19 -07:00
{
2013-09-15 17:18:33 -07:00
* ( pnt_configPage + byte ( x - EEPROM_CONFIG1_SETTINGS ) ) = EEPROM . read ( x ) ;
2013-07-19 04:02:19 -07:00
}
2014-01-09 23:54:33 -08:00
//That concludes the reading of the VE table
2013-07-19 04:02:19 -07:00
2013-09-11 05:34:40 -07:00
//*********************************************************************************************************************************************************************************
//IGNITION CONFIG PAGE (2)
2013-07-19 04:02:19 -07:00
pnt_configPage = ( byte * ) & configPage2 ; //Create a pointer to Page 2 in memory
2013-09-11 05:34:40 -07:00
//Begin writing the Ignition table, basically the same thing as above
//ignitionTable.xSize = EEPROM.read(EEPROM_CONFIG2_XSIZE); //Read the ignition Table RPM dimension size (Currently not supproted)
//ignitionTable.ySize = EEPROM.read(EEPROM_CONFIG2_YSIZE); //Read the ignition Table MAP/TPS dimension size (Currently not supproted)
for ( int x = EEPROM_CONFIG2_MAP ; x < EEPROM_CONFIG2_XBINS ; x + + )
{
offset = x - EEPROM_CONFIG2_MAP ;
ignitionTable . values [ 7 - offset / 8 ] [ offset % 8 ] = EEPROM . read ( x ) ; //Read the 8x8 map
}
//RPM bins
for ( int x = EEPROM_CONFIG2_XBINS ; x < EEPROM_CONFIG2_YBINS ; x + + )
{
offset = x - EEPROM_CONFIG2_XBINS ;
ignitionTable . axisX [ offset ] = ( EEPROM . read ( x ) * 100 ) ; //RPM bins are divided by 100 when stored. Multiply them back now
}
//TPS/MAP bins
for ( int x = EEPROM_CONFIG2_YBINS ; x < EEPROM_CONFIG2_SETTINGS ; x + + )
{
offset = x - EEPROM_CONFIG2_YBINS ;
ignitionTable . axisY [ offset ] = EEPROM . read ( x ) ;
}
2014-01-09 23:54:33 -08:00
//The next 45 bytes can simply be pulled straight from the configTable
2015-02-03 02:33:22 -08:00
for ( int x = EEPROM_CONFIG2_SETTINGS ; x < EEPROM_CONFIG2_END ; x + + )
2013-07-19 04:02:19 -07:00
{
2013-09-15 17:18:33 -07:00
* ( pnt_configPage + byte ( x - EEPROM_CONFIG2_SETTINGS ) ) = EEPROM . read ( x ) ;
2013-07-19 04:02:19 -07:00
}
2015-02-03 02:33:22 -08:00
//*********************************************************************************************************************************************************************************
//AFR TARGET CONFIG PAGE (3)
pnt_configPage = ( byte * ) & configPage3 ; //Create a pointer to Page 2 in memory
//Begin writing the Ignition table, basically the same thing as above
//ignitionTable.xSize = EEPROM.read(EEPROM_CONFIG2_XSIZE); //Read the ignition Table RPM dimension size (Currently not supproted)
//ignitionTable.ySize = EEPROM.read(EEPROM_CONFIG2_YSIZE); //Read the ignition Table MAP/TPS dimension size (Currently not supproted)
for ( int x = EEPROM_CONFIG3_MAP ; x < EEPROM_CONFIG3_XBINS ; x + + )
{
offset = x - EEPROM_CONFIG3_MAP ;
afrTable . values [ 7 - offset / 8 ] [ offset % 8 ] = EEPROM . read ( x ) ; //Read the 8x8 map
}
//RPM bins
for ( int x = EEPROM_CONFIG3_XBINS ; x < EEPROM_CONFIG3_YBINS ; x + + )
{
offset = x - EEPROM_CONFIG3_XBINS ;
afrTable . axisX [ offset ] = ( EEPROM . read ( x ) * 100 ) ; //RPM bins are divided by 100 when stored. Multiply them back now
}
//TPS/MAP bins
for ( int x = EEPROM_CONFIG3_YBINS ; x < EEPROM_CONFIG3_SETTINGS ; x + + )
{
offset = x - EEPROM_CONFIG3_YBINS ;
afrTable . axisY [ offset ] = EEPROM . read ( x ) ;
}
//The next 45 bytes can simply be pulled straight from the configTable
for ( int x = EEPROM_CONFIG3_SETTINGS ; x < EEPROM_CONFIG3_END ; x + + )
{
* ( pnt_configPage + byte ( x - EEPROM_CONFIG3_SETTINGS ) ) = EEPROM . read ( x ) ;
}
2013-07-16 05:31:01 -07:00
}
2014-05-12 04:28:24 -07:00
/*
Reads 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 ( )
{
2014-12-10 15:21:59 -08:00
for ( int x = 0 ; x < CALIBRATION_TABLE_SIZE ; x + + ) //Each calibration table is 512 bytes long
2014-05-12 04:28:24 -07:00
{
2014-12-10 15:21:59 -08:00
int y = EEPROM_CALIBRATION_CLT + x ;
cltCalibrationTable [ x ] = EEPROM . read ( y ) ;
2014-05-12 04:28:24 -07:00
2014-12-10 15:21:59 -08:00
y = EEPROM_CALIBRATION_IAT + x ;
iatCalibrationTable [ x ] = EEPROM . read ( y ) ;
2014-05-12 04:28:24 -07:00
2014-12-10 15:21:59 -08:00
y = EEPROM_CALIBRATION_O2 + x ;
o2CalibrationTable [ x ] = EEPROM . read ( y ) ;
2014-05-12 04:28:24 -07:00
}
}
2014-05-13 00:13:12 -07:00
2014-12-10 15:21:59 -08:00
/*
This takes the values in the 3 calibration tables ( Coolant , Inlet temp and O2 )
and saves them to the EEPROM .
*/
2014-05-13 00:13:12 -07:00
void writeCalibration ( )
{
2014-12-10 15:21:59 -08:00
for ( int x = 0 ; x < CALIBRATION_TABLE_SIZE ; x + + ) //Each calibration table is 512 bytes long
2014-05-13 00:13:12 -07:00
{
2014-12-10 15:21:59 -08:00
int y = EEPROM_CALIBRATION_CLT + x ;
2015-01-09 14:56:14 -08:00
if ( EEPROM . read ( y ) ! = cltCalibrationTable [ x ] ) { EEPROM . write ( y , cltCalibrationTable [ x ] ) ; }
2014-05-13 00:13:12 -07:00
2014-12-10 15:21:59 -08:00
y = EEPROM_CALIBRATION_IAT + x ;
2015-01-09 14:56:14 -08:00
if ( EEPROM . read ( y ) ! = iatCalibrationTable [ x ] ) { EEPROM . write ( y , iatCalibrationTable [ x ] ) ; }
2014-05-13 00:13:12 -07:00
2014-12-10 15:21:59 -08:00
y = EEPROM_CALIBRATION_O2 + x ;
2015-01-09 14:56:14 -08:00
if ( EEPROM . read ( y ) ! = o2CalibrationTable [ x ] ) { EEPROM . write ( y , o2CalibrationTable [ x ] ) ; }
2014-05-13 00:13:12 -07:00
}
}