Class to provide simple access to I2C EEPROM chiles like Microchip's 24LC range or AMTELS AT24C range. Chips up to 64Kb in size are directly supported.
Fork of I2CEeprom by
Revision 2:b7877755371e, committed 2018-01-31
- Comitter:
- amateusz
- Date:
- Wed Jan 31 11:54:02 2018 +0000
- Parent:
- 1:b23f5561266c
- Commit message:
- Do not pass SDA & SCL pins but rather already-existing I2C object. It makes more sense when you have multiple devices on the bus (as you are supposed to in case of I2C anyway)
Changed in this revision
I2CEeprom.cpp | Show annotated file Show diff for this revision Revisions of this file |
I2CEeprom.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r b23f5561266c -r b7877755371e I2CEeprom.cpp --- a/I2CEeprom.cpp Sun Jul 19 09:34:04 2015 +0000 +++ b/I2CEeprom.cpp Wed Jan 31 11:54:02 2018 +0000 @@ -15,13 +15,13 @@ */ #include "I2CEeprom.h" -I2CEeprom::I2CEeprom(PinName sda, PinName scl, int address, size_t pageSize, size_t chipSize, int busSpeed): - m_i2c(sda, scl), +I2CEeprom::I2CEeprom(I2C *i2c_inst, int address, size_t pageSize, size_t chipSize, int busSpeed): + m_i2c(i2c_inst), m_i2cAddress(address), m_chipSize(chipSize), m_pageSize(pageSize) { - m_i2c.frequency(busSpeed); + m_i2c->frequency(busSpeed); } size_t I2CEeprom::read(size_t address, char &value) { @@ -30,8 +30,8 @@ return 0; char values[] = { (address >> 8), (address & 0xFF) }; - if (m_i2c.write(m_i2cAddress, values, 2) == 0) { - if (m_i2c.read(m_i2cAddress, &value, 1) == 0) { + if (m_i2c->write(m_i2cAddress, values, 2) == 0) { + if (m_i2c->read(m_i2cAddress, &value, 1) == 0) { return 1; } } @@ -45,8 +45,8 @@ return 0; char values[] = { (address >> 8), (address & 0xFF) }; - if (m_i2c.write(m_i2cAddress, values, 2) == 0) { - if (m_i2c.read(m_i2cAddress, buffer, size) == 0) { + if (m_i2c->write(m_i2cAddress, values, 2) == 0) { + if (m_i2c->read(m_i2cAddress, buffer, size) == 0) { return size; } } @@ -60,7 +60,7 @@ return 0; char values[] = { (address >> 8), (address & 0xFF), value }; - if (m_i2c.write(m_i2cAddress, values, 3) != 0) { + if (m_i2c->write(m_i2cAddress, values, 3) != 0) { return 0; } @@ -99,7 +99,7 @@ //printf("Writing [%.*s] at %d size %d\n\r", toWrite, page, address, toWrite); // Start the page write with the addres ine one write call. char values[] = { (address >> 8), (address & 0xFF) }; - if (m_i2c.write(m_i2cAddress, values, 2, true) != 0) { + if (m_i2c->write(m_i2cAddress, values, 2, true) != 0) { // Write failed to return bytes written so far. return size - left; } @@ -107,7 +107,7 @@ // Write the bytes out one at a time to avoid having to copy them to // another buffer. for (int count = 0; count != toWrite; ++count) { - if (m_i2c.write(*page) == 0) { + if (m_i2c->write(*page) == 0) { // Write failed to return bytes written so far. return size - left; } @@ -116,7 +116,7 @@ // Stop the transaction now we've completed the page // write. - m_i2c.stop(); + m_i2c->stop(); waitForWrite(); @@ -152,16 +152,16 @@ //printf("Writing %d at %d size %d\n\r", value, address, toWrite); char values[] = { (address >> 8), (address & 0xFF) }; - if (m_i2c.write(m_i2cAddress, values, 2, true) != 0) { + if (m_i2c->write(m_i2cAddress, values, 2, true) != 0) { return size - left; } for (int count = 0; count != toWrite; ++count) { - if (m_i2c.write(value) == 0) + if (m_i2c->write(value) == 0) return size - left; } - m_i2c.stop(); + m_i2c->stop(); waitForWrite(); @@ -176,7 +176,7 @@ // The chip doesn't ACK while writing to the actual EEPROM // so loop trying to do a zero byte write until it is ACKed // by the chip. - while (m_i2c.write(m_i2cAddress, 0, 0) != 0) { + while (m_i2c->write(m_i2cAddress, 0, 0) != 0) { // Wait for ack. wait_ms(1); }
diff -r b23f5561266c -r b7877755371e I2CEeprom.h --- a/I2CEeprom.h Sun Jul 19 09:34:04 2015 +0000 +++ b/I2CEeprom.h Wed Jan 31 11:54:02 2018 +0000 @@ -30,14 +30,13 @@ class I2CEeprom { public: /// Constructor to create a new instance of the class. - /// @param sda The pin name for the sda line of the I2C bus. - /// @param scl The pin name for the scl line of the I2C bus. + /// @param i2c_inst The I2C bus instance. Pass a reference. /// @param address The 8bit I2C address of the chip in the range 0xA0 - 0xAE. /// @param pageSize The size of the page used in writing to the chip. /// @param chipSize The size of the memory in the chip to allow range checkng. Set to /// 0 to disable checks. /// @param busSpeed The frequency of the I2C bus defaults to 400K. - I2CEeprom(PinName sda, PinName scl, int address, size_t pageSize, size_t chipSize, int busSpeed = 400000); + I2CEeprom(I2C *i2c_inst, int address, size_t pageSize, size_t chipSize, int busSpeed = 400000); /// Read a single byte from the address in memory. /// @param address Memory address to read from. @@ -99,7 +98,7 @@ bool checkSpace(size_t address, size_t size); private: - I2C m_i2c; + I2C *m_i2c; int m_i2cAddress; size_t m_chipSize; size_t m_pageSize;