Skip to content

Commit

Permalink
Some potential fixes for comms issues with large writes
Browse files Browse the repository at this point in the history
  • Loading branch information
noisymime committed Jan 18, 2022
1 parent b1dbd81 commit fe33785
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
29 changes: 3 additions & 26 deletions speeduino/newComms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ void parseSerial()
{
//Timeout occurred
serialReceivePending = false; //Reset the serial receive
sendSerialReturnCode(SERIAL_RC_TIMEOUT);

//Flush the serial buffer
while(Serial.available() > 0)
{
Serial.read();
}
sendSerialReturnCode(SERIAL_RC_TIMEOUT);
} //Timeout
} //Data in serial buffer and serial receive in progress
}
Expand Down Expand Up @@ -233,18 +233,7 @@ void processSerialCommand()
generateLiveValues(0, LOG_ENTRY_SIZE);
break;

case 'b': // New EEPROM burn command to only burn a single page at a time

if(isEepromWritePending())
{
//There is already a write pending, force it through.
sendSerialReturnCode(SERIAL_RC_BUSY_ERR);
enableForceBurn();
writeAllConfig();
disableForceBurn();
break;
}

case 'b': // New EEPROM burn command to only burn a single page at a time
writeConfig(serialPayload[2]); //Read the table number and perform burn. Note that byte 1 in the array is unused
sendSerialReturnCode(SERIAL_RC_BURN_OK);
break;
Expand Down Expand Up @@ -399,24 +388,12 @@ void processSerialCommand()
break;
}

if(isEepromWritePending())
{
enableForceBurn();
writeConfig(currentPage);
disableForceBurn();
}

//page_iterator_t entity = map_page_offset_to_entity(currentPage, valueOffset);
for(uint16_t i = 0; i < chunkSize; i++)
{
setPageValue(currentPage, (valueOffset + i), serialPayload[7 + i]);
}

{
//enableForceBurn();
writeConfig(currentPage);
//disableForceBurn();
}
deferEEPROMWrites = true;

sendSerialReturnCode(SERIAL_RC_OK);

Expand Down
2 changes: 2 additions & 0 deletions speeduino/newComms.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
#define SERIAL_RC_BUSY_ERR 0x85 //TS will wait and retry

extern bool serialWriteInProgress;
extern bool serialReceivePending; /**< Whether or not a serial request has only been partially received. This occurs when a the length has been received in the serial buffer, but not all of the payload or CRC has yet been received. */


void parseSerial();//This is the heart of the Command Line Interpeter. All that needed to be done was to make it human readable.
void processSerialCommand();
Expand Down
5 changes: 3 additions & 2 deletions speeduino/speeduino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ void loop()
//updateFullStatus();
checkProgrammableIO();

if( (isEepromWritePending() == true) && (serialReceivePending == false) && (deferEEPROMWrites == false)) { writeAllConfig(); } //Check for any outstanding EEPROM writes.

currentStatus.vss = getSpeed();
currentStatus.gear = getGear();

Expand All @@ -301,8 +303,6 @@ void loop()
readTPS();
#endif

if(isEepromWritePending() == true) { writeAllConfig(); } //Check for any outstanding EEPROM writes.

#ifdef SD_LOGGING
if(configPage13.onboard_log_file_rate == LOGGER_RATE_30HZ) { writeSDLogEntry(); }
#endif
Expand Down Expand Up @@ -387,6 +387,7 @@ void loop()
{
BIT_CLEAR(TIMER_mask, BIT_TIMER_1HZ);
readBaro(); //Infrequent baro readings are not an issue.
deferEEPROMWrites = false; //Reset the slow EEPROM writes flag so that EEPROM burns will return to normal speed. This is set true in NewComms whenever there is a large chunk write to prvent mega2560s halting due to excess EEPROM burn times.

if ( (configPage10.wmiEnabled > 0) && (configPage10.wmiIndicatorEnabled > 0) )
{
Expand Down
15 changes: 14 additions & 1 deletion speeduino/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ A full copy of the license may be found in the projects root directory
#if defined(CORE_STM32) || defined(CORE_TEENSY) & !defined(USE_SPI_EEPROM)
#define EEPROM_MAX_WRITE_BLOCK 64
#else
#define EEPROM_MAX_WRITE_BLOCK 30
#define EEPROM_MAX_WRITE_BLOCK 12
//#define EEPROM_MAX_WRITE_BLOCK 8
#endif

#define EEPROM_DATA_VERSION 0
Expand All @@ -33,6 +34,7 @@ A full copy of the license may be found in the projects root directory

static bool eepromWritesPending = false;
static bool forceBurn = false;
bool deferEEPROMWrites = false;

bool isEepromWritePending()
{
Expand Down Expand Up @@ -131,6 +133,10 @@ static inline write_location writeTable(const void *pTable, table_type_t key, wr
write(rows_begin(pTable, key), location)));
}

//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); }

// ================================= End write support ===============================

/** Write a table or map to EEPROM storage.
Expand All @@ -141,6 +147,8 @@ void writeConfig(uint8_t pageNum)
{
write_location result = { 0, 0 };

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.

switch(pageNum)
{
case veMapPage:
Expand Down Expand Up @@ -499,6 +507,11 @@ uint32_t readPageCRC32(uint8_t pageNum)
return EEPROM.get(compute_crc_address(pageNum), crc32_val);
}

uint16_t getEEPROMSize()
{
return EEPROM.length();
}

// 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.
Expand Down
6 changes: 5 additions & 1 deletion speeduino/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@

void writeAllConfig();
void writeConfig(uint8_t pageNum);
void EEPROMWriteRaw(uint16_t address, uint8_t data);
uint8_t EEPROMReadRaw(uint16_t address);
void loadConfig();
void loadCalibration();
void writeCalibration();
Expand All @@ -125,9 +127,11 @@ uint8_t readEEPROMVersion();
void storeEEPROMVersion(uint8_t);
void storePageCRC32(uint8_t pageNum, uint32_t crcValue);
uint32_t readPageCRC32(uint8_t pageNum);

uint16_t getEEPROMSize();
bool isEepromWritePending();

extern bool deferEEPROMWrites;

#define EEPROM_CONFIG1_MAP 3
#define EEPROM_CONFIG2_START 291
#define EEPROM_CONFIG2_END 419
Expand Down

0 comments on commit fe33785

Please sign in to comment.