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
Revision 5:2f61885d8bc3, committed 2018-09-13
- Comitter:
- acesrobertm
- Date:
- Thu Sep 13 09:56:13 2018 -0500
- Parent:
- 4:650f3259bd4f
- Commit message:
- away auto close feature added and enabled readers 2 and 3
Changed in this revision
24LCxx_I2C.cpp | Show annotated file Show diff for this revision Revisions of this file |
24LCxx_I2C.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/24LCxx_I2C.cpp Wed Apr 25 09:37:34 2018 -0500 +++ b/24LCxx_I2C.cpp Thu Sep 13 09:56:13 2018 -0500 @@ -20,7 +20,7 @@ #include "24LCxx_I2C.h" namespace _24LCXX_I2C { - C24LCXX_I2C::C24LCXX_I2C(const PinName p_sda, const PinName p_scl, const unsigned char p_address, const unsigned int p_frequency) { + C24LCXX_I2C::C24LCXX_I2C(const PinName p_sda, const PinName p_scl, const uint8_t p_address, const uint32_t p_frequency) { _i2cInstance = new I2C(p_sda, p_scl); //, "24LCxx_I2C" _slaveAddress = (p_address << 1) | 0xa0; // Slave address format is: 1 0 1 0 A3 A2 A1 R/W @@ -32,13 +32,13 @@ _i2cInstance = NULL; } - bool C24LCXX_I2C::Write(const short p_address, const unsigned char p_byte) { + bool C24LCXX_I2C::Write(const uint16_t p_address, const uint8_t p_byte) { // 1.Prepare buffer char i2cBuffer[3]; // Memory address + one byte of data // 1.1. Memory address - short address = p_address; - i2cBuffer[0] = (unsigned char)(address >> 8); - i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); + uint16_t address = p_address; + i2cBuffer[0] = (uint8_t)(address >> 8); + i2cBuffer[1] = (uint8_t)((uint8_t)address & 0xff); // 1.2. Datas i2cBuffer[2] = p_byte; @@ -49,20 +49,20 @@ return (bool)(result == 0); } - bool C24LCXX_I2C::Write(const short p_address, const short p_short, const C24LCXX_I2C::Mode p_mode) { + bool C24LCXX_I2C::Write(const uint16_t p_address, const uint16_t p_short, const C24LCXX_I2C::Mode p_mode) { // 1.Prepare buffer char i2cBuffer[4]; // Memory address + one short (2 bytes) // 1.1. Memory address - short address = p_address; - i2cBuffer[0] = (unsigned char)(address >> 8); - i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); + uint16_t address = p_address; + i2cBuffer[0] = (uint8_t)(address >> 8); + i2cBuffer[1] = (uint8_t)((uint8_t)address & 0xff); // 1.2. Datas if (p_mode == BigEndian) { - i2cBuffer[2] = (unsigned char)(p_short >> 8); - i2cBuffer[3] = (unsigned char)((unsigned char)p_short & 0xff); + i2cBuffer[2] = (uint8_t)(p_short >> 8); + i2cBuffer[3] = (uint8_t)((uint8_t)p_short & 0xff); } else { - i2cBuffer[2] = (unsigned char)((unsigned char)p_short & 0xff); - i2cBuffer[3] = (unsigned char)(p_short >> 8); + i2cBuffer[2] = (uint8_t)((uint8_t)p_short & 0xff); + i2cBuffer[3] = (uint8_t)(p_short >> 8); } // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop @@ -72,24 +72,24 @@ return (bool)(result == 0); } - bool C24LCXX_I2C::Write(const short p_address, const int p_int, const C24LCXX_I2C::Mode p_mode) { + bool C24LCXX_I2C::Write(const uint16_t p_address, const uint32_t p_int, const C24LCXX_I2C::Mode p_mode) { // 1.Prepare buffer char i2cBuffer[6]; // Memory address + one integer (4 bytes) // 1.1. Memory address - short address = p_address; - i2cBuffer[0] = (unsigned char)(address >> 8); - i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); + uint16_t address = p_address; + i2cBuffer[0] = (uint8_t)(address >> 8); + i2cBuffer[1] = (uint8_t)((uint8_t)address & 0xff); // 1.2. Datas if (p_mode == BigEndian) { - i2cBuffer[2] = (unsigned char)(p_int >> 24); - i2cBuffer[3] = (unsigned char)(p_int >> 16); - i2cBuffer[4] = (unsigned char)(p_int >> 8); - i2cBuffer[5] = (unsigned char)((unsigned char)p_int & 0xff); + i2cBuffer[2] = (uint8_t)(p_int >> 24); + i2cBuffer[3] = (uint8_t)(p_int >> 16); + i2cBuffer[4] = (uint8_t)(p_int >> 8); + i2cBuffer[5] = (uint8_t)((uint8_t)p_int & 0xff); } else { - i2cBuffer[2] = (unsigned char)((unsigned char)p_int & 0xff); - i2cBuffer[3] = (unsigned char)(p_int >> 8); - i2cBuffer[4] = (unsigned char)(p_int >> 16); - i2cBuffer[5] = (unsigned char)(p_int >> 24); + i2cBuffer[2] = (uint8_t)((uint8_t)p_int & 0xff); + i2cBuffer[3] = (uint8_t)(p_int >> 8); + i2cBuffer[4] = (uint8_t)(p_int >> 16); + i2cBuffer[5] = (uint8_t)(p_int >> 24); } // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop @@ -99,12 +99,12 @@ return (bool)(result == 0); } - bool C24LCXX_I2C::Read(const short p_address, unsigned char * p_byte) { + bool C24LCXX_I2C::Read(const uint16_t p_address, uint8_t * p_byte) { // 1.Prepare buffer char i2cBuffer[2]; // 1.1. Memory address - i2cBuffer[0] = (unsigned char)(p_address >> 8); - i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); + i2cBuffer[0] = (uint8_t)(p_address >> 8); + i2cBuffer[1] = (uint8_t)((uint8_t)p_address & 0xff); // 2. Send I2C start + memory address if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { @@ -117,12 +117,12 @@ return false; } - bool C24LCXX_I2C::Read(const short p_address, short *p_short, const C24LCXX_I2C::Mode p_mode) { + bool C24LCXX_I2C::Read(const uint16_t p_address, uint16_t *p_short, const C24LCXX_I2C::Mode p_mode) { // 1.Prepare buffer char i2cBuffer[2]; // 1.1. Memory address - i2cBuffer[0] = (unsigned char)(p_address >> 8); - i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); + i2cBuffer[0] = (uint8_t)(p_address >> 8); + i2cBuffer[1] = (uint8_t)((uint8_t)p_address & 0xff); // 2. Send I2C start + memory address if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { @@ -130,9 +130,9 @@ int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 2); if (result == 0) { if (p_mode == BigEndian) { - *p_short = (short)(i2cBuffer[0] << 8 | i2cBuffer[1]); + *p_short = (uint16_t)(i2cBuffer[0] << 8 | i2cBuffer[1]); } else { - *p_short = (short)(i2cBuffer[1] << 8 | i2cBuffer[0]); + *p_short = (uint16_t)(i2cBuffer[1] << 8 | i2cBuffer[0]); } return true; @@ -142,12 +142,12 @@ return false; } - bool C24LCXX_I2C::Read(const short p_address, int *p_int, const C24LCXX_I2C::Mode p_mode) { + bool C24LCXX_I2C::Read(const uint16_t p_address, uint32_t *p_int, const C24LCXX_I2C::Mode p_mode) { // 1.Prepare buffer char i2cBuffer[4]; // 1.1. Memory address - i2cBuffer[0] = (unsigned char)(p_address >> 8); - i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); + i2cBuffer[0] = (uint8_t)(p_address >> 8); + i2cBuffer[1] = (uint8_t)((uint8_t)p_address & 0xff); // 2. Send I2C start + memory address if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) {
--- a/24LCxx_I2C.h Wed Apr 25 09:37:34 2018 -0500 +++ b/24LCxx_I2C.h Thu Sep 13 09:56:13 2018 -0500 @@ -40,15 +40,15 @@ BigEndian //<! Little Endian mode: 0xA0B70708 is stored as AO: MSB and 08 LSB }; public: - C24LCXX_I2C(const PinName p_sda, const PinName p_scl, const unsigned char p_address, const unsigned int p_frequency = 400000); + C24LCXX_I2C(const PinName p_sda, const PinName p_scl, const unsigned char p_address, const uint32_t p_frequency = 400000); virtual ~C24LCXX_I2C(); - bool Write(const short p_address, const unsigned char p_byte); - bool Write(const short p_address, const short p_short, const C24LCXX_I2C::Mode p_mode = BigEndian); - bool Write(const short p_address, const int p_int, const C24LCXX_I2C::Mode p_mode = BigEndian); - bool Read(const short p_address, unsigned char *p_value); - bool Read(const short p_address, short *p_short, C24LCXX_I2C::Mode p_mode = BigEndian); - bool Read(const short p_address, int *p_int, C24LCXX_I2C::Mode p_mode = BigEndian); + bool Write(const uint16_t p_address, const uint8_t p_byte); + bool Write(const uint16_t p_address, const uint16_t p_short, const C24LCXX_I2C::Mode p_mode = BigEndian); + bool Write(const uint16_t p_address, const uint32_t p_int, const C24LCXX_I2C::Mode p_mode = BigEndian); + bool Read(const uint16_t p_address, unsigned char *p_value); + bool Read(const uint16_t p_address, uint16_t *p_short, C24LCXX_I2C::Mode p_mode = BigEndian); + bool Read(const uint16_t p_address, uint32_t *p_int, C24LCXX_I2C::Mode p_mode = BigEndian); private: }; // End of class C24LCXX_I2C