Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Antonio Villanueva Segura
- * Test FM24CL16B in RaspberryPi with i2c wiringPi
- * for compiling in RaspberryPi
- * g++ -o i2c i2c.cc -lwiringPi -Wall -std=c++11
- */
- #include <wiringPiI2C.h>
- #include <wiringPi.h>
- #include <iostream>
- #include <string>
- using namespace std;
- #define ADDRESS_FRAM_BASE 0x50 // 0x50 0x51 52 53 54 55 56 57
- /*
- * Registers in FRAM and its relations with the i2c address
- *
- * 0-255 0x50
- * 256-511 0x51
- * 512-767 0x52
- * 768-1023 0x53
- * 1024-1279 0x54
- * 1280-1535 0x55
- * 1536-1791 0x56
- * 1792-2047 0x57
- *
- * Modeling class of a FRAM memo type FM24CL16B to be used in a RaspberryPi with WiringPi
- */
- class FM24CL16{
- public:
- FM24CL16();
- bool writeByteFram(int reg,unsigned char data); //write byte data in FRAM reg
- unsigned char readByteFram(int reg);//Read from FRAM reg
- void writeDataFram(int reg,char *data,int size);//write data from reg to size
- void readDataFram(int reg,int size);//Read amount data from memory
- private:
- int index(int registre);//returns the index of a memory area, point to fd of wiringPi
- int fd[8];// 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57
- };
- /**********************************************************************/
- FM24CL16::FM24CL16(){
- int address= ADDRESS_FRAM_BASE;
- //Create 8 fd, one per address, each contains 256 addresses
- for (int index=0;index<8;index++){
- fd[index]=wiringPiI2CSetup(address);
- if(fd[index]<0)
- {
- cout <<"Error on FRAM fd address=!"<<std::hex<< (address & 0xFF)<<endl;
- exit(0);
- }
- else{
- cout<<"OK FRAM address 0x"<<std::hex<< (address &0xFF) <<" ,fd= "<<std::to_string(fd[index] )<<endl;
- }
- address++;
- }
- }
- /**********************************************************************/
- bool FM24CL16::writeByteFram(int reg,unsigned char data){//write byte data in FRAM reg
- if (wiringPiI2CWriteReg8(fd[index(reg)],reg,data)<0)
- {
- cout <<"Error Write reg ( "+std::to_string(reg)+" )data="+std::to_string (int(data))<<endl;
- return false;//-1 error
- }else
- {
- cout <<std::hex<<"Write reg ( 0x"<<int (reg)<<" ) data="
- <<"0x"<<int (data)<<" "<<char(data)<<endl;
- }
- return true;
- }
- /**********************************************************************/
- unsigned char FM24CL16::readByteFram(int reg){//Read from FRAM reg
- return wiringPiI2CReadReg8(fd[index(reg)],reg);
- }
- /**********************************************************************/
- void FM24CL16::writeDataFram(int reg,char *data,int size){//write data from reg to size
- //Write the same data from a given registre up to a size
- while (size-->=0){
- writeByteFram(reg++,*(data ));
- data++;
- }
- }
- /**********************************************************************/
- int FM24CL16::index(int registre){//returns the fd index of a memory area, point to fd of wiringPi
- int index=0; //index for fd[index]= wiringPi file descripteur
- int area=255; //First memory block 0-255 fd[0] 0x50
- for (index=0;index<8;index++){
- if (registre<=area){return index;}
- area+=256;//memory block 255,511,767,1023,1279,1535,1791,2047
- }
- return index;
- }
- /**********************************************************************/
- void FM24CL16::readDataFram(int reg,int size){//Read amount data from memory
- unsigned char data;
- while (size-->=0){
- data=readByteFram(reg++);//Read Byte from FRAM registre
- cout <<"Reg[0x"<<std::hex<<reg<<"]"<<" = 0x"<< std::hex<<int(data)<<" = "<< (char) data<< endl;//Print
- }
- }
- /**********************************************************************/
- /**********************************************************************/
- /**********************************************************************/
- int main(int argc, char *argv[]){
- FM24CL16 fram;
- //Text example
- string text0x50 ="Antonio Villanueva Segura Write TEST IN FM24CL16B MEMORY. ";
- string text0x51 ="Write Test in 0x51 TEST IN FM24CL16B MEMORY. ";
- //Write some data text in FRAM memory reg=0x00 0x50 i2c address
- fram.writeDataFram(0,(char *) text0x50.c_str() ,text0x50.length());
- //Write some data text in FRAM memory reg=0x00 0x51 i2c address
- fram.writeDataFram(256,(char *) text0x51.c_str() ,text0x51.length());
- //Read amount data from 0x50 reg 0x00 fram
- fram.readDataFram(256,text0x51.length());//Read amount data from memory
- //Read amount data from 0x50 reg 0x00 fram
- fram.readDataFram(0,text0x50.length());//Read amount data from memory
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement