eeprom writer arduino nano
/* EEPROM 28C256 (32K bytes) programmer with sd_card
* written 19 Mar 2024
* SD library sucks!
* use SDfat
* choose ATmega328p (old bootloader)
*/
#include <SdFat.h>
#include <sdios.h>
// SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
// 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
#define SD_FAT_TYPE 1
// SDCARD_SS_PIN is defined for the built-in SD on some boards.
#ifndef SDCARD_SS_PIN
const uint8_t SD_CS_PIN = SS;
#else // SDCARD_SS_PIN
// Assume built-in SD is used.
const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
#endif // SDCARD_SS_PIN
// Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur.
#define SPI_CLOCK SD_SCK_MHZ(50)
// Try to select the best SD card configuration.
#if HAS_SDIO_CLASS
#define SD_CONFIG SdioConfig(FIFO_SDIO)
#elif ENABLE_DEDICATED_SPI
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK)
#else // HAS_SDIO_CLASS
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK)
#endif // HAS_SDIO_CLASS
SdFat32 sd;
File32 bfile;
#define SHIFT_DATA 14 //2
#define SHIFT_CLK 15 //3
#define SHIFT_LATCH 16 //4
#define EEPROM_D0 2 //5
#define EEPROM_D7 9 //12
#define WRITE_EN 17 //13
// File bFile;
/*
* Output the address bits and outputEnable signal using shift registers.
outputEnable=false => write 1000 0000 ; true=>read 0000 0000
address >> 8 = top 8 bits
*/
void setAddress(unsigned int address, bool outputEnable) { // one bit at the time!!!
shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, (address >> 8) | (outputEnable ? 0x00 : 0x80)); // (last bit -> OE)
shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, address);
digitalWrite(SHIFT_LATCH, LOW); // pin 4 = storage clock pulse
digitalWrite(SHIFT_LATCH, HIGH);
digitalWrite(SHIFT_LATCH, LOW);
}
/*
* Read a byte from the EEPROM at the specified address.
*/
byte readEEPROM(unsigned int address) {
// set ports to input (1) portD 2,3,4,5,6,7 portB 0,1
// make the top 6 bits of PORTD input (0)
DDRD = DDRD & 0x03; // 1111 11xx & 0000 0011
// make first 2 bits input (0)
DDRB = DDRB & 0xfC; // e.g xxx xx11 & 1111 1100
setAddress(address, true);
byte data = 0;
data = PIND >> 2; // 1111 11xx => xx11 1111
data = data + (PINB << 6); // xxxx xx11 => 11xx xxxx
return data;
}
/*
* Write a byte to the EEPROM at the specified address.
*/
void writeEEPROM(unsigned int address, byte data) {
setAddress(address, false);
// make the top 6 bits output (1)
DDRD = DDRD | 0xfc; // 0000 00xx or 1111 1100
// make first 2 bits output (1)
DDRB = DDRB | 0x03; // xxxx xx01 or 0000 0011
// data = 1011 1110 PORTD = 1111 10xx PORTB = 10
PORTD = (data << 2 ); // 1011 1111 => 1111 11xx
PORTB = (data >> 6); // 1011 1111 => xxxx xx10
digitalWrite(WRITE_EN, LOW);
delayMicroseconds(1);
digitalWrite(WRITE_EN, HIGH);
delay(10);
}
/*
* Read the contents of the EEPROM and print them to the serial monitor.
*/
void printContents() {
for (unsigned int base = 0; base < 32768; base += 16) { // <2048 => 32768 reads!
byte data[16];
for (unsigned int offset = 0; offset < 16; offset++) {
data[offset] = readEEPROM(base + offset);
}
char buf[80];
sprintf(buf, "%04x: %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 setup() {
unsigned int addr=0;
pinMode(SHIFT_DATA, OUTPUT);
pinMode(SHIFT_CLK, OUTPUT);
pinMode(SHIFT_LATCH, OUTPUT);
pinMode(WRITE_EN, OUTPUT);
digitalWrite(WRITE_EN, HIGH);
Serial.begin(57600);
// Wait for USB Serial
while (!Serial) {
delay(3000);
}
Serial.println("start");
// Initialize the SD.
if (!sd.begin(SD_CONFIG)) {
Serial.println("INIT sd failed, continue to read");
} else {
if (!bfile.open("asm.bin")) {
Serial.println("open failed, halted");
while (1);
}
Serial.println("Write the first and last 1000 bytes");
while (bfile.available()) {
byte cdata = bfile.read();
if ((addr < 1000) || (addr > 31768)) {
writeEEPROM(addr, cdata);
}
if (( addr % 256) == 0) {
Serial.print(".");
}
//Serial.print(cdata, HEX);
//Serial.print(", ");
addr++;
}
bfile.close();
} // else
// Read and print out the contents of the EERPROM
Serial.println("\nReading EEPROM\n");
printContents();
}
void loop() {
}