Advertisement
Narayan

eeprom

Apr 16th, 2023
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.29 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. #define GOT_HERE()  do {Serial.print('['); Serial.print(__LINE__); Serial.println(']'); Serial.flush();} while(0)
  4. #define SDA_PIN 4
  5. #define SCL_PIN 5
  6.  
  7. // define memory address of blocks
  8. #define I2C_SLAVE_ADDR0 0x50
  9. #define I2C_SLAVE_ADDR1 0x51
  10. #define I2C_SLAVE_ADDR2 0x52
  11. #define I2C_SLAVE_ADDR3 0x53
  12. #define I2C_SLAVE_ADDR4 0x54
  13. #define I2C_SLAVE_ADDR5 0x55
  14. #define I2C_SLAVE_ADDR6 0x56
  15. #define I2C_SLAVE_ADDR7 0x57
  16.  
  17. #define PAGE_BUFFER 16
  18. #define NUM_OF_PAGES 16
  19. #define MAX_MEMORY 256
  20.  
  21. bool ok = true;
  22. char addrpk[] = "{\"bc1qjaz2248t09y98vaqguq2560t7kclhesapce5sf\":\"KxdK7M6b148NsEyH6EvR91xSWN27uzCxYfhgdRznZEZfX7Kkr9Rh\"}";
  23. uint16_t i = 0;
  24. char* data;
  25.  
  26. // Read bytes from eeprom device
  27. void readEEPROM(uint8_t deviceaddr, uint16_t eepromaddr, char* data, uint8_t datalen){
  28.     Wire.beginTransmission(deviceaddr);
  29.     Wire.write(eepromaddr);
  30.     Wire.endTransmission();
  31.     Wire.requestFrom(deviceaddr, datalen);
  32.     while( Wire.available() )
  33.         data[i++] = Wire.read();
  34.     i = 0;
  35. }
  36.  
  37. void writeEEPROM(byte deviceaddr, byte eepromaddr, char* data){
  38.     uint8_t msglen = 0, bytes_left_in_page = 0, page_number = 1;
  39.     uint8_t data_fit = 0, avail_eemem = 0, last_write_size = 0;
  40.     uint8_t remaining_pages = 0, pages_needed = 0;
  41.  
  42.     // Compute the length of the string
  43.     do{
  44.        msglen++;
  45.     }while(data[msglen]);
  46.    
  47.     // Given the address to write to, we need to know how much space is
  48.     // available in the current page
  49.     bytes_left_in_page = PAGE_BUFFER - (eepromaddr % PAGE_BUFFER);
  50.     // bytes_left_in_page = 11
  51.    
  52.     // Calculate size of last write
  53.     last_write_size = (msglen - bytes_left_in_page) % PAGE_BUFFER;
  54.     // last_write_size = 10
  55.  
  56.     // We also need to know where does the target address is located in
  57.     // terms of pages, starting at page [0]
  58.     page_number = eepromaddr / PAGE_BUFFER;
  59.     // page_number = 7
  60.    
  61.     // Therefore, for 24LC16B, there are more x pages available for write
  62.     // including the page where the target address actually is
  63.     remaining_pages = NUM_OF_PAGES - page_number - 1;
  64.     // remaining_pages = 8
  65.  
  66.     // Check how much memory there is available, starting from the
  67.     // target address
  68.     avail_eemem = MAX_MEMORY
  69.                 - ( remaining_pages * PAGE_BUFFER - bytes_left_in_page );
  70.     // avail_eemem = 139
  71.  
  72.     // Check if data fits the eeprom memory capacity, starting from
  73.     // target address - [1] doesn't fit, [0] it fits
  74.     data_fit = msglen > avail_eemem ? 1 : 0;
  75.     // data_fit = 1
  76.  
  77.     // Check how many pages we need to write the string
  78.     pages_needed = msglen % PAGE_BUFFER ? (msglen / PAGE_BUFFER + 1)
  79.                                         : msglen / PAGE_BUFFER;
  80.     // pages_needed = 7
  81.  
  82.     // Check if data fits eeprom block memory
  83.     // Also check if target addres is at the beginning of a page
  84.     // if it is, then, we write 16 bytes at once and then we
  85.     // keep writting the remaining string always 16 bytes at
  86.     // aa time, until the end of the string
  87.     if( !data_fit )
  88.         if ( !bytes_left_in_page ){
  89.             for(uint8_t i = 0; i < pages_needed; i++){
  90.                 Wire.beginTransmission(deviceaddr);
  91.                 Wire.write(eepromaddr + i * PAGE_BUFFER);
  92.                 Wire.write(data[eepromaddr + i * PAGE_BUFFER], PAGE_BUFFER);
  93.                 Wire.endTransmission();
  94.                 delay(6); // delay for page write
  95.             }
  96.         }else{
  97.             // We are not at the beginning of any page
  98.             // Fill the remaining bytes of the current page
  99.             Wire.beginTransmission(deviceaddr);
  100.             Wire.write(eepromaddr);
  101.             Wire.write(data[0], bytes_left_in_page);
  102.             Wire.endTransmission();
  103.             delay(6); // delay for page write
  104.  
  105.             // Fill next (complete) pages with 16 bytes
  106.             for(uint8_t i = 0; i < pages_needed - 2; i++){
  107.                 Wire.beginTransmission(deviceaddr);
  108.                 Wire.write(eepromaddr + i * PAGE_BUFFER);
  109.                 Wire.write(data[bytes_left_in_page + i * PAGE_BUFFER], PAGE_BUFFER);
  110.                 Wire.endTransmission();
  111.                 delay(6); // delay for page write
  112.             }
  113.             // Last write
  114.             Wire.beginTransmission(deviceaddr);
  115.             Wire.write((page_number + remaining_pages - 2) * PAGE_BUFFER);
  116.             Wire.write(data[msglen - last_write_size], last_write_size + 1);
  117.             Wire.endTransmission();
  118.             delay(6); // delay for page write
  119.         }
  120.     else
  121.         // Turn on a red led because data is larger than eeprom memory
  122. }
  123.  
  124. void setup(){
  125.     Serial.begin(115200);
  126.     Wire.begin(SDA_PIN, SCL_PIN);
  127. }
  128.  
  129. void loop(){
  130.     if(ok){
  131.         Wire.beginTransmission(I2C_SLAVE_ADDR7);
  132.         Wire.write(0x2f);
  133.         Wire.write(addrpk);
  134.         Wire.endTransmission();
  135.         delay(5);
  136.         /* ---- Read from EEPROM ---- */
  137.         Wire.beginTransmission(I2C_SLAVE_ADDR7);
  138.         Wire.write(0x2f);
  139.         Wire.endTransmission();
  140.         Wire.requestFrom(I2C_SLAVE_ADDR7, sizeof(myData));
  141.         Serial.print("Byte read: ");
  142.         while( Wire.available() ){
  143.             Serial.print(Wire.read(), HEX);
  144.             Serial.print(" ");
  145.         }
  146.         Serial.println();
  147.         ok = false;
  148.         GOT_HERE();
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement