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