This class provides simplified I2C access to a Microchip 24LCxx Serial EEPROM device: - Rename the class (C2424 -> C24) - Add EraseMemoryArea method - Add DumpMemoryArea method only accessible in DEBUG mode - Add 'const' qualifier in parameters
Fork of 24LCxx_I2C by
Diff: 24LCxx_I2C.cpp
- Revision:
- 4:650f3259bd4f
- Parent:
- 3:5df584fbabfe
- Child:
- 5:2f61885d8bc3
diff -r 5df584fbabfe -r 650f3259bd4f 24LCxx_I2C.cpp --- a/24LCxx_I2C.cpp Mon Sep 25 18:59:44 2017 +0000 +++ b/24LCxx_I2C.cpp Wed Apr 25 09:37:34 2018 -0500 @@ -16,64 +16,20 @@ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include <iostream> -#include <sstream> - + #include "24LCxx_I2C.h" namespace _24LCXX_I2C { - - unsigned char C24LCXX_I2C::I2CModuleRefCounter = 0; - - 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("") { - if (C24LCXX_I2C::I2CModuleRefCounter != 0) { - error("C24LCXX_I2C: Wrong params"); - } -#ifdef __DEBUG - std::ostringstream out(std::ostringstream::out); - out << "C24LCXX_I2C #" << C24LCXX_I2C::I2CModuleRefCounter; - _internalId.assign(out.str()); - DEBUG("C24LCXX_I2C: _internalId='%s'", _internalId.c_str()) -#endif // __DEBUG + C24LCXX_I2C::C24LCXX_I2C(const PinName p_sda, const PinName p_scl, const unsigned char p_address, const unsigned int p_frequency) { + _i2cInstance = new I2C(p_sda, p_scl); //, "24LCxx_I2C" - C24LCXX_I2C::I2CModuleRefCounter += 1; - _slaveAddress = (p_address << 1) | 0xa0; // Slave address format is: 1 0 1 0 A3 A2 A1 R/W _i2cInstance->frequency(p_frequency); // Set the frequency of the I2C interface - if (p_wp != NC) { - _wp = new DigitalOut(p_wp); - _wp->write(0); // Disable write protect - } else { - _wp = NULL; // Not used - } } C24LCXX_I2C::~C24LCXX_I2C() { - // Release I2C instance - C24LCXX_I2C::I2CModuleRefCounter -= 1; - if (C24LCXX_I2C::I2CModuleRefCounter == 0) { - delete _i2cInstance; - _i2cInstance = NULL; - } - // Release _wp if required - if (_wp != NULL) { - _wp->write(0); - delete _wp; - } - } - - bool C24LCXX_I2C::WriteProtect(const bool p_writeProtect) { - if (_wp != NULL) { - _wp->write((int)(p_writeProtect)); - return true; - } - - return false; - } - - bool C24LCXX_I2C::EraseMemoryArea(const short p_startAddress, const int p_count, const unsigned char p_pattern) { - std::vector<unsigned char> eraseBuffer(p_count, p_pattern); - return Write(p_startAddress, eraseBuffer, false); + delete _i2cInstance; + _i2cInstance = NULL; } bool C24LCXX_I2C::Write(const short p_address, const unsigned char p_byte) { @@ -143,59 +99,6 @@ return (bool)(result == 0); } - bool C24LCXX_I2C::Write(const short p_address, const std::string & p_string, const bool p_storeLength, const int p_length2write) { - return Write(p_address, p_string.c_str(), p_storeLength, p_length2write); - } - - bool C24LCXX_I2C::Write(const short p_address, const std::vector<unsigned char> & p_datas, const bool p_storeLength, const int p_length2write) { - int length = (p_length2write == -1) ? p_datas.size() : p_length2write; - unsigned char array[length]; - std::copy(p_datas.begin(), p_datas.end(), array); - bool result = Write(p_address, array, p_storeLength, length); - wait(0.02); - - return result; - } - - bool C24LCXX_I2C::Write(const short p_address, const char *p_datas, const bool p_storeLength, const int p_length2write) { - // 1.Prepare buffer - int length = (p_length2write == -1) ? strlen(p_datas) : p_length2write; - if (p_storeLength) { - length += 4; // Add four bytes for the length as integer - } - - char i2cBuffer[2 + length]; - // 1.1. Memory address - short address = p_address; - i2cBuffer[0] = (unsigned char)(address >> 8); - i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); - // 1.2. Datas - if (p_storeLength) { - // Fill the length - i2cBuffer[2] = (unsigned char)(length >> 24); - i2cBuffer[3] = (unsigned char)(length >> 16); - i2cBuffer[4] = (unsigned char)(length >> 8); - i2cBuffer[5] = (unsigned char)((unsigned char)length & 0xff); - for (int i = 0; i < length - 4; i++) { - i2cBuffer[6 + i] = *(p_datas + i); - } - } else { // The length was not stored - for (int i = 0; i < length; i++) { - i2cBuffer[2 + i] = *(p_datas + i); - } - } - - // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop - int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 2 + length); - wait(0.02); - - return (bool)(result == 0); - } - - bool C24LCXX_I2C::Write(const short p_address, const unsigned char *p_datas, const bool p_storeLength, const int p_length2write) { - return Write(p_address, (const char *)p_datas, p_storeLength, p_length2write); - } - bool C24LCXX_I2C::Read(const short p_address, unsigned char * p_byte) { // 1.Prepare buffer char i2cBuffer[2]; @@ -262,113 +165,4 @@ } return false; } - - bool C24LCXX_I2C::Read(const short p_address, std::vector<unsigned char> & p_datas, const bool p_readLengthFirst, const int p_length2write) { - // 1.Prepare buffer - short address = p_address; - int length = 0; - if (p_readLengthFirst) { - if (!Read(address, &length)) { // Read the length in big endian mode - return false; - } - if (length == 0) { - return true; - } - address += 4; // Skip the length value - length -= 4; // length is the size of (string length + string) - } else { - if (p_length2write == -1) { - length = p_datas.size(); - } else { - length = p_length2write; - } - } - - // 2. Memory address - char i2cBuffer[2]; - i2cBuffer[0] = (unsigned char)(address >> 8); - i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); - - // 3. Send I2C start + memory address - if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { - // 4. read data + I2C stop - unsigned char buffer[length]; - int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length); - - if (result == 0) { - p_datas.assign(buffer, buffer + length); - return (bool)(result == 0); - } - } - - return false; - } - - bool C24LCXX_I2C::Read(const short p_address, std::string & p_string, const bool p_readLengthFirst, const int p_length2write) { -/* std::vector<unsigned char> datas; - if (Read(p_address, datas, p_readLengthFirst, p_length2write) == true) { - p_string.assign((char *)datas.begin(), datas.size()); - - return true; - } - return false; -*/ - - // 1.Prepare buffer - short address = p_address; - int length = -1; - if (p_readLengthFirst) { // The string was stored with its length - if (!Read(address, &length)) { // Read the length as integer in big endian mode - return false; - } - if (length == 0) { - return true; - } - address += 4; // Skip the length value size - length -= 4; // length is the size of (string length + string) - } else { // The string length is provided by p_length2write parameter - if (p_length2write == -1) { - length = p_string.size(); - } else { - length = p_length2write; - p_string.resize(p_length2write); - } - } - - // 2. Memory address - char i2cBuffer[2]; - i2cBuffer[0] = (unsigned char)(address >> 8); - i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); - - // 3. Send I2C start + memory address with repeat start - if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { - // 4. Read data + I2C stop - char buffer[length]; - int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length); - if (result == 0) { - p_string.assign(buffer, length); - - return true; - } - } - - return false; - } - -#if defined(__DEBUG) - void C24LCXX_I2C::DumpMemoryArea(const int p_address, const int p_count) { - DEBUG_ENTER("C24LCXX_I2C::DumpMemoryArea: %d - %d", p_address, p_count) - - DEBUG("C24LCXX_I2C::DumpMemoryArea: Reading datas..."); - std::vector<unsigned char> datas(p_count); - if (!Read(p_address, datas, false)) { // Read bytes, including the lenght indication, buffer size is not set before the call - std::cout << "C24LCXX_I2C::DumpMemoryArea: read failed\r" << std::endl; - } else { - std::cout << "C24LCXX_I2C::DumpMemoryArea: Read bytes:\r" << std::endl; - HEXADUMP(&datas[0], p_count); - std::cout << "\r" << std::endl; - } - } -#endif // _DEBUG - } // End of namespace _24LCXX_I2C