Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Data Storage**
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-06-09 18:49:03
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* This code has erratic read and write and produces */
- /* a lot of errors. I need the code fixed so that the */
- /* EEPROM is read accurately. Its a ST95P02 eeprom */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SPI.h>
- #include <SD.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void read_chip(int b);
- void write_full_chip();
- boolean writeByte(byte subAddress, byte data);
- byte readByte(byte subAddress);
- void make_read_opcode(int p);
- void make_write_opcode(int p);
- /****** PIN DEFINITIONS *****/
- #define DATAOUT 11 // MOSI
- #define DATAIN 12 // MISO
- #define SPICLOCK 13 // SCK
- #define CHIPSELECT 10 // CS for EEPROM
- #define SDSELECT 4 // CS for SD module
- #define R_BUT 8 // Read button input
- #define W_BUT 7 // Write button
- // Opcodes
- #define WREN 6
- #define WRDI 4
- #define RDSR 5
- #define WRSR 1
- #define READ 3
- #define WRITE 2
- // Modifiable opcodes for addressing
- byte ADDR_READ = READ;
- byte ADDR_WRITE = WRITE;
- // Global variables
- byte eeprom_output_data;
- byte eeprom_input_data = 0;
- byte clr;
- int address = 0;
- // Memory page
- int page = 0;
- // Data buffer
- char buffer[256];
- // Settings for SPI
- SPISettings SPISet = SPISettings(256000, MSBFIRST, SPI_MODE0);
- File romFile; // For SD in/out
- void setup(void) {
- Serial.begin(9600);
- pinMode(DATAOUT, OUTPUT);
- pinMode(DATAIN, INPUT);
- pinMode(SPICLOCK, OUTPUT);
- pinMode(CHIPSELECT, OUTPUT);
- pinMode(SDSELECT, OUTPUT);
- pinMode(R_BUT, INPUT_PULLUP);
- pinMode(W_BUT, INPUT_PULLUP);
- digitalWrite(CHIPSELECT, HIGH); // Disable device
- // Initialize SD
- Serial.print("Initializing SD card...");
- if (!SD.begin(SDSELECT)) { // Changed to use SDSELECT for consistency
- Serial.println("Initialization failed!");
- while (1);
- }
- Serial.println("Initialization done.");
- }
- void loop(void) {
- Serial.print(" Press R to read \n Press W to write \n \n");
- while (digitalRead(R_BUT) && digitalRead(W_BUT)) {
- // Wait for button press
- }
- if (!digitalRead(R_BUT)) { // Inputs are inverted for wiring convenience
- if (SD.exists("rom.bin")) {
- SD.remove("rom.bin"); // Delete any existing rom file. May need to change this!
- }
- romFile = SD.open("rom.bin", FILE_WRITE);
- if (romFile) {
- Serial.print("Reading chip....\n");
- Serial.print("\n");
- for (int I = 0; I < 4; I++) {
- read_chip(I);
- Serial.print("Contents:");
- Serial.print("\n\n");
- for (int b = 0; b < 256; b++) {
- romFile.write(byte(buffer[b]));
- Serial.print(b + 256 * I, HEX);
- Serial.print(" : ");
- Serial.print(byte(buffer[b]), HEX);
- Serial.print("\n");
- }
- }
- romFile.close();
- }
- }
- if (!digitalRead(W_BUT)) {
- Serial.print("Writing chip....\n");
- write_full_chip();
- Serial.print("Write complete!");
- }
- Serial.print("\n----------------------------------------------------\n\n");
- }
- void read_chip(int b) {
- SPI.beginTransaction(SPISet);
- make_read_opcode(b);
- digitalWrite(CHIPSELECT, LOW);
- SPI.transfer(ADDR_READ); // Send read opcode
- SPI.transfer(0); // Dummy byte to start reading
- for (int I = 0; I < 256; I++) {
- buffer[I] = SPI.transfer(0); // Read data into buffer
- }
- digitalWrite(CHIPSELECT, HIGH);
- SPI.endTransaction();
- }
- void write_full_chip() {
- boolean verify = true; // Flag to confirm successful write. Assume success.
- // Open file and fill buffer
- romFile = SD.open("rom.bin");
- if (romFile) {
- Serial.println("Loading file rom.bin... \n");
- SPI.beginTransaction(SPISet);
- byte data;
- for (int I = 0; I < 4; I++) { // Read in file in 4 blocks of 256 bytes to save memory
- Serial.print("Block ");
- Serial.print(I);
- Serial.print("\n");
- make_write_opcode(I); // Create op-codes for current block of addresses
- for (int b = 0; b < 256; b++) { // Read 256 bytes and write to chip
- data = romFile.read();
- if (!(writeByte(b, data))) verify = false; // Set verify to false if last write op failed
- }
- if (verify) {
- Serial.print("*** Write verification passed! *** \n");
- } else {
- Serial.print("*** Write verification failed! *** \n");
- }
- }
- SPI.endTransaction();
- } else {
- // If the file didn't open, print an error:
- Serial.println("Error opening rom.bin from SD card");
- }
- }
- void make_read_opcode(int p) {
- ADDR_READ = READ + 8 * p;
- }
- void make_write_opcode(int p) {
- ADDR_WRITE = WRITE + 8 * p;
- }
- boolean writeByte(byte subAddress, byte data) {
- boolean verify = true; // Return value to confirm good write. Assume success
- // Write enable chip
- digitalWrite(CHIPSELECT, LOW); // Select chip
- SPI.transfer(WREN); // Set write enable
- digitalWrite(CHIPSELECT, HIGH);
- // Select chip and send address
- digitalWrite(CHIPSELECT, LOW); // Select chip
- SPI.transfer(ADDR_WRITE); // Send write op-code for currently selected block of 256 bytes
- SPI.transfer(subAddress); // Send address within block to write
- SPI.transfer(data); // Send data to write
- digitalWrite(CHIPSELECT, HIGH); // Deselect chip to confirm write
- delay(5); // Delay to ensure write cycle completes.
- // Verify the written data
- digitalWrite(CHIPSELECT, LOW); // Select chip
- SPI.transfer(ADDR_READ); // Send read op-code for currently selected block of 256 bytes
- SPI.transfer(subAddress); // Send address within block to read
- byte read_data = SPI.transfer(0);
- digitalWrite(CHIPSELECT, HIGH); // Deselect chip to confirm write
- if (read_data != data) verify = false;
- return verify;
- }
- byte readByte(byte subAddress) {
- // Select chip and send address
- digitalWrite(CHIPSELECT, LOW); // Select chip
- SPI.transfer(ADDR_READ); // Send read op-code for currently selected block of 256 bytes
- SPI.transfer(subAddress); // Send address within block to read
- byte data = SPI.transfer(0);
- digitalWrite(CHIPSELECT, HIGH); // Deselect chip to confirm write
- return data;
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement