se quito el led de debug de la bibilioteca

Fork of eeprom_1 by JONIX SA

Committer:
juanc95
Date:
Thu Oct 04 18:27:30 2018 +0000
Revision:
6:7d676c26da40
Parent:
5:266d466bd7b2
Child:
7:cf6096a818b7
debugueo de eeprom para el consumo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bborredon 0:80245aff63ce 1 /***********************************************************
bborredon 0:80245aff63ce 2 Author: Bernard Borredon
bborredon 3:925096a4c7f0 3 Version: 1.3
bborredon 3:925096a4c7f0 4 - Correct write(uint32_t address, int8_t data[], uint32_t length) for eeprom >= T24C32.
bborredon 3:925096a4c7f0 5 Tested with 24C02, 24C08, 24C16, 24C64, 24C256, 24C512, 24C1025 on LPC1768 (mbed online and µVision V5.16a).
bborredon 3:925096a4c7f0 6 - Correct main test.
bborredon 3:925096a4c7f0 7
bborredon 2:79ed7ff7c23d 8 Date : 12 decembre 2013
bborredon 2:79ed7ff7c23d 9 Version: 1.2
bborredon 2:79ed7ff7c23d 10 - Update api documentation
bborredon 2:79ed7ff7c23d 11
bborredon 1:a262173cac81 12 Date: 11 december 2013
bborredon 1:a262173cac81 13 Version: 1.1
bborredon 1:a262173cac81 14 - Change address parameter size form uint16_t to uint32_t (error for eeprom > 24C256).
bborredon 1:a262173cac81 15 - Change size parameter size from uint16_t to uint32_t (error for eeprom > 24C256).
bborredon 1:a262173cac81 16 - Correct a bug in function write(uint32_t address, int8_t data[], uint32_t length) :
bborredon 1:a262173cac81 17 last step must be done only if it remain datas to send.
bborredon 1:a262173cac81 18 - Add function getName.
bborredon 1:a262173cac81 19 - Add function clear.
bborredon 1:a262173cac81 20 - Initialize _name array.
bborredon 1:a262173cac81 21
bborredon 0:80245aff63ce 22 Date: 27 december 2011
bborredon 0:80245aff63ce 23 Version: 1.0
bborredon 1:a262173cac81 24
bborredon 0:80245aff63ce 25 ************************************************************/
sanru95 5:266d466bd7b2 26 #include "eeprom_lib.h"
bborredon 0:80245aff63ce 27
bborredon 0:80245aff63ce 28 #define BIT_SET(x,n) (x=x | (0x01<<n))
bborredon 0:80245aff63ce 29 #define BIT_TEST(x,n) (x & (0x01<<n))
bborredon 0:80245aff63ce 30 #define BIT_CLEAR(x,n) (x=x & ~(0x01<<n))
bborredon 0:80245aff63ce 31
bborredon 1:a262173cac81 32 const char * const EEPROM::_name[] = {"24C01","24C02","24C04","24C08","24C16","24C32",
bborredon 1:a262173cac81 33 "24C64","24C128","24C256","24C512","24C1024","24C1025"};
bborredon 1:a262173cac81 34
bborredon 3:925096a4c7f0 35 /**
bborredon 3:925096a4c7f0 36 * EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type) : _i2c(sda, scl)
bborredon 3:925096a4c7f0 37 *
bborredon 3:925096a4c7f0 38 * Constructor, initialize the eeprom on i2c interface.
bborredon 3:925096a4c7f0 39 * @param sda sda i2c pin (PinName)
bborredon 3:925096a4c7f0 40 * @param scl scl i2c pin (PinName)
bborredon 3:925096a4c7f0 41 * @param address eeprom address, according to eeprom type (uint8_t)
bborredon 3:925096a4c7f0 42 * @param type eeprom type (TypeEeprom)
bborredon 3:925096a4c7f0 43 * @return none
bborredon 3:925096a4c7f0 44 */
bborredon 0:80245aff63ce 45 EEPROM::EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type) : _i2c(sda, scl)
bborredon 0:80245aff63ce 46 {
bborredon 0:80245aff63ce 47
bborredon 0:80245aff63ce 48 _errnum = EEPROM_NoError;
bborredon 0:80245aff63ce 49 _type = type;
bborredon 0:80245aff63ce 50
bborredon 0:80245aff63ce 51 // Check address range
bborredon 0:80245aff63ce 52 _address = address;
bborredon 0:80245aff63ce 53 switch(type) {
bborredon 0:80245aff63ce 54 case T24C01 :
bborredon 0:80245aff63ce 55 case T24C02 :
bborredon 0:80245aff63ce 56 if(address > 7) {
bborredon 0:80245aff63ce 57 _errnum = EEPROM_BadAddress;
bborredon 0:80245aff63ce 58 }
bborredon 0:80245aff63ce 59 _address = _address << 1;
bborredon 0:80245aff63ce 60 _page_write = 8;
bborredon 0:80245aff63ce 61 _page_number = 1;
bborredon 0:80245aff63ce 62 break;
bborredon 0:80245aff63ce 63 case T24C04 :
bborredon 0:80245aff63ce 64 if(address > 7) {
bborredon 0:80245aff63ce 65 _errnum = EEPROM_BadAddress;
bborredon 0:80245aff63ce 66 }
bborredon 0:80245aff63ce 67 _address = (_address & 0xFE) << 1;
bborredon 0:80245aff63ce 68 _page_write = 16;
bborredon 0:80245aff63ce 69 _page_number = 2;
bborredon 0:80245aff63ce 70 break;
bborredon 0:80245aff63ce 71 case T24C08 :
bborredon 0:80245aff63ce 72 if(address > 7) {
bborredon 0:80245aff63ce 73 _errnum = EEPROM_BadAddress;
bborredon 0:80245aff63ce 74 }
bborredon 0:80245aff63ce 75 _address = (_address & 0xFC) << 1;
bborredon 0:80245aff63ce 76 _page_write = 16;
bborredon 0:80245aff63ce 77 _page_number = 4;
bborredon 0:80245aff63ce 78 break;
bborredon 0:80245aff63ce 79 case T24C16 :
bborredon 0:80245aff63ce 80 _address = 0;
bborredon 0:80245aff63ce 81 _page_write = 16;
bborredon 0:80245aff63ce 82 _page_number = 8;
bborredon 0:80245aff63ce 83 break;
bborredon 0:80245aff63ce 84 case T24C32 :
bborredon 0:80245aff63ce 85 case T24C64 :
bborredon 0:80245aff63ce 86 if(address > 7) {
bborredon 0:80245aff63ce 87 _errnum = EEPROM_BadAddress;
bborredon 0:80245aff63ce 88 }
bborredon 0:80245aff63ce 89 _address = _address << 1;
bborredon 0:80245aff63ce 90 _page_write = 32;
bborredon 0:80245aff63ce 91 _page_number = 1;
bborredon 0:80245aff63ce 92 break;
bborredon 0:80245aff63ce 93 case T24C128 :
bborredon 0:80245aff63ce 94 case T24C256 :
bborredon 0:80245aff63ce 95 if(address > 3) {
bborredon 0:80245aff63ce 96 _errnum = EEPROM_BadAddress;
bborredon 0:80245aff63ce 97 }
bborredon 0:80245aff63ce 98 _address = _address << 1;
bborredon 0:80245aff63ce 99 _page_write = 64;
bborredon 0:80245aff63ce 100 _page_number = 1;
bborredon 0:80245aff63ce 101 break;
bborredon 0:80245aff63ce 102 case T24C512 :
bborredon 0:80245aff63ce 103 if(address > 3) {
bborredon 0:80245aff63ce 104 _errnum = EEPROM_BadAddress;
bborredon 0:80245aff63ce 105 }
bborredon 0:80245aff63ce 106 _address = _address << 1;
bborredon 0:80245aff63ce 107 _page_write = 128;
bborredon 0:80245aff63ce 108 _page_number = 1;
bborredon 0:80245aff63ce 109 break;
bborredon 0:80245aff63ce 110 case T24C1024 :
bborredon 0:80245aff63ce 111 if(address > 7) {
bborredon 0:80245aff63ce 112 _errnum = EEPROM_BadAddress;
bborredon 0:80245aff63ce 113 }
bborredon 0:80245aff63ce 114 _address = (_address & 0xFE) << 1;
bborredon 0:80245aff63ce 115 _page_write = 128;
bborredon 0:80245aff63ce 116 _page_number = 2;
bborredon 0:80245aff63ce 117 break;
bborredon 0:80245aff63ce 118 case T24C1025 :
bborredon 0:80245aff63ce 119 if(address > 3) {
bborredon 0:80245aff63ce 120 _errnum = EEPROM_BadAddress;
bborredon 0:80245aff63ce 121 }
bborredon 0:80245aff63ce 122 _address = _address << 1;
bborredon 0:80245aff63ce 123 _page_write = 128;
bborredon 0:80245aff63ce 124 _page_number = 2;
bborredon 0:80245aff63ce 125 break;
bborredon 0:80245aff63ce 126 }
bborredon 0:80245aff63ce 127
bborredon 0:80245aff63ce 128 // Size in bytes
bborredon 0:80245aff63ce 129 _size = _type;
bborredon 0:80245aff63ce 130 if(_type == T24C1025)
bborredon 0:80245aff63ce 131 _size = T24C1024;
bborredon 0:80245aff63ce 132
bborredon 0:80245aff63ce 133 // Set I2C frequency
bborredon 0:80245aff63ce 134 _i2c.frequency(400000);
bborredon 3:925096a4c7f0 135
bborredon 0:80245aff63ce 136 }
bborredon 0:80245aff63ce 137
bborredon 3:925096a4c7f0 138 /**
bborredon 3:925096a4c7f0 139 * void write(uint32_t address, int8_t data)
bborredon 3:925096a4c7f0 140 *
bborredon 3:925096a4c7f0 141 * Write byte
bborredon 3:925096a4c7f0 142 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 143 * @param data byte to write (int8_t)
bborredon 3:925096a4c7f0 144 * @return none
bborredon 3:925096a4c7f0 145 */
bborredon 1:a262173cac81 146 void EEPROM::write(uint32_t address, int8_t data)
bborredon 0:80245aff63ce 147 {
bborredon 0:80245aff63ce 148 uint8_t page;
bborredon 0:80245aff63ce 149 uint8_t addr;
bborredon 0:80245aff63ce 150 uint8_t cmd[3];
bborredon 0:80245aff63ce 151 int len;
bborredon 0:80245aff63ce 152 int ack;
bborredon 3:925096a4c7f0 153
bborredon 0:80245aff63ce 154 // Check error
bborredon 0:80245aff63ce 155 if(_errnum)
bborredon 0:80245aff63ce 156 return;
bborredon 0:80245aff63ce 157
bborredon 0:80245aff63ce 158 // Check address
bborredon 0:80245aff63ce 159 if(!checkAddress(address)) {
bborredon 0:80245aff63ce 160 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 161 return;
bborredon 0:80245aff63ce 162 }
bborredon 0:80245aff63ce 163
bborredon 0:80245aff63ce 164 // Compute page number
bborredon 0:80245aff63ce 165 page = 0;
bborredon 0:80245aff63ce 166 if(_type < T24C32)
bborredon 0:80245aff63ce 167 page = (uint8_t) (address / 256);
bborredon 0:80245aff63ce 168
bborredon 0:80245aff63ce 169 // Device address
bborredon 0:80245aff63ce 170 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 171
bborredon 0:80245aff63ce 172 if(_type < T24C32) {
bborredon 0:80245aff63ce 173 len = 2;
bborredon 0:80245aff63ce 174
bborredon 0:80245aff63ce 175 // Word address
bborredon 0:80245aff63ce 176 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 177
bborredon 0:80245aff63ce 178 // Data
bborredon 0:80245aff63ce 179 cmd[1] = (uint8_t) data;
bborredon 0:80245aff63ce 180 }
bborredon 0:80245aff63ce 181 else {
bborredon 0:80245aff63ce 182 len = 3;
bborredon 0:80245aff63ce 183
bborredon 0:80245aff63ce 184 // First word address (MSB)
bborredon 0:80245aff63ce 185 cmd[0] = (uint8_t) (address >> 8);
bborredon 0:80245aff63ce 186
bborredon 0:80245aff63ce 187 // Second word address (LSB)
bborredon 0:80245aff63ce 188 cmd[1] = (uint8_t) address;
bborredon 0:80245aff63ce 189
bborredon 0:80245aff63ce 190 // Data
bborredon 0:80245aff63ce 191 cmd[2] = (uint8_t) data;
bborredon 0:80245aff63ce 192 }
bborredon 3:925096a4c7f0 193
bborredon 0:80245aff63ce 194 ack = _i2c.write((int)addr,(char *)cmd,len);
bborredon 0:80245aff63ce 195 if(ack != 0) {
bborredon 0:80245aff63ce 196 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 197 return;
bborredon 0:80245aff63ce 198 }
bborredon 0:80245aff63ce 199
bborredon 0:80245aff63ce 200 // Wait end of write
bborredon 0:80245aff63ce 201 ready();
bborredon 0:80245aff63ce 202
bborredon 0:80245aff63ce 203 }
bborredon 0:80245aff63ce 204
bborredon 3:925096a4c7f0 205 /**
bborredon 3:925096a4c7f0 206 * void write(uint32_t address, int8_t data[], uint32_t length)
bborredon 3:925096a4c7f0 207 *
bborredon 3:925096a4c7f0 208 * Write array of bytes (use the page mode)
bborredon 3:925096a4c7f0 209 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 210 * @param data bytes array to write (int8_t[])
bborredon 3:925096a4c7f0 211 * @param size number of bytes to write (uint32_t)
bborredon 3:925096a4c7f0 212 * @return none
bborredon 3:925096a4c7f0 213 */
juanc95 6:7d676c26da40 214 #include "main.h"
juanc95 6:7d676c26da40 215 extern DigitalOut MasterLed;
juanc95 6:7d676c26da40 216
bborredon 1:a262173cac81 217 void EEPROM::write(uint32_t address, int8_t data[], uint32_t length)
bborredon 0:80245aff63ce 218 {
bborredon 0:80245aff63ce 219 uint8_t page;
bborredon 1:a262173cac81 220 uint8_t addr = 0;
bborredon 0:80245aff63ce 221 uint8_t blocs,remain;
bborredon 0:80245aff63ce 222 uint8_t fpart,lpart;
bborredon 0:80245aff63ce 223 uint8_t i,j,ind;
bborredon 0:80245aff63ce 224 uint8_t cmd[129];
bborredon 0:80245aff63ce 225 int ack;
juanc95 6:7d676c26da40 226
bborredon 0:80245aff63ce 227 // Check error
bborredon 0:80245aff63ce 228 if(_errnum)
bborredon 0:80245aff63ce 229 return;
bborredon 0:80245aff63ce 230
bborredon 0:80245aff63ce 231 // Check address
bborredon 0:80245aff63ce 232 if(!checkAddress(address)) {
bborredon 0:80245aff63ce 233 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 234 return;
bborredon 0:80245aff63ce 235 }
bborredon 0:80245aff63ce 236
bborredon 0:80245aff63ce 237 // Check length
bborredon 0:80245aff63ce 238 if(!checkAddress(address + length - 1)) {
bborredon 0:80245aff63ce 239 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 240 return;
bborredon 0:80245aff63ce 241 }
juanc95 6:7d676c26da40 242
juanc95 6:7d676c26da40 243
bborredon 0:80245aff63ce 244
bborredon 0:80245aff63ce 245 // Compute blocs numbers
bborredon 0:80245aff63ce 246 blocs = length / _page_write;
bborredon 0:80245aff63ce 247
bborredon 0:80245aff63ce 248 // Compute remaining bytes
bborredon 0:80245aff63ce 249 remain = length - blocs * _page_write;
bborredon 3:925096a4c7f0 250
bborredon 0:80245aff63ce 251 for(i = 0;i < blocs;i++) {
bborredon 0:80245aff63ce 252 // Compute page number
bborredon 0:80245aff63ce 253 page = 0;
bborredon 0:80245aff63ce 254 if(_type < T24C32)
bborredon 0:80245aff63ce 255 page = (uint8_t) (address / 256);
bborredon 0:80245aff63ce 256
bborredon 0:80245aff63ce 257 // Device address
bborredon 0:80245aff63ce 258 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 259
bborredon 0:80245aff63ce 260 if(_type < T24C32) {
bborredon 0:80245aff63ce 261 // Word address
bborredon 0:80245aff63ce 262 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 263
bborredon 0:80245aff63ce 264 if((uint8_t) ((address + _page_write) / 256) == page) { // Data fit in the same page
bborredon 0:80245aff63ce 265 // Add data
bborredon 0:80245aff63ce 266 for(j = 0;j < _page_write;j++)
bborredon 0:80245aff63ce 267 cmd[j + 1] = (uint8_t) data[i * _page_write + j];
bborredon 0:80245aff63ce 268
bborredon 0:80245aff63ce 269 // Write data
bborredon 0:80245aff63ce 270 ack = _i2c.write((int)addr,(char *)cmd,_page_write + 1);
bborredon 0:80245aff63ce 271 if(ack != 0) {
bborredon 0:80245aff63ce 272 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 273 return;
bborredon 0:80245aff63ce 274 }
bborredon 0:80245aff63ce 275
bborredon 0:80245aff63ce 276 // Wait end of write
bborredon 0:80245aff63ce 277 ready();
bborredon 0:80245aff63ce 278
bborredon 0:80245aff63ce 279 // Increment address
bborredon 0:80245aff63ce 280 address += _page_write;
bborredon 0:80245aff63ce 281 }
bborredon 0:80245aff63ce 282 else { // Data on 2 pages. We must split the write
bborredon 0:80245aff63ce 283 // Number of bytes in current page
bborredon 0:80245aff63ce 284 fpart = (page + 1) * 256 - address;
bborredon 0:80245aff63ce 285
bborredon 0:80245aff63ce 286 // Add data for current page
bborredon 0:80245aff63ce 287 for(j = 0;j < fpart;j++)
bborredon 0:80245aff63ce 288 cmd[j + 1] = (uint8_t) data[i * _page_write + j];
bborredon 0:80245aff63ce 289
bborredon 0:80245aff63ce 290 // Write data for current page
bborredon 0:80245aff63ce 291 ack = _i2c.write((int)addr,(char *)cmd,fpart + 1);
bborredon 0:80245aff63ce 292 if(ack != 0) {
bborredon 0:80245aff63ce 293 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 294 return;
bborredon 0:80245aff63ce 295 }
bborredon 0:80245aff63ce 296
bborredon 0:80245aff63ce 297 // Wait end of write
bborredon 0:80245aff63ce 298 ready();
bborredon 0:80245aff63ce 299
bborredon 0:80245aff63ce 300 // Increment address
bborredon 0:80245aff63ce 301 address += fpart;
bborredon 0:80245aff63ce 302
bborredon 0:80245aff63ce 303 if(page < _page_number - 1) {
bborredon 0:80245aff63ce 304 // Increment page
bborredon 0:80245aff63ce 305 page++;
bborredon 0:80245aff63ce 306
bborredon 0:80245aff63ce 307 // Device address
bborredon 0:80245aff63ce 308 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 309
bborredon 0:80245aff63ce 310 // Word address
bborredon 0:80245aff63ce 311 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 312
bborredon 0:80245aff63ce 313 // Data index
bborredon 0:80245aff63ce 314 ind = i * _page_write + fpart;
bborredon 0:80245aff63ce 315
bborredon 0:80245aff63ce 316 // Number of bytes in next page
bborredon 0:80245aff63ce 317 lpart = _page_write - fpart;
bborredon 0:80245aff63ce 318
bborredon 0:80245aff63ce 319 // Add data for next page
bborredon 0:80245aff63ce 320 for(j = 0;j < lpart;j++)
bborredon 0:80245aff63ce 321 cmd[j + 1] = (uint8_t) data[ind + j];
bborredon 0:80245aff63ce 322
bborredon 0:80245aff63ce 323 // Write data for next page
bborredon 0:80245aff63ce 324 ack = _i2c.write((int)addr,(char *)cmd,lpart + 1);
bborredon 0:80245aff63ce 325 if(ack != 0) {
bborredon 0:80245aff63ce 326 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 327 return;
bborredon 0:80245aff63ce 328 }
bborredon 0:80245aff63ce 329
bborredon 0:80245aff63ce 330 // Wait end of write
bborredon 0:80245aff63ce 331 ready();
bborredon 0:80245aff63ce 332
bborredon 0:80245aff63ce 333 // Increment address
bborredon 0:80245aff63ce 334 address += lpart;
bborredon 0:80245aff63ce 335 }
bborredon 0:80245aff63ce 336 }
bborredon 0:80245aff63ce 337 }
bborredon 0:80245aff63ce 338 else {
bborredon 0:80245aff63ce 339 // First word address (MSB)
bborredon 0:80245aff63ce 340 cmd[0] = (uint8_t) (address >> 8);
bborredon 0:80245aff63ce 341
bborredon 0:80245aff63ce 342 // Second word address (LSB)
bborredon 0:80245aff63ce 343 cmd[1] = (uint8_t) address;
bborredon 0:80245aff63ce 344
bborredon 0:80245aff63ce 345 // Add data
bborredon 0:80245aff63ce 346 for(j = 0;j < _page_write;j++)
bborredon 0:80245aff63ce 347 cmd[j + 2] = (uint8_t) data[i * _page_write + j];
bborredon 0:80245aff63ce 348
bborredon 0:80245aff63ce 349 // Write data
bborredon 0:80245aff63ce 350 ack = _i2c.write((int)addr,(char *)cmd,_page_write + 2);
bborredon 0:80245aff63ce 351 if(ack != 0) {
bborredon 0:80245aff63ce 352 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 353 return;
bborredon 0:80245aff63ce 354 }
bborredon 0:80245aff63ce 355
bborredon 0:80245aff63ce 356 // Wait end of write
bborredon 0:80245aff63ce 357 ready();
bborredon 0:80245aff63ce 358
bborredon 0:80245aff63ce 359 // Increment address
bborredon 0:80245aff63ce 360 address += _page_write;
bborredon 0:80245aff63ce 361 }
bborredon 0:80245aff63ce 362 }
bborredon 0:80245aff63ce 363
bborredon 3:925096a4c7f0 364 if(remain) {
bborredon 1:a262173cac81 365 // Compute page number
bborredon 1:a262173cac81 366 page = 0;
bborredon 1:a262173cac81 367 if(_type < T24C32)
bborredon 1:a262173cac81 368 page = (uint8_t) (address / 256);
bborredon 0:80245aff63ce 369
bborredon 1:a262173cac81 370 // Device address
bborredon 1:a262173cac81 371 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 372
bborredon 1:a262173cac81 373 if(_type < T24C32) {
bborredon 1:a262173cac81 374 // Word address
bborredon 1:a262173cac81 375 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 376
bborredon 1:a262173cac81 377 if((uint8_t) ((address + remain) / 256) == page) { // Data fit in the same page
bborredon 1:a262173cac81 378 // Add data for the current page
bborredon 1:a262173cac81 379 for(j = 0;j < remain;j++)
bborredon 1:a262173cac81 380 cmd[j + 1] = (uint8_t) data[blocs * _page_write + j];
bborredon 0:80245aff63ce 381
bborredon 1:a262173cac81 382 // Write data for the current page
bborredon 1:a262173cac81 383 ack = _i2c.write((int)addr,(char *)cmd,remain + 1);
bborredon 0:80245aff63ce 384 if(ack != 0) {
bborredon 0:80245aff63ce 385 _errnum = EEPROM_I2cError;
juanc95 6:7d676c26da40 386 MasterLed=1; //TODO: falla acá
bborredon 0:80245aff63ce 387 return;
bborredon 0:80245aff63ce 388 }
bborredon 1:a262173cac81 389
bborredon 0:80245aff63ce 390 // Wait end of write
bborredon 0:80245aff63ce 391 ready();
bborredon 0:80245aff63ce 392 }
bborredon 1:a262173cac81 393 else { // Data on 2 pages. We must split the write
bborredon 1:a262173cac81 394 // Number of bytes in current page
bborredon 1:a262173cac81 395 fpart = (page + 1) * 256 - address;
bborredon 1:a262173cac81 396
bborredon 1:a262173cac81 397 // Add data for current page
bborredon 1:a262173cac81 398 for(j = 0;j < fpart;j++)
bborredon 1:a262173cac81 399 cmd[j + 1] = (uint8_t) data[blocs * _page_write + j];
bborredon 1:a262173cac81 400
bborredon 1:a262173cac81 401 // Write data for current page
bborredon 1:a262173cac81 402 ack = _i2c.write((int)addr,(char *)cmd,fpart + 1);
bborredon 1:a262173cac81 403 if(ack != 0) {
bborredon 1:a262173cac81 404 _errnum = EEPROM_I2cError;
bborredon 1:a262173cac81 405 return;
bborredon 1:a262173cac81 406 }
bborredon 1:a262173cac81 407
bborredon 1:a262173cac81 408 // Wait end of write
bborredon 1:a262173cac81 409 ready();
bborredon 1:a262173cac81 410
bborredon 1:a262173cac81 411 // Increment address
bborredon 1:a262173cac81 412 address += fpart;
bborredon 1:a262173cac81 413
bborredon 1:a262173cac81 414 if(page < _page_number - 1) {
bborredon 1:a262173cac81 415 // Increment page
bborredon 1:a262173cac81 416 page++;
bborredon 1:a262173cac81 417
bborredon 1:a262173cac81 418 // Device address
bborredon 1:a262173cac81 419 addr = EEPROM_Address | _address | (page << 1);
bborredon 1:a262173cac81 420
bborredon 1:a262173cac81 421 // Word address
bborredon 1:a262173cac81 422 cmd[0] = (uint8_t) (address - page * 256);
bborredon 1:a262173cac81 423
bborredon 1:a262173cac81 424 // Data index
bborredon 1:a262173cac81 425 ind = blocs * _page_write + fpart;
bborredon 1:a262173cac81 426
bborredon 1:a262173cac81 427 // Number of bytes in next page
bborredon 1:a262173cac81 428 lpart = remain - fpart;
bborredon 1:a262173cac81 429
bborredon 1:a262173cac81 430 // Add data for next page
bborredon 1:a262173cac81 431 for(j = 0;j < lpart;j++)
bborredon 1:a262173cac81 432 cmd[j + 1] = (uint8_t) data[ind + j];
bborredon 1:a262173cac81 433
bborredon 1:a262173cac81 434 // Write data for next page
bborredon 1:a262173cac81 435 ack = _i2c.write((int)addr,(char *)cmd,lpart + 1);
bborredon 1:a262173cac81 436 if(ack != 0) {
bborredon 1:a262173cac81 437 _errnum = EEPROM_I2cError;
bborredon 1:a262173cac81 438 return;
bborredon 1:a262173cac81 439 }
bborredon 1:a262173cac81 440
bborredon 1:a262173cac81 441 // Wait end of write
bborredon 1:a262173cac81 442 ready();
bborredon 1:a262173cac81 443 }
bborredon 3:925096a4c7f0 444 }
bborredon 0:80245aff63ce 445 }
bborredon 3:925096a4c7f0 446 else {
bborredon 3:925096a4c7f0 447 // Fist word address (MSB)
bborredon 3:925096a4c7f0 448 cmd[0] = (uint8_t) (address >> 8);
bborredon 0:80245aff63ce 449
bborredon 3:925096a4c7f0 450 // Second word address (LSB)
bborredon 3:925096a4c7f0 451 cmd[1] = (uint8_t) address;
bborredon 0:80245aff63ce 452
bborredon 3:925096a4c7f0 453 // Add data for the current page
bborredon 3:925096a4c7f0 454 for(j = 0;j < remain;j++)
bborredon 3:925096a4c7f0 455 cmd[j + 2] = (uint8_t) data[blocs * _page_write + j];
bborredon 3:925096a4c7f0 456
bborredon 3:925096a4c7f0 457 // Write data for the current page
bborredon 3:925096a4c7f0 458 ack = _i2c.write((int)addr,(char *)cmd,remain + 2);
bborredon 3:925096a4c7f0 459 if(ack != 0) {
bborredon 3:925096a4c7f0 460 _errnum = EEPROM_I2cError;
bborredon 3:925096a4c7f0 461 return;
bborredon 3:925096a4c7f0 462 }
bborredon 0:80245aff63ce 463
bborredon 3:925096a4c7f0 464 // Wait end of write
bborredon 3:925096a4c7f0 465 ready();
bborredon 3:925096a4c7f0 466 }
bborredon 0:80245aff63ce 467 }
juanc95 6:7d676c26da40 468 MasterLed=0;
bborredon 0:80245aff63ce 469 }
bborredon 0:80245aff63ce 470
bborredon 3:925096a4c7f0 471 /**
bborredon 3:925096a4c7f0 472 * void write(uint32_t address, int16_t data)
bborredon 3:925096a4c7f0 473 *
bborredon 3:925096a4c7f0 474 * Write short
bborredon 3:925096a4c7f0 475 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 476 * @param data short to write (int16_t)
bborredon 3:925096a4c7f0 477 * @return none
bborredon 3:925096a4c7f0 478 */
juanc95 6:7d676c26da40 479
bborredon 1:a262173cac81 480 void EEPROM::write(uint32_t address, int16_t data)
bborredon 0:80245aff63ce 481 {
bborredon 0:80245aff63ce 482 int8_t cmd[2];
bborredon 3:925096a4c7f0 483
bborredon 0:80245aff63ce 484 // Check error
bborredon 0:80245aff63ce 485 if(_errnum)
bborredon 0:80245aff63ce 486 return;
bborredon 0:80245aff63ce 487
bborredon 0:80245aff63ce 488 // Check address
bborredon 0:80245aff63ce 489 if(!checkAddress(address + 1)) {
bborredon 0:80245aff63ce 490 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 491 return;
bborredon 0:80245aff63ce 492 }
bborredon 0:80245aff63ce 493
bborredon 0:80245aff63ce 494 memcpy(cmd,&data,2);
bborredon 0:80245aff63ce 495 write(address,cmd,2);
bborredon 0:80245aff63ce 496 }
bborredon 0:80245aff63ce 497
bborredon 3:925096a4c7f0 498 /**
bborredon 3:925096a4c7f0 499 * void write(uint32_t address, int32_t data)
bborredon 3:925096a4c7f0 500 *
bborredon 3:925096a4c7f0 501 * Write long
bborredon 3:925096a4c7f0 502 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 503 * @param data long to write (int32_t)
bborredon 3:925096a4c7f0 504 * @return none
bborredon 3:925096a4c7f0 505 */
bborredon 1:a262173cac81 506 void EEPROM::write(uint32_t address, int32_t data)
bborredon 0:80245aff63ce 507 {
bborredon 0:80245aff63ce 508 int8_t cmd[4];
bborredon 3:925096a4c7f0 509
bborredon 0:80245aff63ce 510 // Check error
bborredon 0:80245aff63ce 511 if(_errnum)
bborredon 0:80245aff63ce 512 return;
bborredon 0:80245aff63ce 513
bborredon 0:80245aff63ce 514 // Check address
bborredon 0:80245aff63ce 515 if(!checkAddress(address + 3)) {
bborredon 0:80245aff63ce 516 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 517 return;
bborredon 0:80245aff63ce 518 }
bborredon 0:80245aff63ce 519
bborredon 0:80245aff63ce 520 memcpy(cmd,&data,4);
bborredon 0:80245aff63ce 521
bborredon 0:80245aff63ce 522 write(address,cmd,4);
bborredon 0:80245aff63ce 523
bborredon 0:80245aff63ce 524 }
bborredon 0:80245aff63ce 525
bborredon 3:925096a4c7f0 526 /**
bborredon 3:925096a4c7f0 527 * void write(uint32_t address, float data)
bborredon 3:925096a4c7f0 528 *
bborredon 3:925096a4c7f0 529 * Write float
bborredon 3:925096a4c7f0 530 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 531 * @param data float to write (float)
bborredon 3:925096a4c7f0 532 * @return none
bborredon 3:925096a4c7f0 533 */
bborredon 1:a262173cac81 534 void EEPROM::write(uint32_t address, float data)
bborredon 0:80245aff63ce 535 {
bborredon 0:80245aff63ce 536 int8_t cmd[4];
bborredon 3:925096a4c7f0 537
bborredon 0:80245aff63ce 538 // Check error
bborredon 0:80245aff63ce 539 if(_errnum)
bborredon 0:80245aff63ce 540 return;
bborredon 0:80245aff63ce 541
bborredon 0:80245aff63ce 542 // Check address
bborredon 0:80245aff63ce 543 if(!checkAddress(address + 3)) {
bborredon 0:80245aff63ce 544 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 545 return;
bborredon 0:80245aff63ce 546 }
bborredon 0:80245aff63ce 547
bborredon 0:80245aff63ce 548 memcpy(cmd,&data,4);
bborredon 0:80245aff63ce 549
bborredon 0:80245aff63ce 550 write(address,cmd,4);
bborredon 0:80245aff63ce 551
bborredon 0:80245aff63ce 552 }
bborredon 0:80245aff63ce 553
bborredon 3:925096a4c7f0 554 /**
bborredon 3:925096a4c7f0 555 * void write(uint32_t address, void *data, uint32_t size)
bborredon 3:925096a4c7f0 556 *
bborredon 3:925096a4c7f0 557 * Write anything (use the page write mode)
bborredon 3:925096a4c7f0 558 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 559 * @param data data to write (void *)
bborredon 3:925096a4c7f0 560 * @param size number of bytes to write (uint32_t)
bborredon 3:925096a4c7f0 561 * @return none
bborredon 3:925096a4c7f0 562 */
bborredon 1:a262173cac81 563 void EEPROM::write(uint32_t address, void *data, uint32_t size)
bborredon 0:80245aff63ce 564 {
bborredon 0:80245aff63ce 565 int8_t *cmd = NULL;
bborredon 3:925096a4c7f0 566
bborredon 0:80245aff63ce 567 // Check error
bborredon 0:80245aff63ce 568 if(_errnum)
bborredon 0:80245aff63ce 569 return;
bborredon 0:80245aff63ce 570
bborredon 0:80245aff63ce 571 // Check address
bborredon 0:80245aff63ce 572 if(!checkAddress(address + size - 1)) {
bborredon 0:80245aff63ce 573 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 574 return;
bborredon 0:80245aff63ce 575 }
bborredon 0:80245aff63ce 576
bborredon 0:80245aff63ce 577 cmd = (int8_t *)malloc(size);
bborredon 0:80245aff63ce 578 if(cmd == NULL) {
bborredon 0:80245aff63ce 579 _errnum = EEPROM_MallocError;
bborredon 0:80245aff63ce 580 return;
bborredon 0:80245aff63ce 581 }
bborredon 0:80245aff63ce 582
bborredon 1:a262173cac81 583 memcpy(cmd,(uint8_t *)data,size);
bborredon 0:80245aff63ce 584
bborredon 0:80245aff63ce 585 write(address,cmd,size);
bborredon 1:a262173cac81 586
bborredon 0:80245aff63ce 587 free(cmd);
bborredon 0:80245aff63ce 588
bborredon 0:80245aff63ce 589 }
bborredon 0:80245aff63ce 590
bborredon 3:925096a4c7f0 591 /**
bborredon 3:925096a4c7f0 592 * void read(uint32_t address, int8_t& data)
bborredon 3:925096a4c7f0 593 *
bborredon 3:925096a4c7f0 594 * Random read byte
bborredon 3:925096a4c7f0 595 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 596 * @param data byte to read (int8_t&)
bborredon 3:925096a4c7f0 597 * @return none
bborredon 3:925096a4c7f0 598 */
bborredon 1:a262173cac81 599 void EEPROM::read(uint32_t address, int8_t& data)
bborredon 0:80245aff63ce 600 {
bborredon 0:80245aff63ce 601 uint8_t page;
bborredon 0:80245aff63ce 602 uint8_t addr;
bborredon 0:80245aff63ce 603 uint8_t cmd[2];
bborredon 0:80245aff63ce 604 uint8_t len;
bborredon 0:80245aff63ce 605 int ack;
bborredon 3:925096a4c7f0 606
bborredon 0:80245aff63ce 607 // Check error
bborredon 0:80245aff63ce 608 if(_errnum)
bborredon 0:80245aff63ce 609 return;
bborredon 0:80245aff63ce 610
bborredon 0:80245aff63ce 611 // Check address
bborredon 0:80245aff63ce 612 if(!checkAddress(address)) {
bborredon 0:80245aff63ce 613 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 614 return;
bborredon 0:80245aff63ce 615 }
bborredon 3:925096a4c7f0 616
bborredon 0:80245aff63ce 617 // Compute page number
bborredon 0:80245aff63ce 618 page = 0;
bborredon 0:80245aff63ce 619 if(_type < T24C32)
bborredon 0:80245aff63ce 620 page = (uint8_t) (address / 256);
bborredon 0:80245aff63ce 621
bborredon 0:80245aff63ce 622 // Device address
bborredon 0:80245aff63ce 623 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 624
bborredon 0:80245aff63ce 625 if(_type < T24C32) {
bborredon 0:80245aff63ce 626 len = 1;
bborredon 0:80245aff63ce 627
bborredon 0:80245aff63ce 628 // Word address
bborredon 0:80245aff63ce 629 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 630 }
bborredon 0:80245aff63ce 631 else {
bborredon 0:80245aff63ce 632 len = 2;
bborredon 0:80245aff63ce 633
bborredon 0:80245aff63ce 634 // First word address (MSB)
bborredon 0:80245aff63ce 635 cmd[0] = (uint8_t) (address >> 8);
bborredon 0:80245aff63ce 636
bborredon 0:80245aff63ce 637 // Second word address (LSB)
bborredon 0:80245aff63ce 638 cmd[1] = (uint8_t)address;
bborredon 0:80245aff63ce 639 }
bborredon 0:80245aff63ce 640
bborredon 0:80245aff63ce 641 // Write command
bborredon 0:80245aff63ce 642 ack = _i2c.write((int)addr,(char *)cmd,len,true);
bborredon 0:80245aff63ce 643 if(ack != 0) {
bborredon 0:80245aff63ce 644 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 645 return;
bborredon 0:80245aff63ce 646 }
bborredon 0:80245aff63ce 647
bborredon 0:80245aff63ce 648 // Read data
bborredon 0:80245aff63ce 649 ack = _i2c.read((int)addr,(char *)&data,sizeof(data));
bborredon 0:80245aff63ce 650 if(ack != 0) {
bborredon 0:80245aff63ce 651 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 652 return;
bborredon 0:80245aff63ce 653 }
bborredon 0:80245aff63ce 654
bborredon 0:80245aff63ce 655 }
bborredon 0:80245aff63ce 656
bborredon 3:925096a4c7f0 657 /**
bborredon 3:925096a4c7f0 658 * void read(uint32_t address, int8_t *data, uint32_t size)
bborredon 3:925096a4c7f0 659 *
bborredon 3:925096a4c7f0 660 * Sequential read byte
bborredon 3:925096a4c7f0 661 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 662 * @param data bytes array to read (int8_t[]&)
bborredon 3:925096a4c7f0 663 * @param size number of bytes to read (uint32_t)
bborredon 3:925096a4c7f0 664 * @return none
bborredon 3:925096a4c7f0 665 */
bborredon 1:a262173cac81 666 void EEPROM::read(uint32_t address, int8_t *data, uint32_t size)
bborredon 0:80245aff63ce 667 {
bborredon 0:80245aff63ce 668 uint8_t page;
bborredon 0:80245aff63ce 669 uint8_t addr;
bborredon 0:80245aff63ce 670 uint8_t cmd[2];
bborredon 0:80245aff63ce 671 uint8_t len;
bborredon 0:80245aff63ce 672 int ack;
bborredon 3:925096a4c7f0 673
bborredon 0:80245aff63ce 674 // Check error
bborredon 0:80245aff63ce 675 if(_errnum)
bborredon 0:80245aff63ce 676 return;
bborredon 0:80245aff63ce 677
bborredon 0:80245aff63ce 678 // Check address
bborredon 0:80245aff63ce 679 if(!checkAddress(address)) {
bborredon 0:80245aff63ce 680 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 681 return;
bborredon 0:80245aff63ce 682 }
bborredon 0:80245aff63ce 683
bborredon 0:80245aff63ce 684 // Check size
bborredon 0:80245aff63ce 685 if(!checkAddress(address + size - 1)) {
bborredon 0:80245aff63ce 686 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 687 return;
bborredon 0:80245aff63ce 688 }
bborredon 0:80245aff63ce 689
bborredon 0:80245aff63ce 690 // Compute page number
bborredon 0:80245aff63ce 691 page = 0;
bborredon 0:80245aff63ce 692 if(_type < T24C32)
bborredon 0:80245aff63ce 693 page = (uint8_t) (address / 256);
bborredon 0:80245aff63ce 694
bborredon 0:80245aff63ce 695 // Device address
bborredon 0:80245aff63ce 696 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 697
bborredon 0:80245aff63ce 698 if(_type < T24C32) {
bborredon 0:80245aff63ce 699 len = 1;
bborredon 0:80245aff63ce 700
bborredon 0:80245aff63ce 701 // Word address
bborredon 0:80245aff63ce 702 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 703 }
bborredon 0:80245aff63ce 704 else {
bborredon 0:80245aff63ce 705 len = 2;
bborredon 0:80245aff63ce 706
bborredon 0:80245aff63ce 707 // First word address (MSB)
bborredon 0:80245aff63ce 708 cmd[0] = (uint8_t) (address >> 8);
bborredon 0:80245aff63ce 709
bborredon 0:80245aff63ce 710 // Second word address (LSB)
bborredon 0:80245aff63ce 711 cmd[1] = (uint8_t) address;
bborredon 0:80245aff63ce 712 }
bborredon 0:80245aff63ce 713
bborredon 0:80245aff63ce 714 // Write command
bborredon 0:80245aff63ce 715 ack = _i2c.write((int)addr,(char *)cmd,len,true);
bborredon 0:80245aff63ce 716 if(ack != 0) {
bborredon 0:80245aff63ce 717 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 718 return;
bborredon 0:80245aff63ce 719 }
bborredon 0:80245aff63ce 720
bborredon 0:80245aff63ce 721 // Sequential read
bborredon 0:80245aff63ce 722 ack = _i2c.read((int)addr,(char *)data,size);
bborredon 0:80245aff63ce 723 if(ack != 0) {
bborredon 0:80245aff63ce 724 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 725 return;
bborredon 0:80245aff63ce 726 }
bborredon 0:80245aff63ce 727
bborredon 0:80245aff63ce 728 }
bborredon 0:80245aff63ce 729
bborredon 3:925096a4c7f0 730 /**
bborredon 3:925096a4c7f0 731 * void read(int8_t& data)
bborredon 3:925096a4c7f0 732 *
bborredon 3:925096a4c7f0 733 * Current address read byte
bborredon 3:925096a4c7f0 734 * @param data byte to read (int8_t&)
bborredon 3:925096a4c7f0 735 * @return none
bborredon 3:925096a4c7f0 736 */
bborredon 0:80245aff63ce 737 void EEPROM::read(int8_t& data)
bborredon 0:80245aff63ce 738 {
bborredon 0:80245aff63ce 739 uint8_t addr;
bborredon 0:80245aff63ce 740 int ack;
bborredon 3:925096a4c7f0 741
bborredon 0:80245aff63ce 742 // Check error
bborredon 0:80245aff63ce 743 if(_errnum)
bborredon 0:80245aff63ce 744 return;
bborredon 0:80245aff63ce 745
bborredon 0:80245aff63ce 746 // Device address
bborredon 0:80245aff63ce 747 addr = EEPROM_Address | _address;
bborredon 0:80245aff63ce 748
bborredon 0:80245aff63ce 749 // Read data
bborredon 0:80245aff63ce 750 ack = _i2c.read((int)addr,(char *)&data,sizeof(data));
bborredon 0:80245aff63ce 751 if(ack != 0) {
bborredon 0:80245aff63ce 752 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 753 return;
bborredon 0:80245aff63ce 754 }
bborredon 0:80245aff63ce 755
bborredon 0:80245aff63ce 756 }
bborredon 0:80245aff63ce 757
bborredon 3:925096a4c7f0 758 /**
bborredon 3:925096a4c7f0 759 * void read(uint32_t address, int16_t& data)
bborredon 3:925096a4c7f0 760 *
bborredon 3:925096a4c7f0 761 * Random read short
bborredon 3:925096a4c7f0 762 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 763 * @param data short to read (int16_t&)
bborredon 3:925096a4c7f0 764 * @return none
bborredon 3:925096a4c7f0 765 */
bborredon 1:a262173cac81 766 void EEPROM::read(uint32_t address, int16_t& data)
bborredon 0:80245aff63ce 767 {
bborredon 0:80245aff63ce 768 int8_t cmd[2];
bborredon 3:925096a4c7f0 769
bborredon 0:80245aff63ce 770 // Check error
bborredon 0:80245aff63ce 771 if(_errnum)
bborredon 0:80245aff63ce 772 return;
bborredon 0:80245aff63ce 773
bborredon 0:80245aff63ce 774 // Check address
bborredon 0:80245aff63ce 775 if(!checkAddress(address + 1)) {
bborredon 0:80245aff63ce 776 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 777 return;
bborredon 0:80245aff63ce 778 }
bborredon 0:80245aff63ce 779
bborredon 0:80245aff63ce 780 read(address,cmd,2);
bborredon 0:80245aff63ce 781
bborredon 0:80245aff63ce 782 memcpy(&data,cmd,2);
bborredon 0:80245aff63ce 783
bborredon 0:80245aff63ce 784 }
bborredon 0:80245aff63ce 785
bborredon 3:925096a4c7f0 786 /**
bborredon 3:925096a4c7f0 787 * void read(uint32_t address, int32_t& data)
bborredon 3:925096a4c7f0 788 *
bborredon 3:925096a4c7f0 789 * Random read long
bborredon 3:925096a4c7f0 790 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 791 * @param data long to read (int32_t&)
bborredon 3:925096a4c7f0 792 * @return none
bborredon 3:925096a4c7f0 793 */
bborredon 1:a262173cac81 794 void EEPROM::read(uint32_t address, int32_t& data)
bborredon 0:80245aff63ce 795 {
bborredon 0:80245aff63ce 796 int8_t cmd[4];
bborredon 3:925096a4c7f0 797
bborredon 0:80245aff63ce 798 // Check error
bborredon 0:80245aff63ce 799 if(_errnum)
bborredon 0:80245aff63ce 800 return;
bborredon 0:80245aff63ce 801
bborredon 0:80245aff63ce 802 // Check address
bborredon 0:80245aff63ce 803 if(!checkAddress(address + 3)) {
bborredon 0:80245aff63ce 804 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 805 return;
bborredon 0:80245aff63ce 806 }
bborredon 0:80245aff63ce 807
bborredon 0:80245aff63ce 808 read(address,cmd,4);
bborredon 0:80245aff63ce 809
bborredon 0:80245aff63ce 810 memcpy(&data,cmd,4);
bborredon 0:80245aff63ce 811
bborredon 0:80245aff63ce 812 }
bborredon 0:80245aff63ce 813
bborredon 3:925096a4c7f0 814 /**
bborredon 3:925096a4c7f0 815 * void read(uint32_t address, float& data)
bborredon 3:925096a4c7f0 816 *
bborredon 3:925096a4c7f0 817 * Random read float
bborredon 3:925096a4c7f0 818 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 819 * @param data float to read (float&)
bborredon 3:925096a4c7f0 820 * @return none
bborredon 3:925096a4c7f0 821 */
bborredon 1:a262173cac81 822 void EEPROM::read(uint32_t address, float& data)
bborredon 0:80245aff63ce 823 {
bborredon 0:80245aff63ce 824 int8_t cmd[4];
bborredon 3:925096a4c7f0 825
bborredon 0:80245aff63ce 826 // Check error
bborredon 0:80245aff63ce 827 if(_errnum)
bborredon 0:80245aff63ce 828 return;
bborredon 0:80245aff63ce 829
bborredon 0:80245aff63ce 830 // Check address
bborredon 0:80245aff63ce 831 if(!checkAddress(address + 3)) {
bborredon 0:80245aff63ce 832 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 833 return;
bborredon 0:80245aff63ce 834 }
bborredon 0:80245aff63ce 835
bborredon 0:80245aff63ce 836 read(address,cmd,4);
bborredon 0:80245aff63ce 837
bborredon 0:80245aff63ce 838 memcpy(&data,cmd,4);
bborredon 0:80245aff63ce 839
bborredon 0:80245aff63ce 840 }
bborredon 0:80245aff63ce 841
bborredon 3:925096a4c7f0 842 /**
bborredon 3:925096a4c7f0 843 * void read(uint32_t address, void *data, uint32_t size)
bborredon 3:925096a4c7f0 844 *
bborredon 3:925096a4c7f0 845 * Random read anything
bborredon 3:925096a4c7f0 846 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 847 * @param data data to read (void *)
bborredon 3:925096a4c7f0 848 * @param size number of bytes to read (uint32_t)
bborredon 3:925096a4c7f0 849 * @return none
bborredon 3:925096a4c7f0 850 */
bborredon 1:a262173cac81 851 void EEPROM::read(uint32_t address, void *data, uint32_t size)
bborredon 0:80245aff63ce 852 {
bborredon 0:80245aff63ce 853 int8_t *cmd = NULL;
bborredon 1:a262173cac81 854
bborredon 0:80245aff63ce 855 // Check error
bborredon 0:80245aff63ce 856 if(_errnum)
bborredon 0:80245aff63ce 857 return;
bborredon 0:80245aff63ce 858
bborredon 0:80245aff63ce 859 // Check address
bborredon 0:80245aff63ce 860 if(!checkAddress(address + size - 1)) {
bborredon 0:80245aff63ce 861 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 862 return;
bborredon 0:80245aff63ce 863 }
bborredon 0:80245aff63ce 864
bborredon 0:80245aff63ce 865 cmd = (int8_t *)malloc(size);
bborredon 1:a262173cac81 866
bborredon 0:80245aff63ce 867 if(cmd == NULL) {
bborredon 0:80245aff63ce 868 _errnum = EEPROM_MallocError;
bborredon 0:80245aff63ce 869 return;
bborredon 0:80245aff63ce 870 }
bborredon 0:80245aff63ce 871
bborredon 1:a262173cac81 872 read(address,(int8_t *)cmd,size);
bborredon 0:80245aff63ce 873
bborredon 0:80245aff63ce 874 memcpy(data,cmd,size);
bborredon 0:80245aff63ce 875
bborredon 0:80245aff63ce 876 free(cmd);
bborredon 0:80245aff63ce 877
bborredon 0:80245aff63ce 878 }
bborredon 0:80245aff63ce 879
bborredon 3:925096a4c7f0 880 /**
bborredon 3:925096a4c7f0 881 * void clear(void)
bborredon 3:925096a4c7f0 882 *
bborredon 3:925096a4c7f0 883 * Clear eeprom (write with 0)
bborredon 3:925096a4c7f0 884 * @param none
bborredon 3:925096a4c7f0 885 * @return none
bborredon 3:925096a4c7f0 886 */
bborredon 1:a262173cac81 887 void EEPROM::clear(void)
bborredon 1:a262173cac81 888 {
bborredon 1:a262173cac81 889 int32_t data;
bborredon 1:a262173cac81 890 uint32_t i;
bborredon 1:a262173cac81 891
bborredon 1:a262173cac81 892 data = 0;
bborredon 1:a262173cac81 893
bborredon 1:a262173cac81 894 for(i = 0; i < _size / 4;i++) {
bborredon 1:a262173cac81 895 write((uint32_t)(i * 4),data);
bborredon 1:a262173cac81 896 }
bborredon 1:a262173cac81 897 }
bborredon 1:a262173cac81 898
bborredon 3:925096a4c7f0 899 /**
bborredon 3:925096a4c7f0 900 * void ready(void)
bborredon 3:925096a4c7f0 901 *
bborredon 3:925096a4c7f0 902 * Wait eeprom ready
bborredon 3:925096a4c7f0 903 * @param none
bborredon 3:925096a4c7f0 904 * @return none
bborredon 3:925096a4c7f0 905 */
bborredon 0:80245aff63ce 906 void EEPROM::ready(void)
bborredon 0:80245aff63ce 907 {
bborredon 0:80245aff63ce 908 int ack;
bborredon 0:80245aff63ce 909 uint8_t addr;
bborredon 1:a262173cac81 910 uint8_t cmd[2];
bborredon 0:80245aff63ce 911
bborredon 0:80245aff63ce 912 // Check error
bborredon 0:80245aff63ce 913 if(_errnum)
bborredon 0:80245aff63ce 914 return;
bborredon 0:80245aff63ce 915
bborredon 0:80245aff63ce 916 // Device address
bborredon 0:80245aff63ce 917 addr = EEPROM_Address | _address;
bborredon 0:80245aff63ce 918
bborredon 1:a262173cac81 919 cmd[0] = 0;
bborredon 0:80245aff63ce 920
bborredon 0:80245aff63ce 921 // Wait end of write
bborredon 0:80245aff63ce 922 do {
bborredon 0:80245aff63ce 923 ack = _i2c.write((int)addr,(char *)cmd,0);
bborredon 1:a262173cac81 924 //wait(0.5);
bborredon 0:80245aff63ce 925 } while(ack != 0);
bborredon 0:80245aff63ce 926
bborredon 0:80245aff63ce 927 }
bborredon 0:80245aff63ce 928
bborredon 3:925096a4c7f0 929 /**
bborredon 3:925096a4c7f0 930 * uint32_t getSize(void)
bborredon 3:925096a4c7f0 931 *
bborredon 3:925096a4c7f0 932 * Get eeprom size in bytes
bborredon 3:925096a4c7f0 933 * @param none
bborredon 3:925096a4c7f0 934 * @return size in bytes (uint32_t)
bborredon 3:925096a4c7f0 935 */
bborredon 0:80245aff63ce 936 uint32_t EEPROM::getSize(void)
bborredon 0:80245aff63ce 937 {
bborredon 0:80245aff63ce 938 return(_size);
bborredon 0:80245aff63ce 939 }
bborredon 0:80245aff63ce 940
bborredon 3:925096a4c7f0 941 /**
bborredon 3:925096a4c7f0 942 * const char* getName(void)
bborredon 3:925096a4c7f0 943 *
bborredon 3:925096a4c7f0 944 * Get eeprom name
bborredon 3:925096a4c7f0 945 * @param none
bborredon 3:925096a4c7f0 946 * @return name (const char*)
bborredon 3:925096a4c7f0 947 */
bborredon 1:a262173cac81 948 const char* EEPROM::getName(void)
bborredon 1:a262173cac81 949 {
bborredon 1:a262173cac81 950 uint8_t i = 0;
bborredon 1:a262173cac81 951
bborredon 1:a262173cac81 952 switch(_type) {
bborredon 1:a262173cac81 953 case T24C01 :
bborredon 1:a262173cac81 954 i = 0;
bborredon 1:a262173cac81 955 break;
bborredon 1:a262173cac81 956 case T24C02 :
bborredon 1:a262173cac81 957 i = 1;
bborredon 1:a262173cac81 958 break;
bborredon 1:a262173cac81 959 case T24C04 :
bborredon 1:a262173cac81 960 i = 2;
bborredon 1:a262173cac81 961 break;
bborredon 1:a262173cac81 962 case T24C08 :
bborredon 1:a262173cac81 963 i = 3;
bborredon 1:a262173cac81 964 break;
bborredon 1:a262173cac81 965 case T24C16 :
bborredon 1:a262173cac81 966 i = 4;
bborredon 1:a262173cac81 967 break;
bborredon 1:a262173cac81 968 case T24C32 :
bborredon 1:a262173cac81 969 i = 5;
bborredon 1:a262173cac81 970 break;
bborredon 1:a262173cac81 971 case T24C64 :
bborredon 1:a262173cac81 972 i = 6;
bborredon 1:a262173cac81 973 break;
bborredon 1:a262173cac81 974 case T24C128 :
bborredon 1:a262173cac81 975 i = 7;
bborredon 1:a262173cac81 976 break;
bborredon 1:a262173cac81 977 case T24C256 :
bborredon 1:a262173cac81 978 i = 8;
bborredon 1:a262173cac81 979 break;
bborredon 1:a262173cac81 980 case T24C512 :
bborredon 1:a262173cac81 981 i = 9;
bborredon 1:a262173cac81 982 break;
bborredon 1:a262173cac81 983 case T24C1024 :
bborredon 1:a262173cac81 984 i = 10;
bborredon 1:a262173cac81 985 break;
bborredon 1:a262173cac81 986 case T24C1025 :
bborredon 1:a262173cac81 987 i = 11;
bborredon 1:a262173cac81 988 break;
bborredon 1:a262173cac81 989 }
bborredon 1:a262173cac81 990
bborredon 1:a262173cac81 991 return(_name[i]);
bborredon 1:a262173cac81 992 }
bborredon 1:a262173cac81 993
bborredon 3:925096a4c7f0 994 /**
bborredon 3:925096a4c7f0 995 * uint8_t getError(void)
bborredon 3:925096a4c7f0 996 *
bborredon 3:925096a4c7f0 997 * Get the current error number (EEPROM_NoError if no error)
bborredon 3:925096a4c7f0 998 * @param none
bborredon 3:925096a4c7f0 999 * @return none
bborredon 3:925096a4c7f0 1000 */
bborredon 0:80245aff63ce 1001 uint8_t EEPROM::getError(void)
bborredon 0:80245aff63ce 1002 {
bborredon 0:80245aff63ce 1003 return(_errnum);
bborredon 0:80245aff63ce 1004 }
bborredon 0:80245aff63ce 1005
bborredon 3:925096a4c7f0 1006 /**
bborredon 3:925096a4c7f0 1007 * bool checkAddress(uint32_t address)
bborredon 3:925096a4c7f0 1008 *
bborredon 3:925096a4c7f0 1009 * Check if address is in the eeprom range address
bborredon 3:925096a4c7f0 1010 * @param address address to check (uint32_t)
bborredon 3:925096a4c7f0 1011 * @return true if in eeprom range, overwise false (bool)
bborredon 3:925096a4c7f0 1012 */
bborredon 1:a262173cac81 1013 bool EEPROM::checkAddress(uint32_t address)
bborredon 0:80245aff63ce 1014 {
bborredon 0:80245aff63ce 1015 bool ret = true;
bborredon 0:80245aff63ce 1016
bborredon 0:80245aff63ce 1017 switch(_type) {
bborredon 0:80245aff63ce 1018 case T24C01 :
bborredon 0:80245aff63ce 1019 if(address >= T24C01)
bborredon 0:80245aff63ce 1020 ret = false;
bborredon 0:80245aff63ce 1021 break;
bborredon 0:80245aff63ce 1022 case T24C02 :
bborredon 0:80245aff63ce 1023 if(address >= T24C02)
bborredon 0:80245aff63ce 1024 ret = false;
bborredon 0:80245aff63ce 1025 break;
bborredon 0:80245aff63ce 1026 case T24C04 :
bborredon 0:80245aff63ce 1027 if(address >= T24C04)
bborredon 0:80245aff63ce 1028 ret = false;
bborredon 0:80245aff63ce 1029 break;
bborredon 0:80245aff63ce 1030 case T24C08 :
bborredon 0:80245aff63ce 1031 if(address >= T24C08)
bborredon 0:80245aff63ce 1032 ret = false;
bborredon 0:80245aff63ce 1033 break;
bborredon 0:80245aff63ce 1034 case T24C16 :
bborredon 0:80245aff63ce 1035 if(address >= T24C16)
bborredon 0:80245aff63ce 1036 ret = false;
bborredon 0:80245aff63ce 1037 break;
bborredon 0:80245aff63ce 1038 case T24C32 :
bborredon 0:80245aff63ce 1039 if(address >= T24C32)
bborredon 0:80245aff63ce 1040 ret = false;
bborredon 0:80245aff63ce 1041 break;
bborredon 0:80245aff63ce 1042 case T24C64 :
bborredon 0:80245aff63ce 1043 if(address >= T24C64)
bborredon 0:80245aff63ce 1044 ret = false;
bborredon 0:80245aff63ce 1045 break;
bborredon 0:80245aff63ce 1046 case T24C128 :
bborredon 0:80245aff63ce 1047 if(address >= T24C128)
bborredon 0:80245aff63ce 1048 ret = false;
bborredon 0:80245aff63ce 1049 break;
bborredon 0:80245aff63ce 1050 case T24C256 :
bborredon 0:80245aff63ce 1051 if(address >= T24C256)
bborredon 0:80245aff63ce 1052 ret = false;
bborredon 0:80245aff63ce 1053 break;
bborredon 0:80245aff63ce 1054 case T24C512 :
bborredon 0:80245aff63ce 1055 if(address >= T24C512)
bborredon 0:80245aff63ce 1056 ret = false;
bborredon 0:80245aff63ce 1057 break;
bborredon 0:80245aff63ce 1058 case T24C1024 :
bborredon 0:80245aff63ce 1059 if(address >= T24C1024)
bborredon 0:80245aff63ce 1060 ret = false;
bborredon 0:80245aff63ce 1061 break;
bborredon 0:80245aff63ce 1062 case T24C1025 :
bborredon 0:80245aff63ce 1063 if(address >= T24C1025 - 1)
bborredon 0:80245aff63ce 1064 ret = false;
bborredon 0:80245aff63ce 1065 break;
bborredon 0:80245aff63ce 1066 }
bborredon 0:80245aff63ce 1067
bborredon 0:80245aff63ce 1068 return(ret);
bborredon 1:a262173cac81 1069 }