Update current library to accommodate small devices.
Diff: I2CEEPROM.cpp
- Revision:
- 7:3e1f751bcc81
- Parent:
- 6:8dc89c3b4718
- Child:
- 8:e92faf840345
diff -r 8dc89c3b4718 -r 3e1f751bcc81 I2CEEPROM.cpp --- a/I2CEEPROM.cpp Sun Oct 16 14:33:09 2016 +0000 +++ b/I2CEEPROM.cpp Fri Jan 13 14:45:55 2017 +0000 @@ -29,12 +29,20 @@ // Check the address and size fit onto the chip. if (!checkSpace(address, 1)) return 0; - - char i2caddr = (m_i2cAddress | address >> 7) & 0xFF; - char values[] = { (address & 0xFF) }; - if (m_i2c.write(i2caddr, values, 1) == 0) { - if (m_i2c.read(m_i2cAddress, &value, 1) == 0) { - return 1; + if(m_chipSize == 256 || m_chipSize == 128 || m_chipSize == 16) { + char values[] = { (address & 0xFF) }; + if (m_i2c.write(m_i2cAddress, values, 1) == 0) { + if (m_i2c.read(m_i2cAddress, &value, 1) == 0) { + return 1; + } + } + } + else { + char values[] = { (address >> 8), (address & 0xFF) }; + if (m_i2c.write(m_i2cAddress, values, 2) == 0) { + if (m_i2c.read(m_i2cAddress, &value, 1) == 0) { + return 1; + } } } @@ -46,11 +54,20 @@ if (!checkSpace(address, size)) return 0; - char i2caddr = (m_i2cAddress | address >> 7) & 0xFF; - char values[] = { (address & 0xFF) }; - if (m_i2c.write(i2caddr, values, 1) == 0) { - if (m_i2c.read(i2caddr, buffer, size) == 0) { - return size; + if(m_chipSize == 256 || m_chipSize == 128 || m_chipSize == 16) { + char values[] = { (address & 0xFF) }; + if (m_i2c.write(m_i2cAddress, values, 1) == 0) { + if (m_i2c.read(m_i2cAddress, buffer, size) == 0) { + return size; + } + } + } + else { + char values[] = { (address >> 8), (address & 0xFF) }; + if (m_i2c.write(m_i2cAddress, values, 2) == 0) { + if (m_i2c.read(m_i2cAddress, buffer, size) == 0) { + return size; + } } } @@ -62,10 +79,17 @@ if (!checkSpace(address, 1)) return 0; - char i2caddr = (m_i2cAddress | address >> 7) & 0xFF; - char values[] = { (address & 0xFF), value }; - if (m_i2c.write(i2caddr, values, 2) != 0) { - return 0; + if(m_chipSize == 256 || m_chipSize == 128 || m_chipSize == 16) { + char values[] = { (address & 0xFF), value }; + if (m_i2c.write(m_i2cAddress, values, 2) != 0) { + return 0; + } + } + else { + char values[] = { (address >> 8), (address & 0xFF), value }; + if (m_i2c.write(m_i2cAddress, values, 3) != 0) { + return 0; + } } waitForWrite(); @@ -75,9 +99,8 @@ size_t I2CEEPROM::write(size_t address, const char *buffer, size_t size) { // Check the address and size fit onto the chip. - if (!checkSpace(address, size)) { + if (!checkSpace(address, size)) return 0; - } const char *page = buffer; size_t left = size; @@ -101,25 +124,44 @@ } } - //pc.printf("Writing [%.*s] at %d size %d\n\r", toWrite, page, address, toWrite); - // Start the page write with the address in one write call. + //printf("Writing [%.*s] at %d size %d\n\r", toWrite, page, address, toWrite); + // Start the page write with the addres ine one write call. + if(m_chipSize == 256 || m_chipSize == 128 || m_chipSize == 16) { + char values[] = { (address & 0xFF) }; + if (m_i2c.write(m_i2cAddress, values, 1, true) != 0) { + // Write failed to return bytes written so far. + return size - left; + } + } + else { + char values[] = { (address >> 8), (address & 0xFF) }; + if (m_i2c.write(m_i2cAddress, values, 2, true) != 0) { + // Write failed to return bytes written so far. + return size - left; + } + } + + // Write the bytes out one at a time to avoid having to copy them to + // another buffer. for (int count = 0; count != toWrite; ++count) { - int addr = address + count; - char i2caddr = (m_i2cAddress | addr >> 7) & 0xFF; - char values[] = { (addr & 0xFF), *page }; - if (m_i2c.write(i2caddr, values, 2) != 0) { + if (m_i2c.write(*page) == 0) { // Write failed to return bytes written so far. return size - left; } ++page; - waitForWrite(); } + // Stop the transaction now we've completed the page + // write. + m_i2c.stop(); + + waitForWrite(); + // Update the counters with the amount we've just written left -= toWrite; address += toWrite; } - + return size; } @@ -129,10 +171,9 @@ return 0; size_t left = size; - + while (left != 0) { - //i2c.start(); - int toWrite; + size_t toWrite; if ((address % m_pageSize) != 0) { toWrite = (((address / m_pageSize) + 1) * m_pageSize) - address; if (toWrite > size) { @@ -147,19 +188,28 @@ } //printf("Writing %d at %d size %d\n\r", value, address, toWrite); - // Start the page write with the address in one write call. - for (int count = 0; count != toWrite; ++count) { - int addr = address + count; - char i2caddr = (m_i2cAddress | addr >> 7) & 0xFF; - char values[] = { (addr & 0xFF), value }; - if (m_i2c.write(i2caddr, values, 2) != 0) { - // Write failed to return bytes written so far. + if(m_chipSize == 256 || m_chipSize == 128 || m_chipSize == 16) { + char values[] = { (address & 0xFF) }; + if (m_i2c.write(m_i2cAddress, values, 1, true) != 0) { + return size - left; + } + } + else { + char values[] = { (address >> 8), (address & 0xFF) }; + if (m_i2c.write(m_i2cAddress, values, 2, true) != 0) { return size - left; } - waitForWrite(); } - // Update the counters with the amount we've just written + for (int count = 0; count != toWrite; ++count) { + if (m_i2c.write(value) == 0) + return size - left; + } + + m_i2c.stop(); + + waitForWrite(); + left -= toWrite; address += toWrite; } @@ -171,7 +221,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, 1) != 0) { + while (m_i2c.write(m_i2cAddress, 0, 0) != 0) { // Wait for ack. wait_ms(1); } @@ -190,4 +240,3 @@ return true; } -