se quito el led de debug de la bibilioteca

Fork of eeprom_1 by JONIX SA

Committer:
juanc95
Date:
Thu Oct 18 17:44:50 2018 +0000
Revision:
8:e3e847a4a95d
Parent:
7:cf6096a818b7
ajustes a eepromlib

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