Proyecto ABInBev para la tarjeta Guaria 1/2.

Committer:
fmanzano_dtk
Date:
Wed Jul 06 21:00:02 2022 -0600
Revision:
10:a0005a4116ae
Parent:
1:9e821e640117
Child:
17:c53ea16f75c0
Finalizacion de modificaciones para grabar en EEPROM o FLASH

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fmanzano_dtk 1:9e821e640117 1 #ifndef __EEPROM__H_
fmanzano_dtk 1:9e821e640117 2 #define __EEPROM__H_
fmanzano_dtk 1:9e821e640117 3
fmanzano_dtk 1:9e821e640117 4 /***********************************************************
fmanzano_dtk 1:9e821e640117 5 Author: Bernard Borredon
fmanzano_dtk 1:9e821e640117 6 Date : 21 decembre 2015
fmanzano_dtk 1:9e821e640117 7 Version: 1.3
fmanzano_dtk 1:9e821e640117 8 - Correct write(uint32_t address, int8_t data[], uint32_t length) for eeprom >= T24C32.
fmanzano_dtk 1:9e821e640117 9 Tested with 24C02, 24C08, 24C16, 24C64, 24C256, 24C512, 24C1025 on LPC1768 (mbed online and µVision V5.16a).
fmanzano_dtk 1:9e821e640117 10 - Correct main test.
fmanzano_dtk 1:9e821e640117 11
fmanzano_dtk 1:9e821e640117 12 Date : 12 decembre 2013
fmanzano_dtk 1:9e821e640117 13 Version: 1.2
fmanzano_dtk 1:9e821e640117 14 - Update api documentation
fmanzano_dtk 1:9e821e640117 15
fmanzano_dtk 1:9e821e640117 16 Date: 11 december 2013
fmanzano_dtk 1:9e821e640117 17 Version: 1.1
fmanzano_dtk 1:9e821e640117 18 - Change address parameter size form uint16_t to uint32_t (error for eeprom > 24C256).
fmanzano_dtk 1:9e821e640117 19 - Change size parameter size from uint16_t to uint32_t (error for eeprom > 24C256).
fmanzano_dtk 1:9e821e640117 20 - Add EEPROM name as a private static const char array.
fmanzano_dtk 1:9e821e640117 21 - Add function getName.
fmanzano_dtk 1:9e821e640117 22 - Add a test program.
fmanzano_dtk 1:9e821e640117 23
fmanzano_dtk 1:9e821e640117 24 Date: 27 december 2011
fmanzano_dtk 1:9e821e640117 25 Version: 1.0
fmanzano_dtk 1:9e821e640117 26 ************************************************************/
fmanzano_dtk 1:9e821e640117 27
fmanzano_dtk 1:9e821e640117 28 // Includes
fmanzano_dtk 1:9e821e640117 29 #include <string>
fmanzano_dtk 1:9e821e640117 30
fmanzano_dtk 1:9e821e640117 31 #include "mbed.h"
fmanzano_dtk 1:9e821e640117 32
fmanzano_dtk 1:9e821e640117 33 // Example
fmanzano_dtk 1:9e821e640117 34 /*
fmanzano_dtk 1:9e821e640117 35 #include <string>
fmanzano_dtk 1:9e821e640117 36
fmanzano_dtk 1:9e821e640117 37 #include "mbed.h"
fmanzano_dtk 1:9e821e640117 38 #include "eeprom.h"
fmanzano_dtk 1:9e821e640117 39
fmanzano_dtk 1:9e821e640117 40 #define EEPROM_ADDR 0x0 // I2c EEPROM address is 0x00
fmanzano_dtk 1:9e821e640117 41
fmanzano_dtk 1:9e821e640117 42 #define SDA p9 // I2C SDA pin
fmanzano_dtk 1:9e821e640117 43 #define SCL p10 // I2C SCL pin
fmanzano_dtk 1:9e821e640117 44
fmanzano_dtk 1:9e821e640117 45 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
fmanzano_dtk 1:9e821e640117 46 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
fmanzano_dtk 1:9e821e640117 47
fmanzano_dtk 1:9e821e640117 48 DigitalOut led2(LED2);
fmanzano_dtk 1:9e821e640117 49
fmanzano_dtk 1:9e821e640117 50 typedef struct _MyData {
fmanzano_dtk 1:9e821e640117 51 int16_t sdata;
fmanzano_dtk 1:9e821e640117 52 int32_t idata;
fmanzano_dtk 1:9e821e640117 53 float fdata;
fmanzano_dtk 1:9e821e640117 54 } MyData;
fmanzano_dtk 1:9e821e640117 55
fmanzano_dtk 1:9e821e640117 56 static void myerror(std::string msg)
fmanzano_dtk 1:9e821e640117 57 {
fmanzano_dtk 1:9e821e640117 58 printf("Error %s\n",msg.c_str());
fmanzano_dtk 1:9e821e640117 59 exit(1);
fmanzano_dtk 1:9e821e640117 60 }
fmanzano_dtk 1:9e821e640117 61
fmanzano_dtk 1:9e821e640117 62 void eeprom_test(void)
fmanzano_dtk 1:9e821e640117 63 {
fmanzano_dtk 1:9e821e640117 64 EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C64); // 24C64 eeprom with sda = p9 and scl = p10
fmanzano_dtk 1:9e821e640117 65 uint8_t data[256],data_r[256];
fmanzano_dtk 1:9e821e640117 66 int8_t ival;
fmanzano_dtk 1:9e821e640117 67 uint16_t s;
fmanzano_dtk 1:9e821e640117 68 int16_t sdata,sdata_r;
fmanzano_dtk 1:9e821e640117 69 int32_t ldata[1024];
fmanzano_dtk 1:9e821e640117 70 int32_t eeprom_size,max_size;
fmanzano_dtk 1:9e821e640117 71 uint32_t addr;
fmanzano_dtk 1:9e821e640117 72 int32_t idata,idata_r;
fmanzano_dtk 1:9e821e640117 73 uint32_t i,j,k,l,t,id;
fmanzano_dtk 1:9e821e640117 74 float fdata,fdata_r;
fmanzano_dtk 1:9e821e640117 75 MyData md,md_r;
fmanzano_dtk 1:9e821e640117 76
fmanzano_dtk 1:9e821e640117 77 eeprom_size = ep.getSize();
fmanzano_dtk 1:9e821e640117 78 max_size = MIN(eeprom_size,256);
fmanzano_dtk 1:9e821e640117 79
fmanzano_dtk 1:9e821e640117 80 printf("Test EEPROM I2C model %s of %d bytes\n\n",ep.getName(),eeprom_size);
fmanzano_dtk 1:9e821e640117 81
fmanzano_dtk 1:9e821e640117 82 // Test sequential read byte (max_size first bytes)
fmanzano_dtk 1:9e821e640117 83 for(i = 0;i < max_size;i++) {
fmanzano_dtk 1:9e821e640117 84 ep.read(i,ival);
fmanzano_dtk 1:9e821e640117 85 data_r[i] = ival;
fmanzano_dtk 1:9e821e640117 86 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 87 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 88 }
fmanzano_dtk 1:9e821e640117 89
fmanzano_dtk 1:9e821e640117 90 printf("Test sequential read %d first bytes :\n",max_size);
fmanzano_dtk 1:9e821e640117 91 for(i = 0;i < max_size/16;i++) {
fmanzano_dtk 1:9e821e640117 92 for(j = 0;j < 16;j++) {
fmanzano_dtk 1:9e821e640117 93 addr = i * 16 + j;
fmanzano_dtk 1:9e821e640117 94 printf("%3d ",(uint8_t)data_r[addr]);
fmanzano_dtk 1:9e821e640117 95 }
fmanzano_dtk 1:9e821e640117 96 printf("\n");
fmanzano_dtk 1:9e821e640117 97 }
fmanzano_dtk 1:9e821e640117 98
fmanzano_dtk 1:9e821e640117 99 // Test sequential read byte (max_size last bytes)
fmanzano_dtk 1:9e821e640117 100 for(i = 0;i < max_size;i++) {
fmanzano_dtk 1:9e821e640117 101 addr = eeprom_size - max_size + i;
fmanzano_dtk 1:9e821e640117 102 ep.read(addr,ival);
fmanzano_dtk 1:9e821e640117 103 data_r[i] = ival;
fmanzano_dtk 1:9e821e640117 104 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 105 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 106 }
fmanzano_dtk 1:9e821e640117 107
fmanzano_dtk 1:9e821e640117 108 printf("\nTest sequential read %d last bytes :\n",max_size);
fmanzano_dtk 1:9e821e640117 109 for(i = 0;i < max_size/16;i++) {
fmanzano_dtk 1:9e821e640117 110 for(j = 0;j < 16;j++) {
fmanzano_dtk 1:9e821e640117 111 addr = i * 16 + j;
fmanzano_dtk 1:9e821e640117 112 printf("%3d ",(uint8_t)data_r[addr]);
fmanzano_dtk 1:9e821e640117 113 }
fmanzano_dtk 1:9e821e640117 114 printf("\n");
fmanzano_dtk 1:9e821e640117 115 }
fmanzano_dtk 1:9e821e640117 116
fmanzano_dtk 1:9e821e640117 117 // Test write byte (max_size first bytes)
fmanzano_dtk 1:9e821e640117 118 for(i = 0;i < max_size;i++)
fmanzano_dtk 1:9e821e640117 119 data[i] = i;
fmanzano_dtk 1:9e821e640117 120
fmanzano_dtk 1:9e821e640117 121 for(i = 0;i < max_size;i++) {
fmanzano_dtk 1:9e821e640117 122 ep.write(i,(int8_t)data[i]);
fmanzano_dtk 1:9e821e640117 123 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 124 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 125 }
fmanzano_dtk 1:9e821e640117 126
fmanzano_dtk 1:9e821e640117 127 // Test read byte (max_size first bytes)
fmanzano_dtk 1:9e821e640117 128 for(i = 0;i < max_size;i++) {
fmanzano_dtk 1:9e821e640117 129 ep.read(i,(int8_t&)ival);
fmanzano_dtk 1:9e821e640117 130 data_r[i] = (uint8_t)ival;
fmanzano_dtk 1:9e821e640117 131 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 132 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 133 }
fmanzano_dtk 1:9e821e640117 134
fmanzano_dtk 1:9e821e640117 135 printf("\nTest write and read %d first bytes :\n",max_size);
fmanzano_dtk 1:9e821e640117 136 for(i = 0;i < max_size/16;i++) {
fmanzano_dtk 1:9e821e640117 137 for(j = 0;j < 16;j++) {
fmanzano_dtk 1:9e821e640117 138 addr = i * 16 + j;
fmanzano_dtk 1:9e821e640117 139 printf("%3d ",(uint8_t)data_r[addr]);
fmanzano_dtk 1:9e821e640117 140 }
fmanzano_dtk 1:9e821e640117 141 printf("\n");
fmanzano_dtk 1:9e821e640117 142 }
fmanzano_dtk 1:9e821e640117 143
fmanzano_dtk 1:9e821e640117 144 // Test current address read byte (max_size first bytes)
fmanzano_dtk 1:9e821e640117 145 ep.read((uint32_t)0,(int8_t&)ival); // current address is 0
fmanzano_dtk 1:9e821e640117 146 data_r[0] = (uint8_t)ival;
fmanzano_dtk 1:9e821e640117 147 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 148 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 149
fmanzano_dtk 1:9e821e640117 150 for(i = 1;i < max_size;i++) {
fmanzano_dtk 1:9e821e640117 151 ep.read((int8_t&)ival);
fmanzano_dtk 1:9e821e640117 152 data_r[i] = (uint8_t)ival;
fmanzano_dtk 1:9e821e640117 153 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 154 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 155 }
fmanzano_dtk 1:9e821e640117 156
fmanzano_dtk 1:9e821e640117 157 printf("\nTest current address read %d first bytes :\n",max_size);
fmanzano_dtk 1:9e821e640117 158 for(i = 0;i < max_size/16;i++) {
fmanzano_dtk 1:9e821e640117 159 for(j = 0;j < 16;j++) {
fmanzano_dtk 1:9e821e640117 160 addr = i * 16 + j;
fmanzano_dtk 1:9e821e640117 161 printf("%3d ",(uint8_t)data_r[addr]);
fmanzano_dtk 1:9e821e640117 162 }
fmanzano_dtk 1:9e821e640117 163 printf("\n");
fmanzano_dtk 1:9e821e640117 164 }
fmanzano_dtk 1:9e821e640117 165
fmanzano_dtk 1:9e821e640117 166 // Test sequential read byte (first max_size bytes)
fmanzano_dtk 1:9e821e640117 167 ep.read((uint32_t)0,(int8_t *)data_r,(uint32_t) max_size);
fmanzano_dtk 1:9e821e640117 168 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 169 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 170
fmanzano_dtk 1:9e821e640117 171 printf("\nTest sequential read %d first bytes :\n",max_size);
fmanzano_dtk 1:9e821e640117 172 for(i = 0;i < max_size/16;i++) {
fmanzano_dtk 1:9e821e640117 173 for(j = 0;j < 16;j++) {
fmanzano_dtk 1:9e821e640117 174 addr = i * 16 + j;
fmanzano_dtk 1:9e821e640117 175 printf("%3d ",(uint8_t)data_r[addr]);
fmanzano_dtk 1:9e821e640117 176 }
fmanzano_dtk 1:9e821e640117 177 printf("\n");
fmanzano_dtk 1:9e821e640117 178 }
fmanzano_dtk 1:9e821e640117 179
fmanzano_dtk 1:9e821e640117 180 // Test write short, long, float
fmanzano_dtk 1:9e821e640117 181 sdata = -15202;
fmanzano_dtk 1:9e821e640117 182 addr = eeprom_size - 16;
fmanzano_dtk 1:9e821e640117 183 ep.write(addr,(int16_t)sdata); // short write at address eeprom_size - 16
fmanzano_dtk 1:9e821e640117 184 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 185 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 186
fmanzano_dtk 1:9e821e640117 187 idata = 45123;
fmanzano_dtk 1:9e821e640117 188 addr = eeprom_size - 12;
fmanzano_dtk 1:9e821e640117 189 ep.write(addr,(int32_t)idata); // long write at address eeprom_size - 12
fmanzano_dtk 1:9e821e640117 190 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 191 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 192
fmanzano_dtk 1:9e821e640117 193 fdata = -12.26;
fmanzano_dtk 1:9e821e640117 194 addr = eeprom_size - 8;
fmanzano_dtk 1:9e821e640117 195 ep.write(addr,(float)fdata); // float write at address eeprom_size - 8
fmanzano_dtk 1:9e821e640117 196 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 197 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 198
fmanzano_dtk 1:9e821e640117 199 // Test read short, long, float
fmanzano_dtk 1:9e821e640117 200 printf("\nTest write and read short (%d), long (%d), float (%f) :\n",
fmanzano_dtk 1:9e821e640117 201 sdata,idata,fdata);
fmanzano_dtk 1:9e821e640117 202
fmanzano_dtk 1:9e821e640117 203 ep.read((uint32_t)(eeprom_size - 16),(int16_t&)sdata_r);
fmanzano_dtk 1:9e821e640117 204 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 205 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 206 printf("sdata %d\n",sdata_r);
fmanzano_dtk 1:9e821e640117 207
fmanzano_dtk 1:9e821e640117 208 ep.read((uint32_t)(eeprom_size - 12),(int32_t&)idata_r);
fmanzano_dtk 1:9e821e640117 209 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 210 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 211 printf("idata %d\n",idata_r);
fmanzano_dtk 1:9e821e640117 212
fmanzano_dtk 1:9e821e640117 213 ep.read((uint32_t)(eeprom_size - 8),fdata_r);
fmanzano_dtk 1:9e821e640117 214 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 215 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 216 printf("fdata %f\n",fdata_r);
fmanzano_dtk 1:9e821e640117 217
fmanzano_dtk 1:9e821e640117 218 // Test read and write a structure
fmanzano_dtk 1:9e821e640117 219 md.sdata = -15203;
fmanzano_dtk 1:9e821e640117 220 md.idata = 45124;
fmanzano_dtk 1:9e821e640117 221 md.fdata = -12.27;
fmanzano_dtk 1:9e821e640117 222
fmanzano_dtk 1:9e821e640117 223 ep.write((uint32_t)(eeprom_size - 32),(void *)&md,sizeof(md)); // write a structure eeprom_size - 32
fmanzano_dtk 1:9e821e640117 224 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 225 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 226
fmanzano_dtk 1:9e821e640117 227 printf("\nTest write and read a structure (%d %d %f) :\n",md.sdata,md.idata,md.fdata);
fmanzano_dtk 1:9e821e640117 228
fmanzano_dtk 1:9e821e640117 229 ep.read((uint32_t)(eeprom_size - 32),(void *)&md_r,sizeof(md_r));
fmanzano_dtk 1:9e821e640117 230 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 231 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 232
fmanzano_dtk 1:9e821e640117 233 printf("md.sdata %d\n",md_r.sdata);
fmanzano_dtk 1:9e821e640117 234 printf("md.idata %d\n",md_r.idata);
fmanzano_dtk 1:9e821e640117 235 printf("md.fdata %f\n",md_r.fdata);
fmanzano_dtk 1:9e821e640117 236
fmanzano_dtk 1:9e821e640117 237 // Test read and write of an array of the first max_size bytes
fmanzano_dtk 1:9e821e640117 238 for(i = 0;i < max_size;i++)
fmanzano_dtk 1:9e821e640117 239 data[i] = max_size - i - 1;
fmanzano_dtk 1:9e821e640117 240
fmanzano_dtk 1:9e821e640117 241 ep.write((uint32_t)(0),data,(uint32_t)max_size);
fmanzano_dtk 1:9e821e640117 242 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 243 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 244
fmanzano_dtk 1:9e821e640117 245 ep.read((uint32_t)(0),data_r,(uint32_t)max_size);
fmanzano_dtk 1:9e821e640117 246 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 247 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 248
fmanzano_dtk 1:9e821e640117 249 printf("\nTest write and read an array of the first %d bytes :\n",max_size);
fmanzano_dtk 1:9e821e640117 250 for(i = 0;i < max_size/16;i++) {
fmanzano_dtk 1:9e821e640117 251 for(j = 0;j < 16;j++) {
fmanzano_dtk 1:9e821e640117 252 addr = i * 16 + j;
fmanzano_dtk 1:9e821e640117 253 printf("%3d ",(uint8_t)data_r[addr]);
fmanzano_dtk 1:9e821e640117 254 }
fmanzano_dtk 1:9e821e640117 255 printf("\n");
fmanzano_dtk 1:9e821e640117 256 }
fmanzano_dtk 1:9e821e640117 257 printf("\n");
fmanzano_dtk 1:9e821e640117 258
fmanzano_dtk 1:9e821e640117 259 // Test write and read an array of int32
fmanzano_dtk 1:9e821e640117 260 s = eeprom_size / 4; // size of eeprom in int32
fmanzano_dtk 1:9e821e640117 261 int ldata_size = sizeof(ldata) / 4; // size of data array in int32
fmanzano_dtk 1:9e821e640117 262 l = s / ldata_size; // loop index
fmanzano_dtk 1:9e821e640117 263
fmanzano_dtk 1:9e821e640117 264 // size of read / write in bytes
fmanzano_dtk 1:9e821e640117 265 t = eeprom_size;
fmanzano_dtk 1:9e821e640117 266 if(t > ldata_size * 4)
fmanzano_dtk 1:9e821e640117 267 t = ldata_size * 4;
fmanzano_dtk 1:9e821e640117 268
fmanzano_dtk 1:9e821e640117 269 printf("Test write and read an array of %d int32 (write entire memory) :\n",t/4);
fmanzano_dtk 1:9e821e640117 270
fmanzano_dtk 1:9e821e640117 271 // Write entire eeprom
fmanzano_dtk 1:9e821e640117 272 if(l) {
fmanzano_dtk 1:9e821e640117 273 for(k = 0;k < l;k++) {
fmanzano_dtk 1:9e821e640117 274 for(i = 0;i < ldata_size;i++)
fmanzano_dtk 1:9e821e640117 275 ldata[i] = ldata_size * k + i;
fmanzano_dtk 1:9e821e640117 276
fmanzano_dtk 1:9e821e640117 277 addr = k * ldata_size * 4;
fmanzano_dtk 1:9e821e640117 278 ep.write(addr,(void *)ldata,t);
fmanzano_dtk 1:9e821e640117 279 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 280 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 281 }
fmanzano_dtk 1:9e821e640117 282
fmanzano_dtk 1:9e821e640117 283 printf("Write OK\n");
fmanzano_dtk 1:9e821e640117 284
fmanzano_dtk 1:9e821e640117 285 // Read entire eeprom
fmanzano_dtk 1:9e821e640117 286 id = 0;
fmanzano_dtk 1:9e821e640117 287 for(k = 0;k < l;k++) {
fmanzano_dtk 1:9e821e640117 288 addr = k * ldata_size * 4;
fmanzano_dtk 1:9e821e640117 289 ep.read(addr,(void *)ldata,t);
fmanzano_dtk 1:9e821e640117 290 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 291 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 292
fmanzano_dtk 1:9e821e640117 293 // format outputs with 8 words rows
fmanzano_dtk 1:9e821e640117 294 for(i = 0;i < ldata_size / 8;i++) {
fmanzano_dtk 1:9e821e640117 295 id++;
fmanzano_dtk 1:9e821e640117 296 printf("%4d ",id);
fmanzano_dtk 1:9e821e640117 297 for(j = 0;j < 8;j++) {
fmanzano_dtk 1:9e821e640117 298 addr = i * 8 + j;
fmanzano_dtk 1:9e821e640117 299 printf("%5d ",ldata[addr]);
fmanzano_dtk 1:9e821e640117 300 }
fmanzano_dtk 1:9e821e640117 301 printf("\n");
fmanzano_dtk 1:9e821e640117 302 }
fmanzano_dtk 1:9e821e640117 303 }
fmanzano_dtk 1:9e821e640117 304 }
fmanzano_dtk 1:9e821e640117 305 else {
fmanzano_dtk 1:9e821e640117 306 for(i = 0;i < s;i++)
fmanzano_dtk 1:9e821e640117 307 ldata[i] = i;
fmanzano_dtk 1:9e821e640117 308
fmanzano_dtk 1:9e821e640117 309 addr = 0;
fmanzano_dtk 1:9e821e640117 310 ep.write(addr,(void *)ldata,t);
fmanzano_dtk 1:9e821e640117 311 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 312 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 313
fmanzano_dtk 1:9e821e640117 314 printf("Write OK\n");
fmanzano_dtk 1:9e821e640117 315
fmanzano_dtk 1:9e821e640117 316 // Read entire eeprom
fmanzano_dtk 1:9e821e640117 317 id = 0;
fmanzano_dtk 1:9e821e640117 318
fmanzano_dtk 1:9e821e640117 319 addr = 0;
fmanzano_dtk 1:9e821e640117 320 ep.read(addr,(void *)ldata,t);
fmanzano_dtk 1:9e821e640117 321 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 322 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 323
fmanzano_dtk 1:9e821e640117 324 // format outputs with 8 words rows
fmanzano_dtk 1:9e821e640117 325 for(i = 0;i < s / 8;i++) {
fmanzano_dtk 1:9e821e640117 326 id++;
fmanzano_dtk 1:9e821e640117 327 printf("%4d ",id);
fmanzano_dtk 1:9e821e640117 328 for(j = 0;j < 8;j++) {
fmanzano_dtk 1:9e821e640117 329 addr = i * 8 + j;
fmanzano_dtk 1:9e821e640117 330 printf("%5d ",ldata[addr]);
fmanzano_dtk 1:9e821e640117 331 }
fmanzano_dtk 1:9e821e640117 332 printf("\n");
fmanzano_dtk 1:9e821e640117 333 }
fmanzano_dtk 1:9e821e640117 334 }
fmanzano_dtk 1:9e821e640117 335
fmanzano_dtk 1:9e821e640117 336 // clear eeprom
fmanzano_dtk 1:9e821e640117 337 printf("\nClear eeprom\n");
fmanzano_dtk 1:9e821e640117 338
fmanzano_dtk 1:9e821e640117 339 ep.clear();
fmanzano_dtk 1:9e821e640117 340 if(ep.getError() != 0)
fmanzano_dtk 1:9e821e640117 341 myerror(ep.getErrorMessage());
fmanzano_dtk 1:9e821e640117 342
fmanzano_dtk 1:9e821e640117 343 printf("End\n");
fmanzano_dtk 1:9e821e640117 344
fmanzano_dtk 1:9e821e640117 345 }
fmanzano_dtk 1:9e821e640117 346
fmanzano_dtk 1:9e821e640117 347 int main()
fmanzano_dtk 1:9e821e640117 348 {
fmanzano_dtk 1:9e821e640117 349
fmanzano_dtk 1:9e821e640117 350 eeprom_test();
fmanzano_dtk 1:9e821e640117 351
fmanzano_dtk 1:9e821e640117 352 return(0);
fmanzano_dtk 1:9e821e640117 353 }
fmanzano_dtk 1:9e821e640117 354 */
fmanzano_dtk 1:9e821e640117 355
fmanzano_dtk 1:9e821e640117 356 // Defines
fmanzano_dtk 1:9e821e640117 357 #define EEPROM_Address 0xa0
fmanzano_dtk 1:9e821e640117 358 #define EEPROM_NoError 0x00
fmanzano_dtk 1:9e821e640117 359 #define EEPROM_BadAddress 0x01
fmanzano_dtk 1:9e821e640117 360 #define EEPROM_I2cError 0x02
fmanzano_dtk 1:9e821e640117 361 #define EEPROM_ParamError 0x03
fmanzano_dtk 1:9e821e640117 362 #define EEPROM_OutOfRange 0x04
fmanzano_dtk 1:9e821e640117 363 #define EEPROM_MallocError 0x05
fmanzano_dtk 1:9e821e640117 364
fmanzano_dtk 1:9e821e640117 365 #define EEPROM_MaxError 6
fmanzano_dtk 1:9e821e640117 366
fmanzano_dtk 1:9e821e640117 367 static std::string _ErrorMessageEEPROM[EEPROM_MaxError] = {
fmanzano_dtk 1:9e821e640117 368 "",
fmanzano_dtk 1:9e821e640117 369 "Bad chip address",
fmanzano_dtk 1:9e821e640117 370 "I2C error (nack)",
fmanzano_dtk 1:9e821e640117 371 "Invalid parameter",
fmanzano_dtk 1:9e821e640117 372 "Data address out of range",
fmanzano_dtk 1:9e821e640117 373 "Memory allocation error"
fmanzano_dtk 1:9e821e640117 374 };
fmanzano_dtk 1:9e821e640117 375
fmanzano_dtk 1:9e821e640117 376 /** EEPROM Class
fmanzano_dtk 1:9e821e640117 377 */
fmanzano_dtk 1:9e821e640117 378 class EEPROM {
fmanzano_dtk 1:9e821e640117 379 public:
fmanzano_dtk 1:9e821e640117 380 enum TypeEeprom {T24C01=128,T24C02=256,T24C04=512,T24C08=1024,T24C16=2048,
fmanzano_dtk 1:9e821e640117 381 T24C32=4096,T24C64=8192,T24C128=16384,T24C256=32768,
fmanzano_dtk 1:9e821e640117 382 T24C512=65536,T24C1024=131072,T24C1025=131073} Type;
fmanzano_dtk 1:9e821e640117 383
fmanzano_dtk 1:9e821e640117 384 /**
fmanzano_dtk 1:9e821e640117 385 * Constructor, initialize the eeprom on i2c interface.
fmanzano_dtk 1:9e821e640117 386 * @param sda sda i2c pin (PinName)
fmanzano_dtk 1:9e821e640117 387 * @param scl scl i2c pin (PinName)
fmanzano_dtk 1:9e821e640117 388 * @param address eeprom address, according to eeprom type (uint8_t)
fmanzano_dtk 1:9e821e640117 389 * @param type eeprom type (TypeEeprom)
fmanzano_dtk 1:9e821e640117 390 * @return none
fmanzano_dtk 1:9e821e640117 391 */
fmanzano_dtk 1:9e821e640117 392 EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type);
fmanzano_dtk 1:9e821e640117 393
fmanzano_dtk 1:9e821e640117 394 /**
fmanzano_dtk 1:9e821e640117 395 * Random read byte
fmanzano_dtk 1:9e821e640117 396 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 397 * @param data byte to read (int8_t&)
fmanzano_dtk 1:9e821e640117 398 * @return none
fmanzano_dtk 1:9e821e640117 399 */
fmanzano_dtk 1:9e821e640117 400 void read(uint32_t address, int8_t& data);
fmanzano_dtk 1:9e821e640117 401
fmanzano_dtk 1:9e821e640117 402 /**
fmanzano_dtk 1:9e821e640117 403 * Random read short
fmanzano_dtk 1:9e821e640117 404 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 405 * @param data short to read (int16_t&)
fmanzano_dtk 1:9e821e640117 406 * @return none
fmanzano_dtk 1:9e821e640117 407 */
fmanzano_dtk 1:9e821e640117 408 void read(uint32_t address, int16_t& data);
fmanzano_dtk 1:9e821e640117 409
fmanzano_dtk 1:9e821e640117 410 /**
fmanzano_dtk 1:9e821e640117 411 * Random read long
fmanzano_dtk 1:9e821e640117 412 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 413 * @param data long to read (int32_t&)
fmanzano_dtk 1:9e821e640117 414 * @return none
fmanzano_dtk 1:9e821e640117 415 */
fmanzano_dtk 1:9e821e640117 416 void read(uint32_t address, int32_t& data);
fmanzano_dtk 1:9e821e640117 417
fmanzano_dtk 1:9e821e640117 418 /**
fmanzano_dtk 1:9e821e640117 419 * Random read float
fmanzano_dtk 1:9e821e640117 420 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 421 * @param data float to read (float&)
fmanzano_dtk 1:9e821e640117 422 * @return none
fmanzano_dtk 1:9e821e640117 423 */
fmanzano_dtk 1:9e821e640117 424 void read(uint32_t address, float& data);
fmanzano_dtk 1:9e821e640117 425
fmanzano_dtk 1:9e821e640117 426 /**
fmanzano_dtk 1:9e821e640117 427 * Random read anything
fmanzano_dtk 1:9e821e640117 428 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 429 * @param data data to read (void *)
fmanzano_dtk 1:9e821e640117 430 * @param size number of bytes to read (uint32_t)
fmanzano_dtk 1:9e821e640117 431 * @return none
fmanzano_dtk 1:9e821e640117 432 */
fmanzano_dtk 1:9e821e640117 433 void read(uint32_t address, void *data, uint32_t size);
fmanzano_dtk 1:9e821e640117 434
fmanzano_dtk 1:9e821e640117 435 /**
fmanzano_dtk 1:9e821e640117 436 * Current address read byte
fmanzano_dtk 1:9e821e640117 437 * @param data byte to read (int8_t&)
fmanzano_dtk 1:9e821e640117 438 * @return none
fmanzano_dtk 1:9e821e640117 439 */
fmanzano_dtk 1:9e821e640117 440 void read(int8_t& data);
fmanzano_dtk 1:9e821e640117 441
fmanzano_dtk 1:9e821e640117 442 /**
fmanzano_dtk 1:9e821e640117 443 * Sequential read byte
fmanzano_dtk 1:9e821e640117 444 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 445 * @param data bytes array to read (int8_t[]&)
fmanzano_dtk 1:9e821e640117 446 * @param size number of bytes to read (uint32_t)
fmanzano_dtk 1:9e821e640117 447 * @return none
fmanzano_dtk 1:9e821e640117 448 */
fmanzano_dtk 1:9e821e640117 449 void read(uint32_t address, int8_t *data, uint32_t size);
fmanzano_dtk 1:9e821e640117 450
fmanzano_dtk 1:9e821e640117 451 /**
fmanzano_dtk 1:9e821e640117 452 * Write byte
fmanzano_dtk 1:9e821e640117 453 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 454 * @param data byte to write (int8_t)
fmanzano_dtk 1:9e821e640117 455 * @return none
fmanzano_dtk 1:9e821e640117 456 */
fmanzano_dtk 1:9e821e640117 457 void write(uint32_t address, int8_t data);
fmanzano_dtk 1:9e821e640117 458
fmanzano_dtk 1:9e821e640117 459 /**
fmanzano_dtk 1:9e821e640117 460 * Write short
fmanzano_dtk 1:9e821e640117 461 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 462 * @param data short to write (int16_t)
fmanzano_dtk 1:9e821e640117 463 * @return none
fmanzano_dtk 1:9e821e640117 464 */
fmanzano_dtk 1:9e821e640117 465 void write(uint32_t address, int16_t data);
fmanzano_dtk 1:9e821e640117 466
fmanzano_dtk 1:9e821e640117 467 /**
fmanzano_dtk 1:9e821e640117 468 * Write long
fmanzano_dtk 1:9e821e640117 469 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 470 * @param data long to write (int32_t)
fmanzano_dtk 1:9e821e640117 471 * @return none
fmanzano_dtk 1:9e821e640117 472 */
fmanzano_dtk 1:9e821e640117 473 void write(uint32_t address, int32_t data);
fmanzano_dtk 1:9e821e640117 474
fmanzano_dtk 1:9e821e640117 475 /**
fmanzano_dtk 1:9e821e640117 476 * Write float
fmanzano_dtk 1:9e821e640117 477 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 478 * @param data float to write (float)
fmanzano_dtk 1:9e821e640117 479 * @return none
fmanzano_dtk 1:9e821e640117 480 */
fmanzano_dtk 1:9e821e640117 481 void write(uint32_t address, float data);
fmanzano_dtk 1:9e821e640117 482
fmanzano_dtk 1:9e821e640117 483 /**
fmanzano_dtk 1:9e821e640117 484 * Write anything (use the page write mode)
fmanzano_dtk 1:9e821e640117 485 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 486 * @param data data to write (void *)
fmanzano_dtk 1:9e821e640117 487 * @param size number of bytes to write (uint32_t)
fmanzano_dtk 1:9e821e640117 488 * @return none
fmanzano_dtk 1:9e821e640117 489 */
fmanzano_dtk 1:9e821e640117 490 void write(uint32_t address, void *data, uint32_t size);
fmanzano_dtk 1:9e821e640117 491
fmanzano_dtk 1:9e821e640117 492 /**
fmanzano_dtk 1:9e821e640117 493 * Write array of bytes (use the page mode)
fmanzano_dtk 1:9e821e640117 494 * @param address start address (uint32_t)
fmanzano_dtk 1:9e821e640117 495 * @param data bytes array to write (int8_t[])
fmanzano_dtk 1:9e821e640117 496 * @param size number of bytes to write (uint32_t)
fmanzano_dtk 1:9e821e640117 497 * @return none
fmanzano_dtk 1:9e821e640117 498 */
fmanzano_dtk 1:9e821e640117 499 void write(uint32_t address, int8_t data[], uint32_t size);
fmanzano_dtk 1:9e821e640117 500
fmanzano_dtk 1:9e821e640117 501 /**
fmanzano_dtk 1:9e821e640117 502 * Wait eeprom ready
fmanzano_dtk 1:9e821e640117 503 * @param none
fmanzano_dtk 1:9e821e640117 504 * @return none
fmanzano_dtk 1:9e821e640117 505 */
fmanzano_dtk 1:9e821e640117 506 void ready(void);
fmanzano_dtk 1:9e821e640117 507
fmanzano_dtk 1:9e821e640117 508 /**
fmanzano_dtk 1:9e821e640117 509 * Get eeprom size in bytes
fmanzano_dtk 1:9e821e640117 510 * @param none
fmanzano_dtk 1:9e821e640117 511 * @return size in bytes (uint32_t)
fmanzano_dtk 1:9e821e640117 512 */
fmanzano_dtk 1:9e821e640117 513 uint32_t getSize(void);
fmanzano_dtk 1:9e821e640117 514
fmanzano_dtk 1:9e821e640117 515 /**
fmanzano_dtk 1:9e821e640117 516 * Get eeprom name
fmanzano_dtk 1:9e821e640117 517 * @param none
fmanzano_dtk 1:9e821e640117 518 * @return name (const char*)
fmanzano_dtk 1:9e821e640117 519 */
fmanzano_dtk 1:9e821e640117 520 const char* getName(void);
fmanzano_dtk 1:9e821e640117 521
fmanzano_dtk 1:9e821e640117 522 /**
fmanzano_dtk 1:9e821e640117 523 * Clear eeprom (write with 0)
fmanzano_dtk 1:9e821e640117 524 * @param none
fmanzano_dtk 1:9e821e640117 525 * @return none
fmanzano_dtk 1:9e821e640117 526 */
fmanzano_dtk 1:9e821e640117 527 void clear(void);
fmanzano_dtk 1:9e821e640117 528
fmanzano_dtk 1:9e821e640117 529 /**
fmanzano_dtk 1:9e821e640117 530 * Get the current error number (EEPROM_NoError if no error)
fmanzano_dtk 1:9e821e640117 531 * @param none
fmanzano_dtk 1:9e821e640117 532 * @return none
fmanzano_dtk 1:9e821e640117 533 */
fmanzano_dtk 1:9e821e640117 534 uint8_t getError(void);
fmanzano_dtk 1:9e821e640117 535
fmanzano_dtk 1:9e821e640117 536 /**
fmanzano_dtk 1:9e821e640117 537 * Get current error message
fmanzano_dtk 1:9e821e640117 538 * @param none
fmanzano_dtk 1:9e821e640117 539 * @return current error message(std::string)
fmanzano_dtk 1:9e821e640117 540 */
fmanzano_dtk 1:9e821e640117 541 std::string getErrorMessage(void)
fmanzano_dtk 1:9e821e640117 542 {
fmanzano_dtk 1:9e821e640117 543 return(_ErrorMessageEEPROM[_errnum]);
fmanzano_dtk 1:9e821e640117 544 }
fmanzano_dtk 1:9e821e640117 545
fmanzano_dtk 1:9e821e640117 546 //---------- local variables ----------
fmanzano_dtk 1:9e821e640117 547 private:
fmanzano_dtk 1:9e821e640117 548 I2C _i2c; // Local i2c communication interface instance
fmanzano_dtk 1:9e821e640117 549 int _address; // Local i2c address
fmanzano_dtk 1:9e821e640117 550 uint8_t _errnum; // Error number
fmanzano_dtk 1:9e821e640117 551 TypeEeprom _type; // EEPROM type
fmanzano_dtk 1:9e821e640117 552 uint8_t _page_write; // Page write size
fmanzano_dtk 1:9e821e640117 553 uint8_t _page_number; // Number of page
fmanzano_dtk 1:9e821e640117 554 uint32_t _size; // Size in bytes
fmanzano_dtk 1:9e821e640117 555 bool checkAddress(uint32_t address); // Check address range
fmanzano_dtk 1:9e821e640117 556 static const char * const _name[]; // eeprom name
fmanzano_dtk 1:9e821e640117 557 //-------------------------------------
fmanzano_dtk 1:9e821e640117 558 };
fmanzano_dtk 1:9e821e640117 559 #endif