Advertisement
pleasedontcode

**Data Storage** rev_01

Jun 9th, 2025
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Data Storage**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-06-09 18:49:03
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* This code has erratic read and write and produces */
  21.     /* a lot of errors. I need the code fixed so that the */
  22.     /* EEPROM is read accurately. Its a ST95P02 eeprom */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SPI.h>
  29. #include <SD.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void read_chip(int b);
  35. void write_full_chip();
  36. boolean writeByte(byte subAddress, byte data);
  37. byte readByte(byte subAddress);
  38. void make_read_opcode(int p);
  39. void make_write_opcode(int p);
  40.  
  41. /****** PIN DEFINITIONS *****/
  42. #define DATAOUT 11 // MOSI
  43. #define DATAIN  12 // MISO
  44. #define SPICLOCK  13 // SCK
  45. #define CHIPSELECT 10 // CS for EEPROM
  46. #define SDSELECT 4 // CS for SD module
  47. #define R_BUT 8 // Read button input
  48. #define W_BUT 7 // Write button
  49.  
  50. // Opcodes
  51. #define WREN  6
  52. #define WRDI  4
  53. #define RDSR  5
  54. #define WRSR  1
  55. #define READ  3
  56. #define WRITE 2
  57.  
  58. // Modifiable opcodes for addressing
  59. byte ADDR_READ = READ;
  60. byte ADDR_WRITE = WRITE;
  61.  
  62. // Global variables
  63. byte eeprom_output_data;
  64. byte eeprom_input_data = 0;
  65. byte clr;
  66. int address = 0;
  67. // Memory page
  68. int page = 0;
  69. // Data buffer
  70. char buffer[256];
  71.  
  72. // Settings for SPI
  73. SPISettings SPISet = SPISettings(256000, MSBFIRST, SPI_MODE0);
  74.  
  75. File romFile; // For SD in/out
  76.  
  77. void setup(void) {
  78.     Serial.begin(9600);
  79.  
  80.     pinMode(DATAOUT, OUTPUT);
  81.     pinMode(DATAIN, INPUT);
  82.     pinMode(SPICLOCK, OUTPUT);
  83.     pinMode(CHIPSELECT, OUTPUT);
  84.     pinMode(SDSELECT, OUTPUT);
  85.     pinMode(R_BUT, INPUT_PULLUP);
  86.     pinMode(W_BUT, INPUT_PULLUP);
  87.     digitalWrite(CHIPSELECT, HIGH); // Disable device
  88.  
  89.     // Initialize SD
  90.     Serial.print("Initializing SD card...");
  91.     if (!SD.begin(SDSELECT)) { // Changed to use SDSELECT for consistency
  92.         Serial.println("Initialization failed!");
  93.         while (1);
  94.     }
  95.     Serial.println("Initialization done.");
  96. }
  97.  
  98. void loop(void) {
  99.     Serial.print(" Press R to read \n Press W to write \n \n");
  100.     while (digitalRead(R_BUT) && digitalRead(W_BUT)) {
  101.         // Wait for button press
  102.     }
  103.     if (!digitalRead(R_BUT)) { // Inputs are inverted for wiring convenience
  104.         if (SD.exists("rom.bin")) {
  105.             SD.remove("rom.bin"); // Delete any existing rom file. May need to change this!
  106.         }
  107.         romFile = SD.open("rom.bin", FILE_WRITE);
  108.         if (romFile) {
  109.             Serial.print("Reading chip....\n");
  110.             Serial.print("\n");
  111.             for (int I = 0; I < 4; I++) {
  112.                 read_chip(I);
  113.                 Serial.print("Contents:");
  114.                 Serial.print("\n\n");
  115.                 for (int b = 0; b < 256; b++) {
  116.                     romFile.write(byte(buffer[b]));
  117.                     Serial.print(b + 256 * I, HEX);
  118.                     Serial.print(" :  ");
  119.                     Serial.print(byte(buffer[b]), HEX);
  120.                     Serial.print("\n");
  121.                 }
  122.             }
  123.             romFile.close();
  124.         }
  125.     }
  126.  
  127.     if (!digitalRead(W_BUT)) {
  128.         Serial.print("Writing chip....\n");
  129.         write_full_chip();
  130.         Serial.print("Write complete!");
  131.     }
  132.     Serial.print("\n----------------------------------------------------\n\n");
  133. }
  134.  
  135. void read_chip(int b) {
  136.     SPI.beginTransaction(SPISet);
  137.     make_read_opcode(b);
  138.     digitalWrite(CHIPSELECT, LOW);
  139.     SPI.transfer(ADDR_READ); // Send read opcode
  140.     SPI.transfer(0); // Dummy byte to start reading
  141.  
  142.     for (int I = 0; I < 256; I++) {
  143.         buffer[I] = SPI.transfer(0); // Read data into buffer
  144.     }
  145.    
  146.     digitalWrite(CHIPSELECT, HIGH);
  147.     SPI.endTransaction();
  148. }
  149.  
  150. void write_full_chip() {
  151.     boolean verify = true; // Flag to confirm successful write. Assume success.
  152.  
  153.     // Open file and fill buffer
  154.     romFile = SD.open("rom.bin");
  155.     if (romFile) {
  156.         Serial.println("Loading file rom.bin... \n");
  157.         SPI.beginTransaction(SPISet);
  158.         byte data;
  159.         for (int I = 0; I < 4; I++) { // Read in file in 4 blocks of 256 bytes to save memory
  160.             Serial.print("Block ");
  161.             Serial.print(I);
  162.             Serial.print("\n");
  163.             make_write_opcode(I);  // Create op-codes for current block of addresses
  164.  
  165.             for (int b = 0; b < 256; b++) { // Read 256 bytes and write to chip
  166.                 data = romFile.read();
  167.                 if (!(writeByte(b, data))) verify = false;  // Set verify to false if last write op failed
  168.             }
  169.  
  170.             if (verify) {
  171.                 Serial.print("*** Write verification passed! *** \n");
  172.             } else {
  173.                 Serial.print("*** Write verification failed! *** \n");
  174.             }
  175.         }
  176.         SPI.endTransaction();
  177.     } else {
  178.         // If the file didn't open, print an error:
  179.         Serial.println("Error opening rom.bin from SD card");
  180.     }
  181. }
  182.  
  183. void make_read_opcode(int p) {
  184.     ADDR_READ = READ + 8 * p;
  185. }
  186.  
  187. void make_write_opcode(int p) {
  188.     ADDR_WRITE = WRITE + 8 * p;
  189. }
  190.  
  191. boolean writeByte(byte subAddress, byte data) {
  192.     boolean verify = true; // Return value to confirm good write. Assume success
  193.     // Write enable chip
  194.     digitalWrite(CHIPSELECT, LOW); // Select chip
  195.     SPI.transfer(WREN);           // Set write enable
  196.     digitalWrite(CHIPSELECT, HIGH);
  197.  
  198.     // Select chip and send address
  199.     digitalWrite(CHIPSELECT, LOW);  // Select chip
  200.     SPI.transfer(ADDR_WRITE);  // Send write op-code for currently selected block of 256 bytes
  201.     SPI.transfer(subAddress);  // Send address within block to write
  202.     SPI.transfer(data);        // Send data to write
  203.     digitalWrite(CHIPSELECT, HIGH); // Deselect chip to confirm write
  204.     delay(5);  // Delay to ensure write cycle completes.
  205.    
  206.     // Verify the written data
  207.     digitalWrite(CHIPSELECT, LOW);  // Select chip
  208.     SPI.transfer(ADDR_READ);  // Send read op-code for currently selected block of 256 bytes
  209.     SPI.transfer(subAddress);  // Send address within block to read
  210.     byte read_data = SPI.transfer(0);
  211.     digitalWrite(CHIPSELECT, HIGH); // Deselect chip to confirm write
  212.     if (read_data != data) verify = false;
  213.  
  214.     return verify;
  215. }
  216.  
  217. byte readByte(byte subAddress) {
  218.     // Select chip and send address
  219.     digitalWrite(CHIPSELECT, LOW);  // Select chip
  220.     SPI.transfer(ADDR_READ);  // Send read op-code for currently selected block of 256 bytes
  221.     SPI.transfer(subAddress);  // Send address within block to read
  222.     byte data = SPI.transfer(0);
  223.     digitalWrite(CHIPSELECT, HIGH); // Deselect chip to confirm write
  224.     return data;
  225. }
  226.  
  227. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement