Same Sketch - 2 different EEPROM memory size H/W...How to tell which is which?

Outline: The plan is to expand an existing sketch to incorporate a different setup() due to the new H/W (the main processing loop will mostly remain intact). The H/W difference is that the new Arduino has an expanded memory IC EEPROM of 256KB vs the current one of 4KB. The sketch should work on BOTH H/W bases to avoid having to keep track of multiple s/w baselines.

Q: Is there a programmatic way to determine which H/W the sketch is working with during the setup()? Perhaps a H/W diagnostic library that would do the trick?

what type of EEPROM do you have?

The easiest way would be to define a revision byte at the beginning of your EEPROM. Read that and it then the code will react accordingly. If your existing 4K code does not have that, then define a new revision byte at the top most memory value. If you read the last bye of the 256K EEPROM, it will return a certain value. If you read the last byte of a 256K EEPROM, but it is only 4K in size, you will get some other value (0xFF or an error or ??). Either way, your setup() code will react to that value and "do the right thing"

This thread may put some details to the general idea

https://www.avrfreaks.net/s/topic/a5C3l000000UH0xEAG/t079197

Are you planning to have any area of the EEPROM available for saving the work of finding the size?

a7

yes, see my library - GitHub - RobTillaart/I2C_EEPROM: Library for I2C EEPROM - 24LC512, 24LC256, 24LC64/32/16/08/04/02/01.

the idea is that EEPROMs wrap the addresses.
Assume you have an EEPROM with unknown size.

make a copy of address 0 to restore later.
loop address A over powers of 2
  make a copy of the address A
  write unique bit pattern to address A
  if the bit pattern is visible on address 0 the EEPROM wrapped the address and thus is smaller.

at the end restore original content.

E.g. if you write a 0x55 to address 32768 and it becomes visible at address 0 the EEPROM wrapped the address and thus the EEPROM is not 32 KB.

Worked quite well, you should try it on your EEPROMs to see if it works.

Get the idea?

Thank you - will investigate your Lib!