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