Created
March 6, 2026 23:48
-
-
Save hed0rah/07e3a3517ed1a0aa66492f82a0004155 to your computer and use it in GitHub Desktop.
EEPROM size detector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Wire.h> | |
| #define EEPROM_I2C_ADDRESS 0x50 | |
| // Common page sizes to test (most 24xx chips use one of these) | |
| const uint8_t possiblePageSizes[] = {8, 16, 32, 64, 128, 256}; | |
| const uint8_t NUM_PAGE_TESTS = sizeof(possiblePageSizes); | |
| void setup() { | |
| Serial.begin(9600); | |
| while (!Serial); // Leonardo/Micro/others wait for serial | |
| delay(1000); | |
| Wire.begin(); | |
| Serial.println("=== Advanced EEPROM Detector ==="); | |
| Serial.println("Detects total size (mirror method) + page size"); | |
| // === PART 1: Detect total size (mirror/wrap-around) === | |
| byte original0 = readEEPROM(0); | |
| Serial.print("Original @ addr 0: 0x"); | |
| Serial.println(original0, HEX); | |
| writeEEPROM(0, 0xA5); // Unique test value | |
| unsigned long detectedSize = 0; | |
| const unsigned long MAX_CHECK = 131072UL; // 128 KB safety | |
| for (unsigned long addr = 128; addr <= MAX_CHECK; addr *= 2) { | |
| if (readEEPROM(addr) == 0xA5) { | |
| detectedSize = addr; | |
| break; | |
| } | |
| } | |
| writeEEPROM(0, original0); // Restore immediately | |
| if (detectedSize == 0) { | |
| Serial.println("❌ No size wrap detected — check wiring/address?"); | |
| return; | |
| } | |
| Serial.print("Detected size: "); | |
| Serial.print(detectedSize); | |
| Serial.print(" bytes ("); | |
| Serial.print(detectedSize / 1024UL); | |
| Serial.println(" KB)"); | |
| // === PART 2: Detect page size === | |
| Serial.println("\nNow detecting page size..."); | |
| uint8_t detectedPageSize = 0; | |
| // We'll use addresses near the start (safe area) | |
| // Write pattern: 0x55 0xAA 0x55 ... and check for wrap within page | |
| for (uint8_t i = 0; i < NUM_PAGE_TESTS; i++) { | |
| uint8_t testPage = possiblePageSizes[i]; | |
| if (testPage > detectedSize) continue; // Can't be larger than total size | |
| // Save original bytes in test range (0 to testPage-1) | |
| byte originals[256]; // Max we'll need is 256 | |
| for (uint8_t j = 0; j < testPage; j++) { | |
| originals[j] = readEEPROM(j); | |
| } | |
| // Write test pattern: byte = (j % 2 == 0) ? 0x55 : 0xAA | |
| for (uint8_t j = 0; j < testPage; j++) { | |
| writeEEPROM(j, (j % 2 == 0) ? 0x55 : 0xAA); | |
| } | |
| // Now write one extra byte at position testPage (should wrap to 0 if page size == testPage) | |
| writeEEPROM(testPage, 0xFF); // Distinct value | |
| // Check if addr 0 got overwritten (wrap happened) | |
| byte check0 = readEEPROM(0); | |
| bool wrapped = (check0 == 0xFF); | |
| // Restore originals regardless | |
| for (uint8_t j = 0; j < testPage; j++) { | |
| writeEEPROM(j, originals[j]); | |
| } | |
| if (wrapped) { | |
| detectedPageSize = testPage; | |
| break; // Found it — stop at the smallest matching one | |
| } | |
| } | |
| if (detectedPageSize > 0) { | |
| Serial.print("Detected page size: "); | |
| Serial.print(detectedPageSize); | |
| Serial.println(" bytes"); | |
| Serial.println("(Typical: 64 for 32/64/128/256 Kb chips, 32 for smaller, 128–256 for big ones)"); | |
| } else { | |
| Serial.println("❌ Could not detect page size reliably."); | |
| Serial.println("Chip may have unusual page size or non-standard behavior."); | |
| } | |
| Serial.println("\nDetection complete. Data restored."); | |
| } | |
| void loop() {} | |
| // === Helpers (2-byte addressing assumed — standard for >256 byte chips) === | |
| void writeEEPROM(unsigned int addr, byte data) { | |
| Wire.beginTransmission(EEPROM_I2C_ADDRESS); | |
| Wire.write((byte)(addr >> 8)); // MSB | |
| Wire.write((byte)(addr & 0xFF)); // LSB | |
| Wire.write(data); | |
| Wire.endTransmission(); | |
| delay(6); // >5 ms write cycle | |
| } | |
| byte readEEPROM(unsigned int addr) { | |
| Wire.beginTransmission(EEPROM_I2C_ADDRESS); | |
| Wire.write((byte)(addr >> 8)); | |
| Wire.write((byte)(addr & 0xFF)); | |
| Wire.endTransmission(); | |
| Wire.requestFrom(EEPROM_I2C_ADDRESS, (byte)1); | |
| return Wire.available() ? Wire.read() : 0xFF; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment