Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ bool readConfigLength()
uint16_t length = MFeeprom.get_length();
configLength = 0;

if (MFeeprom.read_byte(MEM_OFFSET_CONFIG) == 0xFF)
return false;
while (MFeeprom.read_byte(addreeprom++) != 0x00) {
configLength++;
if (addreeprom > length) {
Expand Down Expand Up @@ -588,6 +590,7 @@ void generateRandomSerial()
randomSerial >>= 4;
}
MFeeprom.write_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL);
cmdMessenger.sendCmd(kDebug, F("Serial number generated"));
}

#if defined(ARDUINO_ARCH_RP2040)
Expand Down Expand Up @@ -619,30 +622,35 @@ void generateSerial(bool force)
MFeeprom.read_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL);
return;
}
#if defined(ARDUINO_ARCH_RP2040)

// A uniqueID is already generated and saved to the eeprom
if (MFeeprom.read_byte(MEM_OFFSET_SERIAL) == 'I' && MFeeprom.read_byte(MEM_OFFSET_SERIAL + 1) == 'D') {
#if defined(ARDUINO_ARCH_AVR)
generateRandomSerial();
#elif defined(ARDUINO_ARCH_RP2040)
readUniqueSerial();
#endif
return;
}
#endif


// Coming here no UniqueID and no serial number is available, so it's the first start up of a board
#if defined(ARDUINO_ARCH_AVR)
// Generate a serial number for AVR's
// To have not always the same starting point for the random generator, millis() are
// used as starting point. It is very unlikely that the time between flashing the firmware
// and getting the command to send the info's to the connector is always the same.
// additional double check if it's really a new board, should reduce Jaimes problem
generateRandomSerial();
#elif defined(ARDUINO_ARCH_RP2040)
// Read the uniqueID for Pico's and use it as serial number
readUniqueSerial();
// mark this in the eeprom that a UniqueID is used on first start up for Pico's
MFeeprom.write_block(MEM_OFFSET_SERIAL, "ID", 2);
#endif
// Set first byte of config to 0x00 to ensure empty config on 1st start up
// Otherwise the complete length of the config will be send with 0xFF (empty EEPROM)
MFeeprom.write_byte(MEM_OFFSET_CONFIG, 0x00);
if (MFeeprom.read_byte(MEM_OFFSET_CONFIG) == 0xFF) {
MFeeprom.write_block(MEM_OFFSET_CONFIG, 0x00);
}
}

void OnGenNewSerial()
Expand Down