se quito el led de debug de la bibilioteca

Fork of eeprom_1 by JONIX SA

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