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