Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

add eeprom methods to I2C service

Geo4927f082d856
diff --git a/src/Services/I2cService.cpp b/src/Services/I2cService.cpp
index 62b497b4..910a5286 100644
--- a/src/Services/I2cService.cpp
+++ b/src/Services/I2cService.cpp
@@ -469,4 +469,87 @@ void I2cService::randomClockPulseNoise(uint8_t scl, uint8_t sda, uint32_t freqHz
i2cBitBangSetLevel(sda, random(2));
delayMicroseconds(random(d));
}
-}
\ No newline at end of file
+}
+
+bool I2cService::initEeprom(uint16_t chipSizeKb, uint8_t addr) {
+ eeprom.setMemoryType(chipSizeKb);
+ return eeprom.begin(addr);
+}
+
+bool I2cService::eepromWriteByte(uint16_t address, uint8_t value) {
+ return eeprom.write(address, value);
+}
+
+uint8_t I2cService::eepromReadByte(uint16_t address) {
+ return eeprom.read(address);
+}
+
+bool I2cService::eepromPutString(uint32_t address, const std::string& str) {
+ String arduinoStr(str.c_str());
+ return eeprom.putString(address, arduinoStr) > 0;
+}
+
+bool I2cService::eepromGetString(uint32_t address, std::string& outStr) {
+ String arduinoStr;
+ eeprom.getString(address, arduinoStr);
+ outStr = std::string(arduinoStr.c_str());
+ return true;
+}
+
+void I2cService::eepromErase(uint8_t fill) {
+ eeprom.erase(fill);
+}
+
+bool I2cService::eepromDetectMemorySize() {
+ uint32_t size = eeprom.detectMemorySizeBytes();
+ if (size > 0) {
+ eeprom.setMemorySizeBytes(size);
+ return true;
+ }
+ return false;
+}
+
+uint8_t I2cService::eepromDetectAddressBytes() {
+ uint8_t bytes = eeprom.detectAddressBytes();
+ eeprom.setAddressBytes(bytes);
+ return bytes;
+}
+
+uint16_t I2cService::eepromDetectPageSize() {
+ uint16_t size = eeprom.detectPageSizeBytes();
+ eeprom.setPageSizeBytes(size);
+ return size;
+}
+
+uint8_t I2cService::eepromDetectWriteTime(uint8_t testCount) {
+ return eeprom.detectWriteTimeMs(testCount);
+}
+
+
+uint32_t I2cService::eepromLength() {
+ return eeprom.length();
+}
+
+uint32_t I2cService::eepromGetMemorySize() {
+ return eeprom.getMemorySizeBytes();
+}
+
+uint16_t I2cService::eepromPageSize() {
+ return eeprom.getPageSizeBytes();
+}
+
+uint8_t I2cService::eepromWriteTimeMs() {
+ return eeprom.getWriteTimeMs();
+}
+
+uint8_t I2cService::eepromAddressBytes() {
+ return eeprom.getAddressBytes();
+}
+
+bool I2cService::eepromIsConnected() {
+ return eeprom.isConnected();
+}
+
+bool I2cService::eepromIsBusy() {
+ return eeprom.isBusy();
+}
diff --git a/src/Services/I2cService.h b/src/Services/I2cService.h
index 5630690f..28bd4031 100644
--- a/src/Services/I2cService.h
+++ b/src/Services/I2cService.h
@@ -4,6 +4,7 @@
#include <Wire.h>
#include <vector>
#include "Models/ByteCode.h"
+#include <SparkFun_External_EEPROM.h>
class I2cService {
public:
@@ -51,13 +52,33 @@ public:
// Instructions
std::string executeByteCode(const std::vector<ByteCode>& bytecodes);
+ // EEPROM
+ bool initEeprom(uint16_t chipSizeKb = 512, uint8_t addr=0x50);
+ bool eepromWriteByte(uint16_t address, uint8_t value);
+ uint8_t eepromReadByte(uint16_t address);
+ bool eepromPutString(uint32_t address, const std::string& str);
+ bool eepromGetString(uint32_t address, std::string& outStr);
+ uint32_t eepromLength();
+ uint32_t eepromGetMemorySize();
+ uint16_t eepromPageSize();
+ uint8_t eepromWriteTimeMs();
+ uint8_t eepromAddressBytes();
+ bool eepromIsConnected();
+ bool eepromIsBusy();
+ void eepromErase(uint8_t fill = 0x00);
+ bool eepromDetectMemorySize();
+ uint8_t eepromDetectAddressBytes();
+ uint16_t eepromDetectPageSize();
+ uint8_t eepromDetectWriteTime(uint8_t testCount = 8);
+
+
private:
static std::vector<std::string> slaveLog;
static portMUX_TYPE slaveLogMux;
static uint8_t slaveResponseBuffer[16];
static size_t slaveResponseLength;
static I2cService* activeSlaveInstance;
-
+ ExternalEEPROM eeprom;
static void onSlaveReceive(int len);
static void onSlaveRequest();