EEPRoM

Dependencies:   eeprom mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Example
00002 
00003 #include <string>
00004 
00005 #include "mbed.h"
00006 #include "eeprom.h"
00007 
00008 #define EEPROM_ADDR 0x0   // I2c EEPROM address is 0x00
00009 
00010 #define SDA p9            // I2C SDA pin
00011 #define SCL p10           // I2C SCL pin
00012 
00013 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
00014 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
00015 
00016 DigitalOut led2(LED2);
00017 
00018 typedef struct _MyData {
00019                          int16_t sdata;
00020                          int32_t idata;
00021                          float fdata;
00022                        } MyData;
00023 
00024 static void myerror(std::string msg)
00025 {
00026   printf("Error %s\n",msg.c_str());
00027   exit(1);
00028 }
00029 
00030 void eeprom_test(void)
00031 {
00032   EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C64);  // 24C64 eeprom with sda = p9 and scl = p10
00033   uint8_t data[256],data_r[256];
00034   int8_t ival;
00035   uint16_t s;
00036   int16_t sdata,sdata_r;
00037   int32_t ldata[1024];
00038   int32_t eeprom_size,max_size;
00039   uint32_t addr;
00040   int32_t idata,idata_r;
00041   uint32_t i,j,k,l,t,id;
00042   float fdata,fdata_r;
00043   MyData md,md_r;
00044     
00045   eeprom_size = ep.getSize();
00046   max_size = MIN(eeprom_size,256);
00047   
00048   printf("Test EEPROM I2C model %s of %d bytes\n\n",ep.getName(),eeprom_size);
00049   
00050   // Test sequential read byte (max_size first bytes)
00051   for(i = 0;i < max_size;i++) {
00052      ep.read(i,ival);
00053      data_r[i] = ival;
00054      if(ep.getError() != 0)
00055        myerror(ep.getErrorMessage());
00056   }
00057   
00058   printf("Test sequential read %d first bytes :\n",max_size);
00059   for(i = 0;i < max_size/16;i++) {
00060      for(j = 0;j < 16;j++) {
00061         addr = i * 16 + j;
00062         printf("%3d ",(uint8_t)data_r[addr]);
00063      }
00064      printf("\n");
00065   }
00066     
00067     // Test sequential read byte (max_size last bytes)
00068   for(i = 0;i < max_size;i++) {
00069         addr = eeprom_size - max_size + i;
00070     ep.read(addr,ival);
00071     data_r[i] = ival;
00072     if(ep.getError() != 0)
00073       myerror(ep.getErrorMessage());
00074   }
00075   
00076   printf("\nTest sequential read %d last bytes :\n",max_size);
00077   for(i = 0;i < max_size/16;i++) {
00078      for(j = 0;j < 16;j++) {
00079         addr = i * 16 + j;
00080         printf("%3d ",(uint8_t)data_r[addr]);
00081      }
00082      printf("\n");
00083   }
00084   
00085   // Test write byte (max_size first bytes)
00086   for(i = 0;i < max_size;i++)
00087      data[i] = i;
00088   
00089   for(i = 0;i < max_size;i++) {
00090      ep.write(i,(int8_t)data[i]);
00091      if(ep.getError() != 0)
00092        myerror(ep.getErrorMessage());
00093   }
00094   
00095   // Test read byte (max_size first bytes)
00096   for(i = 0;i < max_size;i++) {
00097      ep.read(i,(int8_t&)ival);
00098      data_r[i] = (uint8_t)ival;
00099      if(ep.getError() != 0)
00100        myerror(ep.getErrorMessage());
00101   }
00102   
00103   printf("\nTest write and read %d first bytes :\n",max_size);
00104   for(i = 0;i < max_size/16;i++) {
00105      for(j = 0;j < 16;j++) {
00106         addr = i * 16 + j;
00107         printf("%3d ",(uint8_t)data_r[addr]);
00108      }
00109      printf("\n");
00110   }
00111   
00112   // Test current address read byte (max_size first bytes)
00113   ep.read((uint32_t)0,(int8_t&)ival); // current address is 0
00114   data_r[0] = (uint8_t)ival;
00115   if(ep.getError() != 0)
00116     myerror(ep.getErrorMessage());
00117   
00118   for(i = 1;i < max_size;i++) {
00119      ep.read((int8_t&)ival);
00120      data_r[i] = (uint8_t)ival;
00121      if(ep.getError() != 0)
00122        myerror(ep.getErrorMessage());
00123   }
00124   
00125   printf("\nTest current address read %d first bytes :\n",max_size);
00126   for(i = 0;i < max_size/16;i++) {
00127      for(j = 0;j < 16;j++) {
00128         addr = i * 16 + j;
00129         printf("%3d ",(uint8_t)data_r[addr]);
00130      }
00131      printf("\n");
00132   }
00133    
00134   // Test sequential read byte (first max_size bytes)
00135   ep.read((uint32_t)0,(int8_t *)data_r,(uint32_t) max_size);
00136   if(ep.getError() != 0)
00137     myerror(ep.getErrorMessage());
00138   
00139   printf("\nTest sequential read %d first bytes :\n",max_size);
00140   for(i = 0;i < max_size/16;i++) {
00141      for(j = 0;j < 16;j++) {
00142         addr = i * 16 + j;
00143         printf("%3d ",(uint8_t)data_r[addr]);
00144      }
00145      printf("\n");
00146   }
00147   
00148   // Test write short, long, float 
00149   sdata = -15202;
00150     addr = eeprom_size - 16;
00151   ep.write(addr,(int16_t)sdata); // short write at address eeprom_size - 16
00152   if(ep.getError() != 0)
00153     myerror(ep.getErrorMessage());
00154   
00155   idata = 45123;
00156     addr = eeprom_size - 12;
00157   ep.write(addr,(int32_t)idata); // long write at address eeprom_size - 12
00158   if(ep.getError() != 0)
00159     myerror(ep.getErrorMessage());
00160     
00161   fdata = -12.26;
00162     addr = eeprom_size - 8;
00163   ep.write(addr,(float)fdata); // float write at address eeprom_size - 8
00164   if(ep.getError() != 0)
00165     myerror(ep.getErrorMessage());
00166   
00167   // Test read short, long, float
00168   printf("\nTest write and read short (%d), long (%d), float (%f) :\n",
00169            sdata,idata,fdata);  
00170   
00171   ep.read((uint32_t)(eeprom_size - 16),(int16_t&)sdata_r);
00172   if(ep.getError() != 0)
00173     myerror(ep.getErrorMessage());
00174   printf("sdata %d\n",sdata_r);
00175   
00176   ep.read((uint32_t)(eeprom_size - 12),(int32_t&)idata_r);
00177   if(ep.getError() != 0)
00178     myerror(ep.getErrorMessage());
00179   printf("idata %d\n",idata_r);
00180   
00181   ep.read((uint32_t)(eeprom_size - 8),fdata_r);
00182   if(ep.getError() != 0)
00183     myerror(ep.getErrorMessage());
00184   printf("fdata %f\n",fdata_r);
00185   
00186   // Test read and write a structure
00187   md.sdata = -15203;
00188   md.idata = 45124;
00189   md.fdata = -12.27;
00190  
00191   ep.write((uint32_t)(eeprom_size - 32),(void *)&md,sizeof(md)); // write a structure eeprom_size - 32
00192   if(ep.getError() != 0)
00193     myerror(ep.getErrorMessage());
00194     
00195   printf("\nTest write and read a structure (%d %d %f) :\n",md.sdata,md.idata,md.fdata);
00196   
00197   ep.read((uint32_t)(eeprom_size - 32),(void *)&md_r,sizeof(md_r));
00198   if(ep.getError() != 0)
00199     myerror(ep.getErrorMessage());
00200   
00201   printf("md.sdata %d\n",md_r.sdata);
00202   printf("md.idata %d\n",md_r.idata);
00203   printf("md.fdata %f\n",md_r.fdata);
00204     
00205     // Test read and write of an array of the first max_size bytes
00206     for(i = 0;i < max_size;i++)
00207        data[i] = max_size - i - 1;
00208     
00209     ep.write((uint32_t)(0),data,(uint32_t)max_size);
00210   if(ep.getError() != 0)
00211     myerror(ep.getErrorMessage());
00212     
00213     ep.read((uint32_t)(0),data_r,(uint32_t)max_size);
00214   if(ep.getError() != 0)
00215     myerror(ep.getErrorMessage());
00216     
00217     printf("\nTest write and read an array of the first %d bytes :\n",max_size);
00218     for(i = 0;i < max_size/16;i++) {
00219      for(j = 0;j < 16;j++) {
00220         addr = i * 16 + j;
00221         printf("%3d ",(uint8_t)data_r[addr]);
00222      }
00223      printf("\n");
00224   }
00225     printf("\n");
00226   
00227   // Test write and read an array of int32
00228   s = eeprom_size / 4;                // size of eeprom in int32
00229   int ldata_size = sizeof(ldata) / 4; // size of data array in int32
00230   l = s / ldata_size;                 // loop index
00231   
00232   // size of read / write in bytes
00233   t = eeprom_size;
00234   if(t > ldata_size * 4)
00235     t = ldata_size * 4;
00236   
00237   printf("Test write and read an array of %d int32 (write entire memory) :\n",t/4);
00238 
00239   // Write entire eeprom
00240     if(l) {
00241     for(k = 0;k < l;k++) {
00242        for(i = 0;i < ldata_size;i++)
00243           ldata[i] = ldata_size * k + i;
00244         
00245        addr = k * ldata_size * 4;
00246        ep.write(addr,(void *)ldata,t);
00247        if(ep.getError() != 0)
00248          myerror(ep.getErrorMessage());
00249     }  
00250     
00251       printf("Write OK\n");
00252     
00253     // Read entire eeprom
00254       id = 0;
00255     for(k = 0;k < l;k++) {
00256        addr = k * ldata_size * 4;
00257        ep.read(addr,(void *)ldata,t);
00258        if(ep.getError() != 0)
00259          myerror(ep.getErrorMessage());
00260   
00261        // format outputs with 8 words rows
00262        for(i = 0;i < ldata_size / 8;i++) {
00263                 id++;
00264           printf("%4d ",id);
00265           for(j = 0;j < 8;j++) {
00266              addr = i * 8 + j;
00267              printf("%5d ",ldata[addr]);
00268           }
00269           printf("\n");
00270        }
00271     }
00272   }
00273     else {
00274         for(i = 0;i < s;i++)
00275        ldata[i] = i;
00276         
00277     addr = 0;
00278     ep.write(addr,(void *)ldata,t);
00279     if(ep.getError() != 0)
00280       myerror(ep.getErrorMessage());
00281         
00282         printf("Write OK\n");
00283     
00284     // Read entire eeprom
00285       id = 0;
00286     
00287     addr = 0;
00288     ep.read(addr,(void *)ldata,t);
00289     if(ep.getError() != 0)
00290       myerror(ep.getErrorMessage());
00291   
00292     // format outputs with 8 words rows
00293     for(i = 0;i < s / 8;i++) {
00294              id++;
00295        printf("%4d ",id);
00296        for(j = 0;j < 8;j++) {
00297           addr = i * 8 + j;
00298           printf("%5d ",ldata[addr]);
00299        }
00300        printf("\n");
00301     }
00302     }
00303   
00304   // clear eeprom
00305   printf("\nClear eeprom\n");
00306 
00307   ep.clear();
00308   if(ep.getError() != 0)
00309     myerror(ep.getErrorMessage());
00310     
00311   printf("End\n");  
00312     
00313 }
00314 
00315 int main() 
00316 {
00317 
00318   eeprom_test();
00319     
00320   return(0);
00321 }