Proyecto ABInBev para la tarjeta Guaria 1/2.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers eeprom.h Source File

eeprom.h

00001 /***********************************************************
00002 Author: Bernard Borredon
00003 Date : 21 decembre 2015
00004 Version: 1.3
00005   - Correct write(uint32_t address, int8_t data[], uint32_t length) for eeprom >= T24C32.
00006     Tested with 24C02, 24C08, 24C16, 24C64, 24C256, 24C512, 24C1025 on LPC1768 (mbed online and µVision V5.16a).
00007   - Correct main test.
00008     
00009 Date : 12 decembre 2013
00010 Version: 1.2
00011   - Update api documentation
00012   
00013 Date: 11 december 2013
00014 Version: 1.1
00015   - Change address parameter size form uint16_t to uint32_t (error for eeprom > 24C256).
00016   - Change size parameter size from uint16_t to uint32_t (error for eeprom > 24C256).
00017     - Add EEPROM name as a private static const char array.
00018     - Add function getName.
00019     - Add a test program.
00020  
00021 Date: 27 december 2011
00022 Version: 1.0
00023 ************************************************************/
00024 
00025 #ifndef __EXT_I2C_EEPROM__H_
00026 #define __EXT_I2C_EEPROM__H_
00027   
00028 // Includes
00029 #include <string> 
00030 #include "mbed.h"
00031  
00032 // Example
00033 /*
00034 #include <string>
00035  
00036 #include "mbed.h"
00037 #include "eeprom.h"
00038  
00039 #define EEPROM_ADDR 0x0   // I2c EEPROM address is 0x00
00040  
00041 #define SDA p9            // I2C SDA pin
00042 #define SCL p10           // I2C SCL pin
00043  
00044 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
00045 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
00046  
00047 DigitalOut led2(LED2);
00048  
00049 typedef struct _MyData {
00050                          int16_t sdata;
00051                          int32_t idata;
00052                          float fdata;
00053                        } MyData;
00054  
00055 static void myerror(std::string msg)
00056 {
00057   printf("Error %s\n",msg.c_str());
00058   exit(1);
00059 }
00060  
00061 void eeprom_test(void)
00062 {
00063   EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C64);  // 24C64 eeprom with sda = p9 and scl = p10
00064   uint8_t data[256],data_r[256];
00065   int8_t ival;
00066   uint16_t s;
00067   int16_t sdata,sdata_r;
00068   int32_t ldata[1024];
00069   int32_t eeprom_size,max_size;
00070   uint32_t addr;
00071   int32_t idata,idata_r;
00072   uint32_t i,j,k,l,t,id;
00073   float fdata,fdata_r;
00074   MyData md,md_r;
00075     
00076   eeprom_size = ep.getSize();
00077   max_size = MIN(eeprom_size,256);
00078   
00079   printf("Test EEPROM I2C model %s of %d bytes\n\n",ep.getName(),eeprom_size);
00080   
00081   // Test sequential read byte (max_size first bytes)
00082   for(i = 0;i < max_size;i++) {
00083      ep.read(i,ival);
00084      data_r[i] = ival;
00085      if(ep.getError() != 0)
00086        myerror(ep.getErrorMessage());
00087   }
00088   
00089   printf("Test sequential read %d first bytes :\n",max_size);
00090   for(i = 0;i < max_size/16;i++) {
00091      for(j = 0;j < 16;j++) {
00092         addr = i * 16 + j;
00093         printf("%3d ",(uint8_t)data_r[addr]);
00094      }
00095      printf("\n");
00096   }
00097     
00098     // Test sequential read byte (max_size last bytes)
00099   for(i = 0;i < max_size;i++) {
00100         addr = eeprom_size - max_size + i;
00101     ep.read(addr,ival);
00102     data_r[i] = ival;
00103     if(ep.getError() != 0)
00104       myerror(ep.getErrorMessage());
00105   }
00106   
00107   printf("\nTest sequential read %d last bytes :\n",max_size);
00108   for(i = 0;i < max_size/16;i++) {
00109      for(j = 0;j < 16;j++) {
00110         addr = i * 16 + j;
00111         printf("%3d ",(uint8_t)data_r[addr]);
00112      }
00113      printf("\n");
00114   }
00115   
00116   // Test write byte (max_size first bytes)
00117   for(i = 0;i < max_size;i++)
00118      data[i] = i;
00119   
00120   for(i = 0;i < max_size;i++) {
00121      ep.write(i,(int8_t)data[i]);
00122      if(ep.getError() != 0)
00123        myerror(ep.getErrorMessage());
00124   }
00125   
00126   // Test read byte (max_size first bytes)
00127   for(i = 0;i < max_size;i++) {
00128      ep.read(i,(int8_t&)ival);
00129      data_r[i] = (uint8_t)ival;
00130      if(ep.getError() != 0)
00131        myerror(ep.getErrorMessage());
00132   }
00133   
00134   printf("\nTest write and read %d first bytes :\n",max_size);
00135   for(i = 0;i < max_size/16;i++) {
00136      for(j = 0;j < 16;j++) {
00137         addr = i * 16 + j;
00138         printf("%3d ",(uint8_t)data_r[addr]);
00139      }
00140      printf("\n");
00141   }
00142   
00143   // Test current address read byte (max_size first bytes)
00144   ep.read((uint32_t)0,(int8_t&)ival); // current address is 0
00145   data_r[0] = (uint8_t)ival;
00146   if(ep.getError() != 0)
00147     myerror(ep.getErrorMessage());
00148   
00149   for(i = 1;i < max_size;i++) {
00150      ep.read((int8_t&)ival);
00151      data_r[i] = (uint8_t)ival;
00152      if(ep.getError() != 0)
00153        myerror(ep.getErrorMessage());
00154   }
00155   
00156   printf("\nTest current address read %d first bytes :\n",max_size);
00157   for(i = 0;i < max_size/16;i++) {
00158      for(j = 0;j < 16;j++) {
00159         addr = i * 16 + j;
00160         printf("%3d ",(uint8_t)data_r[addr]);
00161      }
00162      printf("\n");
00163   }
00164    
00165   // Test sequential read byte (first max_size bytes)
00166   ep.read((uint32_t)0,(int8_t *)data_r,(uint32_t) max_size);
00167   if(ep.getError() != 0)
00168     myerror(ep.getErrorMessage());
00169   
00170   printf("\nTest sequential read %d first bytes :\n",max_size);
00171   for(i = 0;i < max_size/16;i++) {
00172      for(j = 0;j < 16;j++) {
00173         addr = i * 16 + j;
00174         printf("%3d ",(uint8_t)data_r[addr]);
00175      }
00176      printf("\n");
00177   }
00178   
00179   // Test write short, long, float 
00180   sdata = -15202;
00181     addr = eeprom_size - 16;
00182   ep.write(addr,(int16_t)sdata); // short write at address eeprom_size - 16
00183   if(ep.getError() != 0)
00184     myerror(ep.getErrorMessage());
00185   
00186   idata = 45123;
00187     addr = eeprom_size - 12;
00188   ep.write(addr,(int32_t)idata); // long write at address eeprom_size - 12
00189   if(ep.getError() != 0)
00190     myerror(ep.getErrorMessage());
00191     
00192   fdata = -12.26;
00193     addr = eeprom_size - 8;
00194   ep.write(addr,(float)fdata); // float write at address eeprom_size - 8
00195   if(ep.getError() != 0)
00196     myerror(ep.getErrorMessage());
00197   
00198   // Test read short, long, float
00199   printf("\nTest write and read short (%d), long (%d), float (%f) :\n",
00200            sdata,idata,fdata);  
00201   
00202   ep.read((uint32_t)(eeprom_size - 16),(int16_t&)sdata_r);
00203   if(ep.getError() != 0)
00204     myerror(ep.getErrorMessage());
00205   printf("sdata %d\n",sdata_r);
00206   
00207   ep.read((uint32_t)(eeprom_size - 12),(int32_t&)idata_r);
00208   if(ep.getError() != 0)
00209     myerror(ep.getErrorMessage());
00210   printf("idata %d\n",idata_r);
00211   
00212   ep.read((uint32_t)(eeprom_size - 8),fdata_r);
00213   if(ep.getError() != 0)
00214     myerror(ep.getErrorMessage());
00215   printf("fdata %f\n",fdata_r);
00216   
00217   // Test read and write a structure
00218   md.sdata = -15203;
00219   md.idata = 45124;
00220   md.fdata = -12.27;
00221  
00222   ep.write((uint32_t)(eeprom_size - 32),(void *)&md,sizeof(md)); // write a structure eeprom_size - 32
00223   if(ep.getError() != 0)
00224     myerror(ep.getErrorMessage());
00225     
00226   printf("\nTest write and read a structure (%d %d %f) :\n",md.sdata,md.idata,md.fdata);
00227   
00228   ep.read((uint32_t)(eeprom_size - 32),(void *)&md_r,sizeof(md_r));
00229   if(ep.getError() != 0)
00230     myerror(ep.getErrorMessage());
00231   
00232   printf("md.sdata %d\n",md_r.sdata);
00233   printf("md.idata %d\n",md_r.idata);
00234   printf("md.fdata %f\n",md_r.fdata);
00235     
00236     // Test read and write of an array of the first max_size bytes
00237     for(i = 0;i < max_size;i++)
00238        data[i] = max_size - i - 1;
00239     
00240     ep.write((uint32_t)(0),data,(uint32_t)max_size);
00241   if(ep.getError() != 0)
00242     myerror(ep.getErrorMessage());
00243     
00244     ep.read((uint32_t)(0),data_r,(uint32_t)max_size);
00245   if(ep.getError() != 0)
00246     myerror(ep.getErrorMessage());
00247     
00248     printf("\nTest write and read an array of the first %d bytes :\n",max_size);
00249     for(i = 0;i < max_size/16;i++) {
00250      for(j = 0;j < 16;j++) {
00251         addr = i * 16 + j;
00252         printf("%3d ",(uint8_t)data_r[addr]);
00253      }
00254      printf("\n");
00255   }
00256     printf("\n");
00257   
00258   // Test write and read an array of int32
00259   s = eeprom_size / 4;                // size of eeprom in int32
00260   int ldata_size = sizeof(ldata) / 4; // size of data array in int32
00261   l = s / ldata_size;                 // loop index
00262   
00263   // size of read / write in bytes
00264   t = eeprom_size;
00265   if(t > ldata_size * 4)
00266     t = ldata_size * 4;
00267   
00268   printf("Test write and read an array of %d int32 (write entire memory) :\n",t/4);
00269  
00270   // Write entire eeprom
00271     if(l) {
00272     for(k = 0;k < l;k++) {
00273        for(i = 0;i < ldata_size;i++)
00274           ldata[i] = ldata_size * k + i;
00275         
00276        addr = k * ldata_size * 4;
00277        ep.write(addr,(void *)ldata,t);
00278        if(ep.getError() != 0)
00279          myerror(ep.getErrorMessage());
00280     }  
00281     
00282       printf("Write OK\n");
00283     
00284     // Read entire eeprom
00285       id = 0;
00286     for(k = 0;k < l;k++) {
00287        addr = k * ldata_size * 4;
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 < ldata_size / 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     else {
00305         for(i = 0;i < s;i++)
00306        ldata[i] = i;
00307         
00308     addr = 0;
00309     ep.write(addr,(void *)ldata,t);
00310     if(ep.getError() != 0)
00311       myerror(ep.getErrorMessage());
00312         
00313         printf("Write OK\n");
00314     
00315     // Read entire eeprom
00316       id = 0;
00317     
00318     addr = 0;
00319     ep.read(addr,(void *)ldata,t);
00320     if(ep.getError() != 0)
00321       myerror(ep.getErrorMessage());
00322   
00323     // format outputs with 8 words rows
00324     for(i = 0;i < s / 8;i++) {
00325              id++;
00326        printf("%4d ",id);
00327        for(j = 0;j < 8;j++) {
00328           addr = i * 8 + j;
00329           printf("%5d ",ldata[addr]);
00330        }
00331        printf("\n");
00332     }
00333     }
00334   
00335   // clear eeprom
00336   printf("\nClear eeprom\n");
00337  
00338   ep.clear();
00339   if(ep.getError() != 0)
00340     myerror(ep.getErrorMessage());
00341     
00342   printf("End\n");  
00343     
00344 }
00345  
00346 int main() 
00347 {
00348  
00349   eeprom_test();
00350     
00351   return(0);
00352 }
00353 */
00354  
00355 // Defines
00356 #define EEPROM_Address     0xa0
00357 #define EEPROM_NoError     0x00
00358 #define EEPROM_BadAddress  0x01
00359 #define EEPROM_I2cError    0x02
00360 #define EEPROM_ParamError  0x03
00361 #define EEPROM_OutOfRange  0x04
00362 #define EEPROM_MallocError 0x05
00363  
00364 #define EEPROM_MaxError       6
00365  
00366 static std::string _ErrorMessageEEPROM[EEPROM_MaxError] = {
00367                                                             "",
00368                                                             "Bad chip address",
00369                                                             "I2C error (nack)",
00370                                                             "Invalid parameter",
00371                                                             "Data address out of range",
00372                                                             "Memory allocation error"
00373                                                           };
00374  
00375 /** EEPROM Class
00376 */
00377 class EEPROM {
00378 public:
00379     enum TypeEeprom {T24C01=128,T24C02=256,T24C04=512,T24C08=1024,T24C16=2048,
00380                      T24C32=4096,T24C64=8192,T24C128=16384,T24C256=32768,
00381                      T24C512=65536,T24C1024=131072,T24C1025=131073} Type;
00382                                          
00383     /**
00384      * Constructor, initialize the eeprom on i2c interface.
00385      * @param sda sda i2c pin (PinName)
00386      * @param scl scl i2c pin (PinName)
00387      * @param address eeprom address, according to eeprom type (uint8_t)
00388      * @param type eeprom type (TypeEeprom) 
00389      * @return none
00390     */
00391     EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type);
00392     
00393     /**
00394      * Random read byte
00395      * @param address start address (uint32_t)
00396      * @param data byte to read (int8_t&)
00397      * @return none
00398     */
00399     void read(uint32_t address, int8_t& data);
00400     
00401     /**
00402      * Random read short
00403      * @param address start address (uint32_t)
00404      * @param data short to read (int16_t&)
00405      * @return none
00406     */
00407     void read(uint32_t address, int16_t& data);
00408     
00409     /**
00410      * Random read long
00411      * @param address start address (uint32_t)
00412      * @param data long to read (int32_t&)
00413      * @return none
00414     */
00415     void read(uint32_t address, int32_t& data);
00416     
00417     /**
00418      * Random read float
00419      * @param address start address (uint32_t)
00420      * @param data float to read (float&)
00421      * @return none
00422     */
00423     void read(uint32_t address, float& data);
00424     
00425     /**
00426      * Random read anything
00427      * @param address start address (uint32_t)
00428      * @param data data to read (void *)
00429      * @param size number of bytes to read (uint32_t)
00430      * @return none
00431     */
00432     void read(uint32_t address, void *data, uint32_t size);
00433     
00434     /**
00435      * Current address read byte
00436      * @param data byte to read (int8_t&)
00437      * @return none
00438     */
00439     void read(int8_t& data);
00440     
00441     /**
00442      * Sequential read byte
00443      * @param address start address (uint32_t)
00444      * @param data bytes array to read (int8_t[]&)
00445      * @param size number of bytes to read (uint32_t)
00446      * @return none
00447     */
00448     void read(uint32_t address, int8_t *data, uint32_t size);
00449     
00450     /**
00451      * Write byte
00452      * @param address start address (uint32_t)
00453      * @param data byte to write (int8_t)
00454      * @return none
00455     */
00456     void write(uint32_t address, int8_t data);
00457     
00458     /**
00459      * Write short
00460      * @param address start address (uint32_t)
00461      * @param data short to write (int16_t)
00462      * @return none
00463     */
00464     void write(uint32_t address, int16_t data);
00465     
00466     /**
00467      * Write long
00468      * @param address start address (uint32_t)
00469      * @param data long to write (int32_t)
00470      * @return none
00471     */
00472     void write(uint32_t address, int32_t data);
00473     
00474     /**
00475      * Write float
00476      * @param address start address (uint32_t)
00477      * @param data float to write (float)
00478      * @return none
00479     */
00480     void write(uint32_t address, float data);
00481     
00482     /**
00483      * Write anything (use the page write mode)
00484      * @param address start address (uint32_t)
00485      * @param data data to write (void *)
00486      * @param size number of bytes to write (uint32_t)
00487      * @return none
00488     */
00489     void write(uint32_t address, void *data, uint32_t size);
00490     
00491     /**
00492      * Write array of bytes (use the page mode)
00493      * @param address start address (uint32_t)
00494      * @param data bytes array to write (int8_t[])
00495      * @param size number of bytes to write (uint32_t)
00496      * @return none
00497     */
00498     void write(uint32_t address, int8_t data[], uint32_t size);
00499     
00500     /**
00501      * Wait eeprom ready
00502      * @param none
00503      * @return none
00504     */
00505     void ready(void);
00506     
00507     /**
00508      * Get eeprom size in bytes
00509      * @param none
00510      * @return size in bytes (uint32_t)
00511     */
00512     uint32_t getSize(void);
00513         
00514     /**
00515      * Get eeprom name
00516      * @param none
00517      * @return name (const char*)
00518     */
00519     const char* getName(void);
00520     
00521     /**
00522      * Clear eeprom (write with 0)
00523      * @param  none
00524      * @return none
00525     */
00526     void clear(void);
00527     
00528      /**
00529      * Get the current error number (EEPROM_NoError if no error)
00530      * @param  none
00531      * @return none
00532     */
00533     uint8_t getError(void);
00534     
00535     /**
00536      * Get current error message
00537      * @param  none
00538      * @return current error message(std::string)
00539     */
00540     std::string getErrorMessage(void)
00541     { 
00542       return(_ErrorMessageEEPROM[_errnum]);
00543     }
00544     
00545 //---------- local variables ----------
00546 private:
00547     I2C _i2c;              // Local i2c communication interface instance
00548     int _address;          // Local i2c address
00549     uint8_t _errnum;       // Error number
00550     TypeEeprom _type;      // EEPROM type
00551     uint8_t _page_write;   // Page write size
00552     uint8_t _page_number;  // Number of page
00553     uint32_t _size;        // Size in bytes
00554     bool checkAddress(uint32_t address); // Check address range
00555     static const char * const _name[]; // eeprom name
00556 //-------------------------------------
00557 };
00558 
00559 #endif //__EXT_I2C_EEPROM__H_