AntonioVillanueva

FM24CL16 FRAM test with wire in Arduino

May 23rd, 2022 (edited)
675
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Antonio Villanueva Segura
  3.  * FM24CL16 FRAM management continuous memory paging
  4.  *
  5.  * https://www.mouser.fr/datasheet/2/100/CYPR_S_A0010744052_1-2541108.pdf
  6.  *
  7.  * Access by memory location 0 a 256*8=2048
  8.  * The base i2c address is  0b1010
  9.  * After rotating to the left is 0b1010 <<3 =0x50 The base i2c address
  10.  * add the least significant bits A2 A1 A0 to the i2c base address 0x50
  11.  * every block  0x5N ( where N is = 1,2,3,4,5,6,7) contains 256 Bytes, and bits =256 x 8 x 8 = 16384
  12.  * i2c address 0x51 0x52 0x53 0x54 0x55 0x56 0x57
  13.  *
  14.  *                    A7  A6  A5  A4  A3  A2  A1  A0
  15.  * 256 x0 0-255       0   1   0   1   0   0   0   0   0x50
  16.  * 256 x1 256-511     0   1   0   1   0   0   0   1   0x51
  17.  * 256 x2 512-764     0   1   0   1   0   0   1   0   0x52
  18.  * 256 x3 768-1023    0   1   0   1   0   0   1   1   0x53
  19.  * 256 x4 1024-1279   0   1   0   1   0   1   0   0   0x54
  20.  * 256 x5 1280-1535   0   1   0   1   0   1   0   1   0x55
  21.  * 256 x6 1536-1791   0   1   0   1   0   1   1   0   0x56
  22.  * 256 x7 1792- 2047  0   1   0   1   0   1   1   1   0x57
  23.  *
  24.  */
  25. #include <Wire.h>
  26.  
  27. //Base I2C address 0b1010 << 3 = 0x50
  28. #define ADDR (0b1010 << 3)  
  29.  
  30. /*Block memory 8 blocks with address (0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57)
  31.  * which make a total of  256x8=2048 bytes
  32.  * and  256 x 8 x 8 = 16384 bits */
  33.  
  34. #define MEMORY_BLOCK 256
  35.  
  36. String address="";
  37. String data="";
  38. uint8_t tmp[256];//tmp_array to  FM24CL16 and FM24CL16 to tmp_array
  39. /**************************************************************************************************************/
  40. void setup() {
  41.   Serial.println (" FRAM FM24CL16B TEST ");
  42.   Serial.begin(9600);
  43.  
  44.   while (!Serial){} //Wait for serial
  45.  
  46.   Wire.begin();
  47.  
  48.   //readAll();//Read all FRAM memory
  49.   //readMem();//Read all FRAM memory value !=0
  50.   //resetAll ();//Reset all FRMA memory to  0
  51.  
  52.   //while (true){}
  53.  
  54.   //make test array
  55.   uint8_t matriz []={7,8,9,10,11,12,13,14,15,16};
  56.   //Write Array
  57.  
  58.  // writeMultipleI2C(0x50, matriz,sizeof(matriz) );
  59.  
  60.   Serial.println ("Reading .....");
  61.   //Read
  62.   bankToArray(0x50,tmp,256);
  63.   while (true){}  
  64.  
  65. }
  66.  
  67.  
  68.  
  69. /**************************************************************************************************************/
  70. void loop() {
  71.  
  72.   //Pregunta posicion de memoria a escribir en FRAM FM24CL16
  73.   Serial.println (" Write memory in  0 - 016383 ? = ");
  74.   address=serialRead ();
  75.  
  76.   //Pregunta por el dato a escribir en la direccion de memooria anterior de la  FRAM  FM24CL16
  77.   Serial.println ("with data (byte) ? = ");
  78.   data=serialRead();
  79.  
  80.   writeI2CByte(address.toInt(), data.toInt());//Escribe en la direccion addres el dato data
  81.  
  82.   Serial.println ("*************************");
  83.  
  84.   //Comprobamos si el dato esta escrito correctamente en address
  85.   Serial.print ("Read from address = ");Serial.print (address);
  86.  
  87.   Serial.print ( " , value = ");Serial.println (readI2CByte(address.toInt()));
  88.   Serial.println ();
  89.   delay(1000);
  90. }
  91.  
  92. /**************************************************************************************************************/
  93. //Returns reading from the client on the serial port
  94. String serialRead (){
  95.   String input;
  96.   while (Serial.available () ==0){} //Wait serial
  97.    
  98.     if(Serial.available()>0){
  99.       while (input==""){
  100.         input = Serial.readString();
  101.       }
  102.      }
  103.   return input;
  104. }
  105.  
  106. /**************************************************************************************************************/
  107. //Write from 0 to ( 256 * 8) = 2048 bytes
  108. void writeI2CByte(uint16_t mem_addr, byte data){
  109.  
  110.   Serial.print ("ADDR 0x");Serial.print (ADDR | ( mem_addr /MEMORY_BLOCK ) ,HEX);Serial.print ( ", 0x");Serial.println ( (mem_addr-(MEMORY_BLOCK *  (mem_addr/MEMORY_BLOCK))),HEX );
  111.   Wire.beginTransmission (ADDR | ( mem_addr /MEMORY_BLOCK ) ); //Slave Address 0x5n , 0x50 | A2 A1 A0
  112.   Wire.write(mem_addr-(MEMORY_BLOCK *  (mem_addr/MEMORY_BLOCK) ));//Address in memory bank 0 -256
  113.   Wire.write(data);//Data byte
  114.   Wire.endTransmission(false);
  115.  
  116. }
  117.  
  118. /**************************************************************************************************************/
  119. //Reading between 0 to ( 256 * 8) = 2048 bytes
  120. byte readI2CByte(uint16_t mem_addr){
  121.  
  122.   byte data=0x00;
  123.   uint16_t address(ADDR); //Base Address 0x50
  124.   address += ( mem_addr /MEMORY_BLOCK );//A2 A1 A0 =  add to 0x50  -> 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
  125.  
  126.  
  127.   Wire.beginTransmission(address & 0xFF) ;//Slave Address 0x5n , 0x50 | A2 A1 A0
  128.   Wire.write(mem_addr-(MEMORY_BLOCK *  (mem_addr/MEMORY_BLOCK) ));//Address in memory bank 0 -256
  129.   Wire.endTransmission();
  130.  
  131.   Wire.requestFrom(address, 1); //retrieve 1 returned byte
  132.   delay(1);
  133.  
  134.   if(Wire.available()){data = Wire.read();}
  135.   return data;
  136.  
  137.  
  138. }
  139.  
  140. /**************************************************************************************************************/
  141. //Read the entire FM24CL16 FRAM from 0 to (256 * 8) = 2048 bytes
  142. void readAll(){
  143.     for (int mem=0 ;mem<MEMORY_BLOCK*8;mem++){//Loop over fram memory
  144.       Serial.print ("Address ( ");Serial.print (mem);
  145.       Serial.print (" ) = ");Serial.println (readI2CByte(mem));
  146.     }
  147. }
  148.  
  149. /**************************************************************************************************************/
  150. //Read the entire FM24CL16 FRAM from 0 to (256 * 8) = 2048 bytes, shows values other than 0
  151. void readMem(){
  152.   Serial.println ("Analyze memory values , other than 0");
  153.   uint8_t data;
  154.     for (int mem=0 ;mem<MEMORY_BLOCK*8;mem++){//Loop over fram memory
  155.       data =readI2CByte(mem);
  156.       if (data>0){ //If value data !=0
  157.         Serial.print ("Address ( ");Serial.print (mem);
  158.         Serial.print (" ) = ");Serial.println (data);
  159.       }
  160.       data=0;
  161.     }
  162. }
  163. /**************************************************************************************************************/
  164. //Reset all FM24CL16 FRAM memory to 0
  165. void resetAll (){
  166.    for (int mem=0 ;mem<MEMORY_BLOCK*8;mem++){//Loop over fram memory
  167.     Serial.print ("Reset mem pos. = ");Serial.println (mem);
  168.     writeI2CByte(mem, 0);
  169.    }
  170. }
  171. /**************************************************************************************************************/
  172. //Write uint_8 [size] array to  FRAM mem_addr  
  173. void writeMultipleI2C(uint8_t address,uint8_t *matriz, int size){
  174.  
  175.   //Serial.print ("ADDR 0x");Serial.print (ADDR | ( mem_addr /MEMORY_BLOCK ) ,HEX);Serial.print ( ", 0x");Serial.println ( (mem_addr-(MEMORY_BLOCK *  (mem_addr/MEMORY_BLOCK))),HEX );
  176.   Wire.beginTransmission (address ); //Slave Address 0x5n , 0x50 | A2 A1 A0
  177.   Wire.write(0x00);//First Address in FRAM bank 0 -256
  178.  
  179.   while (size>0){//All bank 0-256
  180.     Serial.print ("size = ");Serial.print (size);Serial.print (" = ");Serial.println (*matriz);
  181.     Wire.write( *matriz );//Data byte
  182.     matriz++;//Pointer ++
  183.     size--;//Array size --
  184.   }
  185.  
  186.   Wire.endTransmission(false);
  187. }
  188.  
  189. /**************************************************************************************************************/
  190. //Dump FM24CL16B bank (0x5n)  to uint8_t [] array  ...Continuos reading memory addresss = i2c address
  191. void bankToArray(uint8_t address,uint8_t *matriz, int size){
  192.  
  193.   int n=0;
  194.   Wire.beginTransmission (address ); //Slave Address 0x5n | A2 A1 A0 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57
  195.   Wire.write(0x00);//First Address in FRAM bank 0 -256
  196.   Wire.endTransmission(false);  
  197.  
  198.    Wire.requestFrom(address, size);    // Request size bytes from slave device number address  0x5n
  199.     // Slave may send less than requested
  200.     while(Wire.available()) {
  201.       //Serial.println (Wire.read());
  202.       *matriz=Wire.read();
  203.  
  204.       Serial.print ("(");Serial.print (n++);Serial.print (")=");Serial.println (*matriz);
  205.      
  206.       matriz++; //Pointer to receive array
  207.  
  208.  
  209.     }
  210.  
  211.     delay(500);
  212.  
  213.    
  214. }
RAW Paste Data Copied