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