Driver library for Microchip I2C EERAM (47x04 and 47x16) 4 kbit or 16 kbit EEPROM backed SRAM.
Diff: EERAM.cpp
- Revision:
- 2:bdbf9de0e985
- Parent:
- 1:6028bc22d82f
- Child:
- 3:a869096d7a5d
--- a/EERAM.cpp Tue Apr 25 16:22:53 2017 +0000 +++ b/EERAM.cpp Thu Apr 27 13:25:23 2017 +0000 @@ -2,7 +2,7 @@ * @file EERAM.cpp * @brief mbed driver for Microchip I2C EERAM devices (47x04 and 47x16) * @author Mark Peter Vargha, vmp@varghamarkpeter.hu -* @version 1.0.0 +* @version 1.1.0 * * Copyright (c) 2017 * @@ -36,6 +36,14 @@ _controlAddressRead ^= (-true ^ _controlAddressWrite) & (1 << RW_BIT); } +void EERAM::putAddressIntoBuffer(uint16_t address, char *data) +{ + MemoryAddress location; + location.address = address; + data[0] = location.bytes[1]; + data[1] = location.bytes[0]; +} + bool EERAM::checkAddressRange(uint16_t start, uint16_t length) { return start < _memorySize && start + length <= _memorySize && length > 0; @@ -56,6 +64,13 @@ return success; } +bool EERAM::write(char *data, int length) +{ + bool success = false; + success = _i2c.write(_sramAddressWrite, data, length) == 0; + return success; +} + bool EERAM::read(uint16_t address, char *data, int length) { bool success = checkAddressRange(address, length); @@ -68,6 +83,17 @@ return success; } +bool EERAM::read(char *data, int length) +{ + bool success = setMemoryPointer((uint8_t)data[1], (uint8_t)data[0], true); + if (success) + { + success = _i2c.read(_sramAddressRead, data + 2, length - 2, false) == 0; + } + _i2c.stop(); + return success; +} + bool EERAM::writeRegister(uint8_t registerAddress, uint8_t data) { _i2c.start(); @@ -227,13 +253,18 @@ bool EERAM::setMemoryPointer(uint16_t address, bool stop) { - int result = 0; MemoryAddress location; location.address = address; + return setMemoryPointer(location.bytes[0], location.bytes[1], stop); +} + +bool EERAM::setMemoryPointer(uint8_t address_0, uint8_t address_1, bool stop) +{ + int result = 0; _i2c.start(); result = _i2c.write(_sramAddressWrite); - if (result == 1) result = _i2c.write(location.bytes[1]); - if (result == 1) result = _i2c.write(location.bytes[0]); + if (result == 1) result = _i2c.write(address_1); + if (result == 1) result = _i2c.write(address_0); if (stop) _i2c.stop(); return result == 1; } @@ -259,7 +290,7 @@ void EERAM::dumpRegisters(Serial &serial) { - serial.printf("Control Register Contents\r\n"); + serial.printf("Status Register Contents\r\n"); serial.printf("Event detected: %d\r\n", isEventDetected()); serial.printf("Auto Store Enabled: %d\r\n", isAutoStoreEnabled()); serial.printf("Protected area: %d = ", getProtectedMemoryArea());