memory read and write

Dependencies:   eeprom mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <string>
00003 #include "eeprom.h"
00004 
00005 
00006 #define EEPROM_ADDR 0x0   // I2c EEPROM address is 0x00
00007 
00008 #define SDA p9            // I2C SDA pin
00009 #define SCL p10           // I2C SCL pin
00010 
00011 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
00012 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
00013 
00014 #define MB_ID 0x00
00015 #define NO_OF_DEV 0x01
00016 #define DEV_1_ID 0x02
00017 #define DEV_1_ST 0x03
00018 #define DEV_1_VAL 0x04
00019 #define DEV_2_ID 0x05
00020 #define DEV_2_ST 0x06
00021 #define DEV_2_VAL 0x07
00022 #define DEV_3_ID 0x08
00023 #define DEV_3_VAL 0x09
00024 #define DEV_4_ID 0x0A
00025 #define DEV_4_ST 0x0B
00026 #define DEV_4_VAL 0x0C
00027 
00028 int mb_ID = 0x00;
00029 int NOno_of_dev = 0x00;
00030 int dev_1_id = 0x00;
00031 int dev_1_st = 0x00;
00032 int dev_1_val = 0x00;
00033 int dev_2_id = 0x00;
00034 int dev_2_st = 0x00;
00035 int dev_2_val = 0x00;
00036 int dev_3_id = 0x00;
00037 int dev_3_st = 0x00;
00038 int dev_3_val = 0x00;
00039 int dev_4_id = 0x00;
00040 int dev_5_st = 0x00;
00041 int dev_6_val = 0x00;
00042 
00043 
00044 typedef struct _MyData {
00045                          int16_t sdata;
00046                          int32_t idata;
00047                          float fdata;
00048                        } MyData;
00049                        
00050                        
00051 //static void myerror(std::string msg)
00052 //{
00053 //  printf("Error %s\n",msg.c_str());
00054 //  exit(1);
00055 //}
00056 
00057 int ID_Map(int8_t id)
00058 {
00059     int return_val =0;
00060     
00061     if (id == 0x01) //ethernet
00062     return_val=DEV_1_ID;
00063     
00064     else if (id == 0x02)    //CAN
00065     return_val=DEV_2_ID;
00066     
00067     else if (id ==0x03)     //tem
00068     return_val=DEV_3_ID;
00069     
00070     else if (id == 0x04)    //aceslometer
00071     return_val=DEV_3_ID;
00072     
00073     else 
00074     return_val= -1;
00075     
00076     return return_val;
00077     }
00078     
00079 bool healthWrite( int8_t id, int8_t status, int8_t val)
00080  {  
00081      EEPROM eep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C128);  // 24C64 eeprom with sda = p9 and scl = p10
00082      int8_t add = ID_Map(id);
00083      bool return_val = false;
00084      if (add == 0)
00085      {
00086         printf("\r\n EEPROM not working..!!!");
00087         return_val = false;
00088      }
00089      else if (add == -1)
00090      {
00091         printf("\r\n Wrong Memory address..!!!");
00092         return_val = false;
00093      }
00094      else
00095      {
00096         
00097         eep.write(add,id);
00098         if(eep.getError() != 0)
00099             return_val = false;
00100         else 
00101             return_val = true;
00102        
00103         eep.write(add+1,status);
00104         if(eep.getError() != 0)
00105             return_val = false;
00106         else 
00107             return_val = true;
00108     
00109         eep.write(add+2,val);
00110         if(eep.getError() != 0)
00111             return_val = false;
00112         else 
00113             return_val = true;
00114         }
00115     
00116     //printf("\r\n Memory \r\n");
00117     //printf("\r\n %3d \r\n",i);
00118     //printf("\r\n Value is \r\n");  
00119     //printf("\r\n %3d \r\n",ival);
00120      
00121      return return_val;
00122      }
00123      
00124 int healthReadStatus( int8_t id)
00125 {
00126      EEPROM eep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C128);  // 24C64 eeprom with sda = p9 and scl = p10
00127      int8_t add = ID_Map(id);
00128      int return_val = -1;
00129      int8_t Status;
00130      if (add == 0)
00131      {
00132         printf("\r\n EEPROM not working..!!!");
00133         return_val = -1;
00134      }
00135      else if (add == -1)
00136      {
00137         printf("\r\n Wrong Memory address..!!!");
00138         return_val = -1;
00139      }
00140      else
00141      {
00142         eep.read(add+1,Status);
00143         if (eep.getError() != 0)
00144             return_val = -1;
00145         
00146         else return_val=Status;
00147     } 
00148     return return_val;
00149  
00150  }
00151  int healthReadValue( int8_t id)
00152 {
00153      EEPROM eep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C128);  // 24C64 eeprom with sda = p9 and scl = p10
00154      int8_t add = ID_Map(id);
00155      int8_t val;
00156      int8_t return_val = -1;
00157      if (add == 0)
00158      {
00159         printf("\r\n EEPROM not working..!!!");
00160         return_val = -1;
00161      }
00162      else if (add == -1)
00163      {
00164         printf("\r\n Wrong Memory address request..!!!");
00165         return_val = -1;
00166      }
00167      else
00168      {
00169         eep.read(add+2,val);
00170         if(eep.getError() != 0)
00171             return_val = -1;
00172         
00173         else 
00174             return_val = val;
00175             
00176         }
00177     return return_val;
00178 }
00179  
00180 int main() {
00181     EEPROM eep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C128);  // 24C64 eeprom with sda = p9 and scl = p10
00182     //uint8_t data[256],data_r[256];
00183     //int8_t ival;
00184     //uint16_t s;
00185     //int16_t sdata,sdata_r;
00186     //int32_t ldata[1024];
00187     int32_t max_size,eeprom_size;
00188     //uint32_t addr;
00189     //int32_t idata,idata_r;
00190     //uint32_t i; //,j,k,l,t,id;
00191     //float fdata,fdata_r;
00192     //MyData md,md_r;
00193     
00194     eeprom_size = eep.getSize();
00195     max_size = MIN(eeprom_size,256);
00196   
00197     printf("\r\n Test EEPROM I2C model %s of %d bytes\r\n",eep.getName(),eeprom_size);
00198     
00199     int8_t id =0x02;
00200     
00201     //healthWrite( int8_t id, int8_t status, int8_t val)
00202     bool helthW = healthWrite( id ,0x01, 0x05);
00203     
00204     if (helthW == true)
00205     {
00206          printf("\r\n helth write successfull \r\n");
00207         }
00208     else 
00209     {
00210         printf("\r\n helth write NOT successfull\r\n");
00211         }
00212 ///////////////////////////////////////////////////////////////////////
00213     int helthRS = healthReadStatus(id);
00214 
00215     if (helthRS == -1)
00216     {
00217          printf("\r\n helth read NOT successfull...!!! \r\n");
00218         }
00219     else 
00220     {
00221         printf("\r\n  device %X Status %X \r\n",id,helthRS);
00222         }
00223 /////////////////////////////////////////////////////////////////////
00224     int helthRV = healthReadValue(id);
00225     if (helthRV == -1)
00226     {
00227          printf("\r\n helth read NOT successfull...!!! \r\n");
00228         }
00229     else 
00230     {
00231         printf("\r\n device %X Value %X \r\n",id, helthRV);
00232         }
00233 //////////////////////////////////////////////////////////////////        
00234 //    
00235 //    i=2;
00236 //    
00237 //    eep.read(i,ival);
00238 //    if(eep.getError() != 0)
00239 //       myerror(eep.getErrorMessage());
00240 //    
00241 //    printf("\r\n Memory \r\n");
00242 //    printf("\r\n %3d \r\n",i);
00243 //    printf("\r\n Value is \r\n");  
00244 //    printf("\r\n %3d \r\n",ival);
00245 //    
00246 //    ival = 3;
00247 //    
00248 //    eep.write(i,ival);
00249 //    if(eep.getError() != 0)
00250 //       myerror(eep.getErrorMessage());
00251 //    
00252 //    printf("\r\n Memory \r\n");
00253 //    printf("\r\n %3d \r\n",i);
00254 //    printf("\r\n Value is \r\n");  
00255 //    printf("\r\n %3d \r\n",ival);
00256     
00257   
00258     while(1) {
00259 
00260     }
00261 }