Initial work on CRC32 calcs for pages

This commit is contained in:
Josh Stewart 2019-03-17 22:14:29 +11:00
parent 4f4781a234
commit 0697819f9c
5 changed files with 118 additions and 2 deletions

View File

@ -228,7 +228,8 @@ const char TSfirmwareVersion[] PROGMEM = "Speeduino";
const byte data_structure_version = 2; //This identifies the data structure when reading / writing.
//const byte page_size = 64;
//const int16_t npage_size[11] PROGMEM = {0,288,128,288,128,288,128,240,192,192,192};
const int16_t npage_size[11] PROGMEM = {0,128,288,288,128,288,128,240,192,192,192};
#define NUM_PAGES 11
const int16_t npage_size[NUM_PAGES] PROGMEM = {0,128,288,288,128,288,128,240,192,192,192};
//const byte page11_size = 128;
#define MAP_PAGE_SIZE 288

View File

@ -15,6 +15,8 @@ void storeLastBaro(byte);
void storeCalibrationValue(uint16_t, byte);
byte readEEPROMVersion();
void storeEEPROMVersion(byte);
void storePageCRC32(byte, uint32_t);
uint32_t readPageCRC32(byte);
#if defined(CORE_STM32) || defined(CORE_TEENSY)
#define EEPROM_MAX_WRITE_BLOCK 64 //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)
@ -68,6 +70,8 @@ Current layout of EEPROM data (Version 3) is as follows (All sizes are in bytes)
| 1500 |192 | CANBUS config and data (Table 10_) |
| 1692 |192 | Table 11 - General settings |
| |
| 2514 |44 | Table CRC32 values. Last table first|
| 2558 |1 | Last recorded Baro value |
| 2559 |512 | Calibration data (O2) |
| 3071 |512 | Calibration data (IAT) |
| 3583 |512 | Calibration data (CLT) |
@ -141,6 +145,7 @@ Current layout of EEPROM data (Version 3) is as follows (All sizes are in bytes)
#define EEPROM_CONFIG10_END 2094
//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 EEPROM_PAGE_CRC32 2514 //Size of this is 4 * <number of pages> (CRC32 = 32 bits)
#define EEPROM_LAST_BARO 2558
#define EEPROM_CALIBRATION_O2 2559
#define EEPROM_CALIBRATION_IAT 3071

View File

@ -645,6 +645,46 @@ void writeCalibration()
}
/*
Takes a page number and CRC32 value then stores it in the relevant place in EEPROM
Note: Each pages requires 4 bytes for its CRC32. These are stored in reverse page order (ie the last page is store first in EEPROM)
*/
void storePageCRC32(byte pageNo, uint32_t crc32_val)
{
uint16_t address; //Start address for the relevant page
address = EEPROM_PAGE_CRC32 + ((NUM_PAGES - pageNo) * 4);
//One = Most significant -> Four = Least significant byte
byte four = (crc32_val & 0xFF);
byte three = ((crc32_val >> 8) & 0xFF);
byte two = ((crc32_val >> 16) & 0xFF);
byte one = ((crc32_val >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.update(address, four);
EEPROM.update(address + 1, three);
EEPROM.update(address + 2, two);
EEPROM.update(address + 3, one);
}
/*
Retrieves and returns the 4 byte CRC32 for a given page from EEPROM
*/
uint32_t readPageCRC32(byte pageNo)
{
uint16_t address; //Start address for the relevant page
address = EEPROM_PAGE_CRC32 + ((NUM_PAGES - pageNo) * 4);
//Read the 4 bytes from the eeprom memory.
uint32_t four = EEPROM.read(address);
uint32_t three = EEPROM.read(address + 1);
uint32_t two = EEPROM.read(address + 2);
uint32_t one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
// 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
byte readLastBaro() { return EEPROM.read(EEPROM_LAST_BARO); }

View File

@ -8,6 +8,6 @@ These are some utility functions and variables used through the main code
void setResetControlPinState();
byte pinTranslate(byte);
uint32_t calculateCRC32(byte);
#endif // UTILS_H

View File

@ -13,6 +13,10 @@
#include "globals.h"
#include "utils.h"
#include "decoders.h"
#include "comms.h"
#include "src/FastCRC/FastCRC.h"
FastCRC32 CRC32;
//This function performs a translation between the pin list that appears in TS and the actual pin numbers
//For the digital IO, this will simply return the same number as the rawPin value as those are mapped directly.
@ -51,3 +55,69 @@ void setResetControlPinState()
break;
}
}
/*
Calculates and returns the CRC32 value of a given page of memory
*/
uint32_t calculateCRC32(byte pageNo)
{
uint32_t CRC32_val;
byte raw_value;
void* pnt_configPage;
//This sucks (again) for thall the 3D map pages that have to have a translation performed
switch(pageNo)
{
case veMapPage:
raw_value = getPageValue(veMapPage, 0);
CRC32_val = CRC32.crc32(&raw_value, 1);
for(uint16_t x=1; x< sizeof(fuelTable); x++)
{
raw_value = getPageValue(veMapPage, x);
CRC32_val = CRC32.crc32_upd(&raw_value, 1);
}
break;
case veSetPage:
pnt_configPage = &configPage1; //Create a pointer to Page 1 in memory
CRC32_val = CRC32.crc32((byte *)pnt_configPage, sizeof(configPage1) );
break;
case ignMapPage:
break;
case ignSetPage:
pnt_configPage = &configPage2; //Create a pointer to Page 4 in memory
CRC32_val = CRC32.crc32((byte *)pnt_configPage, sizeof(configPage2) );
break;
case afrMapPage:
break;
case afrSetPage:
pnt_configPage = &configPage3; //Create a pointer to Page 4 in memory
CRC32_val = CRC32.crc32((byte *)pnt_configPage, sizeof(configPage3) );
break;
case boostvvtPage:
break;
case seqFuelPage:
break;
case canbusPage:
pnt_configPage = &configPage9; //Create a pointer to Page 9 in memory
CRC32_val = CRC32.crc32((byte *)pnt_configPage, sizeof(configPage9) );
break;
case warmupPage:
pnt_configPage = &configPage10; //Create a pointer to Page 10 in memory
CRC32_val = CRC32.crc32((byte *)pnt_configPage, sizeof(configPage10) );
break;
default:
break;
}
return CRC32_val;
}