SHARE
TWEET

Test FRAM FM24CL16 I2C wiringPi in Raspberry

AntonioVillanueva Jun 27th, 2019 (edited) 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Antonio Villanueva Segura
  2.  * Test  FM24CL16B in RaspberryPi with i2c wiringPi
  3.  * for compiling in RaspberryPi
  4.  * g++ -o i2c i2c.cc -lwiringPi -Wall -std=c++11
  5.  */
  6.  
  7. #include <wiringPiI2C.h>
  8. #include <wiringPi.h>
  9.  
  10. #include <iostream>
  11. #include <string>
  12.  
  13. using namespace std;
  14. #define ADDRESS_FRAM_BASE 0x50 // 0x50 0x51 52 53 54 55 56 57
  15. /*
  16.  * Registers in FRAM and its relations with the i2c address
  17.  *
  18.  * 0-255        0x50
  19.  * 256-511      0x51
  20.  * 512-767      0x52
  21.  * 768-1023     0x53
  22.  * 1024-1279    0x54
  23.  * 1280-1535    0x55
  24.  * 1536-1791    0x56
  25.  * 1792-2047    0x57
  26.  *
  27.  * Modeling class of a FRAM memo type FM24CL16B to be used in a RaspberryPi with WiringPi
  28.  */
  29.  
  30. class FM24CL16{
  31.     public:
  32.     FM24CL16();
  33.    
  34.     bool writeByteFram(int reg,unsigned char data); //write byte data in FRAM reg
  35.     unsigned char readByteFram(int reg);//Read from FRAM reg   
  36.     void writeDataFram(int reg,char  *data,int size);//write data from reg to size
  37.     void readDataFram(int reg,int size);//Read amount data from memory
  38.    
  39.     private:
  40.     int index(int registre);//returns the index of a memory area, point to fd of wiringPi
  41.     int fd[8];// 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57
  42. };
  43.  
  44. /**********************************************************************/
  45. FM24CL16::FM24CL16(){
  46.    
  47.     int address= ADDRESS_FRAM_BASE;
  48.     //Create 8 fd, one per address, each contains 256 addresses
  49.    
  50.     for (int index=0;index<8;index++){
  51.        
  52.         fd[index]=wiringPiI2CSetup(address);
  53.    
  54.         if(fd[index]<0)
  55.             {
  56.                 cout <<"Error on FRAM fd address=!"<<std::hex<< (address & 0xFF)<<endl;
  57.                 exit(0);
  58.             }
  59.         else{
  60.                 cout<<"OK FRAM address 0x"<<std::hex<< (address &0xFF) <<" ,fd= "<<std::to_string(fd[index] )<<endl;
  61.             }
  62.            
  63.         address++;
  64.     }
  65.  
  66. }
  67. /**********************************************************************/
  68. bool FM24CL16::writeByteFram(int reg,unsigned char data){//write byte data in FRAM reg
  69.     if (wiringPiI2CWriteReg8(fd[index(reg)],reg,data)<0)
  70.     {
  71.         cout <<"Error Write reg ( "+std::to_string(reg)+" )data="+std::to_string (int(data))<<endl;
  72.         return false;//-1 error
  73.     }else
  74.     {
  75.         cout <<std::hex<<"Write reg ( 0x"<<int (reg)<<" ) data="
  76.         <<"0x"<<int (data)<<" "<<char(data)<<endl;
  77.     }
  78.     return true;
  79. }
  80. /**********************************************************************/
  81. unsigned char FM24CL16::readByteFram(int reg){//Read from FRAM reg
  82.     return wiringPiI2CReadReg8(fd[index(reg)],reg);
  83. }
  84. /**********************************************************************/
  85. void FM24CL16::writeDataFram(int reg,char *data,int size){//write data from reg to size
  86. //Write the same data from a given registre up to a size
  87.     while (size-->=0){
  88.         writeByteFram(reg++,*(data ));
  89.         data++;
  90.     }
  91. }
  92. /**********************************************************************/
  93. int FM24CL16::index(int registre){//returns the fd index of a memory area, point to fd of wiringPi
  94.     int index=0; //index for fd[index]= wiringPi file descripteur
  95.     int area=255; //First memory block 0-255 fd[0] 0x50
  96.    
  97.     for (index=0;index<8;index++){
  98.        
  99.         if (registre<=area){return index;}
  100.         area+=256;//memory block 255,511,767,1023,1279,1535,1791,2047
  101.     }
  102.    
  103.     return index;
  104. }
  105.  
  106. /**********************************************************************/
  107. void FM24CL16::readDataFram(int reg,int size){//Read amount data from memory
  108.     unsigned char data;
  109.     while (size-->=0){ 
  110.         data=readByteFram(reg++);//Read Byte from FRAM registre
  111.         cout <<"Reg[0x"<<std::hex<<reg<<"]"<<" = 0x"<< std::hex<<int(data)<<" = "<< (char) data<< endl;//Print
  112.     }
  113. }
  114. /**********************************************************************/
  115. /**********************************************************************/
  116. /**********************************************************************/
  117. int main(int argc, char *argv[]){
  118.     FM24CL16 fram;
  119.    
  120.     //Text example
  121.     string text0x50 ="Antonio Villanueva Segura Write TEST IN FM24CL16B MEMORY. ";
  122.     string text0x51 ="Write Test in 0x51 TEST IN FM24CL16B MEMORY. ";  
  123.  
  124.    
  125.     //Write some data text in FRAM memory  reg=0x00 0x50 i2c address
  126.     fram.writeDataFram(0,(char *) text0x50.c_str() ,text0x50.length());
  127.    
  128.     //Write some data text in FRAM memory  reg=0x00 0x51 i2c address
  129.     fram.writeDataFram(256,(char *) text0x51.c_str() ,text0x51.length());  
  130.  
  131.        
  132.     //Read amount data from 0x50 reg 0x00 fram
  133.     fram.readDataFram(256,text0x51.length());//Read amount data from memory
  134.        
  135.     //Read amount data from 0x50 reg 0x00 fram
  136.     fram.readDataFram(0,text0x50.length());//Read amount data from memory
  137.        
  138.            
  139.     return 0;
  140. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top