Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
24LCxx_I2C.cpp
00001 /* mbed simplified access to Microchip 24LCxx Serial EEPROM devices (I2C) 00002 * Copyright (c) 2010-2012 ygarcia, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 #include <iostream> 00020 #include <sstream> 00021 00022 #include "24LCxx_I2C.h" 00023 00024 namespace _24LCXX_I2C { 00025 00026 unsigned char C24LCXX_I2C::I2CModuleRefCounter = 0; 00027 00028 C24LCXX_I2C::C24LCXX_I2C(const PinName p_sda, const PinName p_scl, const unsigned char p_address, const PinName p_wp, const unsigned int p_frequency) : _internalId("") { 00029 DEBUG_ENTER("C24LCXX_I2C") 00030 00031 if (C24LCXX_I2C::I2CModuleRefCounter != 0) { 00032 error("C24LCXX_I2C: Wrong params"); 00033 } 00034 #ifdef __DEBUG 00035 std::ostringstream out(std::ostringstream::out); 00036 out << "C24LCXX_I2C #" << C24LCXX_I2C::I2CModuleRefCounter; 00037 _internalId.assign(out.str()); 00038 DEBUG("C24LCXX_I2C: _internalId='%s'", _internalId.c_str()) 00039 #endif // __DEBUG 00040 _i2cInstance = new I2C(p_sda, p_scl, "24LCxx_I2C"); 00041 C24LCXX_I2C::I2CModuleRefCounter += 1; 00042 DEBUG_ENTER("C24LCXX_I2C: refCounter=%d", C24LCXX_I2C::I2CModuleRefCounter) 00043 00044 _slaveAddress = (p_address << 1) | 0xa0; // Slave address format is: 1 0 1 0 A3 A2 A1 R/W 00045 DEBUG("C24LCXX_I2C: I2C slave adress: 0x%02x", _slaveAddress) 00046 _i2cInstance->frequency(p_frequency); // Set the frequency of the I2C interface 00047 if (p_wp != NC) { 00048 DEBUG("C24LCXX_I2C: WP managed"); 00049 _wp = new DigitalOut(p_wp); 00050 _wp->write(0); // Disable write protect 00051 } else { 00052 DEBUG("C24LCXX_I2C: WP not managed"); 00053 _wp = NULL; // Not used 00054 } 00055 00056 DEBUG_LEAVE("C24LCXX_I2C") 00057 } 00058 00059 C24LCXX_I2C::~C24LCXX_I2C() { 00060 DEBUG_ENTER("~C24LCXX_I2C") 00061 00062 // Release I2C instance 00063 DEBUG_ENTER("~C24LCXX_I2C: refCounter=%d", C24LCXX_I2C::I2CModuleRefCounter) 00064 C24LCXX_I2C::I2CModuleRefCounter -= 1; 00065 if (C24LCXX_I2C::I2CModuleRefCounter == 0) { 00066 delete _i2cInstance; 00067 _i2cInstance = NULL; 00068 } 00069 // Release _wp if required 00070 if (_wp != NULL) { 00071 _wp->write(0); 00072 delete _wp; 00073 } 00074 00075 DEBUG_LEAVE("~C24LCXX_I2C") 00076 } 00077 00078 bool C24LCXX_I2C::WriteProtect(const bool p_writeProtect) { 00079 if (_wp != NULL) { 00080 DEBUG("WP set to: %x", (int)p_writeProtect) 00081 _wp->write((int)(p_writeProtect)); 00082 return true; 00083 } 00084 00085 return false; 00086 } 00087 00088 bool C24LCXX_I2C::EraseMemoryArea(const short p_startAddress, const int p_count, const unsigned char p_pattern) { 00089 DEBUG_ENTER("C24LCXX_I2C::EraseMemoryArea: 0x%02x - %d - 0x%02x", p_startAddress, p_count, p_pattern) 00090 00091 std::vector<unsigned char> eraseBuffer(p_count, p_pattern); 00092 return Write(p_startAddress, eraseBuffer, false); 00093 } 00094 00095 bool C24LCXX_I2C::Write(const short p_address, const unsigned char p_byte) { 00096 DEBUG_ENTER("C24LCXX_I2C::Write (byte): Memory address: 0x%02x - 0x%02x", p_address, p_byte) 00097 00098 // 1.Prepare buffer 00099 char i2cBuffer[3]; // Memory address + one byte of data 00100 // 1.1. Memory address 00101 short address = p_address + 1; // Index start to 1 00102 i2cBuffer[0] = (unsigned char)(address >> 8); 00103 DEBUG("C24LCXX_I2C::Write (byte): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) 00104 i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); 00105 DEBUG("C24LCXX_I2C::Write (byte): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) 00106 // 1.2. Datas 00107 i2cBuffer[2] = p_byte; 00108 DEBUG("C24LCXX_I2C::Write (byte): value=0x%02x", i2cBuffer[2]) 00109 00110 // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop 00111 int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 3); 00112 wait(0.02); 00113 00114 DEBUG_LEAVE("C24LCXX_I2C::Write (byte) %x", (bool)(result == 0)) 00115 return (bool)(result == 0); 00116 } 00117 00118 bool C24LCXX_I2C::Write(const short p_address, const short p_short, const C24LCXX_I2C::Mode p_mode) { 00119 DEBUG_ENTER("C24LCXX_I2C::Write (short): Memory address:0x%02x, Mode:%d", p_address, p_mode) 00120 00121 // 1.Prepare buffer 00122 char i2cBuffer[4]; // Memory address + one short (2 bytes) 00123 // 1.1. Memory address 00124 short address = p_address + 1; // Index start to 1 00125 i2cBuffer[0] = (unsigned char)(address >> 8); 00126 DEBUG("C24LCXX_I2C::Write (short): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) 00127 i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); 00128 DEBUG("C24LCXX_I2C::Write (short): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) 00129 // 1.2. Datas 00130 if (p_mode == BigEndian) { 00131 i2cBuffer[2] = (unsigned char)(p_short >> 8); 00132 i2cBuffer[3] = (unsigned char)((unsigned char)p_short & 0xff); 00133 } else { 00134 i2cBuffer[2] = (unsigned char)((unsigned char)p_short & 0xff); 00135 i2cBuffer[3] = (unsigned char)(p_short >> 8); 00136 } 00137 DEBUG("C24LCXX_I2C::Write (short): value=0x%02x%02x", i2cBuffer[2], i2cBuffer[3]) 00138 00139 // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop 00140 int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 4); 00141 wait(0.02); 00142 00143 DEBUG_LEAVE("C24LCXX_I2C::Write (short) %x", (bool)(result == 0)) 00144 return (bool)(result == 0); 00145 } 00146 00147 bool C24LCXX_I2C::Write(const short p_address, const int p_int, const C24LCXX_I2C::Mode p_mode) { 00148 DEBUG_ENTER("C24LCXX_I2C::Write (int): Memory address:0x%02x, Mode:%d", p_address, p_mode) 00149 00150 // 1.Prepare buffer 00151 char i2cBuffer[6]; // Memory address + one integer (4 bytes) 00152 // 1.1. Memory address 00153 short address = p_address + 1; // Index start to 1 00154 i2cBuffer[0] = (unsigned char)(address >> 8); 00155 DEBUG("C24LCXX_I2C::Write (int): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) 00156 i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); 00157 DEBUG("C24LCXX_I2C::Write (int): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) 00158 // 1.2. Datas 00159 if (p_mode == BigEndian) { 00160 i2cBuffer[2] = (unsigned char)(p_int >> 24); 00161 i2cBuffer[3] = (unsigned char)(p_int >> 16); 00162 i2cBuffer[4] = (unsigned char)(p_int >> 8); 00163 i2cBuffer[5] = (unsigned char)((unsigned char)p_int & 0xff); 00164 } else { 00165 i2cBuffer[2] = (unsigned char)((unsigned char)p_int & 0xff); 00166 i2cBuffer[3] = (unsigned char)(p_int >> 8); 00167 i2cBuffer[4] = (unsigned char)(p_int >> 16); 00168 i2cBuffer[5] = (unsigned char)(p_int >> 24); 00169 } 00170 DEBUG("C24LCXX_I2C::Write (int): value=0x%02x%02x%02x%02x", i2cBuffer[2], i2cBuffer[3], i2cBuffer[4], i2cBuffer[5]) 00171 00172 // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop 00173 int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 6); 00174 wait(0.02); 00175 00176 DEBUG_LEAVE("C24LCXX_I2C::Write (int) %x", (bool)(result == 0)) 00177 return (bool)(result == 0); 00178 } 00179 00180 bool C24LCXX_I2C::Write(const short p_address, const std::string & p_string, const bool p_storeLength, const int p_length2write) { 00181 DEBUG_ENTER("C24LCXX_I2C::Write (std::string)") 00182 return Write(p_address, p_string.c_str(), p_storeLength, p_length2write); 00183 } 00184 00185 bool C24LCXX_I2C::Write(const short p_address, const std::vector<unsigned char> & p_datas, const bool p_storeLength, const int p_length2write) { 00186 DEBUG_ENTER("C24LCXX_I2C::Write (std::vector)") 00187 00188 int length = (p_length2write == -1) ? p_datas.size() : p_length2write; 00189 unsigned char array[length]; 00190 std::copy(p_datas.begin(), p_datas.end(), array); 00191 bool result = Write(p_address, array, p_storeLength, length); 00192 wait(0.02); 00193 00194 DEBUG_LEAVE("C24LCXX_I2C::Write (std::vector): %d", result) 00195 return result; 00196 } 00197 00198 bool C24LCXX_I2C::Write(const short p_address, const char *p_datas, const bool p_storeLength, const int p_length2write) { 00199 DEBUG_ENTER("C24LCXX_I2C::Write (char *): Memory address: 0x%02x - %x - %d", p_address, p_storeLength, p_length2write) 00200 00201 // 1.Prepare buffer 00202 int length = (p_length2write == -1) ? strlen(p_datas) : p_length2write; 00203 if (p_storeLength) { 00204 length += 4; // Add four bytes for the length as integer 00205 } 00206 DEBUG("C24LCXX_I2C::Write (char *): length:%d", length) 00207 00208 char i2cBuffer[2 + length]; 00209 // 1.1. Memory address 00210 short address = p_address + 1; 00211 i2cBuffer[0] = (unsigned char)(address >> 8); 00212 DEBUG("C24LCXX_I2C::Write (char *): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) 00213 i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); 00214 DEBUG("C24LCXX_I2C::Write (char *): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) 00215 // 1.2. Datas 00216 if (p_storeLength) { 00217 // Fill the length 00218 i2cBuffer[2] = (unsigned char)(length >> 24); 00219 i2cBuffer[3] = (unsigned char)(length >> 16); 00220 i2cBuffer[4] = (unsigned char)(length >> 8); 00221 i2cBuffer[5] = (unsigned char)((unsigned char)length & 0xff); 00222 for (int i = 0; i < length - 4; i++) { 00223 i2cBuffer[6 + i] = *(p_datas + i); 00224 } 00225 } else { // The length was not stored 00226 for (int i = 0; i < length; i++) { 00227 i2cBuffer[2 + i] = *(p_datas + i); 00228 } 00229 } 00230 00231 // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop 00232 int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 2 + length); 00233 wait(0.02); 00234 00235 DEBUG_LEAVE("C24LCXX_I2C::Write (char *) %x", (bool)(result == 0)) 00236 return (bool)(result == 0); 00237 } 00238 00239 bool C24LCXX_I2C::Write(const short p_address, const unsigned char *p_datas, const bool p_storeLength, const int p_length2write) { 00240 DEBUG_ENTER("C24LCXX_I2C::Write (byte *): Memory address: 0x%02x - %x - %d", p_address, p_storeLength, p_length2write) 00241 return Write(p_address, (const char *)p_datas, p_storeLength, p_length2write); 00242 } 00243 00244 bool C24LCXX_I2C::Read(const short p_address, unsigned char * p_byte) { 00245 DEBUG_ENTER("C24LCXX_I2C::Read (byte): Memory address:0x%02x", p_address) 00246 00247 // 1.Prepare buffer 00248 char i2cBuffer[2]; 00249 // 1.1. Memory address 00250 i2cBuffer[0] = (unsigned char)(p_address >> 8); 00251 DEBUG("C24LCXX_I2C::Read (byte): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) 00252 i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); 00253 DEBUG("C24LCXX_I2C::Read (byte): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) 00254 00255 // 2. Send I2C start + memory address 00256 if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { 00257 wait(0.02); 00258 DEBUG("C24LCXX_I2C::Read (byte): Write memory done") 00259 // 2. Read data + I2C stop 00260 int result = _i2cInstance->read(_slaveAddress, (char *)p_byte, 1); 00261 wait(0.02); 00262 00263 DEBUG_LEAVE("C24LCXX_I2C::Read (byte): %x", (bool)(result == 0)) 00264 return (bool)(result == 0); 00265 } 00266 00267 DEBUG_LEAVE("C24LCXX_I2C::Read (byte) (false)") 00268 return false; 00269 } 00270 00271 bool C24LCXX_I2C::Read(const short p_address, short *p_short, const C24LCXX_I2C::Mode p_mode) { 00272 DEBUG_ENTER("C24LCXX_I2C::Read (short): Memory address:0x%02x, Mode:%d", p_address, p_mode) 00273 00274 // 1.Prepare buffer 00275 char i2cBuffer[2]; 00276 // 1.1. Memory address 00277 i2cBuffer[0] = (unsigned char)(p_address >> 8); 00278 DEBUG("C24LCXX_I2C::Read (short): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) 00279 i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); 00280 DEBUG("C24LCXX_I2C::Read (short): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) 00281 00282 // 2. Send I2C start + memory address 00283 if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { 00284 wait(0.02); 00285 DEBUG("C24LCXX_I2C::Read (short): Write memory done") 00286 // 2. Read data + I2C stop 00287 int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 2); 00288 if (result == 0) { 00289 DEBUG("C24LCXX_I2C::Read (short): value: 0x%02x - 0x%02x", i2cBuffer[0], i2cBuffer[1]) 00290 if (p_mode == BigEndian) { 00291 *p_short = (short)(i2cBuffer[0] << 8 | i2cBuffer[1]); 00292 } else { 00293 *p_short = (short)(i2cBuffer[1] << 8 | i2cBuffer[0]); 00294 } 00295 00296 DEBUG_LEAVE("C24LCXX_I2C::Read (short): 0x%04x", *p_short) 00297 return true; 00298 } 00299 } 00300 00301 DEBUG_LEAVE("C24LCXX_I2C::Read (short) (false)") 00302 return false; 00303 } 00304 00305 bool C24LCXX_I2C::Read(const short p_address, int *p_int, const C24LCXX_I2C::Mode p_mode) { 00306 DEBUG_ENTER("C24LCXX_I2C::Read (int): Memory address:0x%02x, Mode:%d", p_address, p_mode) 00307 00308 // 1.Prepare buffer 00309 char i2cBuffer[4]; 00310 // 1.1. Memory address 00311 i2cBuffer[0] = (unsigned char)(p_address >> 8); 00312 DEBUG("C24LCXX_I2C::Read (int): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) 00313 i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); 00314 DEBUG("C24LCXX_I2C::Read (int): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) 00315 00316 // 2. Send I2C start + memory address 00317 if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { 00318 wait(0.02); 00319 DEBUG("C24LCXX_I2C::Read (int): Write memory done") 00320 // 2. Read data + I2C stop 00321 int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 4); 00322 if (result == 0) { 00323 DEBUG("C24LCXX_I2C::Read (int): value: 0x%02x - 0x%02x - 0x%02x - 0x%02x", i2cBuffer[0], i2cBuffer[1], i2cBuffer[2], i2cBuffer[3]) 00324 wait(0.02); 00325 if (p_mode == BigEndian) { 00326 *p_int = (int)(i2cBuffer[0] << 24 | i2cBuffer[1] << 16 | i2cBuffer[2] << 8 | i2cBuffer[3]); 00327 } else { 00328 *p_int = (int)(i2cBuffer[3] << 24 | i2cBuffer[2] << 16 | i2cBuffer[1] << 8 | i2cBuffer[0]); 00329 } 00330 00331 DEBUG_LEAVE("C24LCXX_I2C::Read (int): %d", *p_int) 00332 return true; 00333 } 00334 00335 DEBUG_LEAVE("C24LCXX_I2C::Read (int):false") 00336 return false; 00337 } 00338 00339 DEBUG_LEAVE("C24LCXX_I2C::Read (int) (false)") 00340 return false; 00341 } 00342 00343 bool C24LCXX_I2C::Read(const short p_address, std::vector<unsigned char> & p_datas, const bool p_readLengthFirst, const int p_length2write) { 00344 DEBUG_ENTER("C24LCXX_I2C::Read (vector): Memory address:0x%02x, readLength:%01x, Length:%d", p_address, p_readLengthFirst, p_length2write) 00345 00346 // 1.Prepare buffer 00347 short address = p_address; 00348 int length = 0; 00349 if (p_readLengthFirst) { 00350 if (!Read(address, &length)) { // Read the length in big endian mode 00351 DEBUG_LEAVE("C24LCXX_I2C::Read (vector) Failed to read length") 00352 return false; 00353 } 00354 DEBUG("C24LCXX_I2C::Read (vector): length= %d", length) 00355 if (length == 0) { 00356 return true; 00357 } 00358 address += 4; // Skip the length value 00359 length -= 4; // length is the size of (string length + string) 00360 } else { 00361 if (p_length2write == -1) { 00362 length = p_datas.size(); 00363 } else { 00364 length = p_length2write; 00365 } 00366 } 00367 DEBUG("C24LCXX_I2C::Read (vector): length= %d", length) 00368 00369 // 2. Memory address 00370 char i2cBuffer[2]; 00371 i2cBuffer[0] = (unsigned char)(address >> 8); 00372 DEBUG("C24LCXX_I2C::Read (vector): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) 00373 i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); 00374 DEBUG("C24LCXX_I2C::Read (vector): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) 00375 00376 // 3. Send I2C start + memory address 00377 if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { 00378 wait(0.02); 00379 DEBUG("C24LCXX_I2C::Read (vector): Write memory done") 00380 // 4. read data + I2C stop 00381 unsigned char buffer[length]; 00382 int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length); 00383 wait(0.02); 00384 if (result == 0) { 00385 p_datas.assign(buffer, buffer + length); 00386 00387 DEBUG_LEAVE("C24LCXX_I2C::Read (vector): %x", (bool)(result == 0)) 00388 return (bool)(result == 0); 00389 } 00390 } 00391 00392 DEBUG_LEAVE("C24LCXX_I2C::Read (vector) (false)") 00393 return false; 00394 } 00395 00396 bool C24LCXX_I2C::Read(const short p_address, std::string & p_string, const bool p_readLengthFirst, const int p_length2write) { 00397 DEBUG_ENTER("C24LCXX_I2C::Read (string): Memory address:0x%02x, readLength:%01x, Length:%d", p_address, p_readLengthFirst, p_length2write) 00398 00399 /* std::vector<unsigned char> datas; 00400 if (Read(p_address, datas, p_readLengthFirst, p_length2write) == true) { 00401 p_string.assign((char *)datas.begin(), datas.size()); 00402 00403 return true; 00404 } 00405 00406 DEBUG_LEAVE("C24LCXX_I2C::Read (string) (false)") 00407 return false; 00408 */ 00409 00410 // 1.Prepare buffer 00411 short address = p_address; 00412 int length = -1; 00413 if (p_readLengthFirst) { // The string was stored with its length 00414 if (!Read(address, &length)) { // Read the length as integer in big endian mode 00415 DEBUG_ERROR("C24LCXX_I2C::Read (string): Failed to read length") 00416 return false; 00417 } 00418 wait(0.02); 00419 DEBUG("C24LCXX_I2C::Read (string): length=%d", length) 00420 if (length == 0) { 00421 DEBUG_ERROR("C24LCXX_I2C::Read (string): empty") 00422 return true; 00423 } 00424 address += 4; // Skip the length value size 00425 length -= 4; // length is the size of (string length + string) 00426 } else { // The string length is provided by p_length2write parameter 00427 if (p_length2write == -1) { 00428 length = p_string.size(); 00429 } else { 00430 length = p_length2write; 00431 p_string.resize(p_length2write); 00432 } 00433 } 00434 DEBUG("C24LCXX_I2C::Read (string): Address=0x%02x - Length=%d", address, length) 00435 00436 // 2. Memory address 00437 char i2cBuffer[2]; 00438 i2cBuffer[0] = (unsigned char)(address >> 8); 00439 DEBUG("C24LCXX_I2C::Read (string): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) 00440 i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); 00441 DEBUG("C24LCXX_I2C::Read (string): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) 00442 00443 // 3. Send I2C start + memory address with repeat start 00444 if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { 00445 wait(0.02); 00446 DEBUG("C24LCXX_I2C::Read (string): Write memory done") 00447 // 4. Read data + I2C stop 00448 char buffer[length]; 00449 int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length); 00450 if (result == 0) { 00451 p_string.assign(buffer, length); 00452 00453 return true; 00454 } 00455 } 00456 00457 DEBUG_LEAVE("C24LCXX_I2C::Read (string) (false)") 00458 return false; 00459 } 00460 00461 #if defined(__DEBUG) 00462 void C24LCXX_I2C::DumpMemoryArea(const int p_address, const int p_count) { 00463 DEBUG_ENTER("C24LCXX_I2C::DumpMemoryArea: %d - %d", p_address, p_count) 00464 00465 DEBUG("C24LCXX_I2C::DumpMemoryArea: Reading datas..."); 00466 std::vector<unsigned char> datas(p_count); 00467 if (!Read(p_address, datas, false)) { // Read bytes, including the lenght indication, buffer size is not set before the call 00468 std::cout << "C24LCXX_I2C::DumpMemoryArea: read failed\r" << std::endl; 00469 } else { 00470 std::cout << "C24LCXX_I2C::DumpMemoryArea: Read bytes:\r" << std::endl; 00471 HEXADUMP(&datas[0], p_count); 00472 std::cout << "\r" << std::endl; 00473 } 00474 } 00475 #endif // _DEBUG 00476 00477 } // End of namespace _24LCXX_I2C
Generated on Thu Jul 14 2022 07:01:12 by
1.7.2
24LCxx Serial EEPROM library