se quito el led de debug de la bibilioteca

Fork of eeprom_1 by JONIX SA

Committer:
bborredon
Date:
Mon Dec 21 23:26:42 2015 +0000
Revision:
3:925096a4c7f0
Parent:
2:79ed7ff7c23d
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?

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 ************************************************************/
bborredon 0:80245aff63ce 26 #include "eeprom.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 */
bborredon 1:a262173cac81 214 void EEPROM::write(uint32_t address, int8_t data[], uint32_t length)
bborredon 0:80245aff63ce 215 {
bborredon 0:80245aff63ce 216 uint8_t page;
bborredon 1:a262173cac81 217 uint8_t addr = 0;
bborredon 0:80245aff63ce 218 uint8_t blocs,remain;
bborredon 0:80245aff63ce 219 uint8_t fpart,lpart;
bborredon 0:80245aff63ce 220 uint8_t i,j,ind;
bborredon 0:80245aff63ce 221 uint8_t cmd[129];
bborredon 0:80245aff63ce 222 int ack;
bborredon 3:925096a4c7f0 223
bborredon 0:80245aff63ce 224 // Check error
bborredon 0:80245aff63ce 225 if(_errnum)
bborredon 0:80245aff63ce 226 return;
bborredon 0:80245aff63ce 227
bborredon 0:80245aff63ce 228 // Check address
bborredon 0:80245aff63ce 229 if(!checkAddress(address)) {
bborredon 0:80245aff63ce 230 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 231 return;
bborredon 0:80245aff63ce 232 }
bborredon 0:80245aff63ce 233
bborredon 0:80245aff63ce 234 // Check length
bborredon 0:80245aff63ce 235 if(!checkAddress(address + length - 1)) {
bborredon 0:80245aff63ce 236 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 237 return;
bborredon 0:80245aff63ce 238 }
bborredon 0:80245aff63ce 239
bborredon 0:80245aff63ce 240 // Compute blocs numbers
bborredon 0:80245aff63ce 241 blocs = length / _page_write;
bborredon 0:80245aff63ce 242
bborredon 0:80245aff63ce 243 // Compute remaining bytes
bborredon 0:80245aff63ce 244 remain = length - blocs * _page_write;
bborredon 3:925096a4c7f0 245
bborredon 0:80245aff63ce 246 for(i = 0;i < blocs;i++) {
bborredon 0:80245aff63ce 247 // Compute page number
bborredon 0:80245aff63ce 248 page = 0;
bborredon 0:80245aff63ce 249 if(_type < T24C32)
bborredon 0:80245aff63ce 250 page = (uint8_t) (address / 256);
bborredon 0:80245aff63ce 251
bborredon 0:80245aff63ce 252 // Device address
bborredon 0:80245aff63ce 253 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 254
bborredon 0:80245aff63ce 255 if(_type < T24C32) {
bborredon 0:80245aff63ce 256 // Word address
bborredon 0:80245aff63ce 257 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 258
bborredon 0:80245aff63ce 259 if((uint8_t) ((address + _page_write) / 256) == page) { // Data fit in the same page
bborredon 0:80245aff63ce 260 // Add data
bborredon 0:80245aff63ce 261 for(j = 0;j < _page_write;j++)
bborredon 0:80245aff63ce 262 cmd[j + 1] = (uint8_t) data[i * _page_write + j];
bborredon 0:80245aff63ce 263
bborredon 0:80245aff63ce 264 // Write data
bborredon 0:80245aff63ce 265 ack = _i2c.write((int)addr,(char *)cmd,_page_write + 1);
bborredon 0:80245aff63ce 266 if(ack != 0) {
bborredon 0:80245aff63ce 267 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 268 return;
bborredon 0:80245aff63ce 269 }
bborredon 0:80245aff63ce 270
bborredon 0:80245aff63ce 271 // Wait end of write
bborredon 0:80245aff63ce 272 ready();
bborredon 0:80245aff63ce 273
bborredon 0:80245aff63ce 274 // Increment address
bborredon 0:80245aff63ce 275 address += _page_write;
bborredon 0:80245aff63ce 276 }
bborredon 0:80245aff63ce 277 else { // Data on 2 pages. We must split the write
bborredon 0:80245aff63ce 278 // Number of bytes in current page
bborredon 0:80245aff63ce 279 fpart = (page + 1) * 256 - address;
bborredon 0:80245aff63ce 280
bborredon 0:80245aff63ce 281 // Add data for current page
bborredon 0:80245aff63ce 282 for(j = 0;j < fpart;j++)
bborredon 0:80245aff63ce 283 cmd[j + 1] = (uint8_t) data[i * _page_write + j];
bborredon 0:80245aff63ce 284
bborredon 0:80245aff63ce 285 // Write data for current page
bborredon 0:80245aff63ce 286 ack = _i2c.write((int)addr,(char *)cmd,fpart + 1);
bborredon 0:80245aff63ce 287 if(ack != 0) {
bborredon 0:80245aff63ce 288 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 289 return;
bborredon 0:80245aff63ce 290 }
bborredon 0:80245aff63ce 291
bborredon 0:80245aff63ce 292 // Wait end of write
bborredon 0:80245aff63ce 293 ready();
bborredon 0:80245aff63ce 294
bborredon 0:80245aff63ce 295 // Increment address
bborredon 0:80245aff63ce 296 address += fpart;
bborredon 0:80245aff63ce 297
bborredon 0:80245aff63ce 298 if(page < _page_number - 1) {
bborredon 0:80245aff63ce 299 // Increment page
bborredon 0:80245aff63ce 300 page++;
bborredon 0:80245aff63ce 301
bborredon 0:80245aff63ce 302 // Device address
bborredon 0:80245aff63ce 303 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 304
bborredon 0:80245aff63ce 305 // Word address
bborredon 0:80245aff63ce 306 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 307
bborredon 0:80245aff63ce 308 // Data index
bborredon 0:80245aff63ce 309 ind = i * _page_write + fpart;
bborredon 0:80245aff63ce 310
bborredon 0:80245aff63ce 311 // Number of bytes in next page
bborredon 0:80245aff63ce 312 lpart = _page_write - fpart;
bborredon 0:80245aff63ce 313
bborredon 0:80245aff63ce 314 // Add data for next page
bborredon 0:80245aff63ce 315 for(j = 0;j < lpart;j++)
bborredon 0:80245aff63ce 316 cmd[j + 1] = (uint8_t) data[ind + j];
bborredon 0:80245aff63ce 317
bborredon 0:80245aff63ce 318 // Write data for next page
bborredon 0:80245aff63ce 319 ack = _i2c.write((int)addr,(char *)cmd,lpart + 1);
bborredon 0:80245aff63ce 320 if(ack != 0) {
bborredon 0:80245aff63ce 321 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 322 return;
bborredon 0:80245aff63ce 323 }
bborredon 0:80245aff63ce 324
bborredon 0:80245aff63ce 325 // Wait end of write
bborredon 0:80245aff63ce 326 ready();
bborredon 0:80245aff63ce 327
bborredon 0:80245aff63ce 328 // Increment address
bborredon 0:80245aff63ce 329 address += lpart;
bborredon 0:80245aff63ce 330 }
bborredon 0:80245aff63ce 331 }
bborredon 0:80245aff63ce 332 }
bborredon 0:80245aff63ce 333 else {
bborredon 0:80245aff63ce 334 // First word address (MSB)
bborredon 0:80245aff63ce 335 cmd[0] = (uint8_t) (address >> 8);
bborredon 0:80245aff63ce 336
bborredon 0:80245aff63ce 337 // Second word address (LSB)
bborredon 0:80245aff63ce 338 cmd[1] = (uint8_t) address;
bborredon 0:80245aff63ce 339
bborredon 0:80245aff63ce 340 // Add data
bborredon 0:80245aff63ce 341 for(j = 0;j < _page_write;j++)
bborredon 0:80245aff63ce 342 cmd[j + 2] = (uint8_t) data[i * _page_write + j];
bborredon 0:80245aff63ce 343
bborredon 0:80245aff63ce 344 // Write data
bborredon 0:80245aff63ce 345 ack = _i2c.write((int)addr,(char *)cmd,_page_write + 2);
bborredon 0:80245aff63ce 346 if(ack != 0) {
bborredon 0:80245aff63ce 347 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 348 return;
bborredon 0:80245aff63ce 349 }
bborredon 0:80245aff63ce 350
bborredon 0:80245aff63ce 351 // Wait end of write
bborredon 0:80245aff63ce 352 ready();
bborredon 0:80245aff63ce 353
bborredon 0:80245aff63ce 354 // Increment address
bborredon 0:80245aff63ce 355 address += _page_write;
bborredon 0:80245aff63ce 356 }
bborredon 0:80245aff63ce 357 }
bborredon 0:80245aff63ce 358
bborredon 3:925096a4c7f0 359 if(remain) {
bborredon 1:a262173cac81 360 // Compute page number
bborredon 1:a262173cac81 361 page = 0;
bborredon 1:a262173cac81 362 if(_type < T24C32)
bborredon 1:a262173cac81 363 page = (uint8_t) (address / 256);
bborredon 0:80245aff63ce 364
bborredon 1:a262173cac81 365 // Device address
bborredon 1:a262173cac81 366 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 367
bborredon 1:a262173cac81 368 if(_type < T24C32) {
bborredon 1:a262173cac81 369 // Word address
bborredon 1:a262173cac81 370 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 371
bborredon 1:a262173cac81 372 if((uint8_t) ((address + remain) / 256) == page) { // Data fit in the same page
bborredon 1:a262173cac81 373 // Add data for the current page
bborredon 1:a262173cac81 374 for(j = 0;j < remain;j++)
bborredon 1:a262173cac81 375 cmd[j + 1] = (uint8_t) data[blocs * _page_write + j];
bborredon 0:80245aff63ce 376
bborredon 1:a262173cac81 377 // Write data for the current page
bborredon 1:a262173cac81 378 ack = _i2c.write((int)addr,(char *)cmd,remain + 1);
bborredon 0:80245aff63ce 379 if(ack != 0) {
bborredon 0:80245aff63ce 380 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 381 return;
bborredon 0:80245aff63ce 382 }
bborredon 1:a262173cac81 383
bborredon 0:80245aff63ce 384 // Wait end of write
bborredon 0:80245aff63ce 385 ready();
bborredon 0:80245aff63ce 386 }
bborredon 1:a262173cac81 387 else { // Data on 2 pages. We must split the write
bborredon 1:a262173cac81 388 // Number of bytes in current page
bborredon 1:a262173cac81 389 fpart = (page + 1) * 256 - address;
bborredon 1:a262173cac81 390
bborredon 1:a262173cac81 391 // Add data for current page
bborredon 1:a262173cac81 392 for(j = 0;j < fpart;j++)
bborredon 1:a262173cac81 393 cmd[j + 1] = (uint8_t) data[blocs * _page_write + j];
bborredon 1:a262173cac81 394
bborredon 1:a262173cac81 395 // Write data for current page
bborredon 1:a262173cac81 396 ack = _i2c.write((int)addr,(char *)cmd,fpart + 1);
bborredon 1:a262173cac81 397 if(ack != 0) {
bborredon 1:a262173cac81 398 _errnum = EEPROM_I2cError;
bborredon 1:a262173cac81 399 return;
bborredon 1:a262173cac81 400 }
bborredon 1:a262173cac81 401
bborredon 1:a262173cac81 402 // Wait end of write
bborredon 1:a262173cac81 403 ready();
bborredon 1:a262173cac81 404
bborredon 1:a262173cac81 405 // Increment address
bborredon 1:a262173cac81 406 address += fpart;
bborredon 1:a262173cac81 407
bborredon 1:a262173cac81 408 if(page < _page_number - 1) {
bborredon 1:a262173cac81 409 // Increment page
bborredon 1:a262173cac81 410 page++;
bborredon 1:a262173cac81 411
bborredon 1:a262173cac81 412 // Device address
bborredon 1:a262173cac81 413 addr = EEPROM_Address | _address | (page << 1);
bborredon 1:a262173cac81 414
bborredon 1:a262173cac81 415 // Word address
bborredon 1:a262173cac81 416 cmd[0] = (uint8_t) (address - page * 256);
bborredon 1:a262173cac81 417
bborredon 1:a262173cac81 418 // Data index
bborredon 1:a262173cac81 419 ind = blocs * _page_write + fpart;
bborredon 1:a262173cac81 420
bborredon 1:a262173cac81 421 // Number of bytes in next page
bborredon 1:a262173cac81 422 lpart = remain - fpart;
bborredon 1:a262173cac81 423
bborredon 1:a262173cac81 424 // Add data for next page
bborredon 1:a262173cac81 425 for(j = 0;j < lpart;j++)
bborredon 1:a262173cac81 426 cmd[j + 1] = (uint8_t) data[ind + j];
bborredon 1:a262173cac81 427
bborredon 1:a262173cac81 428 // Write data for next page
bborredon 1:a262173cac81 429 ack = _i2c.write((int)addr,(char *)cmd,lpart + 1);
bborredon 1:a262173cac81 430 if(ack != 0) {
bborredon 1:a262173cac81 431 _errnum = EEPROM_I2cError;
bborredon 1:a262173cac81 432 return;
bborredon 1:a262173cac81 433 }
bborredon 1:a262173cac81 434
bborredon 1:a262173cac81 435 // Wait end of write
bborredon 1:a262173cac81 436 ready();
bborredon 1:a262173cac81 437 }
bborredon 3:925096a4c7f0 438 }
bborredon 0:80245aff63ce 439 }
bborredon 3:925096a4c7f0 440 else {
bborredon 3:925096a4c7f0 441 // Fist word address (MSB)
bborredon 3:925096a4c7f0 442 cmd[0] = (uint8_t) (address >> 8);
bborredon 0:80245aff63ce 443
bborredon 3:925096a4c7f0 444 // Second word address (LSB)
bborredon 3:925096a4c7f0 445 cmd[1] = (uint8_t) address;
bborredon 0:80245aff63ce 446
bborredon 3:925096a4c7f0 447 // Add data for the current page
bborredon 3:925096a4c7f0 448 for(j = 0;j < remain;j++)
bborredon 3:925096a4c7f0 449 cmd[j + 2] = (uint8_t) data[blocs * _page_write + j];
bborredon 3:925096a4c7f0 450
bborredon 3:925096a4c7f0 451 // Write data for the current page
bborredon 3:925096a4c7f0 452 ack = _i2c.write((int)addr,(char *)cmd,remain + 2);
bborredon 3:925096a4c7f0 453 if(ack != 0) {
bborredon 3:925096a4c7f0 454 _errnum = EEPROM_I2cError;
bborredon 3:925096a4c7f0 455 return;
bborredon 3:925096a4c7f0 456 }
bborredon 0:80245aff63ce 457
bborredon 3:925096a4c7f0 458 // Wait end of write
bborredon 3:925096a4c7f0 459 ready();
bborredon 3:925096a4c7f0 460 }
bborredon 0:80245aff63ce 461 }
bborredon 0:80245aff63ce 462
bborredon 0:80245aff63ce 463 }
bborredon 0:80245aff63ce 464
bborredon 3:925096a4c7f0 465 /**
bborredon 3:925096a4c7f0 466 * void write(uint32_t address, int16_t data)
bborredon 3:925096a4c7f0 467 *
bborredon 3:925096a4c7f0 468 * Write short
bborredon 3:925096a4c7f0 469 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 470 * @param data short to write (int16_t)
bborredon 3:925096a4c7f0 471 * @return none
bborredon 3:925096a4c7f0 472 */
bborredon 1:a262173cac81 473 void EEPROM::write(uint32_t address, int16_t data)
bborredon 0:80245aff63ce 474 {
bborredon 0:80245aff63ce 475 int8_t cmd[2];
bborredon 3:925096a4c7f0 476
bborredon 0:80245aff63ce 477 // Check error
bborredon 0:80245aff63ce 478 if(_errnum)
bborredon 0:80245aff63ce 479 return;
bborredon 0:80245aff63ce 480
bborredon 0:80245aff63ce 481 // Check address
bborredon 0:80245aff63ce 482 if(!checkAddress(address + 1)) {
bborredon 0:80245aff63ce 483 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 484 return;
bborredon 0:80245aff63ce 485 }
bborredon 0:80245aff63ce 486
bborredon 0:80245aff63ce 487 memcpy(cmd,&data,2);
bborredon 0:80245aff63ce 488
bborredon 0:80245aff63ce 489 write(address,cmd,2);
bborredon 0:80245aff63ce 490
bborredon 0:80245aff63ce 491 }
bborredon 0:80245aff63ce 492
bborredon 3:925096a4c7f0 493 /**
bborredon 3:925096a4c7f0 494 * void write(uint32_t address, int32_t data)
bborredon 3:925096a4c7f0 495 *
bborredon 3:925096a4c7f0 496 * Write long
bborredon 3:925096a4c7f0 497 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 498 * @param data long to write (int32_t)
bborredon 3:925096a4c7f0 499 * @return none
bborredon 3:925096a4c7f0 500 */
bborredon 1:a262173cac81 501 void EEPROM::write(uint32_t address, int32_t data)
bborredon 0:80245aff63ce 502 {
bborredon 0:80245aff63ce 503 int8_t cmd[4];
bborredon 3:925096a4c7f0 504
bborredon 0:80245aff63ce 505 // Check error
bborredon 0:80245aff63ce 506 if(_errnum)
bborredon 0:80245aff63ce 507 return;
bborredon 0:80245aff63ce 508
bborredon 0:80245aff63ce 509 // Check address
bborredon 0:80245aff63ce 510 if(!checkAddress(address + 3)) {
bborredon 0:80245aff63ce 511 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 512 return;
bborredon 0:80245aff63ce 513 }
bborredon 0:80245aff63ce 514
bborredon 0:80245aff63ce 515 memcpy(cmd,&data,4);
bborredon 0:80245aff63ce 516
bborredon 0:80245aff63ce 517 write(address,cmd,4);
bborredon 0:80245aff63ce 518
bborredon 0:80245aff63ce 519 }
bborredon 0:80245aff63ce 520
bborredon 3:925096a4c7f0 521 /**
bborredon 3:925096a4c7f0 522 * void write(uint32_t address, float data)
bborredon 3:925096a4c7f0 523 *
bborredon 3:925096a4c7f0 524 * Write float
bborredon 3:925096a4c7f0 525 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 526 * @param data float to write (float)
bborredon 3:925096a4c7f0 527 * @return none
bborredon 3:925096a4c7f0 528 */
bborredon 1:a262173cac81 529 void EEPROM::write(uint32_t address, float data)
bborredon 0:80245aff63ce 530 {
bborredon 0:80245aff63ce 531 int8_t cmd[4];
bborredon 3:925096a4c7f0 532
bborredon 0:80245aff63ce 533 // Check error
bborredon 0:80245aff63ce 534 if(_errnum)
bborredon 0:80245aff63ce 535 return;
bborredon 0:80245aff63ce 536
bborredon 0:80245aff63ce 537 // Check address
bborredon 0:80245aff63ce 538 if(!checkAddress(address + 3)) {
bborredon 0:80245aff63ce 539 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 540 return;
bborredon 0:80245aff63ce 541 }
bborredon 0:80245aff63ce 542
bborredon 0:80245aff63ce 543 memcpy(cmd,&data,4);
bborredon 0:80245aff63ce 544
bborredon 0:80245aff63ce 545 write(address,cmd,4);
bborredon 0:80245aff63ce 546
bborredon 0:80245aff63ce 547 }
bborredon 0:80245aff63ce 548
bborredon 3:925096a4c7f0 549 /**
bborredon 3:925096a4c7f0 550 * void write(uint32_t address, void *data, uint32_t size)
bborredon 3:925096a4c7f0 551 *
bborredon 3:925096a4c7f0 552 * Write anything (use the page write mode)
bborredon 3:925096a4c7f0 553 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 554 * @param data data to write (void *)
bborredon 3:925096a4c7f0 555 * @param size number of bytes to write (uint32_t)
bborredon 3:925096a4c7f0 556 * @return none
bborredon 3:925096a4c7f0 557 */
bborredon 1:a262173cac81 558 void EEPROM::write(uint32_t address, void *data, uint32_t size)
bborredon 0:80245aff63ce 559 {
bborredon 0:80245aff63ce 560 int8_t *cmd = NULL;
bborredon 3:925096a4c7f0 561
bborredon 0:80245aff63ce 562 // Check error
bborredon 0:80245aff63ce 563 if(_errnum)
bborredon 0:80245aff63ce 564 return;
bborredon 0:80245aff63ce 565
bborredon 0:80245aff63ce 566 // Check address
bborredon 0:80245aff63ce 567 if(!checkAddress(address + size - 1)) {
bborredon 0:80245aff63ce 568 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 569 return;
bborredon 0:80245aff63ce 570 }
bborredon 0:80245aff63ce 571
bborredon 0:80245aff63ce 572 cmd = (int8_t *)malloc(size);
bborredon 0:80245aff63ce 573 if(cmd == NULL) {
bborredon 0:80245aff63ce 574 _errnum = EEPROM_MallocError;
bborredon 0:80245aff63ce 575 return;
bborredon 0:80245aff63ce 576 }
bborredon 0:80245aff63ce 577
bborredon 1:a262173cac81 578 memcpy(cmd,(uint8_t *)data,size);
bborredon 0:80245aff63ce 579
bborredon 0:80245aff63ce 580 write(address,cmd,size);
bborredon 1:a262173cac81 581
bborredon 0:80245aff63ce 582 free(cmd);
bborredon 0:80245aff63ce 583
bborredon 0:80245aff63ce 584 }
bborredon 0:80245aff63ce 585
bborredon 3:925096a4c7f0 586 /**
bborredon 3:925096a4c7f0 587 * void read(uint32_t address, int8_t& data)
bborredon 3:925096a4c7f0 588 *
bborredon 3:925096a4c7f0 589 * Random read byte
bborredon 3:925096a4c7f0 590 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 591 * @param data byte to read (int8_t&)
bborredon 3:925096a4c7f0 592 * @return none
bborredon 3:925096a4c7f0 593 */
bborredon 1:a262173cac81 594 void EEPROM::read(uint32_t address, int8_t& data)
bborredon 0:80245aff63ce 595 {
bborredon 0:80245aff63ce 596 uint8_t page;
bborredon 0:80245aff63ce 597 uint8_t addr;
bborredon 0:80245aff63ce 598 uint8_t cmd[2];
bborredon 0:80245aff63ce 599 uint8_t len;
bborredon 0:80245aff63ce 600 int ack;
bborredon 3:925096a4c7f0 601
bborredon 0:80245aff63ce 602 // Check error
bborredon 0:80245aff63ce 603 if(_errnum)
bborredon 0:80245aff63ce 604 return;
bborredon 0:80245aff63ce 605
bborredon 0:80245aff63ce 606 // Check address
bborredon 0:80245aff63ce 607 if(!checkAddress(address)) {
bborredon 0:80245aff63ce 608 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 609 return;
bborredon 0:80245aff63ce 610 }
bborredon 3:925096a4c7f0 611
bborredon 0:80245aff63ce 612 // Compute page number
bborredon 0:80245aff63ce 613 page = 0;
bborredon 0:80245aff63ce 614 if(_type < T24C32)
bborredon 0:80245aff63ce 615 page = (uint8_t) (address / 256);
bborredon 0:80245aff63ce 616
bborredon 0:80245aff63ce 617 // Device address
bborredon 0:80245aff63ce 618 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 619
bborredon 0:80245aff63ce 620 if(_type < T24C32) {
bborredon 0:80245aff63ce 621 len = 1;
bborredon 0:80245aff63ce 622
bborredon 0:80245aff63ce 623 // Word address
bborredon 0:80245aff63ce 624 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 625 }
bborredon 0:80245aff63ce 626 else {
bborredon 0:80245aff63ce 627 len = 2;
bborredon 0:80245aff63ce 628
bborredon 0:80245aff63ce 629 // First word address (MSB)
bborredon 0:80245aff63ce 630 cmd[0] = (uint8_t) (address >> 8);
bborredon 0:80245aff63ce 631
bborredon 0:80245aff63ce 632 // Second word address (LSB)
bborredon 0:80245aff63ce 633 cmd[1] = (uint8_t)address;
bborredon 0:80245aff63ce 634 }
bborredon 0:80245aff63ce 635
bborredon 0:80245aff63ce 636 // Write command
bborredon 0:80245aff63ce 637 ack = _i2c.write((int)addr,(char *)cmd,len,true);
bborredon 0:80245aff63ce 638 if(ack != 0) {
bborredon 0:80245aff63ce 639 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 640 return;
bborredon 0:80245aff63ce 641 }
bborredon 0:80245aff63ce 642
bborredon 0:80245aff63ce 643 // Read data
bborredon 0:80245aff63ce 644 ack = _i2c.read((int)addr,(char *)&data,sizeof(data));
bborredon 0:80245aff63ce 645 if(ack != 0) {
bborredon 0:80245aff63ce 646 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 647 return;
bborredon 0:80245aff63ce 648 }
bborredon 0:80245aff63ce 649
bborredon 0:80245aff63ce 650 }
bborredon 0:80245aff63ce 651
bborredon 3:925096a4c7f0 652 /**
bborredon 3:925096a4c7f0 653 * void read(uint32_t address, int8_t *data, uint32_t size)
bborredon 3:925096a4c7f0 654 *
bborredon 3:925096a4c7f0 655 * Sequential read byte
bborredon 3:925096a4c7f0 656 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 657 * @param data bytes array to read (int8_t[]&)
bborredon 3:925096a4c7f0 658 * @param size number of bytes to read (uint32_t)
bborredon 3:925096a4c7f0 659 * @return none
bborredon 3:925096a4c7f0 660 */
bborredon 1:a262173cac81 661 void EEPROM::read(uint32_t address, int8_t *data, uint32_t size)
bborredon 0:80245aff63ce 662 {
bborredon 0:80245aff63ce 663 uint8_t page;
bborredon 0:80245aff63ce 664 uint8_t addr;
bborredon 0:80245aff63ce 665 uint8_t cmd[2];
bborredon 0:80245aff63ce 666 uint8_t len;
bborredon 0:80245aff63ce 667 int ack;
bborredon 3:925096a4c7f0 668
bborredon 0:80245aff63ce 669 // Check error
bborredon 0:80245aff63ce 670 if(_errnum)
bborredon 0:80245aff63ce 671 return;
bborredon 0:80245aff63ce 672
bborredon 0:80245aff63ce 673 // Check address
bborredon 0:80245aff63ce 674 if(!checkAddress(address)) {
bborredon 0:80245aff63ce 675 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 676 return;
bborredon 0:80245aff63ce 677 }
bborredon 0:80245aff63ce 678
bborredon 0:80245aff63ce 679 // Check size
bborredon 0:80245aff63ce 680 if(!checkAddress(address + size - 1)) {
bborredon 0:80245aff63ce 681 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 682 return;
bborredon 0:80245aff63ce 683 }
bborredon 0:80245aff63ce 684
bborredon 0:80245aff63ce 685 // Compute page number
bborredon 0:80245aff63ce 686 page = 0;
bborredon 0:80245aff63ce 687 if(_type < T24C32)
bborredon 0:80245aff63ce 688 page = (uint8_t) (address / 256);
bborredon 0:80245aff63ce 689
bborredon 0:80245aff63ce 690 // Device address
bborredon 0:80245aff63ce 691 addr = EEPROM_Address | _address | (page << 1);
bborredon 0:80245aff63ce 692
bborredon 0:80245aff63ce 693 if(_type < T24C32) {
bborredon 0:80245aff63ce 694 len = 1;
bborredon 0:80245aff63ce 695
bborredon 0:80245aff63ce 696 // Word address
bborredon 0:80245aff63ce 697 cmd[0] = (uint8_t) (address - page * 256);
bborredon 0:80245aff63ce 698 }
bborredon 0:80245aff63ce 699 else {
bborredon 0:80245aff63ce 700 len = 2;
bborredon 0:80245aff63ce 701
bborredon 0:80245aff63ce 702 // First word address (MSB)
bborredon 0:80245aff63ce 703 cmd[0] = (uint8_t) (address >> 8);
bborredon 0:80245aff63ce 704
bborredon 0:80245aff63ce 705 // Second word address (LSB)
bborredon 0:80245aff63ce 706 cmd[1] = (uint8_t) address;
bborredon 0:80245aff63ce 707 }
bborredon 0:80245aff63ce 708
bborredon 0:80245aff63ce 709 // Write command
bborredon 0:80245aff63ce 710 ack = _i2c.write((int)addr,(char *)cmd,len,true);
bborredon 0:80245aff63ce 711 if(ack != 0) {
bborredon 0:80245aff63ce 712 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 713 return;
bborredon 0:80245aff63ce 714 }
bborredon 0:80245aff63ce 715
bborredon 0:80245aff63ce 716 // Sequential read
bborredon 0:80245aff63ce 717 ack = _i2c.read((int)addr,(char *)data,size);
bborredon 0:80245aff63ce 718 if(ack != 0) {
bborredon 0:80245aff63ce 719 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 720 return;
bborredon 0:80245aff63ce 721 }
bborredon 0:80245aff63ce 722
bborredon 0:80245aff63ce 723 }
bborredon 0:80245aff63ce 724
bborredon 3:925096a4c7f0 725 /**
bborredon 3:925096a4c7f0 726 * void read(int8_t& data)
bborredon 3:925096a4c7f0 727 *
bborredon 3:925096a4c7f0 728 * Current address read byte
bborredon 3:925096a4c7f0 729 * @param data byte to read (int8_t&)
bborredon 3:925096a4c7f0 730 * @return none
bborredon 3:925096a4c7f0 731 */
bborredon 0:80245aff63ce 732 void EEPROM::read(int8_t& data)
bborredon 0:80245aff63ce 733 {
bborredon 0:80245aff63ce 734 uint8_t addr;
bborredon 0:80245aff63ce 735 int ack;
bborredon 3:925096a4c7f0 736
bborredon 0:80245aff63ce 737 // Check error
bborredon 0:80245aff63ce 738 if(_errnum)
bborredon 0:80245aff63ce 739 return;
bborredon 0:80245aff63ce 740
bborredon 0:80245aff63ce 741 // Device address
bborredon 0:80245aff63ce 742 addr = EEPROM_Address | _address;
bborredon 0:80245aff63ce 743
bborredon 0:80245aff63ce 744 // Read data
bborredon 0:80245aff63ce 745 ack = _i2c.read((int)addr,(char *)&data,sizeof(data));
bborredon 0:80245aff63ce 746 if(ack != 0) {
bborredon 0:80245aff63ce 747 _errnum = EEPROM_I2cError;
bborredon 0:80245aff63ce 748 return;
bborredon 0:80245aff63ce 749 }
bborredon 0:80245aff63ce 750
bborredon 0:80245aff63ce 751 }
bborredon 0:80245aff63ce 752
bborredon 3:925096a4c7f0 753 /**
bborredon 3:925096a4c7f0 754 * void read(uint32_t address, int16_t& data)
bborredon 3:925096a4c7f0 755 *
bborredon 3:925096a4c7f0 756 * Random read short
bborredon 3:925096a4c7f0 757 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 758 * @param data short to read (int16_t&)
bborredon 3:925096a4c7f0 759 * @return none
bborredon 3:925096a4c7f0 760 */
bborredon 1:a262173cac81 761 void EEPROM::read(uint32_t address, int16_t& data)
bborredon 0:80245aff63ce 762 {
bborredon 0:80245aff63ce 763 int8_t cmd[2];
bborredon 3:925096a4c7f0 764
bborredon 0:80245aff63ce 765 // Check error
bborredon 0:80245aff63ce 766 if(_errnum)
bborredon 0:80245aff63ce 767 return;
bborredon 0:80245aff63ce 768
bborredon 0:80245aff63ce 769 // Check address
bborredon 0:80245aff63ce 770 if(!checkAddress(address + 1)) {
bborredon 0:80245aff63ce 771 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 772 return;
bborredon 0:80245aff63ce 773 }
bborredon 0:80245aff63ce 774
bborredon 0:80245aff63ce 775 read(address,cmd,2);
bborredon 0:80245aff63ce 776
bborredon 0:80245aff63ce 777 memcpy(&data,cmd,2);
bborredon 0:80245aff63ce 778
bborredon 0:80245aff63ce 779 }
bborredon 0:80245aff63ce 780
bborredon 3:925096a4c7f0 781 /**
bborredon 3:925096a4c7f0 782 * void read(uint32_t address, int32_t& data)
bborredon 3:925096a4c7f0 783 *
bborredon 3:925096a4c7f0 784 * Random read long
bborredon 3:925096a4c7f0 785 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 786 * @param data long to read (int32_t&)
bborredon 3:925096a4c7f0 787 * @return none
bborredon 3:925096a4c7f0 788 */
bborredon 1:a262173cac81 789 void EEPROM::read(uint32_t address, int32_t& data)
bborredon 0:80245aff63ce 790 {
bborredon 0:80245aff63ce 791 int8_t cmd[4];
bborredon 3:925096a4c7f0 792
bborredon 0:80245aff63ce 793 // Check error
bborredon 0:80245aff63ce 794 if(_errnum)
bborredon 0:80245aff63ce 795 return;
bborredon 0:80245aff63ce 796
bborredon 0:80245aff63ce 797 // Check address
bborredon 0:80245aff63ce 798 if(!checkAddress(address + 3)) {
bborredon 0:80245aff63ce 799 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 800 return;
bborredon 0:80245aff63ce 801 }
bborredon 0:80245aff63ce 802
bborredon 0:80245aff63ce 803 read(address,cmd,4);
bborredon 0:80245aff63ce 804
bborredon 0:80245aff63ce 805 memcpy(&data,cmd,4);
bborredon 0:80245aff63ce 806
bborredon 0:80245aff63ce 807 }
bborredon 0:80245aff63ce 808
bborredon 3:925096a4c7f0 809 /**
bborredon 3:925096a4c7f0 810 * void read(uint32_t address, float& data)
bborredon 3:925096a4c7f0 811 *
bborredon 3:925096a4c7f0 812 * Random read float
bborredon 3:925096a4c7f0 813 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 814 * @param data float to read (float&)
bborredon 3:925096a4c7f0 815 * @return none
bborredon 3:925096a4c7f0 816 */
bborredon 1:a262173cac81 817 void EEPROM::read(uint32_t address, float& data)
bborredon 0:80245aff63ce 818 {
bborredon 0:80245aff63ce 819 int8_t cmd[4];
bborredon 3:925096a4c7f0 820
bborredon 0:80245aff63ce 821 // Check error
bborredon 0:80245aff63ce 822 if(_errnum)
bborredon 0:80245aff63ce 823 return;
bborredon 0:80245aff63ce 824
bborredon 0:80245aff63ce 825 // Check address
bborredon 0:80245aff63ce 826 if(!checkAddress(address + 3)) {
bborredon 0:80245aff63ce 827 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 828 return;
bborredon 0:80245aff63ce 829 }
bborredon 0:80245aff63ce 830
bborredon 0:80245aff63ce 831 read(address,cmd,4);
bborredon 0:80245aff63ce 832
bborredon 0:80245aff63ce 833 memcpy(&data,cmd,4);
bborredon 0:80245aff63ce 834
bborredon 0:80245aff63ce 835 }
bborredon 0:80245aff63ce 836
bborredon 3:925096a4c7f0 837 /**
bborredon 3:925096a4c7f0 838 * void read(uint32_t address, void *data, uint32_t size)
bborredon 3:925096a4c7f0 839 *
bborredon 3:925096a4c7f0 840 * Random read anything
bborredon 3:925096a4c7f0 841 * @param address start address (uint32_t)
bborredon 3:925096a4c7f0 842 * @param data data to read (void *)
bborredon 3:925096a4c7f0 843 * @param size number of bytes to read (uint32_t)
bborredon 3:925096a4c7f0 844 * @return none
bborredon 3:925096a4c7f0 845 */
bborredon 1:a262173cac81 846 void EEPROM::read(uint32_t address, void *data, uint32_t size)
bborredon 0:80245aff63ce 847 {
bborredon 0:80245aff63ce 848 int8_t *cmd = NULL;
bborredon 1:a262173cac81 849
bborredon 0:80245aff63ce 850 // Check error
bborredon 0:80245aff63ce 851 if(_errnum)
bborredon 0:80245aff63ce 852 return;
bborredon 0:80245aff63ce 853
bborredon 0:80245aff63ce 854 // Check address
bborredon 0:80245aff63ce 855 if(!checkAddress(address + size - 1)) {
bborredon 0:80245aff63ce 856 _errnum = EEPROM_OutOfRange;
bborredon 0:80245aff63ce 857 return;
bborredon 0:80245aff63ce 858 }
bborredon 0:80245aff63ce 859
bborredon 0:80245aff63ce 860 cmd = (int8_t *)malloc(size);
bborredon 1:a262173cac81 861
bborredon 0:80245aff63ce 862 if(cmd == NULL) {
bborredon 0:80245aff63ce 863 _errnum = EEPROM_MallocError;
bborredon 0:80245aff63ce 864 return;
bborredon 0:80245aff63ce 865 }
bborredon 0:80245aff63ce 866
bborredon 1:a262173cac81 867 read(address,(int8_t *)cmd,size);
bborredon 0:80245aff63ce 868
bborredon 0:80245aff63ce 869 memcpy(data,cmd,size);
bborredon 0:80245aff63ce 870
bborredon 0:80245aff63ce 871 free(cmd);
bborredon 0:80245aff63ce 872
bborredon 0:80245aff63ce 873 }
bborredon 0:80245aff63ce 874
bborredon 3:925096a4c7f0 875 /**
bborredon 3:925096a4c7f0 876 * void clear(void)
bborredon 3:925096a4c7f0 877 *
bborredon 3:925096a4c7f0 878 * Clear eeprom (write with 0)
bborredon 3:925096a4c7f0 879 * @param none
bborredon 3:925096a4c7f0 880 * @return none
bborredon 3:925096a4c7f0 881 */
bborredon 1:a262173cac81 882 void EEPROM::clear(void)
bborredon 1:a262173cac81 883 {
bborredon 1:a262173cac81 884 int32_t data;
bborredon 1:a262173cac81 885 uint32_t i;
bborredon 1:a262173cac81 886
bborredon 1:a262173cac81 887 data = 0;
bborredon 1:a262173cac81 888
bborredon 1:a262173cac81 889 for(i = 0; i < _size / 4;i++) {
bborredon 1:a262173cac81 890 write((uint32_t)(i * 4),data);
bborredon 1:a262173cac81 891 }
bborredon 1:a262173cac81 892 }
bborredon 1:a262173cac81 893
bborredon 3:925096a4c7f0 894 /**
bborredon 3:925096a4c7f0 895 * void ready(void)
bborredon 3:925096a4c7f0 896 *
bborredon 3:925096a4c7f0 897 * Wait eeprom ready
bborredon 3:925096a4c7f0 898 * @param none
bborredon 3:925096a4c7f0 899 * @return none
bborredon 3:925096a4c7f0 900 */
bborredon 0:80245aff63ce 901 void EEPROM::ready(void)
bborredon 0:80245aff63ce 902 {
bborredon 0:80245aff63ce 903 int ack;
bborredon 0:80245aff63ce 904 uint8_t addr;
bborredon 1:a262173cac81 905 uint8_t cmd[2];
bborredon 0:80245aff63ce 906
bborredon 0:80245aff63ce 907 // Check error
bborredon 0:80245aff63ce 908 if(_errnum)
bborredon 0:80245aff63ce 909 return;
bborredon 0:80245aff63ce 910
bborredon 0:80245aff63ce 911 // Device address
bborredon 0:80245aff63ce 912 addr = EEPROM_Address | _address;
bborredon 0:80245aff63ce 913
bborredon 1:a262173cac81 914 cmd[0] = 0;
bborredon 0:80245aff63ce 915
bborredon 0:80245aff63ce 916 // Wait end of write
bborredon 0:80245aff63ce 917 do {
bborredon 0:80245aff63ce 918 ack = _i2c.write((int)addr,(char *)cmd,0);
bborredon 1:a262173cac81 919 //wait(0.5);
bborredon 0:80245aff63ce 920 } while(ack != 0);
bborredon 0:80245aff63ce 921
bborredon 0:80245aff63ce 922 }
bborredon 0:80245aff63ce 923
bborredon 3:925096a4c7f0 924 /**
bborredon 3:925096a4c7f0 925 * uint32_t getSize(void)
bborredon 3:925096a4c7f0 926 *
bborredon 3:925096a4c7f0 927 * Get eeprom size in bytes
bborredon 3:925096a4c7f0 928 * @param none
bborredon 3:925096a4c7f0 929 * @return size in bytes (uint32_t)
bborredon 3:925096a4c7f0 930 */
bborredon 0:80245aff63ce 931 uint32_t EEPROM::getSize(void)
bborredon 0:80245aff63ce 932 {
bborredon 0:80245aff63ce 933 return(_size);
bborredon 0:80245aff63ce 934 }
bborredon 0:80245aff63ce 935
bborredon 3:925096a4c7f0 936 /**
bborredon 3:925096a4c7f0 937 * const char* getName(void)
bborredon 3:925096a4c7f0 938 *
bborredon 3:925096a4c7f0 939 * Get eeprom name
bborredon 3:925096a4c7f0 940 * @param none
bborredon 3:925096a4c7f0 941 * @return name (const char*)
bborredon 3:925096a4c7f0 942 */
bborredon 1:a262173cac81 943 const char* EEPROM::getName(void)
bborredon 1:a262173cac81 944 {
bborredon 1:a262173cac81 945 uint8_t i = 0;
bborredon 1:a262173cac81 946
bborredon 1:a262173cac81 947 switch(_type) {
bborredon 1:a262173cac81 948 case T24C01 :
bborredon 1:a262173cac81 949 i = 0;
bborredon 1:a262173cac81 950 break;
bborredon 1:a262173cac81 951 case T24C02 :
bborredon 1:a262173cac81 952 i = 1;
bborredon 1:a262173cac81 953 break;
bborredon 1:a262173cac81 954 case T24C04 :
bborredon 1:a262173cac81 955 i = 2;
bborredon 1:a262173cac81 956 break;
bborredon 1:a262173cac81 957 case T24C08 :
bborredon 1:a262173cac81 958 i = 3;
bborredon 1:a262173cac81 959 break;
bborredon 1:a262173cac81 960 case T24C16 :
bborredon 1:a262173cac81 961 i = 4;
bborredon 1:a262173cac81 962 break;
bborredon 1:a262173cac81 963 case T24C32 :
bborredon 1:a262173cac81 964 i = 5;
bborredon 1:a262173cac81 965 break;
bborredon 1:a262173cac81 966 case T24C64 :
bborredon 1:a262173cac81 967 i = 6;
bborredon 1:a262173cac81 968 break;
bborredon 1:a262173cac81 969 case T24C128 :
bborredon 1:a262173cac81 970 i = 7;
bborredon 1:a262173cac81 971 break;
bborredon 1:a262173cac81 972 case T24C256 :
bborredon 1:a262173cac81 973 i = 8;
bborredon 1:a262173cac81 974 break;
bborredon 1:a262173cac81 975 case T24C512 :
bborredon 1:a262173cac81 976 i = 9;
bborredon 1:a262173cac81 977 break;
bborredon 1:a262173cac81 978 case T24C1024 :
bborredon 1:a262173cac81 979 i = 10;
bborredon 1:a262173cac81 980 break;
bborredon 1:a262173cac81 981 case T24C1025 :
bborredon 1:a262173cac81 982 i = 11;
bborredon 1:a262173cac81 983 break;
bborredon 1:a262173cac81 984 }
bborredon 1:a262173cac81 985
bborredon 1:a262173cac81 986 return(_name[i]);
bborredon 1:a262173cac81 987 }
bborredon 1:a262173cac81 988
bborredon 3:925096a4c7f0 989 /**
bborredon 3:925096a4c7f0 990 * uint8_t getError(void)
bborredon 3:925096a4c7f0 991 *
bborredon 3:925096a4c7f0 992 * Get the current error number (EEPROM_NoError if no error)
bborredon 3:925096a4c7f0 993 * @param none
bborredon 3:925096a4c7f0 994 * @return none
bborredon 3:925096a4c7f0 995 */
bborredon 0:80245aff63ce 996 uint8_t EEPROM::getError(void)
bborredon 0:80245aff63ce 997 {
bborredon 0:80245aff63ce 998 return(_errnum);
bborredon 0:80245aff63ce 999 }
bborredon 0:80245aff63ce 1000
bborredon 3:925096a4c7f0 1001 /**
bborredon 3:925096a4c7f0 1002 * bool checkAddress(uint32_t address)
bborredon 3:925096a4c7f0 1003 *
bborredon 3:925096a4c7f0 1004 * Check if address is in the eeprom range address
bborredon 3:925096a4c7f0 1005 * @param address address to check (uint32_t)
bborredon 3:925096a4c7f0 1006 * @return true if in eeprom range, overwise false (bool)
bborredon 3:925096a4c7f0 1007 */
bborredon 1:a262173cac81 1008 bool EEPROM::checkAddress(uint32_t address)
bborredon 0:80245aff63ce 1009 {
bborredon 0:80245aff63ce 1010 bool ret = true;
bborredon 0:80245aff63ce 1011
bborredon 0:80245aff63ce 1012 switch(_type) {
bborredon 0:80245aff63ce 1013 case T24C01 :
bborredon 0:80245aff63ce 1014 if(address >= T24C01)
bborredon 0:80245aff63ce 1015 ret = false;
bborredon 0:80245aff63ce 1016 break;
bborredon 0:80245aff63ce 1017 case T24C02 :
bborredon 0:80245aff63ce 1018 if(address >= T24C02)
bborredon 0:80245aff63ce 1019 ret = false;
bborredon 0:80245aff63ce 1020 break;
bborredon 0:80245aff63ce 1021 case T24C04 :
bborredon 0:80245aff63ce 1022 if(address >= T24C04)
bborredon 0:80245aff63ce 1023 ret = false;
bborredon 0:80245aff63ce 1024 break;
bborredon 0:80245aff63ce 1025 case T24C08 :
bborredon 0:80245aff63ce 1026 if(address >= T24C08)
bborredon 0:80245aff63ce 1027 ret = false;
bborredon 0:80245aff63ce 1028 break;
bborredon 0:80245aff63ce 1029 case T24C16 :
bborredon 0:80245aff63ce 1030 if(address >= T24C16)
bborredon 0:80245aff63ce 1031 ret = false;
bborredon 0:80245aff63ce 1032 break;
bborredon 0:80245aff63ce 1033 case T24C32 :
bborredon 0:80245aff63ce 1034 if(address >= T24C32)
bborredon 0:80245aff63ce 1035 ret = false;
bborredon 0:80245aff63ce 1036 break;
bborredon 0:80245aff63ce 1037 case T24C64 :
bborredon 0:80245aff63ce 1038 if(address >= T24C64)
bborredon 0:80245aff63ce 1039 ret = false;
bborredon 0:80245aff63ce 1040 break;
bborredon 0:80245aff63ce 1041 case T24C128 :
bborredon 0:80245aff63ce 1042 if(address >= T24C128)
bborredon 0:80245aff63ce 1043 ret = false;
bborredon 0:80245aff63ce 1044 break;
bborredon 0:80245aff63ce 1045 case T24C256 :
bborredon 0:80245aff63ce 1046 if(address >= T24C256)
bborredon 0:80245aff63ce 1047 ret = false;
bborredon 0:80245aff63ce 1048 break;
bborredon 0:80245aff63ce 1049 case T24C512 :
bborredon 0:80245aff63ce 1050 if(address >= T24C512)
bborredon 0:80245aff63ce 1051 ret = false;
bborredon 0:80245aff63ce 1052 break;
bborredon 0:80245aff63ce 1053 case T24C1024 :
bborredon 0:80245aff63ce 1054 if(address >= T24C1024)
bborredon 0:80245aff63ce 1055 ret = false;
bborredon 0:80245aff63ce 1056 break;
bborredon 0:80245aff63ce 1057 case T24C1025 :
bborredon 0:80245aff63ce 1058 if(address >= T24C1025 - 1)
bborredon 0:80245aff63ce 1059 ret = false;
bborredon 0:80245aff63ce 1060 break;
bborredon 0:80245aff63ce 1061 }
bborredon 0:80245aff63ce 1062
bborredon 0:80245aff63ce 1063 return(ret);
bborredon 1:a262173cac81 1064 }