Feng Hong / Mbed OS Nucleo_rtos_basic
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers eeprom_cust.cpp Source File

eeprom_cust.cpp

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