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