Skip to content

Instantly share code, notes, and snippets.

@en0
Created August 7, 2021 04:35
Show Gist options
  • Save en0/dce28885def0dddeb18c041b87a09697 to your computer and use it in GitHub Desktop.
Save en0/dce28885def0dddeb18c041b87a09697 to your computer and use it in GitHub Desktop.
eeprom programmer
#define SHIFT_DATA 13
#define SHIFT_CLK 3
#define SHIFT_LATCH 4
#define EEPROM_D0 5
#define EEPROM_D7 12
#define WRITE_EN 2
void setAddress(int address, bool outputEnable) {
// Send address (and output-enable bit) to shift registers.
shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, (address >> 8) | (outputEnable ? 0x00 : 0x80));
shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, address);
// Store the address in the shift register's output latch
digitalWrite(SHIFT_LATCH, LOW);
digitalWrite(SHIFT_LATCH, HIGH);
digitalWrite(SHIFT_LATCH, LOW);
}
byte readEEPROM(int address) {
// Setup data pins to read input.
for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
pinMode(pin, INPUT);
}
setAddress(address, true);
// Construct a byte from the 8 data pins.
byte data = 0;
for (int pin = EEPROM_D7; pin >= EEPROM_D0; pin -= 1) {
data = (data << 1) + digitalRead(pin);
}
return data;
}
void writeEEPROM(int address, byte data) {
// Setup data pins to write output.
for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
pinMode(pin, OUTPUT);
}
setAddress(address, false);
// Set each of the 8 pins according to the bit position for the given data.
for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
digitalWrite(pin, data & 1);
data = data >> 1;
}
// Address setup time.
delay(10);
// Trigger write operation.
digitalWrite(WRITE_EN, LOW);
// WRITE_EN is wired to a R/C circuit
// low pulse duration is ~700ns
digitalWrite(WRITE_EN, HIGH);
// Allow the EEPROM write cycle to complete
delay(10);
}
void printContents() {
/* Print the first 255 bytes of data from the EEPROM */
for (int base = 0; base <= 255 /* 512*1024 */; base += 16) {
byte data[16];
for (int offset = 0; offset <= 15; offset += 1) {
data[offset] = readEEPROM(base + offset);
}
char buf[80];
sprintf(buf,
"%03x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", base,
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15]);
Serial.println(buf);
}
}
void disableDataProtection() {
// aa -> 5555
// 55 -> 2aaa
// 80 -> 5555
// aa -> 5555
// 55 -> 2aaa
// 20 -> 5555
writeEEPROM(0x5555, 0xaa);
writeEEPROM(0x2aaa, 0x55);
writeEEPROM(0x5555, 0x80);
writeEEPROM(0x5555, 0xaa);
writeEEPROM(0x2aaa, 0x55);
writeEEPROM(0x5555, 0x20);
}
void setup() {
Serial.begin(115200);
Serial.println("Starting up...");
pinMode(SHIFT_DATA, OUTPUT);
pinMode(SHIFT_CLK, OUTPUT);
pinMode(SHIFT_LATCH, OUTPUT);
digitalWrite(WRITE_EN, HIGH);
pinMode(WRITE_EN, OUTPUT);
/* Allow 5 seconds to set trigger on scope */
delay(5000);
disableDataProtection();
writeEEPROM(0, 0xFF);
delay(10);
printContents();
Serial.println("All done.");
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment